Closed rogierschouten closed 7 years ago
Thanks for that snippet!
Two helpers have been added for the ts-stream -> Node.JS stream case, one if you really need an instance of a Node.JS Readable (e.g. for passing to another lib), and one if you have a Node.JS WritableStream (e.g. fs.createWriteStream()), and just want to pipe a ts-stream into it.
Unit tests are still missing, but I've done some quick experiments using a file and a socket. Not published on NPM yet.
The other way around is still on the TODO.
Implementing a FileSink was now trivial:
import { Stream, pipeToNodeStream } from "ts-stream";
import * as fs from "fs";
export class FileSink extends Stream<string> {
constructor(path: string, options?: {
flags?: string;
encoding?: string;
string?: string;
}) {
super();
pipeToNodeStream(this, fs.createWriteStream(path, options));
}
}
Example usage:
let source = Stream.from(["abc", "def"]);
source.pipe(new FileSink("test.txt"));
To wait for the chain's result:
let sink = source.pipe(new FileSink("test.txt"));
sink.result().then(
() => console.log("ok"),
(err) => console.log("error", err)
);
(FileSink is now added to the library, no need to copy/paste :))
The other way around is still on the TODO.
I understand that's still the case? If you have a Node stream, you cannot easily get a ts-stream from it?
(there is also this in the README under todo: "Wrappers for Node streams (some already done), iterators, etc.")
Yes, this is still the case. See the example in #46 for an easy way to convert one though.
It would be nice to have Node.JS stream wrappers both for reading and writing. I've sent you an email with an example file sink which does that for writing to file streams specifically (which are slightly different from ordinary node streams).