Created
July 11, 2024 06:33
-
-
Save ValentaTomas/25caeaca95e11a5eb89603b9939ac063 to your computer and use it in GitHub Desktop.
Transform ReadableStream to async iterator of lines
This file contains hidden or 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
async function* readLines(stream: ReadableStream<Uint8Array>) { | |
const reader = stream.getReader(); | |
let buffer = '' | |
try { | |
while (true) { | |
const { done, value } = await reader.read(); | |
if (value !== undefined) { | |
buffer += new TextDecoder().decode(value) | |
} | |
if (done) { | |
if (buffer.length > 0) { | |
yield buffer | |
} | |
break | |
} | |
let newlineIdx = -1 | |
do { | |
newlineIdx = buffer.indexOf('\n') | |
if (newlineIdx !== -1) { | |
yield buffer.slice(0, newlineIdx) | |
buffer = buffer.slice(newlineIdx + 1) | |
} | |
} while (newlineIdx !== -1) | |
} | |
} finally { | |
reader.releaseLock() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment