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 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
309 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,141 @@ | ||
'use strict' | ||
|
||
exports.IPLDService = require('./ipld-service') | ||
exports.resolve = require('./resolve') | ||
// const isIPFS = require('is-ipfs') | ||
const Block = require('ipfs-block') | ||
// const ipld = require('ipld-dag-cbor') | ||
const pull = require('pull-stream') | ||
const traverse = require('pull-traverse') | ||
// const mh = require('multihashes') | ||
const dagPB = require('ipld-dag-pb') | ||
|
||
const utils = require('./utils') | ||
|
||
module.exports = class IPLDResolver { | ||
constructor (blockService) { | ||
// TODO instead of throwing, just create an in-memory | ||
// block-service, so it is nice for demos | ||
if (!blockService) { | ||
throw new Error('IPLDService requires a BlockService instance') | ||
} | ||
|
||
this.bs = blockService | ||
this.resolvers = {} | ||
this.support(dagPB.resolver.multicodec, dagPB.DAGNode, dagPB.resolver) | ||
} | ||
|
||
// Adds support for an IPLD format | ||
// default ones are dag-pb and dag-cbor | ||
support (multicodec, type, resolver) { | ||
this.resolvers[multicodec] = { | ||
resolver: resolver, | ||
Type: type | ||
} | ||
} | ||
|
||
resolve (cid, path) { | ||
// TODO | ||
} | ||
|
||
put (node, callback) { | ||
callback = callback || noop | ||
pull( | ||
pull.values([node]), | ||
this.putStream(callback) | ||
) | ||
} | ||
|
||
putStream (callback) { | ||
callback = callback || noop | ||
|
||
return pull( | ||
pull.map((node) => { | ||
return { | ||
block: new Block(node.serialize()), | ||
cid: node.cid() | ||
} | ||
}), | ||
this.bs.putStream(), | ||
pull.onEnd(callback) | ||
) | ||
} | ||
|
||
get (cid, callback) { | ||
pull( | ||
this.getStream(cid), | ||
pull.collect((err, res) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
callback(null, res[0]) | ||
}) | ||
) | ||
} | ||
|
||
getStream (cid) { | ||
return pull( | ||
this.bs.getStream(cid), | ||
pull.map((block) => { | ||
if (this.resolvers[cid.codec]) { | ||
const node = new this.resolvers[cid.codec].Type() | ||
node.deserialize(block.data) | ||
return node | ||
} else { // multicodec unknown, send back raw data | ||
return block.data | ||
} | ||
}) | ||
) | ||
} | ||
|
||
getRecursive (cid, callback) { | ||
pull( | ||
this.getRecursiveStream(cid), | ||
pull.collect(callback) | ||
) | ||
} | ||
|
||
getRecursiveStream (cid) { | ||
return pull( | ||
this.getStream(cid), | ||
pull.map((node) => { | ||
traverse.widthFirst(node, (node) => { | ||
return pull( | ||
pull.values(utils.getKeys(node)), | ||
pull.map((link) => this.getStream(link)), | ||
pull.flatten() | ||
) | ||
}) | ||
}), | ||
pull.flatten() | ||
) | ||
} | ||
|
||
remove (cids, callback) { | ||
this.bs.delete(cids, callback) | ||
} | ||
} | ||
|
||
function noop () {} | ||
|
||
/* | ||
function normalizeKey (key) { | ||
let res | ||
const isMhash = isIPFS.multihash(key) | ||
const isPath = isIPFS.path(key) | ||
if (!isMhash && !isPath) { | ||
return null | ||
} | ||
if (isMhash) { | ||
res = key | ||
} else if (isPath) { | ||
res = key.replace('/ipfs/', '') | ||
} | ||
if (typeof res === 'string') { | ||
return mh.fromB58String(res) | ||
} | ||
return res | ||
} | ||
*/ |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
/* | ||
* Test different types of data structures living together | ||
* & | ||
* Test data made of mixed data structures! | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* eslint-env mocha */ | ||
'use strict' |
Oops, something went wrong.