Fix tests for Node 15

This commit is contained in:
2021-02-05 22:31:16 -05:00
parent 0e703d0778
commit 4b6c31b9c1
11 changed files with 861 additions and 1666 deletions

View File

@@ -13,9 +13,9 @@
"url": "https://github.com/Wenzil" "url": "https://github.com/Wenzil"
}, },
{ {
"name": "Jerry Kurian", "name": "Jerry Kurian",
"email": "jerrykurian@protonmail.com", "email": "jerrykurian@protonmail.com",
"url": "https://github.com/jkurian" "url": "https://github.com/jkurian"
}, },
{ {
"name": "Lewis Diamond", "name": "Lewis Diamond",
@@ -42,29 +42,26 @@
}, },
"dependencies": {}, "dependencies": {},
"devDependencies": { "devDependencies": {
"@types/chai": "^4.1.7", "@ava/typescript": "^1.1.1",
"@types/node": "^12.12.15", "@types/chai": "^4.2.14",
"@types/sinon": "^7.0.13", "@types/node": "^14.14.25",
"ava": "^2.4.0", "@types/sinon": "^9.0.10",
"chai": "^4.2.0", "ava": "^3.15.0",
"chai": "^4.3.0",
"prettier": "^2.2.1",
"sinon": "^9.2.4",
"stromjs": "./", "stromjs": "./",
"prettier": "^1.14.3", "ts-node": "^9.1.1",
"sinon": "^7.4.2", "tslint": "^6.1.3",
"ts-node": "^8.3.0",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.16.0", "tslint-config-prettier": "^1.16.0",
"tslint-plugin-prettier": "^2.0.1", "tslint-plugin-prettier": "^2.3.0",
"typescript": "^3.5.3" "typescript": "^4.1.3"
}, },
"ava": { "ava": {
"files": [ "files": [
"tests/*.spec.ts", "tests/*.spec.ts",
"tests/utils/*.spec.ts" "tests/utils/*.spec.ts"
], ],
"sources": [
"src/**/*.ts"
],
"compileEnhancements": false,
"failWithoutAssertions": false, "failWithoutAssertions": false,
"extensions": [ "extensions": [
"ts" "ts"

View File

@@ -8,7 +8,7 @@ strom
.fromArray([1, 2, 3, 4, 6, 8]) .fromArray([1, 2, 3, 4, 6, 8])
.pipe( .pipe(
strom.parallelMap(async d => { strom.parallelMap(async d => {
await sleep(10000 - d * 1000); await sleep(1000 - d * 100);
return `${d}`; return `${d}`;
}, 3), }, 3),
) )

View File

@@ -1,5 +1,5 @@
export interface WithEncoding { export interface WithEncoding {
encoding: string; encoding: BufferEncoding;
} }
export enum SerializationFormats { export enum SerializationFormats {

View File

@@ -1,10 +1,8 @@
import { AllStreams, isReadable } from "../helpers"; import { isReadable, isWritable } from "../helpers";
import { PassThrough, pipeline, TransformOptions, Transform } from "stream"; import { PassThrough, pipeline, TransformOptions, Transform, Stream, Readable, Writable } from "stream";
export function compose( export function compose(
streams: Array< streams: (Readable | Writable)[],
NodeJS.ReadableStream | NodeJS.ReadWriteStream | NodeJS.WritableStream
>,
errorCallback?: (err: any) => void, errorCallback?: (err: any) => void,
options?: TransformOptions, options?: TransformOptions,
): Compose { ): Compose {
@@ -15,30 +13,30 @@ export function compose(
return new Compose(streams, errorCallback, options); return new Compose(streams, errorCallback, options);
} }
enum EventSubscription {
Last = 0,
First,
All,
Self,
}
export class Compose extends Transform { export class Compose extends Transform {
private first: AllStreams; private first: Writable & Readable;
private last: AllStreams; private last: Readable;
private streams: AllStreams[]; private streams: Stream[];
private inputStream: ReadableStream;
constructor( constructor(
streams: AllStreams[], streams: (Readable | Writable)[],
errorCallback?: (err: any) => void, errorCallback?: (err: any) => void,
options?: TransformOptions, options?: TransformOptions,
) { ) {
super(options); super(options);
this.first = new PassThrough(options); this.first = new PassThrough(options);
this.last = streams[streams.length - 1]; const last = streams.pop();
if (last && isReadable(last)) {
this.last = last;
} else {
throw new TypeError("Invalid last stream provided, it must be readable");
}
this.streams = streams; this.streams = streams;
pipeline( pipeline(
[this.first, ...streams], [this.first,
...streams,
this.last],
errorCallback || errorCallback ||
((error: any) => { ((error: any) => {
if (error) { if (error) {
@@ -48,10 +46,10 @@ export class Compose extends Transform {
); );
if (isReadable(this.last)) { if (isReadable(this.last)) {
(this.last as NodeJS.ReadWriteStream).pipe( this.last.pipe(
new Transform({ new Transform({
...options, ...options,
transform: (d: any, encoding, cb) => { transform: (d: any, _encoding, cb) => {
this.push(d); this.push(d);
cb(); cb();
}, },
@@ -60,13 +58,13 @@ export class Compose extends Transform {
} }
} }
public _transform(chunk: any, encoding: string, cb: any) { public _transform(chunk: any, encoding: BufferEncoding, cb: any) {
(this.first as NodeJS.WritableStream).write(chunk, encoding, cb); this.first.write(chunk, encoding, cb);
} }
public _flush(cb: any) { public _flush(cb: any) {
if (isReadable(this.first)) { if (isWritable(this.first)) {
(this.first as any).push(null); this.first.push(null);
} }
this.last.once("end", () => { this.last.once("end", () => {
cb(); cb();

View File

@@ -1,9 +1,7 @@
import { DuplexOptions, Duplex, Transform } from "stream"; import { DuplexOptions, Duplex, Transform, Writable } from "stream";
import { isReadable } from "../helpers"; import { isReadable } from "../helpers";
type DemuxStreams = NodeJS.WritableStream | NodeJS.ReadWriteStream;
export interface DemuxOptions extends DuplexOptions { export interface DemuxOptions extends DuplexOptions {
remultiplex?: boolean; remultiplex?: boolean;
} }
@@ -12,7 +10,7 @@ export function demux(
pipelineConstructor: ( pipelineConstructor: (
destKey?: string, destKey?: string,
chunk?: any, chunk?: any,
) => DemuxStreams | DemuxStreams[], ) => Writable | Writable[],
demuxBy: string | ((chunk: any) => string), demuxBy: string | ((chunk: any) => string),
options?: DemuxOptions, options?: DemuxOptions,
): Duplex { ): Duplex {
@@ -21,20 +19,20 @@ export function demux(
class Demux extends Duplex { class Demux extends Duplex {
private streamsByKey: { private streamsByKey: {
[key: string]: DemuxStreams[]; [key: string]: Writable[];
}; };
private demuxer: (chunk: any) => string; private demuxer: (chunk: any) => string;
private pipelineConstructor: ( private pipelineConstructor: (
destKey?: string, destKey?: string,
chunk?: any, chunk?: any,
) => DemuxStreams[]; ) => Writable[];
private remultiplex: boolean; private remultiplex: boolean;
private transform: Transform; private transform: Transform;
constructor( constructor(
pipelineConstructor: ( pipelineConstructor: (
destKey?: string, destKey?: string,
chunk?: any, chunk?: any,
) => DemuxStreams | DemuxStreams[], ) => Writable | Writable[],
demuxBy: string | ((chunk: any) => string), demuxBy: string | ((chunk: any) => string),
options: DemuxOptions = {}, options: DemuxOptions = {},
) { ) {
@@ -60,7 +58,7 @@ class Demux extends Duplex {
} }
// tslint:disable-next-line // tslint:disable-next-line
public _read(size: number) {} 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);
@@ -70,7 +68,7 @@ class Demux extends Duplex {
newPipelines.forEach(newPipeline => { newPipelines.forEach(newPipeline => {
if (this.remultiplex && isReadable(newPipeline)) { if (this.remultiplex && isReadable(newPipeline)) {
(newPipeline as NodeJS.ReadWriteStream).pipe( newPipeline.pipe(
this.transform, this.transform,
); );
} else if (this.remultiplex) { } else if (this.remultiplex) {
@@ -88,7 +86,7 @@ class Demux extends Duplex {
pendingDrains.push( pendingDrains.push(
new Promise(resolve => { new Promise(resolve => {
pipeline.once("drain", () => { pipeline.once("drain", () => {
resolve(); resolve(null);
}); });
}), }),
); );
@@ -99,7 +97,7 @@ class Demux extends Duplex {
} }
public _flush() { public _flush() {
const pipelines: DemuxStreams[] = Array.prototype.concat.apply( const pipelines: Writable[] = Array.prototype.concat.apply(
[], [],
Object.values(this.streamsByKey), Object.values(this.streamsByKey),
); );
@@ -121,7 +119,7 @@ class Demux extends Duplex {
} }
public _destroy(error: any, cb: (error?: any) => void) { public _destroy(error: any, cb: (error?: any) => void) {
const pipelines: DemuxStreams[] = [].concat.apply( const pipelines: Writable[] = [].concat.apply(
[], [],
Object.values(this.streamsByKey), Object.values(this.streamsByKey),
); );

View File

@@ -1,9 +1,4 @@
import { import { TransformOptions } from "stream";
Transform,
TransformOptions,
WritableOptions,
ReadableOptions,
} from "stream";
import { accumulator, accumulatorBy } from "./accumulator"; import { accumulator, accumulatorBy } from "./accumulator";
import { batch } from "./batch"; import { batch } from "./batch";
import { child } from "./child"; import { child } from "./child";

View File

@@ -9,7 +9,7 @@ export function replace(
const decoder = new StringDecoder(options.encoding); const decoder = new StringDecoder(options.encoding);
return new Transform({ return new Transform({
readableObjectMode: true, readableObjectMode: true,
transform(chunk: Buffer, encoding, callback) { transform(chunk: Buffer, _encoding, callback) {
const asString = decoder.write(chunk); const asString = decoder.write(chunk);
// Take care not to break up multi-byte characters spanning multiple chunks // Take care not to break up multi-byte characters spanning multiple chunks
if (asString !== "" || chunk.length === 0) { if (asString !== "" || chunk.length === 0) {

View File

@@ -1,17 +1,23 @@
import { Readable, Stream, Writable } from "stream";
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( export function isReadable(
stream: AllStreams, stream: Stream,
): stream is NodeJS.WritableStream { ): stream is Readable {
return ( return (
(stream as NodeJS.ReadableStream).pipe !== undefined && (stream as Readable).pipe !== undefined &&
(stream as any).readable === true (stream as Readable).readable === true
);
}
export function isWritable(
stream: Stream,
): stream is Writable {
return (
(stream as Writable).write !== undefined &&
(stream as Writable).writable === true
); );
} }

View File

@@ -144,7 +144,7 @@ test("compose() writable length should be less than highWaterMark when handing w
}); });
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) => {
@@ -529,10 +529,10 @@ test.cb("compose() `finish` and `end` propagates", t => {
}); });
composed.on("end", () => { composed.on("end", () => {
t.pass(); t.pass();
t.end();
}); });
sink.on("finish", () => { sink.on("finish", () => {
t.pass(); t.pass();
t.end();
}); });
const input = [ const input = [

View File

@@ -21,13 +21,11 @@ test.cb("demux() constructor should be called once per key", t => {
{ key: "a", visited: [] }, { key: "a", visited: [] },
{ key: "b", visited: [] }, { key: "b", visited: [] },
]; ];
const construct = sinon.spy((destKey: string) => { const construct = sinon.spy((_destKey: string) => {
const dest = map((chunk: Test) => { return map((chunk: Test) => {
chunk.visited.push(1); chunk.visited.push(1);
return chunk; return chunk;
}); });
return dest;
}); });
const demuxed = demux(construct, "key", {}); const demuxed = demux(construct, "key", {});
@@ -124,13 +122,11 @@ test.cb("demux() constructor should be called once per key using keyBy", t => {
{ key: "b", visited: [] }, { key: "b", visited: [] },
]; ];
const construct = sinon.spy((destKey: string) => { const construct = sinon.spy((_destKey: string) => {
const dest = map((chunk: Test) => { return map((chunk: Test) => {
chunk.visited.push(1); chunk.visited.push(1);
return chunk; return chunk;
}); });
return dest;
}); });
const demuxed = demux(construct, item => item.key, {}); const demuxed = demux(construct, item => item.key, {});
@@ -206,7 +202,7 @@ test("demux() write should return false and emit drain if more than highWaterMar
let pendingReads = input.length; let pendingReads = input.length;
const highWaterMark = 5; const highWaterMark = 5;
const slowProcessorSpeed = 25; const slowProcessorSpeed = 25;
const construct = (destKey: string) => { const construct = (_destKey: string) => {
const first = map( const first = map(
async (chunk: Chunk) => { async (chunk: Chunk) => {
await sleep(slowProcessorSpeed); await sleep(slowProcessorSpeed);
@@ -231,7 +227,7 @@ test("demux() write should return false and emit drain if more than highWaterMar
highWaterMark, highWaterMark,
}); });
_demux.on("error", err => { _demux.on("error", _err => {
reject(); reject();
}); });
@@ -239,7 +235,7 @@ test("demux() write should return false and emit drain if more than highWaterMar
const res = _demux.write(item); const res = _demux.write(item);
expect(_demux._writableState.length).to.be.at.most(highWaterMark); expect(_demux._writableState.length).to.be.at.most(highWaterMark);
if (!res) { if (!res) {
await new Promise((resolv, rej) => { await new Promise((resolv, _rej) => {
_demux.once("drain", () => { _demux.once("drain", () => {
expect(_demux._writableState.length).to.be.equal(0); expect(_demux._writableState.length).to.be.equal(0);
t.pass(); t.pass();
@@ -270,7 +266,7 @@ test("demux() should emit one drain event after slowProcessorSpeed * highWaterMa
const highWaterMark = 5; const highWaterMark = 5;
const slowProcessorSpeed = 25; const slowProcessorSpeed = 25;
const construct = (destKey: string) => { const construct = (_destKey: string) => {
const first = map( const first = map(
async (chunk: Chunk) => { async (chunk: Chunk) => {
await sleep(slowProcessorSpeed); await sleep(slowProcessorSpeed);
@@ -292,7 +288,7 @@ test("demux() should emit one drain event after slowProcessorSpeed * highWaterMa
const _demux = demux(construct, "key", { const _demux = demux(construct, "key", {
highWaterMark, highWaterMark,
}); });
_demux.on("error", err => { _demux.on("error", _err => {
reject(); reject();
}); });
@@ -300,14 +296,14 @@ test("demux() should emit one drain event after slowProcessorSpeed * highWaterMa
for (const item of input) { for (const item of input) {
const res = _demux.write(item); const res = _demux.write(item);
if (!res) { if (!res) {
await new Promise((resolv, rej) => { await new Promise((resolv, _rej) => {
// This event should be received after all items in demux are processed // This event should be received after all items in demux are processed
_demux.once("drain", () => { _demux.once("drain", () => {
expect(performance.now() - start).to.be.greaterThan( expect(performance.now() - start).to.be.greaterThan(
slowProcessorSpeed * highWaterMark, slowProcessorSpeed * highWaterMark,
); );
t.pass(); t.pass();
resolv(); resolv(null);
}); });
}); });
} }
@@ -332,7 +328,7 @@ test("demux() should emit one drain event when writing 6 items with highWaterMar
{ key: "a", mapped: [] }, { key: "a", mapped: [] },
]; ];
let pendingReads = input.length; let pendingReads = input.length;
const construct = (destKey: string) => { const construct = (_destKey: string) => {
const first = map( const first = map(
async (chunk: Chunk) => { async (chunk: Chunk) => {
await sleep(50); await sleep(50);
@@ -354,7 +350,7 @@ test("demux() should emit one drain event when writing 6 items with highWaterMar
highWaterMark: 5, highWaterMark: 5,
}); });
_demux.on("error", err => { _demux.on("error", _err => {
reject(); reject();
}); });
@@ -364,7 +360,7 @@ test("demux() should emit one drain event when writing 6 items with highWaterMar
if (!res) { if (!res) {
await new Promise(_resolve => { await new Promise(_resolve => {
_demux.once("drain", () => { _demux.once("drain", () => {
_resolve(); _resolve(null);
expect(_demux._writableState.length).to.be.equal(0); expect(_demux._writableState.length).to.be.equal(0);
t.pass(); t.pass();
}); });
@@ -683,13 +679,7 @@ test.cb("Demux should send data events", t => {
}); });
test.cb("demux() `finish` and `end` propagates", t => { test.cb("demux() `finish` and `end` propagates", t => {
interface Chunk { t.plan(10);
key: string;
mapped: number[];
}
t.plan(9);
const construct = (destKey: string) => { const construct = (destKey: string) => {
const dest = map((chunk: any) => { const dest = map((chunk: any) => {
chunk.mapped.push(destKey); chunk.mapped.push(destKey);
@@ -728,10 +718,10 @@ test.cb("demux() `finish` and `end` propagates", t => {
}); });
_demux.on("end", () => { _demux.on("end", () => {
t.pass(); t.pass();
t.end();
}); });
sink.on("finish", () => { sink.on("finish", () => {
t.pass(); t.pass();
t.end();
}); });
const input = [ const input = [

2343
yarn.lock

File diff suppressed because it is too large Load Diff