fd-slicer
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -0,1 +1,8 @@ | ||
### 0.3.0 | ||
* write stream emits 'progress' events | ||
* write stream supports 'end' option which causes the stream to emit an error | ||
if a maximum size is exceeded | ||
* improve documentation | ||
### 0.2.1 | ||
@@ -2,0 +9,0 @@ |
@@ -134,2 +134,3 @@ var fs = require('fs'); | ||
this.start = options.start || 0; | ||
this.endOffset = options.end || Infinity; | ||
this.bytesWritten = 0; | ||
@@ -148,2 +149,8 @@ this.pos = this.start; | ||
if (self.bytesWritten + buffer.length > self.endOffset) { | ||
var err = new Error("maximum file length exceeded"); | ||
err.code = 'ETOOBIG'; | ||
callback(err); | ||
return; | ||
} | ||
self.context.pend.go(function(cb) { | ||
@@ -159,2 +166,3 @@ if (self.destroyed) return cb(); | ||
self.pos += bytes; | ||
self.emit('progress'); | ||
cb(); | ||
@@ -161,0 +169,0 @@ callback(); |
{ | ||
"name": "fd-slicer", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "safely create multiple ReadStream or WriteStream objects from the same file descriptor", | ||
@@ -12,3 +12,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"mocha": "~1.21.3", | ||
"mocha": "~1.21.5", | ||
"stream-equal": "~0.1.5" | ||
@@ -15,0 +15,0 @@ }, |
@@ -34,2 +34,8 @@ # fd-slicer | ||
This module also gives you some additional power that the builtin | ||
`fs.createWriteStream` do not give you. These features are: | ||
* Emitting a 'progress' event on write. | ||
* Ability to set a maximum size and emit an error if this size is exceeded. | ||
## Usage | ||
@@ -87,16 +93,44 @@ | ||
Creates a read stream based on the file descriptor. Passes `options` to | ||
the `Readable` stream constructor. Accepts `start` and `end` options just | ||
like `fs.createReadStream`. | ||
Available `options`: | ||
The stream that this returns supports `destroy()` to cancel it. | ||
* `start` - Number. The offset into the file to start reading from. Defaults | ||
to 0. | ||
* `end` - Number. Exclusive upper bound offset into the file to stop reading | ||
from. | ||
* `highWaterMark` - Number. The maximum number of bytes to store in the | ||
internal buffer before ceasing to read from the underlying resource. | ||
Defaults to 16 KB. | ||
* `encoding` - String. If specified, then buffers will be decoded to strings | ||
using the specified encoding. Defaults to `null`. | ||
The ReadableStream that this returns has these additional methods: | ||
* `destroy()` - stop streaming | ||
##### createWriteStream(options) | ||
Creates a write stream based on the file descriptor. Passes `options` to | ||
the `Writable` stream constructor. Accepts the `start` option just | ||
like `fs.createWriteStream`. | ||
Available `options`: | ||
The stream that this returns supports `destroy()` to cancel it. | ||
* `start` - Number. The offset into the file to start writing to. Defaults to | ||
0. | ||
* `end` - Number. Exclusive upper bound offset into the file. If this offset | ||
is reached, the write stream will emit an 'error' event and stop functioning. | ||
In this situation, `err.code === 'ETOOBIG'`. Defaults to `Infinity`. | ||
* `highWaterMark` - Number. Buffer level when `write()` starts returning | ||
false. Defaults to 16KB. | ||
* `decodeStrings` - Boolean. Whether or not to decode strings into Buffers | ||
before passing them to` _write()`. Defaults to `true`. | ||
The WritableStream that this returns has these additional methods: | ||
* `destroy()` - stop streaming | ||
And these additional properties: | ||
* `bytesWritten` - number of bytes written to the stream | ||
And these additional events: | ||
* 'progress' - emitted when `bytesWritten` changes. | ||
##### read(buffer, offset, length, position, callback) | ||
@@ -119,1 +153,11 @@ | ||
Decrease the `autoClose` reference count by 1. | ||
#### Events | ||
##### 'error' | ||
Emitted if `fs.close` returns an error when auto closing. | ||
##### 'close' | ||
Emitted when fd-slicer closes the file descriptor due to `autoClose`. |
@@ -170,2 +170,48 @@ var FdSlicer = require('../'); | ||
}); | ||
it("write stream emits error when max size exceeded", function(done) { | ||
fs.open(testOutBlobFile, 'w', function(err, fd) { | ||
if (err) return done(err); | ||
var fdSlicer = new FdSlicer(fd); | ||
var ws = fdSlicer.createWriteStream({start: 0, end: 1000}); | ||
ws.on('error', function(err) { | ||
assert.strictEqual(err.code, 'ETOOBIG'); | ||
done(); | ||
}); | ||
ws.end(new Buffer(1001)); | ||
}); | ||
}); | ||
it("write stream does not emit error when max size not exceeded", function(done) { | ||
fs.open(testOutBlobFile, 'w', function(err, fd) { | ||
if (err) return done(err); | ||
var fdSlicer = new FdSlicer(fd, {autoClose: true}); | ||
var ws = fdSlicer.createWriteStream({end: 1000}); | ||
fdSlicer.on('close', done); | ||
ws.end(new Buffer(1000)); | ||
}); | ||
}); | ||
it("write stream emits progress events", function(done) { | ||
fs.open(testOutBlobFile, 'w', function(err, fd) { | ||
if (err) return done(err); | ||
var fdSlicer = new FdSlicer(fd, {autoClose: true}); | ||
var ws = fdSlicer.createWriteStream(); | ||
var progressEventCount = 0; | ||
var prevBytesWritten = 0; | ||
ws.on('progress', function() { | ||
progressEventCount += 1; | ||
assert.ok(ws.bytesWritten > prevBytesWritten); | ||
prevBytesWritten = ws.bytesWritten; | ||
}); | ||
fdSlicer.on('close', function() { | ||
assert.ok(progressEventCount > 5); | ||
done(); | ||
}); | ||
for (var i = 0; i < 10; i += 1) { | ||
ws.write(new Buffer(16 * 1024 * 2)); | ||
} | ||
ws.end(); | ||
}); | ||
}); | ||
}); |
19106
354
161