poelstra / ts-stream

Type-safe object streams with seamless support for backpressure, ending, and error handling
MIT License
65 stars 13 forks source link

Read from file? #46

Closed AleFons closed 4 years ago

AleFons commented 4 years ago

Is it possible to read from a file? I can render the file as a text or binary format, if necessary.

poelstra commented 4 years ago

Sure! There's no built-in FileSource though, but you can take a look at the implementation of FileSink for inspiration, or with recent NodeJS just do something along the lines of:

try {
    for (const buffer of myFile) {
        await myStream.write(buffer);
    }
    await mystream.end();
} catch (err) {
   myStream.abort(err);
}

Note that this lib is mostly meant for handling object streams, so it will e.g. not try to concatenate buffers.