Commit 5c60c592 authored by Tim Neutkens's avatar Tim Neutkens

Node.js streams: Apply comments from #92252

parent 8cbab6e6
...@@ -54,5 +54,5 @@ export function createWebDebugChannel(): DebugChannelPair { ...@@ -54,5 +54,5 @@ export function createWebDebugChannel(): DebugChannelPair {
* which expects debugChannel to be a Node.js stream with a .write() method. * which expects debugChannel to be a Node.js stream with a .write() method.
*/ */
export function createNodeDebugChannel(): DebugChannelPair { export function createNodeDebugChannel(): DebugChannelPair {
throw new Error('not implemented') throw new Error('Not implemented')
} }
...@@ -26,7 +26,6 @@ import { ...@@ -26,7 +26,6 @@ import {
streamToString as webStreamToString, streamToString as webStreamToString,
createDocumentClosingStream as webCreateDocumentClosingStream, createDocumentClosingStream as webCreateDocumentClosingStream,
createRuntimePrefetchTransformStream, createRuntimePrefetchTransformStream,
CLOSE_TAG,
} from '../stream-utils/node-web-streams-helper' } from '../stream-utils/node-web-streams-helper'
import { indexOfUint8Array } from '../stream-utils/uint8array-helpers' import { indexOfUint8Array } from '../stream-utils/uint8array-helpers'
import { ENCODED_TAGS } from '../stream-utils/encoded-tags' import { ENCODED_TAGS } from '../stream-utils/encoded-tags'
...@@ -210,9 +209,7 @@ function createFlightDataInjectionTransform( ...@@ -210,9 +209,7 @@ function createFlightDataInjectionTransform(
const nodeTransform = new Transform({ const nodeTransform = new Transform({
transform(chunk, _encoding, callback) { transform(chunk, _encoding, callback) {
this.push(chunk) this.push(chunk)
if (delayDataUntilFirstHtmlChunk) { startOrContinuePulling(this)
startOrContinuePulling(this)
}
callback() callback()
}, },
flush(callback) { flush(callback) {
...@@ -263,7 +260,7 @@ function createHeadInsertionTransform( ...@@ -263,7 +260,7 @@ function createHeadInsertionTransform(
if (index !== -1) { if (index !== -1) {
if (insertion) { if (insertion) {
const encodedInsertion = Buffer.from(insertion) const encodedInsertion = Buffer.from(insertion)
const merged = Buffer.allocUnsafe( const merged = Buffer.alloc(
chunk.length + encodedInsertion.length chunk.length + encodedInsertion.length
) )
merged.set(chunk.slice(0, index)) merged.set(chunk.slice(0, index))
...@@ -336,6 +333,8 @@ function createMetadataTransform( ...@@ -336,6 +333,8 @@ function createMetadataTransform(
} }
iconMarkLength = ENCODED_TAGS.META.ICON_MARK.length iconMarkLength = ENCODED_TAGS.META.ICON_MARK.length
// 47 is `/` – handle self-closing `<nxt-icon/>` (length +2 for `/>`)
// vs non-self-closing `<nxt-icon>` (length +1 for `>`)
if (chunk[iconMarkIndex + iconMarkLength] === 47) { if (chunk[iconMarkIndex + iconMarkLength] === 47) {
iconMarkLength += 2 iconMarkLength += 2
} else { } else {
...@@ -345,7 +344,7 @@ function createMetadataTransform( ...@@ -345,7 +344,7 @@ function createMetadataTransform(
if (chunkIndex === 0) { if (chunkIndex === 0) {
closedHeadIndex = indexOfUint8Array(chunk, ENCODED_TAGS.CLOSED.HEAD) closedHeadIndex = indexOfUint8Array(chunk, ENCODED_TAGS.CLOSED.HEAD)
if (iconMarkIndex < closedHeadIndex) { if (iconMarkIndex < closedHeadIndex) {
const replaced = Buffer.allocUnsafe(chunk.length - iconMarkLength) const replaced = Buffer.alloc(chunk.length - iconMarkLength)
replaced.set(chunk.subarray(0, iconMarkIndex)) replaced.set(chunk.subarray(0, iconMarkIndex))
replaced.set( replaced.set(
chunk.subarray(iconMarkIndex + iconMarkLength), chunk.subarray(iconMarkIndex + iconMarkLength),
...@@ -356,7 +355,7 @@ function createMetadataTransform( ...@@ -356,7 +355,7 @@ function createMetadataTransform(
const insertion = await insert() const insertion = await insert()
const encodedInsertion = Buffer.from(insertion) const encodedInsertion = Buffer.from(insertion)
const insertionLength = encodedInsertion.length const insertionLength = encodedInsertion.length
const replaced = Buffer.allocUnsafe( const replaced = Buffer.alloc(
chunk.length - iconMarkLength + insertionLength chunk.length - iconMarkLength + insertionLength
) )
replaced.set(chunk.subarray(0, iconMarkIndex)) replaced.set(chunk.subarray(0, iconMarkIndex))
...@@ -372,7 +371,7 @@ function createMetadataTransform( ...@@ -372,7 +371,7 @@ function createMetadataTransform(
const insertion = await insert() const insertion = await insert()
const encodedInsertion = Buffer.from(insertion) const encodedInsertion = Buffer.from(insertion)
const insertionLength = encodedInsertion.length const insertionLength = encodedInsertion.length
const replaced = Buffer.allocUnsafe( const replaced = Buffer.alloc(
chunk.length - iconMarkLength + insertionLength chunk.length - iconMarkLength + insertionLength
) )
replaced.set(chunk.subarray(0, iconMarkIndex)) replaced.set(chunk.subarray(0, iconMarkIndex))
...@@ -393,36 +392,6 @@ function createMetadataTransform( ...@@ -393,36 +392,6 @@ function createMetadataTransform(
}) })
} }
// ---------------------------------------------------------------------------
// Deferred suffix – Node.js Transform that appends a suffix string after the
// first HTML chunk, deferring via queueMicrotask so the chunk flushes first.
// ---------------------------------------------------------------------------
function createDeferredSuffixTransform(suffix: string): Transform {
let flushed = false
const encodedSuffix = Buffer.from(suffix)
return new Transform({
transform(chunk, _encoding, callback) {
this.push(chunk)
if (!flushed) {
flushed = true
queueMicrotask(() => {
this.push(encodedSuffix)
})
}
callback()
},
flush(callback) {
if (!flushed) {
this.push(encodedSuffix)
}
callback()
},
})
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Move suffix – Node.js Transform that strips </body></html> from its // Move suffix – Node.js Transform that strips </body></html> from its
// original position and re-appends it at the very end of the stream, so any // original position and re-appends it at the very end of the stream, so any
...@@ -495,9 +464,7 @@ function createHtmlDataDplIdTransform(dplId: string): Transform { ...@@ -495,9 +464,7 @@ function createHtmlDataDplIdTransform(dplId: string): Transform {
const insertionPoint = htmlTagIndex + ENCODED_TAGS.OPENING.HTML.length const insertionPoint = htmlTagIndex + ENCODED_TAGS.OPENING.HTML.length
const encodedAttribute = Buffer.from(` data-dpl-id="${dplId}"`) const encodedAttribute = Buffer.from(` data-dpl-id="${dplId}"`)
const modified = Buffer.allocUnsafe( const modified = Buffer.alloc(chunk.length + encodedAttribute.length)
chunk.length + encodedAttribute.length
)
modified.set(chunk.subarray(0, insertionPoint)) modified.set(chunk.subarray(0, insertionPoint))
modified.set(encodedAttribute, insertionPoint) modified.set(encodedAttribute, insertionPoint)
...@@ -693,7 +660,6 @@ export async function resumeAndAbort( ...@@ -693,7 +660,6 @@ export async function resumeAndAbort(
export async function continueFizzStream( export async function continueFizzStream(
renderStream: AnyStream, renderStream: AnyStream,
{ {
suffix,
inlinedDataStream, inlinedDataStream,
isStaticGeneration, isStaticGeneration,
allReady, allReady,
...@@ -703,9 +669,6 @@ export async function continueFizzStream( ...@@ -703,9 +669,6 @@ export async function continueFizzStream(
validateRootLayout, validateRootLayout,
}: import('./stream-ops.web').ContinueFizzStreamOptions }: import('./stream-ops.web').ContinueFizzStreamOptions
): Promise<Readable> { ): Promise<Readable> {
// Suffix itself might contain close tags at the end, so we need to split it.
const suffixUnclosed = suffix ? suffix.split(CLOSE_TAG, 1)[0] : null
if (isStaticGeneration) { if (isStaticGeneration) {
if (allReady) { if (allReady) {
await allReady await allReady
...@@ -736,13 +699,6 @@ export async function continueFizzStream( ...@@ -736,13 +699,6 @@ export async function continueFizzStream(
source.pipe(metadata) source.pipe(metadata)
source = metadata source = metadata
// Insert suffix content
if (suffixUnclosed != null && suffixUnclosed.length > 0) {
const deferredSuffix = createDeferredSuffixTransform(suffixUnclosed)
source.pipe(deferredSuffix)
source = deferredSuffix
}
// Flight data injection – interleaves RSC data chunks with the HTML stream // Flight data injection – interleaves RSC data chunks with the HTML stream
if (inlinedDataStream) { if (inlinedDataStream) {
const flightInjection = createFlightDataInjectionTransform( const flightInjection = createFlightDataInjectionTransform(
......
...@@ -50,7 +50,6 @@ export type ContinueFizzStreamOptions = ContinueStreamSharedOptions & { ...@@ -50,7 +50,6 @@ export type ContinueFizzStreamOptions = ContinueStreamSharedOptions & {
isStaticGeneration: boolean isStaticGeneration: boolean
allReady?: Promise<void> allReady?: Promise<void>
validateRootLayout?: boolean validateRootLayout?: boolean
suffix?: string
} }
export type ContinueStaticPrerenderOptions = ContinueStreamSharedOptions & { export type ContinueStaticPrerenderOptions = ContinueStreamSharedOptions & {
...@@ -158,7 +157,7 @@ export function continueDynamicHTMLResumeNode( ...@@ -158,7 +157,7 @@ export function continueDynamicHTMLResumeNode(
_renderStream: AnyStream, _renderStream: AnyStream,
_opts: ContinueDynamicHTMLResumeOptions _opts: ContinueDynamicHTMLResumeOptions
): Promise<AnyStream> { ): Promise<AnyStream> {
throw new Error('not implemented') throw new Error('Not implemented')
} }
export async function streamToBuffer(stream: AnyStream): Promise<Buffer> { export async function streamToBuffer(stream: AnyStream): Promise<Buffer> {
...@@ -200,7 +199,7 @@ export function createNodeInlinedDataStream( ...@@ -200,7 +199,7 @@ export function createNodeInlinedDataStream(
_nonce: string | undefined, _nonce: string | undefined,
_formState: unknown | null _formState: unknown | null
): AnyStream { ): AnyStream {
throw new Error('not implemented') throw new Error('Not implemented')
} }
export function createPendingStream(): AnyStream { export function createPendingStream(): AnyStream {
...@@ -235,7 +234,7 @@ export function renderToNodeFlightStream( ...@@ -235,7 +234,7 @@ export function renderToNodeFlightStream(
_clientModules: FlightClientModules, _clientModules: FlightClientModules,
_opts: FlightRenderOptions _opts: FlightRenderOptions
): AnyStream { ): AnyStream {
throw new Error('not implemented') throw new Error('Not implemented')
} }
export function renderToWebFlightStream( export function renderToWebFlightStream(
......
...@@ -666,53 +666,6 @@ export function createInstantTestScriptInsertionTransformStream( ...@@ -666,53 +666,6 @@ export function createInstantTestScriptInsertionTransformStream(
}) })
} }
// Suffix after main body content - scripts before </body>,
// but wait for the major chunks to be enqueued.
export function createDeferredSuffixStream(
suffix: string
): TransformStream<Uint8Array, Uint8Array> {
let flushed = false
let pending: DetachedPromise<void> | undefined
const flush = (controller: TransformStreamDefaultController) => {
const detached = new DetachedPromise<void>()
pending = detached
scheduleImmediate(() => {
try {
controller.enqueue(encoder.encode(suffix))
} catch {
// If an error occurs while enqueuing it can't be due to this
// transformers fault. It's likely due to the controller being
// errored due to the stream being cancelled.
} finally {
pending = undefined
detached.resolve()
}
})
}
return new TransformStream({
transform(chunk, controller) {
controller.enqueue(chunk)
// If we've already flushed, we're done.
if (flushed) return
// Schedule the flush to happen.
flushed = true
flush(controller)
},
flush(controller) {
if (pending) return pending.promise
if (flushed) return
// Flush now.
controller.enqueue(encoder.encode(suffix))
},
})
}
export function createFlightDataInjectionTransformStream( export function createFlightDataInjectionTransformStream(
stream: ReadableStream<Uint8Array>, stream: ReadableStream<Uint8Array>,
delayDataUntilFirstHtmlChunk: boolean delayDataUntilFirstHtmlChunk: boolean
...@@ -996,16 +949,11 @@ export type ContinueStreamOptions = { ...@@ -996,16 +949,11 @@ export type ContinueStreamOptions = {
getServerInsertedHTML: () => Promise<string> getServerInsertedHTML: () => Promise<string>
getServerInsertedMetadata: () => Promise<string> getServerInsertedMetadata: () => Promise<string>
validateRootLayout?: boolean validateRootLayout?: boolean
/**
* Suffix to inject after the buffered data, but before the close tags.
*/
suffix?: string | undefined
} }
export async function continueFizzStream( export async function continueFizzStream(
renderStream: ReactDOMServerReadableStream, renderStream: ReactDOMServerReadableStream,
{ {
suffix,
inlinedDataStream, inlinedDataStream,
isStaticGeneration, isStaticGeneration,
deploymentId, deploymentId,
...@@ -1014,9 +962,6 @@ export async function continueFizzStream( ...@@ -1014,9 +962,6 @@ export async function continueFizzStream(
validateRootLayout, validateRootLayout,
}: ContinueStreamOptions }: ContinueStreamOptions
): Promise<ReadableStream<Uint8Array>> { ): Promise<ReadableStream<Uint8Array>> {
// Suffix itself might contain close tags at the end, so we need to split it.
const suffixUnclosed = suffix ? suffix.split(CLOSE_TAG, 1)[0] : null
if (isStaticGeneration) { if (isStaticGeneration) {
// If we're generating static HTML we need to wait for it to resolve before continuing. // If we're generating static HTML we need to wait for it to resolve before continuing.
await renderStream.allReady await renderStream.allReady
...@@ -1036,11 +981,6 @@ export async function continueFizzStream( ...@@ -1036,11 +981,6 @@ export async function continueFizzStream(
// Transform metadata // Transform metadata
createMetadataTransformStream(getServerInsertedMetadata), createMetadataTransformStream(getServerInsertedMetadata),
// Insert suffix content
suffixUnclosed != null && suffixUnclosed.length > 0
? createDeferredSuffixStream(suffixUnclosed)
: null,
// Insert the inlined data (Flight data, form state, etc.) stream into the HTML // Insert the inlined data (Flight data, form state, etc.) stream into the HTML
inlinedDataStream inlinedDataStream
? createFlightDataInjectionTransformStream(inlinedDataStream, true) ? createFlightDataInjectionTransformStream(inlinedDataStream, true)
......
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