diff --git a/src/stats/bitswap.js b/src/stats/bitswap.js index 619bd0809..9c893fa1c 100644 --- a/src/stats/bitswap.js +++ b/src/stats/bitswap.js @@ -2,6 +2,20 @@ const promisify = require('promisify-es6') +const transform = function (res, callback) { + callback(null, { + provideBufLen: res.ProvideBufLen, + wantlist: res.Wantlist, + peers: res.Peers, + blocksReceived: res.BlocksReceived, + dataReceived: res.DataReceived, + blocksSent: res.BlocksSent, + dataSent: res.DataSent, + dupBlksReceived: res.DupBlksReceived, + dupDataReceived: res.DupDataReceived + }) +} + module.exports = (send) => { return promisify((opts, callback) => { if (typeof (opts) === 'function') { @@ -9,9 +23,9 @@ module.exports = (send) => { opts = {} } - send({ + send.andTransform({ path: 'stats/bitswap', qs: opts - }, callback) + }, transform, callback) }) } diff --git a/src/stats/bw.js b/src/stats/bw.js index a7ee05677..3c0d79c87 100644 --- a/src/stats/bw.js +++ b/src/stats/bw.js @@ -3,6 +3,21 @@ const promisify = require('promisify-es6') const streamToValue = require('../utils/stream-to-value') +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 + }) + }) +} + module.exports = (send) => { return promisify((opts, callback) => { if (typeof (opts) === 'function') { @@ -13,14 +28,6 @@ module.exports = (send) => { send.andTransform({ path: 'stats/bw', qs: opts - }, streamToValue, (err, stats) => { - if (err) { - return callback(err) - } - - // streamToValue returns an array and we're only - // interested in returning the object itself. - callback(err, stats[0]) - }) + }, transform, callback) }) } diff --git a/src/stats/repo.js b/src/stats/repo.js index 71a180363..55a8fdd68 100644 --- a/src/stats/repo.js +++ b/src/stats/repo.js @@ -3,6 +3,16 @@ const promisify = require('promisify-es6') +const transform = function (res, callback) { + callback(null, { + numObjects: res.NumObjects, + repoSize: res.RepoSize, + repoPath: res.RepoPath, + version: res.Version, + storageMax: res.StorageMax + }) +} + module.exports = (send) => { return promisify((opts, callback) => { if (typeof (opts) === 'function') { @@ -10,9 +20,9 @@ module.exports = (send) => { opts = {} } - send({ + send.andTransform({ path: 'stats/repo', qs: opts - }, callback) + }, transform, callback) }) } diff --git a/test/fixtures/test-folder/hello-link b/test/fixtures/test-folder/hello-link deleted file mode 120000 index 50b07e9e7..000000000 --- a/test/fixtures/test-folder/hello-link +++ /dev/null @@ -1 +0,0 @@ -files/hello.txt \ No newline at end of file diff --git a/test/fixtures/test-folder/hello-link b/test/fixtures/test-folder/hello-link new file mode 100644 index 000000000..50b07e9e7 --- /dev/null +++ b/test/fixtures/test-folder/hello-link @@ -0,0 +1 @@ +files/hello.txt \ No newline at end of file diff --git a/test/stats.spec.js b/test/stats.spec.js index 24d54731a..8775d88ed 100644 --- a/test/stats.spec.js +++ b/test/stats.spec.js @@ -31,15 +31,15 @@ describe('stats', function () { ipfs.stats.bitswap((err, res) => { expect(err).to.not.exist() expect(res).to.exist() - expect(res).to.have.a.property('ProvideBufLen') - expect(res).to.have.a.property('Wantlist') - expect(res).to.have.a.property('Peers') - expect(res).to.have.a.property('BlocksReceived') - expect(res).to.have.a.property('DataReceived') - expect(res).to.have.a.property('BlocksSent') - expect(res).to.have.a.property('DataSent') - expect(res).to.have.a.property('DupBlksReceived') - expect(res).to.have.a.property('DupDataReceived') + expect(res).to.have.a.property('provideBufLen') + expect(res).to.have.a.property('wantlist') + expect(res).to.have.a.property('peers') + expect(res).to.have.a.property('blocksReceived') + expect(res).to.have.a.property('dataReceived') + expect(res).to.have.a.property('blocksSent') + expect(res).to.have.a.property('dataSent') + expect(res).to.have.a.property('dupBlksReceived') + expect(res).to.have.a.property('dupDataReceived') done() }) }) @@ -48,10 +48,10 @@ 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') + 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() }) }) @@ -60,11 +60,11 @@ describe('stats', function () { ipfs.stats.repo((err, res) => { expect(err).to.not.exist() expect(res).to.exist() - expect(res).to.have.a.property('NumObjects') - expect(res).to.have.a.property('RepoSize') - expect(res).to.have.a.property('RepoPath') - expect(res).to.have.a.property('Version') - expect(res).to.have.a.property('StorageMax') + expect(res).to.have.a.property('numObjects') + expect(res).to.have.a.property('repoSize') + expect(res).to.have.a.property('repoPath') + expect(res).to.have.a.property('version') + expect(res).to.have.a.property('storageMax') done() }) }) @@ -75,10 +75,10 @@ describe('stats', function () { return ipfs.stats.bw() .then((res) => { 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') + 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') }) }) @@ -86,11 +86,11 @@ describe('stats', function () { return ipfs.stats.repo() .then((res) => { expect(res).to.exist() - expect(res).to.have.a.property('NumObjects') - expect(res).to.have.a.property('RepoSize') - expect(res).to.have.a.property('RepoPath') - expect(res).to.have.a.property('Version') - expect(res).to.have.a.property('StorageMax') + expect(res).to.have.a.property('numObjects') + expect(res).to.have.a.property('repoSize') + expect(res).to.have.a.property('repoPath') + expect(res).to.have.a.property('version') + expect(res).to.have.a.property('storageMax') }) }) @@ -98,15 +98,15 @@ describe('stats', function () { return ipfs.stats.bitswap() .then((res) => { expect(res).to.exist() - expect(res).to.have.a.property('ProvideBufLen') - expect(res).to.have.a.property('Wantlist') - expect(res).to.have.a.property('Peers') - expect(res).to.have.a.property('BlocksReceived') - expect(res).to.have.a.property('DataReceived') - expect(res).to.have.a.property('BlocksSent') - expect(res).to.have.a.property('DataSent') - expect(res).to.have.a.property('DupBlksReceived') - expect(res).to.have.a.property('DupDataReceived') + expect(res).to.have.a.property('provideBufLen') + expect(res).to.have.a.property('wantlist') + expect(res).to.have.a.property('peers') + expect(res).to.have.a.property('blocksReceived') + expect(res).to.have.a.property('dataReceived') + expect(res).to.have.a.property('blocksSent') + expect(res).to.have.a.property('dataSent') + expect(res).to.have.a.property('dupBlksReceived') + expect(res).to.have.a.property('dupDataReceived') }) }) })