Compare commits
2 Commits
feature/de
...
pr/wenzil/
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e168e8b9da | ||
|
|
7c5fe5c9f7 |
14
package.json
14
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@jogogo/mhysa",
|
"name": "mhysa",
|
||||||
"version": "2.0.0-alpha.3",
|
"version": "2.0.0-alpha.1",
|
||||||
"description": "Streams and event emitter utils for Node.js",
|
"description": "Streams and event emitter utils for Node.js",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"promise",
|
"promise",
|
||||||
@@ -27,11 +27,8 @@
|
|||||||
"files": [
|
"files": [
|
||||||
"dist"
|
"dist"
|
||||||
],
|
],
|
||||||
"publishConfig": {
|
|
||||||
"registry": "https://npm.dev.jogogo.co/"
|
|
||||||
},
|
|
||||||
"repository": {
|
"repository": {
|
||||||
"url": "git@github.com:Jogogoplay/mhysa.git",
|
"url": "git@github.com:Wenzil/Mhysa.git",
|
||||||
"type": "git"
|
"type": "git"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -43,7 +40,7 @@
|
|||||||
"dependencies": {},
|
"dependencies": {},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/chai": "^4.1.7",
|
"@types/chai": "^4.1.7",
|
||||||
"@types/node": "^12.12.15",
|
"@types/node": "^12.7.2",
|
||||||
"@types/sinon": "^7.0.13",
|
"@types/sinon": "^7.0.13",
|
||||||
"ava": "^2.4.0",
|
"ava": "^2.4.0",
|
||||||
"chai": "^4.2.0",
|
"chai": "^4.2.0",
|
||||||
@@ -58,8 +55,7 @@
|
|||||||
},
|
},
|
||||||
"ava": {
|
"ava": {
|
||||||
"files": [
|
"files": [
|
||||||
"tests/*.spec.ts",
|
"tests/*.spec.ts"
|
||||||
"tests/utils/*.spec.ts"
|
|
||||||
],
|
],
|
||||||
"sources": [
|
"sources": [
|
||||||
"src/**/*.ts"
|
"src/**/*.ts"
|
||||||
|
|||||||
@@ -12,9 +12,7 @@ export function batch(
|
|||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
}
|
}
|
||||||
timer = null;
|
timer = null;
|
||||||
if (buffer.length > 0) {
|
|
||||||
self.push(buffer);
|
self.push(buffer);
|
||||||
}
|
|
||||||
buffer = [];
|
buffer = [];
|
||||||
};
|
};
|
||||||
return new Transform({
|
return new Transform({
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { AllStreams, isReadable } from "../helpers";
|
import { pipeline, TransformOptions, Transform } from "stream";
|
||||||
import { PassThrough, pipeline, TransformOptions, Transform } from "stream";
|
|
||||||
|
|
||||||
export function compose(
|
export function compose(
|
||||||
streams: Array<
|
streams: Array<
|
||||||
@@ -22,6 +21,18 @@ enum EventSubscription {
|
|||||||
Self,
|
Self,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AllStreams =
|
||||||
|
| NodeJS.ReadableStream
|
||||||
|
| NodeJS.ReadWriteStream
|
||||||
|
| NodeJS.WritableStream;
|
||||||
|
|
||||||
|
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 {
|
export class Compose extends Transform {
|
||||||
private first: AllStreams;
|
private first: AllStreams;
|
||||||
private last: AllStreams;
|
private last: AllStreams;
|
||||||
@@ -34,11 +45,11 @@ export class Compose extends Transform {
|
|||||||
options?: TransformOptions,
|
options?: TransformOptions,
|
||||||
) {
|
) {
|
||||||
super(options);
|
super(options);
|
||||||
this.first = new PassThrough(options);
|
this.first = streams[0];
|
||||||
this.last = streams[streams.length - 1];
|
this.last = streams[streams.length - 1];
|
||||||
this.streams = streams;
|
this.streams = streams;
|
||||||
pipeline(
|
pipeline(
|
||||||
[this.first, ...streams],
|
streams,
|
||||||
errorCallback ||
|
errorCallback ||
|
||||||
((error: any) => {
|
((error: any) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
|||||||
@@ -1,119 +1,99 @@
|
|||||||
import { DuplexOptions, Duplex, Transform } from "stream";
|
import { WritableOptions, Writable } from "stream";
|
||||||
|
|
||||||
import { isReadable } from "../helpers";
|
enum EventSubscription {
|
||||||
|
Last = 0,
|
||||||
|
First,
|
||||||
|
All,
|
||||||
|
Self,
|
||||||
|
Unhandled,
|
||||||
|
}
|
||||||
|
|
||||||
|
const eventsTarget = {
|
||||||
|
close: EventSubscription.Self,
|
||||||
|
data: EventSubscription.All,
|
||||||
|
drain: EventSubscription.Self,
|
||||||
|
end: EventSubscription.Self,
|
||||||
|
error: EventSubscription.Self,
|
||||||
|
finish: EventSubscription.Self,
|
||||||
|
pause: EventSubscription.Self,
|
||||||
|
pipe: EventSubscription.Self,
|
||||||
|
readable: EventSubscription.Self,
|
||||||
|
resume: EventSubscription.Self,
|
||||||
|
unpipe: EventSubscription.Self,
|
||||||
|
};
|
||||||
|
|
||||||
type DemuxStreams = NodeJS.WritableStream | NodeJS.ReadWriteStream;
|
type DemuxStreams = NodeJS.WritableStream | NodeJS.ReadWriteStream;
|
||||||
|
|
||||||
export interface DemuxOptions extends DuplexOptions {
|
|
||||||
remultiplex?: boolean;
|
|
||||||
purgeIdleInterval?: number;
|
|
||||||
maxIdleTime?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function demux(
|
export function demux(
|
||||||
construct: (destKey?: string, chunk?: any) => DemuxStreams,
|
construct: (destKey?: string) => DemuxStreams,
|
||||||
demuxBy: string | ((chunk: any) => string),
|
demuxBy: string | ((chunk: any) => string),
|
||||||
options?: DemuxOptions,
|
options?: WritableOptions,
|
||||||
): Duplex {
|
): Writable {
|
||||||
return new Demux(construct, demuxBy, options);
|
return new Demux(construct, demuxBy, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
class Demux extends Duplex {
|
// @TODO handle pipe event ie) Multiplex
|
||||||
|
class Demux extends Writable {
|
||||||
private streamsByKey: {
|
private streamsByKey: {
|
||||||
[key: string]: { stream: DemuxStreams; lastWrite: number };
|
[key: string]: DemuxStreams;
|
||||||
};
|
};
|
||||||
private demuxer: (chunk: any) => string;
|
private demuxer: (chunk: any) => string;
|
||||||
private construct: (destKey?: string, chunk?: any) => DemuxStreams;
|
private construct: (destKey?: string) => DemuxStreams;
|
||||||
private remultiplex: boolean;
|
|
||||||
private transform: Transform;
|
|
||||||
private maxIdleTime: number;
|
|
||||||
constructor(
|
constructor(
|
||||||
construct: (destKey?: string) => DemuxStreams,
|
construct: (destKey?: string) => DemuxStreams,
|
||||||
demuxBy: string | ((chunk: any) => string),
|
demuxBy: string | ((chunk: any) => string),
|
||||||
options: DemuxOptions = {},
|
options: WritableOptions = {},
|
||||||
) {
|
) {
|
||||||
super(options);
|
super(options);
|
||||||
this.demuxer =
|
this.demuxer =
|
||||||
typeof demuxBy === "string" ? chunk => chunk[demuxBy] : demuxBy;
|
typeof demuxBy === "string" ? chunk => chunk[demuxBy] : demuxBy;
|
||||||
this.construct = construct;
|
this.construct = construct;
|
||||||
this.remultiplex =
|
|
||||||
options.remultiplex === undefined ? true : options.remultiplex;
|
|
||||||
this.streamsByKey = {};
|
this.streamsByKey = {};
|
||||||
this.transform = new Transform({
|
|
||||||
...options,
|
|
||||||
transform: (d, _, cb) => {
|
|
||||||
this.push(d);
|
|
||||||
cb(null);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
this.maxIdleTime = options.maxIdleTime || 600000;
|
|
||||||
const purgeIdleInterval = options.purgeIdleInterval || 600000;
|
|
||||||
setInterval(() => {
|
|
||||||
this._destroyIdle();
|
|
||||||
}, purgeIdleInterval);
|
|
||||||
|
|
||||||
this.on("unpipe", () => this._flush());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _destroyIdle() {
|
|
||||||
for (let key in this.streamsByKey) {
|
|
||||||
const curTime = Date.now();
|
|
||||||
const pipeline = this.streamsByKey[key];
|
|
||||||
|
|
||||||
if (curTime - pipeline.lastWrite > this.maxIdleTime) {
|
|
||||||
delete this.streamsByKey[key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// tslint:disable-next-line
|
|
||||||
public _read(size: number) {}
|
|
||||||
|
|
||||||
public async _write(chunk: any, encoding: any, cb: any) {
|
public async _write(chunk: any, encoding: any, cb: any) {
|
||||||
const destKey = this.demuxer(chunk);
|
const destKey = this.demuxer(chunk);
|
||||||
if (this.streamsByKey[destKey] === undefined) {
|
if (this.streamsByKey[destKey] === undefined) {
|
||||||
const newPipeline = await this.construct(destKey, chunk);
|
this.streamsByKey[destKey] = await this.construct(destKey);
|
||||||
this.streamsByKey[destKey] = {
|
}
|
||||||
stream: newPipeline,
|
if (!this.streamsByKey[destKey].write(chunk, encoding)) {
|
||||||
lastWrite: Date.now(),
|
this.streamsByKey[destKey].once("drain", () => {
|
||||||
};
|
cb();
|
||||||
if (this.remultiplex && isReadable(newPipeline)) {
|
});
|
||||||
(newPipeline as NodeJS.ReadWriteStream).pipe(this.transform);
|
} else {
|
||||||
} else if (this.remultiplex) {
|
cb();
|
||||||
console.error(
|
}
|
||||||
`Pipeline construct for ${destKey} does not implement readable interface`,
|
}
|
||||||
|
|
||||||
|
public on(event: string, cb: any) {
|
||||||
|
switch (eventsTarget[event]) {
|
||||||
|
case EventSubscription.Self:
|
||||||
|
super.on(event, cb);
|
||||||
|
break;
|
||||||
|
case EventSubscription.All:
|
||||||
|
Object.keys(this.streamsByKey).forEach(key =>
|
||||||
|
this.streamsByKey[key].on(event, cb),
|
||||||
);
|
);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
super.on(event, cb);
|
||||||
}
|
}
|
||||||
} else {
|
return this;
|
||||||
this.streamsByKey[destKey].lastWrite = Date.now();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.streamsByKey[destKey].stream.write(chunk, encoding)) {
|
public once(event: string, cb: any) {
|
||||||
this.streamsByKey[destKey].stream.once("drain", () => {
|
switch (eventsTarget[event]) {
|
||||||
cb();
|
case EventSubscription.Self:
|
||||||
});
|
super.once(event, cb);
|
||||||
} else {
|
break;
|
||||||
cb();
|
case EventSubscription.All:
|
||||||
|
Object.keys(this.streamsByKey).forEach(key =>
|
||||||
|
this.streamsByKey[key].once(event, cb),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
super.once(event, cb);
|
||||||
}
|
}
|
||||||
}
|
return this;
|
||||||
|
|
||||||
public _flush() {
|
|
||||||
const pipelines = Object.values(this.streamsByKey);
|
|
||||||
let totalEnded = 0;
|
|
||||||
pipelines.forEach(({ stream }) => {
|
|
||||||
stream.once("end", () => {
|
|
||||||
totalEnded++;
|
|
||||||
if (pipelines.length === totalEnded) {
|
|
||||||
this.push(null);
|
|
||||||
this.emit("end");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
pipelines.forEach(({ stream }) => stream.end());
|
|
||||||
}
|
|
||||||
|
|
||||||
public _destroy(error: any, cb: (error?: any) => void) {
|
|
||||||
const pipelines = Object.values(this.streamsByKey);
|
|
||||||
pipelines.forEach(({ stream }) => (stream as any).destroy());
|
|
||||||
cb(error);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -256,14 +256,11 @@ export default function mhysa(defaultOptions?: TransformOptions) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Composes multiple streams together. Writing occurs on first stream, piping occurs from last stream.
|
* Composes multiple streams together. Writing occurs on first stream, piping occurs from last stream.
|
||||||
* @param {Function} construct Constructor for new output source. Should return a Writable or ReadWrite stream.
|
* @param construct Constructor for new output source. Should return a Writable or ReadWrite stream.
|
||||||
* @param {String | Function} demuxBy
|
* @param demuxBy
|
||||||
* @param {string} demuxBy.key? Key to fetch value from source chunks to demultiplex source.
|
* @param demuxBy.key? Key to fetch value from source chunks to demultiplex source.
|
||||||
* @param {Function} demuxBy.keyBy? Function to fetch value from source chunks to demultiplex source.
|
* @param demuxBy.keyBy? Function to fetch value from source chunks to demultiplex source.
|
||||||
* @param {Object} options Demux stream options
|
* @param options Writable stream options
|
||||||
* @param {boolean} options.remultiplex? If demux should be remultiplexed into a single destination
|
|
||||||
* @param {number} options.purgeIdleInterval? Interval at which a purge for idle pipelines will occur
|
|
||||||
* @param {number} options.maxIdleTime? Min time a demuxed pipeline must be idle for to be purged
|
|
||||||
*/
|
*/
|
||||||
demux: withDefaultOptions(2, demux),
|
demux: withDefaultOptions(2, demux),
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,17 +1,3 @@
|
|||||||
export async function sleep(time: number): Promise<{} | null> {
|
export async function sleep(time: number): Promise<{} | null> {
|
||||||
return time > 0 ? new Promise(resolve => setTimeout(resolve, time)) : null;
|
return time > 0 ? new Promise(resolve => setTimeout(resolve, time)) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AllStreams =
|
|
||||||
| NodeJS.ReadableStream
|
|
||||||
| NodeJS.ReadWriteStream
|
|
||||||
| NodeJS.WritableStream;
|
|
||||||
|
|
||||||
export function isReadable(
|
|
||||||
stream: AllStreams,
|
|
||||||
): stream is NodeJS.WritableStream {
|
|
||||||
return (
|
|
||||||
(stream as NodeJS.ReadableStream).pipe !== undefined &&
|
|
||||||
(stream as any).readable === true
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,2 @@
|
|||||||
import mhysa from "./functions";
|
import mhysa from "./functions";
|
||||||
import * as _utils from "./utils";
|
|
||||||
export default mhysa;
|
export default mhysa;
|
||||||
|
|
||||||
// @TODO fix this with proper import export
|
|
||||||
export const utils = { ..._utils };
|
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
import { Transform } from "stream";
|
|
||||||
|
|
||||||
export function collected(stream: Transform): any {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
stream.once("data", d => {
|
|
||||||
resolve(d);
|
|
||||||
});
|
|
||||||
stream.once("error", e => {
|
|
||||||
reject(e);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
export { collected } from "./collected";
|
|
||||||
@@ -2,7 +2,7 @@ import { Readable } from "stream";
|
|||||||
import test from "ava";
|
import test from "ava";
|
||||||
import { expect } from "chai";
|
import { expect } from "chai";
|
||||||
import mhysa from "../src";
|
import mhysa from "../src";
|
||||||
const { batch, map, fromArray } = mhysa({ objectMode: true });
|
const { batch } = mhysa({ objectMode: true });
|
||||||
|
|
||||||
test.cb("batch() batches chunks together", t => {
|
test.cb("batch() batches chunks together", t => {
|
||||||
t.plan(3);
|
t.plan(3);
|
||||||
@@ -57,28 +57,3 @@ test.cb("batch() yields a batch after the timeout", t => {
|
|||||||
source.push(null);
|
source.push(null);
|
||||||
}, 600 * 2);
|
}, 600 * 2);
|
||||||
});
|
});
|
||||||
|
|
||||||
test.cb(
|
|
||||||
"batch() yields all input data even when the last element(s) dont make a full batch",
|
|
||||||
t => {
|
|
||||||
const data = [1, 2, 3, 4, 5, 6, 7];
|
|
||||||
|
|
||||||
fromArray([...data])
|
|
||||||
.pipe(batch(3))
|
|
||||||
.pipe(
|
|
||||||
map(d => {
|
|
||||||
t.deepEqual(
|
|
||||||
d,
|
|
||||||
[data.shift(), data.shift(), data.shift()].filter(
|
|
||||||
x => !!x,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.on("error", t.fail)
|
|
||||||
.on("finish", () => {
|
|
||||||
t.is(data.length, 0);
|
|
||||||
t.end();
|
|
||||||
});
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { sleep } from "../src/helpers";
|
|||||||
import { Readable, Writable } from "stream";
|
import { Readable, Writable } from "stream";
|
||||||
import mhysa from "../src";
|
import mhysa from "../src";
|
||||||
import { performance } from "perf_hooks";
|
import { performance } from "perf_hooks";
|
||||||
const { compose, map, fromArray } = mhysa({ objectMode: true });
|
const { compose, map } = mhysa({ objectMode: true });
|
||||||
|
|
||||||
test.cb("compose() chains two streams together in the correct order", t => {
|
test.cb("compose() chains two streams together in the correct order", t => {
|
||||||
t.plan(3);
|
t.plan(3);
|
||||||
@@ -98,7 +98,7 @@ test.cb("piping compose() maintains correct order", t => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("compose() writable length should be less than highWaterMark when handing writes", async t => {
|
test("compose() writable length should be less than highWaterMark when handing writes", async t => {
|
||||||
t.plan(2);
|
t.plan(7);
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
interface Chunk {
|
interface Chunk {
|
||||||
key: string;
|
key: string;
|
||||||
@@ -114,13 +114,9 @@ test("compose() writable length should be less than highWaterMark when handing w
|
|||||||
return chunk;
|
return chunk;
|
||||||
});
|
});
|
||||||
|
|
||||||
const composed = compose(
|
const composed = compose([first, second], undefined, {
|
||||||
[first, second],
|
|
||||||
undefined,
|
|
||||||
{
|
|
||||||
highWaterMark: 2,
|
highWaterMark: 2,
|
||||||
},
|
});
|
||||||
);
|
|
||||||
composed.on("error", err => {
|
composed.on("error", err => {
|
||||||
reject();
|
reject();
|
||||||
});
|
});
|
||||||
@@ -144,12 +140,19 @@ test("compose() writable length should be less than highWaterMark when handing w
|
|||||||
{ key: "e", mapped: [] },
|
{ key: "e", mapped: [] },
|
||||||
];
|
];
|
||||||
|
|
||||||
fromArray(input).pipe(composed);
|
for (const item of input) {
|
||||||
|
const res = composed.write(item);
|
||||||
|
expect(composed._writableState.length).to.be.at.most(2);
|
||||||
|
t.pass();
|
||||||
|
if (!res) {
|
||||||
|
await sleep(10);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test("compose() should emit drain event ~rate * highWaterMark ms for every write that causes backpressure", async t => {
|
test("compose() should emit drain event ~rate * highWaterMark ms for every write that causes backpressure", async t => {
|
||||||
t.plan(2);
|
t.plan(7);
|
||||||
const _rate = 100;
|
const _rate = 100;
|
||||||
const highWaterMark = 2;
|
const highWaterMark = 2;
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
@@ -168,13 +171,9 @@ test("compose() should emit drain event ~rate * highWaterMark ms for every write
|
|||||||
return chunk;
|
return chunk;
|
||||||
});
|
});
|
||||||
|
|
||||||
const composed = compose(
|
const composed = compose([first, second], undefined, {
|
||||||
[first, second],
|
|
||||||
undefined,
|
|
||||||
{
|
|
||||||
highWaterMark,
|
highWaterMark,
|
||||||
},
|
});
|
||||||
);
|
|
||||||
composed.on("error", err => {
|
composed.on("error", err => {
|
||||||
reject();
|
reject();
|
||||||
});
|
});
|
||||||
@@ -182,14 +181,19 @@ test("compose() should emit drain event ~rate * highWaterMark ms for every write
|
|||||||
composed.on("drain", () => {
|
composed.on("drain", () => {
|
||||||
t.pass();
|
t.pass();
|
||||||
expect(composed._writableState.length).to.be.equal(0);
|
expect(composed._writableState.length).to.be.equal(0);
|
||||||
|
expect(performance.now() - start).to.be.closeTo(
|
||||||
|
_rate * highWaterMark,
|
||||||
|
40,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
composed.on("data", (chunk: Chunk) => {
|
composed.on("data", (chunk: Chunk) => {
|
||||||
t.deepEqual(chunk.mapped, [1, 2]);
|
pendingReads--;
|
||||||
|
if (pendingReads === 0) {
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
composed.on("finish", () => resolve());
|
|
||||||
|
|
||||||
const input = [
|
const input = [
|
||||||
{ key: "a", mapped: [] },
|
{ key: "a", mapped: [] },
|
||||||
{ key: "b", mapped: [] },
|
{ key: "b", mapped: [] },
|
||||||
@@ -197,7 +201,19 @@ test("compose() should emit drain event ~rate * highWaterMark ms for every write
|
|||||||
{ key: "d", mapped: [] },
|
{ key: "d", mapped: [] },
|
||||||
{ key: "e", mapped: [] },
|
{ key: "e", mapped: [] },
|
||||||
];
|
];
|
||||||
fromArray(input).pipe(composed);
|
|
||||||
|
let start = performance.now();
|
||||||
|
let pendingReads = input.length;
|
||||||
|
start = performance.now();
|
||||||
|
for (const item of input) {
|
||||||
|
const res = composed.write(item);
|
||||||
|
expect(composed._writableState.length).to.be.at.most(highWaterMark);
|
||||||
|
t.pass();
|
||||||
|
if (!res) {
|
||||||
|
await sleep(_rate * highWaterMark * 2);
|
||||||
|
start = performance.now();
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -221,13 +237,9 @@ test.cb(
|
|||||||
return chunk;
|
return chunk;
|
||||||
});
|
});
|
||||||
|
|
||||||
const composed = compose(
|
const composed = compose([first, second], undefined, {
|
||||||
[first, second],
|
|
||||||
undefined,
|
|
||||||
{
|
|
||||||
highWaterMark: 5,
|
highWaterMark: 5,
|
||||||
},
|
});
|
||||||
);
|
|
||||||
|
|
||||||
composed.on("error", err => {
|
composed.on("error", err => {
|
||||||
t.end(err);
|
t.end(err);
|
||||||
@@ -235,6 +247,10 @@ test.cb(
|
|||||||
|
|
||||||
composed.on("drain", () => {
|
composed.on("drain", () => {
|
||||||
expect(composed._writableState.length).to.be.equal(0);
|
expect(composed._writableState.length).to.be.equal(0);
|
||||||
|
expect(performance.now() - start).to.be.closeTo(
|
||||||
|
_rate * input.length,
|
||||||
|
50,
|
||||||
|
);
|
||||||
t.pass();
|
t.pass();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -255,6 +271,7 @@ test.cb(
|
|||||||
input.forEach(item => {
|
input.forEach(item => {
|
||||||
composed.write(item);
|
composed.write(item);
|
||||||
});
|
});
|
||||||
|
const start = performance.now();
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -284,13 +301,9 @@ test.cb(
|
|||||||
{ highWaterMark: 1 },
|
{ highWaterMark: 1 },
|
||||||
);
|
);
|
||||||
|
|
||||||
const composed = compose(
|
const composed = compose([first, second], undefined, {
|
||||||
[first, second],
|
|
||||||
undefined,
|
|
||||||
{
|
|
||||||
highWaterMark: 5,
|
highWaterMark: 5,
|
||||||
},
|
});
|
||||||
);
|
|
||||||
composed.on("error", err => {
|
composed.on("error", err => {
|
||||||
t.end(err);
|
t.end(err);
|
||||||
});
|
});
|
||||||
@@ -355,13 +368,9 @@ test.cb(
|
|||||||
{ highWaterMark: 2 },
|
{ highWaterMark: 2 },
|
||||||
);
|
);
|
||||||
|
|
||||||
const composed = compose(
|
const composed = compose([first, second], undefined, {
|
||||||
[first, second],
|
|
||||||
undefined,
|
|
||||||
{
|
|
||||||
highWaterMark: 5,
|
highWaterMark: 5,
|
||||||
},
|
});
|
||||||
);
|
|
||||||
composed.on("error", err => {
|
composed.on("error", err => {
|
||||||
t.end(err);
|
t.end(err);
|
||||||
});
|
});
|
||||||
@@ -414,13 +423,9 @@ test.cb(
|
|||||||
return chunk;
|
return chunk;
|
||||||
});
|
});
|
||||||
|
|
||||||
const composed = compose(
|
const composed = compose([first, second], undefined, {
|
||||||
[first, second],
|
|
||||||
undefined,
|
|
||||||
{
|
|
||||||
highWaterMark: 6,
|
highWaterMark: 6,
|
||||||
},
|
});
|
||||||
);
|
|
||||||
|
|
||||||
composed.on("error", err => {
|
composed.on("error", err => {
|
||||||
t.end(err);
|
t.end(err);
|
||||||
@@ -470,12 +475,9 @@ test.cb("compose() should be 'destroyable'", t => {
|
|||||||
return chunk;
|
return chunk;
|
||||||
});
|
});
|
||||||
|
|
||||||
const composed = compose(
|
const composed = compose([first, second], (err: any) => {
|
||||||
[first, second],
|
|
||||||
(err: any) => {
|
|
||||||
t.pass();
|
t.pass();
|
||||||
},
|
});
|
||||||
);
|
|
||||||
|
|
||||||
const fakeSource = new Readable({
|
const fakeSource = new Readable({
|
||||||
objectMode: true,
|
objectMode: true,
|
||||||
@@ -531,13 +533,9 @@ test.cb("compose() `finish` and `end` propagates", t => {
|
|||||||
return chunk;
|
return chunk;
|
||||||
});
|
});
|
||||||
|
|
||||||
const composed = compose(
|
const composed = compose([first, second], undefined, {
|
||||||
[first, second],
|
|
||||||
undefined,
|
|
||||||
{
|
|
||||||
highWaterMark: 3,
|
highWaterMark: 3,
|
||||||
},
|
});
|
||||||
);
|
|
||||||
|
|
||||||
const fakeSource = new Readable({
|
const fakeSource = new Readable({
|
||||||
objectMode: true,
|
objectMode: true,
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import test from "ava";
|
import test from "ava";
|
||||||
import { expect } from "chai";
|
import { expect } from "chai";
|
||||||
import mhysa from "../src";
|
import mhysa from "../src";
|
||||||
import { Writable, Readable } from "stream";
|
import { Writable } from "stream";
|
||||||
const sinon = require("sinon");
|
const sinon = require("sinon");
|
||||||
const { sleep } = require("../src/helpers");
|
const { sleep } = require("../src/helpers");
|
||||||
import { performance } from "perf_hooks";
|
import { performance } from "perf_hooks";
|
||||||
const { demux, map, fromArray } = mhysa({ objectMode: true });
|
const { demux, map } = mhysa();
|
||||||
|
|
||||||
interface Test {
|
interface Test {
|
||||||
key: string;
|
key: string;
|
||||||
@@ -31,7 +31,7 @@ test.cb("demux() constructor should be called once per key", t => {
|
|||||||
return dest;
|
return dest;
|
||||||
});
|
});
|
||||||
|
|
||||||
const demuxed = demux(construct, "key", {});
|
const demuxed = demux(construct, "key", { objectMode: true });
|
||||||
|
|
||||||
demuxed.on("finish", () => {
|
demuxed.on("finish", () => {
|
||||||
expect(construct.withArgs("a").callCount).to.equal(1);
|
expect(construct.withArgs("a").callCount).to.equal(1);
|
||||||
@@ -41,35 +41,8 @@ test.cb("demux() constructor should be called once per key", t => {
|
|||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
fromArray(input).pipe(demuxed);
|
input.forEach(event => demuxed.write(event));
|
||||||
});
|
demuxed.end();
|
||||||
|
|
||||||
test.cb("demux() item written passed in constructor", t => {
|
|
||||||
t.plan(4);
|
|
||||||
const input = [
|
|
||||||
{ key: "a", visited: [] },
|
|
||||||
{ key: "b", visited: [] },
|
|
||||||
{ key: "c", visited: [] },
|
|
||||||
];
|
|
||||||
const construct = sinon.spy((destKey: string, item: any) => {
|
|
||||||
expect(item).to.deep.equal({ key: destKey, visited: [] });
|
|
||||||
t.pass();
|
|
||||||
const dest = map((chunk: Test) => {
|
|
||||||
chunk.visited.push(1);
|
|
||||||
return chunk;
|
|
||||||
});
|
|
||||||
|
|
||||||
return dest;
|
|
||||||
});
|
|
||||||
|
|
||||||
const demuxed = demux(construct, "key", {});
|
|
||||||
|
|
||||||
demuxed.on("finish", () => {
|
|
||||||
t.pass();
|
|
||||||
t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
fromArray(input).pipe(demuxed);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test.cb("demux() should send input through correct pipeline", t => {
|
test.cb("demux() should send input through correct pipeline", t => {
|
||||||
@@ -93,7 +66,7 @@ test.cb("demux() should send input through correct pipeline", t => {
|
|||||||
return dest;
|
return dest;
|
||||||
};
|
};
|
||||||
|
|
||||||
const demuxed = demux(construct, "key", {});
|
const demuxed = demux(construct, "key", { objectMode: true });
|
||||||
|
|
||||||
demuxed.on("finish", () => {
|
demuxed.on("finish", () => {
|
||||||
pipelineSpies["a"].getCalls().forEach(call => {
|
pipelineSpies["a"].getCalls().forEach(call => {
|
||||||
@@ -111,7 +84,8 @@ test.cb("demux() should send input through correct pipeline", t => {
|
|||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
fromArray(input).pipe(demuxed);
|
input.forEach(event => demuxed.write(event));
|
||||||
|
demuxed.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
test.cb("demux() constructor should be called once per key using keyBy", t => {
|
test.cb("demux() constructor should be called once per key using keyBy", t => {
|
||||||
@@ -134,7 +108,7 @@ test.cb("demux() constructor should be called once per key using keyBy", t => {
|
|||||||
return dest;
|
return dest;
|
||||||
});
|
});
|
||||||
|
|
||||||
const demuxed = demux(construct, item => item.key, {});
|
const demuxed = demux(construct, item => item.key, { objectMode: true });
|
||||||
|
|
||||||
demuxed.on("finish", () => {
|
demuxed.on("finish", () => {
|
||||||
expect(construct.withArgs("a").callCount).to.equal(1);
|
expect(construct.withArgs("a").callCount).to.equal(1);
|
||||||
@@ -144,7 +118,8 @@ test.cb("demux() constructor should be called once per key using keyBy", t => {
|
|||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
fromArray(input).pipe(demuxed);
|
input.forEach(event => demuxed.write(event));
|
||||||
|
demuxed.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
test.cb("demux() should send input through correct pipeline using keyBy", t => {
|
test.cb("demux() should send input through correct pipeline using keyBy", t => {
|
||||||
@@ -168,7 +143,7 @@ test.cb("demux() should send input through correct pipeline using keyBy", t => {
|
|||||||
return dest;
|
return dest;
|
||||||
};
|
};
|
||||||
|
|
||||||
const demuxed = demux(construct, item => item.key, {});
|
const demuxed = demux(construct, item => item.key, { objectMode: true });
|
||||||
|
|
||||||
demuxed.on("finish", () => {
|
demuxed.on("finish", () => {
|
||||||
pipelineSpies["a"].getCalls().forEach(call => {
|
pipelineSpies["a"].getCalls().forEach(call => {
|
||||||
@@ -186,10 +161,11 @@ test.cb("demux() should send input through correct pipeline using keyBy", t => {
|
|||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
fromArray(input).pipe(demuxed);
|
input.forEach(event => demuxed.write(event));
|
||||||
|
demuxed.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
test("demux() write should return false and emit drain if more than highWaterMark items are buffered", t => {
|
test("demux() write should return false after if it has >= highWaterMark items buffered and drain should be emitted", t => {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
t.plan(7);
|
t.plan(7);
|
||||||
interface Chunk {
|
interface Chunk {
|
||||||
@@ -213,7 +189,7 @@ test("demux() write should return false and emit drain if more than highWaterMar
|
|||||||
await sleep(slowProcessorSpeed);
|
await sleep(slowProcessorSpeed);
|
||||||
return { ...chunk, mapped: [1] };
|
return { ...chunk, mapped: [1] };
|
||||||
},
|
},
|
||||||
{ highWaterMark: 1 },
|
{ highWaterMark: 1, objectMode: true },
|
||||||
);
|
);
|
||||||
|
|
||||||
first.on("data", chunk => {
|
first.on("data", chunk => {
|
||||||
@@ -229,6 +205,7 @@ test("demux() write should return false and emit drain if more than highWaterMar
|
|||||||
};
|
};
|
||||||
|
|
||||||
const _demux = demux(construct, "key", {
|
const _demux = demux(construct, "key", {
|
||||||
|
objectMode: true,
|
||||||
highWaterMark,
|
highWaterMark,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -252,7 +229,7 @@ test("demux() write should return false and emit drain if more than highWaterMar
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test("demux() should emit one drain event after slowProcessorSpeed * highWaterMark ms when first stream is bottleneck", t => {
|
test("demux() should emit one drain event after slowProcessorSpeed * highWaterMark ms", t => {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
t.plan(7);
|
t.plan(7);
|
||||||
interface Chunk {
|
interface Chunk {
|
||||||
@@ -278,7 +255,7 @@ test("demux() should emit one drain event after slowProcessorSpeed * highWaterMa
|
|||||||
chunk.mapped.push(1);
|
chunk.mapped.push(1);
|
||||||
return chunk;
|
return chunk;
|
||||||
},
|
},
|
||||||
{ highWaterMark: 1 },
|
{ highWaterMark: 1, objectMode: true },
|
||||||
);
|
);
|
||||||
|
|
||||||
first.on("data", () => {
|
first.on("data", () => {
|
||||||
@@ -291,6 +268,7 @@ test("demux() should emit one drain event after slowProcessorSpeed * highWaterMa
|
|||||||
return first;
|
return first;
|
||||||
};
|
};
|
||||||
const _demux = demux(construct, "key", {
|
const _demux = demux(construct, "key", {
|
||||||
|
objectMode: true,
|
||||||
highWaterMark,
|
highWaterMark,
|
||||||
});
|
});
|
||||||
_demux.on("error", err => {
|
_demux.on("error", err => {
|
||||||
@@ -318,7 +296,7 @@ test("demux() should emit one drain event after slowProcessorSpeed * highWaterMa
|
|||||||
|
|
||||||
test("demux() should emit one drain event when writing 6 items with highWaterMark of 5", t => {
|
test("demux() should emit one drain event when writing 6 items with highWaterMark of 5", t => {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
t.plan(1);
|
t.plan(7);
|
||||||
interface Chunk {
|
interface Chunk {
|
||||||
key: string;
|
key: string;
|
||||||
mapped: number[];
|
mapped: number[];
|
||||||
@@ -340,11 +318,12 @@ test("demux() should emit one drain event when writing 6 items with highWaterMar
|
|||||||
chunk.mapped.push(2);
|
chunk.mapped.push(2);
|
||||||
return chunk;
|
return chunk;
|
||||||
},
|
},
|
||||||
{ highWaterMark: 1 },
|
{ highWaterMark: 1, objectMode: true },
|
||||||
);
|
);
|
||||||
|
|
||||||
first.on("data", () => {
|
first.on("data", () => {
|
||||||
pendingReads--;
|
pendingReads--;
|
||||||
|
t.pass();
|
||||||
if (pendingReads === 0) {
|
if (pendingReads === 0) {
|
||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
@@ -352,6 +331,7 @@ test("demux() should emit one drain event when writing 6 items with highWaterMar
|
|||||||
return first;
|
return first;
|
||||||
};
|
};
|
||||||
const _demux = demux(construct, "key", {
|
const _demux = demux(construct, "key", {
|
||||||
|
objectMode: true,
|
||||||
highWaterMark: 5,
|
highWaterMark: 5,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -375,10 +355,9 @@ test("demux() should emit one drain event when writing 6 items with highWaterMar
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test.cb(
|
test.cb.only(
|
||||||
"demux() should emit drain event when second stream is bottleneck after (highWaterMark - 2) * slowProcessorSpeed ms",
|
"demux() should emit drain event when third stream is bottleneck",
|
||||||
t => {
|
t => {
|
||||||
// ie) first two items are pushed directly into first and second streams (highWaterMark - 2 remain in demux)
|
|
||||||
t.plan(8);
|
t.plan(8);
|
||||||
const slowProcessorSpeed = 100;
|
const slowProcessorSpeed = 100;
|
||||||
const highWaterMark = 5;
|
const highWaterMark = 5;
|
||||||
@@ -404,7 +383,7 @@ test.cb(
|
|||||||
chunk.mapped.push(1);
|
chunk.mapped.push(1);
|
||||||
return chunk;
|
return chunk;
|
||||||
},
|
},
|
||||||
{ highWaterMark: 1 },
|
{ objectMode: true, highWaterMark: 1 },
|
||||||
);
|
);
|
||||||
|
|
||||||
const second = map(
|
const second = map(
|
||||||
@@ -413,23 +392,25 @@ test.cb(
|
|||||||
chunk.mapped.push(2);
|
chunk.mapped.push(2);
|
||||||
return chunk;
|
return chunk;
|
||||||
},
|
},
|
||||||
{ highWaterMark: 1 },
|
{ objectMode: true, highWaterMark: 1 },
|
||||||
);
|
);
|
||||||
|
|
||||||
first.pipe(second).pipe(sink);
|
first.pipe(second).pipe(sink);
|
||||||
return first;
|
return first;
|
||||||
};
|
};
|
||||||
const _demux = demux(construct, () => "a", {
|
const _demux = demux(construct, () => "a", {
|
||||||
|
objectMode: true,
|
||||||
highWaterMark,
|
highWaterMark,
|
||||||
});
|
});
|
||||||
_demux.on("error", err => {
|
_demux.on("error", err => {
|
||||||
t.end(err);
|
t.end(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// This event should be received after at least 5 * slowProcessorSpeed (two are read immediately by first and second, 5 remaining in demux before drain event)
|
||||||
_demux.on("drain", () => {
|
_demux.on("drain", () => {
|
||||||
expect(_demux._writableState.length).to.be.equal(0);
|
expect(_demux._writableState.length).to.be.equal(0);
|
||||||
expect(performance.now() - start).to.be.greaterThan(
|
expect(performance.now() - start).to.be.greaterThan(
|
||||||
slowProcessorSpeed * 3,
|
slowProcessorSpeed * (input.length - 2),
|
||||||
);
|
);
|
||||||
t.pass();
|
t.pass();
|
||||||
});
|
});
|
||||||
@@ -446,14 +427,15 @@ test.cb(
|
|||||||
let pendingReads = input.length;
|
let pendingReads = input.length;
|
||||||
|
|
||||||
const start = performance.now();
|
const start = performance.now();
|
||||||
fromArray(input).pipe(_demux);
|
input.forEach(item => {
|
||||||
|
_demux.write(item);
|
||||||
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
test.cb(
|
test.cb(
|
||||||
"demux() should emit drain event when third stream is bottleneck",
|
"demux() should emit drain event when second stream is bottleneck",
|
||||||
t => {
|
t => {
|
||||||
// @TODO investigate why drain is emitted after slowProcessorSpeed
|
|
||||||
t.plan(8);
|
t.plan(8);
|
||||||
const slowProcessorSpeed = 100;
|
const slowProcessorSpeed = 100;
|
||||||
const highWaterMark = 5;
|
const highWaterMark = 5;
|
||||||
@@ -464,7 +446,7 @@ test.cb(
|
|||||||
const sink = new Writable({
|
const sink = new Writable({
|
||||||
objectMode: true,
|
objectMode: true,
|
||||||
write(chunk, encoding, cb) {
|
write(chunk, encoding, cb) {
|
||||||
expect(chunk.mapped).to.deep.equal([1, 2, 3]);
|
expect(chunk.mapped).to.deep.equal([1, 2]);
|
||||||
t.pass();
|
t.pass();
|
||||||
pendingReads--;
|
pendingReads--;
|
||||||
if (pendingReads === 0) {
|
if (pendingReads === 0) {
|
||||||
@@ -479,14 +461,14 @@ test.cb(
|
|||||||
chunk.mapped.push(1);
|
chunk.mapped.push(1);
|
||||||
return chunk;
|
return chunk;
|
||||||
},
|
},
|
||||||
{ highWaterMark: 1 },
|
{ objectMode: true, highWaterMark: 1 },
|
||||||
);
|
);
|
||||||
const second = map(
|
const second = map(
|
||||||
(chunk: Chunk) => {
|
(chunk: Chunk) => {
|
||||||
chunk.mapped.push(2);
|
chunk.mapped.push(2);
|
||||||
return chunk;
|
return chunk;
|
||||||
},
|
},
|
||||||
{ highWaterMark: 1 },
|
{ objectMode: true, highWaterMark: 1 },
|
||||||
);
|
);
|
||||||
|
|
||||||
const third = map(
|
const third = map(
|
||||||
@@ -495,7 +477,7 @@ test.cb(
|
|||||||
chunk.mapped.push(3);
|
chunk.mapped.push(3);
|
||||||
return chunk;
|
return chunk;
|
||||||
},
|
},
|
||||||
{ highWaterMark: 1 },
|
{ objectMode: true, highWaterMark: 1 },
|
||||||
);
|
);
|
||||||
|
|
||||||
first
|
first
|
||||||
@@ -505,16 +487,18 @@ test.cb(
|
|||||||
return first;
|
return first;
|
||||||
};
|
};
|
||||||
const _demux = demux(construct, () => "a", {
|
const _demux = demux(construct, () => "a", {
|
||||||
|
objectMode: true,
|
||||||
highWaterMark,
|
highWaterMark,
|
||||||
});
|
});
|
||||||
_demux.on("error", err => {
|
_demux.on("error", err => {
|
||||||
t.end(err);
|
t.end(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// This event should be received after at least 3 * slowProcessorSpeed (two are read immediately by first and second, 3 remaining in demux before drain event)
|
||||||
_demux.on("drain", () => {
|
_demux.on("drain", () => {
|
||||||
expect(_demux._writableState.length).to.be.equal(0);
|
expect(_demux._writableState.length).to.be.equal(0);
|
||||||
expect(performance.now() - start).to.be.greaterThan(
|
expect(performance.now() - start).to.be.greaterThan(
|
||||||
slowProcessorSpeed,
|
slowProcessorSpeed * (input.length - 4),
|
||||||
);
|
);
|
||||||
t.pass();
|
t.pass();
|
||||||
});
|
});
|
||||||
@@ -531,7 +515,9 @@ test.cb(
|
|||||||
let pendingReads = input.length;
|
let pendingReads = input.length;
|
||||||
|
|
||||||
const start = performance.now();
|
const start = performance.now();
|
||||||
fromArray(input).pipe(_demux);
|
input.forEach(item => {
|
||||||
|
_demux.write(item);
|
||||||
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -550,21 +536,10 @@ test("demux() should be blocked by slowest pipeline", t => {
|
|||||||
chunk.mapped.push(1);
|
chunk.mapped.push(1);
|
||||||
return chunk;
|
return chunk;
|
||||||
},
|
},
|
||||||
{ highWaterMark: 1 },
|
{ objectMode: true, highWaterMark: 1 },
|
||||||
);
|
);
|
||||||
|
|
||||||
return first;
|
first.on("data", chunk => {
|
||||||
};
|
|
||||||
|
|
||||||
const _demux = demux(construct, "key", {
|
|
||||||
highWaterMark: 1,
|
|
||||||
});
|
|
||||||
|
|
||||||
_demux.on("error", err => {
|
|
||||||
reject(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
_demux.on("data", async chunk => {
|
|
||||||
pendingReads--;
|
pendingReads--;
|
||||||
if (chunk.key === "b") {
|
if (chunk.key === "b") {
|
||||||
expect(performance.now() - start).to.be.greaterThan(
|
expect(performance.now() - start).to.be.greaterThan(
|
||||||
@@ -575,12 +550,22 @@ test("demux() should be blocked by slowest pipeline", t => {
|
|||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
return first;
|
||||||
|
};
|
||||||
|
const _demux = demux(construct, "key", {
|
||||||
|
objectMode: true,
|
||||||
|
highWaterMark: 1,
|
||||||
|
});
|
||||||
|
_demux.on("error", err => {
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
|
||||||
const input = [
|
const input = [
|
||||||
{ key: "a", mapped: [] },
|
{ key: "a", mapped: [] },
|
||||||
{ key: "a", mapped: [] },
|
{ key: "a", mapped: [] },
|
||||||
{ key: "c", mapped: [] },
|
{ key: "c", mapped: [] },
|
||||||
{ key: "c", mapped: [] },
|
{ key: "c", mapped: [] },
|
||||||
|
{ key: "c", mapped: [] },
|
||||||
{ key: "b", mapped: [] },
|
{ key: "b", mapped: [] },
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -599,302 +584,74 @@ test("demux() should be blocked by slowest pipeline", t => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test.cb("Demux should remux to sink", t => {
|
test("demux() should emit drain event when second stream in pipeline is bottleneck", t => {
|
||||||
t.plan(6);
|
t.plan(5);
|
||||||
let i = 0;
|
const highWaterMark = 3;
|
||||||
const input = [
|
return new Promise(async (resolve, reject) => {
|
||||||
{ key: "a", visited: [] },
|
|
||||||
{ key: "b", visited: [] },
|
|
||||||
{ key: "a", visited: [] },
|
|
||||||
{ key: "c", visited: [] },
|
|
||||||
{ key: "a", visited: [] },
|
|
||||||
{ key: "b", visited: [] },
|
|
||||||
];
|
|
||||||
const result = [
|
|
||||||
{ key: "a", visited: ["a"] },
|
|
||||||
{ key: "b", visited: ["b"] },
|
|
||||||
{ key: "a", visited: ["a"] },
|
|
||||||
{ key: "c", visited: ["c"] },
|
|
||||||
{ key: "a", visited: ["a"] },
|
|
||||||
{ key: "b", visited: ["b"] },
|
|
||||||
];
|
|
||||||
const construct = (destKey: string) => {
|
|
||||||
const dest = map((chunk: any) => {
|
|
||||||
chunk.visited.push(destKey);
|
|
||||||
return chunk;
|
|
||||||
});
|
|
||||||
|
|
||||||
return dest;
|
|
||||||
};
|
|
||||||
|
|
||||||
const sink = map(d => {
|
|
||||||
t.deepEqual(d, result[i]);
|
|
||||||
i++;
|
|
||||||
if (i === input.length) {
|
|
||||||
t.end();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const demuxed = demux(construct, "key", {});
|
|
||||||
|
|
||||||
fromArray(input)
|
|
||||||
.pipe(demuxed)
|
|
||||||
.pipe(sink);
|
|
||||||
});
|
|
||||||
|
|
||||||
test.cb("Demux should send data events", t => {
|
|
||||||
t.plan(6);
|
|
||||||
let i = 0;
|
|
||||||
const input = [
|
|
||||||
{ key: "a", visited: [] },
|
|
||||||
{ key: "b", visited: [] },
|
|
||||||
{ key: "a", visited: [] },
|
|
||||||
{ key: "c", visited: [] },
|
|
||||||
{ key: "a", visited: [] },
|
|
||||||
{ key: "b", visited: [] },
|
|
||||||
];
|
|
||||||
const result = [
|
|
||||||
{ key: "a", visited: ["a"] },
|
|
||||||
{ key: "b", visited: ["b"] },
|
|
||||||
{ key: "a", visited: ["a"] },
|
|
||||||
{ key: "c", visited: ["c"] },
|
|
||||||
{ key: "a", visited: ["a"] },
|
|
||||||
{ key: "b", visited: ["b"] },
|
|
||||||
];
|
|
||||||
const construct = (destKey: string) => {
|
|
||||||
const dest = map((chunk: any) => {
|
|
||||||
chunk.visited.push(destKey);
|
|
||||||
return chunk;
|
|
||||||
});
|
|
||||||
|
|
||||||
return dest;
|
|
||||||
};
|
|
||||||
|
|
||||||
const demuxed = demux(construct, "key", {});
|
|
||||||
|
|
||||||
fromArray(input).pipe(demuxed);
|
|
||||||
|
|
||||||
demuxed.on("data", d => {
|
|
||||||
t.deepEqual(d, result[i]);
|
|
||||||
i++;
|
|
||||||
if (i === input.length) {
|
|
||||||
t.end();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
test.cb("demux() `finish` and `end` propagates", t => {
|
|
||||||
interface Chunk {
|
interface Chunk {
|
||||||
key: string;
|
key: string;
|
||||||
mapped: number[];
|
mapped: number[];
|
||||||
}
|
}
|
||||||
|
const sink = new Writable({
|
||||||
t.plan(9);
|
|
||||||
|
|
||||||
const construct = (destKey: string) => {
|
|
||||||
const dest = map((chunk: any) => {
|
|
||||||
chunk.mapped.push(destKey);
|
|
||||||
return chunk;
|
|
||||||
});
|
|
||||||
return dest;
|
|
||||||
};
|
|
||||||
|
|
||||||
const _demux = demux(construct, "key", {
|
|
||||||
highWaterMark: 3,
|
|
||||||
});
|
|
||||||
|
|
||||||
const fakeSource = new Readable({
|
|
||||||
objectMode: true,
|
objectMode: true,
|
||||||
read() {
|
write(chunk, encoding, cb) {
|
||||||
return;
|
expect(chunk.mapped).to.deep.equal([1, 2]);
|
||||||
|
t.pass();
|
||||||
|
cb();
|
||||||
|
if (pendingReads === 0) {
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const sink = map((d: any) => {
|
|
||||||
const curr = input.shift();
|
|
||||||
t.is(curr.key, d.key);
|
|
||||||
t.deepEqual(d.mapped, [d.key]);
|
|
||||||
});
|
|
||||||
|
|
||||||
fakeSource.pipe(_demux).pipe(sink);
|
|
||||||
|
|
||||||
fakeSource.on("end", () => {
|
|
||||||
t.pass();
|
|
||||||
});
|
|
||||||
_demux.on("finish", () => {
|
|
||||||
t.pass();
|
|
||||||
});
|
|
||||||
_demux.on("unpipe", () => {
|
|
||||||
t.pass();
|
|
||||||
});
|
|
||||||
_demux.on("end", () => {
|
|
||||||
t.pass();
|
|
||||||
t.end();
|
|
||||||
});
|
|
||||||
sink.on("finish", () => {
|
|
||||||
t.pass();
|
|
||||||
});
|
|
||||||
|
|
||||||
const input = [
|
|
||||||
{ key: "a", mapped: [] },
|
|
||||||
{ key: "b", mapped: [] },
|
|
||||||
{ key: "a", mapped: [] },
|
|
||||||
{ key: "a", mapped: [] },
|
|
||||||
{ key: "b", mapped: [] },
|
|
||||||
];
|
|
||||||
fakeSource.push(input[0]);
|
|
||||||
fakeSource.push(input[1]);
|
|
||||||
fakeSource.push(null);
|
|
||||||
});
|
|
||||||
|
|
||||||
test.cb("demux() `unpipe` propagates", t => {
|
|
||||||
interface Chunk {
|
|
||||||
key: string;
|
|
||||||
mapped: number[];
|
|
||||||
}
|
|
||||||
|
|
||||||
t.plan(7);
|
|
||||||
|
|
||||||
const construct = (destKey: string) => {
|
const construct = (destKey: string) => {
|
||||||
const dest = map((chunk: any) => {
|
const first = map(
|
||||||
chunk.mapped.push(destKey);
|
(chunk: Chunk) => {
|
||||||
|
expect(first._readableState.length).to.be.at.most(2);
|
||||||
|
chunk.mapped.push(1);
|
||||||
return chunk;
|
return chunk;
|
||||||
});
|
|
||||||
return dest;
|
|
||||||
};
|
|
||||||
|
|
||||||
const _demux = demux(construct, "key", {
|
|
||||||
highWaterMark: 3,
|
|
||||||
});
|
|
||||||
|
|
||||||
const fakeSource = new Readable({
|
|
||||||
objectMode: true,
|
|
||||||
read() {
|
|
||||||
return;
|
|
||||||
},
|
},
|
||||||
});
|
{ objectMode: true, highWaterMark: 2 },
|
||||||
|
);
|
||||||
|
|
||||||
const sink = map((d: any) => {
|
const second = map(
|
||||||
const curr = input.shift();
|
async (chunk: Chunk) => {
|
||||||
t.is(curr.key, d.key);
|
await sleep(100);
|
||||||
t.deepEqual(d.mapped, [d.key]);
|
chunk.mapped.push(2);
|
||||||
});
|
expect(second._writableState.length).to.be.equal(1);
|
||||||
|
pendingReads--;
|
||||||
fakeSource.pipe(_demux).pipe(sink);
|
|
||||||
|
|
||||||
_demux.on("unpipe", () => {
|
|
||||||
t.pass();
|
|
||||||
});
|
|
||||||
|
|
||||||
sink.on("unpipe", () => {
|
|
||||||
t.pass();
|
|
||||||
});
|
|
||||||
|
|
||||||
sink.on("finish", () => {
|
|
||||||
t.pass();
|
|
||||||
t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
const input = [
|
|
||||||
{ key: "a", mapped: [] },
|
|
||||||
{ key: "b", mapped: [] },
|
|
||||||
{ key: "a", mapped: [] },
|
|
||||||
{ key: "a", mapped: [] },
|
|
||||||
{ key: "b", mapped: [] },
|
|
||||||
];
|
|
||||||
fakeSource.push(input[0]);
|
|
||||||
fakeSource.push(input[1]);
|
|
||||||
fakeSource.push(null);
|
|
||||||
});
|
|
||||||
|
|
||||||
test.cb("demux() should be 'destroyable'", t => {
|
|
||||||
t.plan(2);
|
|
||||||
const _sleep = 100;
|
|
||||||
interface Chunk {
|
|
||||||
key: string;
|
|
||||||
mapped: string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
const construct = (destKey: string) => {
|
|
||||||
const first = map(async (chunk: Chunk) => {
|
|
||||||
await sleep(_sleep);
|
|
||||||
chunk.mapped.push(destKey);
|
|
||||||
return chunk;
|
return chunk;
|
||||||
});
|
},
|
||||||
|
{ objectMode: true, highWaterMark: 1 },
|
||||||
|
);
|
||||||
|
|
||||||
|
first.pipe(second).pipe(sink);
|
||||||
return first;
|
return first;
|
||||||
};
|
};
|
||||||
|
|
||||||
const _demux = demux(construct, "key");
|
const _demux = demux(construct, "key", {
|
||||||
|
|
||||||
const fakeSource = new Readable({
|
|
||||||
objectMode: true,
|
objectMode: true,
|
||||||
read() {
|
highWaterMark,
|
||||||
return;
|
});
|
||||||
},
|
_demux.on("error", err => {
|
||||||
|
reject();
|
||||||
});
|
});
|
||||||
|
|
||||||
const fakeSink = new Writable({
|
_demux.on("drain", () => {
|
||||||
objectMode: true,
|
expect(_demux._writableState.length).to.be.equal(0);
|
||||||
write(data, enc, cb) {
|
t.pass();
|
||||||
const cur = input.shift();
|
|
||||||
t.is(cur.key, data.key);
|
|
||||||
t.deepEqual(cur.mapped, ["a"]);
|
|
||||||
if (cur.key === "a") {
|
|
||||||
_demux.destroy();
|
|
||||||
}
|
|
||||||
cb();
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
_demux.on("close", t.end);
|
|
||||||
fakeSource.pipe(_demux).pipe(fakeSink);
|
|
||||||
|
|
||||||
const input = [
|
const input = [
|
||||||
{ key: "a", mapped: [] },
|
{ key: "a", mapped: [] },
|
||||||
{ key: "b", mapped: [] },
|
{ key: "a", mapped: [] },
|
||||||
{ key: "c", mapped: [] },
|
{ key: "a", mapped: [] },
|
||||||
{ key: "d", mapped: [] },
|
{ key: "a", mapped: [] },
|
||||||
{ key: "e", mapped: [] },
|
|
||||||
];
|
];
|
||||||
fakeSource.push(input[0]);
|
let pendingReads = input.length;
|
||||||
fakeSource.push(input[1]);
|
|
||||||
fakeSource.push(input[2]);
|
input.forEach(item => {
|
||||||
fakeSource.push(input[3]);
|
_demux.write(item);
|
||||||
fakeSource.push(input[4]);
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test.cb("Should delete idle pipelines", t => {
|
|
||||||
t.plan(5);
|
|
||||||
const input = [
|
|
||||||
{ key: "a", visited: [] },
|
|
||||||
{ key: "b", visited: [] },
|
|
||||||
{ key: "b", visited: [] },
|
|
||||||
{ key: "a", visited: [] },
|
|
||||||
{ key: "b", visited: [] },
|
|
||||||
{ key: "c", visited: [] },
|
|
||||||
];
|
|
||||||
const construct = sinon.spy((destKey: string) => {
|
|
||||||
const dest = map((chunk: Test) => {
|
|
||||||
chunk.visited.push(1);
|
|
||||||
return chunk;
|
|
||||||
});
|
|
||||||
t.pass();
|
|
||||||
|
|
||||||
return dest;
|
|
||||||
});
|
|
||||||
|
|
||||||
const demuxed = demux(construct, "key", {
|
|
||||||
maxIdleTime: 110,
|
|
||||||
purgeIdleInterval: 110,
|
|
||||||
});
|
|
||||||
|
|
||||||
demuxed.on("data", data => {
|
|
||||||
if (data.key === "c") t.end();
|
|
||||||
});
|
|
||||||
|
|
||||||
for (let i = 0; i < input.length; i++) {
|
|
||||||
setTimeout(() => {
|
|
||||||
demuxed.write(input[i]);
|
|
||||||
}, i * 100);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
import test from "ava";
|
|
||||||
import { collected } from "../../src/utils";
|
|
||||||
import mhysa from "../../src";
|
|
||||||
const { fromArray, collect } = mhysa({ objectMode: true });
|
|
||||||
|
|
||||||
test("collected returns a promise for the first data point", async t => {
|
|
||||||
const data = collected(fromArray([1, 2, 3, 4]).pipe(collect()));
|
|
||||||
t.deepEqual(await data, [1, 2, 3, 4]);
|
|
||||||
});
|
|
||||||
10
yarn.lock
10
yarn.lock
@@ -409,10 +409,10 @@
|
|||||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.7.2.tgz#c4e63af5e8823ce9cc3f0b34f7b998c2171f0c44"
|
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.7.2.tgz#c4e63af5e8823ce9cc3f0b34f7b998c2171f0c44"
|
||||||
integrity sha512-dyYO+f6ihZEtNPDcWNR1fkoTDf3zAK3lAABDze3mz6POyIercH0lEUawUFXlG8xaQZmm1yEBON/4TsYv/laDYg==
|
integrity sha512-dyYO+f6ihZEtNPDcWNR1fkoTDf3zAK3lAABDze3mz6POyIercH0lEUawUFXlG8xaQZmm1yEBON/4TsYv/laDYg==
|
||||||
|
|
||||||
"@types/node@^12.12.15":
|
"@types/node@^12.7.2":
|
||||||
version "12.12.15"
|
version "12.12.14"
|
||||||
resolved "https://npm.dev.jogogo.co/@types%2fnode/-/node-12.12.15.tgz#8dfb6ce22fedd469128137640a3aa8f17415422f"
|
resolved "https://npm.dev.jogogo.co/@types%2fnode/-/node-12.12.14.tgz#1c1d6e3c75dba466e0326948d56e8bd72a1903d2"
|
||||||
integrity sha512-Pv+vWicyFd07Hw/SmNnTUguqrHgDfMtjabvD9sQyxeqbpCEg8CmViLBaVPHtNsoBgZECrRf5/pgV6FJIBrGSjw==
|
integrity sha512-u/SJDyXwuihpwjXy7hOOghagLEV1KdAST6syfnOk6QZAMzZuWZqXy5aYYZbh8Jdpd4escVFP0MvftHNDb9pruA==
|
||||||
|
|
||||||
"@types/sinon@^7.0.13":
|
"@types/sinon@^7.0.13":
|
||||||
version "7.5.1"
|
version "7.5.1"
|
||||||
@@ -1935,7 +1935,7 @@ merge2@^1.2.3, merge2@^1.3.0:
|
|||||||
integrity sha512-2j4DAdlBOkiSZIsaXk4mTE3sRS02yBHAtfy127xRV3bQUFqXkjHCHLW6Scv7DwNRbIWNHH8zpnz9zMaKXIdvYw==
|
integrity sha512-2j4DAdlBOkiSZIsaXk4mTE3sRS02yBHAtfy127xRV3bQUFqXkjHCHLW6Scv7DwNRbIWNHH8zpnz9zMaKXIdvYw==
|
||||||
|
|
||||||
mhysa@./:
|
mhysa@./:
|
||||||
version "2.0.0-alpha.1"
|
version "0.0.1-beta.4"
|
||||||
|
|
||||||
micromatch@^4.0.2:
|
micromatch@^4.0.2:
|
||||||
version "4.0.2"
|
version "4.0.2"
|
||||||
|
|||||||
Reference in New Issue
Block a user