This repository has been 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
/
rm.js
67 lines (54 loc) · 1.88 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
67
'use strict'
const errCode = require('err-code')
const multibase = require('multibase')
const { parallelMap, collect } = require('streaming-iterables')
const pipe = require('it-pipe')
const { resolvePath } = require('../utils')
const { PinTypes } = require('./pin/pin-manager')
const PIN_RM_CONCURRENCY = 8
module.exports = ({ pinManager, gcLock, object }) => {
return async function rm (paths, options) {
options = options || {}
const recursive = options.recursive !== false
if (options.cidBase && !multibase.names.includes(options.cidBase)) {
throw errCode(new Error('invalid multibase'), 'ERR_INVALID_MULTIBASE')
}
const cids = await resolvePath(object, paths)
const release = await gcLock.readLock()
try {
// verify that each hash can be unpinned
const results = await pipe(
cids,
parallelMap(PIN_RM_CONCURRENCY, async cid => {
const res = await pinManager.isPinnedWithType(cid, PinTypes.all)
const { pinned, reason } = res
const key = cid.toBaseEncodedString()
if (!pinned) {
throw new Error(`${key} is not pinned`)
}
if (reason !== PinTypes.recursive && reason !== PinTypes.direct) {
throw new Error(`${key} is pinned indirectly under ${reason}`)
}
if (reason === PinTypes.recursive && !recursive) {
throw new Error(`${key} is pinned recursively`)
}
return key
}),
collect
)
// update the pin sets in memory
results.forEach(key => {
if (recursive && pinManager.recursivePins.has(key)) {
pinManager.recursivePins.delete(key)
} else {
pinManager.directPins.delete(key)
}
})
// persist updated pin sets to datastore
await pinManager.flushPins()
return results
} finally {
release()
}
}
}