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 299
feat: support DAG API #534
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
450c581
feat: dag.put
daviddias 7a8b030
Feat(dag): DAG api (#568)
atfornes 2e0c4db
feat(dag): update option names to reflect go-ipfs API
magik6k af9fc47
feat(dag): proper get implementation
magik6k fce3138
feat(dag): rebase, use waterfall for put
magik6k b7e95c2
fix(dag): path logic for DAG get was wrong
vmx d98f8e1
fix(dag): js-ipld format resolver take the raw block
vmx 49dd9a7
fix(dag): use SendOneFile for dag put
vmx 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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use strict' | ||
|
||
const dagPB = require('ipld-dag-pb') | ||
const dagCBOR = require('ipld-dag-cbor') | ||
const promisify = require('promisify-es6') | ||
const CID = require('cids') | ||
const waterfall = require('async/waterfall') | ||
const block = require('../block') | ||
|
||
module.exports = (send) => { | ||
return promisify((cid, path, options, callback) => { | ||
if (typeof path === 'function') { | ||
callback = path | ||
path = undefined | ||
} | ||
|
||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
|
||
options = options || {} | ||
path = path || '' | ||
|
||
if (CID.isCID(cid)) { | ||
cid = cid.toBaseEncodedString() | ||
} | ||
|
||
waterfall([ | ||
cb => { | ||
send({ | ||
path: 'dag/resolve', | ||
args: cid + '/' + path, | ||
qs: options | ||
}, cb) | ||
}, | ||
(resolved, cb) => { | ||
block(send).get(new CID(resolved['Cid']['/']), (err, ipfsBlock) => { | ||
cb(err, ipfsBlock, resolved['RemPath']) | ||
}) | ||
}, | ||
(ipfsBlock, path, cb) => { | ||
if (ipfsBlock.cid.codec === 'dag-cbor') { | ||
dagCBOR.resolver.resolve(ipfsBlock.data, path, cb) | ||
} | ||
if (ipfsBlock.cid.codec === 'dag-pb') { | ||
dagPB.resolver.resolve(ipfsBlock.data, path, cb) | ||
} | ||
} | ||
], callback) | ||
}) | ||
} |
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,12 @@ | ||
'use strict' | ||
|
||
const moduleConfig = require('../utils/module-config') | ||
|
||
module.exports = (arg) => { | ||
const send = moduleConfig(arg) | ||
|
||
return { | ||
get: require('./get')(send), | ||
put: require('./put')(send) | ||
} | ||
} |
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,69 @@ | ||
'use strict' | ||
|
||
const dagPB = require('ipld-dag-pb') | ||
const dagCBOR = require('ipld-dag-cbor') | ||
const promisify = require('promisify-es6') | ||
const CID = require('cids') | ||
const multihash = require('multihashes') | ||
const SendOneFile = require('../utils/send-one-file') | ||
|
||
function noop () {} | ||
|
||
module.exports = (send) => { | ||
const sendOneFile = SendOneFile(send, 'dag/put') | ||
|
||
return promisify((dagNode, options, callback) => { | ||
if (typeof options === 'function') { | ||
return setImmediate(() => callback(new Error('no options were passed'))) | ||
} | ||
|
||
callback = callback || noop | ||
|
||
let hashAlg = options.hash || 'sha2-256' | ||
let format | ||
let inputEnc | ||
|
||
if (options.cid && CID.isCID(options.cid)) { | ||
format = options.cid.codec | ||
hashAlg = multihash.decode(options.cid.multihash).name | ||
prepare() | ||
} else if (options.format) { | ||
format = options.format | ||
prepare() | ||
} else { | ||
callback(new Error('Invalid arguments')) | ||
} | ||
|
||
function prepare () { | ||
inputEnc = 'raw' | ||
|
||
if (format === 'dag-cbor') { | ||
dagCBOR.util.serialize(dagNode, finalize) | ||
} | ||
if (format === 'dag-pb') { | ||
dagPB.util.serialize(dagNode, finalize) | ||
} | ||
} | ||
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. Same drawbacks as my comment above https://github.com/ipfs/js-ipfs-api/pull/534/files#r133178508 |
||
|
||
function finalize (err, serialized) { | ||
if (err) { return callback(err) } | ||
const sendOptions = { | ||
qs: { | ||
hash: hashAlg, | ||
format: format, | ||
'input-enc': inputEnc | ||
} | ||
} | ||
sendOneFile(serialized, sendOptions, (err, result) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
if (result['Cid']) { | ||
return callback(null, new CID(result['Cid']['/'])) | ||
} else { | ||
return callback(result) | ||
} | ||
}) | ||
} | ||
}) | ||
} |
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,34 @@ | ||
/* eslint-env mocha */ | ||
|
||
'use strict' | ||
|
||
const test = require('interface-ipfs-core') | ||
const parallel = require('async/parallel') | ||
|
||
const IPFSApi = require('../../src') | ||
|
||
const DaemonFactory = require('ipfsd-ctl') | ||
const df = DaemonFactory.create() | ||
|
||
const nodes = [] | ||
const common = { | ||
setup: function (callback) { | ||
callback(null, { | ||
spawnNode: (cb) => { | ||
df.spawn((err, _ipfsd) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
|
||
nodes.push(_ipfsd) | ||
cb(null, IPFSApi(_ipfsd.apiAddr)) | ||
}) | ||
} | ||
}) | ||
}, | ||
teardown: function (callback) { | ||
parallel(nodes.map((node) => (cb) => node.stop(cb)), callback) | ||
} | ||
} | ||
|
||
test.dag(common) |
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.
What about all the other types?
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.
I'd use js-ipld 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.
Later. This PR is on for almost a year.
The plan surely is to use js-ipld. But that's not straight forward as js-ipld currently only works with local repositories and not with remote ones (that's what I wanted to mention in the JS Dev call but couldn't remember).
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.
Fair enough.
I'd import
git
here too as it's the one other (well) supported codec on go-ipfs side (and would be really handy to have for https://github.com/ipfs-shipyard/IGiS which can't usewindow.ipfs
from api now)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.
Is it OK if I I open an issue for it and do it in a separate PR? That' also something newcomers can work on.
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.
This PR won't let users resolve through multiple IPLD formats.
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.
@diasdavid Sounds like a test for that is missing in interface-ipfs-core then.
It will work once js-ipld is used.
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.
Summary of this thread: everything mentioned here is solved if js-ipld is used. Hence I propose merging this change as is currently is and open a new issue (I'll do that) to use js-ipld instead.
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.
Sounds good my IPLD captain! 🚢