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

Commit

Permalink
fix: returns cid of flushed path (#72)
Browse files Browse the repository at this point in the history
Fixes #50
  • Loading branch information
achingbrain committed Jan 10, 2020
1 parent 68bd372 commit d331b35
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 19 deletions.
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -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",
Expand Down
21 changes: 18 additions & 3 deletions src/cli/flush.js
Expand Up @@ -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)
}))
})())
}
}
4 changes: 3 additions & 1 deletion src/core/flush.js
Expand Up @@ -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
}
}
16 changes: 12 additions & 4 deletions src/http/flush.js
Expand Up @@ -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: {
Expand All @@ -28,7 +35,8 @@ const mfsFlush = {
stripUnknown: true
},
query: Joi.object().keys({
arg: Joi.string()
arg: Joi.string(),
cidBase: Joi.string().default('base58btc')
})
}
}
Expand Down
26 changes: 23 additions & 3 deletions test/cli/flush.js
Expand Up @@ -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'))
})
})
4 changes: 3 additions & 1 deletion test/core/flush.js
Expand Up @@ -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 () => {
Expand Down
26 changes: 22 additions & 4 deletions test/http/flush.js
Expand Up @@ -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 })
Expand All @@ -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 })
Expand All @@ -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'))
})
})

0 comments on commit d331b35

Please sign in to comment.