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

fix(dag): ensure dag.put() allows for optional options #801

Merged
Changes from 1 commit
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
25 changes: 15 additions & 10 deletions src/dag/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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 () {}
Expand All @@ -15,29 +14,35 @@ module.exports = (send) => {

return promisify((dagNode, options, callback) => {
if (typeof options === 'function') {
return setImmediate(() => callback(new Error('no options were passed')))
callback = options
} else if (options.cid && (options.format || options.hash)) {
Copy link
Author

@0x-r4bbit 0x-r4bbit Jul 3, 2018

Choose a reason for hiding this comment

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

Notice that js-ipfs-api's implementation expects options.hash instead of options.hashAlg. This seems to be inconsistent with the spec as per https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DAG.md#javascript---ipfsdagputdagnode-options-callback

Changing this would be a breaking change. @alanshaw how do you feel about this?

return callback(new Error('Can\'t put dag node. Please provide either `cid` OR `format` and `hash` options.'))
} else if ((options.format && !options.hash) || (!options.format && options.hash)) {
return callback(new Error('Can\'t put dag node. Please provide `format` AND `hash` options.'))
Copy link
Author

Choose a reason for hiding this comment

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

Turns out this is a breaking change as well (which is also one of the reasons CI fails at this time) Prior to this commit, it was okay to just have format with hash falling back to sha2-256.

Again, this would be a point of either introducing a breaking change and staying consistent with js-ipfs API, or leaving as is and live with the consequences of inconsistency.

@alanshaw your call.

}

callback = callback || noop

let hashAlg = options.hash || 'sha2-256'
let format
let inputEnc
const optionDefaults = {
format: 'dag-cbor',
hash: 'sha2-255',
inputEnc: 'raw'
}

let hashAlg = options.hash || optionDefaults.hash
let format = optionDefaults.format
let inputEnc = optionDefaults.inputEnc

if (options.cid && CID.isCID(options.cid)) {
format = options.cid.codec
hashAlg = multihash.decode(options.cid.multihash).name
prepare()
} else if (options.format) {
} else {
format = options.format
prepare()
} else {
callback(new Error('Invalid arguments'))
}

function prepare () {
inputEnc = 'raw'

if (format === 'dag-cbor') {
dagCBOR.util.serialize(dagNode, finalize)
}
Expand Down