@gjsify/stream 0.3.21 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/esm/_virtual/_rolldown/runtime.js +1 -0
- package/lib/esm/consumers/index.js +1 -1
- package/lib/esm/duplex.js +1 -0
- package/lib/esm/index.js +1 -1
- package/lib/esm/internal/state.js +1 -0
- package/lib/esm/internal/types.js +0 -0
- package/lib/esm/passthrough.js +1 -0
- package/lib/esm/promises/index.js +1 -1
- package/lib/esm/readable.js +1 -0
- package/lib/esm/stream-base.js +1 -0
- package/lib/esm/transform.js +1 -0
- package/lib/esm/utils/finished.js +1 -0
- package/lib/esm/utils/pipe.js +1 -0
- package/lib/esm/utils/pipeline.js +1 -0
- package/lib/esm/writable.js +1 -0
- package/lib/types/duplex.d.ts +42 -0
- package/lib/types/index.d.ts +15 -190
- package/lib/types/internal/state.d.ts +4 -0
- package/lib/types/internal/types.d.ts +21 -0
- package/lib/types/passthrough.d.ts +5 -0
- package/lib/types/readable.d.ts +73 -0
- package/lib/types/stream-base.d.ts +26 -0
- package/lib/types/transform.d.ts +11 -0
- package/lib/types/utils/finished.d.ts +14 -0
- package/lib/types/utils/pipe.d.ts +10 -0
- package/lib/types/utils/pipeline.d.ts +7 -0
- package/lib/types/writable.d.ts +47 -0
- package/package.json +55 -51
- package/src/callable.spec.ts +0 -157
- package/src/callable.ts +0 -3
- package/src/consumers/index.spec.ts +0 -107
- package/src/consumers/index.ts +0 -39
- package/src/edge-cases.spec.ts +0 -593
- package/src/index.spec.ts +0 -2252
- package/src/index.ts +0 -1676
- package/src/inheritance.spec.ts +0 -315
- package/src/pipe.spec.ts +0 -530
- package/src/promises/index.spec.ts +0 -140
- package/src/promises/index.ts +0 -31
- package/src/spec-internals.d.ts +0 -28
- package/src/test.mts +0 -26
- package/src/transform.spec.ts +0 -520
- package/src/web/index.ts +0 -32
- package/tsconfig.json +0 -29
- package/tsconfig.tsbuildinfo +0 -1
package/src/inheritance.spec.ts
DELETED
|
@@ -1,315 +0,0 @@
|
|
|
1
|
-
// Stream and util.inherits inheritance tests.
|
|
2
|
-
//
|
|
3
|
-
// Ported from:
|
|
4
|
-
// refs/node-test/parallel/test-stream-inheritance.js
|
|
5
|
-
// refs/node-test/parallel/test-util-inherits.js
|
|
6
|
-
// Original: MIT license, Node.js contributors
|
|
7
|
-
// Modifications: adapted to @gjsify/unit, async/await.
|
|
8
|
-
|
|
9
|
-
import { describe, it, expect } from '@gjsify/unit';
|
|
10
|
-
import { Readable, Writable, Duplex, Transform, PassThrough } from 'node:stream';
|
|
11
|
-
import { inherits } from 'node:util';
|
|
12
|
-
|
|
13
|
-
export default async () => {
|
|
14
|
-
|
|
15
|
-
// -------------------------------------------------------------------------
|
|
16
|
-
// instanceof hierarchy
|
|
17
|
-
// -------------------------------------------------------------------------
|
|
18
|
-
await describe('stream inheritance: instanceof checks', async () => {
|
|
19
|
-
await it('Readable is instanceof Readable only', async () => {
|
|
20
|
-
const r = new Readable({ read() {} });
|
|
21
|
-
expect(r instanceof Readable).toBe(true);
|
|
22
|
-
expect(r instanceof Writable).toBe(false);
|
|
23
|
-
expect(r instanceof Duplex).toBe(false);
|
|
24
|
-
expect(r instanceof Transform).toBe(false);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
await it('Writable is instanceof Writable only', async () => {
|
|
28
|
-
const w = new Writable({ write(_c, _e, cb) { cb(); } });
|
|
29
|
-
expect(w instanceof Readable).toBe(false);
|
|
30
|
-
expect(w instanceof Writable).toBe(true);
|
|
31
|
-
expect(w instanceof Duplex).toBe(false);
|
|
32
|
-
expect(w instanceof Transform).toBe(false);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
await it('Duplex is instanceof Readable and Writable and Duplex', async () => {
|
|
36
|
-
const d = new Duplex({ read() {}, write(_c, _e, cb) { cb(); } });
|
|
37
|
-
expect(d instanceof Readable).toBe(true);
|
|
38
|
-
expect(d instanceof Writable).toBe(true);
|
|
39
|
-
expect(d instanceof Duplex).toBe(true);
|
|
40
|
-
expect(d instanceof Transform).toBe(false);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
await it('Transform is instanceof Readable, Writable, Duplex, and Transform', async () => {
|
|
44
|
-
const t = new Transform({ transform(_c, _e, cb) { cb(); } });
|
|
45
|
-
expect(t instanceof Readable).toBe(true);
|
|
46
|
-
expect(t instanceof Writable).toBe(true);
|
|
47
|
-
expect(t instanceof Duplex).toBe(true);
|
|
48
|
-
expect(t instanceof Transform).toBe(true);
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
await it('null and undefined are not instanceof Writable', async () => {
|
|
52
|
-
// Cast through unknown — TS rejects `null instanceof X` directly.
|
|
53
|
-
expect((null as unknown as object) instanceof Writable).toBe(false);
|
|
54
|
-
expect((undefined as unknown as object) instanceof Writable).toBe(false);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
await it('PassThrough is instanceof Transform, Duplex, Readable, Writable', async () => {
|
|
58
|
-
const pt = new PassThrough();
|
|
59
|
-
expect(pt instanceof PassThrough).toBe(true);
|
|
60
|
-
expect(pt instanceof Transform).toBe(true);
|
|
61
|
-
expect(pt instanceof Duplex).toBe(true);
|
|
62
|
-
expect(pt instanceof Readable).toBe(true);
|
|
63
|
-
expect(pt instanceof Writable).toBe(true);
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
// -------------------------------------------------------------------------
|
|
68
|
-
// Object.setPrototypeOf-based subclass
|
|
69
|
-
// -------------------------------------------------------------------------
|
|
70
|
-
await describe('stream inheritance: Object.setPrototypeOf subclassing', async () => {
|
|
71
|
-
await it('subclass via setPrototypeOf is instanceof parent', async () => {
|
|
72
|
-
function CustomWritable(this: any) {}
|
|
73
|
-
Object.setPrototypeOf(CustomWritable, Writable);
|
|
74
|
-
Object.setPrototypeOf((CustomWritable as any).prototype, Writable.prototype);
|
|
75
|
-
|
|
76
|
-
const cw: any = new (CustomWritable as any)();
|
|
77
|
-
expect(cw instanceof CustomWritable).toBe(true);
|
|
78
|
-
expect(cw instanceof Writable).toBe(true);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
await it('ES6 class extending Writable is instanceof Writable', async () => {
|
|
82
|
-
class MyWritable extends Writable {
|
|
83
|
-
_write(_c: any, _e: any, cb: () => void) { cb(); }
|
|
84
|
-
}
|
|
85
|
-
const mw = new MyWritable();
|
|
86
|
-
expect(mw instanceof Writable).toBe(true);
|
|
87
|
-
expect(mw instanceof MyWritable).toBe(true);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
await it('different Writable subclasses are not cross-instanceof', async () => {
|
|
91
|
-
function CustomWritable(this: any) {}
|
|
92
|
-
Object.setPrototypeOf(CustomWritable, Writable);
|
|
93
|
-
Object.setPrototypeOf((CustomWritable as any).prototype, Writable.prototype);
|
|
94
|
-
|
|
95
|
-
class OtherWritable extends Writable {
|
|
96
|
-
_write(_c: any, _e: any, cb: () => void) { cb(); }
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
expect(new OtherWritable() instanceof (CustomWritable as any)).toBe(false);
|
|
100
|
-
expect(new (CustomWritable as any)() instanceof OtherWritable).toBe(false);
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
// -------------------------------------------------------------------------
|
|
105
|
-
// util.inherits — single and multi-level
|
|
106
|
-
// -------------------------------------------------------------------------
|
|
107
|
-
await describe('util.inherits: single-level inheritance', async () => {
|
|
108
|
-
await it('sets B.super_ to A', async () => {
|
|
109
|
-
function A(this: any) { this._a = 'a'; }
|
|
110
|
-
(A.prototype as any).a = function() { return this._a; };
|
|
111
|
-
|
|
112
|
-
function B(this: any, value: string) {
|
|
113
|
-
(A as any).call(this);
|
|
114
|
-
this._b = value;
|
|
115
|
-
}
|
|
116
|
-
inherits(B as any, A as any);
|
|
117
|
-
|
|
118
|
-
const desc = Object.getOwnPropertyDescriptor(B, 'super_');
|
|
119
|
-
expect(desc).toBeDefined();
|
|
120
|
-
expect((desc as any).value).toBe(A);
|
|
121
|
-
expect((desc as any).enumerable).toBe(false);
|
|
122
|
-
expect((desc as any).configurable).toBe(true);
|
|
123
|
-
expect((desc as any).writable).toBe(true);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
await it('instance methods from parent are accessible on child', async () => {
|
|
127
|
-
function A(this: any) { this._a = 'a'; }
|
|
128
|
-
(A.prototype as any).a = function() { return this._a; };
|
|
129
|
-
|
|
130
|
-
function B(this: any, value: string) {
|
|
131
|
-
(A as any).call(this);
|
|
132
|
-
this._b = value;
|
|
133
|
-
}
|
|
134
|
-
inherits(B as any, A as any);
|
|
135
|
-
(B.prototype as any).b = function() { return this._b; };
|
|
136
|
-
|
|
137
|
-
const b: any = new (B as any)('hello');
|
|
138
|
-
expect(b.a()).toBe('a');
|
|
139
|
-
expect(b.b()).toBe('hello');
|
|
140
|
-
expect(b.constructor).toBe(B);
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
await describe('util.inherits: two-level inheritance', async () => {
|
|
145
|
-
await it('C inherits from B inherits from A — all methods accessible', async () => {
|
|
146
|
-
function A(this: any) { this._a = 'a'; }
|
|
147
|
-
(A.prototype as any).a = function() { return this._a; };
|
|
148
|
-
|
|
149
|
-
function B(this: any) { (A as any).call(this); this._b = 'b'; }
|
|
150
|
-
inherits(B as any, A as any);
|
|
151
|
-
(B.prototype as any).b = function() { return this._b; };
|
|
152
|
-
|
|
153
|
-
function C(this: any) { (B as any).call(this); this._c = 'c'; }
|
|
154
|
-
inherits(C as any, B as any);
|
|
155
|
-
(C.prototype as any).c = function() { return this._c; };
|
|
156
|
-
(C.prototype as any).getValue = function() { return this.a() + this.b() + this.c(); };
|
|
157
|
-
|
|
158
|
-
expect((C as any).super_).toBe(B);
|
|
159
|
-
|
|
160
|
-
const c: any = new (C as any)();
|
|
161
|
-
expect(c.getValue()).toBe('abc');
|
|
162
|
-
expect(c.constructor).toBe(C);
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
await describe('util.inherits: prototype methods set before inherits()', async () => {
|
|
167
|
-
await it('prototype methods defined before inherits() are preserved', async () => {
|
|
168
|
-
function A(this: any) { this._a = 'a'; }
|
|
169
|
-
(A.prototype as any).a = function() { return this._a; };
|
|
170
|
-
|
|
171
|
-
function B(this: any) { (A as any).call(this); this._b = 'b'; }
|
|
172
|
-
inherits(B as any, A as any);
|
|
173
|
-
(B.prototype as any).b = function() { return this._b; };
|
|
174
|
-
|
|
175
|
-
function D(this: any) { (B as any).call(this); this._d = 'd'; }
|
|
176
|
-
// Set method BEFORE calling inherits
|
|
177
|
-
(D.prototype as any).d = function() { return this._d; };
|
|
178
|
-
inherits(D as any, B as any);
|
|
179
|
-
|
|
180
|
-
expect((D as any).super_).toBe(B);
|
|
181
|
-
const d: any = new (D as any)();
|
|
182
|
-
expect(d.b()).toBe('b');
|
|
183
|
-
expect(d.d()).toBe('d');
|
|
184
|
-
expect(d.constructor).toBe(D);
|
|
185
|
-
});
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
await describe('util.inherits: invalid arguments', async () => {
|
|
189
|
-
await it('throws ERR_INVALID_ARG_TYPE when superCtor has no prototype', async () => {
|
|
190
|
-
function A(this: any) {}
|
|
191
|
-
let threw = false;
|
|
192
|
-
let code = '';
|
|
193
|
-
try {
|
|
194
|
-
inherits(A as any, {} as any);
|
|
195
|
-
} catch (e: any) {
|
|
196
|
-
threw = true;
|
|
197
|
-
code = e.code;
|
|
198
|
-
}
|
|
199
|
-
expect(threw).toBe(true);
|
|
200
|
-
expect(code).toBe('ERR_INVALID_ARG_TYPE');
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
await it('throws ERR_INVALID_ARG_TYPE when superCtor is null', async () => {
|
|
204
|
-
function A(this: any) {}
|
|
205
|
-
let threw = false;
|
|
206
|
-
let code = '';
|
|
207
|
-
try {
|
|
208
|
-
inherits(A as any, null as any);
|
|
209
|
-
} catch (e: any) {
|
|
210
|
-
threw = true;
|
|
211
|
-
code = e.code;
|
|
212
|
-
}
|
|
213
|
-
expect(threw).toBe(true);
|
|
214
|
-
expect(code).toBe('ERR_INVALID_ARG_TYPE');
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
await it('throws ERR_INVALID_ARG_TYPE when ctor is null', async () => {
|
|
218
|
-
function A(this: any) {}
|
|
219
|
-
(A.prototype as any).a = function() {};
|
|
220
|
-
let threw = false;
|
|
221
|
-
let code = '';
|
|
222
|
-
try {
|
|
223
|
-
inherits(null as any, A as any);
|
|
224
|
-
} catch (e: any) {
|
|
225
|
-
threw = true;
|
|
226
|
-
code = e.code;
|
|
227
|
-
}
|
|
228
|
-
expect(threw).toBe(true);
|
|
229
|
-
expect(code).toBe('ERR_INVALID_ARG_TYPE');
|
|
230
|
-
});
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
// -------------------------------------------------------------------------
|
|
234
|
-
// util.inherits + stream classes — integration
|
|
235
|
-
// -------------------------------------------------------------------------
|
|
236
|
-
await describe('util.inherits + stream: practical subclassing', async () => {
|
|
237
|
-
await it('function-based Readable subclass via inherits works', async () => {
|
|
238
|
-
function NumberSource(this: any, numbers: number[]) {
|
|
239
|
-
Readable.call(this, { objectMode: true });
|
|
240
|
-
this._numbers = numbers.slice();
|
|
241
|
-
}
|
|
242
|
-
inherits(NumberSource as any, Readable as any);
|
|
243
|
-
(NumberSource.prototype as any)._read = function() {
|
|
244
|
-
const n = this._numbers.shift();
|
|
245
|
-
this.push(n !== undefined ? n : null);
|
|
246
|
-
};
|
|
247
|
-
|
|
248
|
-
const collected: number[] = [];
|
|
249
|
-
const src: any = new (NumberSource as any)([10, 20, 30]);
|
|
250
|
-
await new Promise<void>((resolve, reject) => {
|
|
251
|
-
src.on('data', (n: number) => collected.push(n));
|
|
252
|
-
src.on('end', resolve);
|
|
253
|
-
src.on('error', reject);
|
|
254
|
-
});
|
|
255
|
-
expect(collected).toEqualArray([10, 20, 30]);
|
|
256
|
-
expect(src instanceof Readable).toBe(true);
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
await it('function-based Writable subclass via inherits works', async () => {
|
|
260
|
-
function CollectWritable(this: any) {
|
|
261
|
-
Writable.call(this);
|
|
262
|
-
this.collected = [];
|
|
263
|
-
}
|
|
264
|
-
inherits(CollectWritable as any, Writable as any);
|
|
265
|
-
(CollectWritable.prototype as any)._write = function(
|
|
266
|
-
chunk: Buffer,
|
|
267
|
-
_enc: string,
|
|
268
|
-
cb: () => void,
|
|
269
|
-
) {
|
|
270
|
-
this.collected.push(chunk.toString());
|
|
271
|
-
cb();
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
const cw: any = new (CollectWritable as any)();
|
|
275
|
-
await new Promise<void>((resolve, reject) => {
|
|
276
|
-
cw.on('finish', resolve);
|
|
277
|
-
cw.on('error', reject);
|
|
278
|
-
cw.write('hello');
|
|
279
|
-
cw.write('world');
|
|
280
|
-
cw.end();
|
|
281
|
-
});
|
|
282
|
-
expect(cw.collected).toEqualArray(['hello', 'world']);
|
|
283
|
-
expect(cw instanceof Writable).toBe(true);
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
await it('function-based Transform subclass via inherits works', async () => {
|
|
287
|
-
function UpperCase(this: any) {
|
|
288
|
-
Transform.call(this);
|
|
289
|
-
}
|
|
290
|
-
inherits(UpperCase as any, Transform as any);
|
|
291
|
-
(UpperCase.prototype as any)._transform = function(
|
|
292
|
-
chunk: Buffer,
|
|
293
|
-
_enc: string,
|
|
294
|
-
cb: (err: null, data: Buffer) => void,
|
|
295
|
-
) {
|
|
296
|
-
cb(null, Buffer.from(chunk.toString().toUpperCase()));
|
|
297
|
-
};
|
|
298
|
-
|
|
299
|
-
const out: string[] = [];
|
|
300
|
-
const uc: any = new (UpperCase as any)();
|
|
301
|
-
await new Promise<void>((resolve, reject) => {
|
|
302
|
-
uc.on('data', (chunk: Buffer) => out.push(chunk.toString()));
|
|
303
|
-
uc.on('end', resolve);
|
|
304
|
-
uc.on('error', reject);
|
|
305
|
-
uc.write('hello');
|
|
306
|
-
uc.write('world');
|
|
307
|
-
uc.end();
|
|
308
|
-
});
|
|
309
|
-
expect(out).toEqualArray(['HELLO', 'WORLD']);
|
|
310
|
-
expect(uc instanceof Transform).toBe(true);
|
|
311
|
-
expect(uc instanceof Readable).toBe(true);
|
|
312
|
-
expect(uc instanceof Writable).toBe(true);
|
|
313
|
-
});
|
|
314
|
-
});
|
|
315
|
-
};
|