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

Commit 4b39a4f

Browse files
committed
doc: Documents the offset/length arguments to ipfs.files.cat and friends
1 parent 4536160 commit 4b39a4f

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
@@ -159,9 +159,9 @@ pull(
159159
160160
##### `Go` **WIP**
161161

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

164-
ipfsPath can be of type:
164+
`ipfsPath` can be of type:
165165

166166
- [`cid`][cid] of type:
167167
- a [CID](https://github.com/ipfs/js-cid) instance
@@ -172,6 +172,10 @@ ipfsPath can be of type:
172172
- '/ipfs/QmXEmhrMpbVvTh61FNAxP9nU7ygVtyvZA8HZDUaqQCAb66/a.txt'
173173
- 'QmXEmhrMpbVvTh61FNAxP9nU7ygVtyvZA8HZDUaqQCAb66/a.txt'
174174

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

177181
If no `callback` is passed, a promise is returned.
@@ -196,9 +200,9 @@ A great source of [examples][] can be found in the tests for this API.
196200
197201
##### `Go` **WIP**
198202

199-
##### `JavaScript` - ipfs.files.catReadableStream(ipfsPath) -> [Readable Stream][rs]
203+
##### `JavaScript` - ipfs.files.catReadableStream(ipfsPath, [options]) -> [Readable Stream][rs]
200204

201-
ipfsPath can be of type:
205+
`ipfsPath` can be of type:
202206

203207
- [`cid`][cid] of type:
204208
- a [CID](https://github.com/ipfs/js-cid) instance
@@ -209,6 +213,10 @@ ipfsPath can be of type:
209213
- '/ipfs/QmXEmhrMpbVvTh61FNAxP9nU7ygVtyvZA8HZDUaqQCAb66/a.txt'
210214
- 'QmXEmhrMpbVvTh61FNAxP9nU7ygVtyvZA8HZDUaqQCAb66/a.txt'
211215

216+
`options` is an optional object that may contain the following keys:
217+
- `offset` is an optional byte offset to start the stream at
218+
- `length` is an optional number of bytes to read from the stream
219+
212220
Returns a [Readable Stream][rs] with the contents of the file.
213221

214222

@@ -225,9 +233,9 @@ A great source of [examples][] can be found in the tests for this API.
225233
226234
##### `Go` **WIP**
227235

228-
##### `JavaScript` - ipfs.files.catPullStream(ipfsPath) -> [Pull Stream][rs]
236+
##### `JavaScript` - ipfs.files.catPullStream(ipfsPath, [options]) -> [Pull Stream][rs]
229237

230-
ipfsPath can be of type:
238+
`ipfsPath` can be of type:
231239

232240
- [`cid`][cid] of type:
233241
- [Buffer][b], the raw Buffer of the cid
@@ -237,6 +245,10 @@ ipfsPath can be of type:
237245
- '/ipfs/QmXEmhrMpbVvTh61FNAxP9nU7ygVtyvZA8HZDUaqQCAb66/a.txt'
238246
- 'QmXEmhrMpbVvTh61FNAxP9nU7ygVtyvZA8HZDUaqQCAb66/a.txt'
239247

248+
`options` is an optional object that may contain the following keys:
249+
- `offset` is an optional byte offset to start the stream at
250+
- `length` is an optional number of bytes to read from the stream
251+
240252
Returns a [Pull Stream][ps] with the contents of the file.
241253

242254
```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')
@@ -64,7 +64,7 @@ module.exports = (common) => {
6464
ipfs = node
6565
node.id((err, id) => {
6666
expect(err).to.not.exist()
67-
// withGo = id.agentVersion.startsWith('go-ipfs')
67+
withGo = id.agentVersion.startsWith('go-ipfs')
6868
done()
6969
})
7070
})
@@ -474,6 +474,22 @@ module.exports = (common) => {
474474
})
475475
})
476476
})
477+
478+
it('exports a chunk of a file', (done) => {
479+
if (withGo) { this.skip() }
480+
481+
const offset = 1
482+
const length = 3
483+
484+
ipfs.files.cat(smallFile.cid, {
485+
offset,
486+
length
487+
}, (err, data) => {
488+
expect(err).to.not.exist()
489+
expect(data.toString()).to.equal('lz ')
490+
done()
491+
})
492+
})
477493
})
478494

479495
describe('.catReadableStream', () => {
@@ -488,6 +504,24 @@ module.exports = (common) => {
488504
done()
489505
}))
490506
})
507+
508+
it('exports a chunk of a file in a ReadableStream', (done) => {
509+
if (withGo) { this.skip() }
510+
511+
const offset = 1
512+
const length = 3
513+
514+
const stream = ipfs.files.catReadableStream(smallFile.cid, {
515+
offset,
516+
length
517+
})
518+
519+
stream.pipe(bl((err, data) => {
520+
expect(err).to.not.exist()
521+
expect(data.toString()).to.equal('lz ')
522+
done()
523+
}))
524+
})
491525
})
492526

493527
describe('.catPullStream', () => {
@@ -506,6 +540,27 @@ module.exports = (common) => {
506540
})
507541
)
508542
})
543+
544+
it('exports a chunk of a file in a PullStream', (done) => {
545+
if (withGo) { this.skip() }
546+
547+
const offset = 1
548+
const length = 3
549+
550+
const stream = ipfs.files.catPullStream(smallFile.cid, {
551+
offset,
552+
length
553+
})
554+
555+
pull(
556+
stream,
557+
pull.concat((err, data) => {
558+
expect(err).to.not.exist()
559+
expect(data.toString()).to.equal('lz ')
560+
done()
561+
})
562+
)
563+
})
509564
})
510565

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

0 commit comments

Comments
 (0)