I wrote a module read-next-line, which provide functionality to iterate over each line of text read via a Web API ReadableStream.
The following code snippets shows how that is done, which streams from an online text file which is streamed using fetch:
// import {ReadNextLine} from 'read-next-line';
async function run() {
const {ReadNextLine} = await import('https://cdn.jsdelivr.net/npm/[email protected]/+esm');
const response = await fetch('https://raw.githubusercontent.com/Borewit/read-next-line/refs/heads/master/test/samples/lorem-ipsem.utf-8.txt');
if(!response.ok) {
console.error(`HTTP-response-status=${response.status}`);
return;
}
console.log('Parsing text stream...');
const reader = new ReadNextLine(response.body);
let line;
let lineNr = 0;
while (lineNr < 10 && (line = await reader.readLine()) !== null) {
console.log(`Line #${++lineNr}: ${line}`); // Process each line as needed
}
}
run().catch(e => console.error(e));