Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit d299ed7

Browse files
0x-r4bbitalanshaw
authored andcommitted
fix(dag): make options in put API optional (#1415)
* fix(core/components/dag): make options in `put` API optional The [dag.put](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/DAG.md#dagput) interface takes an options object which is required, but should be optional with decent defaults. See dedicated test here: ipfs-inactive/interface-js-ipfs-core#316 This commit implements this behaviour. **Before**: ```js ipfs.dag.put(obj, options, (err, cid) => {...}); ``` ^ Prior to this commit, without passing `options`, this call resulted in an error. **After**: ```js ipfs.dag.put(obj, (err, cid) => {...}); ``` ^ This is now perfectly fine. Fixes #1395 License: MIT Signed-off-by: Pascal Precht <pascal.precht@gmail.com> * chore: update interface-ipfs-core dependency License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
1 parent 18888be commit d299ed7

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"expose-loader": "~0.7.5",
7474
"form-data": "^2.3.2",
7575
"hat": "0.0.3",
76-
"interface-ipfs-core": "~0.70.2",
76+
"interface-ipfs-core": "~0.70.3",
7777
"ipfsd-ctl": "~0.37.3",
7878
"mocha": "^5.1.1",
7979
"ncp": "^2.0.0",

src/core/components/dag.js

+15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ const flattenDeep = require('lodash/flattenDeep')
99
module.exports = function dag (self) {
1010
return {
1111
put: promisify((dagNode, options, callback) => {
12+
if (typeof options === 'function') {
13+
callback = options
14+
} else if (options.cid && (options.format || options.hashAlg)) {
15+
return callback(new Error('Can\'t put dag node. Please provide either `cid` OR `format` and `hashAlg` options.'))
16+
} else if ((options.format && !options.hashAlg) || (!options.format && options.hashAlg)) {
17+
return callback(new Error('Can\'t put dag node. Please provide `format` AND `hashAlg` options.'))
18+
}
19+
20+
const optionDefaults = {
21+
format: 'dag-cbor',
22+
hashAlg: 'sha2-255'
23+
}
24+
25+
options = options.cid ? options : Object.assign({}, optionDefaults, options)
26+
1227
self._ipld.put(dagNode, options, callback)
1328
}),
1429

0 commit comments

Comments
 (0)