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
442 lines (384 loc) · 11 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
437
438
439
440
441
442
'use strict'
const Block = require('ipfs-block')
const pull = require('pull-stream')
const CID = require('cids')
const doUntil = require('async/doUntil')
const IPFSRepo = require('ipfs-repo')
const BlockService = require('ipfs-block-service')
const joinPath = require('path').join
const osPathSep = require('path').sep
const pullDeferSource = require('pull-defer').source
const pullTraverse = require('pull-traverse')
const map = require('async/map')
const series = require('async/series')
const waterfall = require('async/waterfall')
const MemoryStore = require('interface-datastore').MemoryDatastore
const dagPB = require('ipld-dag-pb')
const dagCBOR = require('ipld-dag-cbor')
const ipldGit = require('ipld-git')
const ipldBitcoin = require('ipld-bitcoin')
const ipldEthAccountSnapshot = require('ipld-ethereum').ethAccountSnapshot
const ipldEthBlock = require('ipld-ethereum').ethBlock
const ipldEthBlockList = require('ipld-ethereum').ethBlockList
const ipldEthStateTrie = require('ipld-ethereum').ethStateTrie
const ipldEthStorageTrie = require('ipld-ethereum').ethStorageTrie
const ipldEthTx = require('ipld-ethereum').ethTx
const ipldEthTxTrie = require('ipld-ethereum').ethTxTrie
const ipldRaw = require('ipld-raw')
const ipldZcash = require('ipld-zcash')
function noop () {}
class IPLDResolver {
constructor (blockService) {
if (!blockService) {
throw new Error('Missing blockservice')
}
this.bs = blockService
this.resolvers = {}
this.support = {}
// Adds support for an IPLD format
this.support.add = (multicodec, resolver, util) => {
if (this.resolvers[multicodec]) {
throw new Error('Resolver already exists for codec "' + multicodec + '"')
}
this.resolvers[multicodec] = {
resolver: resolver,
util: util
}
}
this.support.rm = (multicodec) => {
if (this.resolvers[multicodec]) {
delete this.resolvers[multicodec]
}
}
// Support by default dag-pb, dag-cbor, git, and eth-*
this.support.add(dagPB.resolver.multicodec,
dagPB.resolver,
dagPB.util)
this.support.add(dagCBOR.resolver.multicodec,
dagCBOR.resolver,
dagCBOR.util)
this.support.add(ipldGit.resolver.multicodec,
ipldGit.resolver,
ipldGit.util)
this.support.add(ipldBitcoin.resolver.multicodec,
ipldBitcoin.resolver,
ipldBitcoin.util)
this.support.add(ipldEthAccountSnapshot.resolver.multicodec,
ipldEthAccountSnapshot.resolver,
ipldEthAccountSnapshot.util)
this.support.add(ipldEthBlock.resolver.multicodec,
ipldEthBlock.resolver,
ipldEthBlock.util)
this.support.add(ipldEthBlockList.resolver.multicodec,
ipldEthBlockList.resolver,
ipldEthBlockList.util)
this.support.add(ipldEthStateTrie.resolver.multicodec,
ipldEthStateTrie.resolver,
ipldEthStateTrie.util)
this.support.add(ipldEthStorageTrie.resolver.multicodec,
ipldEthStorageTrie.resolver,
ipldEthStorageTrie.util)
this.support.add(ipldEthTx.resolver.multicodec,
ipldEthTx.resolver,
ipldEthTx.util)
this.support.add(ipldEthTxTrie.resolver.multicodec,
ipldEthTxTrie.resolver,
ipldEthTxTrie.util)
this.support.add(ipldRaw.resolver.multicodec,
ipldRaw.resolver,
ipldRaw.util)
this.support.add(ipldZcash.resolver.multicodec,
ipldZcash.resolver,
ipldZcash.util)
}
get (cid, path, options, callback) {
if (typeof path === 'function') {
callback = path
path = undefined
}
if (typeof options === 'function') {
callback = options
options = {}
}
// this removes occurrences of ./, //, ../
// makes sure that path never starts with ./ or /
// path.join is OS specific. Need to convert back to POSIX format.
if (typeof path === 'string') {
path = joinPath('/', path)
.substr(1)
.split(osPathSep)
.join('/')
}
if (path === '' || !path) {
return this._get(cid, (err, node) => {
if (err) {
return callback(err)
}
callback(null, {
value: node,
remainderPath: ''
})
})
}
let value
doUntil(
(cb) => {
// get block
// use local resolver
// update path value
this.bs.get(cid, (err, block) => {
if (err) {
return cb(err)
}
const r = this.resolvers[cid.codec]
if (!r) {
return cb(new Error('No resolver found for codec "' + cid.codec + '"'))
}
r.resolver.resolve(block.data, path, (err, result) => {
if (err) {
return cb(err)
}
value = result.value
path = result.remainderPath
cb()
})
})
},
() => {
const endReached = !path || path === '' || path === '/'
const isTerminal = value && !value['/']
if ((endReached && isTerminal) || options.localResolve) {
return true
} else {
// continue traversing
if (value) {
cid = new CID(value['/'])
}
return false
}
},
(err, results) => {
if (err) {
return callback(err)
}
return callback(null, {
value: value,
remainderPath: path
})
}
)
}
getStream (cid, path, options) {
const deferred = pullDeferSource()
this.get(cid, path, options, (err, result) => {
if (err) {
return deferred.resolve(
pull.error(err)
)
}
deferred.resolve(
pull.values([result])
)
})
return deferred
}
put (node, options, callback) {
if (typeof options === 'function') {
callback = options
return setImmediate(() => callback(
new Error('IPLDResolver.put requires options')
))
}
callback = callback || noop
if (options.cid && CID.isCID(options.cid)) {
return this._put(options.cid, node, callback)
}
options.hashAlg = options.hashAlg || 'sha2-256'
const r = this.resolvers[options.format]
if (!r) {
return callback(new Error('No resolver found for codec "' + options.format + '"'))
}
// TODO add support for different hash funcs in the utils of
// each format (just really needed for CBOR for now, really
// r.util.cid(node1, hashAlg, (err, cid) => {
r.util.cid(node, (err, cid) => {
if (err) {
return callback(err)
}
this._put(cid, node, callback)
})
}
treeStream (cid, path, options) {
if (typeof path === 'object') {
options = path
path = undefined
}
options = options || {}
let p
if (!options.recursive) {
p = pullDeferSource()
const r = this.resolvers[cid.codec]
if (!r) {
p.abort(new Error('No resolver found for codec "' + cid.codec + '"'))
return p
}
waterfall([
(cb) => this.bs.get(cid, cb),
(block, cb) => r.resolver.tree(block.data, cb)
], (err, paths) => {
if (err) {
p.abort(err)
return p
}
p.resolve(pull.values(paths))
})
}
// recursive
if (options.recursive) {
p = pull(
pullTraverse.widthFirst({
basePath: null,
cid: cid
}, (el) => {
// pass the paths through the pushable pull stream
// continue traversing the graph by returning
// the next cids with deferred
if (typeof el === 'string') {
return pull.empty()
}
const deferred = pullDeferSource()
const cid = el.cid
const r = this.resolvers[cid.codec]
if (!r) {
deferred.abort(new Error('No resolver found for codec "' + cid.codec + '"'))
return deferred
}
waterfall([
(cb) => this.bs.get(el.cid, cb),
(block, cb) => r.resolver.tree(block.data, (err, paths) => {
if (err) {
return cb(err)
}
map(paths, (p, cb) => {
r.resolver.isLink(block.data, p, (err, link) => {
if (err) {
return cb(err)
}
cb(null, {path: p, link: link})
})
}, cb)
})
], (err, paths) => {
if (err) {
deferred.abort(err)
return deferred
}
deferred.resolve(pull.values(paths.map((p) => {
const base = el.basePath ? el.basePath + '/' + p.path : p.path
if (p.link) {
return {
basePath: base,
cid: new CID(p.link['/'])
}
}
return base
})))
})
return deferred
}),
pull.map((e) => {
if (typeof e === 'string') {
return e
}
return e.basePath
}),
pull.filter(Boolean)
)
}
// filter out by path
if (path) {
return pull(
p,
pull.map((el) => {
if (el.indexOf(path) === 0) {
el = el.slice(path.length + 1)
return el
}
}),
pull.filter(Boolean)
)
}
return p
}
remove (cids, callback) {
this.bs.delete(cids, callback)
}
/* */
/* internals */
/* */
_get (cid, callback) {
const r = this.resolvers[cid.codec]
if (!r) {
return callback(new Error('No resolver found for codec "' + cid.codec + '"'))
}
waterfall([
(cb) => this.bs.get(cid, cb),
(block, cb) => {
if (r) {
r.util.deserialize(block.data, (err, deserialized) => {
if (err) {
return cb(err)
}
cb(null, deserialized)
})
} else { // multicodec unknown, send back raw data
cb(null, block.data)
}
}
], callback)
}
_put (cid, node, callback) {
callback = callback || noop
const r = this.resolvers[cid.codec]
if (!r) {
return callback(new Error('No resolver found for codec "' + cid.codec + '"'))
}
waterfall([
(cb) => r.util.serialize(node, cb),
(buf, cb) => this.bs.put(new Block(buf, cid), cb)
], (err) => {
if (err) {
return callback(err)
}
callback(null, cid)
})
}
}
/**
* Create an IPLD resolver with an inmemory blockservice and
* repo.
*
* @param {function(Error, IPLDResolver)} callback
* @returns {void}
*/
IPLDResolver.inMemory = function (callback) {
const repo = new IPFSRepo('in-memory', {
storageBackends: {
root: MemoryStore,
blocks: MemoryStore,
datastore: MemoryStore
},
lock: 'memory'
})
const blockService = new BlockService(repo)
series([
(cb) => repo.init({}, cb),
(cb) => repo.open(cb)
], (err) => {
if (err) {
return callback(err)
}
callback(null, new IPLDResolver(blockService))
})
}
module.exports = IPLDResolver