Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

fix: block.put options #844

Merged
merged 4 commits into from
Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
61 changes: 49 additions & 12 deletions src/block/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't remember but I wonder if this was a protection to go-ipfs sometimes sending multiple errors ?!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, I guess we'll find out soon ;)

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'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the key change for me that would have allowed me to complete #815. Still learned a lot from trying to work that story. Time to find a new issue to dig into. :)

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)))
})
})
}
5 changes: 5 additions & 0 deletions test/interface.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
]
})
Expand Down