Skip to content

Instantly share code, notes, and snippets.

@hubgit
Created September 18, 2022 19:32
Show Gist options
  • Save hubgit/a4df70825687251218d3bb75e5cecfcb to your computer and use it in GitHub Desktop.
Save hubgit/a4df70825687251218d3bb75e5cecfcb to your computer and use it in GitHub Desktop.
LineReader TransformStream
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