Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/codegen/builtins/host/features-node.builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export default b.declarations(

extendAll(["NodeSmallModules"], "node:util/types", "node:constants"),

extendAll(["Buffer"], "Buffer", "internal/stream", "internal/streams/legacy", "internal/streams/readable"),
extendAll(["Buffer"], "Buffer", "internal/streams/legacy", "internal/streams/readable"),
extendAll(["NodeBufferModule", "Buffer"], "node:buffer"),

extendAll(["NodeFetch"], "node-fetch", ...nativeBindings("Cpp", "NodeFetch.cpp", "createNodeFetchInternalBinding")),
Expand Down
27 changes: 7 additions & 20 deletions src/js/internal/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
const ObjectKeys = Object.keys;
const ObjectDefineProperty = Object.defineProperty;

const { setDefaultHighWaterMark, getDefaultHighWaterMark, internals } = require("internal/streams/state");
// Set before internal/streams/legacy can load: a `Stream` that is made from here on gets its members from the stores below, not as accessors.
internals.streamDefinesItsMembers = true;
const customPromisify = Symbol.for("nodejs.util.promisify.custom");
const { streamReturningOperators, promiseReturningOperators } = require("internal/streams/operators");
const compose = require("internal/streams/compose");
const { setDefaultHighWaterMark, getDefaultHighWaterMark } = require("internal/streams/state");
const { pipeline } = require("internal/streams/pipeline");
const { destroyer } = require("internal/streams/destroy");
const eos = require("internal/streams/end-of-stream");
const utils = require("internal/streams/utils");
const { isArrayBufferView, isUint8Array } = require("node:util/types");
const Stream = require("internal/streams/legacy").Stream;
const { Stream, hasLazyMembers, definePromisesAndStream } = require("internal/streams/legacy");

Stream.isDestroyed = utils.isDestroyed;
Stream.isDisturbed = utils.isDisturbed;
Expand Down Expand Up @@ -75,14 +76,9 @@ Stream.compose = compose;
Stream.setDefaultHighWaterMark = setDefaultHighWaterMark;
Stream.getDefaultHighWaterMark = getDefaultHighWaterMark;

ObjectDefineProperty(Stream, "promises", {
__proto__: null,
configurable: true,
enumerable: true,
get() {
return require("internal/stream.promises");
},
});
if (!hasLazyMembers) definePromisesAndStream();
// The old name of `finished`.
Stream.eos = eos;

ObjectDefineProperty(pipeline, customPromisify, {
__proto__: null,
Expand All @@ -100,13 +96,4 @@ ObjectDefineProperty(eos, customPromisify, {
},
});

// Backwards-compat with node 0.4.x
Stream.Stream = Stream;

Stream._isArrayBufferView = isArrayBufferView;
Stream._isUint8Array = isUint8Array;
Stream._uint8ArrayToBuffer = function _uint8ArrayToBuffer(chunk) {
return new $Buffer(chunk.buffer, chunk.byteOffset, chunk.byteLength);
};

export default Stream;
147 changes: 141 additions & 6 deletions src/js/internal/streams/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

const EE = require("node:events");
const { isArrayBufferView, isUint8Array } = require("node:util/types");

const ReflectOwnKeys = Reflect.ownKeys;
const { internals } = require("internal/streams/state");

// Reflect lists a member from when it is first read. internal/primordials, which is loaded behind this one, reads the second ahead of the last two.
const {
ownKeys: ReflectOwnKeys,
getPrototypeOf: _ReflectGetPrototypeOf,
getOwnPropertyDescriptor: ReflectGetOwnPropertyDescriptor,
defineProperty: ReflectDefineProperty,
} = Reflect;
const ArrayIsArray = Array.isArray;
const ObjectDefineProperty = Object.defineProperty;

declare global {
namespace $Node {
Expand All @@ -24,7 +32,7 @@ declare global {
resume?(): unknown;
}

/** `require("node:stream")`. internal/stream.ts adds the members that other modules define. */
/** `require("node:stream")`. internal/stream.ts stores the members that other modules define. */
interface StreamConstructor {
new (options?: EventEmitterOptions): Stream;
(this: Stream, options?: EventEmitterOptions): void;
Expand All @@ -38,8 +46,8 @@ declare global {
duplexPair: typeof import("internal/streams/duplexpair").default;
pipeline: typeof import("internal/streams/pipeline").default.pipeline;
finished: typeof import("internal/streams/end-of-stream").default;
/** The old name of `finished`, which node/stream.ts adds. */
eos?: typeof import("internal/streams/end-of-stream").default;
/** The old name of `finished`. */
eos: typeof import("internal/streams/end-of-stream").default;
compose: typeof import("internal/streams/compose").default;
addAbortSignal: typeof import("internal/streams/add-abort-signal").default.addAbortSignal;
destroy: typeof import("internal/streams/destroy").default.destroyer;
Expand Down Expand Up @@ -179,4 +187,131 @@ Stream._uint8ArrayToBuffer = function _uint8ArrayToBuffer(chunk) {
return new $Buffer(chunk.buffer, chunk.byteOffset, chunk.byteLength);
};

export default { Stream: Stream as $Node.StreamConstructor, prependListener };
type LazyMember = Exclude<keyof $Node.StreamConstructor, "prototype" | "Stream" | "promises" | `_${string}`>;

// What internal/stream.ts stores to `Stream` under that name.
function loadMember(name: LazyMember): Function {
switch (name) {
case "isDestroyed":
return require("internal/streams/utils").isDestroyed;
case "isDisturbed":
return require("internal/streams/utils").isDisturbed;
case "isErrored":
return require("internal/streams/utils").isErrored;
case "isReadable":
return require("internal/streams/utils").isReadable;
case "isWritable":
return require("internal/streams/utils").isWritable;
case "Readable":
return require("internal/streams/readable");
case "Writable":
return require("internal/streams/writable");
case "Duplex":
return require("internal/streams/duplex");
case "Transform":
return require("internal/streams/transform");
case "PassThrough":
return require("internal/streams/passthrough");
case "duplexPair":
return require("internal/streams/duplexpair");
case "pipeline":
return require("internal/streams/pipeline").pipeline;
case "addAbortSignal":
return require("internal/streams/add-abort-signal").addAbortSignal;
case "finished":
case "eos":
return require("internal/streams/end-of-stream");
case "destroy":
return require("internal/streams/destroy").destroyer;
case "compose":
return require("internal/streams/compose");
case "setDefaultHighWaterMark":
return require("internal/streams/state").setDefaultHighWaterMark;
case "getDefaultHighWaterMark":
return require("internal/streams/state").getDefaultHighWaterMark;
}
}

function lazyMember(name: LazyMember) {
// As a store to a data property: the value goes on the receiver, a class below `Stream` for `Socket.pipeline = value`.
function set(this: unknown, value: unknown) {
if (!$isObject(this)) return;
const own = ReflectGetOwnPropertyDescriptor(this, name);
if (own === undefined)
ReflectDefineProperty(this, name, {
__proto__: null,
value,
writable: true,
enumerable: true,
configurable: true,
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
robobun marked this conversation as resolved.
Resolved
else if (own.writable || own.set === set)
ReflectDefineProperty(this, name, { __proto__: null, value, writable: true });
Comment thread
robobun marked this conversation as resolved.
Resolved
Comment thread
robobun marked this conversation as resolved.
Resolved
}
ObjectDefineProperty(Stream, name, {
__proto__: null,
enumerable: true,
configurable: true,
get() {
// internal/stream stores the members itself once it has begun to load, and a load that throws does so at require("node:stream").
if (!internals.streamDefinesItsMembers) {
try {
require("internal/stream");
} catch {}
}
return loadMember(name);
},
set,
});
}

// Modules such as node:net and node:crypto make `Stream` without internal/stream, where a program can freeze it: so it has every member from the start, as an accessor that loads internal/stream.
function defineLazyMembers() {
const names = [
"isDestroyed",
"isDisturbed",
"isErrored",
"isReadable",
"isWritable",
"Readable",
"Writable",
"Duplex",
"Transform",
"PassThrough",
"duplexPair",
"pipeline",
"addAbortSignal",
"finished",
"destroy",
"compose",
"setDefaultHighWaterMark",
"getDefaultHighWaterMark",
] as const satisfies readonly LazyMember[];
for (let i = 0; i < names.length; i++) lazyMember(names[i]);
definePromisesAndStream();
lazyMember("eos");
}

// Behind the members that other modules define and ahead of `eos`, from whichever module puts the members on `Stream`.
function definePromisesAndStream() {
ObjectDefineProperty(Stream, "promises", {
__proto__: null,
configurable: true,
enumerable: true,
get() {
return require("internal/stream.promises");
},
});
// Backwards-compat with node 0.4.x
(Stream as $Node.StreamConstructor).Stream = Stream as $Node.StreamConstructor;
}

const hasLazyMembers = !internals.streamDefinesItsMembers;
if (hasLazyMembers) defineLazyMembers();

export default {
Stream: Stream as $Node.StreamConstructor,
prependListener,
hasLazyMembers,
definePromisesAndStream,
};
3 changes: 3 additions & 0 deletions src/js/internal/streams/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ interface Internals {
destroyEndedRequest: ((state: ReadableState, stream: $Node.Readable) => boolean) | undefined;
/** Only in what was compiled ahead of time. */
makeObjectReadable?: (highWaterMark: number) => $Node.Readable;
/** internal/stream sets it before it loads its parts: it stores the members of node:stream on `Stream` itself. */
streamDefinesItsMembers: boolean;
}

type WritableAccessorName =
Expand Down Expand Up @@ -183,6 +185,7 @@ const internals: Internals = {
onRequestBodyData: undefined,
takeRequestCloseTick: undefined,
destroyEndedRequest: undefined,
streamDefinesItsMembers: false,
};

// TODO(benjamingr) it is likely slower to do it this way than with free functions
Expand Down
4 changes: 3 additions & 1 deletion src/js/node/stream.promises.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// Hardcoded module "node:stream/promises"
export default require("node:stream").promises;
// As in Node.js, node:stream loads with it. Not the `promises` of node:stream: a program can delete or replace that.
require("node:stream");
export default require("internal/stream.promises");
Comment thread
robobun marked this conversation as resolved.
Resolved
2 changes: 0 additions & 2 deletions src/js/node/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ const exports = require("internal/stream");

$debug("node:stream loaded");

exports.eos = require("internal/streams/end-of-stream");

export default exports;
Loading
Loading