8 Commits

Author SHA1 Message Date
Lewis Diamond
1c02cb5aea No need to create an array to call concat 2020-07-12 18:52:21 -04:00
Lewis Diamond
12cbddf7e0 Update README 2020-07-09 12:51:08 -04:00
Lewis Diamond
2e5a6dbcc8 update samples 2020-07-04 11:06:41 -04:00
Lewis Diamond
e6fb55eb1a ObjectMode is now true by default 2020-07-04 10:58:08 -04:00
Lewis Diamond
74ce415118 Update tests make export a bit nicer 2020-07-04 10:43:52 -04:00
Lewis Diamond
1675b18d5b add prepare script 2020-07-04 00:15:11 -04:00
Lewis Diamond
ca6f36d19a Quick rename 2020-07-04 00:08:31 -04:00
Jerry Kurian
db783805ad Arrays of pipelines by key (#7)
* Demux handles arrays by key

* Rename construct

* version

* Minor changes to types
2020-05-12 09:44:11 -04:00
46 changed files with 315 additions and 337 deletions

View File

@@ -1,15 +1,13 @@
# Mhysa
# Strom
**Dependency-free stream utils for Node.js**
<sub>Released under the [MIT](https://github.com/Wenzil/Mhysa/blob/master/LICENSE) license.</sub>
<sub>Released under the [MIT](https://git.lewis.id/strom/blob/master/LICENSE) license.</sub>
```sh
yarn add mhysa
yarn add strom // Name TBD
```
<sub>Tested with Node.js versions 8+</sub>
## fromArray(array)
Convert an array into a `Readable` stream of its elements
@@ -18,7 +16,7 @@ Convert an array into a `Readable` stream of its elements
| `array` | `T[]` | Array of elements to stream |
```js
Mhysa.fromArray(["a", "b"])
strom.fromArray(["a", "b"])
.pipe(process.stdout);
// ab is printed out
```
@@ -35,8 +33,8 @@ Return a `ReadWrite` stream that maps streamed chunks
| `options.writableObjectMode` | `boolean` | Whether this stream should behave as a writable stream of objects |
```js
Mhysa.fromArray(["a", "b"])
.pipe(Mhysa.map(s => s.toUpperCase()))
strom.fromArray(["a", "b"])
.pipe(strom.map(s => s.toUpperCase()))
.pipe(process.stdout);
// AB is printed out
```
@@ -53,8 +51,8 @@ Return a `ReadWrite` stream that flat maps streamed chunks
| `options.writableObjectMode` | `boolean` | Whether this stream should behave as a writable stream of objects |
```js
Mhysa.fromArray(["a", "AA"])
.pipe(Mhysa.flatMap(s => new Array(s.length).fill(s)))
strom.fromArray(["a", "AA"])
.pipe(strom.flatMap(s => new Array(s.length).fill(s)))
.pipe(process.stdout);
// aAAAA is printed out
```
@@ -70,8 +68,8 @@ Return a `ReadWrite` stream that filters out streamed chunks for which the predi
| `options.objectMode` | `boolean` | `boolean` | Whether this stream should behave as a stream of objects |
```js
Mhysa.fromArray(["a", "b", "c"])
.pipe(Mhysa.filter(s => s !== "b"))
strom.fromArray(["a", "b", "c"])
.pipe(strom.filter(s => s !== "b"))
.pipe(process.stdout);
// ac is printed out
```
@@ -90,9 +88,9 @@ value
| `options.writableObjectMode` | `boolean` | Whether this stream should behave as a writable stream of objects |
```js
Mhysa.fromArray(["a", "b", "cc"])
.pipe(Mhysa.reduce((acc, s) => ({ ...acc, [s]: s.length }), {}))
.pipe(Mhysa.stringify())
strom.fromArray(["a", "b", "cc"])
.pipe(strom.reduce((acc, s) => ({ ...acc, [s]: s.length }), {}))
.pipe(strom.stringify())
.pipe(process.stdout);
// {"a":1,"b":1","c":2} is printed out
```
@@ -108,9 +106,9 @@ Return a `ReadWrite` stream that splits streamed chunks using the given separato
| `options.encoding` | `string` | Character encoding to use for decoding chunks. Defaults to utf8
```js
Mhysa.fromArray(["a,b", "c,d"])
.pipe(Mhysa.split(","))
.pipe(Mhysa.join("|"))
strom.fromArray(["a,b", "c,d"])
.pipe(strom.split(","))
.pipe(strom.join("|"))
.pipe(process.stdout);
// a|bc|d is printed out
```
@@ -126,8 +124,8 @@ Return a `ReadWrite` stream that joins streamed chunks using the given separator
| `options.encoding` | `string` | Character encoding to use for decoding chunks. Defaults to utf8
```js
Mhysa.fromArray(["a", "b", "c"])
.pipe(Mhysa.join(","))
strom.fromArray(["a", "b", "c"])
.pipe(strom.join(","))
.pipe(process.stdout);
// a,b,c is printed out
```
@@ -145,8 +143,8 @@ the streamed chunks with the specified replacement string
| `options.encoding` | `string` | Character encoding to use for decoding chunks. Defaults to utf8
```js
Mhysa.fromArray(["a1", "b22", "c333"])
.pipe(Mhysa.replace(/b\d+/, "B"))
strom.fromArray(["a1", "b22", "c333"])
.pipe(strom.replace(/b\d+/, "B"))
.pipe(process.stdout);
// a1Bc333 is printed out
```
@@ -156,8 +154,8 @@ Mhysa.fromArray(["a1", "b22", "c333"])
Return a `ReadWrite` stream that parses the streamed chunks as JSON
```js
Mhysa.fromArray(['{ "a": "b" }'])
.pipe(Mhysa.parse())
strom.fromArray(['{ "a": "b" }'])
.pipe(strom.parse())
.once("data", object => console.log(object));
// { a: 'b' } is printed out
```
@@ -167,8 +165,8 @@ Mhysa.fromArray(['{ "a": "b" }'])
Return a `ReadWrite` stream that stringifies the streamed chunks to JSON
```js
Mhysa.fromArray([{ a: "b" }])
.pipe(Mhysa.stringify())
strom.fromArray([{ a: "b" }])
.pipe(strom.stringify())
.pipe(process.stdout);
// {"a":"b"} is printed out
```
@@ -183,8 +181,8 @@ Return a `ReadWrite` stream that collects streamed chunks into an array or buffe
| `options.objectMode` | `boolean` | Whether this stream should behave as a stream of objects |
```js
Mhysa.fromArray(["a", "b", "c"])
.pipe(Mhysa.collect({ objectMode: true }))
strom.fromArray(["a", "b", "c"])
.pipe(strom.collect({ objectMode: true }))
.once("data", object => console.log(object));
// [ 'a', 'b', 'c' ] is printed out
```
@@ -200,7 +198,7 @@ Return a `Readable` stream of readable streams concatenated together
```js
const source1 = new Readable();
const source2 = new Readable();
Mhysa.concat(source1, source2).pipe(process.stdout)
strom.concat(source1, source2).pipe(process.stdout)
source1.push("a1 ");
source2.push("c3 ");
source1.push("b2 ");
@@ -221,7 +219,7 @@ Return a `Readable` stream of readable streams merged together in chunk arrival
```js
const source1 = new Readable({ read() {} });
const source2 = new Readable({ read() {} });
Mhysa.merge(source1, source2).pipe(process.stdout);
strom.merge(source1, source2).pipe(process.stdout);
source1.push("a1 ");
setTimeout(() => source2.push("c3 "), 10);
setTimeout(() => source1.push("b2 "), 20);
@@ -243,8 +241,8 @@ cause the given readable stream to yield chunks
```js
const catProcess = require("child_process").exec("grep -o ab");
Mhysa.fromArray(["a", "b", "c"])
.pipe(Mhysa.duplex(catProcess.stdin, catProcess.stdout))
strom.fromArray(["a", "b", "c"])
.pipe(strom.duplex(catProcess.stdin, catProcess.stdout))
.pipe(process.stdout);
// ab is printed out
```
@@ -259,8 +257,8 @@ Return a `Duplex` stream from a child process' stdin and stdout
```js
const catProcess = require("child_process").exec("grep -o ab");
Mhysa.fromArray(["a", "b", "c"])
.pipe(Mhysa.child(catProcess))
strom.fromArray(["a", "b", "c"])
.pipe(strom.child(catProcess))
.pipe(process.stdout);
// ab is printed out
```
@@ -276,8 +274,8 @@ ended
```js
let f = async () => {
const source = Mhysa.fromArray(["a", "b", "c"]);
console.log(await Mhysa.last(source));
const source = strom.fromArray(["a", "b", "c"]);
console.log(await strom.last(source));
};
f();
// c is printed out

View File

@@ -1,17 +1,16 @@
{
"name": "@jogogo/mhysa",
"version": "2.0.0-alpha.3",
"description": "Streams and event emitter utils for Node.js",
"name": "strom",
"version": "2.0.0",
"description": "Streams utils for Node.js",
"keywords": [
"promise",
"stream",
"event emitter",
"utils"
],
"author": {
"contributors": [
{
"name": "Wenzil"
},
"contributors": [
{
"name": "jerry",
"email": "jerry@jogogo.co"
@@ -27,18 +26,16 @@
"files": [
"dist"
],
"publishConfig": {
"registry": "https://npm.dev.jogogo.co/"
},
"repository": {
"url": "git@github.com:Jogogoplay/mhysa.git",
"url": "git@git.lewis.id:ldiamond/strom.git",
"type": "git"
},
"scripts": {
"test": "ava",
"lint": "tslint -p tsconfig.json",
"validate:tslint": "tslint-config-prettier-check ./tslint.json",
"prepublishOnly": "yarn lint && yarn test && yarn tsc -d"
"prepublishOnly": "yarn lint && yarn test && yarn tsc -d",
"prepare": "tsc"
},
"dependencies": {},
"devDependencies": {

View File

@@ -1,8 +1,8 @@
const Mhysa = require("mhysa");
const strom = require("strom").strom();
Mhysa.fromArray(["a", "b", "c"])
.pipe(Mhysa.map(s => Promise.resolve(s + s)))
.pipe(Mhysa.flatMap(s => Promise.resolve([s, s.toUpperCase()])))
.pipe(Mhysa.filter(s => Promise.resolve(s !== "bb")))
.pipe(Mhysa.join(","))
strom.fromArray(["a", "b", "c"])
.pipe(strom.map(s => Promise.resolve(s + s)))
.pipe(strom.flatMap(s => Promise.resolve([s, s.toUpperCase()])))
.pipe(strom.filter(s => Promise.resolve(s !== "bb")))
.pipe(strom.join(","))
.pipe(process.stdout);

View File

@@ -1,6 +1,6 @@
const Mhysa = require("mhysa");
const strom = require("strom").strom();
const catProcess = require("child_process").exec("grep -o ab");
Mhysa.fromArray(["a", "b", "c"])
.pipe(Mhysa.child(catProcess))
strom.fromArray(["a", "b", "c"])
.pipe(strom.child(catProcess))
.pipe(process.stdout);

View File

@@ -1,5 +1,5 @@
const Mhysa = require("mhysa");
const strom = require("strom").strom();
Mhysa.fromArray(["a", "b", "c"])
.pipe(Mhysa.collect({ objectMode: true }))
strom.fromArray(["a", "b", "c"])
.pipe(strom.collect({ objectMode: true }))
.on("data", object => console.log(object));

View File

@@ -1,9 +1,9 @@
const { Readable } = require("stream");
const Mhysa = require("mhysa");
const strom = require("strom").strom();
const source1 = new Readable();
const source2 = new Readable();
Mhysa.concat(source1, source2).pipe(process.stdout);
strom.concat(source1, source2).pipe(process.stdout);
source1.push("a1 ");
source2.push("c3 ");
source1.push("b2 ");

View File

@@ -1,6 +1,6 @@
const Mhysa = require("mhysa");
const strom = require("strom").strom();
const catProcess = require("child_process").exec("grep -o ab");
Mhysa.fromArray(["a", "b", "c"])
.pipe(Mhysa.duplex(catProcess.stdin, catProcess.stdout))
strom.fromArray(["a", "b", "c"])
.pipe(strom.duplex(catProcess.stdin, catProcess.stdout))
.pipe(process.stdout);

View File

@@ -1,6 +1,6 @@
const Mhysa = require("mhysa");
const strom = require("strom").strom();
Mhysa.fromArray(["a", "b", "c"])
.pipe(Mhysa.filter(s => s !== "b"))
.pipe(Mhysa.join(","))
strom.fromArray(["a", "b", "c"])
.pipe(strom.filter(s => s !== "b"))
.pipe(strom.join(","))
.pipe(process.stdout);

View File

@@ -1,6 +1,6 @@
const Mhysa = require("mhysa");
const strom = require("strom").strom();
Mhysa.fromArray(["a", "AA"])
.pipe(Mhysa.flatMap(s => new Array(s.length).fill(s)))
.pipe(Mhysa.join(","))
strom.fromArray(["a", "AA"])
.pipe(strom.flatMap(s => new Array(s.length).fill(s)))
.pipe(strom.join(","))
.pipe(process.stdout);

View File

@@ -1,5 +1,5 @@
const Mhysa = require("mhysa");
const strom = require("strom").strom();
Mhysa.fromArray(["a", "b", "c"])
.pipe(Mhysa.join(","))
strom.fromArray(["a", "b", "c"])
.pipe(strom.join(","))
.pipe(process.stdout);

View File

@@ -1,7 +1,7 @@
const Mhysa = require("mhysa");
const strom = require("strom").strom();
let f = async () => {
const source = Mhysa.fromArray(["a", "b", "c"]);
console.log(await Mhysa.last(source));
const source = strom.fromArray(["a", "b", "c"]);
console.log(await strom.last(source));
};
f();

View File

@@ -1,6 +1,6 @@
const Mhysa = require("mhysa");
const strom = require("strom").strom();
Mhysa.fromArray(["a", "b"])
.pipe(Mhysa.map(s => s.toUpperCase()))
.pipe(Mhysa.join(","))
strom.fromArray(["a", "b"])
.pipe(strom.map(s => s.toUpperCase()))
.pipe(strom.join(","))
.pipe(process.stdout);

View File

@@ -1,9 +1,9 @@
const { Readable } = require("stream");
const Mhysa = require("mhysa");
const strom = require("strom").strom();
const source1 = new Readable({ read() {} });
const source2 = new Readable({ read() {} });
Mhysa.merge(source1, source2).pipe(process.stdout);
strom.merge(source1, source2).pipe(process.stdout);
source1.push("a1 ");
setTimeout(() => source2.push("c3 "), 10);
setTimeout(() => source1.push("b2 "), 20);

View File

@@ -1,5 +1,5 @@
const Mhysa = require("mhysa");
const strom = require("strom").strom();
Mhysa.fromArray(['{ "a": "b" }'])
.pipe(Mhysa.parse())
strom.fromArray(['{ "a": "b" }'])
.pipe(strom.parse())
.on("data", object => console.log(object));

View File

@@ -1,6 +1,6 @@
const Mhysa = require("mhysa");
const strom = require("strom").strom();
Mhysa.fromArray(["a", "b", "cc"])
.pipe(Mhysa.reduce((acc, s) => ({ ...acc, [s]: s.length }), {}))
.pipe(Mhysa.stringify())
strom.fromArray(["a", "b", "cc"])
.pipe(strom.reduce((acc, s) => ({ ...acc, [s]: s.length }), {}))
.pipe(strom.stringify())
.pipe(process.stdout);

View File

@@ -1,5 +1,5 @@
const Mhysa = require("mhysa");
const strom = require("strom").strom();
Mhysa.fromArray(["a1", "b22", "c333"])
.pipe(Mhysa.replace(/b\d+/, "B"))
strom.fromArray(["a1", "b22", "c333"])
.pipe(strom.replace(/b\d+/, "B"))
.pipe(process.stdout);

View File

@@ -1,6 +1,6 @@
const Mhysa = require("mhysa");
const strom = require("strom").strom();
Mhysa.fromArray(["a,b", "c,d"])
.pipe(Mhysa.split(","))
.pipe(Mhysa.join("|"))
strom.fromArray(["a,b", "c,d"])
.pipe(strom.split(","))
.pipe(strom.join("|"))
.pipe(process.stdout);

View File

@@ -1,5 +1,5 @@
const Mhysa = require("mhysa");
const strom = require("strom").strom();
Mhysa.fromArray([{ a: "b" }])
.pipe(Mhysa.stringify())
strom.fromArray([{ a: "b" }])
.pipe(strom.stringify())
.pipe(process.stdout);

View File

@@ -6,36 +6,45 @@ type DemuxStreams = NodeJS.WritableStream | NodeJS.ReadWriteStream;
export interface DemuxOptions extends DuplexOptions {
remultiplex?: boolean;
purgeIdleInterval?: number;
maxIdleTime?: number;
}
export function demux(
construct: (destKey?: string, chunk?: any) => DemuxStreams,
pipelineConstructor: (
destKey?: string,
chunk?: any,
) => DemuxStreams | DemuxStreams[],
demuxBy: string | ((chunk: any) => string),
options?: DemuxOptions,
): Duplex {
return new Demux(construct, demuxBy, options);
return new Demux(pipelineConstructor, demuxBy, options);
}
class Demux extends Duplex {
private streamsByKey: {
[key: string]: { stream: DemuxStreams; lastWrite: number };
[key: string]: DemuxStreams[];
};
private demuxer: (chunk: any) => string;
private construct: (destKey?: string, chunk?: any) => DemuxStreams;
private pipelineConstructor: (
destKey?: string,
chunk?: any,
) => DemuxStreams[];
private remultiplex: boolean;
private transform: Transform;
private maxIdleTime: number;
constructor(
construct: (destKey?: string) => DemuxStreams,
pipelineConstructor: (
destKey?: string,
chunk?: any,
) => DemuxStreams | DemuxStreams[],
demuxBy: string | ((chunk: any) => string),
options: DemuxOptions = {},
) {
super(options);
this.demuxer =
typeof demuxBy === "string" ? chunk => chunk[demuxBy] : demuxBy;
this.construct = construct;
this.pipelineConstructor = (destKey: string, chunk?: any) => {
const pipeline = pipelineConstructor(destKey, chunk);
return Array.isArray(pipeline) ? pipeline : [pipeline];
};
this.remultiplex =
options.remultiplex === undefined ? true : options.remultiplex;
this.streamsByKey = {};
@@ -46,74 +55,77 @@ class Demux extends Duplex {
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) {
const destKey = this.demuxer(chunk);
if (this.streamsByKey[destKey] === undefined) {
const newPipeline = await this.construct(destKey, chunk);
this.streamsByKey[destKey] = {
stream: newPipeline,
lastWrite: Date.now(),
};
const newPipelines = this.pipelineConstructor(destKey, chunk);
this.streamsByKey[destKey] = newPipelines;
newPipelines.forEach(newPipeline => {
if (this.remultiplex && isReadable(newPipeline)) {
(newPipeline as NodeJS.ReadWriteStream).pipe(this.transform);
(newPipeline as NodeJS.ReadWriteStream).pipe(
this.transform,
);
} else if (this.remultiplex) {
console.error(
`Pipeline construct for ${destKey} does not implement readable interface`,
);
}
} else {
this.streamsByKey[destKey].lastWrite = Date.now();
}
if (!this.streamsByKey[destKey].stream.write(chunk, encoding)) {
this.streamsByKey[destKey].stream.once("drain", () => {
cb();
});
} else {
cb();
}
const pipelines = this.streamsByKey[destKey];
const pendingDrains: Array<Promise<any>> = [];
pipelines.forEach(pipeline => {
if (!pipeline.write(chunk, encoding)) {
pendingDrains.push(
new Promise(resolve => {
pipeline.once("drain", () => {
resolve();
});
}),
);
}
});
await Promise.all(pendingDrains);
cb();
}
public _flush() {
const pipelines = Object.values(this.streamsByKey);
let totalEnded = 0;
pipelines.forEach(({ stream }) => {
stream.once("end", () => {
totalEnded++;
if (pipelines.length === totalEnded) {
const pipelines: DemuxStreams[] = Array.prototype.concat.apply(
[],
Object.values(this.streamsByKey),
);
const flushPromises: Array<Promise<void>> = [];
pipelines.forEach(pipeline => {
flushPromises.push(
new Promise(resolve => {
pipeline.once("end", () => {
resolve();
});
}),
);
});
pipelines.forEach(pipeline => pipeline.end());
Promise.all(flushPromises).then(() => {
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());
const pipelines: DemuxStreams[] = [].concat.apply(
[],
Object.values(this.streamsByKey),
);
pipelines.forEach(p => (p as any).destroy());
cb(error);
}
}

View File

@@ -28,7 +28,7 @@ import { unbatch } from "./unbatch";
import { compose } from "./compose";
import { demux } from "./demux";
export default function mhysa(defaultOptions?: TransformOptions) {
export function strom(defaultOptions: TransformOptions = { objectMode: true }) {
function withDefaultOptions<T extends any[], R>(
n: number,
fn: (...args: T) => R,
@@ -256,14 +256,11 @@ export default function mhysa(defaultOptions?: TransformOptions) {
/**
* 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 {String | Function} demuxBy
* @param {string} 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 {Object} options Demux 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
* @param construct Constructor for new output source. Should return a Writable or ReadWrite stream.
* @param demuxBy
* @param demuxBy.key? Key to fetch value from source chunks to demultiplex source.
* @param demuxBy.keyBy? Function to fetch value from source chunks to demultiplex source.
* @param options Writable stream options
*/
demux: withDefaultOptions(2, demux),
};

View File

@@ -1,6 +1,2 @@
import mhysa from "./functions";
import * as _utils from "./utils";
export default mhysa;
// @TODO fix this with proper import export
export const utils = { ..._utils };
export { strom } from "./functions";
export * from "./utils";

View File

@@ -1,10 +1,10 @@
import test from "ava";
import { expect } from "chai";
import { Readable } from "stream";
import mhysa from "../src";
import { strom } from "../src";
import { FlushStrategy } from "../src/functions/accumulator";
import { performance } from "perf_hooks";
const { accumulator, accumulatorBy } = mhysa({ objectMode: true });
const { accumulator, accumulatorBy } = strom({ objectMode: true });
test.cb("accumulator() rolling", t => {
t.plan(3);
@@ -14,8 +14,14 @@ test.cb("accumulator() rolling", t => {
key: string;
}
const source = new Readable({ objectMode: true });
const firstFlush = [{ ts: 0, key: "a" }, { ts: 1, key: "b" }];
const secondFlush = [{ ts: 2, key: "d" }, { ts: 3, key: "e" }];
const firstFlush = [
{ ts: 0, key: "a" },
{ ts: 1, key: "b" },
];
const secondFlush = [
{ ts: 2, key: "d" },
{ ts: 3, key: "e" },
];
const thirdFlush = [{ ts: 4, key: "f" }];
const flushes = [firstFlush, secondFlush, thirdFlush];
@@ -88,7 +94,10 @@ test.cb(
"nonExistingKey",
{ objectMode: true },
);
const input = [{ ts: 0, key: "a" }, { ts: 1, key: "b" }];
const input = [
{ ts: 0, key: "a" },
{ ts: 1, key: "b" },
];
source
.pipe(accumulatorStream)
@@ -182,7 +191,10 @@ test.cb("accumulator() sliding", t => {
{ ts: 4, key: "d" },
];
const firstFlush = [{ ts: 0, key: "a" }];
const secondFlush = [{ ts: 0, key: "a" }, { ts: 1, key: "b" }];
const secondFlush = [
{ ts: 0, key: "a" },
{ ts: 1, key: "b" },
];
const thirdFlush = [
{ ts: 0, key: "a" },
{ ts: 1, key: "b" },
@@ -232,7 +244,10 @@ test.cb("accumulator() sliding with key", t => {
{ ts: 6, key: "g" },
];
const firstFlush = [{ ts: 0, key: "a" }];
const secondFlush = [{ ts: 0, key: "a" }, { ts: 1, key: "b" }];
const secondFlush = [
{ ts: 0, key: "a" },
{ ts: 1, key: "b" },
];
const thirdFlush = [
{ ts: 0, key: "a" },
{ ts: 1, key: "b" },
@@ -243,8 +258,14 @@ test.cb("accumulator() sliding with key", t => {
{ ts: 2, key: "c" },
{ ts: 3, key: "d" },
];
const fifthFlush = [{ ts: 3, key: "d" }, { ts: 5, key: "f" }];
const sixthFlush = [{ ts: 5, key: "f" }, { ts: 6, key: "g" }];
const fifthFlush = [
{ ts: 3, key: "d" },
{ ts: 5, key: "f" },
];
const sixthFlush = [
{ ts: 5, key: "f" },
{ ts: 6, key: "g" },
];
const flushes = [
firstFlush,
@@ -286,7 +307,10 @@ test.cb(
"nonExistingKey",
{ objectMode: true },
);
const input = [{ ts: 0, key: "a" }, { ts: 1, key: "b" }];
const input = [
{ ts: 0, key: "a" },
{ ts: 1, key: "b" },
];
source
.pipe(accumulatorStream)
@@ -334,10 +358,22 @@ test.cb(
{ ts: 6, key: "g" },
];
const firstFlush = [{ ts: 0, key: "a" }];
const secondFlush = [{ ts: 0, key: "a" }, { ts: 2, key: "c" }];
const thirdFlush = [{ ts: 2, key: "c" }, { ts: 3, key: "d" }];
const fourthFlush = [{ ts: 3, key: "d" }, { ts: 5, key: "f" }];
const fifthFlush = [{ ts: 5, key: "f" }, { ts: 6, key: "g" }];
const secondFlush = [
{ ts: 0, key: "a" },
{ ts: 2, key: "c" },
];
const thirdFlush = [
{ ts: 2, key: "c" },
{ ts: 3, key: "d" },
];
const fourthFlush = [
{ ts: 3, key: "d" },
{ ts: 5, key: "f" },
];
const fifthFlush = [
{ ts: 5, key: "f" },
{ ts: 6, key: "g" },
];
const flushes = [
firstFlush,
@@ -469,7 +505,10 @@ test.cb("accumulatorBy() sliding", t => {
{ ts: 6, key: "g" },
];
const firstFlush = [{ ts: 0, key: "a" }];
const secondFlush = [{ ts: 0, key: "a" }, { ts: 1, key: "b" }];
const secondFlush = [
{ ts: 0, key: "a" },
{ ts: 1, key: "b" },
];
const thirdFlush = [
{ ts: 0, key: "a" },
{ ts: 1, key: "b" },
@@ -480,8 +519,14 @@ test.cb("accumulatorBy() sliding", t => {
{ ts: 2, key: "c" },
{ ts: 3, key: "d" },
];
const fifthFlush = [{ ts: 3, key: "d" }, { ts: 5, key: "f" }];
const sixthFlush = [{ ts: 5, key: "f" }, { ts: 6, key: "g" }];
const fifthFlush = [
{ ts: 3, key: "d" },
{ ts: 5, key: "f" },
];
const sixthFlush = [
{ ts: 5, key: "f" },
{ ts: 6, key: "g" },
];
const flushes = [
firstFlush,

View File

@@ -1,8 +1,8 @@
import { Readable } from "stream";
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { batch, map, fromArray } = mhysa({ objectMode: true });
import { strom } from "../src";
const { batch, map, fromArray } = strom({ objectMode: true });
test.cb("batch() batches chunks together", t => {
t.plan(3);

View File

@@ -2,8 +2,8 @@ import * as cp from "child_process";
import { Readable } from "stream";
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { child } = mhysa();
import { strom } from "../src";
const { child } = strom();
test.cb(
"child() allows easily writing to child process stdin and reading from its stdout",

View File

@@ -1,8 +1,8 @@
import { Readable } from "stream";
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { collect } = mhysa();
import { strom } from "../src";
const { collect } = strom();
test.cb(
"collect() collects streamed elements into an array (object, flowing mode)",
@@ -59,7 +59,7 @@ test.cb(
const source = new Readable({ objectMode: false });
source
.pipe(collect())
.pipe(collect({ objectMode: false }))
.on("data", collected => {
expect(collected).to.deep.equal(Buffer.from("abc"));
t.pass();

View File

@@ -2,9 +2,9 @@ import * as test from "ava";
import { expect } from "chai";
import { sleep } from "../src/helpers";
import { Readable, Writable } from "stream";
import mhysa from "../src";
import { strom } from "../src";
import { performance } from "perf_hooks";
const { compose, map, fromArray } = mhysa({ objectMode: true });
const { compose, map, fromArray } = strom({ objectMode: true });
test.cb("compose() chains two streams together in the correct order", t => {
t.plan(3);
@@ -114,13 +114,9 @@ test("compose() writable length should be less than highWaterMark when handing w
return chunk;
});
const composed = compose(
[first, second],
undefined,
{
const composed = compose([first, second], undefined, {
highWaterMark: 2,
},
);
});
composed.on("error", err => {
reject();
});
@@ -168,13 +164,9 @@ test("compose() should emit drain event ~rate * highWaterMark ms for every write
return chunk;
});
const composed = compose(
[first, second],
undefined,
{
const composed = compose([first, second], undefined, {
highWaterMark,
},
);
});
composed.on("error", err => {
reject();
});
@@ -221,13 +213,9 @@ test.cb(
return chunk;
});
const composed = compose(
[first, second],
undefined,
{
const composed = compose([first, second], undefined, {
highWaterMark: 5,
},
);
});
composed.on("error", err => {
t.end(err);
@@ -284,13 +272,9 @@ test.cb(
{ highWaterMark: 1 },
);
const composed = compose(
[first, second],
undefined,
{
const composed = compose([first, second], undefined, {
highWaterMark: 5,
},
);
});
composed.on("error", err => {
t.end(err);
});
@@ -355,13 +339,9 @@ test.cb(
{ highWaterMark: 2 },
);
const composed = compose(
[first, second],
undefined,
{
const composed = compose([first, second], undefined, {
highWaterMark: 5,
},
);
});
composed.on("error", err => {
t.end(err);
});
@@ -414,13 +394,9 @@ test.cb(
return chunk;
});
const composed = compose(
[first, second],
undefined,
{
const composed = compose([first, second], undefined, {
highWaterMark: 6,
},
);
});
composed.on("error", err => {
t.end(err);
@@ -470,12 +446,9 @@ test.cb("compose() should be 'destroyable'", t => {
return chunk;
});
const composed = compose(
[first, second],
(err: any) => {
const composed = compose([first, second], (err: any) => {
t.pass();
},
);
});
const fakeSource = new Readable({
objectMode: true,
@@ -531,13 +504,9 @@ test.cb("compose() `finish` and `end` propagates", t => {
return chunk;
});
const composed = compose(
[first, second],
undefined,
{
const composed = compose([first, second], undefined, {
highWaterMark: 3,
},
);
});
const fakeSource = new Readable({
objectMode: true,

View File

@@ -1,8 +1,8 @@
import { Readable } from "stream";
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { concat, collect } = mhysa();
import { strom } from "../src";
const { concat, collect } = strom();
test.cb(
"concat() concatenates multiple readable streams (object, flowing mode)",
@@ -172,7 +172,7 @@ test.cb(
test.cb("concat() concatenates empty list of readable streams", t => {
t.plan(0);
concat()
.pipe(collect())
.pipe(collect({ objectMode: false }))
.on("data", _ => {
t.fail();
})

View File

@@ -1,11 +1,11 @@
import { Readable } from "stream";
import test from "ava";
import mhysa from "../src";
import { strom } from "../src";
const withDefaultOptions = mhysa({ objectMode: true });
const withoutOptions = mhysa();
const withDefaultOptions = strom({ objectMode: true });
const withoutOptions = strom();
test("Mhysa instances can have default options", t => {
test("strom instances can have default options", t => {
let batch = withDefaultOptions.batch();
t.true(batch._readableState.objectMode);
t.true(batch._writableState.objectMode);

View File

@@ -1,11 +1,11 @@
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
import { strom } from "../src";
import { Writable, Readable } from "stream";
const sinon = require("sinon");
const { sleep } = require("../src/helpers");
import * as sinon from "sinon";
import { sleep } from "../src/helpers";
import { performance } from "perf_hooks";
const { demux, map, fromArray } = mhysa({ objectMode: true });
const { demux, map, fromArray } = strom({ objectMode: true });
interface Test {
key: string;
@@ -862,39 +862,3 @@ test.cb("demux() should be 'destroyable'", t => {
fakeSource.push(input[3]);
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);
}
});

View File

@@ -2,8 +2,8 @@ import * as cp from "child_process";
import { Readable } from "stream";
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { duplex } = mhysa();
import { strom } from "../src";
const { duplex } = strom();
test.cb(
"duplex() combines a writable and readable stream into a ReadWrite stream",

View File

@@ -1,8 +1,8 @@
import test from "ava";
import { expect } from "chai";
import { Readable } from "stream";
import mhysa from "../src";
const { filter } = mhysa();
import { strom } from "../src";
const { filter } = strom();
test.cb("filter() filters elements synchronously", t => {
t.plan(2);

View File

@@ -1,8 +1,8 @@
import { Readable } from "stream";
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { flatMap } = mhysa({ objectMode: true });
import { strom } from "../src";
const { flatMap } = strom({ objectMode: true });
test.cb("flatMap() maps elements synchronously", t => {
t.plan(6);

View File

@@ -1,7 +1,7 @@
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { fromArray } = mhysa();
import { strom } from "../src";
const { fromArray } = strom();
test.cb("fromArray() streams array elements in flowing mode", t => {
t.plan(3);

View File

@@ -1,8 +1,8 @@
import { Readable } from "stream";
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { join } = mhysa();
import { strom } from "../src";
const { join } = strom();
test.cb("join() joins chunks using the specified separator", t => {
t.plan(9);

View File

@@ -1,8 +1,8 @@
import { Readable } from "stream";
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { last } = mhysa();
import { strom } from "../src";
const { last } = strom();
test("last() resolves to the last chunk streamed by the given readable stream", async t => {
const source = new Readable({ objectMode: true });

View File

@@ -1,8 +1,8 @@
import { Readable } from "stream";
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { map } = mhysa();
import { strom } from "../src";
const { map } = strom();
test.cb("map() maps elements synchronously", t => {
t.plan(3);

View File

@@ -1,8 +1,8 @@
import { Readable } from "stream";
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { merge } = mhysa();
import { strom } from "../src";
const { merge } = strom();
test.cb(
"merge() merges multiple readable streams in chunk arrival order",

View File

@@ -2,9 +2,9 @@ import { Readable } from "stream";
import { performance } from "perf_hooks";
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
import { strom } from "../src";
import { sleep } from "../src/helpers";
const { parallelMap } = mhysa({ objectMode: true });
const { parallelMap } = strom({ objectMode: true });
test.cb("parallelMap() parallel mapping", t => {
t.plan(6);

View File

@@ -1,8 +1,8 @@
import { Readable, finished } from "stream";
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { parse } = mhysa();
import { strom } from "../src";
const { parse } = strom();
test.cb("parse() parses the streamed elements as JSON", t => {
t.plan(3);

View File

@@ -2,8 +2,8 @@ import { Readable } from "stream";
import { performance } from "perf_hooks";
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { rate } = mhysa({ objectMode: true });
import { strom } from "../src";
const { rate } = strom({ objectMode: true });
test.cb("rate() sends data at a rate of 150", t => {
t.plan(5);

View File

@@ -1,8 +1,8 @@
import { Readable } from "stream";
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { reduce } = mhysa({ objectMode: true });
import { strom } from "../src";
const { reduce } = strom({ objectMode: true });
test.cb("reduce() reduces elements synchronously", t => {
t.plan(1);

View File

@@ -1,8 +1,8 @@
import { Readable } from "stream";
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { replace } = mhysa();
import { strom } from "../src";
const { replace } = strom();
test.cb(
"replace() replaces occurrences of the given string in the streamed elements with the specified " +

View File

@@ -1,8 +1,8 @@
import { Readable } from "stream";
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { split } = mhysa();
import { strom } from "../src";
const { split } = strom();
test.cb("split() splits chunks using the default separator (\\n)", t => {
t.plan(5);

View File

@@ -1,8 +1,8 @@
import { Readable } from "stream";
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { stringify } = mhysa();
import { strom } from "../src";
const { stringify } = strom();
test.cb("stringify() stringifies the streamed elements as JSON", t => {
t.plan(4);

View File

@@ -1,8 +1,8 @@
import { Readable } from "stream";
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { unbatch, batch } = mhysa({ objectMode: true });
import { strom } from "../src";
const { unbatch, batch } = strom({ objectMode: true });
test.cb("unbatch() unbatches", t => {
t.plan(3);

View File

@@ -1,7 +1,7 @@
import test from "ava";
import { collected } from "../../src/utils";
import mhysa from "../../src";
const { fromArray, collect } = mhysa({ objectMode: true });
import { strom } from "../../src";
const { fromArray, collect } = strom({ objectMode: true });
test("collected returns a promise for the first data point", async t => {
const data = collected(fromArray([1, 2, 3, 4]).pipe(collect()));