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

Commit 3a56d56

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

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

src/stats/bw.js

+31-14
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,15 @@
22

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

6-
const transform = function (res, callback) {
7-
streamToValue(res, (err, data) => {
8-
if (err) {
9-
return callback(err)
10-
}
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-
})
18-
})
7+
const transformChunk = (chunk) => {
8+
return {
9+
totalIn: chunk.TotalIn,
10+
totalOut: chunk.TotalOut,
11+
rateIn: chunk.RateIn,
12+
rateOut: chunk.RateOut
13+
}
1914
}
2015

2116
module.exports = (send) => {
@@ -28,6 +23,28 @@ module.exports = (send) => {
2823
send.andTransform({
2924
path: 'stats/bw',
3025
qs: opts
31-
}, transform, callback)
26+
}, (res, callback) => {
27+
if (!opts.poll) {
28+
// If not polling, just send the result.
29+
return streamToValue(res, (err, data) => {
30+
if (err) {
31+
return callback(err)
32+
}
33+
34+
callback(null, transformChunk(data[0]))
35+
})
36+
}
37+
38+
// If polling, return a readable stream.
39+
const output = new Transform({
40+
objectMode: true,
41+
transform(chunk, encoding, cb) {
42+
cb(null, transformChunk(chunk))
43+
}
44+
})
45+
46+
res.pipe(output)
47+
callback(null, output)
48+
}, callback)
3249
})
3350
}

0 commit comments

Comments
 (0)