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
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
"eslint-plugin-react": "^7.9.1",
"go-ipfs-dep": "~0.4.15",
"gulp": "^3.9.1",
"interface-ipfs-core": "~0.70.2",
"ipfsd-ctl": "~0.37.3",
"interface-ipfs-core": "~0.71.0",
"ipfsd-ctl": "~0.37.5",
"pull-stream": "^3.6.8",
"socket.io": "^2.1.1",
"socket.io-client": "^2.1.1",
Expand Down
67 changes: 40 additions & 27 deletions src/dag/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,67 @@ 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 () {}

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

return promisify((dagNode, options, callback) => {
if (typeof options === 'function') {
return setImmediate(() => callback(new Error('no options were passed')))
callback = options
}

callback = callback || noop
options = options || {}

let hashAlg = options.hash || 'sha2-256'
let format
let inputEnc
if (options.hash) {
options.hashAlg = options.hash
delete options.hash
}

if (options.cid && CID.isCID(options.cid)) {
format = options.cid.codec
hashAlg = multihash.decode(options.cid.multihash).name
prepare()
} else if (options.format) {
format = options.format
prepare()
} else {
callback(new Error('Invalid arguments'))
if (options.cid && (options.format || options.hashAlg)) {
return callback(new Error('Can\'t put dag node. Please provide either `cid` OR `format` and `hash` options.'))
} else if ((options.format && !options.hashAlg) || (!options.format && options.hashAlg)) {
return callback(new Error('Can\'t put dag node. Please provide `format` AND `hash` options.'))
}

function prepare () {
inputEnc = 'raw'
if (options.cid) {
let cid

if (format === 'dag-cbor') {
dagCBOR.util.serialize(dagNode, finalize)
}
if (format === 'dag-pb') {
dagPB.util.serialize(dagNode, finalize)
try {
cid = new CID(options.cid)
} catch (err) {
return callback(err)
}

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

const optionDefaults = {
format: 'dag-cbor',
hashAlg: 'sha2-256',
inputEnc: 'raw'
}

options = Object.assign(optionDefaults, options)

if (options.format === 'dag-cbor') {
dagCBOR.util.serialize(dagNode, finalize)
} else if (options.format === 'dag-pb') {
dagPB.util.serialize(dagNode, finalize)
} else {
// FIXME Hopefully already serialized...can we use IPLD to serialise instead?
finalize(null, dagNode)
}

function finalize (err, serialized) {
if (err) { return callback(err) }
const sendOptions = {
qs: {
hash: hashAlg,
format: format,
'input-enc': inputEnc
hash: options.hashAlg,
format: options.format,
'input-enc': options.inputEnc
}
}
sendOneFile(serialized, sendOptions, (err, result) => {
Expand Down
4 changes: 3 additions & 1 deletion test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ describe('.util', () => {
.then(out => expectTimeout(ipfs.object.get(out[0].hash), 4000))
})

it('with wrap-with-directory=true', (done) => {
it('with wrap-with-directory=true', function (done) {
Copy link
Author

Choose a reason for hiding this comment

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

I think the function keyword is a leftover here?

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 using function here because on the next time we want to call this.timeout. Mocha provides an object with this method on it for the function context (this) but arrow functions do not allow you to change the context.

Copy link
Author

Choose a reason for hiding this comment

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

Got it. Thanks for clarifying!

this.timeout(20 * 1000)

ipfs.util.addFromURL('http://ipfs.io/ipfs/QmWjppACLcFLQ2qL38unKQvJBhXH3RUtcGLPk7zmrTwV61/969165.jpg?foo=bar#buzz', {
wrapWithDirectory: true
}, (err, result) => {
Expand Down