diff --git a/js/src/files/index.js b/js/src/files/index.js index 11829cc14..62cc2a156 100644 --- a/js/src/files/index.js +++ b/js/src/files/index.js @@ -18,6 +18,8 @@ const tests = { rm: require('./rm'), stat: require('./stat'), read: require('./read'), + readReadableStream: require('./read-readable-stream'), + readPullStream: require('./read-pull-stream'), ls: require('./ls'), flush: require('./flush') } diff --git a/js/src/files/read-pull-stream.js b/js/src/files/read-pull-stream.js new file mode 100644 index 000000000..34cf6c289 --- /dev/null +++ b/js/src/files/read-pull-stream.js @@ -0,0 +1,70 @@ +/* eslint-env mocha */ +'use strict' + +const series = require('async/series') +const hat = require('hat') +const { getDescribe, getIt, expect } = require('../utils/mocha') +const pull = require('pull-stream/pull') +const collect = require('pull-stream/sinks/collect') + +module.exports = (createCommon, options) => { + const describe = getDescribe(options) + const it = getIt(options) + const common = createCommon() + + describe('.files.readPullStream', function () { + this.timeout(40 * 1000) + + let ipfs + + before(function (done) { + // CI takes longer to instantiate the daemon, so we need to increase the + // timeout for the before step + this.timeout(60 * 1000) + + common.setup((err, factory) => { + expect(err).to.not.exist() + factory.spawnNode((err, node) => { + expect(err).to.not.exist() + ipfs = node + done() + }) + }) + }) + + after((done) => common.teardown(done)) + + it('should not read not found, expect error', (done) => { + const testDir = `/test-${hat()}` + + pull( + ipfs.files.readPullStream(`${testDir}/404`), + collect((err) => { + expect(err).to.exist() + expect(err.message).to.contain('does not exist') + done() + }) + ) + }) + + it('should read file', (done) => { + const testDir = `/test-${hat()}` + + series([ + (cb) => ipfs.files.mkdir(testDir, cb), + (cb) => ipfs.files.write(`${testDir}/a`, Buffer.from('Hello, world!'), { create: true }, cb) + ], (err) => { + expect(err).to.not.exist() + + pull( + ipfs.files.readPullStream(`${testDir}/a`), + collect((err, bufs) => { + expect(err).to.not.exist() + expect(bufs).to.eql([Buffer.from('Hello, world!')]) + done() + }) + ) + }) + }) + }) +} diff --git a/js/src/files/read-readable-stream.js b/js/src/files/read-readable-stream.js new file mode 100644 index 000000000..e9a6430e1 --- /dev/null +++ b/js/src/files/read-readable-stream.js @@ -0,0 +1,67 @@ +/* eslint-env mocha */ +'use strict' + +const series = require('async/series') +const hat = require('hat') +const { getDescribe, getIt, expect } = require('../utils/mocha') +const bl = require('bl') + +module.exports = (createCommon, options) => { + const describe = getDescribe(options) + const it = getIt(options) + const common = createCommon() + + describe('.files.readReadableStream', function () { + this.timeout(40 * 1000) + + let ipfs + + before(function (done) { + // CI takes longer to instantiate the daemon, so we need to increase the + // timeout for the before step + this.timeout(60 * 1000) + + common.setup((err, factory) => { + expect(err).to.not.exist() + factory.spawnNode((err, node) => { + expect(err).to.not.exist() + ipfs = node + done() + }) + }) + }) + + after((done) => common.teardown(done)) + + it('should not read not found, expect error', (done) => { + const testDir = `/test-${hat()}` + + const stream = ipfs.files.readReadableStream(`${testDir}/404`) + + stream.on('error', (err) => { + expect(err).to.exist() + expect(err.message).to.contain('does not exist') + done() + }) + }) + + it('should read file', (done) => { + const testDir = `/test-${hat()}` + + series([ + (cb) => ipfs.files.mkdir(testDir, cb), + (cb) => ipfs.files.write(`${testDir}/a`, Buffer.from('Hello, world!'), { create: true }, cb) + ], (err) => { + expect(err).to.not.exist() + + const stream = ipfs.files.readReadableStream(`${testDir}/a`) + + stream.pipe(bl((err, buf) => { + expect(err).to.not.exist() + expect(buf).to.eql(Buffer.from('Hello, world!')) + done() + })) + }) + }) + }) +}