Add demux

This commit is contained in:
Jerry Kurian
2019-08-28 17:01:51 -04:00
parent c7903376e9
commit 9b09a3f949
6 changed files with 120 additions and 1 deletions

View File

@@ -20,3 +20,4 @@ export { split } from "./split";
export { stringify } from "./stringify";
export { unbatch } from "./unbatch";
export { compose } from "./compose";
export { demux } from "./demux";

49
src/functions/demux.ts Normal file
View File

@@ -0,0 +1,49 @@
import { WritableOptions, Writable } from "stream";
/**
* Return a Duplex stream that is pushed data from multiple sources
* @param streams Source streams to multiplex
* @param options Duplex stream options
*/
export function demux(
construct: () => NodeJS.WritableStream | NodeJS.ReadWriteStream,
demuxBy: { key?: string; keyBy?: (chunk: any) => string },
options?: WritableOptions,
): Writable {
return new Demux(construct, demuxBy, options);
}
class Demux extends Writable {
private keyMap: object;
private demuxer: (chunk: any) => string;
private construct: (
destKey?: string,
) => NodeJS.WritableStream | NodeJS.ReadWriteStream;
constructor(
construct: (
destKey?: string,
) => NodeJS.WritableStream | NodeJS.ReadWriteStream,
demuxBy: { key?: string; keyBy?: (chunk: any) => string },
options?: WritableOptions,
) {
super(options);
if (demuxBy.keyBy === undefined && demuxBy.key === undefined) {
throw new Error("Need one");
}
this.demuxer = demuxBy.keyBy || ((chunk: any) => chunk[demuxBy.key!]);
this.construct = construct;
this.keyMap = {};
}
public write(chunk: any, encoding?: any, cb?: any): boolean {
const destKey = this.demuxer(chunk);
if (this.keyMap[destKey] === undefined) {
this.keyMap[destKey] = this.construct(destKey);
}
const writeRes = this.keyMap[destKey].write(chunk);
if (cb !== undefined) {
cb();
}
return writeRes;
}
}

View File

@@ -297,3 +297,13 @@ export function compose(
options,
);
}
export function demux(
construct: (
destKey?: string,
) => NodeJS.WritableStream | NodeJS.ReadWriteStream,
demuxer: { key?: string; keyBy?: (chunk: any) => string },
options?: DuplexOptions,
) {
return baseFunctions.demux(construct, demuxer, options);
}

View File

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