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
/
ls.js
91 lines (72 loc) · 2.44 KB
/
ls.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* eslint max-nested-callbacks: ["error", 8] */
'use strict'
const { parallelMap } = require('streaming-iterables')
const CID = require('cids')
const { resolvePath } = require('../../utils')
const PinManager = require('./pin-manager')
const { PinTypes } = PinManager
const PIN_LS_CONCURRENCY = 8
module.exports = ({ pinManager, object }) => {
return async function * ls (paths, options) {
options = options || {}
let type = PinTypes.all
if (paths && paths.type) {
options = paths
paths = null
}
if (options.type) {
type = options.type
if (typeof options.type === 'string') {
type = options.type.toLowerCase()
}
const err = PinManager.checkPinType(type)
if (err) {
throw err
}
}
if (paths) {
paths = Array.isArray(paths) ? paths : [paths]
// check the pinned state of specific hashes
const cids = await resolvePath(object, paths)
yield * parallelMap(PIN_LS_CONCURRENCY, async cid => {
const { reason, pinned } = await pinManager.isPinnedWithType(cid, type)
if (!pinned) {
throw new Error(`path '${paths[cids.indexOf(cid)]}' is not pinned`)
}
if (reason === PinTypes.direct || reason === PinTypes.recursive) {
return { cid, type: reason }
}
return { cid, type: `${PinTypes.indirect} through ${reason}` }
}, cids)
return
}
// show all pinned items of type
let pins = []
if (type === PinTypes.direct || type === PinTypes.all) {
pins = pins.concat(
Array.from(pinManager.directPins).map(cid => ({
type: PinTypes.direct,
cid: new CID(cid)
}))
)
}
if (type === PinTypes.recursive || type === PinTypes.all) {
pins = pins.concat(
Array.from(pinManager.recursivePins).map(cid => ({
type: PinTypes.recursive,
cid: new CID(cid)
}))
)
}
if (type === PinTypes.indirect || type === PinTypes.all) {
const indirects = await pinManager.getIndirectKeys(options)
pins = pins
// if something is pinned both directly and indirectly,
// report the indirect entry
.filter(({ cid }) => !indirects.includes(cid.toString()) || !pinManager.directPins.has(cid.toString()))
.concat(indirects.map(cid => ({ type: PinTypes.indirect, cid: new CID(cid) })))
}
// FIXME: https://github.com/ipfs/js-ipfs/issues/2244
yield * pins
}
}