Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit da7d0f4

Browse files
magik6kdaviddias
authored andcommitted
feat: Use CID for api.get/api.cat (#555)
1 parent 6b83215 commit da7d0f4

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/api/cat.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

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

66
module.exports = (send) => {
77
return promisify((hash, opts, callback) => {
@@ -11,7 +11,7 @@ module.exports = (send) => {
1111
}
1212

1313
try {
14-
hash = cleanMultihash(hash)
14+
hash = cleanCID(hash)
1515
} catch (err) {
1616
return callback(err)
1717
}

src/api/get.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

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

77
module.exports = (send) => {
@@ -21,7 +21,7 @@ module.exports = (send) => {
2121
}
2222

2323
try {
24-
path = cleanMultihash(path)
24+
path = cleanCID(path)
2525
} catch (err) {
2626
return callback(err)
2727
}

src/clean-cid.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
const bs58 = require('bs58')
4+
const CID = require('cids')
5+
6+
module.exports = function (cid) {
7+
if (Buffer.isBuffer(cid)) {
8+
cid = bs58.encode(cid)
9+
}
10+
if (typeof cid !== 'string') {
11+
throw new Error('unexpected cid type: ' + typeof cid)
12+
}
13+
CID.validateCID(new CID(cid.split('/')[0]))
14+
return cid
15+
}

0 commit comments

Comments
 (0)