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

fix: properly serialize CID instances #906

Merged
merged 4 commits into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"eslint-plugin-react": "^7.11.1",
"go-ipfs-dep": "~0.4.18",
"gulp": "^3.9.1",
"interface-ipfs-core": "~0.91.0",
"interface-ipfs-core": "~0.92.0",
"ipfsd-ctl": "~0.40.0",
"nock": "^10.0.2",
"pull-stream": "^3.6.9",
Expand Down
7 changes: 7 additions & 0 deletions src/files-regular/ls-pull-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const moduleConfig = require('../utils/module-config')
const pull = require('pull-stream')
const deferred = require('pull-defer')
const cleanCID = require('../utils/clean-cid')

module.exports = (arg) => {
const send = moduleConfig(arg)
Expand All @@ -13,6 +14,12 @@ module.exports = (arg) => {
opts = {}
}

try {
args = cleanCID(args)
} catch (err) {
return callback(err)
}

const p = deferred.source()

send({ path: 'ls', args: args, qs: opts }, (err, results) => {
Expand Down
7 changes: 7 additions & 0 deletions src/files-regular/ls-readable-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const moduleConfig = require('../utils/module-config')
const Stream = require('readable-stream')
const cleanCID = require('../utils/clean-cid')

module.exports = (arg) => {
const send = moduleConfig(arg)
Expand All @@ -12,6 +13,12 @@ module.exports = (arg) => {
opts = {}
}

try {
args = cleanCID(args)
} catch (err) {
return callback(err)
}

const pt = new Stream.PassThrough({ objectMode: true })

send({ path: 'ls', args: args, qs: opts }, (err, results) => {
Expand Down
8 changes: 8 additions & 0 deletions src/files-regular/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const promisify = require('promisify-es6')
const moduleConfig = require('../utils/module-config')
const cleanCID = require('../utils/clean-cid')

module.exports = (arg) => {
const send = moduleConfig(arg)
Expand All @@ -11,6 +12,13 @@ module.exports = (arg) => {
callback = opts
opts = {}
}

try {
args = cleanCID(args)
} catch (err) {
return callback(err)
}

send({
path: 'ls',
args: args,
Expand Down
7 changes: 3 additions & 4 deletions src/utils/clean-cid.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
'use strict'

const bs58 = require('bs58')
const CID = require('cids')

module.exports = function (cid) {
if (Buffer.isBuffer(cid)) {
cid = bs58.encode(cid)
return new CID(cid).toString()
}
if (CID.isCID(cid)) {
cid = cid.toBaseEncodedString()
return cid.toString()
}
if (typeof cid !== 'string') {
throw new Error('unexpected cid type: ' + typeof cid)
}
CID.validateCID(new CID(cid.split('/')[0]))
new CID(cid.split('/')[0]) // eslint-disable-line no-new
return cid
}
7 changes: 6 additions & 1 deletion test/interface.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ describe('interface-ipfs-core tests', () => {
]
})

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

tests.bootstrap(defaultCommonFactory)

Expand Down