From 70c9df13d5d30b526786243fe91596452dc55abe Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Thu, 2 Aug 2018 18:23:28 +0100 Subject: [PATCH] feat: expose mfs files.read*Stream methods (#823) * feat: compatible with go-ipfs 0.4.16 * chore: update to use go0.4.17 * feat: expose mfs files.read*Stream methods * chore: update interface-ipfs-core dependency License: MIT Signed-off-by: Alan Shaw * chore: increase timeouts for urlAdd tests License: MIT Signed-off-by: Alan Shaw --- package.json | 12 ++++++------ src/files/index.js | 2 ++ src/files/read-pull-stream.js | 26 ++++++++++++++++++++++++++ src/files/read-readable-stream.js | 26 ++++++++++++++++++++++++++ test/util.spec.js | 18 ++++++++++-------- 5 files changed, 70 insertions(+), 14 deletions(-) create mode 100644 src/files/read-pull-stream.js create mode 100644 src/files/read-readable-stream.js diff --git a/package.json b/package.json index 8aaa3697d..4257eddca 100644 --- a/package.json +++ b/package.json @@ -38,8 +38,8 @@ "ipfs-block": "~0.7.1", "ipfs-unixfs": "~0.1.15", "ipld-dag-cbor": "~0.12.1", - "ipld-dag-pb": "~0.14.5", - "is-ipfs": "~0.3.2", + "ipld-dag-pb": "~0.14.6", + "is-ipfs": "~0.4.2", "is-pull-stream": "0.0.0", "is-stream": "^1.1.0", "libp2p-crypto": "~0.13.0", @@ -72,16 +72,16 @@ "url": "https://github.com/ipfs/js-ipfs-api" }, "devDependencies": { - "aegir": "^15.0.0", + "aegir": "^15.1.0", "browser-process-platform": "~0.1.1", "chai": "^4.1.2", "cross-env": "^5.2.0", "dirty-chai": "^2.0.1", "eslint-plugin-react": "^7.10.0", - "go-ipfs-dep": "~0.4.16", + "go-ipfs-dep": "~0.4.17", "gulp": "^3.9.1", - "interface-ipfs-core": "~0.72.1", - "ipfsd-ctl": "~0.38.0", + "interface-ipfs-core": "~0.73.0", + "ipfsd-ctl": "~0.39.0", "pull-stream": "^3.6.8", "socket.io": "^2.1.1", "socket.io-client": "^2.1.1", diff --git a/src/files/index.js b/src/files/index.js index f729f4305..6bcbd722b 100644 --- a/src/files/index.js +++ b/src/files/index.js @@ -24,6 +24,8 @@ module.exports = (arg) => { rm: require('./rm')(send), ls: require('./ls')(send), read: require('./read')(send), + readReadableStream: require('./read-readable-stream')(send), + readPullStream: require('./read-pull-stream')(send), write: require('./write')(send), mv: require('./mv')(send) } diff --git a/src/files/read-pull-stream.js b/src/files/read-pull-stream.js new file mode 100644 index 000000000..3e9ab8c3d --- /dev/null +++ b/src/files/read-pull-stream.js @@ -0,0 +1,26 @@ +'use strict' + +const toPull = require('stream-to-pull-stream') +const deferred = require('pull-defer') + +module.exports = (send) => { + return (args, opts) => { + opts = opts || {} + + const p = deferred.source() + + send({ + path: 'files/read', + args: args, + qs: opts + }, (err, stream) => { + if (err) { + return p.abort(err) + } + + p.resolve(toPull(stream)) + }) + + return p + } +} diff --git a/src/files/read-readable-stream.js b/src/files/read-readable-stream.js new file mode 100644 index 000000000..df8b92fbf --- /dev/null +++ b/src/files/read-readable-stream.js @@ -0,0 +1,26 @@ +'use strict' + +const Stream = require('readable-stream') +const pump = require('pump') + +module.exports = (send) => { + return (args, opts) => { + opts = opts || {} + + const pt = new Stream.PassThrough() + + send({ + path: 'files/read', + args: args, + qs: opts + }, (err, stream) => { + if (err) { + return pt.destroy(err) + } + + pump(stream, pt) + }) + + return pt + } +} diff --git a/test/util.spec.js b/test/util.spec.js index 52ce9ebd7..f905911cc 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -114,7 +114,7 @@ describe('.util', () => { describe('.urlAdd', () => { it('http', function (done) { - this.timeout(20 * 1000) + this.timeout(40 * 1000) ipfs.util.addFromURL('http://example.com/', (err, result) => { expect(err).to.not.exist() @@ -124,7 +124,7 @@ describe('.util', () => { }) it('https', function (done) { - this.timeout(20 * 1000) + this.timeout(40 * 1000) ipfs.util.addFromURL('https://example.com/', (err, result) => { expect(err).to.not.exist() @@ -134,7 +134,7 @@ describe('.util', () => { }) it('http with redirection', function (done) { - this.timeout(20 * 1000) + this.timeout(40 * 1000) ipfs.util.addFromURL('http://covers.openlibrary.org/book/id/969165.jpg', (err, result) => { expect(err).to.not.exist() @@ -143,8 +143,8 @@ describe('.util', () => { }) }) - it('.urlAdd http with redirection', function (done) { - this.timeout(20 * 1000) + it('https with redirection', function (done) { + this.timeout(40 * 1000) ipfs.util.addFromURL('https://coverartarchive.org/release/6e2a1694-d8b9-466a-aa33-b1077b2333c1', (err, result) => { expect(err).to.not.exist() @@ -161,7 +161,7 @@ describe('.util', () => { }) it('with wrap-with-directory=true', function (done) { - this.timeout(20 * 1000) + this.timeout(40 * 1000) ipfs.util.addFromURL('http://ipfs.io/ipfs/QmWjppACLcFLQ2qL38unKQvJBhXH3RUtcGLPk7zmrTwV61/969165.jpg?foo=bar#buzz', { wrapWithDirectory: true @@ -175,7 +175,9 @@ describe('.util', () => { }) }) - it('with wrap-with-directory=true and URL-escaped file name', (done) => { + it('with wrap-with-directory=true and URL-escaped file name', function (done) { + this.timeout(40 * 1000) + // Sample URL contains URL-escaped ( ) and local diacritics ipfs.util.addFromURL('https://upload.wikimedia.org/wikipedia/commons/thumb/c/cf/Doma%C5%BElice%2C_Jir%C3%A1skova_43_%289102%29.jpg/320px-Doma%C5%BElice%2C_Jir%C3%A1skova_43_%289102%29.jpg?foo=bar#buzz', { wrapWithDirectory: true @@ -189,7 +191,7 @@ describe('.util', () => { }) }) - it('with invalid url', function (done) { + it('with invalid url', (done) => { ipfs.util.addFromURL('http://invalid', (err, result) => { expect(err.code).to.equal('ENOTFOUND') expect(result).to.not.exist()