This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathrm.js
66 lines (55 loc) · 1.68 KB
/
rm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import errCode from 'err-code'
import parallel from 'it-parallel'
import map from 'it-map'
import filter from 'it-filter'
import { pipe } from 'it-pipe'
import { cleanCid } from './utils.js'
import { withTimeoutOption } from 'ipfs-core-utils/with-timeout-option'
const BLOCK_RM_CONCURRENCY = 8
/**
* @param {Object} config
* @param {import('ipfs-repo').IPFSRepo} config.repo
*/
export function createRm ({ repo }) {
/**
* @type {import('ipfs-core-types/src/block').API["rm"]}
*/
async function * rm (cids, options = {}) {
if (!Array.isArray(cids)) {
cids = [cids]
}
// We need to take a write lock here to ensure that adding and removing
// blocks are exclusive operations
const release = await repo.gcLock.writeLock()
try {
yield * pipe(
cids,
source => map(source, cid => {
return async () => {
cid = cleanCid(cid)
/** @type {import('ipfs-core-types/src/block').RmResult} */
const result = { cid }
try {
const has = await repo.blocks.has(cid)
if (!has) {
throw errCode(new Error('block not found'), 'ERR_BLOCK_NOT_FOUND')
}
await repo.blocks.delete(cid)
} catch (/** @type {any} */ err) {
if (!options.force) {
err.message = `cannot remove ${cid}: ${err.message}`
result.error = err
}
}
return result
}
}),
source => parallel(source, BLOCK_RM_CONCURRENCY),
source => filter(source, () => !options.quiet)
)
} finally {
release()
}
}
return withTimeoutOption(rm)
}