Created
November 18, 2013 03:03
-
-
Save Raynos/7521773 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
function pipe(source, dest, options) { | |
if (!options) { | |
options = {} | |
} | |
var close = "close" in options ? Boolean(options.close) : true | |
pump() | |
return dest | |
function pump() { | |
if (dest.writableState === "writable") { | |
moveChunk() | |
} else if (dest.writableState === "waiting") { | |
dest.waitForWritable().then(pump) | |
} else if (dest.writableState === "errored") { | |
source.abort() | |
} else if (dest.writableState === "closed") { | |
// NO OP. Terminate pipe(). Do not close source | |
} | |
} | |
function moveChunk() { | |
if (source.readableState === "readable") { | |
var chunk = source.read() | |
dest.write(chunk) | |
pump() | |
} else if (source.readableState === "waiting") { | |
source.waitForReadable().then(pump) | |
} else if (source.readableState === "finished") { | |
if (close) { | |
dest.close() | |
} | |
} else if (source.readableState === "errored") { | |
dest.dispose() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment