This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Create DAGLink list instead of passing existing links through
- Loading branch information
1 parent
a5df7c9
commit 723bbe9
Showing
15 changed files
with
400 additions
and
317 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict' | ||
|
||
const CID = require('cids') | ||
const dagPb = require('ipld-dag-pb') | ||
const { | ||
DAGNode, | ||
DAGLink | ||
} = dagPb | ||
const waterfall = require('async/waterfall') | ||
|
||
const addLink = (ipfs, options, callback) => { | ||
options = Object.assign({}, { | ||
parent: undefined, | ||
child: undefined, | ||
name: undefined, | ||
flush: true | ||
}, options) | ||
|
||
if (!options.parent) { | ||
return callback(new Error('No parent passed to addLink')) | ||
} | ||
|
||
if (!options.child) { | ||
return callback(new Error('No child passed to addLink')) | ||
} | ||
|
||
if (!options.name) { | ||
return callback(new Error('No name passed to addLink')) | ||
} | ||
|
||
waterfall([ | ||
(done) => { | ||
// Remove the old link if necessary | ||
DAGNode.rmLink(options.parent, options.name, done) | ||
}, | ||
(parent, done) => { | ||
// Add the new link to the parent | ||
DAGNode.addLink(parent, new DAGLink(options.name, options.child.size, options.child.hash || options.child.multihash), done) | ||
}, | ||
(parent, done) => { | ||
if (!options.flush) { | ||
return done() | ||
} | ||
|
||
// Persist the new parent DAGNode | ||
ipfs.dag.put(parent, { | ||
cid: new CID(parent.hash || parent.multihash) | ||
}, (error) => done(error, parent)) | ||
} | ||
], callback) | ||
} | ||
|
||
module.exports = addLink |
Oops, something went wrong.