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

Commit 4d8a744

Browse files
achingbraindaviddias
authored andcommitted
docs: Documents the offset/length arguments to ipfs.files.cat and friends (#242)
1 parent 646ff24 commit 4d8a744

File tree

2 files changed

+75
-8
lines changed

2 files changed

+75
-8
lines changed

SPEC/FILES.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ pull(
162162
163163
##### `Go` **WIP**
164164

165-
##### `JavaScript` - ipfs.files.cat(ipfsPath, [callback])
165+
##### `JavaScript` - ipfs.files.cat(ipfsPath, [options], [callback])
166166

167-
ipfsPath can be of type:
167+
`ipfsPath` can be of type:
168168

169169
- [`cid`][cid] of type:
170170
- a [CID](https://github.com/ipfs/js-cid) instance
@@ -175,6 +175,10 @@ ipfsPath can be of type:
175175
- '/ipfs/QmXEmhrMpbVvTh61FNAxP9nU7ygVtyvZA8HZDUaqQCAb66/a.txt'
176176
- 'QmXEmhrMpbVvTh61FNAxP9nU7ygVtyvZA8HZDUaqQCAb66/a.txt'
177177

178+
`options` is an optional object that may contain the following keys:
179+
- `offset` is an optional byte offset to start the stream at
180+
- `length` is an optional number of bytes to read from the stream
181+
178182
`callback` must follow `function (err, file) {}` signature, where `err` is an error if the operation was not successful and `file` is a [Buffer][b]
179183

180184
If no `callback` is passed, a promise is returned.
@@ -199,9 +203,9 @@ A great source of [examples][] can be found in the tests for this API.
199203
200204
##### `Go` **WIP**
201205

202-
##### `JavaScript` - ipfs.files.catReadableStream(ipfsPath) -> [Readable Stream][rs]
206+
##### `JavaScript` - ipfs.files.catReadableStream(ipfsPath, [options]) -> [Readable Stream][rs]
203207

204-
ipfsPath can be of type:
208+
`ipfsPath` can be of type:
205209

206210
- [`cid`][cid] of type:
207211
- a [CID](https://github.com/ipfs/js-cid) instance
@@ -212,6 +216,10 @@ ipfsPath can be of type:
212216
- '/ipfs/QmXEmhrMpbVvTh61FNAxP9nU7ygVtyvZA8HZDUaqQCAb66/a.txt'
213217
- 'QmXEmhrMpbVvTh61FNAxP9nU7ygVtyvZA8HZDUaqQCAb66/a.txt'
214218

219+
`options` is an optional object that may contain the following keys:
220+
- `offset` is an optional byte offset to start the stream at
221+
- `length` is an optional number of bytes to read from the stream
222+
215223
Returns a [Readable Stream][rs] with the contents of the file.
216224

217225

@@ -228,9 +236,9 @@ A great source of [examples][] can be found in the tests for this API.
228236
229237
##### `Go` **WIP**
230238

231-
##### `JavaScript` - ipfs.files.catPullStream(ipfsPath) -> [Pull Stream][rs]
239+
##### `JavaScript` - ipfs.files.catPullStream(ipfsPath, [options]) -> [Pull Stream][rs]
232240

233-
ipfsPath can be of type:
241+
`ipfsPath` can be of type:
234242

235243
- [`cid`][cid] of type:
236244
- [Buffer][b], the raw Buffer of the cid
@@ -240,6 +248,10 @@ ipfsPath can be of type:
240248
- '/ipfs/QmXEmhrMpbVvTh61FNAxP9nU7ygVtyvZA8HZDUaqQCAb66/a.txt'
241249
- 'QmXEmhrMpbVvTh61FNAxP9nU7ygVtyvZA8HZDUaqQCAb66/a.txt'
242250

251+
`options` is an optional object that may contain the following keys:
252+
- `offset` is an optional byte offset to start the stream at
253+
- `length` is an optional number of bytes to read from the stream
254+
243255
Returns a [Pull Stream][ps] with the contents of the file.
244256

245257
```JavaScript

js/src/files.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = (common) => {
2525
this.timeout(40 * 1000)
2626

2727
let ipfs
28-
// let withGo
28+
let withGo
2929

3030
function fixture (path) {
3131
return loadFixture(path, 'interface-ipfs-core')
@@ -70,7 +70,7 @@ module.exports = (common) => {
7070
ipfs = node
7171
node.id((err, id) => {
7272
expect(err).to.not.exist()
73-
// withGo = id.agentVersion.startsWith('go-ipfs')
73+
withGo = id.agentVersion.startsWith('go-ipfs')
7474
done()
7575
})
7676
})
@@ -493,6 +493,22 @@ module.exports = (common) => {
493493
})
494494
})
495495
})
496+
497+
it('exports a chunk of a file', (done) => {
498+
if (withGo) { this.skip() }
499+
500+
const offset = 1
501+
const length = 3
502+
503+
ipfs.files.cat(smallFile.cid, {
504+
offset,
505+
length
506+
}, (err, data) => {
507+
expect(err).to.not.exist()
508+
expect(data.toString()).to.equal('lz ')
509+
done()
510+
})
511+
})
496512
})
497513

498514
describe('.catReadableStream', () => {
@@ -507,6 +523,24 @@ module.exports = (common) => {
507523
done()
508524
}))
509525
})
526+
527+
it('exports a chunk of a file in a ReadableStream', (done) => {
528+
if (withGo) { this.skip() }
529+
530+
const offset = 1
531+
const length = 3
532+
533+
const stream = ipfs.files.catReadableStream(smallFile.cid, {
534+
offset,
535+
length
536+
})
537+
538+
stream.pipe(bl((err, data) => {
539+
expect(err).to.not.exist()
540+
expect(data.toString()).to.equal('lz ')
541+
done()
542+
}))
543+
})
510544
})
511545

512546
describe('.catPullStream', () => {
@@ -525,6 +559,27 @@ module.exports = (common) => {
525559
})
526560
)
527561
})
562+
563+
it('exports a chunk of a file in a PullStream', (done) => {
564+
if (withGo) { this.skip() }
565+
566+
const offset = 1
567+
const length = 3
568+
569+
const stream = ipfs.files.catPullStream(smallFile.cid, {
570+
offset,
571+
length
572+
})
573+
574+
pull(
575+
stream,
576+
pull.concat((err, data) => {
577+
expect(err).to.not.exist()
578+
expect(data.toString()).to.equal('lz ')
579+
done()
580+
})
581+
)
582+
})
528583
})
529584

530585
describe('.get', () => {

0 commit comments

Comments
 (0)