Commit 11cc7f23 authored by Jimmy Lai's avatar Jimmy Lai Committed by Tim Neutkens

feat: use fast-webstreams for server rendering

Replace global WebStream constructors (ReadableStream, WritableStream,
TransformStream) with faster Node.js-backed implementations from
experimental-fast-webstreams. The patching happens synchronously in
node-environment-baseline.ts before any rendering code loads.

Includes vendored fixes:
- fix(pipe-to): drain buffered flush chunks before shutdown in specPipeTo
- perf(pipe-to): batch sync reads to reduce microtask yields

Benchmark (force-dynamic page, 4KB response):
  Single client: 611 -> 1,011 req/s (+65% vs native WebStreams)
  Under load:  1,167 -> 1,128 req/s (on par, -22% p99 latency)
parent cbc4888b
......@@ -206,6 +206,7 @@
"execa": "2.0.3",
"expect": "29.7.0",
"expect-type": "0.14.2",
"experimental-fast-webstreams": "0.0.5",
"express": "4.17.0",
"faker": "5.5.3",
"faunadb": "2.6.1",
......@@ -344,7 +345,8 @@
"@types/node@20.17.6": "patches/@types__node@20.17.6.patch",
"taskr@1.1.0": "patches/taskr@1.1.0.patch",
"minizlib@3.1.0": "patches/minizlib@3.1.0.patch",
"http-proxy@1.18.1": "patches/http-proxy@1.18.1.patch"
"http-proxy@1.18.1": "patches/http-proxy@1.18.1.patch",
"experimental-fast-webstreams@0.0.5": "patches/experimental-fast-webstreams@0.0.5.patch"
}
}
}
This diff is collapsed.
// This file should be imported before any others. It sets up the environment
// for later imports to work properly.
// Replace global WebStream constructors with faster Node.js-backed implementations.
// Must run synchronously before any code captures references to globalThis.ReadableStream etc.
{
const {
patchGlobalWebStreams,
} = (require('next/dist/compiled/fast-webstreams/patch.js') as typeof import('next/dist/compiled/fast-webstreams/patch.js'))
patchGlobalWebStreams()
}
// expose AsyncLocalStorage on global for react usage if it isn't already provided by the environment
if (typeof (globalThis as any).AsyncLocalStorage !== 'function') {
const { AsyncLocalStorage } =
......
......@@ -330,6 +330,37 @@ export async function ncc_acorn(task, opts) {
.target('src/compiled/acorn')
}
export async function ncc_fast_webstreams(task, opts) {
// `experimental-fast-webstreams` provides drop-in faster WebStream
// constructors backed by Node.js native streams.
// The package is ESM ("type": "module"), but ncc auto-detects this and
// outputs ESM. To force CJS output, we temporarily remove the "type" field
// from the package.json before bundling, then restore it.
const mainEntry = require.resolve('experimental-fast-webstreams')
const srcDir = dirname(mainEntry)
const pkgJsonPath = join(dirname(srcDir), 'package.json')
const originalPkgJson = await fs.readFile(pkgJsonPath, 'utf8')
const pkg = JSON.parse(originalPkgJson)
delete pkg.type
await fs.writeFile(pkgJsonPath, JSON.stringify(pkg, null, 2))
try {
await task
.source(relative(__dirname, join(srcDir, 'patch.js')))
.ncc({
packageName: 'experimental-fast-webstreams',
externals: {
...externals,
'node:stream': 'node:stream',
},
})
.target('src/compiled/fast-webstreams')
} finally {
// Restore original package.json
await fs.writeFile(pkgJsonPath, originalPkgJson)
}
}
externals['@edge-runtime/cookies'] = 'next/dist/compiled/@edge-runtime/cookies'
export async function ncc_edge_runtime_cookies() {
......@@ -2346,6 +2377,7 @@ export async function ncc(task, opts) {
'ncc_edge_runtime_ponyfill',
'ncc_edge_runtime',
'ncc_busboy',
'ncc_fast_webstreams',
'ncc_mswjs_interceptors',
'ncc_rsc_poison_packages',
'ncc_modelcontextprotocol_sdk',
......
diff --git a/src/pipe-to.js b/src/pipe-to.js
index 714522c631bedf917da079546b08fda462e6e430..3cb5388f29a4ab6d2b56538bc4f13a09eb9760ac 100644
--- a/src/pipe-to.js
+++ b/src/pipe-to.js
@@ -12,21 +12,10 @@
import { kNodeReadable, kWritableState, noop, RESOLVED_UNDEFINED } from './utils.js';
/**
- * Wait for all promises in a set to settle, resolving to undefined (not an array).
- * Unlike Promise.all(), this avoids creating an intermediate array result that would
- * trigger observable thenable resolution on Object.prototype.then monkey-patching.
+ * Wait for a write promise to settle, resolving to undefined regardless of outcome.
*/
-function _waitForAll(promiseSet) {
- let p = RESOLVED_UNDEFINED;
- for (const w of promiseSet) {
- p = p
- .then(
- () => w,
- () => w,
- )
- .then(noop, noop);
- }
- return p;
+function _waitForWrite(writePromise) {
+ return writePromise.then(noop, noop);
}
export function specPipeTo(source, dest, options = {}) {
@@ -40,7 +29,6 @@ export function specPipeTo(source, dest, options = {}) {
let shuttingDown = false;
let currentWrite = RESOLVED_UNDEFINED;
- const pendingWrites = new Set();
// Track dest state for WritableStreamDefaultWriterCloseWithErrorPropagation
let destClosed = false;
@@ -91,28 +79,19 @@ export function specPipeTo(source, dest, options = {}) {
);
// --- Source close/error handler ---
+ // NOTE: Don't trigger shutdown on source close here. The reader.closed
+ // promise can resolve (via Node.js 'end'/'close' events) before the pump
+ // has drained all buffered data from the underlying Node.js readable.
+ // Instead, just set the flag and kick the pump. The pump will read done
+ // and trigger shutdown after draining all buffered chunks.
reader.closed.then(
() => {
- // Source closed
+ // Source closed — let the pump drain remaining data and handle shutdown
sourceClosed = true;
- if (shuttingDown) return;
- if (!preventClose) {
- shutdownWithAction(() => {
- // WritableStreamDefaultWriterCloseWithErrorPropagation
- if (destErrored) return Promise.reject(destStoredError);
- if (destClosed) return RESOLVED_UNDEFINED;
- // writer.close() may reject if already closed; treat as success
- return writer.close().catch((e) => {
- if (e instanceof TypeError) return; // Already closed/closing
- throw e;
- });
- });
- } else {
- shutdown();
- }
+ if (!shuttingDown) pipeLoop();
},
(storedError) => {
- // Source errored
+ // Source errored — immediate shutdown is correct (no data to drain)
if (shuttingDown) return;
if (!preventAbort) {
shutdownWithAction(() => writer.abort(storedError), true, storedError);
@@ -171,45 +150,66 @@ export function specPipeTo(source, dest, options = {}) {
}
}
+ // Close-shutdown helper (called when pump reads done)
+ function pumpClose() {
+ sourceClosed = true;
+ if (shuttingDown) return;
+ if (!preventClose) {
+ shutdownWithAction(() => {
+ if (destErrored) return Promise.reject(destStoredError);
+ if (destClosed) return RESOLVED_UNDEFINED;
+ return writer.close().catch((e) => {
+ if (e instanceof TypeError) return;
+ throw e;
+ });
+ });
+ } else {
+ shutdown();
+ }
+ }
+
function pumpRead() {
if (shuttingDown) return;
- // Sync fast path: read directly from Node buffer via reader._readSync
- // (saves 2 promises per chunk: the read result Promise + the .then Promise).
- // Must yield to microtask queue after each read for close/error handler interleaving.
+ // Sync fast path: batch-read directly from Node buffer via reader._readSync.
+ // Drains all available buffered chunks in one go before yielding to the
+ // microtask queue, amortizing the yield cost across multiple chunks.
if (hasSyncRead) {
- const syncResult = reader._readSync();
- if (syncResult !== null) {
+ let didSync = false;
+ let syncResult = reader._readSync();
+ while (syncResult !== null) {
if (syncResult.done) {
- sourceClosed = true;
+ pumpClose();
return;
}
+ didSync = true;
currentWrite = writer.write(syncResult.value);
- pendingWrites.add(currentWrite);
- const w = currentWrite;
- currentWrite.then(
- () => pendingWrites.delete(w),
- () => pendingWrites.delete(w),
- );
+ if (shuttingDown) return;
+ // Check backpressure before reading more
+ if (writer.desiredSize <= 0) {
+ queueMicrotask(pipeLoop);
+ return;
+ }
+ syncResult = reader._readSync();
+ }
+ // _readSync returned null — no more buffered data.
+ if (didSync) {
+ // We processed at least one chunk. Yield to allow events to fire,
+ // then re-enter pipeLoop which will try sync again or fall to async.
queueMicrotask(pipeLoop);
return;
}
+ // No sync data available at all — fall through to async read below.
}
- // Async fallback
+ // Async fallback: wait for data via 'readable' event
reader.read().then(({ value, done }) => {
if (shuttingDown) return;
if (done) {
- sourceClosed = true;
+ pumpClose();
return;
}
currentWrite = writer.write(value);
- pendingWrites.add(currentWrite);
- const w = currentWrite;
- currentWrite.then(
- () => pendingWrites.delete(w),
- () => pendingWrites.delete(w),
- );
pipeLoop();
}, noop);
}
@@ -233,17 +233,15 @@ export function specPipeTo(source, dest, options = {}) {
);
};
- // Wait for all in-flight writes to complete before running action
- const allWrites = pendingWrites.size > 0 ? _waitForAll(pendingWrites) : currentWrite;
- allWrites.then(doAction, doAction);
+ // Wait for the last in-flight write to settle before running action
+ _waitForWrite(currentWrite).then(doAction, doAction);
}
// --- Shutdown without action ---
function shutdown(isError = false, error) {
if (shuttingDown) return;
shuttingDown = true;
- const allWrites = pendingWrites.size > 0 ? _waitForAll(pendingWrites) : currentWrite;
- allWrites.then(
+ _waitForWrite(currentWrite).then(
() => finalize(isError, error),
() => finalize(isError, error),
);
......@@ -28,6 +28,9 @@ patchedDependencies:
'@types/node@20.17.6':
hash: rvl3vkomen3tospgr67bzubfyu
path: patches/@types__node@20.17.6.patch
experimental-fast-webstreams@0.0.5:
hash: 45ceg3shibzyxkompnbutv5bma
path: patches/experimental-fast-webstreams@0.0.5.patch
http-proxy@1.18.1:
hash: qqiqxx62zlcu62nljjmhlvexni
path: patches/http-proxy@1.18.1.patch
......@@ -324,6 +327,9 @@ importers:
expect-type:
specifier: 0.14.2
version: 0.14.2
experimental-fast-webstreams:
specifier: 0.0.5
version: 0.0.5(patch_hash=45ceg3shibzyxkompnbutv5bma)
express:
specifier: 4.17.0
version: 4.17.0
......@@ -2893,6 +2899,7 @@ packages:
'@base-ui-components/react@1.0.0-beta.2':
resolution: {integrity: sha512-jfAUfSgXvsfr8mQi7r/6gLG8U1Ybr77NN8WK5IXXM0c/hBvFDBtvUfwDJACV0gXiYbSKpA+dRzZz01V1tULobA==}
engines: {node: '>=14.0.0'}
deprecated: Package was renamed to @base-ui/react
peerDependencies:
'@types/react': 19.2.10
react: 19.3.0-canary-ab18f33d-20260220
......@@ -2903,6 +2910,7 @@ packages:
'@base-ui-components/utils@0.1.0':
resolution: {integrity: sha512-9+uaWyF1o/PgXqHLJnC81IIG0HlV3o9eFCQ5hWZDMx5NHrFk0rrwqEFGQOB8lti/rnbxNPi+kYYw1D4e8xSn/Q==}
deprecated: Package was renamed to @base-ui/utils
peerDependencies:
'@types/react': 19.2.10
react: 19.3.0-canary-ab18f33d-20260220
......@@ -10312,6 +10320,10 @@ packages:
resolution: {integrity: sha512-WVi2V4iHKw/vHEyye00Q9CSZz7KHDbJkJyteUI8kTih9jiyMl3bIk7wLYFcY9D1Blnadlyb5w5NBuNjQBow99g==}
engines: {node: ^16.10.0 || ^18.12.0 || >=20.0.0}
experimental-fast-webstreams@0.0.5:
resolution: {integrity: sha512-VsKpdIur3SL8DCMANpxu6jxyVnVrWOUuqlTlaxFa+8SUd2NIZISAsiwNBx0NG4OZ6xHuuZBBSx/QnAeFmv6oXw==}
engines: {node: '>=20'}
exponential-backoff@3.1.3:
resolution: {integrity: sha512-ZgEeZXj30q+I0EN+CbSSpIyPaJ5HVQD18Z1m+u1FXbAeT94mr1zw50q4q6jiiC447Nl/YTcIYSAftiGqetwXCA==}
......@@ -15710,6 +15722,7 @@ packages:
engines: {node: '>=0.6.0', teleport: '>=0.2.0'}
deprecated: |-
You or someone you depend on is using Q, the JavaScript Promise library that gave JavaScript developers strong feelings about promises. They can almost certainly migrate to the native JavaScript promise now. Thank you literally everyone for joining me in this bet against the odds. Be excellent to each other.
(For a CapTP with native promises, see @endo/eventual-send and @endo/captp)
qs@6.11.0:
......@@ -17412,7 +17425,7 @@ packages:
tar@6.2.1:
resolution: {integrity: sha512-DZ4yORTwrbTj/7MZYq2w+/ZFdI6OZ/f9SFHR+71gIVUZhOQPHzVCLpvRnPgyaMpfWxxk/4ONva3GQSyNIKRv6A==}
engines: {node: '>=10'}
deprecated: Old versions of tar are not supported, and contain widely publicized security vulnerabilities, which have been fixed in the current version. Please update. Support for old versions may be purchased (at exhorbitant rates) by contacting i@izs.me
deprecated: Old versions of tar are not supported, and contain widely publicized security vulnerabilities, which have been fixed in the current version. Please update. Support for old versions may be purchased (at exorbitant rates) by contacting i@izs.me
tar@7.5.7:
resolution: {integrity: sha512-fov56fJiRuThVFXD6o6/Q354S7pnWMJIVlDBYijsTNx6jKSE4pvrDTs6lUnmGvNyfJwFQQwWy3owKz1ucIhveQ==}
......@@ -28549,6 +28562,8 @@ snapshots:
jest-mock: 30.0.0-alpha.6
jest-util: 30.0.0-alpha.6
experimental-fast-webstreams@0.0.5(patch_hash=45ceg3shibzyxkompnbutv5bma): {}
exponential-backoff@3.1.3: {}
express-rate-limit@7.5.1(express@5.1.0):
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment