rsnext/packages/next/server/node-web-streams-helper.ts

383 lines
10 KiB
TypeScript
Raw Normal View History

import type { FlightRouterState } from './app-render'
import { nonNullable } from '../lib/non-nullable'
const queueTask =
process.env.NEXT_RUNTIME === 'edge' ? globalThis.setTimeout : setImmediate
export type ReactReadableStream = ReadableStream<Uint8Array> & {
allReady?: Promise<void> | undefined
}
export function encodeText(input: string) {
return new TextEncoder().encode(input)
}
export function decodeText(input?: Uint8Array, textDecoder?: TextDecoder) {
return textDecoder
? textDecoder.decode(input, { stream: true })
: new TextDecoder().decode(input)
}
export function readableStreamTee<T = any>(
readable: ReadableStream<T>
): [ReadableStream<T>, ReadableStream<T>] {
const transformStream = new TransformStream()
const transformStream2 = new TransformStream()
const writer = transformStream.writable.getWriter()
const writer2 = transformStream2.writable.getWriter()
const reader = readable.getReader()
function read() {
reader.read().then(({ done, value }) => {
if (done) {
writer.close()
writer2.close()
return
}
writer.write(value)
writer2.write(value)
read()
})
}
read()
return [transformStream.readable, transformStream2.readable]
}
export function chainStreams<T>(
streams: ReadableStream<T>[]
): ReadableStream<T> {
const { readable, writable } = new TransformStream()
let promise = Promise.resolve()
for (let i = 0; i < streams.length; ++i) {
promise = promise.then(() =>
streams[i].pipeTo(writable, { preventClose: i + 1 < streams.length })
)
}
return readable
}
export function streamFromArray(strings: string[]): ReadableStream<Uint8Array> {
// Note: we use a TransformStream here instead of instantiating a ReadableStream
// because the built-in ReadableStream polyfill runs strings through TextEncoder.
const { readable, writable } = new TransformStream()
const writer = writable.getWriter()
strings.forEach((str) => writer.write(encodeText(str)))
writer.close()
return readable
}
export async function streamToString(
stream: ReadableStream<Uint8Array>
): Promise<string> {
const reader = stream.getReader()
const textDecoder = new TextDecoder()
let bufferedString = ''
while (true) {
const { done, value } = await reader.read()
if (done) {
return bufferedString
}
bufferedString += decodeText(value, textDecoder)
}
}
export function createBufferedTransformStream(
transform: (v: string) => string | Promise<string> = (v) => v
): TransformStream<Uint8Array, Uint8Array> {
let bufferedString = ''
let pendingFlush: Promise<void> | null = null
const flushBuffer = (controller: TransformStreamDefaultController) => {
if (!pendingFlush) {
pendingFlush = new Promise((resolve) => {
setTimeout(async () => {
const buffered = await transform(bufferedString)
controller.enqueue(encodeText(buffered))
bufferedString = ''
pendingFlush = null
resolve()
}, 0)
})
}
return pendingFlush
}
const textDecoder = new TextDecoder()
return new TransformStream({
transform(chunk, controller) {
bufferedString += decodeText(chunk, textDecoder)
flushBuffer(controller)
},
flush() {
if (pendingFlush) {
return pendingFlush
}
},
})
}
export function createInsertedHTMLStream(
getServerInsertedHTML: () => Promise<string>
): TransformStream<Uint8Array, Uint8Array> {
return new TransformStream({
Drop legacy React DOM Server in Edge runtime (#40018) When possible (`ReactRoot` enabled), we always use `renderToReadableStream` to render the element to string and drop all `renderToString` and `renderToStaticMarkup` usages. Since this is always true for the Edge Runtime (which requires React 18+), so we can safely eliminate the `./cjs/react-dom-server-legacy.browser.production.min.js` module there ([ref](https://unpkg.com/browse/react-dom@18.2.0/server.browser.js)). This reduces the gzipped bundle by 11kb (~9%). Let me know if there's any concern or it's too hacky. <img width="904" alt="image" src="https://user-images.githubusercontent.com/11064311/192544933-298e3638-13ba-436d-9bcb-42dfb1224025.png"> ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm lint` - [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples) Co-authored-by: Jimmy Lai <laijimmy0@gmail.com>
2022-09-29 10:56:28 +02:00
async transform(chunk, controller) {
const insertedHTMLChunk = encodeText(await getServerInsertedHTML())
controller.enqueue(insertedHTMLChunk)
controller.enqueue(chunk)
},
})
}
export function renderToInitialStream({
ReactDOMServer,
element,
streamOptions,
}: {
ReactDOMServer: any
element: React.ReactElement
streamOptions?: any
}): Promise<ReactReadableStream> {
return ReactDOMServer.renderToReadableStream(element, streamOptions)
}
function createHeadInsertionTransformStream(
insert: () => Promise<string>
): TransformStream<Uint8Array, Uint8Array> {
let inserted = false
let freezing = false
return new TransformStream({
Drop legacy React DOM Server in Edge runtime (#40018) When possible (`ReactRoot` enabled), we always use `renderToReadableStream` to render the element to string and drop all `renderToString` and `renderToStaticMarkup` usages. Since this is always true for the Edge Runtime (which requires React 18+), so we can safely eliminate the `./cjs/react-dom-server-legacy.browser.production.min.js` module there ([ref](https://unpkg.com/browse/react-dom@18.2.0/server.browser.js)). This reduces the gzipped bundle by 11kb (~9%). Let me know if there's any concern or it's too hacky. <img width="904" alt="image" src="https://user-images.githubusercontent.com/11064311/192544933-298e3638-13ba-436d-9bcb-42dfb1224025.png"> ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm lint` - [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples) Co-authored-by: Jimmy Lai <laijimmy0@gmail.com>
2022-09-29 10:56:28 +02:00
async transform(chunk, controller) {
// While react is flushing chunks, we don't apply insertions
if (freezing) {
controller.enqueue(chunk)
return
}
const insertion = await insert()
if (inserted) {
controller.enqueue(encodeText(insertion))
controller.enqueue(chunk)
freezing = true
} else {
const content = decodeText(chunk)
const index = content.indexOf('</head>')
if (index !== -1) {
const insertedHeadContent =
content.slice(0, index) + insertion + content.slice(index)
controller.enqueue(encodeText(insertedHeadContent))
freezing = true
inserted = true
}
}
if (!inserted) {
controller.enqueue(chunk)
} else {
queueTask(() => {
freezing = false
})
}
},
})
}
// 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 suffixFlushed = false
let suffixFlushTask: Promise<void> | null = null
return new TransformStream({
transform(chunk, controller) {
controller.enqueue(chunk)
if (!suffixFlushed && suffix) {
suffixFlushed = true
suffixFlushTask = new Promise((res) => {
// NOTE: streaming flush
// Enqueue suffix part before the major chunks are enqueued so that
// suffix won't be flushed too early to interrupt the data stream
setTimeout(() => {
controller.enqueue(encodeText(suffix))
res()
})
})
}
},
flush(controller) {
if (suffixFlushTask) return suffixFlushTask
if (!suffixFlushed && suffix) {
suffixFlushed = true
controller.enqueue(encodeText(suffix))
}
},
})
}
export function createInlineDataStream(
dataStream: ReadableStream<Uint8Array>
): TransformStream<Uint8Array, Uint8Array> {
let dataStreamFinished: Promise<void> | null = null
return new TransformStream({
transform(chunk, controller) {
controller.enqueue(chunk)
if (!dataStreamFinished) {
const dataStreamReader = dataStream.getReader()
// NOTE: streaming flush
// We are buffering here for the inlined data stream because the
// "shell" stream might be chunkenized again by the underlying stream
// implementation, e.g. with a specific high-water mark. To ensure it's
// the safe timing to pipe the data stream, this extra tick is
// necessary.
dataStreamFinished = new Promise((res) =>
setTimeout(async () => {
try {
while (true) {
const { done, value } = await dataStreamReader.read()
if (done) {
return res()
}
controller.enqueue(value)
}
} catch (err) {
controller.error(err)
}
res()
}, 0)
)
}
},
flush() {
if (dataStreamFinished) {
return dataStreamFinished
}
},
})
}
export function createSuffixStream(
suffix: string
): TransformStream<Uint8Array, Uint8Array> {
return new TransformStream({
flush(controller) {
if (suffix) {
controller.enqueue(encodeText(suffix))
}
},
})
}
export function createRootLayoutValidatorStream(
assetPrefix = '',
getTree: () => FlightRouterState
): TransformStream<Uint8Array, Uint8Array> {
let foundHtml = false
let foundBody = false
return new TransformStream({
async transform(chunk, controller) {
if (!foundHtml || !foundBody) {
const content = decodeText(chunk)
if (!foundHtml && content.includes('<html')) {
foundHtml = true
}
if (!foundBody && content.includes('<body')) {
foundBody = true
}
}
controller.enqueue(chunk)
},
flush(controller) {
const missingTags = [
foundHtml ? null : 'html',
foundBody ? null : 'body',
].filter(nonNullable)
if (missingTags.length > 0) {
controller.enqueue(
encodeText(
`<script>self.__next_root_layout_missing_tags_error=${JSON.stringify(
{ missingTags, assetPrefix: assetPrefix ?? '', tree: getTree() }
)}</script>`
)
)
}
},
})
}
export async function continueFromInitialStream(
renderStream: ReactReadableStream,
{
suffix,
dataStream,
generateStaticHTML,
getServerInsertedHTML,
serverInsertedHTMLToHead,
validateRootLayout,
}: {
suffix?: string
dataStream?: ReadableStream<Uint8Array>
generateStaticHTML: boolean
getServerInsertedHTML?: () => Promise<string>
serverInsertedHTMLToHead: boolean
validateRootLayout?: {
assetPrefix?: string
getTree: () => FlightRouterState
}
}
): Promise<ReadableStream<Uint8Array>> {
const closeTag = '</body></html>'
const suffixUnclosed = suffix ? suffix.split(closeTag)[0] : null
if (generateStaticHTML) {
await renderStream.allReady
}
const transforms: Array<TransformStream<Uint8Array, Uint8Array>> = [
createBufferedTransformStream(),
getServerInsertedHTML && !serverInsertedHTMLToHead
? createInsertedHTMLStream(getServerInsertedHTML)
: null,
suffixUnclosed != null ? createDeferredSuffixStream(suffixUnclosed) : null,
dataStream ? createInlineDataStream(dataStream) : null,
suffixUnclosed != null ? createSuffixStream(closeTag) : null,
createHeadInsertionTransformStream(async () => {
// TODO-APP: Insert server side html to end of head in app layout rendering, to avoid
// hydration errors. Remove this once it's ready to be handled by react itself.
const serverInsertedHTML =
getServerInsertedHTML && serverInsertedHTMLToHead
? await getServerInsertedHTML()
Drop legacy React DOM Server in Edge runtime (#40018) When possible (`ReactRoot` enabled), we always use `renderToReadableStream` to render the element to string and drop all `renderToString` and `renderToStaticMarkup` usages. Since this is always true for the Edge Runtime (which requires React 18+), so we can safely eliminate the `./cjs/react-dom-server-legacy.browser.production.min.js` module there ([ref](https://unpkg.com/browse/react-dom@18.2.0/server.browser.js)). This reduces the gzipped bundle by 11kb (~9%). Let me know if there's any concern or it's too hacky. <img width="904" alt="image" src="https://user-images.githubusercontent.com/11064311/192544933-298e3638-13ba-436d-9bcb-42dfb1224025.png"> ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm lint` - [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples) Co-authored-by: Jimmy Lai <laijimmy0@gmail.com>
2022-09-29 10:56:28 +02:00
: ''
Load `beforeInteractive` scripts properly without blocking hydration (#41164) This PR ensures that for the app directory, `beforeInteractive`, `afterInteractive` and `lazyOnload` scripts via `next/script` are properly supported. For both `beforeInteractive` and `afterInteractive` scripts, a preload link tag needs to be injected by Float. For `beforeInteractive` scripts and Next.js' polyfills, they need to be manually executed in order before starting the Next.js' runtime, without blocking the downloading of HTML and other scripts. This PR doesn't include the `worker` type of scripts yet. Note: in this PR I changed the inlined flight data `__next_s` to `__next_f`, and use `__next_s` for scripts data, because I can't find a better name for `next/script` that is also short at the same time. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2022-10-09 17:08:51 +02:00
return serverInsertedHTML
}),
validateRootLayout
? createRootLayoutValidatorStream(
validateRootLayout.assetPrefix,
validateRootLayout.getTree
)
: null,
].filter(nonNullable)
return transforms.reduce(
(readable, transform) => readable.pipeThrough(transform),
renderStream
)
}