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

Commit 0044fa0

Browse files
alanshawAlan Shaw
authored and
Alan Shaw
committed
fix: block.put options
License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 63682dd commit 0044fa0

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

src/block/put.js

+38-11
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,62 @@
33
const promisify = require('promisify-es6')
44
const Block = require('ipfs-block')
55
const CID = require('cids')
6-
const once = require('once')
6+
const multihash = require('multihashes')
77
const SendOneFile = require('../utils/send-one-file')
88

99
module.exports = (send) => {
1010
const sendOneFile = SendOneFile(send, 'block/put')
1111

12-
return promisify((block, cid, _callback) => {
13-
// TODO this needs to be adjusted with the new go-ipfs http-api
14-
if (typeof cid === 'function') {
15-
_callback = cid
16-
cid = {}
12+
return promisify((block, options, callback) => {
13+
if (typeof options === 'function') {
14+
callback = options
15+
options = {}
1716
}
1817

19-
const callback = once(_callback)
18+
options = options || {}
2019

2120
if (Array.isArray(block)) {
2221
return callback(new Error('block.put accepts only one block'))
2322
}
2423

25-
if (typeof block === 'object' && block.data) {
26-
block = block.data
24+
if (Buffer.isBuffer(block)) {
25+
block = { data: block }
2726
}
2827

29-
sendOneFile(block, {}, (err, result) => {
28+
if (!block || !block.data) {
29+
return callback(new Error('invalid block arg'))
30+
}
31+
32+
const qs = {}
33+
34+
if (block.cid || options.cid) {
35+
let cid
36+
37+
try {
38+
cid = new CID(block.cid || options.cid)
39+
} catch (err) {
40+
return callback(err)
41+
}
42+
43+
const { name, length } = multihash.decode(cid.multihash)
44+
45+
qs.format = cid.codec
46+
qs.mhtype = name
47+
qs.mhlen = length
48+
qs.version = cid.version
49+
} else {
50+
if (options.format) qs.format = options.format
51+
if (options.mhtype) qs.mhtype = options.mhtype
52+
if (options.mhlen) qs.mhlen = options.mhlen
53+
if (options.version != null) qs.version = options.version
54+
}
55+
56+
sendOneFile(block.data, { qs }, (err, result) => {
3057
if (err) {
3158
return callback(err) // early
3259
}
3360

34-
callback(null, new Block(block, new CID(result.Key)))
61+
callback(null, new Block(block.data, new CID(result.Key)))
3562
})
3663
})
3764
}

0 commit comments

Comments
 (0)