NodeJS streams as well as web streams are both async iterable and both yields uint8arrays. and both env have TextDecoderStream in the global namespace
so there is no need to use eventEmitter and writing different code for both.
so this
let stream = fs.createReadStream(filePath);
let parser = null;
let result = null;
stream.on('data', (chunk) => {
// convert from Buffer
let strChunk = chunk.toString();
// on first chunk, infer schema and init parser
parser ??= initParser(inferSchema(strChunk));
// incremental parse to string arrays
parser.chunk(strChunk, parser.stringArrs);
});
stream.on('end', () => {
result = p.end();
});
and this:
let stream = fs.createReadStream(filePath);
let webStream = Stream.Readable.toWeb(stream);
let textStream = webStream.pipeThrough(new TextDecoderStream());
let parser = null;
for await (const strChunk of textStream) {
parser ??= initParser(inferSchema(strChunk));
parser.chunk(strChunk, parser.stringArrs);
}
let result = parser.end();
could be written as:
for await (const chunk of stream) {
parser ??= initParser(inferSchema(strChunk.toString()));
parser.chunk(strChunk, parser.stringArrs);
});
for await (const strChunk of textStream) {
parser ??= initParser(inferSchema(strChunk));
parser.chunk(strChunk, parser.stringArrs);
}
NodeJS streams as well as web streams are both async iterable and both yields uint8arrays. and both env have
TextDecoderStreamin the global namespaceso there is no need to use eventEmitter and writing different code for both.
so this
and this:
could be written as: