Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created November 18, 2013 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Raynos/7521773 to your computer and use it in GitHub Desktop.
Save Raynos/7521773 to your computer and use it in GitHub Desktop.
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