Skip to content

Commit

Permalink
feat(node:stream): allow overriding Duplex, Readable, Transform
Browse files Browse the repository at this point in the history
… and `Writable` with `globalThis`
  • Loading branch information
pi0 committed Aug 8, 2023
1 parent 5ba2d03 commit e06358d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/runtime/node/stream/duplex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Writable } from "./writable";

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

export const Duplex: DuplexClass = class {
export const _Duplex: DuplexClass = class {
allowHalfOpen: boolean = true;
private _destroy: (error?: Error) => void;

Expand All @@ -19,5 +19,8 @@ export const Duplex: DuplexClass = class {
}
} as any;

Object.assign(Duplex.prototype, Readable.prototype);
Object.assign(Duplex.prototype, Writable.prototype);
Object.assign(_Duplex.prototype, Readable.prototype);
Object.assign(_Duplex.prototype, Writable.prototype);

export const Duplex: typeof stream.Duplex =
(globalThis as any).Duplex || _Duplex;
7 changes: 5 additions & 2 deletions src/runtime/node/stream/readable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { EventEmitter } from "../events";
// Implementation: https://github.com/nodejs/node/blob/master/lib/internal/streams/readable.js

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

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

constructor(_opts?: stream.ReadableOptions) {
Expand Down Expand Up @@ -92,3 +92,6 @@ export class Readable extends EventEmitter implements stream.Readable {
throw new Error("[h3] Method not implemented.");
}
}

export const Readable: typeof stream.Readable =
(globalThis as any).Readable || _Readable;
5 changes: 4 additions & 1 deletion src/runtime/node/stream/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Duplex } from "./duplex";
// Docs: https://nodejs.org/api/stream.html#stream_duplex_and_transform_streams
// Implementation: https://github.com/nodejs/node/blob/master/lib/internal/streams/transform.js

export class Transform extends Duplex implements stream.Transform {
export class _Transform extends Duplex implements stream.Transform {
readonly __unenv__ = true;

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

_flush(callback: stream.TransformCallback): void {}
}

export const Transform: typeof stream.Transform =
(globalThis as any).Transform || _Transform;
5 changes: 4 additions & 1 deletion src/runtime/node/stream/writable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { EventEmitter } from "../events";
// Docs: https://nodejs.org/api/stream.html#stream_writable_streams
// Implementation: https://github.com/nodejs/node/blob/master/lib/internal/streams/writable.js
// eslint-disable-next-line unicorn/prefer-event-target
export class Writable extends EventEmitter implements stream.Writable {
class _Writable extends EventEmitter implements stream.Writable {
readonly __unenv__ = true;

readonly writable: boolean = true;
Expand Down Expand Up @@ -133,3 +133,6 @@ export class Writable extends EventEmitter implements stream.Writable {
throw new Error("[h3] Method not implemented.");
}
}

export const Writable: typeof stream.Writable =
(globalThis as any).Writable || _Writable;

0 comments on commit e06358d

Please sign in to comment.