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'
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
// 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
// in one that doesn't close even when the underlying is complete.
export class ReactServerResult {
private _stream: null | ReadableStream<Uint8Array>
private _stream: null | AnyStream
constructor(stream: ReadableStream<Uint8Array>) {
constructor(stream: AnyStream) {
this._stream = stream
}
tee() {
tee(): AnyStream {
if (this._stream === null) {
throw new Error(
'Cannot tee a ReactServerResult that has already been consumed'
)
}
const tee = this._stream.tee()
this._stream = tee[0]
return tee[1]
if (isWebStream(this._stream)) {
const tee = this._stream.tee()
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) {
throw new Error(
'Cannot consume a ReactServerResult that has already been consumed'
......@@ -55,18 +80,26 @@ export async function createReactServerPrerenderResult(
}
export async function createReactServerPrerenderResultFromRender(
underlying: ReadableStream<Uint8Array>
underlying: AnyStream
): Promise<ReactServerPrerenderResult> {
const chunks: Array<Uint8Array> = []
const reader = underlying.getReader()
while (true) {
const { done, value } = await reader.read()
if (done) {
break
} else {
chunks.push(value)
if (isWebStream(underlying)) {
const reader = underlying.getReader()
while (true) {
const { done, value } = await reader.read()
if (done) {
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)
}
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.
*
* Simple re-export from the web implementation.
* A future change will add a conditional branch for node streams.
* When __NEXT_USE_NODE_STREAMS is true, uses a Node PassThrough-based channel.
* Otherwise, uses web WritableStream APIs.
*/
export type {
DebugChannelPair,
DebugChannelServer,
} from './debug-channel-server.web'
export {
createDebugChannel,
toNodeDebugChannel,
} from './debug-channel-server.web'
type DebugChannelMod = {
createDebugChannel: typeof import('./debug-channel-server.web').createDebugChannel
}
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 @@
* 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 = {
serverSide: DebugChannelServer
clientSide: DebugChannelClient
}
export type DebugChannelServer = {
readable?: ReadableStream<Uint8Array>
writable: WritableStream<Uint8Array>
}
// Opaque: PassThrough on node, { writable: WritableStream } on web.
// Each React render API handles its own variant.
export type DebugChannelServer = any
type DebugChannelClient = {
readable: ReadableStream<Uint8Array>
writable?: WritableStream<Uint8Array>
readable: AnyStream
}
export function createDebugChannel(): DebugChannelPair | undefined {
......@@ -52,15 +52,3 @@ export function createWebDebugChannel(): DebugChannelPair {
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 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.
*/
export class FlightRenderResult extends RenderResult {
constructor(
response: string | ReadableStream<Uint8Array>,
response: string | AnyStream,
metadata: RenderResultMetadata = {},
waitUntil?: Promise<unknown>
) {
......
......@@ -42,10 +42,10 @@ import {
createNodeStreamFromChunks,
} from './stream-utils'
import { createDebugChannel } from '../debug-channel-server'
import type { FlightComponentMod } from '../stream-ops'
// eslint-disable-next-line import/no-extraneous-dependencies
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 {
addSearchParamsIfPageSegment,
isGroupSegment,
......@@ -203,7 +203,16 @@ export type StageEndTimes = {
* Splits an existing staged stream (represented as arrays of chunks)
* 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(
ComponentMod: FlightComponentMod,
renderFlightStream: RenderToFlightStream,
fullPageChunks: StageChunks,
fullPageDebugChunks: Uint8Array[] | null,
startTime: number,
......@@ -291,7 +300,8 @@ export async function collectStagedSegmentData(
? createDebugChannel()
: undefined
const itemStream = renderToReadableStream(
const itemStream = renderFlightStream(
ComponentMod,
data,
clientReferenceManifest.clientModules,
{
......@@ -335,14 +345,14 @@ export async function collectStagedSegmentData(
await Promise.all([
// accumulate Flight chunks
(async () => {
for await (const chunk of itemStream.values()) {
for await (const chunk of itemStream) {
writeChunk(cacheEntry.chunks, controller.currentStage, chunk)
}
})(),
// accumulate Debug chunks
segmentDebugChannel &&
(async () => {
for await (const chunk of segmentDebugChannel.clientSide.readable.values()) {
for await (const chunk of segmentDebugChannel.clientSide.readable) {
cacheEntry.debugChunks!.push(chunk)
}
})(),
......@@ -510,6 +520,8 @@ function writeChunk(
* to provide extra debug info.
* */
export async function createCombinedPayloadStream(
ComponentMod: FlightComponentMod,
renderFlightStream: RenderToFlightStream,
payload: InitialRSCPayload,
extraChunksAbortController: AbortController,
renderSignal: AbortSignal,
......@@ -530,7 +542,8 @@ export async function createCombinedPayloadStream(
await runInSequentialTasks(
() => {
const stream = renderToReadableStream(
const stream = renderFlightStream(
ComponentMod,
payload,
clientReferenceManifest.clientModules,
{
......@@ -573,7 +586,7 @@ export async function createCombinedPayloadStream(
streamFinished = Promise.all([
// Accumulate Flight chunks
(async () => {
for await (const chunk of stream.values()) {
for await (const chunk of stream) {
allChunks.push(chunk)
if (isRenderable) {
renderableChunks.push(chunk)
......@@ -583,7 +596,7 @@ export async function createCombinedPayloadStream(
// Accumulate debug chunks
debugChannel &&
(async () => {
for await (const chunk of debugChannel.clientSide.readable.values()) {
for await (const chunk of debugChannel.clientSide.readable) {
debugChunks!.push(chunk)
}
})(),
......
This diff is collapsed.
/**
* Compile-time switcher for stream operations.
*
* PR2: Simple re-export from the web implementation.
* A future change will add a conditional branch for node streams.
* When __NEXT_USE_NODE_STREAMS is true, uses Node.js pipeable stream APIs.
* 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 {
AnyStream,
......@@ -18,26 +21,35 @@ export type {
FizzStreamResult,
} from './stream-ops.web'
export {
continueFizzStream,
continueStaticPrerender,
continueDynamicPrerender,
continueStaticFallbackPrerender,
continueDynamicHTMLResume,
streamToBuffer,
chainStreams,
createDocumentClosingStream,
processPrelude,
nodeReadableToWeb,
createInlinedDataStream,
createPendingStream,
createOnHeadersCallback,
resumeAndAbort,
renderToFlightStream,
streamToString,
renderToFizzStream,
resumeToFizzStream,
getServerPrerender,
getClientPrerender,
pipeRuntimePrefetchTransform,
} from './stream-ops.web'
type WebMod = typeof import('./stream-ops.web')
let _m: WebMod
if (process.env.__NEXT_USE_NODE_STREAMS) {
_m = require('./stream-ops.node') as typeof import('./stream-ops.node')
} else {
_m = require('./stream-ops.web') as typeof import('./stream-ops.web')
}
export const continueFizzStream = _m.continueFizzStream
export const continueStaticPrerender = _m.continueStaticPrerender
export const continueDynamicPrerender = _m.continueDynamicPrerender
export const continueStaticFallbackPrerender =
_m.continueStaticFallbackPrerender
export const continueDynamicHTMLResume = _m.continueDynamicHTMLResume
export const streamToBuffer = _m.streamToBuffer
export const chainStreams = _m.chainStreams
export const createDocumentClosingStream = _m.createDocumentClosingStream
export const processPrelude = _m.processPrelude
export const createInlinedDataStream = _m.createInlinedDataStream
export const createPendingStream = _m.createPendingStream
export const createOnHeadersCallback = _m.createOnHeadersCallback
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.
* 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'
......@@ -12,11 +15,21 @@ import {
streamToString as webStreamToString,
createRuntimePrefetchTransformStream,
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'
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 = (
......@@ -25,7 +38,7 @@ type FlightRenderToReadableStream = (
options?: any
) => ReadableStream<Uint8Array>
export type AnyStream = ReadableStream<Uint8Array>
export type AnyStream = AnyStreamType
export type ContinueStreamSharedOptions = {
deploymentId: string | undefined
......@@ -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(
renderStream: AnyStream,
opts: ContinueFizzStreamOptions
): Promise<ReadableStream<Uint8Array>> {
return webContinueFizzStream(renderStream as any, opts)
): Promise<AnyStream> {
return webContinueFizzStream(
renderStream as ReadableStream<Uint8Array> as any,
{
...opts,
inlinedDataStream: opts.inlinedDataStream as
| ReadableStream<Uint8Array>
| undefined,
}
)
}
// Not available in web bundles
export const nodeReadableToWeb:
| ((readable: import('node:stream').Readable) => ReadableStream<Uint8Array>)
| undefined = undefined
export async function continueStaticPrerender(
prerenderStream: AnyStream,
opts: ContinueStaticPrerenderOptions
): 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
......@@ -196,3 +274,7 @@ export function pipeRuntimePrefetchTransform(
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'
import type { RenderResumeDataCache } from '../resume-data-cache/resume-data-cache'
import type { ServerCacheStatus } from '../../next-devtools/dev-overlay/cache-indicator'
import type { PrefetchHints } from '../../shared/lib/app-router-types'
import type { AnyStream } from './stream-ops'
const dynamicParamTypesSchema = s.enums([
'c',
......@@ -115,12 +116,12 @@ export interface RenderOptsPartial {
setCacheStatus?: (status: ServerCacheStatus, htmlRequestId: string) => void
setIsrStatus?: (key: string, value: boolean | undefined) => void
setReactDebugChannel?: (
debugChannel: { readable: ReadableStream<Uint8Array> },
debugChannel: { readable: AnyStream },
htmlRequestId: string,
requestId: string
) => void
sendErrorsToBrowser?: (
errorsRscStream: ReadableStream<Uint8Array>,
errorsRscStream: AnyStream,
htmlRequestId: string
) => void
isBuildTimePrerendering?: boolean
......
......@@ -76,9 +76,17 @@ export function getFlightStream<T>(
const { Readable } =
require('node:stream') as typeof import('node:stream')
// The types of flightStream and debugStream should match.
if (debugStream && !(debugStream instanceof Readable)) {
throw new InvariantError('Expected debug stream to be a Readable')
// Convert debug stream to Readable if it's a ReadableStream.
// When __NEXT_USE_NODE_STREAMS is enabled, the debug channel produces
// 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
......@@ -96,7 +104,7 @@ export function getFlightStream<T>(
{
findSourceMapURL,
nonce,
debugChannel: debugStream,
debugChannel: nodeDebugStream,
endTime: debugEndTime,
}
)
......
import type { Readable } from 'node:stream'
import { createBufferedTransformStream } from '../stream-utils/node-web-streams-helper'
import {
HMR_MESSAGE_SENT_TO_BROWSER,
type HmrMessageSentToBrowser,
} 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 {
readonly readable: ReadableStream<Uint8Array>
// Might also get a writable stream as return channel in the future.
readonly readable: AnyStream
}
const reactDebugChannelsByHtmlRequestId = new Map<
......@@ -19,7 +29,7 @@ export function connectReactDebugChannel(
debugChannel: ReactDebugChannelForBrowser,
sendToClient: (message: HmrMessageSentToBrowser) => void
) {
const reader = debugChannel.readable
const reader = toWebReadableStream(debugChannel.readable)
.pipeThrough(
// We're sending the chunks in batches to reduce overhead in the browser.
createBufferedTransformStream({ maxBufferByteLength: 128 * 1024 })
......
......@@ -14,6 +14,7 @@ import type {
} from '../../next-devtools/dev-overlay/cache-indicator'
import type { DevToolsConfig } from '../../next-devtools/dev-overlay/shared'
import type { ReactDebugChannelForBrowser } from './debug-channel'
import type { AnyStream } from '../app-render/stream-ops'
export const enum HMR_MESSAGE_SENT_TO_BROWSER {
// JSON messages:
......@@ -242,10 +243,7 @@ export interface NextJsHotReloaderInterface {
htmlRequestId: string,
requestId: string
): void
sendErrorsToBrowser(
errorsRscStream: ReadableStream<Uint8Array>,
htmlRequestId: string
): void
sendErrorsToBrowser(errorsRscStream: AnyStream, htmlRequestId: string): void
getCompilationErrors(page: string): Promise<any[]>
onHMR(
req: IncomingMessage,
......
......@@ -5,6 +5,7 @@ import type { Telemetry } from '../../telemetry/storage'
import type { IncomingMessage, ServerResponse } from 'http'
import type { UrlObject } from 'url'
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 {
......@@ -1820,7 +1821,7 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
}
public sendErrorsToBrowser(
errorsRscStream: ReadableStream<Uint8Array>,
errorsRscStream: AnyStream,
htmlRequestId: string
): void {
const client = this.webpackHotMiddleware?.getClient(htmlRequestId)
......
import { streamToUint8Array } from '../stream-utils/node-web-streams-helper'
import {
HMR_MESSAGE_SENT_TO_BROWSER,
type HmrMessageSentToBrowser,
} from './hot-reloader-types'
import type { AnyStream } from '../app-render/stream-ops'
import { streamToUint8Array } from '../app-render/stream-ops'
const errorsRscStreamsByHtmlRequestId = new Map<
string,
ReadableStream<Uint8Array>
>()
const errorsRscStreamsByHtmlRequestId = new Map<string, AnyStream>()
export function sendSerializedErrorsToClient(
errorsRscStream: ReadableStream<Uint8Array>,
errorsRscStream: AnyStream,
sendToClient: (message: HmrMessageSentToBrowser) => void
) {
streamToUint8Array(errorsRscStream).then(
......@@ -43,7 +41,7 @@ export function sendSerializedErrorsToClientForHtmlRequest(
export function setErrorsRscStreamForHtmlRequest(
htmlRequestId: string,
errorsRscStream: ReadableStream<Uint8Array>
errorsRscStream: AnyStream
) {
// 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.
......
......@@ -2,6 +2,7 @@ import type { IncomingMessage, ServerResponse } from 'node:http'
import type { NextConfigRuntime } from '../../config-shared'
import type { UrlWithParsedQuery } from 'node:url'
import type { ServerCacheStatus } from '../../../next-devtools/dev-overlay/cache-indicator'
import type { AnyStream } from '../../app-render/stream-ops'
export type RevalidateFn = (config: {
urlPath: string
......@@ -40,13 +41,13 @@ export type RouterServerContext = Record<
// allow setting ISR status in dev
setIsrStatus?: (key: string, value: boolean | undefined) => void
setReactDebugChannel?: (
debugChannel: { readable: ReadableStream<Uint8Array> },
debugChannel: { readable: AnyStream },
htmlRequestId: string,
requestId: string
) => void
setCacheStatus?: (status: ServerCacheStatus, htmlRequestId: string) => void
sendErrorsToBrowser?: (
errorsRscStream: ReadableStream<Uint8Array>,
errorsRscStream: AnyStream,
htmlRequestId: string
) => void
// indicates request handlers are already wrapped by next-server tracing
......
import type { ServerResponse } from 'node:http'
import type { Readable } from 'node:stream'
import {
ResponseAbortedName,
......@@ -144,3 +145,97 @@ export async function pipeToNodeResponse(
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 { Readable } from 'stream'
import type { CacheControl } from './lib/cache-control'
import type { FetchMetrics } from './base-http'
import type { PrefetchHints } from '../shared/lib/app-router-types'
......@@ -9,7 +10,11 @@ import {
streamFromString,
streamToString,
} 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 { InvariantError } from '../shared/lib/invariant-error'
import type {
......@@ -82,6 +87,7 @@ export type RenderResultMetadata = AppPageRenderResultMetadata &
export type RenderResultResponse =
| ReadableStream<Uint8Array>[]
| ReadableStream<Uint8Array>
| Readable
| string
| Buffer
| null
......@@ -94,6 +100,16 @@ export type RenderResultOptions<
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<
Metadata extends RenderResultMetadata = RenderResultMetadata,
> {
......@@ -231,6 +247,19 @@ export default class RenderResult<
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
}
......@@ -253,6 +282,17 @@ export default class RenderResult<
return this.response
} else if (Buffer.isBuffer(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 {
return [this.response]
}
......@@ -349,6 +389,16 @@ export default class RenderResult<
* @param res
*/
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)
}
}
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