diff --git a/README.md b/README.md index 3f60c5a3..79f9bf47 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,29 @@ test.all(common) A valid (read: that follows this interface) IPFS core implementation, must expose the following API. +## Files + +### `cat` + +> Streams the data contained by an IPFS object(s) at the given IPFS multihash.. + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.cat(multihash, [callback]) + +`multihash` is a [multihash][] which can be passed as + +- Buffer, the raw Buffer of the multihash (or of and encoded version) +- String, the toString version of the multihash (or of an encoded version) + +`callback` must follow `function (err, stream) {}` signature, where `err` is an error if the operation was not successful and `stream` is a readable stream of the file. + +If no `callback` is passed, a promise is returned. + + + + + ## Object ### `object.new` diff --git a/test/files.js b/test/files.js new file mode 100644 index 00000000..68ec69dd --- /dev/null +++ b/test/files.js @@ -0,0 +1,133 @@ +/* eslint-env mocha */ +'use strict' + +const expect = require('chai').expect +const bl = require('bl') +const bs58 = require('bs58') +const Readable = require('readable-stream') + +module.exports = (common) => { + describe('.file', () => { + let ipfs + + before((done) => { + common.setup((err, _ipfs) => { + expect(err).to.not.exist + ipfs = _ipfs + done() + }) + }) + + after((done) => { + common.teardown(done) + }) + + // go-ipfs http and js-ipfs core have different responses + describe('.add', () => { + it('add', (done) => { + const buffered = new Buffer('some data') + const rs = new Readable() + rs.push(buffered) + rs.push(null) + const arr = [] + const filePair = {path: 'data.txt', content: rs} + arr.push(filePair) + ipfs.add(arr, (err, res) => { + expect(err).to.not.exist + expect(res[0].path).to.equal('data.txt') + expect(res[0].size).to.equal(17) + expect(bs58.encode(res[0].multihash).toString()).to.equal('QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS') + done() + }) + }) + }) + + describe('.cat', () => { + it('returns file stream', (done) => { + const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB' + ipfs.cat(hash, (err, file) => { + expect(err).to.not.exist + file.pipe(bl((err, bldata) => { + expect(err).to.not.exist + expect(bldata.toString()).to.contain('Check out some of the other files in this directory:') + done() + })) + }) + }) + + // This fails on js-ipfs-api + it.skip('takes a buffer input', (done) => { + const mhBuf = new Buffer(bs58.decode('QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB')) + ipfs.cat(mhBuf, (err, file) => { + expect(err).to.not.exist + file.pipe(bl((err, bldata) => { + expect(err).to.not.exist + expect(bldata.toString()).to.contain('Check out some of the other files in this directory:') + done() + })) + }) + }) + + // You can add a large file to your ipfs repo and change the hash to the file after installing js-ipfs + it.skip('returns a large file', (done) => { + const hash = '' + ipfs.cat(hash, (err, file) => { + expect(err).to.not.exist + file.pipe(bl((err, bldata) => { + expect(err).to.not.exist + done() + })) + }) + }) + + it('returns error on invalid key', (done) => { + const hash = 'somethingNotMultihash' + ipfs.cat(hash, (err, file) => { + expect(err).to.exist + const errString = err.toString() + if (errString === 'Error: invalid ipfs ref path') { + expect(err.toString()).to.contain('Error: invalid ipfs ref path') + } + if (errString === 'Error: Invalid Key') { + expect(err.toString()).to.contain('Error: Invalid Key') + } + done() + }) + }) + + describe('promise', () => { + it('files.cat', (done) => { + const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB' + ipfs.cat(hash) + .then((stream) => { + stream.pipe(bl((err, bldata) => { + expect(err).to.not.exist + expect(bldata.toString()).to.contain('Check out some of the other files in this directory:') + done() + })) + }) + .catch((err) => { + expect(err).to.not.exist + }) + }) + + it('returns error on invalid key', (done) => { + const hash = 'somethingNotMultihash' + ipfs.cat(hash) + .then((stream) => {}) + .catch((err) => { + expect(err).to.exist + const errString = err.toString() + if (errString === 'Error: invalid ipfs ref path') { + expect(err.toString()).to.contain('Error: invalid ipfs ref path') + } + if (errString === 'Error: Invalid Key') { + expect(err.toString()).to.contain('Error: Invalid Key') + } + done() + }) + }) + }) + }) + }) +} diff --git a/test/index.js b/test/index.js index 8a8de083..574aec51 100644 --- a/test/index.js +++ b/test/index.js @@ -2,3 +2,4 @@ exports.all = () => {} exports.object = require('./object') +exports.files = require('./files') diff --git a/test/test-data/15mb.random b/test/test-data/15mb.random new file mode 100644 index 00000000..8ee755f5 Binary files /dev/null and b/test/test-data/15mb.random differ