From f2aab2b949fa66293d6b2f14dd2984fefd7fae81 Mon Sep 17 00:00:00 2001 From: nginnever Date: Sat, 21 May 2016 23:18:46 -0700 Subject: [PATCH] cat interface-core upgrades --- package.json | 3 ++- src/api/cat.js | 29 +++++++++++++++++++++++++++-- test/api/file.spec.js | 17 +++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 test/api/file.spec.js diff --git a/package.json b/package.json index ac57b0fe4..2372bf0c7 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "flatmap": "0.0.3", "glob": "^7.0.3", "ipfs-merkle-dag": "^0.6.0", + "is-ipfs": "^0.2.0", "multiaddr": "^2.0.0", "multipart-stream": "^2.0.1", "ndjson": "^1.4.3", @@ -90,4 +91,4 @@ "url": "https://github.com/ipfs/js-ipfs-api/issues" }, "homepage": "https://github.com/ipfs/js-ipfs-api" -} \ No newline at end of file +} diff --git a/src/api/cat.js b/src/api/cat.js index 6a9639688..e5a4ff5c7 100644 --- a/src/api/cat.js +++ b/src/api/cat.js @@ -1,7 +1,32 @@ 'use strict' -const argCommand = require('../cmd-helpers').argCommand +const bs58 = require('bs58') +const isIPFS = require('is-ipfs') +const promisify = require('promisify-es6') module.exports = (send) => { - return argCommand(send, 'cat') + const cat = promisify((multihash, callback) => { + try { + multihash = cleanMultihash(multihash) + } catch (err) { + return callback(err) + } + send('cat', multihash, null, null, function (err, result) { + if (err) { + return callback(err) + } + return callback(null, result) + }) + }) + return cat +} + +function cleanMultihash (multihash) { + if (!isIPFS.multihash(multihash)) { + throw new Error('not valid multihash') + } + if (Buffer.isBuffer(multihash)) { + return bs58.encode(multihash) + } + return multihash } diff --git a/test/api/file.spec.js b/test/api/file.spec.js new file mode 100644 index 000000000..ce2338369 --- /dev/null +++ b/test/api/file.spec.js @@ -0,0 +1,17 @@ +/* eslint-env mocha */ +/* globals apiClients */ + +'use strict' + +const test = require('interface-ipfs-core') + +const common = { + setup: function (cb) { + cb(null, apiClients.a) + }, + teardown: function (cb) { + cb() + } +} + +test.files(common)