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

Commit 94efd70

Browse files
committed
Use stream-to-value in Block command instead of bl
1 parent f9d3d34 commit 94efd70

8 files changed

+54
-36
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
},
2424
"dependencies": {
2525
"async": "^2.1.4",
26-
"bl": "^1.1.2",
2726
"bs58": "^3.1.0",
2827
"concat-stream": "^1.5.2",
2928
"detect-node": "^2.0.3",
@@ -43,6 +42,7 @@
4342
"peer-id": "^0.8.1",
4443
"peer-info": "^0.8.1",
4544
"promisify-es6": "^1.0.2",
45+
"pump": "^1.0.1",
4646
"qs": "^6.3.0",
4747
"readable-stream": "1.1.14",
4848
"stream-http": "^2.5.0",
@@ -117,4 +117,4 @@
117117
"url": "https://github.com/ipfs/js-ipfs-api/issues"
118118
},
119119
"homepage": "https://github.com/ipfs/js-ipfs-api"
120-
}
120+
}

src/api/block.js

+30-25
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict'
22

33
const promisify = require('promisify-es6')
4-
const bl = require('bl')
54
const Block = require('ipfs-block')
65
const multihash = require('multihashes')
76
const CID = require('cids')
7+
const streamToValue = require('../stream-to-value')
88

99
module.exports = (send) => {
1010
return {
@@ -21,25 +21,27 @@ module.exports = (send) => {
2121
opts = {}
2222
}
2323

24-
return send({
25-
path: 'block/get',
26-
args: args,
27-
qs: opts
28-
}, (err, res) => {
29-
if (err) {
30-
return callback(err)
31-
}
24+
// Transform the response from Buffer or a Stream to a Block
25+
const transform = (res, callback) => {
3226
if (Buffer.isBuffer(res)) {
3327
callback(null, new Block(res))
3428
} else {
35-
res.pipe(bl((err, data) => {
29+
streamToValue(res, (err, data) => {
3630
if (err) {
3731
return callback(err)
3832
}
3933
callback(null, new Block(data))
40-
}))
34+
})
4135
}
42-
})
36+
}
37+
38+
const request = {
39+
path: 'block/get',
40+
args: args,
41+
qs: opts
42+
}
43+
44+
send.andTransform(request, transform, callback)
4345
}),
4446
stat: promisify((args, opts, callback) => {
4547
// TODO this needs to be adjusted with the new go-ipfs http-api
@@ -51,19 +53,22 @@ module.exports = (send) => {
5153
callback = opts
5254
opts = {}
5355
}
54-
return send({
56+
57+
const request = {
5558
path: 'block/stat',
5659
args: args,
5760
qs: opts
58-
}, (err, stats) => {
59-
if (err) {
60-
return callback(err)
61-
}
61+
}
62+
63+
// Transform the response from { Key, Size } objects to { key, size } objects
64+
const transform = (stats, callback) => {
6265
callback(null, {
6366
key: stats.Key,
6467
size: stats.Size
6568
})
66-
})
69+
}
70+
71+
send.andTransform(request, transform, callback)
6772
}),
6873
put: promisify((block, cid, callback) => {
6974
// TODO this needs to be adjusted with the new go-ipfs http-api
@@ -81,15 +86,15 @@ module.exports = (send) => {
8186
block = block.data
8287
}
8388

84-
return send({
89+
const request = {
8590
path: 'block/put',
8691
files: block
87-
}, (err, blockInfo) => {
88-
if (err) {
89-
return callback(err)
90-
}
91-
callback(null, new Block(block))
92-
})
92+
}
93+
94+
// Transform the response to a Block
95+
const transform = (blockInfo, callback) => callback(null, new Block(block))
96+
97+
send.andTransform(request, transform, callback)
9398
})
9499
}
95100
}

src/api/log.js

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

3+
const pump = require('pump')
34
const ndjson = require('ndjson')
45
const promisify = require('promisify-es6')
56

@@ -12,7 +13,8 @@ module.exports = (send) => {
1213
if (err) {
1314
return callback(err)
1415
}
15-
callback(null, response.pipe(ndjson.parse()))
16+
const outputStream = pump(response, ndjson.parse())
17+
callback(null, outputStream)
1618
})
1719
})
1820
}

src/api/object.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const DAGNode = dagPB.DAGNode
55
const DAGLink = dagPB.DAGLink
66
const promisify = require('promisify-es6')
77
const bs58 = require('bs58')
8-
const bl = require('bl')
8+
const streamToValue = require('../stream-to-value')
99
const cleanMultihash = require('../clean-multihash')
1010
const LRU = require('lru-cache')
1111
const lruOptions = {
@@ -188,7 +188,7 @@ module.exports = (send) => {
188188
}
189189

190190
if (typeof result.pipe === 'function') {
191-
result.pipe(bl(callback))
191+
streamToValue(result, callback)
192192
} else {
193193
callback(null, result)
194194
}

src/dagnode-stream.js

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

3+
const pump = require('pump')
34
const TransformStream = require('readable-stream').Transform
45
const streamToValue = require('./stream-to-value')
56
const getDagNode = require('./get-dagnode')
@@ -31,7 +32,9 @@ class DAGNodeStream extends TransformStream {
3132
}
3233

3334
static streamToValue (send, inputStream, callback) {
34-
const outputStream = inputStream.pipe(new DAGNodeStream({ send: send }))
35+
const outputStream = pump(inputStream, new DAGNodeStream({ send: send }), (err) => {
36+
if (err) callback(err)
37+
})
3538
streamToValue(outputStream, callback)
3639
}
3740

src/request-api.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const Qs = require('qs')
44
const isNode = require('detect-node')
55
const ndjson = require('ndjson')
6+
const pump = require('pump')
67
const once = require('once')
78
const getFilesStream = require('./get-files-stream')
89
const streamToValue = require('./stream-to-value')
@@ -43,7 +44,8 @@ function onRes (buffer, cb) {
4344

4445
// Return a stream of JSON objects
4546
if (chunkedObjects && isJson) {
46-
return cb(null, res.pipe(ndjson.parse()))
47+
const outputStream = pump(res, ndjson.parse())
48+
return cb(null, outputStream)
4749
}
4850

4951
// Return a JSON object

src/stream-to-value.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
'use strict'
22

3+
const pump = require('pump')
34
const concat = require('concat-stream')
45

56
/*
67
Concatenate a stream to a single value.
78
*/
89
function streamToValue (res, callback) {
9-
res.pipe(concat((data) => callback(null, data)))
10+
const done = (data) => callback(null, data)
11+
pump(res, concat(done), (err) => {
12+
if (err) callback(err)
13+
})
1014
}
1115

1216
module.exports = streamToValue

src/tar-stream-to-objects.js

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

3+
const pump = require('pump')
34
const tar = require('tar-stream')
45
const ReadableStream = require('readable-stream').Readable
56

@@ -9,7 +10,7 @@ class ObjectsStreams extends ReadableStream {
910
super(opts)
1011
}
1112

12-
_read () {}
13+
_read () {}
1314
}
1415

1516
/*
@@ -20,9 +21,9 @@ class ObjectsStreams extends ReadableStream {
2021
*/
2122
const TarStreamToObjects = (inputStream, callback) => {
2223
let outputStream = new ObjectsStreams()
24+
let extractStream = tar.extract()
2325

24-
inputStream
25-
.pipe(tar.extract())
26+
extractStream
2627
.on('entry', (header, stream, next) => {
2728
stream.on('end', next)
2829

@@ -40,6 +41,7 @@ const TarStreamToObjects = (inputStream, callback) => {
4041
})
4142
.on('finish', () => outputStream.push(null))
4243

44+
pump(inputStream, extractStream)
4345
callback(null, outputStream)
4446
}
4547

0 commit comments

Comments
 (0)