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.
feat: Update API, use new block-service, block and ipld-dag-pb
- Loading branch information
Showing
5 changed files
with
151 additions
and
126 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
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,137 @@ | ||
'use strict' | ||
|
||
// 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.resolver) | ||
} | ||
|
||
// Adds support for an IPLD format | ||
// default ones are dag-pb and dag-cbor | ||
support (multicodec, resolver) { | ||
this.resolvers[multicodec] = resolver | ||
} | ||
|
||
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) => { | ||
// TODO | ||
// deserialize this block into the format described by the CID | ||
// if the multicodec is not known (i.e we don't have a deserializer) | ||
// send back the raw data | ||
|
||
// old -> ipld.unmarshal(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.
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