Fix a few thing with compose

This commit is contained in:
Lewis Diamond
2019-12-06 16:38:52 -05:00
parent ff2b652ddf
commit 4b806c4d4e
7 changed files with 1351 additions and 1826 deletions

View File

@@ -1,16 +1,17 @@
import { pipeline, Duplex, DuplexOptions } from "stream";
import { pipeline, TransformOptions, Transform } from "stream";
export function compose(
streams: Array<
NodeJS.ReadableStream | NodeJS.ReadWriteStream | NodeJS.WritableStream
>,
options?: DuplexOptions,
errorCallback?: (err: any) => void,
options?: TransformOptions,
): Compose {
if (streams.length < 2) {
throw new Error("At least two streams are required to compose");
}
return new Compose(streams, options);
return new Compose(streams, errorCallback, options);
}
enum EventSubscription {
@@ -20,46 +21,72 @@ enum EventSubscription {
Self,
}
const eventsTarget = {
close: EventSubscription.Last,
data: EventSubscription.Last,
drain: EventSubscription.Self,
end: EventSubscription.Last,
error: EventSubscription.Self,
finish: EventSubscription.Last,
pause: EventSubscription.Last,
pipe: EventSubscription.First,
readable: EventSubscription.Last,
resume: EventSubscription.Last,
unpipe: EventSubscription.First,
};
type AllStreams =
| NodeJS.ReadableStream
| NodeJS.ReadWriteStream
| NodeJS.WritableStream;
export class Compose extends Duplex {
function isReadable(stream: AllStreams): stream is NodeJS.WritableStream {
return (
(stream as NodeJS.ReadableStream).pipe !== undefined &&
(stream as any).readable === true
);
}
export class Compose extends Transform {
private first: AllStreams;
private last: AllStreams;
private streams: AllStreams[];
private inputStream: ReadableStream;
constructor(streams: AllStreams[], options?: DuplexOptions) {
constructor(
streams: AllStreams[],
errorCallback?: (err: any) => void,
options?: TransformOptions,
) {
super(options);
this.first = streams[0];
this.last = streams[streams.length - 1];
this.streams = streams;
pipeline(streams, (err: any) => {
this.emit("error", err);
pipeline(
streams,
errorCallback ||
((error: any) => {
if (error) {
this.emit("error", error);
}
}),
);
if (isReadable(this.last)) {
(this.last as NodeJS.ReadWriteStream).pipe(
new Transform({
...options,
transform: (d: any, encoding, cb) => {
this.push(d);
cb();
},
}),
);
}
}
public _transform(chunk: any, encoding: string, cb: any) {
(this.first as NodeJS.WritableStream).write(chunk, encoding, cb);
}
public _flush(cb: any) {
if (isReadable(this.first)) {
(this.first as any).push(null);
}
this.last.once("end", () => {
cb();
});
}
public pipe<T extends NodeJS.WritableStream>(dest: T) {
return (this.last as NodeJS.ReadableStream).pipe(dest);
}
public _write(chunk: any, encoding: string, cb: any) {
(this.first as NodeJS.WritableStream).write(chunk, encoding, cb);
public _destroy(error: any, cb: (error?: any) => void) {
this.streams.forEach(s => (s as any).destroy());
cb(error);
}
public bubble(...events: string[]) {
@@ -69,38 +96,4 @@ export class Compose extends Duplex {
});
});
}
public on(event: string, cb: any) {
switch (eventsTarget[event]) {
case EventSubscription.First:
this.first.on(event, cb);
break;
case EventSubscription.Last:
this.last.on(event, cb);
break;
case EventSubscription.All:
this.streams.forEach(s => s.on(event, cb));
break;
default:
super.on(event, cb);
}
return this;
}
public once(event: string, cb: any) {
switch (eventsTarget[event]) {
case EventSubscription.First:
this.first.once(event, cb);
break;
case EventSubscription.Last:
this.last.once(event, cb);
break;
case EventSubscription.All:
this.streams.forEach(s => s.once(event, cb));
break;
default:
super.once(event, cb);
}
return this;
}
}

View File

@@ -121,6 +121,10 @@ export default function mhysa(defaultOptions?: TransformOptions) {
/**
* Return a ReadWrite stream that parses the streamed chunks as JSON. Each streamed chunk
* must be a fully defined JSON string in utf8.
* @param format: @type SerializationFormats defaults SerializationFormats.utf8
* @param emitError: @type boolean Whether or not to emit an error when
* failing to parse. An error will automatically close the stream.
* Defaults to true.
*/
parse,
@@ -245,9 +249,10 @@ export default function mhysa(defaultOptions?: TransformOptions) {
/**
* Composes multiple streams together. Writing occurs on first stream, piping occurs from last stream.
* @param streams Array of streams to compose. Minimum of two.
* @param errorCallback a function that handles any error coming out of the pipeline
* @param options Transform stream options
*/
compose: withDefaultOptions(1, compose),
compose: withDefaultOptions(2, compose),
/**
* Composes multiple streams together. Writing occurs on first stream, piping occurs from last stream.

View File

@@ -4,6 +4,7 @@ import { SerializationFormats } from "./baseDefinitions";
export function parse(
format: SerializationFormats = SerializationFormats.utf8,
emitError: boolean = true,
): Transform {
const decoder = new StringDecoder(format);
return new Transform({
@@ -13,9 +14,13 @@ export function parse(
try {
const asString = decoder.write(chunk);
// Using await causes parsing errors to be emitted
callback(undefined, await JSON.parse(asString));
callback(null, await JSON.parse(asString));
} catch (err) {
callback(err);
if (emitError) {
callback(err);
} else {
callback();
}
}
},
});