Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
fix: support count as well as length
Browse files Browse the repository at this point in the history
fixes #21

Some userland applications switch between go and js implementations
at will.  Go prefers `offset`/`count`, the interface spec says it
should be `offset`/`length`. Bend like a reed in the wind and
support both.

License: MIT
Signed-off-by: achingbrain <[email protected]>
  • Loading branch information
achingbrain committed Nov 16, 2018
1 parent 55faec0 commit e787bf9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/core/read-pull-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 4 additions & 2 deletions src/http/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
40 changes: 40 additions & 0 deletions test/read.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 = '/'

Expand Down

0 comments on commit e787bf9

Please sign in to comment.