Skip to content

Instantly share code, notes, and snippets.

@Asjas
Last active March 22, 2021 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Asjas/bf1f1095f7dbefd8fa70a8b0e4182cc1 to your computer and use it in GitHub Desktop.
Save Asjas/bf1f1095f7dbefd8fa70a8b0e4182cc1 to your computer and use it in GitHub Desktop.
Node Backpressure Aware Copy
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