Skip to content

Instantly share code, notes, and snippets.

@ValentaTomas
Created July 11, 2024 06:33
Show Gist options
  • Save ValentaTomas/25caeaca95e11a5eb89603b9939ac063 to your computer and use it in GitHub Desktop.
Save ValentaTomas/25caeaca95e11a5eb89603b9939ac063 to your computer and use it in GitHub Desktop.
Transform ReadableStream to async iterator of lines
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