Skip to content

Commit e06358d

Browse files
committed
feat(node:stream): allow overriding Duplex, Readable, Transform and Writable with globalThis
1 parent 5ba2d03 commit e06358d

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

src/runtime/node/stream/duplex.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Writable } from "./writable";
88

99
type DuplexClass = new () => stream.Duplex;
1010

11-
export const Duplex: DuplexClass = class {
11+
export const _Duplex: DuplexClass = class {
1212
allowHalfOpen: boolean = true;
1313
private _destroy: (error?: Error) => void;
1414

@@ -19,5 +19,8 @@ export const Duplex: DuplexClass = class {
1919
}
2020
} as any;
2121

22-
Object.assign(Duplex.prototype, Readable.prototype);
23-
Object.assign(Duplex.prototype, Writable.prototype);
22+
Object.assign(_Duplex.prototype, Readable.prototype);
23+
Object.assign(_Duplex.prototype, Writable.prototype);
24+
25+
export const Duplex: typeof stream.Duplex =
26+
(globalThis as any).Duplex || _Duplex;

src/runtime/node/stream/readable.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { EventEmitter } from "../events";
77
// Implementation: https://github.com/nodejs/node/blob/master/lib/internal/streams/readable.js
88

99
// eslint-disable-next-line unicorn/prefer-event-target
10-
export class Readable extends EventEmitter implements stream.Readable {
10+
export class _Readable extends EventEmitter implements stream.Readable {
1111
__unenv__: unknown = true;
1212

1313
readonly readableEncoding: BufferEncoding | null = null;
@@ -28,7 +28,7 @@ export class Readable extends EventEmitter implements stream.Readable {
2828
_iterable: Iterable<any> | AsyncIterable<any>,
2929
options?: stream.ReadableOptions,
3030
) {
31-
return new Readable(options);
31+
return new _Readable(options);
3232
}
3333

3434
constructor(_opts?: stream.ReadableOptions) {
@@ -92,3 +92,6 @@ export class Readable extends EventEmitter implements stream.Readable {
9292
throw new Error("[h3] Method not implemented.");
9393
}
9494
}
95+
96+
export const Readable: typeof stream.Readable =
97+
(globalThis as any).Readable || _Readable;

src/runtime/node/stream/transform.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Duplex } from "./duplex";
44
// Docs: https://nodejs.org/api/stream.html#stream_duplex_and_transform_streams
55
// Implementation: https://github.com/nodejs/node/blob/master/lib/internal/streams/transform.js
66

7-
export class Transform extends Duplex implements stream.Transform {
7+
export class _Transform extends Duplex implements stream.Transform {
88
readonly __unenv__ = true;
99

1010
_transform(
@@ -15,3 +15,6 @@ export class Transform extends Duplex implements stream.Transform {
1515

1616
_flush(callback: stream.TransformCallback): void {}
1717
}
18+
19+
export const Transform: typeof stream.Transform =
20+
(globalThis as any).Transform || _Transform;

src/runtime/node/stream/writable.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { EventEmitter } from "../events";
66
// Docs: https://nodejs.org/api/stream.html#stream_writable_streams
77
// Implementation: https://github.com/nodejs/node/blob/master/lib/internal/streams/writable.js
88
// eslint-disable-next-line unicorn/prefer-event-target
9-
export class Writable extends EventEmitter implements stream.Writable {
9+
class _Writable extends EventEmitter implements stream.Writable {
1010
readonly __unenv__ = true;
1111

1212
readonly writable: boolean = true;
@@ -133,3 +133,6 @@ export class Writable extends EventEmitter implements stream.Writable {
133133
throw new Error("[h3] Method not implemented.");
134134
}
135135
}
136+
137+
export const Writable: typeof stream.Writable =
138+
(globalThis as any).Writable || _Writable;

0 commit comments

Comments
 (0)