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

Commit a4e43e8

Browse files
committed
feat(block): make the block api follow the interface definition
1 parent b24dab8 commit a4e43e8

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"detect-node": "^2.0.3",
2525
"flatmap": "0.0.3",
2626
"glob": "^7.0.5",
27+
"ipfs-block": "^0.3.0",
2728
"ipfs-merkle-dag": "^0.6.0",
2829
"is-ipfs": "^0.2.0",
2930
"isstream": "^0.1.2",
@@ -47,7 +48,7 @@
4748
"chai": "^3.5.0",
4849
"gulp": "^3.9.1",
4950
"hapi": "^14.1.0",
50-
"interface-ipfs-core": "^0.13.0",
51+
"interface-ipfs-core": "^0.14.0",
5152
"ipfsd-ctl": "^0.14.0",
5253
"pre-commit": "^1.1.3",
5354
"socket.io": "^1.4.8",
@@ -99,4 +100,4 @@
99100
"url": "https://github.com./ipfs/js-ipfs-api/issues"
100101
},
101102
"homepage": "https://github.com./ipfs/js-ipfs-api"
102-
}
103+
}

src/api/block.js

+35-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict'
22

33
const promisify = require('promisify-es6')
4+
const bl = require('bl')
5+
const Block = require('ipfs-block')
46

57
module.exports = (send) => {
68
return {
@@ -13,7 +15,17 @@ module.exports = (send) => {
1315
path: 'block/get',
1416
args: args,
1517
qs: opts
16-
}, callback)
18+
}, (err, res) => {
19+
if (err) {
20+
return callback(err)
21+
}
22+
res.pipe(bl((err, data) => {
23+
if (err) {
24+
return callback(err)
25+
}
26+
callback(null, new Block(data))
27+
}))
28+
})
1729
}),
1830
stat: promisify((args, opts, callback) => {
1931
if (typeof (opts) === 'function') {
@@ -24,18 +36,35 @@ module.exports = (send) => {
2436
path: 'block/stat',
2537
args: args,
2638
qs: opts
27-
}, callback)
39+
}, (err, stats) => {
40+
if (err) {
41+
return callback(err)
42+
}
43+
callback(null, {
44+
key: stats.Key,
45+
size: stats.Size
46+
})
47+
})
2848
}),
29-
put: promisify((file, callback) => {
30-
if (Array.isArray(file)) {
49+
put: promisify((block, callback) => {
50+
if (Array.isArray(block)) {
3151
const err = new Error('block.put() only accepts 1 file')
3252
return callback(err)
3353
}
3454

55+
if (typeof block === 'object' && block.data) {
56+
block = block.data
57+
}
58+
3559
return send({
3660
path: 'block/put',
37-
files: file
38-
}, callback)
61+
files: block
62+
}, (err, blockInfo) => {
63+
if (err) {
64+
return callback(err)
65+
}
66+
callback(null, new Block(block))
67+
})
3968
})
4069
}
4170
}

0 commit comments

Comments
 (0)