Skip to content

Commit 0712766

Browse files
author
Alan Shaw
authored
fix: properly serialize CID instances (ipfs#906)
The CID version agnostic tests ipfs-inactive/interface-js-ipfs-core#413 identified some functions were not properly serializing CID instances. This PR adds `cleanCID` step to several functions and also updates the `cleanCID` function to not assume buffer CIDs are CIDv0 multihashes. depends on ipfs-inactive/interface-js-ipfs-core#413 License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai> * fix: disable just the rule we're breaking License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai> * chore: update dependencies License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai> * fix: skip test that go-ipfs cannot pass License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
2 parents 72955db + 0e15761 commit 0712766

File tree

6 files changed

+32
-6
lines changed

6 files changed

+32
-6
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
"eslint-plugin-react": "^7.11.1",
8787
"go-ipfs-dep": "~0.4.18",
8888
"gulp": "^3.9.1",
89-
"interface-ipfs-core": "~0.91.0",
89+
"interface-ipfs-core": "~0.92.0",
9090
"ipfsd-ctl": "~0.40.0",
9191
"nock": "^10.0.2",
9292
"pull-stream": "^3.6.9",

src/files-regular/ls-pull-stream.js

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const moduleConfig = require('../utils/module-config')
44
const pull = require('pull-stream')
55
const deferred = require('pull-defer')
6+
const cleanCID = require('../utils/clean-cid')
67

78
module.exports = (arg) => {
89
const send = moduleConfig(arg)
@@ -13,6 +14,12 @@ module.exports = (arg) => {
1314
opts = {}
1415
}
1516

17+
try {
18+
args = cleanCID(args)
19+
} catch (err) {
20+
return callback(err)
21+
}
22+
1623
const p = deferred.source()
1724

1825
send({ path: 'ls', args: args, qs: opts }, (err, results) => {

src/files-regular/ls-readable-stream.js

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const moduleConfig = require('../utils/module-config')
44
const Stream = require('readable-stream')
5+
const cleanCID = require('../utils/clean-cid')
56

67
module.exports = (arg) => {
78
const send = moduleConfig(arg)
@@ -12,6 +13,12 @@ module.exports = (arg) => {
1213
opts = {}
1314
}
1415

16+
try {
17+
args = cleanCID(args)
18+
} catch (err) {
19+
return callback(err)
20+
}
21+
1522
const pt = new Stream.PassThrough({ objectMode: true })
1623

1724
send({ path: 'ls', args: args, qs: opts }, (err, results) => {

src/files-regular/ls.js

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const promisify = require('promisify-es6')
44
const moduleConfig = require('../utils/module-config')
5+
const cleanCID = require('../utils/clean-cid')
56

67
module.exports = (arg) => {
78
const send = moduleConfig(arg)
@@ -11,6 +12,13 @@ module.exports = (arg) => {
1112
callback = opts
1213
opts = {}
1314
}
15+
16+
try {
17+
args = cleanCID(args)
18+
} catch (err) {
19+
return callback(err)
20+
}
21+
1422
send({
1523
path: 'ls',
1624
args: args,

src/utils/clean-cid.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
'use strict'
22

3-
const bs58 = require('bs58')
43
const CID = require('cids')
54

65
module.exports = function (cid) {
76
if (Buffer.isBuffer(cid)) {
8-
cid = bs58.encode(cid)
7+
return new CID(cid).toString()
98
}
109
if (CID.isCID(cid)) {
11-
cid = cid.toBaseEncodedString()
10+
return cid.toString()
1211
}
1312
if (typeof cid !== 'string') {
1413
throw new Error('unexpected cid type: ' + typeof cid)
1514
}
16-
CID.validateCID(new CID(cid.split('/')[0]))
15+
new CID(cid.split('/')[0]) // eslint-disable-line no-new
1716
return cid
1817
}

test/interface.spec.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ describe('interface-ipfs-core tests', () => {
3434
]
3535
})
3636

37-
tests.block(defaultCommonFactory)
37+
tests.block(defaultCommonFactory, {
38+
skip: [{
39+
name: 'should get a block added as CIDv1 with a CIDv0',
40+
reason: 'go-ipfs does not support the `version` param'
41+
}]
42+
})
3843

3944
tests.bootstrap(defaultCommonFactory)
4045

0 commit comments

Comments
 (0)