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

Commit d331b35

Browse files
authored
fix: returns cid of flushed path (#72)
Fixes #50
1 parent 68bd372 commit d331b35

File tree

7 files changed

+84
-19
lines changed

7 files changed

+84
-19
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
"scripts": {
1212
"test": "aegir test",
1313
"test:node": "aegir test -t node",
14-
"test:cli": "aegir test -t node -f test/cli/**/*.js",
15-
"test:core": "aegir test -t node -f test/core/**/*.js",
16-
"test:http": "aegir test -t node -f test/http/**/*.js",
14+
"test:cli": "aegir test -t node -f test/cli/index.js",
15+
"test:core": "aegir test -t node -f test/core/index.js",
16+
"test:http": "aegir test -t node -f test/http/index.js",
1717
"test:browser": "aegir test -t browser",
1818
"test:webworker": "aegir test -t webworker",
1919
"build": "aegir build",

src/cli/flush.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,32 @@ module.exports = {
99

1010
describe: ' Flush a given path\'s data to disk',
1111

12-
builder: {},
12+
builder: {
13+
'cid-base': {
14+
default: 'base58btc',
15+
describe: 'CID base to use.'
16+
}
17+
},
1318

1419
handler (argv) {
1520
const {
1621
path,
17-
getIpfs
22+
getIpfs,
23+
cidBase,
24+
print
1825
} = argv
1926

2027
argv.resolve((async () => {
2128
const ipfs = await getIpfs()
22-
return ipfs.files.flush(path || FILE_SEPARATOR, {})
29+
let cid = await ipfs.files.flush(path || FILE_SEPARATOR, {})
30+
31+
if (cidBase !== 'base58btc' && cid.version === 0) {
32+
cid = cid.toV1()
33+
}
34+
35+
print(JSON.stringify({
36+
Cid: cid.toString(cidBase)
37+
}))
2338
})())
2439
}
2540
}

src/core/flush.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module.exports = (context) => {
1212
return async function mfsFlush (path = FILE_SEPARATOR, options = defaultOptions) {
1313
options = applyDefaultOptions(options, defaultOptions)
1414

15-
await stat(context)(path, options)
15+
const result = await stat(context)(path, options)
16+
17+
return result.cid
1618
}
1719
}

src/http/flush.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@ const mfsFlush = {
1414
ipfs
1515
} = request.server.app
1616
const {
17-
arg
17+
arg,
18+
cidBase
1819
} = request.query
1920

20-
await ipfs.files.flush(arg || FILE_SEPARATOR, {})
21+
let cid = await ipfs.files.flush(arg || FILE_SEPARATOR, {})
2122

22-
return h.response()
23+
if (cidBase !== 'base58btc' && cid.version === 0) {
24+
cid = cid.toV1()
25+
}
26+
27+
return h.response({
28+
Cid: cid.toString(cidBase)
29+
})
2330
},
2431
options: {
2532
validate: {
@@ -28,7 +35,8 @@ const mfsFlush = {
2835
stripUnknown: true
2936
},
3037
query: Joi.object().keys({
31-
arg: Joi.string()
38+
arg: Joi.string(),
39+
cidBase: Joi.string().default('base58btc')
3240
})
3341
}
3442
}

test/cli/flush.js

+23-3
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,56 @@
44
const expect = require('../helpers/chai')
55
const cli = require('../helpers/cli')
66
const sinon = require('sinon')
7+
const CID = require('cids')
8+
const cid = new CID('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
79

810
describe('flush', () => {
911
const path = '/foo'
1012
let ipfs
13+
let print
14+
let output
1115

1216
beforeEach(() => {
1317
ipfs = {
1418
files: {
15-
flush: sinon.stub()
19+
flush: sinon.stub().resolves(cid)
1620
}
1721
}
22+
print = (msg = '', newline = true) => {
23+
output += newline ? msg + '\n' : msg
24+
}
1825
})
1926

2027
it('should flush a path', async () => {
21-
await cli(`files flush ${path}`, { ipfs })
28+
await cli(`files flush ${path}`, { ipfs, print })
2229

2330
expect(ipfs.files.flush.callCount).to.equal(1)
2431
expect(ipfs.files.flush.getCall(0).args).to.deep.equal([
2532
path,
2633
{}
2734
])
35+
expect(output).to.include(cid.toString())
2836
})
2937

3038
it('should flush without a path', async () => {
31-
await cli('files flush', { ipfs })
39+
await cli('files flush', { ipfs, print })
40+
41+
expect(ipfs.files.flush.callCount).to.equal(1)
42+
expect(ipfs.files.flush.getCall(0).args).to.deep.equal([
43+
'/',
44+
{}
45+
])
46+
expect(output).to.include(cid.toString())
47+
})
48+
49+
it('should flush with a different CID base', async () => {
50+
await cli('files flush --cid-base base64', { ipfs, print })
3251

3352
expect(ipfs.files.flush.callCount).to.equal(1)
3453
expect(ipfs.files.flush.getCall(0).args).to.deep.equal([
3554
'/',
3655
{}
3756
])
57+
expect(output).to.include(cid.toV1().toString('base64'))
3858
})
3959
})

test/core/flush.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ describe('flush', () => {
1414
})
1515

1616
it('flushes the root node', async () => {
17-
await mfs.flush()
17+
const cid = await mfs.flush()
18+
19+
expect(cid.toString()).to.equal((await mfs.stat('/')).cid.toString())
1820
})
1921

2022
it('throws a error when trying to flush non-existent directories', async () => {

test/http/flush.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44
const expect = require('../helpers/chai')
55
const http = require('../helpers/http')
66
const sinon = require('sinon')
7+
const CID = require('cids')
8+
const cid = new CID('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
79

8-
describe('flush', () => () => {
10+
describe('flush', () => {
911
const path = '/foo'
1012
let ipfs
1113

1214
beforeEach(() => {
1315
ipfs = {
1416
files: {
15-
flush: sinon.stub()
17+
flush: sinon.stub().resolves(cid)
1618
}
1719
}
1820
})
1921

2022
it('should flush a path', async () => {
21-
await http({
23+
const response = await http({
2224
method: 'POST',
2325
url: `/api/v0/files/flush?arg=${path}`
2426
}, { ipfs })
@@ -28,10 +30,11 @@ describe('flush', () => () => {
2830
path,
2931
{}
3032
])
33+
expect(response).to.have.nested.property('result.Cid', cid.toString())
3134
})
3235

3336
it('should flush without a path', async () => {
34-
await http({
37+
const response = await http({
3538
method: 'POST',
3639
url: '/api/v0/files/flush'
3740
}, { ipfs })
@@ -41,5 +44,20 @@ describe('flush', () => () => {
4144
'/',
4245
{}
4346
])
47+
expect(response).to.have.nested.property('result.Cid', cid.toString())
48+
})
49+
50+
it('should flush with a different CID base', async () => {
51+
const response = await http({
52+
method: 'POST',
53+
url: '/api/v0/files/flush?cidBase=base64'
54+
}, { ipfs })
55+
56+
expect(ipfs.files.flush.callCount).to.equal(1)
57+
expect(ipfs.files.flush.getCall(0).args).to.deep.equal([
58+
'/',
59+
{}
60+
])
61+
expect(response).to.have.nested.property('result.Cid', cid.toV1().toString('base64'))
4462
})
4563
})

0 commit comments

Comments
 (0)