Last active
March 22, 2021 15:41
-
-
Save Asjas/bf1f1095f7dbefd8fa70a8b0e4182cc1 to your computer and use it in GitHub Desktop.
Node Backpressure Aware Copy
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 backpressureAwareCopy(srcStream, destStream) { | |
srcStream.on('data', (chunk) => { | |
const canContinue = destStream.write(chunk); | |
if (!canContinue) { | |
// if we are overflowing the destination, we stop reading | |
srcStream.pause(); | |
// once all the buffered data is flushed, we resume reading from source | |
destStream.once('drain', () => srcStream.resume()); | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment