diff --git a/package.json b/package.json index 3ae08c53..73e17e5f 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "dependencies": { "base32.js": "~0.1.0", "bignumber.js": "^9.0.0", + "bytes": "^3.1.0", "cids": "~0.7.0", "datastore-core": "~0.7.0", "datastore-fs": "~0.9.0", @@ -70,6 +71,7 @@ "just-safe-set": "^2.1.0", "lodash.has": "^4.5.2", "p-queue": "^6.0.0", + "pretty-bytes": "^5.3.0", "proper-lockfile": "^4.0.0", "sort-keys": "^3.0.0" }, diff --git a/src/index.js b/src/index.js index 83e0c9dd..a05b4d4f 100644 --- a/src/index.js +++ b/src/index.js @@ -7,6 +7,8 @@ const debug = require('debug') const Big = require('bignumber.js') const errcode = require('err-code') const migrator = require('ipfs-repo-migrations') +const prettyBytes = require('pretty-bytes') +const bytes = require('bytes') const constants = require('./constants') const backends = require('./backends') @@ -258,19 +260,22 @@ class IpfsRepo { getSize(this.datastore), getSize(this.keys) ]) - let size = blocks.size + const size = blocks.size .plus(datastore) .plus(keys) - if (options.human) { - size = size.div(1048576) - } return { repoPath: this.path, - storageMax: storageMax, + storageMax: options.human + ? prettyBytes(storageMax.toNumber()).toUpperCase() + : storageMax, version: version, - numObjects: blocks.count, - repoSize: size + numObjects: options.human + ? blocks.count.toNumber() + : blocks.count, + repoSize: options.human + ? prettyBytes(size.toNumber()).toUpperCase() + : size } } @@ -308,7 +313,7 @@ class IpfsRepo { async _storageMaxStat () { try { const max = await this.config.get('Datastore.StorageMax') - return new Big(max) + return new Big(bytes(max)) } catch (err) { return new Big(noLimit) } diff --git a/test/repo-test.js b/test/repo-test.js index d4f62e21..8765c47f 100644 --- a/test/repo-test.js +++ b/test/repo-test.js @@ -8,6 +8,7 @@ const path = require('path') const IPFSRepo = require('../') const Errors = require('../src/errors') const os = require('os') +const bytes = require('bytes') module.exports = (repo) => { describe('IPFS Repo Tests', () => { @@ -247,14 +248,17 @@ module.exports = (repo) => { }) it('should return the max storage stat when set', async () => { + const maxStorage = '1GB' + otherRepo = new IPFSRepo(path.join(os.tmpdir(), 'repo-' + Date.now())) await otherRepo.init({}) await otherRepo.open() - await otherRepo.config.set('Datastore.StorageMax', 100) + await otherRepo.config.set('Datastore.StorageMax', maxStorage) const stat = await otherRepo.stat({}) - expect(stat.storageMax.toNumber()).to.equal(100) + expect(stat).to.have.property('storageMax') + expect(stat.storageMax.toNumber()).to.equal(bytes(maxStorage)) }) it('should throw unexpected errors when closing', async () => { diff --git a/test/stat-test.js b/test/stat-test.js index aceb8938..b88db655 100644 --- a/test/stat-test.js +++ b/test/stat-test.js @@ -6,6 +6,7 @@ chai.use(require('dirty-chai')) const expect = chai.expect const Block = require('ipfs-block') const CID = require('cids') +const prettyBytes = require('pretty-bytes') module.exports = (repo) => { describe('stat', () => { @@ -33,18 +34,23 @@ module.exports = (repo) => { }) it('get human stats', async () => { - const stats = await repo.stat({ human: true }) - expect(stats).to.exist() - expect(stats).to.have.property('numObjects') - expect(stats).to.have.property('version') - expect(stats).to.have.property('repoPath') - expect(stats).to.have.property('repoSize') - expect(stats).to.have.property('storageMax') + const { repoSize, storageMax } = await repo.stat() - expect(stats.numObjects > '0').to.eql(true) - expect(stats.version > '0').to.eql(true) - expect(stats.repoSize > '0').to.eql(true) - expect(stats.storageMax > '0').to.eql(true) + const humanizedRepoSize = prettyBytes(repoSize.toNumber()).toUpperCase() + const humanizedStorageMax = prettyBytes(storageMax.toNumber()).toUpperCase() + + const humanizedStats = await repo.stat({ human: true }) + + expect(humanizedStats).to.exist() + expect(humanizedStats).to.have.property('numObjects') + expect(humanizedStats).to.have.property('version').and.be.above(0) + expect(humanizedStats).to.have.property('repoPath') + expect(humanizedStats).to.have.property('repoSize').that.equals(humanizedRepoSize) + expect(humanizedStats).to.have.property('storageMax').that.equals(humanizedStorageMax) + + expect(humanizedStats.numObjects > '0').to.eql(true) + expect(humanizedStats.repoSize > '0').to.eql(true) + expect(humanizedStats.storageMax > '0').to.eql(true) }) }) }