Unverified Commit 47ed9ed2 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Load client reference manifests without a new VM context

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 7657545d
...@@ -529,6 +529,11 @@ async fn build_manifest( ...@@ -529,6 +529,11 @@ async fn build_manifest(
// note this only applies to the manifests, assets are placed to the original // note this only applies to the manifests, assets are placed to the original
// path still (same as webpack does) // path still (same as webpack does)
let normalized_manifest_entry = entry_name.replace("%5F", "_"); let normalized_manifest_entry = entry_name.replace("%5F", "_");
// The shape of this file (including the deployment ID suffix) is
// recognized by `classifyClientReferenceManifestSource` in
// `packages/next/src/server/load-manifest.external.ts` to load it faster.
// Keep both in sync; unrecognized shapes still work but fall back to a
// slower evaluation.
Ok(ClientReferenceManifestResult { Ok(ClientReferenceManifestResult {
content: AssetContent::file( content: AssetContent::file(
FileContent::Content(File::from(formatdoc! { FileContent::Content(File::from(formatdoc! {
......
import path from 'path' import path from 'path'
import { promises as fs, statSync } from 'fs' import { promises as fs, readFileSync, statSync } from 'fs'
import { import {
APP_PATHS_MANIFEST, APP_PATHS_MANIFEST,
CLIENT_REFERENCE_MANIFEST, CLIENT_REFERENCE_MANIFEST,
...@@ -7,6 +7,7 @@ import { ...@@ -7,6 +7,7 @@ import {
} from '../shared/lib/constants' } from '../shared/lib/constants'
import type { BuildManifest } from '../server/get-page-files' import type { BuildManifest } from '../server/get-page-files'
import { filterAndSortList } from './utils' import { filterAndSortList } from './utils'
import { evalClientReferenceManifestSource } from '../server/load-manifest.external'
const ROUTE_BUNDLE_STATS_FILE = 'route-bundle-stats.json' const ROUTE_BUNDLE_STATS_FILE = 'route-bundle-stats.json'
...@@ -67,9 +68,10 @@ function buildRouteToAppPathsMap( ...@@ -67,9 +68,10 @@ function buildRouteToAppPathsMap(
return routeToAppPaths return routeToAppPaths
} }
// Reads the manfiest file and gets the entry JS files. The manifest file is // Reads the manifest file and gets the entry JS files. The manifest file is
// a JavaScript file that sets a global variable (__RSC_MANIFEST). We require() // a JavaScript file that sets a global variable (__RSC_MANIFEST). It is
// it with a save/restore of the global // loaded with `evalClientReferenceManifestSource`, which parses the generated
// shapes as JSON where possible instead of require()-ing the file.
function readEntryJSFiles( function readEntryJSFiles(
distDir: string, distDir: string,
pagePath: string, pagePath: string,
...@@ -82,14 +84,11 @@ function readEntryJSFiles( ...@@ -82,14 +84,11 @@ function readEntryJSFiles(
`${pagePath}_${CLIENT_REFERENCE_MANIFEST}.js` `${pagePath}_${CLIENT_REFERENCE_MANIFEST}.js`
) )
try { try {
const g = global as Record<string, unknown> const rscManifest = evalClientReferenceManifestSource(
const prev = g.__RSC_MANIFEST readFileSync(manifestFile, 'utf8')
g.__RSC_MANIFEST = undefined ).__RSC_MANIFEST as
require(manifestFile)
const rscManifest = g.__RSC_MANIFEST as
| Record<string, { entryJSFiles?: Record<string, string[]> }> | Record<string, { entryJSFiles?: Record<string, string[]> }>
| undefined | undefined
g.__RSC_MANIFEST = prev
// The key in __RSC_MANIFEST is the app path (e.g. /blog/[slug]/page) // The key in __RSC_MANIFEST is the app path (e.g. /blog/[slug]/page)
const manifestEntry = rscManifest?.[pagePath] ?? rscManifest?.[appRoute] const manifestEntry = rscManifest?.[pagePath] ?? rscManifest?.[appRoute]
......
...@@ -602,6 +602,9 @@ export class ClientReferenceManifestPlugin { ...@@ -602,6 +602,9 @@ export class ClientReferenceManifestPlugin {
const pagePath = pageName.replace(/%5F/g, '_') const pagePath = pageName.replace(/%5F/g, '_')
const pageBundlePath = normalizePagePath(pagePath.slice('app'.length)) const pageBundlePath = normalizePagePath(pagePath.slice('app'.length))
// This shape is recognized and parsed as JSON by
// `classifyClientReferenceManifestSource` in
// `server/load-manifest.external.ts`. Keep both in sync.
compilation.emitAsset( compilation.emitAsset(
'server/app' + pageBundlePath + '_' + CLIENT_REFERENCE_MANIFEST + '.js', 'server/app' + pageBundlePath + '_' + CLIENT_REFERENCE_MANIFEST + '.js',
new sources.RawSource( new sources.RawSource(
......
...@@ -2,7 +2,8 @@ import type { DeepReadonly } from '../shared/lib/deep-readonly' ...@@ -2,7 +2,8 @@ import type { DeepReadonly } from '../shared/lib/deep-readonly'
import { join } from 'path' import { join } from 'path'
import { readFileSync } from 'fs' import { readFileSync } from 'fs'
import { runInNewContext } from 'vm' import { createContext, runInContext, runInNewContext } from 'vm'
import type { Context } from 'vm'
import { deepFreeze } from '../shared/lib/deep-freeze' import { deepFreeze } from '../shared/lib/deep-freeze'
const sharedCache = new Map<string, unknown>() const sharedCache = new Map<string, unknown>()
...@@ -118,10 +119,7 @@ export function evalManifest<T extends object>( ...@@ -118,10 +119,7 @@ export function evalManifest<T extends object>(
throw new Error('Manifest file is empty') throw new Error('Manifest file is empty')
} }
let contextObject = { let contextObject = evalClientReferenceManifestSource(content)
process: { env: { NEXT_DEPLOYMENT_ID: process.env.NEXT_DEPLOYMENT_ID } },
}
runInNewContext(content, contextObject)
// Freeze the context object so it cannot be modified if we're caching it. // Freeze the context object so it cannot be modified if we're caching it.
if (shouldCache) { if (shouldCache) {
...@@ -135,6 +133,241 @@ export function evalManifest<T extends object>( ...@@ -135,6 +133,241 @@ export function evalManifest<T extends object>(
return contextObject as T return contextObject as T
} }
export type ClientReferenceManifestSourceKind =
| {
/** A plain `globalThis.__RSC_MANIFEST[key] = <JSON>;` assignment. */
type: 'json'
key: string
value: unknown
}
| {
/** The JSON assignment followed by Turbopack's deployment ID suffix. */
type: 'deployment-id'
}
| {
/** Anything else: must be evaluated in an isolated V8 context. */
type: 'unknown'
}
const MANIFEST_PREFIX_RE =
// Webpack: `globalThis.__RSC_MANIFEST=(globalThis.__RSC_MANIFEST||{});`
// Turbopack: `globalThis.__RSC_MANIFEST = globalThis.__RSC_MANIFEST || {};`
/^\s*globalThis\.__RSC_MANIFEST\s*=\s*(?:\(\s*globalThis\.__RSC_MANIFEST\s*\|\|\s*\{\s*\}\s*\)|globalThis\.__RSC_MANIFEST\s*\|\|\s*\{\s*\})\s*;\s*globalThis\.__RSC_MANIFEST\[/
const ASSIGNMENT_RE = /\s*\]\s*=\s*/y
/**
* The deployment ID suffix Turbopack appends to client reference manifests
* when the deployment ID is added at runtime. Must be kept in sync with
* `crates/next-core/src/next_manifests/client_reference_manifest.rs`.
* `KEY` is replaced with the entry name string literal.
*/
const DEPLOYMENT_ID_SUFFIX_TEMPLATE = `
for (const key in globalThis.__RSC_MANIFEST[KEY].clientModules) {
const val = { ...globalThis.__RSC_MANIFEST[KEY].clientModules[key] }
globalThis.__RSC_MANIFEST[KEY].clientModules[key] = val
val.chunks = val.chunks.map((c) =>
typeof c === 'string'
? \`\${c}?dpl=\${process.env.NEXT_DEPLOYMENT_ID}\`
: [\`\${c[0]}?dpl=\${process.env.NEXT_DEPLOYMENT_ID}\`, c[1], c[2]])
}
`
/**
* Splits source into its non-empty lines with surrounding whitespace removed.
* Line breaks are preserved (they matter for ASI), only indentation and blank
* lines are ignored.
*/
function toNormalizedLines(source: string): string[] {
const lines: string[] = []
for (const line of source.split(/\r?\n/)) {
const trimmed = line.trim()
if (trimmed !== '') {
lines.push(trimmed)
}
}
return lines
}
function isDeploymentIdSuffix(suffix: string, keyLiteral: string): boolean {
const expected = toNormalizedLines(
DEPLOYMENT_ID_SUFFIX_TEMPLATE.split('[KEY]').join(`[${keyLiteral}]`)
)
const actual = toNormalizedLines(suffix)
return (
actual.length === expected.length &&
actual.every((line, i) => line === expected[i])
)
}
function hasOwnProtoKey(value: unknown): boolean {
if (value === null || typeof value !== 'object') {
return false
}
if (Object.hasOwn(value, '__proto__')) {
return true
}
for (const child of Object.values(value)) {
if (hasOwnProtoKey(child)) {
return true
}
}
return false
}
/**
* Classifies the source of a `*_client-reference-manifest.js` file without
* evaluating it. The webpack and Turbopack generated files are a JSON object
* assigned to `globalThis.__RSC_MANIFEST[<entry name>]`, which can be read
* with `JSON.parse` instead of evaluating JavaScript (`json`). Turbopack's
* deployment ID variant appends a fixed loop that still has to be evaluated
* (`deployment-id`).
*
* Anything that doesn't exactly match a known generated shape is classified
* as `unknown`, so the caller falls back to evaluating it in a new context.
*/
export function classifyClientReferenceManifestSource(
content: string
): ClientReferenceManifestSourceKind {
const unknown = { type: 'unknown' } as const
// `JSON.parse` treats `__proto__` as a regular key whereas an object
// literal sets the prototype, so the results could differ.
if (content.includes('__proto__')) {
return unknown
}
const prefix = MANIFEST_PREFIX_RE.exec(content)
if (!prefix) {
return unknown
}
// The entry name is a JSON string literal.
const keyStart = prefix[0].length
if (content[keyStart] !== '"') {
return unknown
}
let keyEnd = keyStart + 1
while (keyEnd < content.length && content[keyEnd] !== '"') {
keyEnd += content[keyEnd] === '\\' ? 2 : 1
}
if (keyEnd >= content.length) {
return unknown
}
const keyLiteral = content.slice(keyStart, keyEnd + 1)
ASSIGNMENT_RE.lastIndex = keyEnd + 1
if (!ASSIGNMENT_RE.test(content)) {
return unknown
}
const bodyStart = ASSIGNMENT_RE.lastIndex
// The generated JSON is serialized without line breaks, so the assignment
// ends on the line it starts on. Anything after it is a suffix.
const rest = content.slice(bodyStart)
const lineBreak = rest.search(/[\r\n]/)
const assignment = (
lineBreak === -1 ? rest : rest.slice(0, lineBreak)
).trimEnd()
const suffix = lineBreak === -1 ? '' : rest.slice(lineBreak)
if (!assignment.endsWith(';')) {
return unknown
}
let key: string
let value: unknown
try {
key = JSON.parse(keyLiteral)
value = JSON.parse(assignment.slice(0, -1))
} catch {
return unknown
}
// `__proto__` can also be spelled with unicode escapes, which the
// `includes` check above doesn't catch.
if (
content.includes('\\u') &&
(key === '__proto__' || hasOwnProtoKey(value))
) {
return unknown
}
if (suffix.trim() === '') {
return { type: 'json', key, value }
}
if (isDeploymentIdSuffix(suffix, keyLiteral)) {
return { type: 'deployment-id' }
}
return unknown
}
let deploymentIdManifestContext: Context | undefined
/**
* Evaluates a Turbopack client reference manifest with the deployment ID
* suffix. Creating a V8 context is expensive, so a single context is reused.
* This is only done for sources that were verified to exactly match the
* generated shape (see `classifyClientReferenceManifestSource`), which only
* write `globalThis.__RSC_MANIFEST` and read `process.env`, both of which are
* reset for every call. The context itself is never returned.
*/
function evalDeploymentIdManifest(content: string) {
deploymentIdManifestContext ??= createContext({})
const context = deploymentIdManifestContext
const processObject = {
env: { NEXT_DEPLOYMENT_ID: process.env.NEXT_DEPLOYMENT_ID },
}
context.process = processObject
context.__RSC_MANIFEST = undefined
try {
runInContext(content, context)
return {
process: processObject,
__RSC_MANIFEST: context.__RSC_MANIFEST,
}
} finally {
context.process = undefined
context.__RSC_MANIFEST = undefined
}
}
/**
* Evaluates the source of a `*_client-reference-manifest.js` file and returns
* the resulting global object (`{ process, __RSC_MANIFEST }`):
* - plain generated assignments are parsed with `JSON.parse`,
* - Turbopack's generated deployment ID variant runs in a reused V8 context,
* - anything else runs in a new V8 context.
*/
export function evalClientReferenceManifestSource(content: string): {
process: { env: { NEXT_DEPLOYMENT_ID: string | undefined } }
__RSC_MANIFEST?: Record<string, unknown>
[key: string]: unknown
} {
const kind = classifyClientReferenceManifestSource(content)
switch (kind.type) {
case 'json':
return {
process: {
env: { NEXT_DEPLOYMENT_ID: process.env.NEXT_DEPLOYMENT_ID },
},
__RSC_MANIFEST: { [kind.key]: kind.value },
}
case 'deployment-id':
return evalDeploymentIdManifest(content)
default: {
const contextObject = {
process: {
env: { NEXT_DEPLOYMENT_ID: process.env.NEXT_DEPLOYMENT_ID },
},
}
runInNewContext(content, contextObject)
return contextObject
}
}
}
export function evalManifestFromRelativePath<T extends object>({ export function evalManifestFromRelativePath<T extends object>({
projectDir, projectDir,
distDir, distDir,
......
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