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

Commit ccabe76

Browse files
nginneverdaviddias
authored andcommitted
upgrade cat
1 parent 8b9ee53 commit ccabe76

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"flatmap": "0.0.3",
1414
"glob": "^7.0.3",
1515
"ipfs-merkle-dag": "^0.6.0",
16+
"is-ipfs": "^0.2.0",
1617
"isstream": "^0.1.2",
1718
"multiaddr": "^2.0.0",
1819
"multipart-stream": "^2.0.1",
@@ -93,4 +94,4 @@
9394
"url": "https://github.com/ipfs/js-ipfs-api/issues"
9495
},
9596
"homepage": "https://github.com/ipfs/js-ipfs-api"
96-
}
97+
}

src/api/cat.js

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

3-
const argCommand = require('../cmd-helpers').argCommand
3+
const bs58 = require('bs58')
4+
const isIPFS = require('is-ipfs')
5+
const promisify = require('promisify-es6')
46

57
module.exports = (send) => {
6-
return argCommand(send, 'cat')
8+
const cat = promisify((multihash, callback) => {
9+
try {
10+
multihash = cleanMultihash(multihash)
11+
} catch (err) {
12+
return callback(err)
13+
}
14+
send('cat', multihash, null, null, function (err, result) {
15+
if (err) {
16+
return callback(err)
17+
}
18+
return callback(null, result)
19+
})
20+
})
21+
return cat
22+
}
23+
24+
function cleanMultihash (multihash) {
25+
if (!isIPFS.multihash(multihash)) {
26+
throw new Error('not valid multihash')
27+
}
28+
if (Buffer.isBuffer(multihash)) {
29+
return bs58.encode(multihash)
30+
}
31+
return multihash
732
}

0 commit comments

Comments
 (0)