From 8c9c94572a148c62675a4e181cec1e3480e75bc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sun, 22 Nov 2026 20:32:43 +0000 Subject: [PATCH] fix(producer): a render serves every GSAP the CLI ships, and its own when the CDN is blocked (#8349) * test(producer): an older GSAP our templates name, or a blocked CDN's GSAP 3, renders offline * fix(producer): a render serves every GSAP the CLI ships, and its own when the CDN is blocked * fix(cli): the CLI's size limit covers the GSAP 3.14.2 files renders serve --- bun.lock | 3 + packages/cli/package.json | 1 + packages/cli/scripts/build-copy.mjs | 24 +++++-- packages/cli/src/server/gsapVendor.test.ts | 37 ++++++++++ .../src/services/bundledScripts.test.ts | 71 ++++++++++++++++--- .../producer/src/services/bundledScripts.ts | 25 +++++-- .../producer/src/services/htmlCompiler.ts | 17 ++++- scripts/package-size-budgets.json | 2 +- 8 files changed, 156 insertions(+), 24 deletions(-) diff --git a/bun.lock b/bun.lock index 9271fe2e2d..b827d49cf7 100644 --- a/bun.lock +++ b/bun.lock @@ -106,6 +106,7 @@ "@types/fontkit": "^2.0.9", "@types/node": "^25.0.10", "ajv": "^8.20.0", + "gsap-3.14.2": "npm:gsap@3.14.2", "linkedom": "^0.18.12", "onnxruntime-node": "1.21.1", "picocolors": "^1.1.1", @@ -1694,6 +1695,8 @@ "gsap": ["gsap@3.15.0", "", {}, "sha512-dMW4CWBTUK1AEEDeZc1g4xpPGIrSf9fJF960qbTZmN/QwZIWY5wgliS6JWl9/25fpTGJrMRtSjGtOmPnfjZB+A=="], + "gsap-3.14.2": ["gsap@3.14.2", "", {}, "sha512-P8/mMxVLU7o4+55+1TCnQrPmgjPKnwkzkXOK1asnR9Jg2lna4tEY5qBJjMmAaOBDDZWtlRjBXjLa0w53G/uBLA=="], + "gtoken": ["gtoken@7.1.0", "", { "dependencies": { "gaxios": "^6.0.0", "jws": "^4.0.0" } }, "sha512-pCcEwRi+TKpMlxAQObHDQ56KawURgyAf6jtIY046fJ5tIv3zDe/LEIubckAO8fj6JnAxLdmWkUfNyulQ2iKdEw=="], "happy-dom": ["happy-dom@21.0.4", "", { "dependencies": { "@types/node": ">=20.0.0", "@types/whatwg-mimetype": "^3.0.2", "@types/ws": "^8.18.1", "buffer-image-size": "^0.6.4", "entities": "^7.0.1", "whatwg-mimetype": "^3.0.0", "ws": "^8.21.0" } }, "sha512-tFm+GaLBgo6o0awHnvHaNoraVo27qJ+2tZEtZ+cIa08hXbeitrxAJajLI05hsUy/2/YTKcUjQYzHe5mCcraLBw=="], diff --git a/packages/cli/package.json b/packages/cli/package.json index c65bcdf161..bc2d815e1b 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -92,6 +92,7 @@ "@types/fontkit": "^2.0.9", "@types/node": "^25.0.10", "ajv": "^8.20.0", + "gsap-3.14.2": "npm:gsap@3.14.2", "linkedom": "^0.18.12", "onnxruntime-node": "1.21.1", "picocolors": "^1.1.1", diff --git a/packages/cli/scripts/build-copy.mjs b/packages/cli/scripts/build-copy.mjs index f7168db2d0..e5caf267e6 100644 --- a/packages/cli/scripts/build-copy.mjs +++ b/packages/cli/scripts/build-copy.mjs @@ -92,11 +92,25 @@ function copyGsapVendorFiles() { } const vendorDir = join(DIST, "vendor", "npm"); rmSync(vendorDir, { recursive: true, force: true }); - const dest = join(vendorDir, `gsap@${version}`, "dist"); - mkdirSync(dest, { recursive: true }); - for (const file of GSAP_VENDOR_FILES) { - copyFileSync(join(gsapRoot, "dist", file), join(dest, file)); - chmodSync(join(dest, file), 0o644); + // Older versions templates still name ship too, as the CDN's exact files: package.json's `gsap-` aliases + // (src/server/gsapVendor.test.ts pins their bytes). + const cliManifest = join(REPO_ROOT, "packages", "cli", "package.json"); + const cliRequire = createRequire(cliManifest); + const aliases = Object.keys(JSON.parse(readFileSync(cliManifest, "utf-8")).devDependencies ?? {}); + const roots = [ + gsapRoot, + ...aliases + .filter((name) => /^gsap-\d+\.\d+\.\d+$/.test(name)) + .map((name) => dirname(cliRequire.resolve(`${name}/package.json`))), + ]; + for (const root of roots) { + const { version: shipped } = JSON.parse(readFileSync(join(root, "package.json"), "utf-8")); + const dest = join(vendorDir, `gsap@${shipped}`, "dist"); + mkdirSync(dest, { recursive: true }); + for (const file of GSAP_VENDOR_FILES) { + copyFileSync(join(root, "dist", file), join(dest, file)); + chmodSync(join(dest, file), 0o644); + } } } diff --git a/packages/cli/src/server/gsapVendor.test.ts b/packages/cli/src/server/gsapVendor.test.ts index b00264d761..74c758523c 100644 --- a/packages/cli/src/server/gsapVendor.test.ts +++ b/packages/cli/src/server/gsapVendor.test.ts @@ -1,4 +1,7 @@ import { execFileSync } from "node:child_process"; +import { createHash } from "node:crypto"; +import { readFileSync } from "node:fs"; +import { createRequire } from "node:module"; import { dirname, join } from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; import { describe, expect, it } from "vitest"; @@ -22,3 +25,37 @@ describe("resolveGsapVendorDir", () => { expect(out.trim()).toBe("ok"); }, 30_000); }); + +// The CDN's own files (jsDelivr's sha384, as a tag's integrity attribute would pin them). +const CDN_SHA384: Record> = { + "3.14.2": { + "gsap.min.js": "sG0Hv1tP1lZCk9KQmrIbY/XNwi+OY84GQqhMscbnsoBFqAz8KNCil1kvfL3Hbbk2", + "CustomEase.min.js": "Sjkr6w0fFKkxtEQY7MVGUGYK7lNjV5adCOLTzv8eBXdW2xDCLMNz5mo+iXchMXTh", + "MotionPathPlugin.min.js": "bRevQK3DiusS1XALpRyWfXNcSVNuNxBOUk12Tk44IsnfepFlBwFe8h/82zWbDxqi", + }, +}; + +describe("the older GSAP versions the CLI ships", () => { + const manifest = join(here, "..", "..", "package.json"); + const aliases = Object.keys(JSON.parse(readFileSync(manifest, "utf8")).devDependencies).filter( + (name) => /^gsap-\d+\.\d+\.\d+$/.test(name), + ); + + // fails without the fix: our own caption templates write gsap@3.14.2, which the CLI did not ship. + it("include 3.14.2, and each is the CDN's exact bytes", () => { + expect(aliases).toContain("gsap-3.14.2"); + const require = createRequire(manifest); + for (const alias of aliases) { + const version = alias.slice("gsap-".length); + const root = dirname(require.resolve(`${alias}/package.json`)); + expect(JSON.parse(readFileSync(join(root, "package.json"), "utf8")).version).toBe(version); + expect(Object.keys(CDN_SHA384[version] ?? {})).toHaveLength(3); + for (const [file, sha] of Object.entries(CDN_SHA384[version] ?? {})) { + const got = createHash("sha384") + .update(readFileSync(join(root, "dist", file))) + .digest("base64"); + expect(`${version}/${file} ${got}`).toBe(`${version}/${file} ${sha}`); + } + } + }); +}); diff --git a/packages/producer/src/services/bundledScripts.test.ts b/packages/producer/src/services/bundledScripts.test.ts index 8b92f91473..9a85e25767 100644 --- a/packages/producer/src/services/bundledScripts.test.ts +++ b/packages/producer/src/services/bundledScripts.test.ts @@ -1,9 +1,9 @@ import { afterEach, beforeEach, describe, expect, it, mock } from "bun:test"; import { createHash } from "node:crypto"; -import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; -import { gsapCdnDist } from "@hyperframes/core/gsap-cdn"; +import { GSAP_CDN_VERSION, gsapCdnDist } from "@hyperframes/core/gsap-cdn"; import { _setDownloadFetchForTests } from "@hyperframes/engine"; import { inlineExternalScripts } from "./htmlCompiler.js"; import { GSAP_VENDOR_DIR_ENV } from "./bundledScripts.js"; @@ -17,18 +17,25 @@ const failingFetch = () => throw new TypeError("net::ERR_INTERNET_DISCONNECTED"); }) as unknown as typeof fetch; -let dir: string; +/** The host's copy of `version`, laid out as the CLI ships it: `/gsap@/dist`. */ +const ship = (version: string, file: string, bytes: string) => { + const at = join(root, `gsap@${version}`, "dist"); + mkdirSync(at, { recursive: true }); + writeFileSync(join(at, file), bytes); + return at; +}; + +let root: string; const realEnv = process.env[GSAP_VENDOR_DIR_ENV]; beforeEach(() => { - dir = mkdtempSync(join(tmpdir(), "gsap-vendor-")); - writeFileSync(join(dir, "gsap.min.js"), BYTES); - process.env[GSAP_VENDOR_DIR_ENV] = dir; + root = mkdtempSync(join(tmpdir(), "gsap-vendor-")); + process.env[GSAP_VENDOR_DIR_ENV] = ship(GSAP_CDN_VERSION, "gsap.min.js", BYTES); }); afterEach(() => { _setDownloadFetchForTests(); if (realEnv === undefined) delete process.env[GSAP_VENDOR_DIR_ENV]; else process.env[GSAP_VENDOR_DIR_ENV] = realEnv; - rmSync(dir, { recursive: true, force: true }); + rmSync(root, { recursive: true, force: true }); }); describe("the host's own GSAP needs no network", () => { @@ -46,16 +53,60 @@ describe("the host's own GSAP needs no network", () => { expect(await inlineExternalScripts(page(URL_))).toContain("var net = 1;"); }); - it("leaves any other URL, including a mirror's, to the network", async () => { + it("leaves a version the host does not ship, and a mirror's URL, to the network", async () => { _setDownloadFetchForTests(mock(async () => new Response("var net = 1;"))); for (const other of [ - "https://cdn.jsdelivr.net/npm/gsap@3.14.2/dist/gsap.min.js", - "https://mirror.example.com/gsap@3.15.0/dist/gsap.min.js", + "https://cdn.jsdelivr.net/npm/gsap@3.12.5/dist/gsap.min.js", + `https://mirror.example.com/gsap@${GSAP_CDN_VERSION}/dist/gsap.min.js`, ]) { expect(await inlineExternalScripts(page(other))).toContain("var net = 1;"); } }); + // fails without the fix: our caption templates write gsap@3.14.2, and with jsDelivr blocked every export failed. + it("inlines another version the host ships, and its plugins, with no network", async () => { + ship("3.14.2", "gsap.min.js", "var gsap = 3142;"); + ship("3.14.2", "CustomEase.min.js", "var ease = 3142;"); + const fetch = failingFetch(); + _setDownloadFetchForTests(fetch); + const old = "https://cdn.jsdelivr.net/npm/gsap@3.14.2/dist/"; + const html = await inlineExternalScripts( + ``, + ); + expect(html).toContain("var gsap = 3142;"); + expect(html).toContain("var ease = 3142;"); + expect(fetch).not.toHaveBeenCalled(); + }); +}); + +describe("a GSAP 3 file the CDN cannot bring", () => { + const other = "https://cdn.jsdelivr.net/npm/gsap@3.12.5/dist/gsap.min.js"; + + // fails without the fix: the tag stayed external, so the render browser failed on the blocked CDN too. + it("is inlined as the host's own GSAP", async () => { + _setDownloadFetchForTests(failingFetch()); + expect(await inlineExternalScripts(page(other))).toContain(BYTES); + }); + + it("stays the CDN's file when the CDN answers", async () => { + _setDownloadFetchForTests(mock(async () => new Response("var gsap = 3125;"))); + const html = await inlineExternalScripts(page(other)); + expect(html).toContain("var gsap = 3125;"); + expect(html).not.toContain(BYTES); + }); + + it("is never replaced when its tag pins its bytes, nor when it is not GSAP 3 from the default CDN", async () => { + _setDownloadFetchForTests(failingFetch()); + for (const html of [ + page(other, ' integrity="sha384-AAAA"'), + page("https://cdn.jsdelivr.net/npm/gsap@2.1.3/dist/gsap.min.js"), + page("https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.min.js"), + page(`https://mirror.example.com/gsap@3.12.5/dist/gsap.min.js`), + ]) { + expect(await inlineExternalScripts(html)).not.toContain(BYTES); + } + }); + it("downloads when the host's copy fails the tag's integrity metadata", async () => { const fresh = "var fresh = 2;"; const digest = `sha256-${createHash("sha256").update(fresh).digest("base64")}`; diff --git a/packages/producer/src/services/bundledScripts.ts b/packages/producer/src/services/bundledScripts.ts index 43a2c07f9c..3e76d1e2eb 100644 --- a/packages/producer/src/services/bundledScripts.ts +++ b/packages/producer/src/services/bundledScripts.ts @@ -1,14 +1,27 @@ import { existsSync, readFileSync } from "node:fs"; import { join } from "node:path"; -import { gsapCdnDist } from "@hyperframes/core/gsap-cdn"; +import { GSAP_CDN_VERSION } from "@hyperframes/core/gsap-cdn"; -/** The folder a host that ships its own GSAP names, so a render needs no network for it. */ +/** The host's GSAP_CDN_VERSION files; other versions it ships sit beside them at `../../gsap@/dist`. */ export const GSAP_VENDOR_DIR_ENV = "HYPERFRAMES_GSAP_VENDOR_DIR"; -/** The host's bytes for exactly the default CDN's GSAP file; a mirror's URL differs, so it never matches. */ +const DEFAULT_CDN_GSAP_3 = + /^https:\/\/cdn\.jsdelivr\.net\/npm\/gsap@(3\.\d+\.\d+)\/dist\/([A-Za-z]+\.min\.js)$/; + +const read = (file: string) => (existsSync(file) ? readFileSync(file) : null); + +/** The host's bytes for exactly this default-CDN GSAP file, at any version the host ships. */ export function bundledScript(url: string): Uint8Array | null { const dir = process.env[GSAP_VENDOR_DIR_ENV]; - if (!dir || url !== `${gsapCdnDist()}gsap.min.js`) return null; - const file = join(dir, "gsap.min.js"); - return existsSync(file) ? readFileSync(file) : null; + const [, version, file] = DEFAULT_CDN_GSAP_3.exec(url) ?? []; + if (!dir || !version || !file) return null; + const at = version === GSAP_CDN_VERSION ? dir : join(dir, "..", "..", `gsap@${version}`, "dist"); + return read(join(at, file)); +} + +/** The same file at the host's own GSAP version, for one the network could not bring. */ +export function bundledGsapStandIn(url: string): Uint8Array | null { + const dir = process.env[GSAP_VENDOR_DIR_ENV]; + const [, , file] = DEFAULT_CDN_GSAP_3.exec(url) ?? []; + return dir && file ? read(join(dir, file)) : null; } diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index 86caffceb5..f9eb357ce0 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -28,7 +28,7 @@ import { createHash } from "node:crypto"; import { createReadStream, existsSync, mkdirSync, readFileSync, statSync } from "fs"; import { join, dirname, resolve, basename, relative, sep } from "path"; import { parseHTML } from "linkedom"; -import { bundledScript } from "./bundledScripts.js"; +import { bundledGsapStandIn, bundledScript } from "./bundledScripts.js"; import { inlineScriptBlocks, remoteSrcUrls, @@ -1659,6 +1659,9 @@ export async function inlineExternalScripts( }); const remote = new Set(externalScripts.filter((_, i) => !local[i]).map(({ src }) => src)); assertRemoteFetchCount("scripts", remote.size); + const pinned = new Set( + externalScripts.filter(({ el }) => el.getAttribute("integrity")).map(({ src }) => src), + ); const mirror = operatorGsapMirror(); const limit = createConcurrencyLimit(MAX_CONCURRENT_SCRIPT_FETCHES); @@ -1683,7 +1686,17 @@ export async function inlineExternalScripts( if (!bytes) { bytes = limit(async () => { fetchSignal.throwIfAborted(); - const downloaded = await downloadScript(src, mirror, fetchSignal); + const downloaded = await downloadScript(src, mirror, fetchSignal).catch( + (error: unknown) => { + // GSAP keeps its API within a major version, so a blocked CDN's 3.x file runs as the host's 3.x. + const standIn = pinned.has(src) || fetchSignal.aborted ? null : bundledGsapStandIn(src); + if (!standIn) throw error; + defaultLogger.warn( + `[Compiler] Could not download ${src} (${error}); using the host's own GSAP instead.`, + ); + return standIn; + }, + ); charge(downloaded.byteLength); return downloaded; }, fetchSignal); diff --git a/scripts/package-size-budgets.json b/scripts/package-size-budgets.json index 81b612772d..5497555bb7 100644 --- a/scripts/package-size-budgets.json +++ b/scripts/package-size-budgets.json @@ -5,7 +5,7 @@ }, "@hyperframes/cli": { "packed": 8577627, - "unpacked": 25500000 + "unpacked": 25830000 }, "@hyperframes/core": { "packed": 1948041,