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

Commit

Permalink
fix: properly serialize CID instances
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>
  • Loading branch information
Alan Shaw committed Dec 12, 2018
1 parent 72955db commit 45b344c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
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
return cid
}

0 comments on commit 45b344c

Please sign in to comment.