Comparing version 1.1.20 to 1.1.21
110
lib/pipe.js
@@ -17,5 +17,3 @@ (function () { | ||
}; | ||
} | ||
if (typeof options.end === 'undefined') | ||
} else if (typeof options.end === 'undefined') | ||
options.end = true; | ||
@@ -30,8 +28,29 @@ | ||
function pipe(streams, options, callback) { | ||
var error, | ||
isEnd = typeof options.end === 'undefined', | ||
var error, finish, end, | ||
readError, writeError, | ||
last = streams.length - 1, | ||
read = streams[0], | ||
write = streams[last], | ||
last = streams.length - 1, | ||
read = streams[0], | ||
write = streams[last]; | ||
onWriteError = function(error) { | ||
writeError = true; | ||
onError(error); | ||
onResult(); | ||
}, | ||
onReadError = function(error) { | ||
readError = true; | ||
onError(error); | ||
onResult(); | ||
}, | ||
onReadEnd = function() { | ||
end = true; | ||
onResult(); | ||
}, | ||
onWriteFinish = function() { | ||
finish = true; | ||
onResult(); | ||
}; | ||
@@ -42,9 +61,12 @@ callWhenOpen(write, function() { | ||
read.on('error', onResultError); | ||
write.on('error', onResultError); | ||
if (options.end) { | ||
end = true; | ||
write.once('finish', onWriteFinish); | ||
} else { | ||
finish = true; | ||
read.once('end', onReadEnd); | ||
} | ||
if (options.end || isEnd) | ||
write.on('finish', onEnd); | ||
else | ||
read.on('end', onEnd); | ||
read.once('error', onReadError); | ||
write.once('error', onWriteError); | ||
@@ -55,5 +77,33 @@ function onError(e) { | ||
function onResultError(e) { | ||
error = e; | ||
onEnd(); | ||
function onResult() { | ||
var is; | ||
if (readError && finish) { | ||
is = true; | ||
write.removeListener('error', onWriteError); | ||
read.removeListener('end', onReadEnd); | ||
} else if (writeError && readError) { | ||
is = true; | ||
read.removeListener('end', onReadEnd); | ||
write.removeListener('finish', onWriteFinish); | ||
} else if (writeError && end) { | ||
is = true; | ||
write.removeListener('finish', onWriteFinish); | ||
read.removeListener('error', onReadError); | ||
} else if (end && finish) { | ||
is = true; | ||
read.removeListener('error', onReadError); | ||
write.removeListener('error', onWriteError); | ||
} else if (end && readError) { | ||
is = true; | ||
write.removeListener('error', onWriteError); | ||
write.removeListener('finish', onWriteFinish); | ||
} else if (finish && writeError) { | ||
is = true; | ||
read.removeListener('error', onReadError); | ||
read.removeListener('end', onReadEnd); | ||
} | ||
if (is) | ||
onEnd(); | ||
} | ||
@@ -64,8 +114,2 @@ | ||
read.removeListener('error', onResultError); | ||
read.removeListener('end', onEnd); | ||
write.removeListener('error', onResultError); | ||
write.removeListener('finish', onEnd); | ||
callback(error); | ||
@@ -80,10 +124,18 @@ } | ||
function callWhenOpen(stream, fn) { | ||
var isFsWrite = stream instanceof fs.WriteStream; | ||
var isFsWrite = stream instanceof fs.WriteStream, | ||
onOpen = function() { | ||
stream.removeListener('error', onError); | ||
fn(); | ||
}, | ||
onError = function() { | ||
stream.removeListener('open', fn); | ||
}; | ||
if (!isFsWrite) | ||
if (!isFsWrite) { | ||
fn(); | ||
else | ||
stream.on('open', function() { | ||
fn(); | ||
}); | ||
} else { | ||
stream.once('open', onOpen); | ||
stream.once('error', onError); | ||
} | ||
} | ||
@@ -90,0 +142,0 @@ |
{ | ||
"name": "pipe-io", | ||
"version": "1.1.20", | ||
"version": "1.1.21", | ||
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)", | ||
@@ -5,0 +5,0 @@ "description": "Pipe streams and handle events", |
Sorry, the diff of this file is not supported yet
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
9591
158