Comparing version 0.3.0 to 0.4.0
@@ -15,2 +15,3 @@ | ||
stream.Writable.call(this, options); | ||
var self = this; | ||
@@ -22,7 +23,16 @@ this._objectMode = !!options.objectMode; | ||
// Either finish or error will be used to declar a done state | ||
// Cleanup event listeners | ||
var sources = []; | ||
function cleanup() { | ||
self.removeListener('pipe', onpipe); | ||
self.removeListener('error', error); | ||
self.removeListener('finish', finish); | ||
for (var i = 0, l = sources.length; i < l; i++) { | ||
sources[i].removeListener('error', error); | ||
} | ||
} | ||
// Either finish or error will be used to declare a done state | ||
function finish() { | ||
this.removeListener('error', error); | ||
this.emit('close'); | ||
cleanup(); | ||
callback(null, this.buffer); | ||
@@ -32,10 +42,14 @@ } | ||
function error(err) { | ||
this.removeListener('finish', finish); | ||
this.emit('close'); | ||
cleanup(); | ||
callback(err, this.buffer); | ||
} | ||
function onpipe(source) { | ||
sources.push(source); | ||
source.once('error', error); | ||
} | ||
this.once('finish', finish); | ||
this.once('error', error); | ||
this.on('pipe', onpipe); | ||
} | ||
@@ -42,0 +56,0 @@ module.exports = Endpoint; |
{ | ||
"name": "endpoint", | ||
"description": "Converts a stream intro a buffer or array of objects", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"author": "Andreas Madsen <amwebdk@gmail.com>", | ||
@@ -6,0 +6,0 @@ "main": "./endpoint.js", |
@@ -14,3 +14,3 @@ #endpoint | ||
`endpoint` is a `WriteStream` there converts a stream to a buffer or an array | ||
of object, it also collectes stream errors. | ||
of object, it also collectes stream errors from the source. | ||
@@ -17,0 +17,0 @@ **Collect a buffer stream intro a single buffer:** |
16
test.js
var test = require('tap').test; | ||
var endpoint = require('./endpoint.js'); | ||
var PassThrough = require('stream').PassThrough; | ||
@@ -41,5 +42,18 @@ test('simple write and end', function (t) { | ||
point.emit('error', fakeError); | ||
point.end(); | ||
point.end(); // should not affect callback | ||
}); | ||
test('collect error from source', function (t) { | ||
var fakeError = new Error('error'); | ||
var source = new PassThrough(); | ||
var point = endpoint(function (err, buffer) { | ||
t.equal(err, fakeError); | ||
t.end(); | ||
}); | ||
source.pipe(point); | ||
source.emit('error', fakeError); | ||
}); | ||
test('simple write and end', function (t) { | ||
@@ -46,0 +60,0 @@ var point = endpoint({objectMode: true}, function (err, data) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6980
115