Comparing version 0.1.3 to 0.1.4
59
index.js
var once = require('once'); | ||
var noop = function() {}; | ||
var patch = function(stream, onend) { // patch 0.8 stream since they dont emit finish | ||
var patch = function(stream, callback) { // patch 0.8 stream since they dont emit finish | ||
var end = stream.end; | ||
stream.end = function() { | ||
onend(); | ||
callback(); | ||
end.apply(this, arguments); | ||
@@ -12,34 +12,36 @@ }; | ||
var destroyer = function(stream, callback) { | ||
var ended = false; | ||
var destroyer = function(stream, reading, writing, callback) { | ||
callback = once(callback); | ||
var destroyed = false; | ||
var closed = false; | ||
var destroyed = false; | ||
callback = once(callback); | ||
var onend = function() { | ||
ended = true; | ||
callback(); | ||
var onfinish = function() { | ||
writing = false; | ||
if (!reading) callback(); | ||
}; | ||
var onclose = function() { | ||
stream.on('error', callback); | ||
stream.on('finish', onfinish); | ||
stream.on('end', function() { | ||
reading = false; | ||
if (!writing) callback(); | ||
}); | ||
stream.on('close', function() { | ||
closed = true; | ||
if (ended || (stream._readableState && stream._readableState.ended)) return; | ||
if (!reading && !writing) return; | ||
if (reading && stream._readableState && stream._readableState.ended) return; | ||
callback(new Error('stream closed')); | ||
}; | ||
}); | ||
stream.on('error', callback); | ||
stream.on('close', onclose); | ||
stream.on('finish', onend); | ||
stream.on('end', onend); | ||
if (writing && stream.writable && !stream._writableState) patch(stream, onfinish); | ||
var destroy = function() { | ||
if (ended || closed || destroyed || !stream.destroy) return; | ||
return function() { | ||
if (closed || destroyed || (!reading && !writing) || !stream.destroy) return; | ||
destroyed = true; | ||
stream.destroy(); | ||
}; | ||
if (!stream._writableState && stream.writable) patch(stream, onend); | ||
return destroy; | ||
}; | ||
@@ -65,8 +67,13 @@ | ||
var error; | ||
var readables = Math.max(streams.length-1, 1); | ||
var destroys = streams.map(function(stream, i) { | ||
return destroyer(stream, function(err) { | ||
var reading = i < readables; | ||
var writing = i > 0; | ||
return destroyer(stream, reading, writing, function(err) { | ||
if (!error) error = err; | ||
if (err) destroys.forEach(call); | ||
if (i < streams.length-1) return; | ||
if (reading) return; | ||
destroys.forEach(call); | ||
callback(err); | ||
callback(error); | ||
}); | ||
@@ -73,0 +80,0 @@ }); |
{ | ||
"name": "pump", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"repository": "git://github.com/mafintosh/pump.git", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
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
4515
6
108