Skip to content

Commit 78dd63c

Browse files
committed
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]>
1 parent 6413fa1 commit 78dd63c

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/dag/put.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const dagCBOR = require('ipld-dag-cbor')
55
const promisify = require('promisify-es6')
66
const CID = require('cids')
77
const multihash = require('multihashes')
8-
const setImmediate = require('async/setImmediate')
98
const SendOneFile = require('../utils/send-one-file')
109

1110
function noop () {}
@@ -15,29 +14,35 @@ module.exports = (send) => {
1514

1615
return promisify((dagNode, options, callback) => {
1716
if (typeof options === 'function') {
18-
return setImmediate(() => callback(new Error('no options were passed')))
17+
callback = options
18+
} else if (options.cid && (options.format || options.hash)) {
19+
return callback(new Error('Can\'t put dag node. Please provide either `cid` OR `format` and `hash` options.'))
20+
} else if ((options.format && !options.hash) || (!options.format && options.hash)) {
21+
return callback(new Error('Can\'t put dag node. Please provide `format` AND `hash` options.'))
1922
}
2023

2124
callback = callback || noop
2225

23-
let hashAlg = options.hash || 'sha2-256'
24-
let format
25-
let inputEnc
26+
const optionDefaults = {
27+
format: 'dag-cbor',
28+
hash: 'sha2-255',
29+
inputEnc: 'raw'
30+
}
31+
32+
let hashAlg = options.hash || optionDefaults.hash
33+
let format = optionDefaults.format
34+
let inputEnc = optionDefaults.inputEnc
2635

2736
if (options.cid && CID.isCID(options.cid)) {
2837
format = options.cid.codec
2938
hashAlg = multihash.decode(options.cid.multihash).name
3039
prepare()
31-
} else if (options.format) {
40+
} else {
3241
format = options.format
3342
prepare()
34-
} else {
35-
callback(new Error('Invalid arguments'))
3643
}
3744

3845
function prepare () {
39-
inputEnc = 'raw'
40-
4146
if (format === 'dag-cbor') {
4247
dagCBOR.util.serialize(dagNode, finalize)
4348
}

0 commit comments

Comments
 (0)