This commit is contained in:
Jerry Kurian
2019-08-15 11:54:50 -04:00
parent 3a1fbf44d7
commit a40b1bf38c
38 changed files with 1981 additions and 2616 deletions

View File

@@ -0,0 +1,26 @@
import { Transform } from "stream";
import { ThroughOptions } from "../definitions";
/**
* Return a ReadWrite stream that collects streamed chunks into an array or buffer
* @param options
* @param options.objectMode Whether this stream should behave as a stream of objects
*/
export function collect(
options: ThroughOptions = { objectMode: false },
): NodeJS.ReadWriteStream {
const collected: any[] = [];
return new Transform({
readableObjectMode: options.objectMode,
writableObjectMode: options.objectMode,
transform(data, encoding, callback) {
collected.push(data);
callback();
},
flush(callback) {
this.push(
options.objectMode ? collected : Buffer.concat(collected),
);
callback();
},
});
}