From 78dd63ccdfadd4f4925dfe0c9e2b72774094576f Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Tue, 3 Jul 2018 11:06:49 +0200 Subject: [PATCH 1/5] fix(dag): ensure `dag.put()` allows for optional options This is to align with API changes made in https://github.com/ipfs/interface-ipfs-core/commit/011c417b97e58aceafaa4e98fce8f32217bc5cf7 and https://github.com/ipfs/js-ipfs/pull/1415 License: MIT Signed-off-by: Pascal Precht --- src/dag/put.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/dag/put.js b/src/dag/put.js index c0c2f211a..5287d9d2a 100644 --- a/src/dag/put.js +++ b/src/dag/put.js @@ -5,7 +5,6 @@ const dagCBOR = require('ipld-dag-cbor') const promisify = require('promisify-es6') const CID = require('cids') const multihash = require('multihashes') -const setImmediate = require('async/setImmediate') const SendOneFile = require('../utils/send-one-file') function noop () {} @@ -15,29 +14,35 @@ module.exports = (send) => { return promisify((dagNode, options, callback) => { if (typeof options === 'function') { - return setImmediate(() => callback(new Error('no options were passed'))) + callback = options + } else if (options.cid && (options.format || options.hash)) { + return callback(new Error('Can\'t put dag node. Please provide either `cid` OR `format` and `hash` options.')) + } else if ((options.format && !options.hash) || (!options.format && options.hash)) { + return callback(new Error('Can\'t put dag node. Please provide `format` AND `hash` options.')) } callback = callback || noop - let hashAlg = options.hash || 'sha2-256' - let format - let inputEnc + const optionDefaults = { + format: 'dag-cbor', + hash: 'sha2-255', + inputEnc: 'raw' + } + + let hashAlg = options.hash || optionDefaults.hash + let format = optionDefaults.format + let inputEnc = optionDefaults.inputEnc if (options.cid && CID.isCID(options.cid)) { format = options.cid.codec hashAlg = multihash.decode(options.cid.multihash).name prepare() - } else if (options.format) { + } else { format = options.format prepare() - } else { - callback(new Error('Invalid arguments')) } function prepare () { - inputEnc = 'raw' - if (format === 'dag-cbor') { dagCBOR.util.serialize(dagNode, finalize) } From cc7a4f4b44f1017035d8a6db3e5473a1e87fe555 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 4 Jul 2018 09:55:18 +0100 Subject: [PATCH 2/5] fix(dag): fixes to allow options to be optional License: MIT Signed-off-by: Alan Shaw --- src/dag/put.js | 62 ++++++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/src/dag/put.js b/src/dag/put.js index 5287d9d2a..636d39ae4 100644 --- a/src/dag/put.js +++ b/src/dag/put.js @@ -7,57 +7,65 @@ 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') { callback = options - } else if (options.cid && (options.format || options.hash)) { + } + + options = options || {} + + if (options.hash) { + options.hashAlg = options.hash + delete options.hash + } + + if (options.cid && (options.format || options.hashAlg)) { return callback(new Error('Can\'t put dag node. Please provide either `cid` OR `format` and `hash` options.')) - } else if ((options.format && !options.hash) || (!options.format && options.hash)) { + } else if ((options.format && !options.hashAlg) || (!options.format && options.hashAlg)) { return callback(new Error('Can\'t put dag node. Please provide `format` AND `hash` options.')) } - callback = callback || noop + if (options.cid) { + let cid + + try { + cid = new CID(options.cid) + } catch (err) { + return callback(err) + } + + options.format = cid.codec + options.hashAlg = multihash.decode(cid.multihash).name + delete options.cid + } const optionDefaults = { format: 'dag-cbor', - hash: 'sha2-255', + hashAlg: 'sha2-256', inputEnc: 'raw' } - let hashAlg = options.hash || optionDefaults.hash - let format = optionDefaults.format - let inputEnc = optionDefaults.inputEnc + options = Object.assign(optionDefaults, options) - if (options.cid && CID.isCID(options.cid)) { - format = options.cid.codec - hashAlg = multihash.decode(options.cid.multihash).name - prepare() + if (options.format === 'dag-cbor') { + dagCBOR.util.serialize(dagNode, finalize) + } else if (options.format === 'dag-pb') { + dagPB.util.serialize(dagNode, finalize) } else { - format = options.format - prepare() - } - - function prepare () { - if (format === 'dag-cbor') { - dagCBOR.util.serialize(dagNode, finalize) - } - if (format === 'dag-pb') { - dagPB.util.serialize(dagNode, finalize) - } + // FIXME Hopefully already serialized...can we use IPLD to serialise instead? + finalize(null, dagNode) } function finalize (err, serialized) { if (err) { return callback(err) } const sendOptions = { qs: { - hash: hashAlg, - format: format, - 'input-enc': inputEnc + hash: options.hashAlg, + format: options.format, + 'input-enc': options.inputEnc } } sendOneFile(serialized, sendOptions, (err, result) => { From 2ba55ac70d088052b74ebd886268a269c2a935eb Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 4 Jul 2018 10:07:06 +0100 Subject: [PATCH 3/5] chore: update interface-ipfs-core dependency License: MIT Signed-off-by: Alan Shaw --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b6fbee7f5..eebdb507d 100644 --- a/package.json +++ b/package.json @@ -79,7 +79,7 @@ "eslint-plugin-react": "^7.9.1", "go-ipfs-dep": "~0.4.15", "gulp": "^3.9.1", - "interface-ipfs-core": "~0.70.2", + "interface-ipfs-core": "~0.71.0", "ipfsd-ctl": "~0.37.3", "pull-stream": "^3.6.8", "socket.io": "^2.1.1", From e3f4017e4544bfa0eacee5938c2926c203a28460 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 4 Jul 2018 10:15:35 +0100 Subject: [PATCH 4/5] chore: update ipfsd-ctl dependency License: MIT Signed-off-by: Alan Shaw --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index eebdb507d..11eb0e24a 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,7 @@ "go-ipfs-dep": "~0.4.15", "gulp": "^3.9.1", "interface-ipfs-core": "~0.71.0", - "ipfsd-ctl": "~0.37.3", + "ipfsd-ctl": "~0.37.5", "pull-stream": "^3.6.8", "socket.io": "^2.1.1", "socket.io-client": "^2.1.1", From d678a27e44b502bad26423731ed52a8431d0c505 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 4 Jul 2018 10:24:40 +0100 Subject: [PATCH 5/5] fix: increase timeout for addFromURL with wrap-with-directory Sadly we can't guarantee ipfs.io will respond within 5s License: MIT Signed-off-by: Alan Shaw --- test/util.spec.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/util.spec.js b/test/util.spec.js index 83e67e8de..52ce9ebd7 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -160,7 +160,9 @@ describe('.util', () => { .then(out => expectTimeout(ipfs.object.get(out[0].hash), 4000)) }) - it('with wrap-with-directory=true', (done) => { + it('with wrap-with-directory=true', function (done) { + this.timeout(20 * 1000) + ipfs.util.addFromURL('http://ipfs.io/ipfs/QmWjppACLcFLQ2qL38unKQvJBhXH3RUtcGLPk7zmrTwV61/969165.jpg?foo=bar#buzz', { wrapWithDirectory: true }, (err, result) => {