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

doc: Documents the offset/length arguments to ipfs.files.cat and friends #242

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions SPEC/FILES.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ pull(

##### `Go` **WIP**

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

ipfsPath can be of type:
`ipfsPath` can be of type:

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

`options` is an optional object that may contain the following keys:
- `offset` is an optional byte offset to start the stream at
- `length` is an optional number of bytes to read from the stream

`callback` must follow `function (err, file) {}` signature, where `err` is an error if the operation was not successful and `file` is a [Buffer][b]

If no `callback` is passed, a promise is returned.
Expand All @@ -196,9 +200,9 @@ A great source of [examples][] can be found in the tests for this API.

##### `Go` **WIP**

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

ipfsPath can be of type:
`ipfsPath` can be of type:

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

`options` is an optional object that may contain the following keys:
- `offset` is an optional byte offset to start the stream at
- `length` is an optional number of bytes to read from the stream

Returns a [Readable Stream][rs] with the contents of the file.


Expand All @@ -225,9 +233,9 @@ A great source of [examples][] can be found in the tests for this API.

##### `Go` **WIP**

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

ipfsPath can be of type:
`ipfsPath` can be of type:

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

`options` is an optional object that may contain the following keys:
- `offset` is an optional byte offset to start the stream at
- `length` is an optional number of bytes to read from the stream

Returns a [Pull Stream][ps] with the contents of the file.

```JavaScript
Expand Down
59 changes: 57 additions & 2 deletions js/src/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = (common) => {
this.timeout(40 * 1000)

let ipfs
// let withGo
let withGo

function fixture (path) {
return loadFixture(path, 'interface-ipfs-core')
Expand Down Expand Up @@ -64,7 +64,7 @@ module.exports = (common) => {
ipfs = node
node.id((err, id) => {
expect(err).to.not.exist()
// withGo = id.agentVersion.startsWith('go-ipfs')
withGo = id.agentVersion.startsWith('go-ipfs')
done()
})
})
Expand Down Expand Up @@ -474,6 +474,22 @@ module.exports = (common) => {
})
})
})

it('exports a chunk of a file', (done) => {
if (withGo) { this.skip() }

const offset = 1
const length = 3

ipfs.files.cat(smallFile.cid, {
offset,
length
}, (err, data) => {
expect(err).to.not.exist()
expect(data.toString()).to.equal('lz ')
done()
})
})
})

describe('.catReadableStream', () => {
Expand All @@ -488,6 +504,24 @@ module.exports = (common) => {
done()
}))
})

it('exports a chunk of a file in a ReadableStream', (done) => {
if (withGo) { this.skip() }

const offset = 1
const length = 3

const stream = ipfs.files.catReadableStream(smallFile.cid, {
offset,
length
})

stream.pipe(bl((err, data) => {
expect(err).to.not.exist()
expect(data.toString()).to.equal('lz ')
done()
}))
})
})

describe('.catPullStream', () => {
Expand All @@ -506,6 +540,27 @@ module.exports = (common) => {
})
)
})

it('exports a chunk of a file in a PullStream', (done) => {
if (withGo) { this.skip() }

const offset = 1
const length = 3

const stream = ipfs.files.catPullStream(smallFile.cid, {
offset,
length
})

pull(
stream,
pull.concat((err, data) => {
expect(err).to.not.exist()
expect(data.toString()).to.equal('lz ')
done()
})
)
})
})

describe('.get', () => {
Expand Down