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

Commit

Permalink
fix(dag): dag.put() allows for optional options (#801)
Browse files Browse the repository at this point in the history
* fix(dag): ensure `dag.put()` allows for optional options

This is to align with API changes made in

ipfs-inactive/interface-js-ipfs-core@011c417

and

ipfs/js-ipfs#1415

License: MIT
Signed-off-by: Pascal Precht <[email protected]>

* fix(dag): fixes to allow options to be optional

License: MIT
Signed-off-by: Alan Shaw <[email protected]>

* chore: update interface-ipfs-core dependency

License: MIT
Signed-off-by: Alan Shaw <[email protected]>

* chore: update ipfsd-ctl dependency

License: MIT
Signed-off-by: Alan Shaw <[email protected]>

* fix: increase timeout for addFromURL with wrap-with-directory

Sadly we can't guarantee ipfs.io will respond within 5s

License: MIT
Signed-off-by: Alan Shaw <[email protected]>
  • Loading branch information
0x-r4bbit authored and daviddias committed Jul 4, 2018
1 parent dc1cb72 commit c53d3cd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 28 deletions.
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) {
this.timeout(20 * 1000)

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

0 comments on commit c53d3cd

Please sign in to comment.