Commit 5a224361 authored by Janka Uryga's avatar Janka Uryga

use length-encoding util for postponed state

parent ff399348
import { lengthEncodeTuple, lengthDecodeTuple } from './length-encoding'
const cases: { description: string; input: string[] }[] = [
[],
// empty strings
[''],
['', ''],
['foo', ''],
// parts that look like last-element delimiters
[':'],
['', ':'],
[':', ':'],
['foo', ':'],
// mostly benign inputs
['foo'],
['foo', 'bar'],
['foo', 'baar', 'zaaaap'],
['a', 'bb', 'ccc', 'dddd', 'eeeee', 'ffffff'],
// parts that look length-prefixed
['0:foo', '1:baar', '3:zaaaap'],
[lengthEncodeTuple(['a', 'bb', 'ccc', 'dddd', 'eeeee', 'ffffff'])],
['0:', '1:', '22:', '333:', '4444:', '55555:'],
['0:', '1:', ':22', ':333', '4444:', ':55555:'],
['0', '1', '22', '333', '4444', ':55555'],
].map((input) => ({ description: JSON.stringify(input), input }))
it.each(cases)(
'can encode and decode tuples without changes - $description',
({ input }) => {
const encoded: string = lengthEncodeTuple(input)
const decoded: string[] = lengthDecodeTuple(encoded)
expect(decoded).toEqual(input)
}
)
import { InvariantError } from '../../shared/lib/invariant-error'
/**
* Encodes a tuple of strings into a single string,
* where each part except the last is prefixed with its length.
* */
export function lengthEncodeTuple(parts: string[]) {
if (parts.length === 0) {
return ''
}
// Fast path for single-element tuples
if (parts.length === 1) {
return ':' + parts[0]
}
let encoded = ''
const lastIx = parts.length - 1
for (let i = 0; i < parts.length; i++) {
const part = parts[i]
if (i < lastIx) {
// All parts (other than the last one) are prefixed with their length --
// we have to know where one part ends and the next part begins.
encoded += `${part.length}:${part}`
} else {
// The last part does not need a length, because there are no more parts after it,
// and we can just take the rest of the string.
// But it needs a delimiter so that we don't accidentally interpret contents like "3:"
// as another length-prefixed part.
//
// This could be optimized to only add an escape character if
// - it's empty (would be skipped when deserializing)
// - it starts with a ':' (would be mistaken for an escape)
// - it starts with a number (could be mistaken for a length prefix)
// but skipping one `.slice()` call when decoding doesn't feel
// like it's worth the added complexity.
encoded += `:${part}`
}
}
return encoded
}
export function lengthDecodeTuple(encoded: string): string[] {
if (!encoded) {
return []
}
let remaining = encoded
const parts: string[] = []
while (true) {
// Check for a length header. If we don't find one, we'll check for a last-part marker.
// If we don't find that either, the input is malformed.
const lengthHeaderMatch = remaining.match(/^([0-9]+):/)
if (lengthHeaderMatch) {
// This is a length-prefixed part. It can't be the last part.
// '<length>:<part><moreParts>'
// Parse the length header
const partLength = Number.parseInt(lengthHeaderMatch[1])
if (Number.isNaN(partLength)) {
throw new InvariantError(
`Part length cannot be parsed as a number: '${remaining}'`
)
}
// Extract the part, i.e. the next `partLength` characters following the header
const partContentsStartIx = lengthHeaderMatch[0].length
const nextPartStartIx = partContentsStartIx + partLength
const partContents = remaining.slice(partContentsStartIx, nextPartStartIx)
if (partContents.length !== partLength) {
throw new InvariantError(
`Actual part length ${partContents.length} does not match length ${partLength} from header: '${remaining}'`
)
}
parts.push(partContents)
// Advance to the next part
remaining = remaining.slice(nextPartStartIx)
continue
} else if (remaining && remaining[0] === ':') {
// The final part is prefixed with a ':', without a length
// ':<lastPart>'
parts.push(remaining.slice(1))
// This was the final part, so we're done parsing
break
} else {
throw new InvariantError(
`Unexpected part without length header or last-part indicator: '${remaining}'`
)
}
}
return parts
}
import type { FallbackRouteParams } from '../../server/request/fallback-params'
import { InvariantError } from '../../shared/lib/invariant-error'
import type { Params } from '../request/params'
import {
createPrerenderResumeDataCache,
......@@ -7,6 +8,7 @@ import {
type RenderResumeDataCache,
} from '../resume-data-cache/resume-data-cache'
import { stringifyResumeDataCache } from '../resume-data-cache/resume-data-cache'
import { lengthDecodeTuple, lengthEncodeTuple } from './length-encoding'
export enum DynamicState {
/**
......@@ -71,6 +73,19 @@ export type PostponedState =
| DynamicDataPostponedState
| DynamicHTMLPostponedState
type SerializedStateParts =
| SerializedDynamicData
| SerializedDynamicHTML
| SerializedDynamicHTMLWithReplacements
type SerializedDynamicData = [resumeDataCache: string]
type SerializedDynamicHTML = [postponed: string, resumeDataCache: string]
type SerializedDynamicHTMLWithReplacements = [
replacements: string,
postponed: string,
resumeDataCache: string,
]
export async function getDynamicHTMLPostponedState(
postponed: ReactPostponed,
preludeState: DynamicHTMLPreludeState,
......@@ -81,26 +96,32 @@ export async function getDynamicHTMLPostponedState(
const dataString = JSON.stringify(data)
if (!fallbackRouteParams || fallbackRouteParams.size === 0) {
// Serialized as `<postponedString.length>:<postponedString><renderResumeDataCache>`
return `${dataString.length}:${dataString}${await stringifyResumeDataCache(
createRenderResumeDataCache(resumeDataCache)
)}`
return lengthEncodeTuple([
dataString,
await stringifyResumeDataCache(
createRenderResumeDataCache(resumeDataCache)
),
] satisfies SerializedDynamicHTML)
}
const replacements: Array<[string, string]> = Array.from(fallbackRouteParams)
const replacementsString = JSON.stringify(replacements)
// Serialized as `<replacements.length><replacements><data>`
const postponedString = `${replacementsString.length}${replacementsString}${dataString}`
// Serialized as `<postponedString.length>:<postponedString><renderResumeDataCache>`
return `${postponedString.length}:${postponedString}${await stringifyResumeDataCache(resumeDataCache)}`
return lengthEncodeTuple([
replacementsString,
dataString,
await stringifyResumeDataCache(resumeDataCache),
] satisfies SerializedDynamicHTMLWithReplacements)
}
export async function getDynamicDataPostponedState(
resumeDataCache: PrerenderResumeDataCache | RenderResumeDataCache
): Promise<string> {
return `4:null${await stringifyResumeDataCache(createRenderResumeDataCache(resumeDataCache))}`
return lengthEncodeTuple([
await stringifyResumeDataCache(
createRenderResumeDataCache(resumeDataCache)
),
] satisfies SerializedDynamicData)
}
export function parsePostponedState(
......@@ -108,69 +129,61 @@ export function parsePostponedState(
params: Params | undefined
): PostponedState {
try {
const postponedStringLengthMatch = state.match(/^([0-9]*):/)?.[1]
if (!postponedStringLengthMatch) {
throw new Error(`Invariant: invalid postponed state ${state}`)
}
const postponedStringLength = parseInt(postponedStringLengthMatch)
// We add a `:` to the end of the length as the first character of the
// postponed string is the length of the replacement entries.
const postponedString = state.slice(
postponedStringLengthMatch.length + 1,
postponedStringLengthMatch.length + postponedStringLength + 1
)
const renderResumeDataCache = createRenderResumeDataCache(
state.slice(postponedStringLengthMatch.length + postponedStringLength + 1)
)
try {
if (postponedString === 'null') {
return { type: DynamicState.DATA, renderResumeDataCache }
const parts = lengthDecodeTuple(state) as SerializedStateParts
if (parts.length === 1) {
parts satisfies SerializedDynamicData
const [resumeDataCacheString] = parts
const renderResumeDataCache = createRenderResumeDataCache(
resumeDataCacheString
)
return {
type: DynamicState.DATA,
renderResumeDataCache,
}
} else if (parts.length === 2 || parts.length === 3) {
let replacementsString: string | null = null,
postponedString: string,
resumeDataCacheString: string
if (parts.length === 2) {
parts satisfies SerializedDynamicHTML
;[postponedString, resumeDataCacheString] = parts
} else {
parts satisfies SerializedDynamicHTMLWithReplacements
;[replacementsString, postponedString, resumeDataCacheString] = parts
}
if (/^[0-9]/.test(postponedString)) {
const match = postponedString.match(/^([0-9]*)/)?.[1]
if (!match) {
throw new Error(
`Invariant: invalid postponed state ${JSON.stringify(postponedString)}`
)
}
// This is the length of the replacements entries.
const length = parseInt(match)
const replacements = JSON.parse(
postponedString.slice(
match.length,
// We then go to the end of the string.
match.length + length
)
) as ReadonlyArray<[string, string]>
let postponed = postponedString.slice(match.length + length)
for (const [key, searchValue] of replacements) {
const value = params?.[key] ?? ''
const replaceValue = Array.isArray(value) ? value.join('/') : value
postponed = postponed.replaceAll(searchValue, replaceValue)
const renderResumeDataCache = createRenderResumeDataCache(
resumeDataCacheString
)
try {
if (replacementsString !== null) {
const replacements = JSON.parse(replacementsString) as ReadonlyArray<
[string, string]
>
for (const [key, searchValue] of replacements) {
const value = params?.[key] ?? ''
const replaceValue = Array.isArray(value) ? value.join('/') : value
postponedString = postponedString.replaceAll(
searchValue,
replaceValue
)
}
}
return {
type: DynamicState.HTML,
data: JSON.parse(postponed),
data: JSON.parse(postponedString),
renderResumeDataCache,
}
} catch (err) {
console.error('Failed to parse postponed state', err)
return { type: DynamicState.DATA, renderResumeDataCache }
}
return {
type: DynamicState.HTML,
data: JSON.parse(postponedString),
renderResumeDataCache,
}
} catch (err) {
console.error('Failed to parse postponed state', err)
return { type: DynamicState.DATA, renderResumeDataCache }
} else {
parts satisfies never
throw new InvariantError('Postponed state tuple has invalid length')
}
} catch (err) {
console.error('Failed to parse postponed state', err)
......
/// <reference path="../../../../packages/next/types/react-dom.d.ts" />
// in order to use `parsePostponedState` here, we need to bring the extended type definition
// for `import('react-dom/static').PrerenderResult` into the compilation, because that module relies on it
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
import stripAnsi from 'strip-ansi'
import { format } from 'util'
import { Playwright } from 'next-webdriver'
import {
createRenderResumeDataCache,
RenderResumeDataCache,
} from 'next/dist/server/resume-data-cache/resume-data-cache'
import type { RenderResumeDataCache } from 'next/dist/server/resume-data-cache/resume-data-cache'
import { PrerenderManifest } from 'next/dist/build'
import { parsePostponedState } from 'next/dist/server/app-render/postponed-state'
const GENERIC_RSC_ERROR =
'An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.'
......@@ -1042,12 +1044,8 @@ async function getSanitizedLogs(browser: Playwright): Promise<string[]> {
}
function extractResumeDataCacheFromPostponedState(
state: string
serializedState: string
): RenderResumeDataCache {
const postponedStringLengthMatch = state.match(/^([0-9]*):/)![1]
const postponedStringLength = parseInt(postponedStringLengthMatch)
return createRenderResumeDataCache(
state.slice(postponedStringLengthMatch.length + postponedStringLength + 1)
)
const state = parsePostponedState(serializedState, {})
return state.renderResumeDataCache
}
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