This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
feat: support --raw-leaves #219
Merged
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -8,12 +8,12 @@ const CID = require('cids') | |
const DAGLink = dagPB.DAGLink | ||
const DAGNode = dagPB.DAGNode | ||
|
||
module.exports = function (file, ipld, options) { | ||
module.exports = function reduce (file, ipld, options) { | ||
return function (leaves, callback) { | ||
if (leaves.length === 1 && leaves[0].single && options.reduceSingleLeafToSelf) { | ||
const leaf = leaves[0] | ||
|
||
if (!options.rawLeafNodes) { | ||
if (options.leafType === 'file' && !options.rawLeaves) { | ||
return callback(null, { | ||
path: file.path, | ||
multihash: leaf.multihash, | ||
|
@@ -23,33 +23,37 @@ module.exports = function (file, ipld, options) { | |
}) | ||
} | ||
|
||
// we are using raw leaf nodes, this file only has one node but it'll be marked raw | ||
// so convert it back to a file node | ||
// we're using raw leaf nodes so we convert the node into a UnixFS `file` node. | ||
return waterfall([ | ||
(cb) => ipld.get(new CID(leaf.multihash), cb), | ||
(cb) => ipld.get(leaf.cid, cb), | ||
(result, cb) => { | ||
const meta = UnixFS.unmarshal(result.value.data) | ||
const fileNode = new UnixFS('file', meta.data) | ||
const data = result.value.data | ||
const fileNode = new UnixFS('file', data) | ||
|
||
DAGNode.create(fileNode.marshal(), [], options.hashAlg, (err, node) => { | ||
cb(err, { DAGNode: node, fileNode: fileNode }) | ||
DAGNode.create(fileNode.marshal(), [], options.hashAlg, (error, node) => { | ||
cb(error, { DAGNode: node, fileNode: fileNode }) | ||
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. Nitpick, why s/err/error? 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. Personal preference really - I think short variable names make the code less expressive. |
||
}) | ||
}, | ||
(result, cb) => { | ||
if (options.onlyHash) { | ||
return cb(null, result) | ||
} | ||
|
||
let cid = new CID(result.DAGNode.multihash) | ||
|
||
if (options.cidVersion === 1) { | ||
cid = cid.toV1() | ||
} | ||
|
||
ipld.put(result.DAGNode, { cid }, (err) => cb(err, result)) | ||
ipld.put(result.DAGNode, { cid }, (error) => cb(error, result)) | ||
}, | ||
(result, cb) => { | ||
cb(null, { | ||
path: file.path, | ||
multihash: result.DAGNode.multihash, | ||
size: result.DAGNode.size, | ||
leafSize: result.fileNode.fileSize(), | ||
name: '' | ||
name: leaf.name | ||
}) | ||
} | ||
], callback) | ||
|
@@ -61,37 +65,45 @@ module.exports = function (file, ipld, options) { | |
const links = leaves.map((leaf) => { | ||
f.addBlockSize(leaf.leafSize) | ||
|
||
return new DAGLink(leaf.name, leaf.size, leaf.multihash) | ||
let cid = leaf.cid | ||
|
||
if (!cid) { | ||
// we are an intermediate node | ||
cid = new CID(options.cidVersion, 'dag-pb', leaf.multihash) | ||
} | ||
|
||
return new DAGLink(leaf.name, leaf.size, cid.buffer) | ||
}) | ||
|
||
waterfall([ | ||
(cb) => DAGNode.create(f.marshal(), links, options.hashAlg, cb), | ||
(node, cb) => { | ||
if (options.onlyHash) return cb(null, node) | ||
const cid = new CID(options.cidVersion, 'dag-pb', node.multihash) | ||
|
||
let cid = new CID(node.multihash) | ||
|
||
if (options.cidVersion === 1) { | ||
cid = cid.toV1() | ||
if (options.onlyHash) { | ||
return cb(null, { | ||
node, cid | ||
}) | ||
} | ||
|
||
ipld.put(node, { cid }, (err) => cb(err, node)) | ||
ipld.put(node, { | ||
cid | ||
}, (error) => cb(error, { | ||
node, cid | ||
})) | ||
} | ||
], (err, node) => { | ||
if (err) { | ||
callback(err) | ||
return // early | ||
], (error, result) => { | ||
if (error) { | ||
return callback(error) | ||
} | ||
|
||
const root = { | ||
callback(null, { | ||
name: '', | ||
path: file.path, | ||
multihash: node.multihash, | ||
size: node.size, | ||
multihash: result.cid.buffer, | ||
size: result.node.size, | ||
leafSize: f.fileSize() | ||
} | ||
|
||
callback(null, root) | ||
}) | ||
}) | ||
} | ||
} |
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,32 @@ | ||
'use strict' | ||
|
||
const pull = require('pull-stream') | ||
const traverse = require('pull-traverse') | ||
const CID = require('cids') | ||
|
||
module.exports = (ipld, multihash, callback) => { | ||
pull( | ||
traverse.depthFirst(new CID(multihash), (cid) => { | ||
return pull( | ||
pull.values([cid]), | ||
pull.asyncMap((cid, callback) => { | ||
ipld.get(cid, (error, result) => { | ||
callback(error, !error && result.value) | ||
}) | ||
}), | ||
pull.asyncMap((node, callback) => { | ||
if (!node.links) { | ||
return callback() | ||
} | ||
|
||
return callback( | ||
null, node.links.map(link => new CID(link.multihash)) | ||
) | ||
}), | ||
pull.filter(Boolean), | ||
pull.flatten() | ||
) | ||
}), | ||
pull.collect(callback) | ||
) | ||
} |
Oops, something went wrong.
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.
Document
leafType
here?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.
Done!