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

Commit c53d3cd

Browse files
0x-r4bbitdaviddias
authored andcommitted
fix(dag): dag.put() allows for optional options (#801)
* 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 <pascal.precht@gmail.com> * fix(dag): fixes to allow options to be optional License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io> * chore: update interface-ipfs-core dependency License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io> * chore: update ipfsd-ctl dependency License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io> * 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 <alan@tableflip.io>
1 parent dc1cb72 commit c53d3cd

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

src/dag/put.js

+40-27
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,67 @@ 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

11-
function noop () {}
12-
1310
module.exports = (send) => {
1411
const sendOneFile = SendOneFile(send, 'dag/put')
1512

1613
return promisify((dagNode, options, callback) => {
1714
if (typeof options === 'function') {
18-
return setImmediate(() => callback(new Error('no options were passed')))
15+
callback = options
1916
}
2017

21-
callback = callback || noop
18+
options = options || {}
2219

23-
let hashAlg = options.hash || 'sha2-256'
24-
let format
25-
let inputEnc
20+
if (options.hash) {
21+
options.hashAlg = options.hash
22+
delete options.hash
23+
}
2624

27-
if (options.cid && CID.isCID(options.cid)) {
28-
format = options.cid.codec
29-
hashAlg = multihash.decode(options.cid.multihash).name
30-
prepare()
31-
} else if (options.format) {
32-
format = options.format
33-
prepare()
34-
} else {
35-
callback(new Error('Invalid arguments'))
25+
if (options.cid && (options.format || options.hashAlg)) {
26+
return callback(new Error('Can\'t put dag node. Please provide either `cid` OR `format` and `hash` options.'))
27+
} else if ((options.format && !options.hashAlg) || (!options.format && options.hashAlg)) {
28+
return callback(new Error('Can\'t put dag node. Please provide `format` AND `hash` options.'))
3629
}
3730

38-
function prepare () {
39-
inputEnc = 'raw'
31+
if (options.cid) {
32+
let cid
4033

41-
if (format === 'dag-cbor') {
42-
dagCBOR.util.serialize(dagNode, finalize)
43-
}
44-
if (format === 'dag-pb') {
45-
dagPB.util.serialize(dagNode, finalize)
34+
try {
35+
cid = new CID(options.cid)
36+
} catch (err) {
37+
return callback(err)
4638
}
39+
40+
options.format = cid.codec
41+
options.hashAlg = multihash.decode(cid.multihash).name
42+
delete options.cid
43+
}
44+
45+
const optionDefaults = {
46+
format: 'dag-cbor',
47+
hashAlg: 'sha2-256',
48+
inputEnc: 'raw'
49+
}
50+
51+
options = Object.assign(optionDefaults, options)
52+
53+
if (options.format === 'dag-cbor') {
54+
dagCBOR.util.serialize(dagNode, finalize)
55+
} else if (options.format === 'dag-pb') {
56+
dagPB.util.serialize(dagNode, finalize)
57+
} else {
58+
// FIXME Hopefully already serialized...can we use IPLD to serialise instead?
59+
finalize(null, dagNode)
4760
}
4861

4962
function finalize (err, serialized) {
5063
if (err) { return callback(err) }
5164
const sendOptions = {
5265
qs: {
53-
hash: hashAlg,
54-
format: format,
55-
'input-enc': inputEnc
66+
hash: options.hashAlg,
67+
format: options.format,
68+
'input-enc': options.inputEnc
5669
}
5770
}
5871
sendOneFile(serialized, sendOptions, (err, result) => {

test/util.spec.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ describe('.util', () => {
160160
.then(out => expectTimeout(ipfs.object.get(out[0].hash), 4000))
161161
})
162162

163-
it('with wrap-with-directory=true', (done) => {
163+
it('with wrap-with-directory=true', function (done) {
164+
this.timeout(20 * 1000)
165+
164166
ipfs.util.addFromURL('http://ipfs.io/ipfs/QmWjppACLcFLQ2qL38unKQvJBhXH3RUtcGLPk7zmrTwV61/969165.jpg?foo=bar#buzz', {
165167
wrapWithDirectory: true
166168
}, (err, result) => {

0 commit comments

Comments
 (0)