diff --git a/package.json b/package.json index 808f5c5..1afded5 100644 --- a/package.json +++ b/package.json @@ -11,9 +11,9 @@ "scripts": { "test": "aegir test", "test:node": "aegir test -t node", - "test:cli": "aegir test -t node -f test/cli/**/*.js", - "test:core": "aegir test -t node -f test/core/**/*.js", - "test:http": "aegir test -t node -f test/http/**/*.js", + "test:cli": "aegir test -t node -f test/cli/index.js", + "test:core": "aegir test -t node -f test/core/index.js", + "test:http": "aegir test -t node -f test/http/index.js", "test:browser": "aegir test -t browser", "test:webworker": "aegir test -t webworker", "build": "aegir build", diff --git a/src/cli/flush.js b/src/cli/flush.js index aab5dfc..9fa587d 100644 --- a/src/cli/flush.js +++ b/src/cli/flush.js @@ -9,17 +9,32 @@ module.exports = { describe: ' Flush a given path\'s data to disk', - builder: {}, + builder: { + 'cid-base': { + default: 'base58btc', + describe: 'CID base to use.' + } + }, handler (argv) { const { path, - getIpfs + getIpfs, + cidBase, + print } = argv argv.resolve((async () => { const ipfs = await getIpfs() - return ipfs.files.flush(path || FILE_SEPARATOR, {}) + let cid = await ipfs.files.flush(path || FILE_SEPARATOR, {}) + + if (cidBase !== 'base58btc' && cid.version === 0) { + cid = cid.toV1() + } + + print(JSON.stringify({ + Cid: cid.toString(cidBase) + })) })()) } } diff --git a/src/core/flush.js b/src/core/flush.js index ecfc333..a672f78 100644 --- a/src/core/flush.js +++ b/src/core/flush.js @@ -12,6 +12,8 @@ module.exports = (context) => { return async function mfsFlush (path = FILE_SEPARATOR, options = defaultOptions) { options = applyDefaultOptions(options, defaultOptions) - await stat(context)(path, options) + const result = await stat(context)(path, options) + + return result.cid } } diff --git a/src/http/flush.js b/src/http/flush.js index 47db6fe..65dc11b 100644 --- a/src/http/flush.js +++ b/src/http/flush.js @@ -14,12 +14,19 @@ const mfsFlush = { ipfs } = request.server.app const { - arg + arg, + cidBase } = request.query - await ipfs.files.flush(arg || FILE_SEPARATOR, {}) + let cid = await ipfs.files.flush(arg || FILE_SEPARATOR, {}) - return h.response() + if (cidBase !== 'base58btc' && cid.version === 0) { + cid = cid.toV1() + } + + return h.response({ + Cid: cid.toString(cidBase) + }) }, options: { validate: { @@ -28,7 +35,8 @@ const mfsFlush = { stripUnknown: true }, query: Joi.object().keys({ - arg: Joi.string() + arg: Joi.string(), + cidBase: Joi.string().default('base58btc') }) } } diff --git a/test/cli/flush.js b/test/cli/flush.js index f40a0d7..83c4095 100644 --- a/test/cli/flush.js +++ b/test/cli/flush.js @@ -4,36 +4,56 @@ const expect = require('../helpers/chai') const cli = require('../helpers/cli') const sinon = require('sinon') +const CID = require('cids') +const cid = new CID('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn') describe('flush', () => { const path = '/foo' let ipfs + let print + let output beforeEach(() => { ipfs = { files: { - flush: sinon.stub() + flush: sinon.stub().resolves(cid) } } + print = (msg = '', newline = true) => { + output += newline ? msg + '\n' : msg + } }) it('should flush a path', async () => { - await cli(`files flush ${path}`, { ipfs }) + await cli(`files flush ${path}`, { ipfs, print }) expect(ipfs.files.flush.callCount).to.equal(1) expect(ipfs.files.flush.getCall(0).args).to.deep.equal([ path, {} ]) + expect(output).to.include(cid.toString()) }) it('should flush without a path', async () => { - await cli('files flush', { ipfs }) + await cli('files flush', { ipfs, print }) + + expect(ipfs.files.flush.callCount).to.equal(1) + expect(ipfs.files.flush.getCall(0).args).to.deep.equal([ + '/', + {} + ]) + expect(output).to.include(cid.toString()) + }) + + it('should flush with a different CID base', async () => { + await cli('files flush --cid-base base64', { ipfs, print }) expect(ipfs.files.flush.callCount).to.equal(1) expect(ipfs.files.flush.getCall(0).args).to.deep.equal([ '/', {} ]) + expect(output).to.include(cid.toV1().toString('base64')) }) }) diff --git a/test/core/flush.js b/test/core/flush.js index 494711d..4c3ae5d 100644 --- a/test/core/flush.js +++ b/test/core/flush.js @@ -14,7 +14,9 @@ describe('flush', () => { }) it('flushes the root node', async () => { - await mfs.flush() + const cid = await mfs.flush() + + expect(cid.toString()).to.equal((await mfs.stat('/')).cid.toString()) }) it('throws a error when trying to flush non-existent directories', async () => { diff --git a/test/http/flush.js b/test/http/flush.js index cfd28f4..264677e 100644 --- a/test/http/flush.js +++ b/test/http/flush.js @@ -4,21 +4,23 @@ const expect = require('../helpers/chai') const http = require('../helpers/http') const sinon = require('sinon') +const CID = require('cids') +const cid = new CID('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn') -describe('flush', () => () => { +describe('flush', () => { const path = '/foo' let ipfs beforeEach(() => { ipfs = { files: { - flush: sinon.stub() + flush: sinon.stub().resolves(cid) } } }) it('should flush a path', async () => { - await http({ + const response = await http({ method: 'POST', url: `/api/v0/files/flush?arg=${path}` }, { ipfs }) @@ -28,10 +30,11 @@ describe('flush', () => () => { path, {} ]) + expect(response).to.have.nested.property('result.Cid', cid.toString()) }) it('should flush without a path', async () => { - await http({ + const response = await http({ method: 'POST', url: '/api/v0/files/flush' }, { ipfs }) @@ -41,5 +44,20 @@ describe('flush', () => () => { '/', {} ]) + expect(response).to.have.nested.property('result.Cid', cid.toString()) + }) + + it('should flush with a different CID base', async () => { + const response = await http({ + method: 'POST', + url: '/api/v0/files/flush?cidBase=base64' + }, { ipfs }) + + expect(ipfs.files.flush.callCount).to.equal(1) + expect(ipfs.files.flush.getCall(0).args).to.deep.equal([ + '/', + {} + ]) + expect(response).to.have.nested.property('result.Cid', cid.toV1().toString('base64')) }) })