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

Use CID for api.get/api.cat #555

Merged
merged 1 commit into from
May 14, 2017
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
4 changes: 2 additions & 2 deletions src/api/cat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const promisify = require('promisify-es6')
const cleanMultihash = require('../clean-multihash')
const cleanCID = require('../clean-cid')

module.exports = (send) => {
return promisify((hash, opts, callback) => {
Expand All @@ -11,7 +11,7 @@ module.exports = (send) => {
}

try {
hash = cleanMultihash(hash)
hash = cleanCID(hash)
} catch (err) {
return callback(err)
}
Expand Down
4 changes: 2 additions & 2 deletions src/api/get.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const promisify = require('promisify-es6')
const cleanMultihash = require('../clean-multihash')
const cleanCID = require('../clean-cid')
const TarStreamToObjects = require('../tar-stream-to-objects')

module.exports = (send) => {
Expand All @@ -21,7 +21,7 @@ module.exports = (send) => {
}

try {
path = cleanMultihash(path)
path = cleanCID(path)
} catch (err) {
return callback(err)
}
Expand Down
15 changes: 15 additions & 0 deletions src/clean-cid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

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

module.exports = function (cid) {
if (Buffer.isBuffer(cid)) {
cid = bs58.encode(cid)
}
if (typeof cid !== 'string') {
throw new Error('unexpected cid type: ' + typeof cid)
}
CID.validateCID(new CID(cid.split('/')[0]))
return cid
}