Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
108af215
Commit
108af215
authored
Feb 09, 2026
by
Jimmy Lai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: tighten node stream wrapper typing and header adapters
parent
436e2d45
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
165 additions
and
98 deletions
+165
-98
app-render.tsx
packages/next/src/server/app-render/app-render.tsx
+2
-2
pipeable-stream-wrappers.ts
...es/next/src/server/app-render/pipeable-stream-wrappers.ts
+53
-28
stream-ops.ts
packages/next/src/server/app-render/stream-ops.ts
+102
-62
use-flight-response.tsx
packages/next/src/server/app-render/use-flight-response.tsx
+8
-6
No files found.
packages/next/src/server/app-render/app-render.tsx
View file @
108af215
...
@@ -5760,7 +5760,7 @@ async function prerenderToStream(
...
@@ -5760,7 +5760,7 @@ async function prerenderToStream(
return htmlRendererErrorHandler(err, errorInfo)
return htmlRendererErrorHandler(err, errorInfo)
},
},
onHeaders: finalClientOnHeaders
as any
,
onHeaders: finalClientOnHeaders,
maxHeadersLength: reactMaxHeadersLength,
maxHeadersLength: reactMaxHeadersLength,
bootstrapScripts: [bootstrapScript],
bootstrapScripts: [bootstrapScript],
}
}
...
@@ -6047,7 +6047,7 @@ async function prerenderToStream(
...
@@ -6047,7 +6047,7 @@ async function prerenderToStream(
/>,
/>,
{
{
onError: htmlRendererErrorHandler,
onError: htmlRendererErrorHandler,
onHeaders: pprOnHeaders
as any
,
onHeaders: pprOnHeaders,
maxHeadersLength: reactMaxHeadersLength,
maxHeadersLength: reactMaxHeadersLength,
bootstrapScripts: [bootstrapScript],
bootstrapScripts: [bootstrapScript],
}
}
...
...
packages/next/src/server/app-render/pipeable-stream-wrappers.ts
View file @
108af215
import
type
{
Writable
,
Readable
}
from
'node:stream'
import
type
{
Writable
,
Readable
}
from
'node:stream'
import
type
{
PostponedState
}
from
'react-dom/static'
// Lazy require to avoid webpack trying to resolve node:stream at parse time.
// Lazy require to avoid webpack trying to resolve node:stream at parse time.
// When __NEXT_USE_NODE_STREAMS is false, DCE removes all call sites so this
// When __NEXT_USE_NODE_STREAMS is false, DCE removes all call sites so this
...
@@ -20,6 +21,29 @@ export interface FizzPipeableStreamResult {
...
@@ -20,6 +21,29 @@ export interface FizzPipeableStreamResult {
abort
:
(
reason
?:
unknown
)
=>
void
abort
:
(
reason
?:
unknown
)
=>
void
}
}
type
FlightRenderToPipeableStream
=
typeof
import
(
'react-server-dom-webpack/server.node'
).
renderToPipeableStream
type
FlightRenderToReadableStream
=
typeof
import
(
'react-server-dom-webpack/server.edge'
).
renderToReadableStream
type
FlightModel
=
Parameters
<
FlightRenderToReadableStream
>
[
0
]
type
FlightWebpackMap
=
Parameters
<
FlightRenderToReadableStream
>
[
1
]
type
FlightRenderOptions
=
Parameters
<
FlightRenderToReadableStream
>
[
2
]
type
ResumeToPipeableOptions
=
Parameters
<
typeof
import
(
'react-dom/server'
).
resumeToPipeableStream
>
[
2
]
&
{
onHeaders
?:
NonNullable
<
RenderToPipeableStreamOptions
[
'onHeaders'
]
>
onAllReady
?:
()
=>
void
}
function
wrapOnHeaders
(
onHeaders
:
RenderToPipeableStreamOptions
[
'onHeaders'
]
|
undefined
):
RenderToPipeableStreamOptions
[
'onHeaders'
]
|
undefined
{
if
(
!
onHeaders
)
return
undefined
return
(
headersDescriptor
:
Headers
|
HeadersInit
)
=>
{
onHeaders
(
new
Headers
(
headersDescriptor
))
}
}
/**
/**
* Wraps react-dom/server renderToPipeableStream in an async/await interface
* Wraps react-dom/server renderToPipeableStream in an async/await interface
* that matches the existing renderToReadableStream usage patterns.
* that matches the existing renderToReadableStream usage patterns.
...
@@ -49,16 +73,11 @@ export function renderToFizzPipeableStream(
...
@@ -49,16 +73,11 @@ export function renderToFizzPipeableStream(
// (e.g. { Link: "..." }), while renderToReadableStream wraps it in a
// (e.g. { Link: "..." }), while renderToReadableStream wraps it in a
// Headers instance. We need to do the same wrapping here so that
// Headers instance. We need to do the same wrapping here so that
// callers can use headers.forEach().
// callers can use headers.forEach().
const
originalOnHeaders
=
options
?.
onHeaders
const
wrappedOnHeaders
=
wrapOnHeaders
(
options
?.
onHeaders
)
const
wrappedOnHeaders
=
originalOnHeaders
?
(
headersDescriptor
:
any
)
=>
{
originalOnHeaders
(
new
Headers
(
headersDescriptor
))
}
:
undefined
const
{
pipe
,
abort
}
=
renderToPipeableStream
(
element
,
{
const
{
pipe
,
abort
}
=
renderToPipeableStream
(
element
,
{
...
options
,
...
options
,
onHeaders
:
wrappedOnHeaders
as
any
,
onHeaders
:
wrappedOnHeaders
,
onShellReady
()
{
onShellReady
()
{
pipe
(
passthrough
as
unknown
as
Writable
)
pipe
(
passthrough
as
unknown
as
Writable
)
originalOnShellReady
?.()
originalOnShellReady
?.()
...
@@ -88,12 +107,12 @@ export function renderToFizzPipeableStream(
...
@@ -88,12 +107,12 @@ export function renderToFizzPipeableStream(
export
function
resumeToFizzPipeableStream
(
export
function
resumeToFizzPipeableStream
(
resumeToPipeableStreamFn
:
(
resumeToPipeableStreamFn
:
(
children
:
React
.
ReactElement
,
children
:
React
.
ReactElement
,
postponedState
:
any
,
postponedState
:
PostponedState
,
options
?:
any
options
?:
ResumeToPipeableOptions
)
=>
Promise
<
PipeableStream
>
,
)
=>
Promise
<
PipeableStream
>
,
element
:
React
.
ReactElement
,
element
:
React
.
ReactElement
,
postponedState
:
any
,
postponedState
:
PostponedState
,
options
?:
any
options
?:
ResumeToPipeableOptions
):
Promise
<
FizzPipeableStreamResult
>
{
):
Promise
<
FizzPipeableStreamResult
>
{
return
getTracer
().
trace
(
AppRenderSpan
.
renderToReadableStream
,
async
()
=>
{
return
getTracer
().
trace
(
AppRenderSpan
.
renderToReadableStream
,
async
()
=>
{
const
allReady
=
new
DetachedPromise
<
void
>
()
const
allReady
=
new
DetachedPromise
<
void
>
()
...
@@ -102,12 +121,7 @@ export function resumeToFizzPipeableStream(
...
@@ -102,12 +121,7 @@ export function resumeToFizzPipeableStream(
const
originalOnAllReady
=
options
?.
onAllReady
const
originalOnAllReady
=
options
?.
onAllReady
// Same onHeaders wrapping as renderToFizzPipeableStream
// Same onHeaders wrapping as renderToFizzPipeableStream
const
originalOnHeaders
=
options
?.
onHeaders
const
wrappedOnHeaders
=
wrapOnHeaders
(
options
?.
onHeaders
)
const
wrappedOnHeaders
=
originalOnHeaders
?
(
headersDescriptor
:
any
)
=>
{
originalOnHeaders
(
new
Headers
(
headersDescriptor
))
}
:
undefined
const
{
pipe
,
abort
}
=
await
resumeToPipeableStreamFn
(
const
{
pipe
,
abort
}
=
await
resumeToPipeableStreamFn
(
element
,
element
,
...
@@ -138,14 +152,10 @@ export function resumeToFizzPipeableStream(
...
@@ -138,14 +152,10 @@ export function resumeToFizzPipeableStream(
* (no shell concept), we pipe to a PassThrough right away.
* (no shell concept), we pipe to a PassThrough right away.
*/
*/
export
function
renderToFlightPipeableStream
(
export
function
renderToFlightPipeableStream
(
renderToPipeableStreamFn
:
(
renderToPipeableStreamFn
:
FlightRenderToPipeableStream
,
model
:
any
,
model
:
FlightModel
,
webpackMap
:
any
,
webpackMap
:
FlightWebpackMap
,
options
?:
any
options
?:
FlightRenderOptions
,
)
=>
PipeableStream
,
model
:
any
,
webpackMap
:
any
,
options
?:
any
,
runInContext
?:
<
T
>
(
fn
:
()
=>
T
)
=>
T
runInContext
?:
<
T
>
(
fn
:
()
=>
T
)
=>
T
):
Readable
{
):
Readable
{
const
{
PassThrough
}
=
getNodeStream
()
const
{
PassThrough
}
=
getNodeStream
()
...
@@ -157,15 +167,30 @@ export function renderToFlightPipeableStream(
...
@@ -157,15 +167,30 @@ export function renderToFlightPipeableStream(
if
(
options
?.
debugChannel
)
{
if
(
options
?.
debugChannel
)
{
const
{
toNodeDebugChannel
}
=
const
{
toNodeDebugChannel
}
=
require
(
'./debug-channel-server'
)
as
typeof
import
(
'./debug-channel-server'
)
require
(
'./debug-channel-server'
)
as
typeof
import
(
'./debug-channel-server'
)
const
debugChannel
=
options
.
debugChannel
const
isNodeWritable
=
typeof
debugChannel
===
'object'
&&
debugChannel
!==
null
&&
'write'
in
debugChannel
&&
typeof
debugChannel
.
write
===
'function'
options
=
{
options
=
{
...
options
,
...
options
,
debugChannel
:
toNodeDebugChannel
(
options
.
debugChannel
),
debugChannel
:
isNodeWritable
?
debugChannel
:
toNodeDebugChannel
(
debugChannel
as
import
(
'./debug-channel-server'
).
DebugChannelServer
),
}
}
}
}
const
run
=
runInContext
??
((
fn
:
()
=>
any
)
=>
fn
())
const
run
:
<
T
>
(
fn
:
()
=>
T
)
=>
T
=
runInContext
??
((
fn
)
=>
fn
())
const
{
pipe
}
=
run
(()
=>
const
{
pipe
}
=
run
(()
=>
renderToPipeableStreamFn
(
model
,
webpackMap
,
options
)
renderToPipeableStreamFn
(
model
,
webpackMap
,
options
as
Parameters
<
FlightRenderToPipeableStream
>
[
2
]
)
)
)
run
(()
=>
pipe
(
passthrough
as
unknown
as
Writable
))
run
(()
=>
pipe
(
passthrough
as
unknown
as
Writable
))
return
passthrough
return
passthrough
...
...
packages/next/src/server/app-render/stream-ops.ts
View file @
108af215
This diff is collapsed.
Click to expand it.
packages/next/src/server/app-render/use-flight-response.tsx
View file @
108af215
...
@@ -14,8 +14,8 @@ const INLINE_FLIGHT_PAYLOAD_FORM_STATE = 2
...
@@ -14,8 +14,8 @@ const INLINE_FLIGHT_PAYLOAD_FORM_STATE = 2
const
INLINE_FLIGHT_PAYLOAD_BINARY
=
3
const
INLINE_FLIGHT_PAYLOAD_BINARY
=
3
const
flightResponses
=
new
WeakMap
<
const
flightResponses
=
new
WeakMap
<
Readable
|
BinaryStreamOf
<
any
>
,
Readable
|
BinaryStreamOf
<
unknown
>
,
Promise
<
any
>
Promise
<
unknown
>
>
()
>
()
const
encoder
=
new
TextEncoder
()
const
encoder
=
new
TextEncoder
()
...
@@ -38,7 +38,7 @@ export function getFlightStream<T>(
...
@@ -38,7 +38,7 @@ export function getFlightStream<T>(
const
response
=
flightResponses
.
get
(
flightStream
)
const
response
=
flightResponses
.
get
(
flightStream
)
if
(
response
)
{
if
(
response
)
{
return
response
return
response
as
Promise
<
T
>
}
}
const
{
moduleLoading
,
edgeSSRModuleMapping
,
ssrModuleMapping
}
=
const
{
moduleLoading
,
edgeSSRModuleMapping
,
ssrModuleMapping
}
=
...
@@ -84,9 +84,11 @@ export function getFlightStream<T>(
...
@@ -84,9 +84,11 @@ export function getFlightStream<T>(
if
(
debugStream
instanceof
Readable
)
{
if
(
debugStream
instanceof
Readable
)
{
nodeDebugStream
=
debugStream
nodeDebugStream
=
debugStream
}
else
{
}
else
{
// Cast through any: global ReadableStream and stream/web.ReadableStream
// Cast through unknown: global ReadableStream and stream/web.ReadableStream
// have a minor type mismatch (Promise<void> vs Promise<undefined>).
// differ slightly in type declarations.
nodeDebugStream
=
Readable
.
fromWeb
(
debugStream
as
any
)
nodeDebugStream
=
Readable
.
fromWeb
(
debugStream
as
unknown
as
import
(
'stream/web'
).
ReadableStream
<
Uint8Array
>
)
}
}
// With createFromNodeStream + debugChannel, the response can resolve
// With createFromNodeStream + debugChannel, the response can resolve
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment