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
API update #80
Merged
Merged
API update #80
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d8e5a9e
chore: update deps
daviddias dc51268
feat: return CID on .put
daviddias 27cdf79
feat: treeStream
daviddias 6ec040e
fix: pick right resolver for the job
daviddias 20e4b81
fix: path -> remainderPath
daviddias 04bf489
test: update remaining tests
daviddias 78cb2c9
chore: update deps
daviddias 6be87a8
refactor: .treeStream non recursive
daviddias d5fb58b
refactor: tree recursive as pully pull
dignifiedquire da90e06
revert unneeded change
dignifiedquire 972f6d4
tests: fix tests, not assume sorting
daviddias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,16 +42,16 @@ | |
"lodash": "^4.17.4", | ||
"ncp": "^2.0.0", | ||
"pre-commit": "^1.2.2", | ||
"rimraf": "^2.5.4", | ||
"rimraf": "^2.6.1", | ||
"rlp": "^2.0.0" | ||
}, | ||
"dependencies": { | ||
"async": "^2.1.4", | ||
"async": "^2.1.5", | ||
"cids": "~0.4.1", | ||
"interface-pull-blob-store": "~0.6.0", | ||
"ipfs-block": "~0.5.4", | ||
"ipfs-block-service": "~0.8.1", | ||
"ipfs-repo": "~0.11.2", | ||
"ipfs-block": "~0.5.5", | ||
"ipfs-block-service": "~0.8.3", | ||
"ipfs-repo": "~0.11.3", | ||
"ipld-dag-cbor": "~0.9.1", | ||
"ipld-dag-pb": "~0.9.5", | ||
"ipld-eth-block": "^2.2.1", | ||
|
@@ -62,7 +62,8 @@ | |
"is-ipfs": "~0.3.0", | ||
"lodash.flatten": "^4.4.0", | ||
"lodash.includes": "^4.3.0", | ||
"multihashes": "~0.3.3", | ||
"multihashes": "~0.4.3", | ||
"pull-sort": "^1.0.0", | ||
"pull-stream": "^3.5.0", | ||
"pull-traverse": "^1.0.3" | ||
}, | ||
|
@@ -75,4 +76,4 @@ | |
"kumavis <[email protected]>", | ||
"wanderer <[email protected]>" | ||
] | ||
} | ||
} |
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 |
---|---|---|
|
@@ -2,13 +2,17 @@ | |
|
||
const Block = require('ipfs-block') | ||
const pull = require('pull-stream') | ||
const pullPushable = require('pull-pushable') | ||
const CID = require('cids') | ||
const doUntil = require('async/doUntil') | ||
const IPFSRepo = require('ipfs-repo') | ||
const MemoryStore = require('interface-pull-blob-store') | ||
const BlockService = require('ipfs-block-service') | ||
const joinPath = require('path').join | ||
const pullDeferSource = require('pull-defer').source | ||
const pullTraverse = require('pull-traverse') | ||
const asyncEach = require('async/each') | ||
const pullSort = require('pull-sort') | ||
|
||
const dagPB = require('ipld-dag-pb') | ||
const dagCBOR = require('ipld-dag-cbor') | ||
|
@@ -18,7 +22,9 @@ const ipldEthTxTrie = require('ipld-eth-tx-trie') | |
const ipldEthStateTrie = require('ipld-eth-state-trie') | ||
const ipldEthStorageTrie = require('ipld-eth-storage-trie') | ||
|
||
module.exports = class IPLDResolver { | ||
function noop () {} | ||
|
||
class IPLDResolver { | ||
constructor (blockService) { | ||
// nicola will love this! | ||
if (!blockService) { | ||
|
@@ -213,16 +219,137 @@ module.exports = class IPLDResolver { | |
|
||
pull( | ||
pull.values([nodeAndCID]), | ||
this._putStream(callback) | ||
this._putStream((err) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
callback(null, nodeAndCID.cid) | ||
}) | ||
) | ||
} | ||
} | ||
|
||
treeStream (cid, path, options) { | ||
if (typeof path === 'object') { | ||
options = path | ||
path = undefined | ||
} | ||
|
||
options = options || {} | ||
|
||
// non recursive | ||
const p = pullPushable() | ||
|
||
if (!options.recursive) { | ||
const r = this.resolvers[cid.codec] | ||
|
||
this.bs.get(cid, (err, block) => { | ||
if (err) { | ||
return p(err) | ||
} | ||
|
||
r.resolver.tree(block, (err, paths) => { | ||
if (err) { | ||
return p(err) | ||
} | ||
paths.forEach((path) => p.push(path)) | ||
p.end() | ||
}) | ||
}) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could be written as if (!options.recursive) {
p = pullDefer.source()
waterfall([
(cb) => this.bs.get(cid, cb),
(block, cb) => r.resolver.tree(block, cb)
], (err, paths) => {
if (err) {
return p.abort(err)
}
p.resolve(pull.values(paths))
})
} |
||
|
||
// recursive | ||
if (options.recursive) { | ||
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 | ||
|
||
const deferred = pullDeferSource() | ||
|
||
this.bs.get(el.cid, (err, block) => { | ||
if (err) { | ||
return p(err) | ||
} | ||
|
||
const r = this.resolvers[el.cid.codec] | ||
|
||
r.resolver.tree(block, (err, paths) => { | ||
if (err) { | ||
p(err) | ||
return deferred.resolve(pull.empty()) | ||
} | ||
|
||
const next = [] | ||
|
||
asyncEach(paths, (path, cb) => { | ||
r.resolver.isLink(block, path, (err, link) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
p.push(el.basePath | ||
? el.basePath + '/' + path | ||
: path | ||
) | ||
|
||
// if it is a link, continue traversing | ||
if (link) { | ||
next.push({ | ||
basePath: el.basePath | ||
? el.basePath + '/' + path | ||
: path, | ||
cid: new CID(link['/']) | ||
}) | ||
} | ||
cb() | ||
}) | ||
}, (err) => { | ||
if (err) { | ||
p(err) | ||
return deferred.resolve(pull.empty()) | ||
} | ||
|
||
deferred.resolve(pull.values(next)) | ||
}) | ||
}) | ||
}) | ||
|
||
return deferred | ||
}), | ||
pull.onEnd(() => p.end()) | ||
) | ||
} | ||
|
||
// 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((el) => el && el.length > 0), | ||
pullSort((a, b) => a.localeCompare(b)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion being: let the user do the sorting themselves? sgtm if that is what you propose |
||
) | ||
} | ||
|
||
return pull( | ||
p, | ||
pullSort((a, b) => a.localeCompare(b)) | ||
) | ||
} | ||
|
||
remove (cids, callback) { | ||
this.bs.delete(cids, callback) | ||
} | ||
|
||
/* */ | ||
/* internals */ | ||
/* */ | ||
|
||
_get (cid, callback) { | ||
pull( | ||
|
@@ -279,4 +406,4 @@ module.exports = class IPLDResolver { | |
} | ||
} | ||
|
||
function noop () {} | ||
module.exports = IPLDResolver |
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 |
---|---|---|
|
@@ -113,7 +113,7 @@ module.exports = (repo) => { | |
}) | ||
}) | ||
|
||
describe('public api', () => { | ||
describe.only('public api', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove |
||
it('resolver.put with CID', (done) => { | ||
resolver.put(node1, { cid: cid1 }, done) | ||
}) | ||
|
@@ -239,6 +239,77 @@ module.exports = (repo) => { | |
}) | ||
}) | ||
|
||
it('resolver.tree', (done) => { | ||
pull( | ||
resolver.treeStream(cid3), | ||
pull.collect((err, values) => { | ||
expect(err).to.not.exist | ||
expect(values).to.eql([ | ||
'one', | ||
'someData', | ||
'two' | ||
]) | ||
done() | ||
}) | ||
) | ||
}) | ||
|
||
it('resolver.tree with existent path', (done) => { | ||
pull( | ||
resolver.treeStream(cid3, 'one'), | ||
pull.collect((err, values) => { | ||
expect(err).to.not.exist | ||
expect(values).to.eql([]) | ||
done() | ||
}) | ||
) | ||
}) | ||
|
||
it('resolver.tree with non existent path', (done) => { | ||
pull( | ||
resolver.treeStream(cid3, 'bananas'), | ||
pull.collect((err, values) => { | ||
expect(err).to.not.exist | ||
expect(values).to.eql([]) | ||
done() | ||
}) | ||
) | ||
}) | ||
|
||
it('resolver.tree recursive', (done) => { | ||
pull( | ||
resolver.treeStream(cid3, { recursive: true }), | ||
pull.collect((err, values) => { | ||
expect(err).to.not.exist | ||
expect(values).to.eql([ | ||
'one', | ||
'one/someData', | ||
'someData', | ||
'two', | ||
'two/one', | ||
'two/one/someData', | ||
'two/someData' | ||
]) | ||
done() | ||
}) | ||
) | ||
}) | ||
|
||
it('resolver.tree with existent path recursive', (done) => { | ||
pull( | ||
resolver.treeStream(cid3, 'two', { recursive: true }), | ||
pull.collect((err, values) => { | ||
expect(err).to.not.exist | ||
expect(values).to.eql([ | ||
'one', | ||
'one/someData', | ||
'someData' | ||
]) | ||
done() | ||
}) | ||
) | ||
}) | ||
|
||
it('resolver.remove', (done) => { | ||
resolver.put(node1, { cid: cid1 }, (err) => { | ||
expect(err).to.not.exist | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you need to use a push stream? you should be able to return source stream that only walks the next thing if it actually needs to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wanna exemplify what you mean?