diff --git a/package.json b/package.json index 3c49b0c1c..0d07189e8 100644 --- a/package.json +++ b/package.json @@ -80,7 +80,7 @@ "eslint-plugin-react": "^7.10.0", "go-ipfs-dep": "~0.4.17", "gulp": "^3.9.1", - "interface-ipfs-core": "~0.75.1", + "interface-ipfs-core": "~0.78.0", "ipfsd-ctl": "~0.39.0", "pull-stream": "^3.6.8", "socket.io": "^2.1.1", diff --git a/src/block/put.js b/src/block/put.js index 0ec1e5c5f..5c9eba414 100644 --- a/src/block/put.js +++ b/src/block/put.js @@ -3,35 +3,72 @@ const promisify = require('promisify-es6') const Block = require('ipfs-block') const CID = require('cids') -const once = require('once') +const multihash = require('multihashes') const SendOneFile = require('../utils/send-one-file') module.exports = (send) => { const sendOneFile = SendOneFile(send, 'block/put') - return promisify((block, cid, _callback) => { - // TODO this needs to be adjusted with the new go-ipfs http-api - if (typeof cid === 'function') { - _callback = cid - cid = {} + return promisify((block, options, callback) => { + if (typeof options === 'function') { + callback = options + options = {} } - const callback = once(_callback) + options = options || {} if (Array.isArray(block)) { return callback(new Error('block.put accepts only one block')) } - if (typeof block === 'object' && block.data) { - block = block.data + if (Buffer.isBuffer(block)) { + block = { data: block } } - sendOneFile(block, {}, (err, result) => { + if (!block || !block.data) { + return callback(new Error('invalid block arg')) + } + + const qs = {} + + if (block.cid || options.cid) { + let cid + + try { + cid = new CID(block.cid || options.cid) + } catch (err) { + return callback(err) + } + + const { name, length } = multihash.decode(cid.multihash) + + qs.format = cid.codec + qs.mhtype = name + qs.mhlen = length + qs.version = cid.version + } else { + if (options.format) qs.format = options.format + if (options.mhtype) qs.mhtype = options.mhtype + if (options.mhlen) qs.mhlen = options.mhlen + if (options.version != null) qs.version = options.version + } + + sendOneFile(block.data, { qs }, (err, result) => { if (err) { - return callback(err) // early + // Retry with "protobuf" format for go-ipfs + // TODO: remove when https://github.com/ipfs/go-cid/issues/75 resolved + if (qs.format === 'dag-pb') { + qs.format = 'protobuf' + return sendOneFile(block.data, { qs }, (err, result) => { + if (err) return callback(err) + callback(null, new Block(block.data, new CID(result.Key))) + }) + } + + return callback(err) } - callback(null, new Block(block, new CID(result.Key))) + callback(null, new Block(block.data, new CID(result.Key))) }) }) } diff --git a/test/interface.spec.js b/test/interface.spec.js index 5f169433f..30fe04a92 100644 --- a/test/interface.spec.js +++ b/test/interface.spec.js @@ -129,6 +129,11 @@ describe('interface-ipfs-core tests', () => { isNode ? null : { name: 'should get a directory', reason: 'FIXME https://github.com/ipfs/js-ipfs-api/issues/339' + }, + // files.write + { + name: 'should write to deeply nested non existent file with create and parents flags', + reason: 'TODO remove when 0.4.18 is released https://github.com/ipfs/go-ipfs/pull/5359' } ] })