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

Commit

Permalink
fix(dag): make options in put API optional (#1415)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* chore: update interface-ipfs-core dependency

License: MIT
Signed-off-by: Alan Shaw <[email protected]>
  • Loading branch information
0x-r4bbit authored and alanshaw committed Jul 3, 2018
1 parent 18888be commit d299ed7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"expose-loader": "~0.7.5",
"form-data": "^2.3.2",
"hat": "0.0.3",
"interface-ipfs-core": "~0.70.2",
"interface-ipfs-core": "~0.70.3",
"ipfsd-ctl": "~0.37.3",
"mocha": "^5.1.1",
"ncp": "^2.0.0",
Expand Down
15 changes: 15 additions & 0 deletions src/core/components/dag.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ const flattenDeep = require('lodash/flattenDeep')
module.exports = function dag (self) {
return {
put: promisify((dagNode, options, callback) => {
if (typeof options === 'function') {
callback = options
} else if (options.cid && (options.format || options.hashAlg)) {

This comment has been minimized.

Copy link
@negamaxi

negamaxi Jul 29, 2018

Contributor
await ipfs.dag.put(obj)

Cannot read property 'cid' of undefined

This comment has been minimized.

Copy link
@0x-r4bbit

0x-r4bbit Jul 29, 2018

Author Contributor

@negamaxi is that something you ran into?

This definitely needs fixing then.

This comment has been minimized.

Copy link
@0x-r4bbit

0x-r4bbit Jul 29, 2018

Author Contributor

And it makes sense, because we didn't account for when there's also no callback given. This commit assumed that there's always going to be either options or a callback.

Didn't think of async/await use cases - my bad! 😥

This comment has been minimized.

Copy link
@negamaxi

negamaxi Jul 29, 2018

Contributor

@PascalPrecht I've created a pull request with additional tests ipfs-inactive/interface-js-ipfs-core#338. I'm ready to provide a patch for js-ipfs too, if needed.

This comment has been minimized.

Copy link
@alanshaw

alanshaw Jul 29, 2018

Member

@negamaxi yes please for a PR ❤️

This comment has been minimized.

Copy link
@alanshaw

alanshaw Jul 29, 2018

Member

Tracking this issue here #1479

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

const optionDefaults = {
format: 'dag-cbor',
hashAlg: 'sha2-255'
}

options = options.cid ? options : Object.assign({}, optionDefaults, options)

self._ipld.put(dagNode, options, callback)
}),

Expand Down

0 comments on commit d299ed7

Please sign in to comment.