import {Readable} from 'stream';
class TimestampSource {
#interval
#counter = 0
start(controller) {
this.#interval = setInterval(() => {
const string = `${this.#counter}`
controller.enqueue(string);
this.#counter++;
console.log(`Enqueued ${string}`);
}, 1_000);
setTimeout(() => {
clearInterval(this.#interval);
controller.close();
}, 10_000);
}
cancel() {
clearInterval(this.#interval);
}
}
async function concatStringStream(stream) {
let result = '';
const reader = stream.getReader();
while (true) {
const {done, value} = await reader.read();
if (done) return result;
result += value;
console.log(`Read ${result.length} characters so far`);
console.log(`Most recently read chunk: ${value}`);
}
}
async function test1Impl() {
const stream = new ReadableStream(new TimestampSource());
concatStringStream(stream).then((result) => console.log('Stream complete', result));
}
export const test1 = test1Impl;
export async function testNodeStream1() {
async function readableToString2(readable) {
let result = '';
for await (const chunk of readable) {
result += chunk;
}
return result;
}
const readable = Readable.from('Good morning!', {encoding: 'utf8'});
return await readableToString2(readable);
}