From 82fdf406c43851ac75a82a0d49aa944cdc955254 Mon Sep 17 00:00:00 2001 From: Michael Wang Date: Mon, 16 Nov 2026 02:08:44 +0000 Subject: [PATCH] fix(producer): compile and render-telemetry tag scans run in linear time (#7896) * fix(producer): compile and render-telemetry tag scans run in linear time The compiler's gsap script rewrite, crossorigin and preload strips, remote media and image src scans and inline-script scan, and the render telemetry's tag counts, now use forward scans that return exactly what the regexes they replace returned. Those regexes rescanned to the end of the page from every tag opener that never closed, or from every character of a long whitespace run in one tag. The telemetry's script and style strip stops after 8 passes. * test(producer): give the scan fuzz tests room on CI and raise the producer size budget --- .../producer/src/services/htmlCompiler.ts | 94 ++-- .../src/services/htmlTagScans.test.ts | 363 +++++++++++++++ .../producer/src/services/htmlTagScans.ts | 414 ++++++++++++++++++ .../src/services/renderOrchestrator.test.ts | 28 ++ .../src/services/renderOrchestrator.ts | 49 ++- scripts/package-size-budgets.json | 2 +- 6 files changed, 863 insertions(+), 87 deletions(-) create mode 100644 packages/producer/src/services/htmlTagScans.test.ts create mode 100644 packages/producer/src/services/htmlTagScans.ts diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index 77f444502b..951b0a82a5 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -29,6 +29,14 @@ import { createReadStream, existsSync, mkdirSync, readFileSync } from "fs"; import { join, dirname, resolve, basename, relative, sep } from "path"; import { parseHTML } from "linkedom"; import { bundledScript } from "./bundledScripts.js"; +import { + inlineScriptBlocks, + remoteSrcUrls, + removeDelimitedRuns, + replaceGsapDistScripts, + stripCrossoriginAttr, + stripPreloadNone, +} from "./htmlTagScans.js"; import { COMPOSITION_SOURCE_URL, compileTimingAttrs, @@ -393,22 +401,15 @@ export interface RenderModeHints { reasons: RenderModeHint[]; } -const INLINE_SCRIPT_PATTERN = /]*)>([\s\S]*?)<\/script>/gi; const COMPILER_MOUNT_BLOCK_START = "/* __HF_COMPILER_MOUNT_START__ */"; const COMPILER_MOUNT_BLOCK_END = "/* __HF_COMPILER_MOUNT_END__ */"; function stripJsComments(source: string): string { - return source.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, ""); + return removeDelimitedRuns(source.replace(/\/\/.*$/gm, ""), "/*", "*/"); } function stripCompilerMountBootstrap(source: string): string { - return source.replace( - new RegExp( - `${COMPILER_MOUNT_BLOCK_START.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}[\\s\\S]*?${COMPILER_MOUNT_BLOCK_END.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`, - "g", - ), - "", - ); + return removeDelimitedRuns(source, COMPILER_MOUNT_BLOCK_START, COMPILER_MOUNT_BLOCK_END); } /** The composition the runtime reads data-motion-blur from: `data-root="true"`, else the first. */ @@ -449,12 +450,9 @@ export function detectRenderModeHints(html: string, pageMotionBlur?: "off"): Ren }); } - let scriptMatch: RegExpExecArray | null; - const scriptPattern = new RegExp(INLINE_SCRIPT_PATTERN.source, INLINE_SCRIPT_PATTERN.flags); - while ((scriptMatch = scriptPattern.exec(html)) !== null) { - const attrs = scriptMatch[1] || ""; + for (const { attrs, body } of inlineScriptBlocks(html)) { if (/\bsrc\s*=/i.test(attrs)) continue; - const content = stripJsComments(stripCompilerMountBootstrap(scriptMatch[2] || "")); + const content = stripJsComments(stripCompilerMountBootstrap(body)); if (!/requestAnimationFrame\s*\(/.test(content)) continue; reasons.push({ code: "requestAnimationFrame", @@ -551,12 +549,9 @@ const SHADER_TRANSITION_USAGE_PATTERN = /\b(?:(?:window|globalThis)\s*\.\s*)?HyperShader\s*\.\s*init\s*\(|\b__hf\s*\.\s*transitions\s*=/; export function detectShaderTransitionUsage(html: string): boolean { - let scriptMatch: RegExpExecArray | null; - const scriptPattern = new RegExp(INLINE_SCRIPT_PATTERN.source, INLINE_SCRIPT_PATTERN.flags); - while ((scriptMatch = scriptPattern.exec(html)) !== null) { - const attrs = scriptMatch[1] || ""; + for (const { attrs, body } of inlineScriptBlocks(html)) { if (/\bsrc\s*=/i.test(attrs)) continue; - const content = stripJsComments(stripCompilerMountBootstrap(scriptMatch[2] || "")); + const content = stripJsComments(stripCompilerMountBootstrap(body)); if (SHADER_TRANSITION_USAGE_PATTERN.test(content)) return true; } @@ -809,13 +804,13 @@ async function compileHtmlFile( // injected frame images, so the browser never needs to load the source. // Without this, videos with crossorigin="anonymous" targeting CORS-restricted // origins (e.g. S3 without CORS headers) keep readyState=0, blocking page setup. - compiledHtml = compiledHtml.replace(/(]*)\s+crossorigin(?:=["'][^"']*["'])?/gi, "$1"); + compiledHtml = stripCrossoriginAttr(compiledHtml, "video"); // Strip crossorigin from img elements. The renderer captures DOM frames visually — // no canvas readback — so CORS compliance is unnecessary. External images from // CORS-restricted origins (e.g. S3) render blank when crossorigin forces a failed // CORS request against the renderer's localhost file server. - compiledHtml = compiledHtml.replace(/(]*)\s+crossorigin(?:=["'][^"']*["'])?/gi, "$1"); + compiledHtml = stripCrossoriginAttr(compiledHtml, "img"); // Strip crossorigin from audio elements. Audio is processed out-of-band via // FFmpeg; the browser's CORS policy for audio elements is irrelevant to @@ -824,7 +819,7 @@ async function compileHtmlFile( // headers reject — leaving audio elements in a failed network state. The // FFmpeg audio path reads the src URL directly and is unaffected by browser // CORS, so stripping the attribute has no side effects. - compiledHtml = compiledHtml.replace(/(]*)\s+crossorigin(?:=["'][^"']*["'])?/gi, "$1"); + compiledHtml = stripCrossoriginAttr(compiledHtml, "audio"); return { html: compiledHtml, unresolvedCompositions }; } @@ -1540,22 +1535,6 @@ export function collectExternalAssets( } export const REMOTE_MEDIA_SUBDIR = "_remote_media"; -// Match opening tags of