-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathindex.js
159 lines (133 loc) · 3.43 KB
/
index.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict'
const errCode = require('err-code')
const { CID } = require('multiformats/cid')
const resolve = require('./resolvers')
const last = require('it-last')
/**
* @typedef {import('ipfs-unixfs').UnixFS} UnixFS
* @typedef {import('interface-blockstore').Blockstore} Blockstore
* @typedef {import('./types').ExporterOptions} ExporterOptions
* @typedef {import('./types').UnixFSFile} UnixFSFile
* @typedef {import('./types').UnixFSDirectory} UnixFSDirectory
* @typedef {import('./types').ObjectNode} ObjectNode
* @typedef {import('./types').RawNode} RawNode
* @typedef {import('./types').IdentityNode} IdentityNode
* @typedef {import('./types').UnixFSEntry} UnixFSEntry
*/
const toPathComponents = (path = '') => {
// split on / unless escaped with \
return (path
.trim()
.match(/([^\\^/]|\\\/)+/g) || [])
.filter(Boolean)
}
/**
* @param {string|Uint8Array|CID} path
*/
const cidAndRest = (path) => {
if (path instanceof Uint8Array) {
return {
cid: CID.decode(path),
toResolve: []
}
}
const cid = CID.asCID(path)
if (cid) {
return {
cid,
toResolve: []
}
}
if (typeof path === 'string') {
if (path.indexOf('/ipfs/') === 0) {
path = path.substring(6)
}
const output = toPathComponents(path)
return {
cid: CID.parse(output[0]),
toResolve: output.slice(1)
}
}
throw errCode(new Error(`Unknown path type ${path}`), 'ERR_BAD_PATH')
}
/**
* @param {string | CID} path
* @param {Blockstore} blockstore
* @param {ExporterOptions} [options]
*/
async function * walkPath (path, blockstore, options = {}) {
let {
cid,
toResolve
} = cidAndRest(path)
let name = cid.toString()
let entryPath = name
const startingDepth = toResolve.length
while (true) {
const result = await resolve(cid, name, entryPath, toResolve, startingDepth, blockstore, options)
if (!result.entry && !result.next) {
throw errCode(new Error(`Could not resolve ${path}`), 'ERR_NOT_FOUND')
}
if (result.entry) {
yield result.entry
}
if (!result.next) {
return
}
// resolve further parts
toResolve = result.next.toResolve
cid = result.next.cid
name = result.next.name
entryPath = result.next.path
}
}
/**
* @param {string | CID} path
* @param {Blockstore} blockstore
* @param {ExporterOptions} [options]
*/
async function exporter (path, blockstore, options = {}) {
const result = await last(walkPath(path, blockstore, options))
if (!result) {
throw errCode(new Error(`Could not resolve ${path}`), 'ERR_NOT_FOUND')
}
return result
}
/**
* @param {string | CID} path
* @param {Blockstore} blockstore
* @param {ExporterOptions} [options]
*/
async function * recursive (path, blockstore, options = {}) {
const node = await exporter(path, blockstore, options)
if (!node) {
return
}
yield node
if (node.type === 'directory') {
for await (const child of recurse(node, options)) {
yield child
}
}
/**
* @param {UnixFSDirectory} node
* @param {ExporterOptions} options
* @returns {AsyncGenerator<UnixFSEntry, void, any>}
*/
async function * recurse (node, options) {
for await (const file of node.content(options)) {
yield file
if (file instanceof Uint8Array) {
continue
}
if (file.type === 'directory') {
yield * recurse(file, options)
}
}
}
}
module.exports = {
exporter,
walkPath,
recursive
}