diff --git a/package.json b/package.json index 32404487c..c730d69ec 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "flatmap": "0.0.3", "glob": "^7.0.3", "ipfs-merkle-dag": "^0.6.0", + "is-ipfs": "^0.2.0", "isstream": "^0.1.2", "multiaddr": "^2.0.0", "multipart-stream": "^2.0.1", @@ -32,7 +33,7 @@ "aegir": "^3.2.0", "chai": "^3.5.0", "gulp": "^3.9.1", - "interface-ipfs-core": "^0.2.2", + "interface-ipfs-core": "^0.3.0", "ipfsd-ctl": "^0.14.0", "pre-commit": "^1.1.2", "stream-equal": "^0.1.8", @@ -93,4 +94,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..8b61f66a1 100644 --- a/src/api/cat.js +++ b/src/api/cat.js @@ -1,7 +1,27 @@ '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, callback) + }) + 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/add.spec.js b/test/api/add.spec.js deleted file mode 100644 index 67c190235..000000000 --- a/test/api/add.spec.js +++ /dev/null @@ -1,16 +0,0 @@ -/* 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) diff --git a/test/api/cat.spec.js b/test/api/cat.spec.js deleted file mode 100644 index 0e5a9508e..000000000 --- a/test/api/cat.spec.js +++ /dev/null @@ -1,77 +0,0 @@ -/* eslint-env mocha */ -/* globals apiClients */ -'use strict' - -const expect = require('chai').expect -const isNode = require('detect-node') -const fs = require('fs') -const streamifier = require('streamifier') - -const path = require('path') -const streamEqual = require('stream-equal') - -let testfile -let testfileBig - -testfile = fs.readFileSync(path.join(__dirname, '/../testfile.txt')) -testfileBig = fs.readFileSync(path.join(__dirname, '/../15mb.random')) - -describe('.cat', () => { - it('cat', (done) => { - apiClients.a - .cat('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => { - expect(err).to.not.exist - - let buf = '' - res - .on('error', (err) => { - expect(err).to.not.exist - }) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(buf).to.be.equal(testfile.toString()) - done() - }) - }) - }) - - it('cat BIG file', (done) => { - if (!isNode) { - return done() - } - - apiClients.a.cat('Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq', (err, res) => { - expect(err).to.not.exist - - // Do not blow out the memory of nodejs :) - const bigStream = streamifier.createReadStream(testfileBig) - streamEqual(res, bigStream, (err, equal) => { - expect(err).to.not.exist - expect(equal).to.be.true - done() - }) - }) - }) - - describe('promise', () => { - it('cat', (done) => { - return apiClients.a.cat('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP') - .then((res) => { - let buf = '' - res - .on('error', (err) => { - throw err - }) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(buf).to.be.equal(testfile.toString()) - done() - }) - }) - }) - }) -}) diff --git a/test/api/files.spec.js b/test/api/files.spec.js index d46254c99..e7f97a0f4 100644 --- a/test/api/files.spec.js +++ b/test/api/files.spec.js @@ -5,12 +5,25 @@ const expect = require('chai').expect const isNode = require('detect-node') const path = require('path') +const test = require('interface-ipfs-core') let testfile testfile = require('fs').readFileSync(path.join(__dirname, '/../testfile.txt')) -describe('.files', () => { +// Load the add/cat/get/ls commands from interface-ipfs-core +const common = { + setup: function (cb) { + cb(null, apiClients.a) + }, + teardown: function (cb) { + cb() + } +} +test.files(common) + +// Describe the (mfs) tests +describe('.files (pseudo mfs)', () => { it('files.mkdir', (done) => { apiClients.a.files.mkdir('/test-folder', function (err) { expect(err).to.not.exist