diff --git a/package.json b/package.json index b6fbee7f5..11eb0e24a 100644 --- a/package.json +++ b/package.json @@ -79,8 +79,8 @@ "eslint-plugin-react": "^7.9.1", "go-ipfs-dep": "~0.4.15", "gulp": "^3.9.1", - "interface-ipfs-core": "~0.70.2", - "ipfsd-ctl": "~0.37.3", + "interface-ipfs-core": "~0.71.0", + "ipfsd-ctl": "~0.37.5", "pull-stream": "^3.6.8", "socket.io": "^2.1.1", "socket.io-client": "^2.1.1", diff --git a/src/dag/put.js b/src/dag/put.js index c0c2f211a..636d39ae4 100644 --- a/src/dag/put.js +++ b/src/dag/put.js @@ -5,54 +5,67 @@ 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 () {} - 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 = options } - callback = callback || noop + options = options || {} - let hashAlg = options.hash || 'sha2-256' - let format - let inputEnc + if (options.hash) { + options.hashAlg = options.hash + delete options.hash + } - 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')) + 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.hashAlg) || (!options.format && options.hashAlg)) { + return callback(new Error('Can\'t put dag node. Please provide `format` AND `hash` options.')) } - function prepare () { - inputEnc = 'raw' + if (options.cid) { + let cid - if (format === 'dag-cbor') { - dagCBOR.util.serialize(dagNode, finalize) - } - if (format === 'dag-pb') { - dagPB.util.serialize(dagNode, finalize) + 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', + hashAlg: 'sha2-256', + inputEnc: 'raw' + } + + options = Object.assign(optionDefaults, options) + + if (options.format === 'dag-cbor') { + dagCBOR.util.serialize(dagNode, finalize) + } else if (options.format === 'dag-pb') { + dagPB.util.serialize(dagNode, finalize) + } else { + // 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) => { 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) => {