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

Commit

Permalink
feat(breaking change): use stream on stats.bw
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Jan 31, 2018
1 parent 4f7999d commit 3a56d56
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions src/stats/bw.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@

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

const transform = function (res, callback) {
streamToValue(res, (err, data) => {
if (err) {
return callback(err)
}

callback(null, {
totalIn: data[0].TotalIn,
totalOut: data[0].TotalOut,
rateIn: data[0].RateIn,
rateOut: data[0].RateOut
})
})
const transformChunk = (chunk) => {
return {
totalIn: chunk.TotalIn,
totalOut: chunk.TotalOut,
rateIn: chunk.RateIn,
rateOut: chunk.RateOut
}
}

module.exports = (send) => {
Expand All @@ -28,6 +23,28 @@ module.exports = (send) => {
send.andTransform({
path: 'stats/bw',
qs: opts
}, transform, callback)
}, (res, callback) => {
if (!opts.poll) {
// If not polling, just send the result.
return streamToValue(res, (err, data) => {
if (err) {
return callback(err)
}

callback(null, transformChunk(data[0]))
})
}

// If polling, return a readable stream.
const output = new Transform({
objectMode: true,
transform(chunk, encoding, cb) {
cb(null, transformChunk(chunk))
}
})

res.pipe(output)
callback(null, output)
}, callback)
})
}

0 comments on commit 3a56d56

Please sign in to comment.