Skip to content

Commit

Permalink
Add start, end options to file reading functions
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Aug 20, 2017
1 parent 24a47be commit 33f97dc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,26 @@ Replaces one file in the DRS.
`id` is the new file ID.
`buffer` is a Buffer or string with the new file contents.

#### `DRS#readFile(id: number, callback)`
#### `DRS#readFile(id: number[, options], callback)`

Reads a file's contents for ID `id`. The callback gets an `err` and a `Buffer` containing the file contents.

#### `DRS#createReadStream(id: number): Readable`
Optionally, provide an `options` object to read only part of the file:

- `start` - Byte offset inside the file to start reading at.
- `end` - Byte offset inside the file to stop reading at.

#### `DRS#createReadStream(id: number[, options]): Readable`

Returns a Readable stream of the file contents for file ID `id`.

The returned stream also emits a `meta` event with information about the file, like in `getFiles()`.

Optionally, provide an `options` object to read only part of the file:

- `start` - Byte offset inside the file to start reading at.
- `end` - Byte offset inside the file to stop reading at.

#### `DRS#createWriteStream(type: string, id: number): Writable`

Returns a stream, stuff that is written to it will be saved in the DRS file.
Expand Down
34 changes: 29 additions & 5 deletions src/DRS.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,9 @@ DRS.prototype.getFile = function (id) {
*
* @return {Readable} A Readable stream.
*/
DRS.prototype.createReadStream = function (id) {
DRS.prototype.createReadStream = function (id, opts) {
var drs = this
opts = opts || {}

var stream = through()
if (!drs.numTables) {
Expand All @@ -264,7 +265,16 @@ DRS.prototype.createReadStream = function (id) {
pump(fromBuffer(file.buffer), stream)
return
}
pump(drs.source.createReadStream(file.offset, file.offset + file.size - 1), stream)

var start = 0
var end = file.size - 1
if (opts && typeof opts.start === 'number') {
start = opts.start
}
if (opts && typeof opts.end === 'number') {
end = opts.end
}
pump(drs.source.createReadStream(file.offset + start, file.offset + end), stream)
}
}

Expand All @@ -274,19 +284,33 @@ DRS.prototype.createReadStream = function (id) {
* @param {number} id File ID.
* @param {function} cb Function `(err, file)` to call when finished. `file` is a `DRSFile` object.
*/
DRS.prototype.readFile = function (id, cb) {
DRS.prototype.readFile = function (id, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}

var drs = this
// make sure we've read tables first
if (!this.numTables) {
return this.read(function (e) {
if (e) cb(e)
else drs.readFile(id, cb)
else drs.readFile(id, opts, cb)
})
}

var file = this.getFile(id)
if (file == null) return cb(new Error('Cannot find file #' + id))
this.source.read(file.offset, file.offset + file.size, function (err, buffer) {

var start = 0
var end = file.size
if (opts && typeof opts.start === 'number') {
start = opts.start
}
if (opts && typeof opts.end === 'number') {
end = opts.end
}
this.source.read(file.offset + start, file.offset + end, function (err, buffer) {
if (err) cb(err)
else cb(null, buffer, file)
})
Expand Down

0 comments on commit 33f97dc

Please sign in to comment.