-
-
Save Raynos/c9d21023a2e62ebdaf54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stream.pipe = function (target, opts) { | |
if (!(opts && opts.end === false)) { | |
reemit(stream, target, ["end"]) | |
} | |
targets.push(target) | |
target.emit("pipe", stream) | |
if (flowing === false) { | |
flow() | |
} | |
return target | |
function flow() { | |
var chunk = stream.read() | |
, terminate = false | |
, count = 0 | |
while (chunk) { | |
targets.forEach(writeToTarget) | |
if (terminate) { | |
return | |
} | |
chunk = stream.read() | |
} | |
stream.once("readable", flow) | |
function writeToTarget(target) { | |
var written = target.write(chunk) | |
if (!written) { | |
count++ | |
terminate = true | |
target.once("drain", next) | |
} | |
} | |
function next() { | |
if (--count === 0) { | |
flow() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment