Commit 63c39dca59a for nodejs

commit 63c39dca59ad65406e3903e60a6a6386e9c5617c
Author: Yagiz Nizipli <yagiz@nizipli.com>
Date:   Sun Nov 29 11:17:11 2026 -0500

    stream: speed up WHATWG web streams

    Rebase the remaining construction-path work onto current main. The
    shared hot-path reductions (raw pull/write callbacks, parked promise
    results) already landed, and so did the immediate-start fast path for
    sources without pull(). This keeps what is still left:

    - Defer the default controller for `new ReadableStream()` until
      getReader(), cancel(), or an error on a readable stream.
    - Skip validateObject on empty WritableStream and TransformStream
      construction, and create the writable AbortController lazily.
    - Share isNonThenable() for start/pull/write results. Public
      writer.ready, writer.closed, and cancel() promises stay distinct.

    Assisted-by: Cursor
    Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com>
    Co-authored-by: Yagiz Nizipli <anonrig@users.noreply.github.com>
    PR-URL: https://github.com/nodejs/node/pull/65273
    Reviewed-By: Mattias Buelens <mattias@buelens.com>
    Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
    Reviewed-By: Filip Skokan <panva.ip@gmail.com>

diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js
index 7e083286cf1..af058ddad12 100644
--- a/lib/internal/webstreams/readablestream.js
+++ b/lib/internal/webstreams/readablestream.js
@@ -120,6 +120,7 @@ const {
   extractHighWaterMark,
   extractSizeAlgorithm,
   getNonWritablePropertyDescriptor,
+  isNonThenable,
   kDeferredStart,
   kEmptyQueue,
   kParkedAlgorithmResult,
@@ -266,7 +267,14 @@ class ReadableStream {
     // Internal construction (tee, transform streams, adapters, transfer):
     // the caller sets up the controller, so every ReadableStream shares
     // one hidden class and no per-instance prototype swap is needed.
-    if (source === kSkipThrow) {
+    // Internal construction (tee, transform streams, adapters, transfer)
+    // and empty-argument `new ReadableStream()` share this path: no
+    // controller yet. Reads of an empty stream never deliver data, so
+    // skip those allocations until getReader/cancel/error first need a
+    // default controller. Subclasses that call those methods after
+    // super() materialize it; see ensureEmptyDefaultController.
+    if (source === kSkipThrow ||
+        (source === kEmptyObject && strategy === kEmptyObject)) {
       this[kState] = createReadableStreamState();
       return;
     }
@@ -319,6 +327,12 @@ class ReadableStream {
     // only default controllers were wired here; byte stream controllers
     // keep the previous no-op behavior.
     const controller = this[kState].controller;
+    if (controller === undefined) {
+      if (this[kState].state === 'readable') {
+        readableStreamError(this, error);
+      }
+      return;
+    }
     if (isReadableStreamDefaultController(controller))
       controller.error(error);
   }
@@ -369,6 +383,10 @@ class ReadableStream {
       return PromiseReject(
         new ERR_INVALID_STATE.TypeError('ReadableStream is locked'));
     }
+    // Only materialize the deferred empty controller when cancel will
+    // actually run cancel steps. closed/errored streams return immediately.
+    if (this[kState].state === 'readable')
+      ensureEmptyDefaultController(this);
     return readableStreamCancel(this, reason);
   }

@@ -2849,6 +2867,7 @@ function setupReadableStreamBYOBReader(reader, stream) {
 function setupReadableStreamDefaultReader(reader, stream) {
   if (isReadableStreamLocked(stream))
     throw new ERR_INVALID_STATE.TypeError('ReadableStream is locked');
+  ensureEmptyDefaultController(stream);
   readableStreamReaderGenericInitialize(reader, stream);
   reader[kState].readRequests = kEmptyQueue;
 }
@@ -3070,6 +3089,44 @@ function readableStreamDefaultControllerPullSteps(controller, readRequest) {
     readableStreamDefaultControllerPull(controller);
 }

+// Materialize the deferred default controller for `new ReadableStream()`.
+//
+// started is true immediately: the empty-argument start algorithm is a
+// no-op, so there is no initial pull and nothing can observe an unstarted
+// controller without first calling getReader/cancel/pipeTo/tee/values,
+// all of which come through here. That is also why this still matches
+// WPT: those tests either pass a source (leaving this path) or wait for
+// start, which is already complete for a no-op start.
+//
+// Subclasses that call cancel(), getReader(), pipeTo(), tee(), or
+// values() in the constructor body after super() will materialize the
+// controller before the subclass constructor finishes. Passing a source
+// (for example to install start/pull) leaves the empty-argument path
+// and creates the controller during super() as usual.
+function ensureEmptyDefaultController(stream) {
+  if (stream[kState].controller !== undefined)
+    return stream[kState].controller;
+  const controller = new ReadableStreamDefaultController(kSkipThrow);
+  controller[kState] = {
+    cancelAlgorithm: nonOpCancel,
+    closeRequested: false,
+    highWaterMark: 1,
+    pullAgain: false,
+    pullAlgorithm: nonOpCallback,
+    pulling: false,
+    pullFulfilled: undefined,
+    pullRejected: undefined,
+    queue: kEmptyQueue,
+    queueTotalSize: 0,
+    started: true,
+    sizeAlgorithm: defaultSizeAlgorithm,
+    stream,
+    transformStream: undefined,
+  };
+  stream[kState].controller = controller;
+  return controller;
+}
+
 function setupReadableStreamDefaultController(
   stream,
   controller,
@@ -3105,8 +3162,7 @@ function setupReadableStreamDefaultController(

   // A non-thenable start result guarantees fulfillment, and no .then
   // lookup on it is observable.
-  const startFulfilled = startResult === null ||
-    (typeof startResult !== 'object' && typeof startResult !== 'function');
+  const startFulfilled = isNonThenable(startResult);

   // The started flag only gates calls into the pull algorithm, so for a
   // source without pull() the post-start step has nothing observable
@@ -4007,8 +4063,7 @@ function setupReadableByteStreamController(

   const startResult = startAlgorithm();
   // See setupReadableStreamDefaultController.
-  const startFulfilled = startResult === null ||
-    (typeof startResult !== 'object' && typeof startResult !== 'function');
+  const startFulfilled = isNonThenable(startResult);

   if (startFulfilled && pullAlgorithm === nonOpCallback) {
     controller[kState].started = true;
diff --git a/lib/internal/webstreams/transformstream.js b/lib/internal/webstreams/transformstream.js
index e9cf0d9978e..4307c3d9282 100644
--- a/lib/internal/webstreams/transformstream.js
+++ b/lib/internal/webstreams/transformstream.js
@@ -133,9 +133,15 @@ class TransformStream {
     writableStrategy = kEmptyObject,
     readableStrategy = kEmptyObject) {
     markTransferMode(this, false, true);
-    validateObject(transformer, 'transformer', kValidateObjectAllowObjects);
-    validateObject(writableStrategy, 'writableStrategy', kValidateObjectAllowObjectsAndNull);
-    validateObject(readableStrategy, 'readableStrategy', kValidateObjectAllowObjectsAndNull);
+    if (transformer !== kEmptyObject) {
+      validateObject(transformer, 'transformer', kValidateObjectAllowObjects);
+    }
+    if (writableStrategy !== kEmptyObject) {
+      validateObject(writableStrategy, 'writableStrategy', kValidateObjectAllowObjectsAndNull);
+    }
+    if (readableStrategy !== kEmptyObject) {
+      validateObject(readableStrategy, 'readableStrategy', kValidateObjectAllowObjectsAndNull);
+    }
     const readableType = transformer?.readableType;
     const writableType = transformer?.writableType;
     const start = transformer?.start;
diff --git a/lib/internal/webstreams/util.js b/lib/internal/webstreams/util.js
index 79a512b05dd..fc5bd4b7a60 100644
--- a/lib/internal/webstreams/util.js
+++ b/lib/internal/webstreams/util.js
@@ -130,6 +130,16 @@ function cloneAsUint8Array(view) {
   );
 }

+// True when `value` cannot be a thenable: null, undefined, or a
+// non-object non-function primitive. Objects and functions are treated
+// as maybe-thenable without looking up `.then` (that lookup is
+// observable). Proxies of objects/functions take the maybe-thenable
+// path; a Proxy around a primitive is still an object.
+function isNonThenable(value) {
+  return value === null ||
+    (typeof value !== 'object' && typeof value !== 'function');
+}
+
 function canCopyArrayBuffer(toBuffer, toIndex, fromBuffer, fromIndex, count) {
   return toBuffer !== fromBuffer &&
     !ArrayBufferPrototypeGetDetached(toBuffer) &&
@@ -403,8 +413,7 @@ function deferredStartAlgorithm() {
 function thenAlgorithmResult(result, onFulfilled, onRejected) {
   if (result === kParkedAlgorithmResult)
     return;
-  if (result === null ||
-      (typeof result !== 'object' && typeof result !== 'function')) {
+  if (isNonThenable(result)) {
     PromisePrototypeThen(kResolvedPromise, onFulfilled);
   } else {
     PromisePrototypeThen(PromiseResolve(result), onFulfilled, onRejected);
@@ -489,6 +498,7 @@ module.exports = {
   extractHighWaterMark,
   extractSizeAlgorithm,
   getNonWritablePropertyDescriptor,
+  isNonThenable,
   isPromisePending,
   kDeferredStart,
   kEmptyQueue,
diff --git a/lib/internal/webstreams/writablestream.js b/lib/internal/webstreams/writablestream.js
index 1cf10984d3d..6496f554194 100644
--- a/lib/internal/webstreams/writablestream.js
+++ b/lib/internal/webstreams/writablestream.js
@@ -63,6 +63,7 @@ const {
   extractHighWaterMark,
   extractSizeAlgorithm,
   getNonWritablePropertyDescriptor,
+  isNonThenable,
   isPromisePending,
   kDeferredStart,
   kEmptyQueue,
@@ -190,6 +191,17 @@ class WritableStream {
       this[kState] = createWritableStreamState();
       return;
     }
+    // Empty construction skips validateObject. The controller is still
+    // created so signal/abort observe the same object as a normal stream.
+    if (sink === kEmptyObject && strategy === kEmptyObject) {
+      this[kState] = createWritableStreamState();
+      setupWritableStreamDefaultControllerFromSink(
+        this,
+        sink,
+        1,
+        defaultSizeAlgorithm);
+      return;
+    }
     validateObject(sink, 'sink', kValidateObjectAllowObjects);
     validateObject(strategy, 'strategy', kValidateObjectAllowObjectsAndNull);
     const type = sink?.type;
@@ -529,7 +541,7 @@ class WritableStreamDefaultController {
   get signal() {
     if (!isWritableStreamDefaultController(this))
       throw new ERR_INVALID_THIS('WritableStreamDefaultController');
-    return this[kState].abortController.signal;
+    return (this[kState].abortController ??= new AbortController()).signal;
   }

   /**
@@ -716,7 +728,9 @@ function writableStreamAbort(stream, reason) {
   if (state === 'closed' || state === 'errored')
     return PromiseResolve();

-  controller[kState].abortController.abort(reason);
+  // Materialize lazily so construction stays cheap, but abort() must
+  // still abort the same signal later observed via controller.signal.
+  (controller[kState].abortController ??= new AbortController()).abort(reason);

   state = stream[kState].state;
   if (state === 'closed' || state === 'errored')
@@ -1412,7 +1426,7 @@ function setupWritableStreamDefaultController(
     highWaterMark,
     queue: kEmptyQueue,
     queueTotalSize: 0,
-    abortController: new AbortController(),
+    abortController: undefined,
     sizeAlgorithm,
     started: false,
     stream,
@@ -1436,8 +1450,7 @@ function setupWritableStreamDefaultController(

   const started = () => writableStreamDefaultControllerStarted(controller);

-  if (startResult === null ||
-      (typeof startResult !== 'object' && typeof startResult !== 'function')) {
+  if (isNonThenable(startResult)) {
     // Non-thenable start result: fulfillment is guaranteed and no .then
     // lookup on the result is observable, so the post-start step runs at
     // the exact microtask position the promise reaction would have had.
diff --git a/test/parallel/test-whatwg-webstreams-hotpath.js b/test/parallel/test-whatwg-webstreams-hotpath.js
new file mode 100644
index 00000000000..8bd2c7e805b
--- /dev/null
+++ b/test/parallel/test-whatwg-webstreams-hotpath.js
@@ -0,0 +1,307 @@
+// Flags: --expose-internals --no-warnings
+'use strict';
+
+const common = require('../common');
+const assert = require('assert');
+const {
+  ReadableStream,
+  WritableStream,
+} = require('node:stream/web');
+const {
+  cloneAsUint8Array,
+  isNonThenable,
+  kState,
+} = require('internal/webstreams/util');
+const {
+  kControllerErrorFunction,
+} = require('internal/streams/utils');
+
+assert.strictEqual(typeof isNonThenable, 'function');
+assert.strictEqual(typeof cloneAsUint8Array, 'function');
+
+assert.strictEqual(isNonThenable(undefined), true);
+assert.strictEqual(isNonThenable(null), true);
+assert.strictEqual(isNonThenable(1), true);
+assert.strictEqual(isNonThenable('x'), true);
+assert.strictEqual(isNonThenable(true), true);
+assert.strictEqual(isNonThenable({}), false);
+assert.strictEqual(isNonThenable(() => {}), false);
+assert.strictEqual(isNonThenable(Promise.resolve()), false);
+assert.strictEqual(isNonThenable(new Proxy({}, {})), false);
+assert.strictEqual(isNonThenable(new Proxy(Object(1), {})), false);
+assert.strictEqual(isNonThenable(new Proxy(() => {}, {})), false);
+
+{
+  const src = new Uint8Array([1, 2, 3, 4]);
+  const cloned = cloneAsUint8Array(src);
+  assert.ok(cloned instanceof Uint8Array);
+  assert.deepStrictEqual([...cloned], [1, 2, 3, 4]);
+  src[0] = 9;
+  assert.strictEqual(cloned[0], 1);
+}
+
+{
+  const view = new DataView(new ArrayBuffer(4));
+  new Uint8Array(view.buffer).set([9, 8, 7, 6]);
+  const cloned = cloneAsUint8Array(view);
+  assert.ok(cloned instanceof Uint8Array);
+  assert.deepStrictEqual([...cloned], [9, 8, 7, 6]);
+}
+
+{
+  const cloned = cloneAsUint8Array(new Uint8Array());
+  assert.ok(cloned instanceof Uint8Array);
+  assert.strictEqual(cloned.byteLength, 0);
+}
+
+{
+  const buf = Buffer.alloc(16);
+  buf[4] = 7;
+  buf[5] = 8;
+  const sliced = buf.subarray(4, 8);
+  const cloned = cloneAsUint8Array(sliced);
+  assert.deepStrictEqual([...cloned], [7, 8, 0, 0]);
+  assert.strictEqual(cloned.buffer.byteLength, 4);
+}
+
+{
+  const ab = new ArrayBuffer(8);
+  const view = new Uint8Array(ab);
+  ab.transfer();
+  assert.throws(() => cloneAsUint8Array(view), {
+    name: 'TypeError',
+  });
+}
+
+// Public API: pull-driven ReadableStream + read().
+(async () => {
+  const rs = new ReadableStream({
+    start(controller) {
+      controller.enqueue('a');
+      controller.enqueue('b');
+      controller.close();
+    },
+  });
+  const reader = rs.getReader();
+  {
+    const { value, done } = await reader.read();
+    assert.strictEqual(value, 'a');
+    assert.strictEqual(done, false);
+  }
+  {
+    const { value, done } = await reader.read();
+    assert.strictEqual(value, 'b');
+    assert.strictEqual(done, false);
+  }
+  {
+    const { value, done } = await reader.read();
+    assert.strictEqual(value, undefined);
+    assert.strictEqual(done, true);
+  }
+})().then(common.mustCall());
+
+// Public API: pipeTo with a sync sink.
+(async () => {
+  const expected = [];
+  const received = [];
+  const rs = new ReadableStream({
+    start(controller) {
+      for (let i = 0; i < 32; i++) {
+        expected.push(i);
+        controller.enqueue(i);
+      }
+      controller.close();
+    },
+  });
+  await rs.pipeTo(new WritableStream({
+    write(chunk) {
+      received.push(chunk);
+    },
+  }));
+  assert.deepStrictEqual(received, expected);
+})().then(common.mustCall());
+
+// Spec path: each pull is separated by a microtask. Start schedules one
+// pull; further pulls wait for that fulfillment and do not run in the
+// same turn. Nested queueMicrotask checks encode that exact schedule
+// and will fail if an unrelated change shifts pull timing.
+{
+  let calls = 0;
+  new ReadableStream({
+    pull(controller) {
+      controller.enqueue(++calls);
+    },
+  }, {
+    highWaterMark: 4,
+  });
+  queueMicrotask(common.mustCall(() => {
+    assert.strictEqual(calls, 1);
+    // The next pull is queued only after fulfillment, so it is not
+    // invoked in this same turn.
+    queueMicrotask(common.mustCall(() => {
+      assert.strictEqual(calls, 2);
+    }));
+  }));
+}
+
+// pipeTo of a pull-driven source must deliver every chunk.
+(async () => {
+  const n = 64;
+  let i = 0;
+  const received = [];
+  const rs = new ReadableStream({
+    pull(controller) {
+      if (i < n)
+        controller.enqueue(i++);
+      else
+        controller.close();
+    },
+  }, { highWaterMark: 8 });
+  await rs.pipeTo(new WritableStream({
+    write(chunk) {
+      received.push(chunk);
+    },
+  }, { highWaterMark: 8 }));
+  assert.strictEqual(received.length, n);
+  assert.deepStrictEqual(received, Array.from({ length: n }, (_, k) => k));
+})().then(common.mustCall());
+
+{
+  // Empty-argument construction defers the controller. cancel() and
+  // getReader() must still work on the public API.
+  const rs = new ReadableStream();
+  rs.cancel().then(common.mustCall());
+}
+
+{
+  // Subclass that calls getReader() before the subclass constructor
+  // finishes: the deferred controller is materialized then.
+  class Sub extends ReadableStream {
+    constructor() {
+      super();
+      this.reader = this.getReader();
+    }
+  }
+  const rs = new Sub();
+  assert.ok(rs.locked);
+  rs.reader.cancel().then(common.mustCall());
+}
+
+{
+  // Subclass that calls cancel() before the subclass constructor finishes.
+  class Sub extends ReadableStream {
+    constructor() {
+      super();
+      this.closed = this.cancel();
+    }
+  }
+  const rs = new Sub();
+  rs.closed.then(common.mustCall());
+}
+
+{
+  // Passing a source leaves the empty-argument path, so start() receives
+  // a controller during super() even if the subclass constructor later
+  // calls getReader().
+  let sawController = false;
+  class Sub extends ReadableStream {
+    constructor() {
+      super({
+        start(controller) {
+          sawController = controller != null;
+        },
+      });
+      this.reader = this.getReader();
+    }
+  }
+  const rs = new Sub();
+  assert.ok(rs.locked);
+  queueMicrotask(common.mustCall(() => {
+    assert.strictEqual(sawController, true);
+    rs.reader.cancel().then(common.mustCall());
+  }));
+}
+
+{
+  const rs = new ReadableStream();
+  const reader = rs.getReader();
+  reader.cancel().then(common.mustCall());
+}
+
+{
+  // A Proxy around a thenable must not take the non-thenable shortcut.
+  let pulled = false;
+  const thenable = new Proxy({
+    then(resolve) {
+      resolve();
+    },
+  }, {});
+  const rs = new ReadableStream({
+    pull(controller) {
+      if (pulled) {
+        controller.close();
+        return thenable;
+      }
+      pulled = true;
+      controller.enqueue('proxied');
+      return thenable;
+    },
+  });
+  rs.getReader().read().then(common.mustCall(({ value, done }) => {
+    assert.strictEqual(value, 'proxied');
+    assert.strictEqual(done, false);
+  }));
+}
+
+{
+  // Do not read controller.signal before abort(): the lazy AbortController
+  // must still report the abort reason on first access.
+  let ctrl;
+  const err = new Error('hotpath-abort-before-signal');
+  const ws = new WritableStream({
+    start(c) { ctrl = c; },
+  });
+  ws.abort(err);
+  assert.strictEqual(ctrl.signal.aborted, true);
+  assert.strictEqual(ctrl.signal.reason, err);
+}
+
+{
+  // writer.ready and writer.closed are distinct spec slots, and two
+  // writers must not share a process-wide resolved promise.
+  const a = new WritableStream().getWriter();
+  const b = new WritableStream().getWriter();
+  assert.notStrictEqual(a.ready, b.ready);
+  assert.notStrictEqual(a.closed, b.closed);
+  assert.notStrictEqual(a.ready, a.closed);
+}
+
+(async () => {
+  const ws = new WritableStream();
+  await ws.close();
+  const writer = ws.getWriter();
+  assert.notStrictEqual(writer.ready, writer.closed);
+  await Promise.all([writer.ready, writer.closed]);
+})().then(common.mustCall());
+
+{
+  // stream.cancel() must not return a process-shared resolved promise.
+  const a = new ReadableStream();
+  const b = new ReadableStream();
+  const pa = a.cancel();
+  const pb = b.cancel();
+  assert.notStrictEqual(pa, pb);
+  pa.then(common.mustCall());
+  pb.then(common.mustCall());
+}
+
+{
+  // Erroring an empty stream via interop must not allocate a controller,
+  // and a later cancel() must not allocate one either.
+  const rs = new ReadableStream();
+  const err = new Error('empty-error');
+  rs[kControllerErrorFunction](err);
+  assert.strictEqual(rs[kState].controller, undefined);
+  assert.rejects(rs.cancel(), err).then(common.mustCall());
+  assert.strictEqual(rs[kState].controller, undefined);
+}
diff --git a/test/parallel/test-whatwg-writablestream.js b/test/parallel/test-whatwg-writablestream.js
index 88d9c57b9de..66b181c8eb5 100644
--- a/test/parallel/test-whatwg-writablestream.js
+++ b/test/parallel/test-whatwg-writablestream.js
@@ -248,6 +248,20 @@ class Sink {
   });
 }

+{
+  // abort() must abort the controller signal even if .signal was never
+  // observed before the abort (lazy AbortController materialization).
+  let ctrl;
+  const err = new Error('abort-before-signal');
+  const ws = new WritableStream({
+    start(c) { ctrl = c; },
+  });
+  assert.ok(ctrl);
+  ws.abort(err);
+  assert.strictEqual(ctrl.signal.aborted, true);
+  assert.strictEqual(ctrl.signal.reason, err);
+}
+
 {
   let controller;
   const writable = new WritableStream({