This commit is contained in:
Jerry Kurian
2019-08-22 12:07:30 -04:00
parent 1e7fad2403
commit d097fa6aa5
5 changed files with 93 additions and 43 deletions

View File

@@ -19,4 +19,4 @@ export { replace } from "./replace";
export { split } from "./split";
export { stringify } from "./stringify";
export { unbatch } from "./unbatch";
export { compose } from "./compose";
export { compose, composeDuplex } from "./compose";

View File

@@ -1,45 +1,54 @@
import { Transform, Writable, Pipe, WritableOptions } from "stream";
class Compose extends Writable implements Pipe {
private head: Writable | Transform;
private tail: Writable | Transform;
constructor(
streams: Array<Transform | Writable>,
options?: WritableOptions,
) {
super(options);
if (streams.length < 2) {
throw new Error("Cannot compose 1 or less streams");
}
this.head = streams[0];
for (let i = 1; i < streams.length; i++) {
streams[i - 1].pipe(streams[i]);
}
this.tail = streams[streams.length - 1];
}
public pipe<T extends NodeJS.WritableStream>(
destination: T,
options: { end?: boolean } | undefined,
) {
return this.tail.pipe(
destination,
options,
);
}
public _write(chunk: any, enc: string, cb: any) {
this.head.write(chunk.toString ? chunk.toString() : chunk, cb);
}
}
import {
pipeline,
Transform,
Writable,
Pipe,
WritableOptions,
Readable,
Duplex,
} from "stream";
/**
* Return a Readable stream of readable streams concatenated together
* @param streams Readable streams to concatenate
*/
// First Readable --> Readable
// First Transform | Duplex, Last Writable --> Writable
//
export function compose(
streams: Array<Transform | Writable>,
streams: Array<Readable | Duplex | Transform | Writable>,
options?: WritableOptions,
): Compose {
return new Compose(streams, options);
): Duplex {
// Maybe just return a new stream here
if (streams.length < 2) {
throw new Error("Not enough");
}
const duplex = new Duplex({
objectMode: true,
write(chunk, enc, cb) {
const first = streams[0] as Writable;
if (!first.write(chunk)) {
first.on("drain", cb);
} else {
cb();
}
},
read(size) {
let chunk;
while (
null !==
(chunk = (streams[streams.length - 1] as Readable).read())
) {
this.push(chunk);
}
},
});
pipeline(streams, (err: any) => {
duplex.emit("error", err);
});
return duplex;
}

View File

@@ -297,3 +297,9 @@ export function compose(
options,
);
}
export function composeDuplex(
streams: Array<Writable | Transform>,
options?: WritableOptions,
) {
return baseFunctions.composeDuplex(streams, options);
}

View File

@@ -22,4 +22,5 @@ export {
accumulator,
accumulatorBy,
compose,
composeDuplex,
} from "./functions";