diff --git a/packages/engine/src/index.ts b/packages/engine/src/index.ts index a76a4a32a2..4d9a755445 100644 --- a/packages/engine/src/index.ts +++ b/packages/engine/src/index.ts @@ -394,6 +394,7 @@ export { } from "./utils/loopbackOnly.js"; export { readWebGlVendorInfoFromCanvas } from "./utils/readWebGlVendorInfoFromCanvas.js"; +export { createConcurrencyLimit } from "./utils/concurrencyLimit.js"; export { extractMediaMetadata, diff --git a/packages/producer/src/services/compositionSizeLimit.ts b/packages/producer/src/services/compositionSizeLimit.ts index 28ea57ddc2..9e859481f1 100644 --- a/packages/producer/src/services/compositionSizeLimit.ts +++ b/packages/producer/src/services/compositionSizeLimit.ts @@ -49,6 +49,9 @@ const fileTooLarge = (file: string, bytes: number) => const limitText = `One render generates at most ${megabytes(MAX_EXPANDED_COMPOSITION_BYTES)}`; +export const REMOTE_SCRIPTS = "remote scripts"; +export const REMOTE_STYLESHEETS = "remote stylesheets"; + function generatedTooLarge(file: string, bytes: number): string { if (file === "composition variables") { return ( @@ -62,6 +65,12 @@ function generatedTooLarge(file: string, bytes: number): string { "Use shorter URLs or bind fewer elements." ); } + if (file === REMOTE_SCRIPTS || file === REMOTE_STYLESHEETS) { + return ( + `Inlined ${file} add up to ${megabytes(bytes)}. ${limitText}. ` + + "Bundle scripts and fonts with the project, or link each one once." + ); + } return ( `Scripts, styles and rebased asset paths generated for compositions, ending at "${file}", add up to ` + `${megabytes(bytes)}. One render generates at most ` + diff --git a/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts b/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts index 293c7cde53..9fe3082bed 100644 --- a/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts +++ b/packages/producer/src/services/htmlCompiler.sizeLimit.test.ts @@ -2,13 +2,19 @@ import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { parseHTML } from "linkedom"; -import { describe, expect, it, vi } from "vitest"; +import { afterEach, describe, expect, it, vi } from "vitest"; +import { _setDownloadFetchForTests } from "@hyperframes/engine"; import { assertCompositionFileSize, MAX_COMPOSITION_HTML_BYTES, MAX_EXPANDED_COMPOSITION_BYTES, } from "./compositionSizeLimit.js"; -import { compileForRender, MAX_VARIABLE_MEDIA_SRC_BYTES } from "./htmlCompiler.js"; +import { + compileForRender, + inlineExternalScripts, + localizeRemoteFontFaces, + MAX_VARIABLE_MEDIA_SRC_BYTES, +} from "./htmlCompiler.js"; import { extractSafeRenderErrorCode } from "./safeRenderErrorCode.js"; const ROOT = ` @@ -397,3 +403,88 @@ describe("asset paths rebased from a composition's folder", () => { expect(extractSafeRenderErrorCode(error)).toBe("COMPOSITION_TOO_LARGE"); }); }); + +describe("remote scripts and stylesheets inlined at compile time", () => { + afterEach(() => _setDownloadFetchForTests()); + const MiB = 1024 * 1024; + + /** Serves `body(url)` and records each request and the most requests open at once. */ + function serve(body: (url: string) => string) { + const seen = { requests: [] as string[], open: 0, mostOpen: 0 }; + _setDownloadFetchForTests(async (url: string) => { + seen.requests.push(url); + seen.mostOpen = Math.max(seen.mostOpen, ++seen.open); + await new Promise((resolve) => setImmediate(resolve)); + seen.open -= 1; + return new Response(body(url)); + }); + return seen; + } + const scriptTags = (urls: string[]) => + `
${urls.map((url) => ``).join("")}`; + + it("fetches a repeated script once and refuses copies past the render's limit", async () => { + const seen = serve(() => `/*${"x".repeat(MiB)}*/`); + const url = "https://cdn.example.com/big.js"; + const error = await inlineExternalScripts(scriptTags(Array(40).fill(url))).catch((e) => e); + expect(extractSafeRenderErrorCode(error)).toBe("COMPOSITION_TOO_LARGE"); + expect(seen.requests).toEqual([url]); + }); + + it("downloads a few scripts at a time and stops the queued ones once over the limit", async () => { + const seen = serve(() => `/*${"x".repeat(8 * MiB)}*/`); + const urls = Array.from({ length: 20 }, (_, i) => `https://cdn.example.com/s${i}.js`); + const error = await inlineExternalScripts(scriptTags(urls)).catch((e) => e); + expect(extractSafeRenderErrorCode(error)).toBe("COMPOSITION_TOO_LARGE"); + expect(seen.mostOpen).toBe(4); + expect(seen.requests.length).toBeLessThan(urls.length); + }); + + it("stops queued downloads once a script fails its integrity check", async () => { + const seen = serve(() => `/*${"x".repeat(MiB)}*/`); + const urls = Array.from({ length: 40 }, (_, i) => `https://cdn.example.com/i${i}.js`); + const html = scriptTags(urls).replaceAll("