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 type { FallbackRouteParams } from '../../server/request/fallback-params'
import { InvariantError } from '../../shared/lib/invariant-error'
import type { Params } from '../request/params' import type { Params } from '../request/params'
import { import {
createPrerenderResumeDataCache, createPrerenderResumeDataCache,
...@@ -7,6 +8,7 @@ import { ...@@ -7,6 +8,7 @@ import {
type RenderResumeDataCache, type RenderResumeDataCache,
} from '../resume-data-cache/resume-data-cache' } from '../resume-data-cache/resume-data-cache'
import { stringifyResumeDataCache } 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 { export enum DynamicState {
/** /**
...@@ -71,6 +73,19 @@ export type PostponedState = ...@@ -71,6 +73,19 @@ export type PostponedState =
| DynamicDataPostponedState | DynamicDataPostponedState
| DynamicHTMLPostponedState | 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( export async function getDynamicHTMLPostponedState(
postponed: ReactPostponed, postponed: ReactPostponed,
preludeState: DynamicHTMLPreludeState, preludeState: DynamicHTMLPreludeState,
...@@ -81,26 +96,32 @@ export async function getDynamicHTMLPostponedState( ...@@ -81,26 +96,32 @@ export async function getDynamicHTMLPostponedState(
const dataString = JSON.stringify(data) const dataString = JSON.stringify(data)
if (!fallbackRouteParams || fallbackRouteParams.size === 0) { if (!fallbackRouteParams || fallbackRouteParams.size === 0) {
// Serialized as `<postponedString.length>:<postponedString><renderResumeDataCache>` return lengthEncodeTuple([
return `${dataString.length}:${dataString}${await stringifyResumeDataCache( dataString,
createRenderResumeDataCache(resumeDataCache) await stringifyResumeDataCache(
)}` createRenderResumeDataCache(resumeDataCache)
),
] satisfies SerializedDynamicHTML)
} }
const replacements: Array<[string, string]> = Array.from(fallbackRouteParams) const replacements: Array<[string, string]> = Array.from(fallbackRouteParams)
const replacementsString = JSON.stringify(replacements) const replacementsString = JSON.stringify(replacements)
// Serialized as `<replacements.length><replacements><data>` return lengthEncodeTuple([
const postponedString = `${replacementsString.length}${replacementsString}${dataString}` replacementsString,
dataString,
// Serialized as `<postponedString.length>:<postponedString><renderResumeDataCache>` await stringifyResumeDataCache(resumeDataCache),
return `${postponedString.length}:${postponedString}${await stringifyResumeDataCache(resumeDataCache)}` ] satisfies SerializedDynamicHTMLWithReplacements)
} }
export async function getDynamicDataPostponedState( export async function getDynamicDataPostponedState(
resumeDataCache: PrerenderResumeDataCache | RenderResumeDataCache resumeDataCache: PrerenderResumeDataCache | RenderResumeDataCache
): Promise<string> { ): Promise<string> {
return `4:null${await stringifyResumeDataCache(createRenderResumeDataCache(resumeDataCache))}` return lengthEncodeTuple([
await stringifyResumeDataCache(
createRenderResumeDataCache(resumeDataCache)
),
] satisfies SerializedDynamicData)
} }
export function parsePostponedState( export function parsePostponedState(
...@@ -108,69 +129,61 @@ export function parsePostponedState( ...@@ -108,69 +129,61 @@ export function parsePostponedState(
params: Params | undefined params: Params | undefined
): PostponedState { ): PostponedState {
try { try {
const postponedStringLengthMatch = state.match(/^([0-9]*):/)?.[1] const parts = lengthDecodeTuple(state) as SerializedStateParts
if (!postponedStringLengthMatch) { if (parts.length === 1) {
throw new Error(`Invariant: invalid postponed state ${state}`) parts satisfies SerializedDynamicData
} const [resumeDataCacheString] = parts
const renderResumeDataCache = createRenderResumeDataCache(
const postponedStringLength = parseInt(postponedStringLengthMatch) resumeDataCacheString
)
// We add a `:` to the end of the length as the first character of the return {
// postponed string is the length of the replacement entries. type: DynamicState.DATA,
const postponedString = state.slice( renderResumeDataCache,
postponedStringLengthMatch.length + 1, }
postponedStringLengthMatch.length + postponedStringLength + 1 } else if (parts.length === 2 || parts.length === 3) {
) let replacementsString: string | null = null,
postponedString: string,
const renderResumeDataCache = createRenderResumeDataCache( resumeDataCacheString: string
state.slice(postponedStringLengthMatch.length + postponedStringLength + 1)
) if (parts.length === 2) {
parts satisfies SerializedDynamicHTML
try { ;[postponedString, resumeDataCacheString] = parts
if (postponedString === 'null') { } else {
return { type: DynamicState.DATA, renderResumeDataCache } parts satisfies SerializedDynamicHTMLWithReplacements
;[replacementsString, postponedString, resumeDataCacheString] = parts
} }
if (/^[0-9]/.test(postponedString)) { const renderResumeDataCache = createRenderResumeDataCache(
const match = postponedString.match(/^([0-9]*)/)?.[1] resumeDataCacheString
if (!match) { )
throw new Error(
`Invariant: invalid postponed state ${JSON.stringify(postponedString)}` try {
) if (replacementsString !== null) {
} const replacements = JSON.parse(replacementsString) as ReadonlyArray<
[string, string]
// This is the length of the replacements entries. >
const length = parseInt(match) for (const [key, searchValue] of replacements) {
const replacements = JSON.parse( const value = params?.[key] ?? ''
postponedString.slice( const replaceValue = Array.isArray(value) ? value.join('/') : value
match.length, postponedString = postponedString.replaceAll(
// We then go to the end of the string. searchValue,
match.length + length replaceValue
) )
) 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)
} }
return { return {
type: DynamicState.HTML, type: DynamicState.HTML,
data: JSON.parse(postponed), data: JSON.parse(postponedString),
renderResumeDataCache, renderResumeDataCache,
} }
} catch (err) {
console.error('Failed to parse postponed state', err)
return { type: DynamicState.DATA, renderResumeDataCache }
} }
} else {
return { parts satisfies never
type: DynamicState.HTML, throw new InvariantError('Postponed state tuple has invalid length')
data: JSON.parse(postponedString),
renderResumeDataCache,
}
} catch (err) {
console.error('Failed to parse postponed state', err)
return { type: DynamicState.DATA, renderResumeDataCache }
} }
} catch (err) { } catch (err) {
console.error('Failed to parse postponed state', 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 { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils' import { retry } from 'next-test-utils'
import stripAnsi from 'strip-ansi' import stripAnsi from 'strip-ansi'
import { format } from 'util' import { format } from 'util'
import { Playwright } from 'next-webdriver' import { Playwright } from 'next-webdriver'
import { import type { RenderResumeDataCache } from 'next/dist/server/resume-data-cache/resume-data-cache'
createRenderResumeDataCache,
RenderResumeDataCache,
} from 'next/dist/server/resume-data-cache/resume-data-cache'
import { PrerenderManifest } from 'next/dist/build' import { PrerenderManifest } from 'next/dist/build'
import { parsePostponedState } from 'next/dist/server/app-render/postponed-state'
const GENERIC_RSC_ERROR = 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.' '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[]> { ...@@ -1042,12 +1044,8 @@ async function getSanitizedLogs(browser: Playwright): Promise<string[]> {
} }
function extractResumeDataCacheFromPostponedState( function extractResumeDataCacheFromPostponedState(
state: string serializedState: string
): RenderResumeDataCache { ): RenderResumeDataCache {
const postponedStringLengthMatch = state.match(/^([0-9]*):/)![1] const state = parsePostponedState(serializedState, {})
const postponedStringLength = parseInt(postponedStringLengthMatch) return state.renderResumeDataCache
return createRenderResumeDataCache(
state.slice(postponedStringLengthMatch.length + postponedStringLength + 1)
)
} }
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