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

Commit

Permalink
feat: Use CID for api.get/api.cat (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k authored and daviddias committed May 14, 2017
1 parent 6b83215 commit da7d0f4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
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
}

0 comments on commit da7d0f4

Please sign in to comment.