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

fix: block.put() options #793

Closed
wants to merge 2 commits into from
Closed
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
35 changes: 28 additions & 7 deletions src/block/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ const Block = require('ipfs-block')
const CID = require('cids')
const once = require('once')
const SendOneFile = require('../utils/send-one-file')
const multihashes = require('multihashes')

module.exports = (send) => {
const sendOneFile = SendOneFile(send, 'block/put')

return promisify((block, cid, _callback) => {
return promisify((block, opts, _callback) => {
// TODO this needs to be adjusted with the new go-ipfs http-api
if (typeof cid === 'function') {
_callback = cid
cid = {}
if (typeof opts === 'function') {
_callback = opts
opts = {}
}

const callback = once(_callback)
Expand All @@ -22,16 +23,36 @@ module.exports = (send) => {
return callback(new Error('block.put accepts only one block'))
}

let cid = opts.cid

if (typeof block === 'object' && block.data) {
if (block.cid) cid = block.cid
block = block.data
}

sendOneFile(block, {}, (err, result) => {
let qs = Object.assign(opts, {
'input-enc': 'raw',
hashAlg: opts.mhtype || 'sha2-256'
})

if (cid && CID.isCID(cid)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

At this point cid could be a string, buffer or CID instance (spec). isCID only checks if the parameter is a CID instance, so currently the passed cid will be ignored if it's not a CID instance!

qs.format = cid.codec
qs.hashAlg = multihashes.decode(cid.multihash).name
}
delete qs.cid

let _send = sendOneFile

if (qs.format && qs.format.startsWith('dag-')) {
_send = SendOneFile(send, 'dag/put')
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm mildly against adding this workaround here. I think it'll be unexpected for this method to call dag/put and without some comments or docs this behaviour will become confusing for contributors as well. It also sounds as though it's specific to go-ipfs and I'd rather not add implementation specific workarounds here in the API if we can avoid it. I'd like to know if js-ipfs has the same issue.

Is there an open issue in go-ipfs for this? It would be good to get this fixed there.


_send(block, {qs}, (err, result) => {
if (err) {
return callback(err) // early
}

callback(null, new Block(block, new CID(result.Key)))
let cid = result.Key || result.Cid['/']
callback(null, new Block(block, new CID(cid)))
})
})
}