Skip to content

Commit

Permalink
fix: properly serialize CID instances (ipfs#906)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Alan Shaw authored Dec 13, 2018
2 parents 72955db + 0e15761 commit 0712766
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
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

0 comments on commit 0712766

Please sign in to comment.