Unverified Commit 3421858b authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Node.js streams: First pass (#90500)

### What?

Note: This is only the first step. There's more follow-up PRs to
implement the transforms as Node.js streams.

Introduces a compile-time switchable Node.js streams layer for the app
rendering pipeline, gated behind `__NEXT_USE_NODE_STREAMS`. When
enabled, React's `renderToPipeableStream` / `resumeToPipeableStream`
APIs produce native Node.js `Readable` streams instead of web
`ReadableStream`s, eliminating repeated conversions between the two
stream types during rendering.


### Why?

The current rendering pipeline uses web `ReadableStream` everywhere to
be consistent between Node.js/Edge, but the Node.js server ultimately
pipes responses to Node `http.ServerResponse`. We found ReadableStream
to be much slower than using Node.js streams directly, especially under
load. By using Node.js streams natively throughout the pipeline, we can
avoid this cost.

### How?

- **`AnyStream` type** (`ReadableStream<Uint8Array> | Readable`):
Introduced as the universal stream type across the rendering pipeline,
replacing hardcoded `ReadableStream<Uint8Array>` in all stream-carrying
signatures (`app-render.tsx`, `render-result.ts`,
`flight-render-result.ts`, `hot-reloader-types.ts`, etc.).

- **`stream-ops.ts` compile-time switcher**: Changed from a simple
re-export of `stream-ops.web.ts` to a conditional `require()` that loads
either `stream-ops.node.ts` or `stream-ops.web.ts` based on
`process.env.__NEXT_USE_NODE_STREAMS`. Both modules export the same type
surface so no casts are needed.

- **`stream-ops.node.ts` (new)**: Node.js stream implementation that
uses `renderToPipeableStream`, `resumeToPipeableStream`, and
`PassThrough` for native pipeable rendering. Continue functions bridge
to existing web transforms via `Readable.fromWeb()` / `Readable.toWeb()`
at the boundary.

- **`stream-ops.web.ts`**: Updated to wrap continue/utility functions
instead of re-exporting them directly, so both modules have the same
shaped exports. Added `teeStream`, `streamToUint8Array`,
`processPrelude` wrappers.

- **`debug-channel-server.ts` switcher**: Same compile-time pattern —
loads `debug-channel-server.node.ts` (PassThrough-based) or
`debug-channel-server.web.ts` (WritableStream-based).

- **`pipe-readable.ts`**: Added `pipeNodeReadableToNodeResponse()` that
pipes a Node `Readable` directly to `ServerResponse` with backpressure
handling, without converting to web streams first.

- **`render-result.ts`**: Accepts `Readable` as a response type. When a
Node `Readable` is the response, `pipeToNodeResponse()` uses the new
native pipe path. `readable` and `pipeable` getters convert via
`Readable.toWeb()` only when needed.

- **`app-render.tsx`**: Replaced `ReadableStream<Uint8Array>` with
`AnyStream` throughout. Replaced `.tee()` calls with `teeStream()`.
Updated `accumulateStreamChunks` to handle both stream types with a
shared `accumulateChunk` helper. Removed `nodeStreamFromReadableStream`
utility (no longer needed).

- **`instant-validation.tsx`**: Changed from directly importing
`renderToReadableStream` from `react-server-dom-webpack/server` to using
the `renderToFlightStream` function from `stream-ops`, so validation
uses the same stream backend as the main render.

- **`use-flight-response.tsx`**: Updated debug stream handling to accept
both `Readable` and `ReadableStream`, converting as needed.
parent 0aec1df2
import type { Readable } from 'node:stream'
import { InvariantError } from '../../shared/lib/invariant-error' import { InvariantError } from '../../shared/lib/invariant-error'
export type AnyStream = ReadableStream<Uint8Array> | Readable
function isWebStream(stream: AnyStream): stream is ReadableStream<Uint8Array> {
return typeof (stream as ReadableStream).tee === 'function'
}
// React's RSC prerender function will emit an incomplete flight stream when using `prerender`. If the connection // React's RSC prerender function will emit an incomplete flight stream when using `prerender`. If the connection
// closes then whatever hanging chunks exist will be errored. This is because prerender (an experimental feature) // closes then whatever hanging chunks exist will be errored. This is because prerender (an experimental feature)
// has not yet implemented a concept of resume. For now we will simulate a paused connection by wrapping the stream // has not yet implemented a concept of resume. For now we will simulate a paused connection by wrapping the stream
// in one that doesn't close even when the underlying is complete. // in one that doesn't close even when the underlying is complete.
export class ReactServerResult { export class ReactServerResult {
private _stream: null | ReadableStream<Uint8Array> private _stream: null | AnyStream
constructor(stream: ReadableStream<Uint8Array>) { constructor(stream: AnyStream) {
this._stream = stream this._stream = stream
} }
tee() { tee(): AnyStream {
if (this._stream === null) { if (this._stream === null) {
throw new Error( throw new Error(
'Cannot tee a ReactServerResult that has already been consumed' 'Cannot tee a ReactServerResult that has already been consumed'
) )
} }
const tee = this._stream.tee() if (isWebStream(this._stream)) {
this._stream = tee[0] const tee = this._stream.tee()
return tee[1] this._stream = tee[0]
return tee[1]
}
let Readable: typeof import('node:stream').Readable
if (process.env.TURBOPACK) {
Readable = (require('node:stream') as typeof import('node:stream'))
.Readable
} else {
Readable = (
__non_webpack_require__('node:stream') as typeof import('node:stream')
).Readable
}
const webStream = Readable.toWeb(this._stream) as ReadableStream<Uint8Array>
const tee = webStream.tee()
this._stream = Readable.fromWeb(
tee[0] as import('stream/web').ReadableStream
)
return Readable.fromWeb(tee[1] as import('stream/web').ReadableStream)
} }
consume() { consume(): AnyStream {
if (this._stream === null) { if (this._stream === null) {
throw new Error( throw new Error(
'Cannot consume a ReactServerResult that has already been consumed' 'Cannot consume a ReactServerResult that has already been consumed'
...@@ -55,18 +80,26 @@ export async function createReactServerPrerenderResult( ...@@ -55,18 +80,26 @@ export async function createReactServerPrerenderResult(
} }
export async function createReactServerPrerenderResultFromRender( export async function createReactServerPrerenderResultFromRender(
underlying: ReadableStream<Uint8Array> underlying: AnyStream
): Promise<ReactServerPrerenderResult> { ): Promise<ReactServerPrerenderResult> {
const chunks: Array<Uint8Array> = [] const chunks: Array<Uint8Array> = []
const reader = underlying.getReader()
while (true) { if (isWebStream(underlying)) {
const { done, value } = await reader.read() const reader = underlying.getReader()
if (done) { while (true) {
break const { done, value } = await reader.read()
} else { if (done) {
chunks.push(value) break
} else {
chunks.push(value)
}
}
} else {
for await (const chunk of underlying) {
chunks.push(chunk instanceof Uint8Array ? chunk : new Uint8Array(chunk))
} }
} }
return new ReactServerPrerenderResult(chunks) return new ReactServerPrerenderResult(chunks)
} }
export class ReactServerPrerenderResult { export class ReactServerPrerenderResult {
......
/**
* Node debug channel implementation.
* Loaded by debug-channel-server.ts when __NEXT_USE_NODE_STREAMS is enabled.
*/
import { PassThrough, Writable } from 'node:stream'
import type { DebugChannelPair } from './debug-channel-server.web'
export function createDebugChannel(): DebugChannelPair | undefined {
if (process.env.NODE_ENV === 'production') {
return undefined
}
return createNodeDebugChannel()
}
function createNodeDebugChannel(): DebugChannelPair {
const readable = new PassThrough()
// Use a plain Writable instead of exposing the PassThrough directly.
// React's renderToPipeableStream detects .read() on the debugChannel and
// enters bidirectional mode, reading its own output back as commands.
const writable = new Writable({
write(chunk, _encoding, callback) {
readable.push(chunk)
callback()
},
final(callback) {
readable.push(null)
callback()
},
})
return {
serverSide: writable,
clientSide: { readable },
}
}
/** /**
* Compile-time switcher for debug channel operations. * Compile-time switcher for debug channel operations.
* *
* Simple re-export from the web implementation. * When __NEXT_USE_NODE_STREAMS is true, uses a Node PassThrough-based channel.
* A future change will add a conditional branch for node streams. * Otherwise, uses web WritableStream APIs.
*/ */
export type { export type {
DebugChannelPair, DebugChannelPair,
DebugChannelServer, DebugChannelServer,
} from './debug-channel-server.web' } from './debug-channel-server.web'
export { type DebugChannelMod = {
createDebugChannel, createDebugChannel: typeof import('./debug-channel-server.web').createDebugChannel
toNodeDebugChannel, }
} from './debug-channel-server.web'
let _m: DebugChannelMod
if (process.env.__NEXT_USE_NODE_STREAMS) {
_m =
require('./debug-channel-server.node') as typeof import('./debug-channel-server.node')
} else {
_m =
require('./debug-channel-server.web') as typeof import('./debug-channel-server.web')
}
export const createDebugChannel = _m.createDebugChannel
...@@ -3,20 +3,20 @@ ...@@ -3,20 +3,20 @@
* Loaded by debug-channel-server.ts. * Loaded by debug-channel-server.ts.
*/ */
// Types defined inline for now; will move to debug-channel-server.node.ts later. import type { AnyStream } from './app-render-prerender-utils'
export type DebugChannelPair = { export type DebugChannelPair = {
serverSide: DebugChannelServer serverSide: DebugChannelServer
clientSide: DebugChannelClient clientSide: DebugChannelClient
} }
export type DebugChannelServer = { // Opaque: PassThrough on node, { writable: WritableStream } on web.
readable?: ReadableStream<Uint8Array> // Each React render API handles its own variant.
writable: WritableStream<Uint8Array>
} export type DebugChannelServer = any
type DebugChannelClient = { type DebugChannelClient = {
readable: ReadableStream<Uint8Array> readable: AnyStream
writable?: WritableStream<Uint8Array>
} }
export function createDebugChannel(): DebugChannelPair | undefined { export function createDebugChannel(): DebugChannelPair | undefined {
...@@ -52,15 +52,3 @@ export function createWebDebugChannel(): DebugChannelPair { ...@@ -52,15 +52,3 @@ export function createWebDebugChannel(): DebugChannelPair {
clientSide: { readable: clientSideReadable }, clientSide: { readable: clientSideReadable },
} }
} }
/**
* toNodeDebugChannel is a no-op stub on the web path.
* It should never be called in edge/web builds.
*/
export function toNodeDebugChannel(
_webDebugChannel: DebugChannelServer
): never {
throw new Error(
'toNodeDebugChannel cannot be used in edge/web runtime, this is a bug in the Next.js codebase'
)
}
import { RSC_CONTENT_TYPE_HEADER } from '../../client/components/app-router-headers' import { RSC_CONTENT_TYPE_HEADER } from '../../client/components/app-router-headers'
import RenderResult, { type RenderResultMetadata } from '../render-result' import RenderResult, { type RenderResultMetadata } from '../render-result'
import type { AnyStream } from './stream-ops'
/** /**
* Flight Response is always set to RSC_CONTENT_TYPE_HEADER to ensure it does not get interpreted as HTML. * Flight Response is always set to RSC_CONTENT_TYPE_HEADER to ensure it does not get interpreted as HTML.
*/ */
export class FlightRenderResult extends RenderResult { export class FlightRenderResult extends RenderResult {
constructor( constructor(
response: string | ReadableStream<Uint8Array>, response: string | AnyStream,
metadata: RenderResultMetadata = {}, metadata: RenderResultMetadata = {},
waitUntil?: Promise<unknown> waitUntil?: Promise<unknown>
) { ) {
......
...@@ -42,10 +42,10 @@ import { ...@@ -42,10 +42,10 @@ import {
createNodeStreamFromChunks, createNodeStreamFromChunks,
} from './stream-utils' } from './stream-utils'
import { createDebugChannel } from '../debug-channel-server' import { createDebugChannel } from '../debug-channel-server'
import type { FlightComponentMod } from '../stream-ops'
// eslint-disable-next-line import/no-extraneous-dependencies // eslint-disable-next-line import/no-extraneous-dependencies
import { createFromNodeStream } from 'react-server-dom-webpack/client' import { createFromNodeStream } from 'react-server-dom-webpack/client'
// eslint-disable-next-line import/no-extraneous-dependencies
import { renderToReadableStream } from 'react-server-dom-webpack/server'
import { import {
addSearchParamsIfPageSegment, addSearchParamsIfPageSegment,
isGroupSegment, isGroupSegment,
...@@ -203,7 +203,16 @@ export type StageEndTimes = { ...@@ -203,7 +203,16 @@ export type StageEndTimes = {
* Splits an existing staged stream (represented as arrays of chunks) * Splits an existing staged stream (represented as arrays of chunks)
* into separate staged streams (also in arrays-of-chunks form), one for each segment. * into separate staged streams (also in arrays-of-chunks form), one for each segment.
* */ * */
type RenderToFlightStream = (
ComponentMod: FlightComponentMod,
payload: any,
clientModules: any,
opts: any
) => AsyncIterable<Uint8Array>
export async function collectStagedSegmentData( export async function collectStagedSegmentData(
ComponentMod: FlightComponentMod,
renderFlightStream: RenderToFlightStream,
fullPageChunks: StageChunks, fullPageChunks: StageChunks,
fullPageDebugChunks: Uint8Array[] | null, fullPageDebugChunks: Uint8Array[] | null,
startTime: number, startTime: number,
...@@ -291,7 +300,8 @@ export async function collectStagedSegmentData( ...@@ -291,7 +300,8 @@ export async function collectStagedSegmentData(
? createDebugChannel() ? createDebugChannel()
: undefined : undefined
const itemStream = renderToReadableStream( const itemStream = renderFlightStream(
ComponentMod,
data, data,
clientReferenceManifest.clientModules, clientReferenceManifest.clientModules,
{ {
...@@ -335,14 +345,14 @@ export async function collectStagedSegmentData( ...@@ -335,14 +345,14 @@ export async function collectStagedSegmentData(
await Promise.all([ await Promise.all([
// accumulate Flight chunks // accumulate Flight chunks
(async () => { (async () => {
for await (const chunk of itemStream.values()) { for await (const chunk of itemStream) {
writeChunk(cacheEntry.chunks, controller.currentStage, chunk) writeChunk(cacheEntry.chunks, controller.currentStage, chunk)
} }
})(), })(),
// accumulate Debug chunks // accumulate Debug chunks
segmentDebugChannel && segmentDebugChannel &&
(async () => { (async () => {
for await (const chunk of segmentDebugChannel.clientSide.readable.values()) { for await (const chunk of segmentDebugChannel.clientSide.readable) {
cacheEntry.debugChunks!.push(chunk) cacheEntry.debugChunks!.push(chunk)
} }
})(), })(),
...@@ -510,6 +520,8 @@ function writeChunk( ...@@ -510,6 +520,8 @@ function writeChunk(
* to provide extra debug info. * to provide extra debug info.
* */ * */
export async function createCombinedPayloadStream( export async function createCombinedPayloadStream(
ComponentMod: FlightComponentMod,
renderFlightStream: RenderToFlightStream,
payload: InitialRSCPayload, payload: InitialRSCPayload,
extraChunksAbortController: AbortController, extraChunksAbortController: AbortController,
renderSignal: AbortSignal, renderSignal: AbortSignal,
...@@ -530,7 +542,8 @@ export async function createCombinedPayloadStream( ...@@ -530,7 +542,8 @@ export async function createCombinedPayloadStream(
await runInSequentialTasks( await runInSequentialTasks(
() => { () => {
const stream = renderToReadableStream( const stream = renderFlightStream(
ComponentMod,
payload, payload,
clientReferenceManifest.clientModules, clientReferenceManifest.clientModules,
{ {
...@@ -573,7 +586,7 @@ export async function createCombinedPayloadStream( ...@@ -573,7 +586,7 @@ export async function createCombinedPayloadStream(
streamFinished = Promise.all([ streamFinished = Promise.all([
// Accumulate Flight chunks // Accumulate Flight chunks
(async () => { (async () => {
for await (const chunk of stream.values()) { for await (const chunk of stream) {
allChunks.push(chunk) allChunks.push(chunk)
if (isRenderable) { if (isRenderable) {
renderableChunks.push(chunk) renderableChunks.push(chunk)
...@@ -583,7 +596,7 @@ export async function createCombinedPayloadStream( ...@@ -583,7 +596,7 @@ export async function createCombinedPayloadStream(
// Accumulate debug chunks // Accumulate debug chunks
debugChannel && debugChannel &&
(async () => { (async () => {
for await (const chunk of debugChannel.clientSide.readable.values()) { for await (const chunk of debugChannel.clientSide.readable) {
debugChunks!.push(chunk) debugChunks!.push(chunk)
} }
})(), })(),
......
This diff is collapsed.
/** /**
* Compile-time switcher for stream operations. * Compile-time switcher for stream operations.
* *
* PR2: Simple re-export from the web implementation. * When __NEXT_USE_NODE_STREAMS is true, uses Node.js pipeable stream APIs.
* A future change will add a conditional branch for node streams. * Otherwise, uses web ReadableStream APIs.
*
* Both modules export AnyStream = AnyStreamType so their type surfaces are
* structurally identical — no `as unknown as` cast is needed.
*/ */
export type { export type {
AnyStream, AnyStream,
...@@ -18,26 +21,35 @@ export type { ...@@ -18,26 +21,35 @@ export type {
FizzStreamResult, FizzStreamResult,
} from './stream-ops.web' } from './stream-ops.web'
export { type WebMod = typeof import('./stream-ops.web')
continueFizzStream,
continueStaticPrerender, let _m: WebMod
continueDynamicPrerender, if (process.env.__NEXT_USE_NODE_STREAMS) {
continueStaticFallbackPrerender, _m = require('./stream-ops.node') as typeof import('./stream-ops.node')
continueDynamicHTMLResume, } else {
streamToBuffer, _m = require('./stream-ops.web') as typeof import('./stream-ops.web')
chainStreams, }
createDocumentClosingStream,
processPrelude, export const continueFizzStream = _m.continueFizzStream
nodeReadableToWeb, export const continueStaticPrerender = _m.continueStaticPrerender
createInlinedDataStream, export const continueDynamicPrerender = _m.continueDynamicPrerender
createPendingStream, export const continueStaticFallbackPrerender =
createOnHeadersCallback, _m.continueStaticFallbackPrerender
resumeAndAbort, export const continueDynamicHTMLResume = _m.continueDynamicHTMLResume
renderToFlightStream, export const streamToBuffer = _m.streamToBuffer
streamToString, export const chainStreams = _m.chainStreams
renderToFizzStream, export const createDocumentClosingStream = _m.createDocumentClosingStream
resumeToFizzStream, export const processPrelude = _m.processPrelude
getServerPrerender, export const createInlinedDataStream = _m.createInlinedDataStream
getClientPrerender, export const createPendingStream = _m.createPendingStream
pipeRuntimePrefetchTransform, export const createOnHeadersCallback = _m.createOnHeadersCallback
} from './stream-ops.web' export const resumeAndAbort = _m.resumeAndAbort
export const renderToFlightStream = _m.renderToFlightStream
export const streamToString = _m.streamToString
export const streamToUint8Array = _m.streamToUint8Array
export const renderToFizzStream = _m.renderToFizzStream
export const resumeToFizzStream = _m.resumeToFizzStream
export const getServerPrerender = _m.getServerPrerender
export const getClientPrerender = _m.getClientPrerender
export const pipeRuntimePrefetchTransform = _m.pipeRuntimePrefetchTransform
export const teeStream = _m.teeStream
/** /**
* Web stream operations for the rendering pipeline. * Web stream operations for the rendering pipeline.
* Loaded by stream-ops.ts (re-export in this PR, conditional switcher later). * Loaded by stream-ops.ts when __NEXT_USE_NODE_STREAMS is false (default).
*
* AnyStream = AnyStreamType so the exported type surface matches stream-ops.node.ts,
* allowing the switcher to assign either module without `as unknown as`.
*/ */
import type { PostponedState, PrerenderOptions } from 'react-dom/static' import type { PostponedState, PrerenderOptions } from 'react-dom/static'
...@@ -12,11 +15,21 @@ import { ...@@ -12,11 +15,21 @@ import {
streamToString as webStreamToString, streamToString as webStreamToString,
createRuntimePrefetchTransformStream, createRuntimePrefetchTransformStream,
continueFizzStream as webContinueFizzStream, continueFizzStream as webContinueFizzStream,
continueStaticPrerender as webContinueStaticPrerender,
continueDynamicPrerender as webContinueDynamicPrerender,
continueStaticFallbackPrerender as webContinueStaticFallbackPrerender,
continueDynamicHTMLResume as webContinueDynamicHTMLResume,
streamToBuffer as webStreamToBuffer,
streamToUint8Array as webStreamToUint8Array,
chainStreams as webChainStreams,
createDocumentClosingStream as webCreateDocumentClosingStream,
} from '../stream-utils/node-web-streams-helper' } from '../stream-utils/node-web-streams-helper'
import { createInlinedDataReadableStream } from './use-flight-response' import { createInlinedDataReadableStream } from './use-flight-response'
import { processPrelude as webProcessPrelude } from './app-render-prerender-utils'
import type { AnyStream as AnyStreamType } from './app-render-prerender-utils'
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Shared types (web-only for now; will move to stream-ops.node.ts later) // Shared types
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
type FlightRenderToReadableStream = ( type FlightRenderToReadableStream = (
...@@ -25,7 +38,7 @@ type FlightRenderToReadableStream = ( ...@@ -25,7 +38,7 @@ type FlightRenderToReadableStream = (
options?: any options?: any
) => ReadableStream<Uint8Array> ) => ReadableStream<Uint8Array>
export type AnyStream = ReadableStream<Uint8Array> export type AnyStream = AnyStreamType
export type ContinueStreamSharedOptions = { export type ContinueStreamSharedOptions = {
deploymentId: string | undefined deploymentId: string | undefined
...@@ -69,37 +82,102 @@ export type FizzStreamResult = { ...@@ -69,37 +82,102 @@ export type FizzStreamResult = {
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Continue functions // Continue function wrappers
// Thin wrappers that accept AnyStream and narrow to
// ReadableStream<Uint8Array> internally for the web helper functions.
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
export {
continueStaticPrerender,
continueDynamicPrerender,
continueStaticFallbackPrerender,
continueDynamicHTMLResume,
streamToBuffer,
chainStreams,
createDocumentClosingStream,
} from '../stream-utils/node-web-streams-helper'
export { processPrelude } from './app-render-prerender-utils'
/**
* Wrapper for continueFizzStream that accepts AnyStream.
* The underlying implementation expects ReactDOMServerReadableStream but at
* the stream-ops boundary we only expose AnyStream.
*/
export function continueFizzStream( export function continueFizzStream(
renderStream: AnyStream, renderStream: AnyStream,
opts: ContinueFizzStreamOptions opts: ContinueFizzStreamOptions
): Promise<ReadableStream<Uint8Array>> { ): Promise<AnyStream> {
return webContinueFizzStream(renderStream as any, opts) return webContinueFizzStream(
renderStream as ReadableStream<Uint8Array> as any,
{
...opts,
inlinedDataStream: opts.inlinedDataStream as
| ReadableStream<Uint8Array>
| undefined,
}
)
} }
// Not available in web bundles export async function continueStaticPrerender(
export const nodeReadableToWeb: prerenderStream: AnyStream,
| ((readable: import('node:stream').Readable) => ReadableStream<Uint8Array>) opts: ContinueStaticPrerenderOptions
| undefined = undefined ): Promise<AnyStream> {
return webContinueStaticPrerender(
prerenderStream as ReadableStream<Uint8Array>,
{
...opts,
inlinedDataStream: opts.inlinedDataStream as ReadableStream<Uint8Array>,
}
)
}
export async function continueDynamicPrerender(
prerenderStream: AnyStream,
opts: {
getServerInsertedHTML: () => Promise<string>
getServerInsertedMetadata: () => Promise<string>
deploymentId: string | undefined
}
): Promise<AnyStream> {
return webContinueDynamicPrerender(
prerenderStream as ReadableStream<Uint8Array>,
opts
)
}
export async function continueStaticFallbackPrerender(
prerenderStream: AnyStream,
opts: ContinueStaticPrerenderOptions
): Promise<AnyStream> {
return webContinueStaticFallbackPrerender(
prerenderStream as ReadableStream<Uint8Array>,
{
...opts,
inlinedDataStream: opts.inlinedDataStream as ReadableStream<Uint8Array>,
}
)
}
export async function continueDynamicHTMLResume(
renderStream: AnyStream,
opts: ContinueDynamicHTMLResumeOptions
): Promise<AnyStream> {
return webContinueDynamicHTMLResume(
renderStream as ReadableStream<Uint8Array>,
{
...opts,
inlinedDataStream: opts.inlinedDataStream as ReadableStream<Uint8Array>,
}
)
}
export async function streamToBuffer(stream: AnyStream): Promise<Buffer> {
return webStreamToBuffer(stream as ReadableStream<Uint8Array>)
}
export async function streamToUint8Array(
stream: AnyStream
): Promise<Uint8Array> {
return webStreamToUint8Array(stream as ReadableStream<Uint8Array>)
}
export function chainStreams(...streams: AnyStream[]): AnyStream {
return webChainStreams(...(streams as ReadableStream<Uint8Array>[]))
}
export function createDocumentClosingStream(): AnyStream {
return webCreateDocumentClosingStream()
}
export async function processPrelude(
unprocessedPrelude: AnyStream
): Promise<{ prelude: AnyStream; preludeIsEmpty: boolean }> {
return webProcessPrelude(unprocessedPrelude as ReadableStream<Uint8Array>)
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Composed helpers // Composed helpers
...@@ -196,3 +274,7 @@ export function pipeRuntimePrefetchTransform( ...@@ -196,3 +274,7 @@ export function pipeRuntimePrefetchTransform(
createRuntimePrefetchTransformStream(sentinel, isPartial, staleTime) createRuntimePrefetchTransformStream(sentinel, isPartial, staleTime)
) )
} }
export function teeStream(stream: AnyStream): [AnyStream, AnyStream] {
return (stream as ReadableStream<Uint8Array>).tee()
}
...@@ -21,6 +21,7 @@ import type { IncomingMessage } from 'http' ...@@ -21,6 +21,7 @@ import type { IncomingMessage } from 'http'
import type { RenderResumeDataCache } from '../resume-data-cache/resume-data-cache' import type { RenderResumeDataCache } from '../resume-data-cache/resume-data-cache'
import type { ServerCacheStatus } from '../../next-devtools/dev-overlay/cache-indicator' import type { ServerCacheStatus } from '../../next-devtools/dev-overlay/cache-indicator'
import type { PrefetchHints } from '../../shared/lib/app-router-types' import type { PrefetchHints } from '../../shared/lib/app-router-types'
import type { AnyStream } from './stream-ops'
const dynamicParamTypesSchema = s.enums([ const dynamicParamTypesSchema = s.enums([
'c', 'c',
...@@ -115,12 +116,12 @@ export interface RenderOptsPartial { ...@@ -115,12 +116,12 @@ export interface RenderOptsPartial {
setCacheStatus?: (status: ServerCacheStatus, htmlRequestId: string) => void setCacheStatus?: (status: ServerCacheStatus, htmlRequestId: string) => void
setIsrStatus?: (key: string, value: boolean | undefined) => void setIsrStatus?: (key: string, value: boolean | undefined) => void
setReactDebugChannel?: ( setReactDebugChannel?: (
debugChannel: { readable: ReadableStream<Uint8Array> }, debugChannel: { readable: AnyStream },
htmlRequestId: string, htmlRequestId: string,
requestId: string requestId: string
) => void ) => void
sendErrorsToBrowser?: ( sendErrorsToBrowser?: (
errorsRscStream: ReadableStream<Uint8Array>, errorsRscStream: AnyStream,
htmlRequestId: string htmlRequestId: string
) => void ) => void
isBuildTimePrerendering?: boolean isBuildTimePrerendering?: boolean
......
...@@ -76,9 +76,17 @@ export function getFlightStream<T>( ...@@ -76,9 +76,17 @@ export function getFlightStream<T>(
const { Readable } = const { Readable } =
require('node:stream') as typeof import('node:stream') require('node:stream') as typeof import('node:stream')
// The types of flightStream and debugStream should match. // Convert debug stream to Readable if it's a ReadableStream.
if (debugStream && !(debugStream instanceof Readable)) { // When __NEXT_USE_NODE_STREAMS is enabled, the debug channel produces
throw new InvariantError('Expected debug stream to be a Readable') // Node Readables natively. Otherwise, it produces web ReadableStreams.
let nodeDebugStream: Readable | undefined
if (debugStream) {
if (debugStream instanceof Readable) {
nodeDebugStream = debugStream
} else {
type WebReadableStream = import('stream/web').ReadableStream
nodeDebugStream = Readable.fromWeb(debugStream as WebReadableStream)
}
} }
// react-server-dom-webpack/client.edge must not be hoisted for require cache clearing to work correctly // react-server-dom-webpack/client.edge must not be hoisted for require cache clearing to work correctly
...@@ -96,7 +104,7 @@ export function getFlightStream<T>( ...@@ -96,7 +104,7 @@ export function getFlightStream<T>(
{ {
findSourceMapURL, findSourceMapURL,
nonce, nonce,
debugChannel: debugStream, debugChannel: nodeDebugStream,
endTime: debugEndTime, endTime: debugEndTime,
} }
) )
......
import type { Readable } from 'node:stream'
import { createBufferedTransformStream } from '../stream-utils/node-web-streams-helper' import { createBufferedTransformStream } from '../stream-utils/node-web-streams-helper'
import { import {
HMR_MESSAGE_SENT_TO_BROWSER, HMR_MESSAGE_SENT_TO_BROWSER,
type HmrMessageSentToBrowser, type HmrMessageSentToBrowser,
} from './hot-reloader-types' } from './hot-reloader-types'
import type { AnyStream } from '../app-render/stream-ops'
function toWebReadableStream(stream: AnyStream): ReadableStream<Uint8Array> {
if (stream instanceof ReadableStream) {
return stream
}
const { Readable: ReadableClass } =
require('node:stream') as typeof import('node:stream')
return ReadableClass.toWeb(stream as Readable) as ReadableStream<Uint8Array>
}
export interface ReactDebugChannelForBrowser { export interface ReactDebugChannelForBrowser {
readonly readable: ReadableStream<Uint8Array> readonly readable: AnyStream
// Might also get a writable stream as return channel in the future.
} }
const reactDebugChannelsByHtmlRequestId = new Map< const reactDebugChannelsByHtmlRequestId = new Map<
...@@ -19,7 +29,7 @@ export function connectReactDebugChannel( ...@@ -19,7 +29,7 @@ export function connectReactDebugChannel(
debugChannel: ReactDebugChannelForBrowser, debugChannel: ReactDebugChannelForBrowser,
sendToClient: (message: HmrMessageSentToBrowser) => void sendToClient: (message: HmrMessageSentToBrowser) => void
) { ) {
const reader = debugChannel.readable const reader = toWebReadableStream(debugChannel.readable)
.pipeThrough( .pipeThrough(
// We're sending the chunks in batches to reduce overhead in the browser. // We're sending the chunks in batches to reduce overhead in the browser.
createBufferedTransformStream({ maxBufferByteLength: 128 * 1024 }) createBufferedTransformStream({ maxBufferByteLength: 128 * 1024 })
......
...@@ -14,6 +14,7 @@ import type { ...@@ -14,6 +14,7 @@ import type {
} from '../../next-devtools/dev-overlay/cache-indicator' } from '../../next-devtools/dev-overlay/cache-indicator'
import type { DevToolsConfig } from '../../next-devtools/dev-overlay/shared' import type { DevToolsConfig } from '../../next-devtools/dev-overlay/shared'
import type { ReactDebugChannelForBrowser } from './debug-channel' import type { ReactDebugChannelForBrowser } from './debug-channel'
import type { AnyStream } from '../app-render/stream-ops'
export const enum HMR_MESSAGE_SENT_TO_BROWSER { export const enum HMR_MESSAGE_SENT_TO_BROWSER {
// JSON messages: // JSON messages:
...@@ -242,10 +243,7 @@ export interface NextJsHotReloaderInterface { ...@@ -242,10 +243,7 @@ export interface NextJsHotReloaderInterface {
htmlRequestId: string, htmlRequestId: string,
requestId: string requestId: string
): void ): void
sendErrorsToBrowser( sendErrorsToBrowser(errorsRscStream: AnyStream, htmlRequestId: string): void
errorsRscStream: ReadableStream<Uint8Array>,
htmlRequestId: string
): void
getCompilationErrors(page: string): Promise<any[]> getCompilationErrors(page: string): Promise<any[]>
onHMR( onHMR(
req: IncomingMessage, req: IncomingMessage,
......
...@@ -5,6 +5,7 @@ import type { Telemetry } from '../../telemetry/storage' ...@@ -5,6 +5,7 @@ import type { Telemetry } from '../../telemetry/storage'
import type { IncomingMessage, ServerResponse } from 'http' import type { IncomingMessage, ServerResponse } from 'http'
import type { UrlObject } from 'url' import type { UrlObject } from 'url'
import type { RouteDefinition } from '../route-definitions/route-definition' import type { RouteDefinition } from '../route-definitions/route-definition'
import type { AnyStream } from '../app-render/stream-ops'
import { type webpack, StringXor } from 'next/dist/compiled/webpack/webpack' import { type webpack, StringXor } from 'next/dist/compiled/webpack/webpack'
import { import {
...@@ -1820,7 +1821,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface { ...@@ -1820,7 +1821,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
} }
public sendErrorsToBrowser( public sendErrorsToBrowser(
errorsRscStream: ReadableStream<Uint8Array>, errorsRscStream: AnyStream,
htmlRequestId: string htmlRequestId: string
): void { ): void {
const client = this.webpackHotMiddleware?.getClient(htmlRequestId) const client = this.webpackHotMiddleware?.getClient(htmlRequestId)
......
import { streamToUint8Array } from '../stream-utils/node-web-streams-helper'
import { import {
HMR_MESSAGE_SENT_TO_BROWSER, HMR_MESSAGE_SENT_TO_BROWSER,
type HmrMessageSentToBrowser, type HmrMessageSentToBrowser,
} from './hot-reloader-types' } from './hot-reloader-types'
import type { AnyStream } from '../app-render/stream-ops'
import { streamToUint8Array } from '../app-render/stream-ops'
const errorsRscStreamsByHtmlRequestId = new Map< const errorsRscStreamsByHtmlRequestId = new Map<string, AnyStream>()
string,
ReadableStream<Uint8Array>
>()
export function sendSerializedErrorsToClient( export function sendSerializedErrorsToClient(
errorsRscStream: ReadableStream<Uint8Array>, errorsRscStream: AnyStream,
sendToClient: (message: HmrMessageSentToBrowser) => void sendToClient: (message: HmrMessageSentToBrowser) => void
) { ) {
streamToUint8Array(errorsRscStream).then( streamToUint8Array(errorsRscStream).then(
...@@ -43,7 +41,7 @@ export function sendSerializedErrorsToClientForHtmlRequest( ...@@ -43,7 +41,7 @@ export function sendSerializedErrorsToClientForHtmlRequest(
export function setErrorsRscStreamForHtmlRequest( export function setErrorsRscStreamForHtmlRequest(
htmlRequestId: string, htmlRequestId: string,
errorsRscStream: ReadableStream<Uint8Array> errorsRscStream: AnyStream
) { ) {
// TODO: Clean up after a timeout, in case the client never connects, e.g. // TODO: Clean up after a timeout, in case the client never connects, e.g.
// when CURL'ing the page, or loading the page with JavaScript disabled etc. // when CURL'ing the page, or loading the page with JavaScript disabled etc.
......
...@@ -2,6 +2,7 @@ import type { IncomingMessage, ServerResponse } from 'node:http' ...@@ -2,6 +2,7 @@ import type { IncomingMessage, ServerResponse } from 'node:http'
import type { NextConfigRuntime } from '../../config-shared' import type { NextConfigRuntime } from '../../config-shared'
import type { UrlWithParsedQuery } from 'node:url' import type { UrlWithParsedQuery } from 'node:url'
import type { ServerCacheStatus } from '../../../next-devtools/dev-overlay/cache-indicator' import type { ServerCacheStatus } from '../../../next-devtools/dev-overlay/cache-indicator'
import type { AnyStream } from '../../app-render/stream-ops'
export type RevalidateFn = (config: { export type RevalidateFn = (config: {
urlPath: string urlPath: string
...@@ -40,13 +41,13 @@ export type RouterServerContext = Record< ...@@ -40,13 +41,13 @@ export type RouterServerContext = Record<
// allow setting ISR status in dev // allow setting ISR status in dev
setIsrStatus?: (key: string, value: boolean | undefined) => void setIsrStatus?: (key: string, value: boolean | undefined) => void
setReactDebugChannel?: ( setReactDebugChannel?: (
debugChannel: { readable: ReadableStream<Uint8Array> }, debugChannel: { readable: AnyStream },
htmlRequestId: string, htmlRequestId: string,
requestId: string requestId: string
) => void ) => void
setCacheStatus?: (status: ServerCacheStatus, htmlRequestId: string) => void setCacheStatus?: (status: ServerCacheStatus, htmlRequestId: string) => void
sendErrorsToBrowser?: ( sendErrorsToBrowser?: (
errorsRscStream: ReadableStream<Uint8Array>, errorsRscStream: AnyStream,
htmlRequestId: string htmlRequestId: string
) => void ) => void
// indicates request handlers are already wrapped by next-server tracing // indicates request handlers are already wrapped by next-server tracing
......
import type { ServerResponse } from 'node:http' import type { ServerResponse } from 'node:http'
import type { Readable } from 'node:stream'
import { import {
ResponseAbortedName, ResponseAbortedName,
...@@ -144,3 +145,97 @@ export async function pipeToNodeResponse( ...@@ -144,3 +145,97 @@ export async function pipeToNodeResponse(
throw new Error('failed to pipe response', { cause: err }) throw new Error('failed to pipe response', { cause: err })
} }
} }
export async function pipeNodeReadableToNodeResponse(
readable: Readable,
res: ServerResponse,
waitUntilForEnd?: Promise<unknown>
) {
try {
const { errored, destroyed } = res
if (errored || destroyed) return
let started = false
const finished = new DetachedPromise<void>()
res.once('close', () => {
readable.destroy()
finished.resolve()
})
readable.on('data', (chunk: Buffer) => {
if (!started) {
started = true
if (
'performance' in globalThis &&
process.env.NEXT_OTEL_PERFORMANCE_PREFIX
) {
const metrics = getClientComponentLoaderMetrics()
if (metrics) {
performance.measure(
`${process.env.NEXT_OTEL_PERFORMANCE_PREFIX}:next-client-component-loading`,
{
start: metrics.clientComponentLoadStart,
end:
metrics.clientComponentLoadStart +
metrics.clientComponentLoadTimes,
}
)
}
}
res.flushHeaders()
getTracer().trace(
NextNodeServerSpan.startResponse,
{
spanName: 'start response',
},
() => undefined
)
}
const ok = res.write(chunk)
if ('flush' in res && typeof res.flush === 'function') {
res.flush()
}
if (!ok) {
readable.pause()
res.once('drain', () => {
readable.resume()
})
}
})
readable.on('end', async () => {
if (waitUntilForEnd) {
await waitUntilForEnd
}
if (!res.writableFinished) {
res.end()
}
finished.resolve()
})
readable.on('error', (err) => {
if (isAbortError(err)) {
finished.resolve()
return
}
res.destroy(err)
finished.resolve()
})
await finished.promise
} catch (err: any) {
if (isAbortError(err)) return
throw new Error('failed to pipe response', { cause: err })
}
}
import type { OutgoingHttpHeaders, ServerResponse } from 'http' import type { OutgoingHttpHeaders, ServerResponse } from 'http'
import type { Readable } from 'stream'
import type { CacheControl } from './lib/cache-control' import type { CacheControl } from './lib/cache-control'
import type { FetchMetrics } from './base-http' import type { FetchMetrics } from './base-http'
import type { PrefetchHints } from '../shared/lib/app-router-types' import type { PrefetchHints } from '../shared/lib/app-router-types'
...@@ -9,7 +10,11 @@ import { ...@@ -9,7 +10,11 @@ import {
streamFromString, streamFromString,
streamToString, streamToString,
} from './stream-utils/node-web-streams-helper' } from './stream-utils/node-web-streams-helper'
import { isAbortError, pipeToNodeResponse } from './pipe-readable' import {
isAbortError,
pipeToNodeResponse,
pipeNodeReadableToNodeResponse,
} from './pipe-readable'
import type { RenderResumeDataCache } from './resume-data-cache/resume-data-cache' import type { RenderResumeDataCache } from './resume-data-cache/resume-data-cache'
import { InvariantError } from '../shared/lib/invariant-error' import { InvariantError } from '../shared/lib/invariant-error'
import type { import type {
...@@ -82,6 +87,7 @@ export type RenderResultMetadata = AppPageRenderResultMetadata & ...@@ -82,6 +87,7 @@ export type RenderResultMetadata = AppPageRenderResultMetadata &
export type RenderResultResponse = export type RenderResultResponse =
| ReadableStream<Uint8Array>[] | ReadableStream<Uint8Array>[]
| ReadableStream<Uint8Array> | ReadableStream<Uint8Array>
| Readable
| string | string
| Buffer | Buffer
| null | null
...@@ -94,6 +100,16 @@ export type RenderResultOptions< ...@@ -94,6 +100,16 @@ export type RenderResultOptions<
metadata: Metadata metadata: Metadata
} }
function isNodeReadable(value: unknown): value is Readable {
return (
value !== null &&
typeof value === 'object' &&
typeof (value as Record<string, unknown>).pipe === 'function' &&
typeof (value as Record<string, unknown>).on === 'function' &&
!(value instanceof ReadableStream)
)
}
export default class RenderResult< export default class RenderResult<
Metadata extends RenderResultMetadata = RenderResultMetadata, Metadata extends RenderResultMetadata = RenderResultMetadata,
> { > {
...@@ -231,6 +247,19 @@ export default class RenderResult< ...@@ -231,6 +247,19 @@ export default class RenderResult<
return chainStreams(...this.response) return chainStreams(...this.response)
} }
if (isNodeReadable(this.response)) {
let Readable: typeof import('node:stream').Readable
if (process.env.TURBOPACK) {
Readable = (require('node:stream') as typeof import('node:stream'))
.Readable
} else {
Readable = (
__non_webpack_require__('node:stream') as typeof import('node:stream')
).Readable
}
return Readable.toWeb(this.response) as ReadableStream<Uint8Array>
}
return this.response return this.response
} }
...@@ -253,6 +282,17 @@ export default class RenderResult< ...@@ -253,6 +282,17 @@ export default class RenderResult<
return this.response return this.response
} else if (Buffer.isBuffer(this.response)) { } else if (Buffer.isBuffer(this.response)) {
return [streamFromBuffer(this.response)] return [streamFromBuffer(this.response)]
} else if (isNodeReadable(this.response)) {
let Readable: typeof import('node:stream').Readable
if (process.env.TURBOPACK) {
Readable = (require('node:stream') as typeof import('node:stream'))
.Readable
} else {
Readable = (
__non_webpack_require__('node:stream') as typeof import('node:stream')
).Readable
}
return [Readable.toWeb(this.response) as ReadableStream<Uint8Array>]
} else { } else {
return [this.response] return [this.response]
} }
...@@ -349,6 +389,16 @@ export default class RenderResult< ...@@ -349,6 +389,16 @@ export default class RenderResult<
* @param res * @param res
*/ */
public async pipeToNodeResponse(res: ServerResponse) { public async pipeToNodeResponse(res: ServerResponse) {
if (
this.response !== null &&
typeof this.response !== 'string' &&
!Buffer.isBuffer(this.response) &&
!Array.isArray(this.response) &&
isNodeReadable(this.response)
) {
await pipeNodeReadableToNodeResponse(this.response, res, this.waitUntil)
return
}
await pipeToNodeResponse(this.readable, res, this.waitUntil) await pipeToNodeResponse(this.readable, res, this.waitUntil)
} }
} }
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