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: upgrade to latest dag-pb API #88
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -8,12 +8,13 @@ const pullWrite = require('pull-write') | |
const parallel = require('async/parallel') | ||
const dagPB = require('ipld-dag-pb') | ||
const CID = require('cids') | ||
const series = require('async/series') | ||
const each = require('async/each') | ||
|
||
const fsc = require('./../chunker/fixed-size') | ||
const createAndStoreTree = require('./flush-tree') | ||
|
||
const DAGNode = dagPB.DAGNode | ||
const DAGLink = dagPB.DAGLink | ||
|
||
const CHUNK_SIZE = 262144 | ||
|
||
|
@@ -71,49 +72,48 @@ function makeWriter (source, files, ipldResolver) { | |
} | ||
} | ||
|
||
function createAndStoreDir (item, ipldResolver, cb) { | ||
function createAndStoreDir (item, ipldResolver, callback) { | ||
// 1. create the empty dir dag node | ||
// 2. write it to the dag store | ||
|
||
const d = new UnixFS('directory') | ||
const n = new DAGNode() | ||
n.data = d.marshal() | ||
let n | ||
|
||
n.multihash((err, multihash) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
ipldResolver.put({ | ||
node: n, | ||
cid: new CID(multihash) | ||
}, (err) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
n.size((err, size) => { | ||
series([ | ||
(cb) => { | ||
DAGNode.create(d.marshal(), (err, node) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
cb(null, { | ||
path: item.path, | ||
multihash: multihash, | ||
size: size | ||
}) | ||
n = node | ||
cb() | ||
}) | ||
}, | ||
(cb) => { | ||
ipldResolver.put({ | ||
node: n, | ||
cid: new CID(n.multihash) | ||
}, cb) | ||
} | ||
], (err) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
callback(null, { | ||
path: item.path, | ||
multihash: n.multihash, | ||
size: n.size | ||
}) | ||
}) | ||
} | ||
|
||
function createAndStoreFile (file, ipldResolver, cb) { | ||
function createAndStoreFile (file, ipldResolver, callback) { | ||
if (Buffer.isBuffer(file.content)) { | ||
file.content = pull.values([file.content]) | ||
} | ||
|
||
if (typeof file.content !== 'function') { | ||
return cb(new Error('invalid content')) | ||
return callback(new Error('invalid content')) | ||
} | ||
|
||
// 1. create the unixfs merkledag node | ||
|
@@ -128,44 +128,37 @@ function createAndStoreFile (file, ipldResolver, cb) { | |
file.content, | ||
fsc(CHUNK_SIZE), | ||
pull.asyncMap((chunk, cb) => { | ||
const l = new UnixFS('file', Buffer(chunk)) | ||
const n = new DAGNode(l.marshal()) | ||
const l = new UnixFS('file', new Buffer(chunk)) | ||
|
||
n.multihash((err, multihash) => { | ||
DAGNode.create(l.marshal(), (err, node) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
ipldResolver.put({ | ||
node: n, | ||
cid: new CID(multihash) | ||
node: node, | ||
cid: new CID(node.multihash) | ||
}, (err) => { | ||
if (err) { | ||
return cb(new Error('Failed to store chunk')) | ||
return cb(err) | ||
} | ||
|
||
n.size((err, size) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
cb(null, { | ||
Hash: multihash, | ||
Size: size, | ||
leafSize: l.fileSize(), | ||
Name: '' | ||
}) | ||
cb(null, { | ||
Hash: node.multihash, | ||
Size: node.size, | ||
leafSize: l.fileSize(), | ||
Name: '' | ||
}) | ||
}) | ||
}) | ||
}), | ||
pull.collect((err, leaves) => { | ||
if (err) { | ||
return cb(err) | ||
return callback(err) | ||
} | ||
|
||
if (leaves.length === 1) { | ||
return cb(null, { | ||
return callback(null, { | ||
path: file.path, | ||
multihash: leaves[0].Hash, | ||
size: leaves[0].Size | ||
|
@@ -175,41 +168,49 @@ function createAndStoreFile (file, ipldResolver, cb) { | |
// create a parent node and add all the leafs | ||
|
||
const f = new UnixFS('file') | ||
const n = new DAGNode() | ||
|
||
for (let leaf of leaves) { | ||
f.addBlockSize(leaf.leafSize) | ||
n.addRawLink( | ||
new DAGLink(leaf.Name, leaf.Size, leaf.Hash) | ||
) | ||
} | ||
let n | ||
|
||
n.data = f.marshal() | ||
|
||
n.multihash((err, multihash) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
ipldResolver.put({ | ||
node: n, | ||
cid: new CID(multihash) | ||
}, (err) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
n.size((err, size) => { | ||
series([ | ||
(cb) => { | ||
DAGNode.create(f.marshal(), (err, node) => { | ||
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. It is here where I'm failing, I'm passing the f.marshal() before I do the addBlockSize 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. ok, this was one bit, now only missing 10 bytes |
||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
cb(null, { | ||
path: file.path, | ||
multihash: multihash, | ||
size: size | ||
}) | ||
n = node | ||
cb() | ||
}) | ||
}, | ||
(cb) => { | ||
each(leaves, (leaf, next) => { | ||
f.addBlockSize(leaf.leafSize) | ||
DAGNode.addLink(n, { | ||
name: leaf.Name, | ||
size: leaf.Size, | ||
multihash: leaf.Hash | ||
}, (err, node) => { | ||
if (err) { | ||
return next(err) | ||
} | ||
n = node | ||
next() | ||
}) | ||
}, cb) | ||
}, | ||
(cb) => { | ||
ipldResolver.put({ | ||
node: n, | ||
cid: new CID(n.multihash) | ||
}, cb) | ||
} | ||
], (err) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
callback(null, { | ||
path: file.path, | ||
multihash: n.multihash, | ||
size: n.size | ||
}) | ||
}) | ||
}) | ||
|
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
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.
can we please not use one letter variables if possible? :)
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.
👍