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

Commit 1561a69

Browse files
committed
feat(breaking change): use stream on stats.bw
1 parent 4f7999d commit 1561a69

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

src/stats/bw.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
'use strict'
22

33
const promisify = require('promisify-es6')
4-
const streamToValue = require('../utils/stream-to-value')
4+
const { Transform } = require('readable-stream')
55

66
const transform = function (res, callback) {
7-
streamToValue(res, (err, data) => {
8-
if (err) {
9-
return callback(err)
7+
const output = new Transform({
8+
objectMode: true,
9+
transform(chunk, encoding, cb) {
10+
cb(null, {
11+
totalIn: chunk.TotalIn,
12+
totalOut: chunk.TotalOut,
13+
rateIn: chunk.RateIn,
14+
rateOut: chunk.RateOut
15+
})
1016
}
11-
12-
callback(null, {
13-
totalIn: data[0].TotalIn,
14-
totalOut: data[0].TotalOut,
15-
rateIn: data[0].RateIn,
16-
rateOut: data[0].RateOut
17-
})
1817
})
18+
19+
res.pipe(output)
20+
callback(null, output)
1921
}
2022

2123
module.exports = (send) => {

test/stats.spec.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ describe('stats', function () {
4949
ipfs.stats.bw((err, res) => {
5050
expect(err).to.not.exist()
5151
expect(res).to.exist()
52-
expect(res).to.have.a.property('totalIn')
53-
expect(res).to.have.a.property('totalOut')
54-
expect(res).to.have.a.property('rateIn')
55-
expect(res).to.have.a.property('rateOut')
56-
done()
52+
53+
res.once('data', (data) => {
54+
expect(data).to.have.a.property('totalIn')
55+
expect(data).to.have.a.property('totalOut')
56+
expect(data).to.have.a.property('rateIn')
57+
expect(data).to.have.a.property('rateOut')
58+
done()
59+
})
5760
})
5861
})
5962

0 commit comments

Comments
 (0)