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 30, 2018
1 parent 4f7999d commit 1561a69
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
24 changes: 13 additions & 11 deletions src/stats/bw.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
'use strict'

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)
const output = new Transform({
objectMode: true,
transform(chunk, encoding, cb) {
cb(null, {
totalIn: chunk.TotalIn,
totalOut: chunk.TotalOut,
rateIn: chunk.RateIn,
rateOut: chunk.RateOut
})
}

callback(null, {
totalIn: data[0].TotalIn,
totalOut: data[0].TotalOut,
rateIn: data[0].RateIn,
rateOut: data[0].RateOut
})
})

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

module.exports = (send) => {
Expand Down
13 changes: 8 additions & 5 deletions test/stats.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@ describe('stats', function () {
ipfs.stats.bw((err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()
expect(res).to.have.a.property('totalIn')
expect(res).to.have.a.property('totalOut')
expect(res).to.have.a.property('rateIn')
expect(res).to.have.a.property('rateOut')
done()

res.once('data', (data) => {
expect(data).to.have.a.property('totalIn')
expect(data).to.have.a.property('totalOut')
expect(data).to.have.a.property('rateIn')
expect(data).to.have.a.property('rateOut')
done()
})
})
})

Expand Down

0 comments on commit 1561a69

Please sign in to comment.