From 0044fa0fd2e02055ced0472f4080e8cbdf9c824f Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 30 Aug 2018 14:56:16 +0100 Subject: [PATCH 1/4] fix: block.put options License: MIT Signed-off-by: Alan Shaw --- src/block/put.js | 49 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/src/block/put.js b/src/block/put.js index 0ec1e5c5f..91f648b87 100644 --- a/src/block/put.js +++ b/src/block/put.js @@ -3,35 +3,62 @@ 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 } - callback(null, new Block(block, new CID(result.Key))) + callback(null, new Block(block.data, new CID(result.Key))) }) }) } From 2bc33d9d5334e317a26e0e800108c4ce38cf19ae Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 20 Sep 2018 14:44:33 +0100 Subject: [PATCH 2/4] fix: workaround for https://github.com/ipfs/go-cid/issues/75 License: MIT Signed-off-by: Alan Shaw --- src/block/put.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/block/put.js b/src/block/put.js index 91f648b87..5c9eba414 100644 --- a/src/block/put.js +++ b/src/block/put.js @@ -55,7 +55,17 @@ module.exports = (send) => { 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.data, new CID(result.Key))) From 81d1518233b35561bef0be39faa0286323ef7cba Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 20 Sep 2018 15:05:14 +0100 Subject: [PATCH 3/4] 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 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", From a39c2dea488005dcc61d5fd059440c278d9c04c7 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Thu, 20 Sep 2018 15:41:39 +0100 Subject: [PATCH 4/4] chore: skip test for future feature License: MIT Signed-off-by: Alan Shaw --- test/interface.spec.js | 5 +++++ 1 file changed, 5 insertions(+) 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' } ] })