diff --git a/src/core/read-pull-stream.js b/src/core/read-pull-stream.js index f908eca..353845d 100644 --- a/src/core/read-pull-stream.js +++ b/src/core/read-pull-stream.js @@ -22,6 +22,9 @@ module.exports = (context) => { return function mfsReadPullStream (path, options = {}) { options = Object.assign({}, defaultOptions, options) + // support legacy go arguments + options.length = options.length || options.count + log(`Reading ${path}`) const deferred = defer.source() diff --git a/src/http/read.js b/src/http/read.js index 7b06df9..2686dc6 100644 --- a/src/http/read.js +++ b/src/http/read.js @@ -17,12 +17,14 @@ const mfsRead = (api) => { const { arg, offset, - length + length, + count } = request.query const stream = ipfs.files.readReadableStream(arg, { offset, - length + length, + count }) if (!stream._read) { diff --git a/test/read.spec.js b/test/read.spec.js index 4e830b9..a521251 100644 --- a/test/read.spec.js +++ b/test/read.spec.js @@ -128,6 +128,25 @@ describe('read', function () { .then((buffer) => expect(buffer).to.deep.equal(data.slice(0, length))) }) + it('reads a file with a legacy count argument', () => { + const path = `/some-file-${Math.random()}.txt` + let data = Buffer.alloc(0) + const length = 10 + + return mfs.write(path, bufferStream(100, { + collector: (bytes) => { + data = Buffer.concat([data, bytes]) + } + }), { + create: true + }) + .then(() => method.read(path, { + count: length + })) + .then((result) => method.collect(result)) + .then((buffer) => expect(buffer).to.deep.equal(data.slice(0, length))) + }) + it('reads a file with an offset and a length', () => { const path = `/some-file-${Math.random()}.txt` let data = Buffer.alloc(0) @@ -149,6 +168,27 @@ describe('read', function () { .then((buffer) => expect(buffer).to.deep.equal(data.slice(offset, offset + length))) }) + it('reads a file with an offset and a legacy count argument', () => { + const path = `/some-file-${Math.random()}.txt` + let data = Buffer.alloc(0) + const offset = 10 + const length = 10 + + return mfs.write(path, bufferStream(100, { + collector: (bytes) => { + data = Buffer.concat([data, bytes]) + } + }), { + create: true + }) + .then(() => method.read(path, { + offset, + count: length + })) + .then((result) => method.collect(result)) + .then((buffer) => expect(buffer).to.deep.equal(data.slice(offset, offset + length))) + }) + it('refuses to read a directory', () => { const path = '/'