Created
September 18, 2022 19:32
-
-
Save hubgit/a4df70825687251218d3bb75e5cecfcb to your computer and use it in GitHub Desktop.
LineReader TransformStream
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
lineReader = () => { | |
let buffer = ""; | |
return new TransformStream({ | |
transform(chunk, controller) { | |
buffer += chunk; | |
const parts = buffer.split("\n"); | |
parts.slice(0, -1).forEach((part) => controller.enqueue(part)); | |
buffer = parts[parts.length - 1]; | |
}, | |
flush(controller) { | |
if (buffer) controller.enqueue(buffer); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment