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

Commit 70714ca

Browse files
author
Alan Shaw
committed
fix: properly serialize CID instances
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>
1 parent 0959866 commit 70714ca

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

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
1716
return cid
1817
}

0 commit comments

Comments
 (0)