Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit e787bf9

Browse files
committed
fix: support count as well as length
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]>
1 parent 55faec0 commit e787bf9

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

src/core/read-pull-stream.js

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ module.exports = (context) => {
2222
return function mfsReadPullStream (path, options = {}) {
2323
options = Object.assign({}, defaultOptions, options)
2424

25+
// support legacy go arguments
26+
options.length = options.length || options.count
27+
2528
log(`Reading ${path}`)
2629

2730
const deferred = defer.source()

src/http/read.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ const mfsRead = (api) => {
1717
const {
1818
arg,
1919
offset,
20-
length
20+
length,
21+
count
2122
} = request.query
2223

2324
const stream = ipfs.files.readReadableStream(arg, {
2425
offset,
25-
length
26+
length,
27+
count
2628
})
2729

2830
if (!stream._read) {

test/read.spec.js

+40
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,25 @@ describe('read', function () {
128128
.then((buffer) => expect(buffer).to.deep.equal(data.slice(0, length)))
129129
})
130130

131+
it('reads a file with a legacy count argument', () => {
132+
const path = `/some-file-${Math.random()}.txt`
133+
let data = Buffer.alloc(0)
134+
const length = 10
135+
136+
return mfs.write(path, bufferStream(100, {
137+
collector: (bytes) => {
138+
data = Buffer.concat([data, bytes])
139+
}
140+
}), {
141+
create: true
142+
})
143+
.then(() => method.read(path, {
144+
count: length
145+
}))
146+
.then((result) => method.collect(result))
147+
.then((buffer) => expect(buffer).to.deep.equal(data.slice(0, length)))
148+
})
149+
131150
it('reads a file with an offset and a length', () => {
132151
const path = `/some-file-${Math.random()}.txt`
133152
let data = Buffer.alloc(0)
@@ -149,6 +168,27 @@ describe('read', function () {
149168
.then((buffer) => expect(buffer).to.deep.equal(data.slice(offset, offset + length)))
150169
})
151170

171+
it('reads a file with an offset and a legacy count argument', () => {
172+
const path = `/some-file-${Math.random()}.txt`
173+
let data = Buffer.alloc(0)
174+
const offset = 10
175+
const length = 10
176+
177+
return mfs.write(path, bufferStream(100, {
178+
collector: (bytes) => {
179+
data = Buffer.concat([data, bytes])
180+
}
181+
}), {
182+
create: true
183+
})
184+
.then(() => method.read(path, {
185+
offset,
186+
count: length
187+
}))
188+
.then((result) => method.collect(result))
189+
.then((buffer) => expect(buffer).to.deep.equal(data.slice(offset, offset + length)))
190+
})
191+
152192
it('refuses to read a directory', () => {
153193
const path = '/'
154194

0 commit comments

Comments
 (0)