This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
436 lines (374 loc) · 12.5 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
'use strict'
const Block = require('ipld-block')
const CID = require('cids')
const mergeOptions = require('merge-options')
const ipldDagCbor = require('ipld-dag-cbor')
const ipldDagPb = require('ipld-dag-pb')
const ipldRaw = require('ipld-raw')
const multicodec = require('multicodec')
// @ts-ignore no types
const typical = require('typical')
const { extendIterator } = require('./util')
/**
* @typedef {import('interface-ipld-format').Format<object>} IPLDFormat
* @typedef {import('multicodec').CodecCode} CodecCode
* @typedef {import('./types').LoadFormatFn} LoadFormatFn
* @typedef {import('./types').Options} Options
* @typedef {import('./types').PutOptions} PutOptions
* @typedef {import('./types').GetOptions} GetOptions
* @typedef {import('./types').ResolveOptions} ResolveOptions
* @typedef {import('./types').RemoveOptions} RemoveOptions
* @typedef {import('./types').TreeOptions} TreeOptions
*/
class IPLDResolver {
/**
* @param {Options} userOptions
*/
constructor (userOptions) {
const options = mergeOptions(IPLDResolver.defaultOptions, userOptions)
if (!options.blockService) {
throw new Error('Missing blockservice')
}
this.bs = options.blockService
// Object with current list of active resolvers
/** @type {{ [key: number]: IPLDFormat}} */
this.resolvers = {}
if (typeof options.loadFormat !== 'function') {
/**
* @type {LoadFormatFn}
*/
this.loadFormat = (codec) => {
const codecName = multicodec.getNameFromCode(codec)
throw new Error(`No resolver found for codec "${codecName}"`)
}
} else {
this.loadFormat = options.loadFormat
}
// Enable all supplied formats
for (const format of options.formats) {
this.addFormat(format)
}
}
/**
* Add support for an IPLD Format.
*
* @param {IPLDFormat} format - The implementation of an IPLD Format.
*/
addFormat (format) {
const codec = format.codec
if (this.resolvers[format.codec]) {
const codecName = multicodec.getNameFromCode(codec)
throw new Error(`Resolver already exists for codec "${codecName}"`)
}
this.resolvers[codec] = format
return this
}
/**
* Remove support for an IPLD Format.
*
* @param {multicodec.CodecCode} codec - The codec of the IPLD Format to remove.
*/
removeFormat (codec) {
if (this.resolvers[codec]) {
delete this.resolvers[codec]
}
return this
}
/**
* Retrieves IPLD Nodes along the `path` that is rooted at `cid`.
*
* @param {CID} cid - the CID the resolving starts.
* @param {string} path - the path that should be resolved.
* @param {ResolveOptions} [options]
*/
resolve (cid, path, options) {
if (!CID.isCID(cid)) {
throw new Error('`cid` argument must be a CID')
}
if (typeof path !== 'string') {
throw new Error('`path` argument must be a string')
}
const ipld = this
const generator = async function * () {
// End iteration if there isn't a CID to follow any more
while (true) {
const format = await ipld.getFormat(multicodec.getCodeFromName(cid.codec))
// get block
// use local resolver
// update path value
const block = await ipld.bs.get(cid, options)
const result = format.resolver.resolve(block.data, path)
// Prepare for the next iteration if there is a `remainderPath`
path = result.remainderPath
let value = result.value
// NOTE vmx 2018-11-29: Not all IPLD Formats return links as
// CIDs yet. Hence try to convert old style links to CIDs
if (Object.keys(value).length === 1 && '/' in value) {
try {
value = new CID(value['/'])
} catch (_error) {
value = null
}
}
yield {
remainderPath: path,
value
}
if (CID.isCID(value)) {
cid = value
} else {
return
}
}
}
return extendIterator(generator())
}
/**
* Get a node by CID.
*
* @param {CID} cid - The CID of the IPLD Node that should be retrieved.
* @param {GetOptions} [options]
*/
async get (cid, options) {
const block = await this.bs.get(cid, options)
const format = await this.getFormat(block.cid.codec)
const node = format.util.deserialize(block.data)
return node
}
/**
* Get multiple nodes back from an array of CIDs.
*
* @param {Iterable.<CID>} cids - The CIDs of the IPLD Nodes that should be retrieved.
* @param {GetOptions} [options]
*/
getMany (cids, options) {
if (!typical.isIterable(cids) || typeof cids === 'string' ||
cids instanceof Uint8Array) {
throw new Error('`cids` must be an iterable of CIDs')
}
const ipld = this
const generator = async function * () {
for await (const cid of cids) {
yield ipld.get(cid, options)
}
}
return extendIterator(generator())
}
/**
* Stores the given IPLD Node of a recognized IPLD Format.
*
* @param {Object} node - The deserialized IPLD node that should be inserted.
* @param {CodecCode} format - The multicodec of the format that IPLD Node should be encoded in.
* @param {PutOptions} [userOptions] - Options is an object with the following properties.
*/
async put (node, format, userOptions) {
if (format === undefined) {
throw new Error('`put` requires a format')
}
if (typeof format !== 'number') {
throw new Error('`format` parameter must be number (multicodec)')
}
const formatImpl = await this.getFormat(format)
const defaultOptions = {
hashAlg: formatImpl.defaultHashAlg,
cidVersion: 1,
onlyHash: false
}
const options = mergeOptions(defaultOptions, userOptions)
const cidOptions = {
cidVersion: options.cidVersion,
hashAlg: options.hashAlg,
onlyHash: options.onlyHash
}
const serialized = formatImpl.util.serialize(node)
const cid = await formatImpl.util.cid(serialized, cidOptions)
if (!options.onlyHash) {
const block = new Block(serialized, cid)
await this.bs.put(block, options)
}
return cid
}
/**
* Stores the given IPLD Nodes of a recognized IPLD Format.
*
* @param {Iterable<any>} nodes - Deserialized IPLD nodes that should be inserted.
* @param {CodecCode} format - The multicodec of the format that IPLD Node should be encoded in.
* @param {PutOptions} [userOptions] - Options are applied to any of the `nodes` and is an object with the following properties.
*/
putMany (nodes, format, userOptions) {
if (!typical.isIterable(nodes) || typeof nodes === 'string' ||
nodes instanceof Uint8Array) {
throw new Error('`nodes` must be an iterable')
}
if (format === undefined) {
throw new Error('`put` requires a format')
}
if (typeof format !== 'number') {
throw new Error('`format` parameter must be number (multicodec)')
}
/** @type {PutOptions} */
let options
/** @type {IPLDFormat} */
let formatImpl
const ipld = this
const generator = async function * () {
for await (const node of nodes) {
// Lazy load the options not when the iterator is initialized, but
// when we hit the first iteration. This way the constructor can be
// a synchronous function.
if (options === undefined) {
formatImpl = await ipld.getFormat(format)
const defaultOptions = {
hashAlg: formatImpl.defaultHashAlg,
cidVersion: 1,
onlyHash: false
}
options = mergeOptions(defaultOptions, userOptions)
}
yield ipld.put(node, format, options)
}
}
return extendIterator(generator())
}
/**
* Remove an IPLD Node by the given CID.
*
* @param {CID} cid - The CID of the IPLD Node that should be removed.
* @param {RemoveOptions} [options]
*/
async remove (cid, options) { // eslint-disable-line require-await
return this.bs.delete(cid, options)
}
/**
* Remove IPLD Nodes by the given CIDs.
*
* Throws an error if any of the Blocks can’t be removed. This operation is
* *not* atomic, some Blocks might have already been removed.
*
* @param {Iterable<CID>} cids - The CIDs of the IPLD Nodes that should be removed.
* @param {RemoveOptions} [options]
*/
removeMany (cids, options) {
if (!typical.isIterable(cids) || typeof cids === 'string' ||
cids instanceof Uint8Array) {
throw new Error('`cids` must be an iterable of CIDs')
}
const ipld = this
const generator = async function * () {
for await (const cid of cids) {
yield ipld.remove(cid, options)
}
}
return extendIterator(generator())
}
/**
* Returns all the paths that can be resolved into.
*
* @param {CID} cid - The ID to get the paths from
* @param {string} [offsetPath=''] - the path to start to retrieve the other paths from.
* @param {TreeOptions} [userOptions]
*/
tree (cid, offsetPath, userOptions) {
if (typeof offsetPath === 'object') {
userOptions = offsetPath
offsetPath = undefined
}
offsetPath = offsetPath || ''
const defaultOptions = {
recursive: false
}
const options = mergeOptions(defaultOptions, userOptions)
/**
* If a path is a link then follow it and return its CID
*
* @param {Block} block
* @param {string} treePath
*/
const maybeRecurse = async (block, treePath) => {
// A treepath we might want to follow recursively
const format = await this.getFormat(multicodec.getCodeFromName(block.cid.codec))
const result = format.resolver.resolve(block.data, treePath)
// Something to follow recursively, hence push it into the queue
if (CID.isCID(result.value)) {
return result.value
}
}
const ipld = this
const generator = async function * () {
// The list of paths that will get returned
const treePaths = []
// The current block, needed to call `isLink()` on every iteration
let block
// The list of items we want to follow recursively. The items are
// an object consisting of the CID and the currently already resolved
// path
const queue = [{ cid, basePath: '' }]
// The path that was already traversed
let basePath
// End of iteration if there aren't any paths left to return or
// if we don't want to traverse recursively and have already
// returns the first level
while (treePaths.length > 0 || queue.length > 0) {
// There aren't any paths left, get them from the given CID
if (treePaths.length === 0 && queue.length > 0) {
const next = queue.shift()
if (next) {
({ cid, basePath } = next)
const format = await ipld.getFormat(multicodec.getCodeFromName(cid.codec))
block = await ipld.bs.get(cid, options)
const paths = format.resolver.tree(block.data)
treePaths.push(...paths)
}
}
const treePath = treePaths.shift() || ''
let fullPath = basePath + treePath
// Only follow links if recursion is intended
if (options.recursive) {
const cid = await maybeRecurse(block, treePath)
if (cid != null) {
queue.push({ cid, basePath: fullPath + '/' })
}
}
// Return it if it matches the given offset path, but is not the
// offset path itself
if (offsetPath !== undefined && fullPath.startsWith(offsetPath) &&
fullPath.length > offsetPath.length) {
if (offsetPath.length > 0) {
fullPath = fullPath.slice(offsetPath.length + 1)
}
yield fullPath
}
}
}
return extendIterator(generator())
}
/**
* @param {CodecCode} codec
*/
async getFormat (codec) {
// TODO vmx 2019-01-24: Once all CIDs support accessing the codec code
// instead of the name, remove this part
if (typeof codec === 'string') {
codec = multicodec.getCodeFromName(codec)
}
if (this.resolvers[codec]) {
return this.resolvers[codec]
}
// If not supported, attempt to dynamically load this format
const format = await this.loadFormat(codec)
if (this.resolvers[codec] == null) {
this.addFormat(format)
} else {
return this.resolvers[codec]
}
return format
}
}
/**
* Default options for IPLD.
*/
IPLDResolver.defaultOptions = {
/** @type {IPLDFormat[]} */
formats: [ipldDagCbor, ipldDagPb, ipldRaw]
}
module.exports = IPLDResolver