diff --git a/.github/workflows/lambda-arm64-image.yml b/.github/workflows/lambda-arm64-image.yml index 2b20d44648..9c9de6c53c 100644 --- a/.github/workflows/lambda-arm64-image.yml +++ b/.github/workflows/lambda-arm64-image.yml @@ -129,8 +129,14 @@ jobs: reply=$(curl -sS --retry 30 --retry-all-errors --retry-delay 1 \ -d '{}' http://localhost:9001/2015-03-31/functions/function/invocations) docker logs hf-smoke - echo "$reply" + jq 'del(.textPng)' <<<"$reply" jq -e '.arch == "arm64" and .screenshotBytes > 0 and (.ffmpeg | startswith("ffmpeg version 7.0.2"))' <<<"$reply" + mkdir text && jq -S .fonts <<<"$reply" >text/fonts.json && jq -r .textPng <<<"$reply" | base64 -d >text/text.png + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: text-arm64 + path: text/ + retention-days: 7 - name: The Lambda runtime loads the handler run: | docker run -d --name hf-video -p 9000:8080 hf-video:ci @@ -139,3 +145,46 @@ jobs: docker logs hf-video echo "$reply" grep -q 'no recognised Action' <<<"$reply" + + # The x86 function: the handler ZIP on the same base, running its @sparticuz/chromium. + x86-text: + name: x86 text render + needs: handler-zip + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + name: handler-zip + path: artifacts + - name: The handler ZIP renders the text page under the Lambda runtime + run: | + unzip -q artifacts/handler.zip -d task + cp packages/aws-lambda/image/smoke-video.mjs task/ + base=$(sed -n 's/^ARG BASE=//p' packages/aws-lambda/image/Dockerfile) + docker run -d --name hf-x86 --platform linux/amd64 -p 9002:8080 -v "$PWD/task:/var/task:ro" \ + "$base" smoke-video.handler + reply=$(curl -sS --retry 30 --retry-all-errors --retry-delay 1 \ + -d '{}' http://localhost:9002/2015-03-31/functions/function/invocations) + docker logs hf-x86 + jq 'del(.textPng)' <<<"$reply" + jq -e '.arch == "x64" and .screenshotBytes > 0' <<<"$reply" + mkdir text && jq -S .fonts <<<"$reply" >text/fonts.json && jq -r .textPng <<<"$reply" | base64 -d >text/text.png + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: text-x86 + path: text/ + retention-days: 7 + + # --video-arch picks which of the two renders a stack's frames, so each line must resolve to the same fonts on both. + text-compare: + name: Text fonts match across arches + needs: [image, x86-text] + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + pattern: text-* + - run: cat text-arm64/fonts.json && diff text-x86/fonts.json text-arm64/fonts.json diff --git a/.github/workflows/lambda-handler-zip.yml b/.github/workflows/lambda-handler-zip.yml new file mode 100644 index 0000000000..0df1232a0d --- /dev/null +++ b/.github/workflows/lambda-handler-zip.yml @@ -0,0 +1,50 @@ +name: Lambda handler ZIP + +permissions: + contents: read + +# The x86 handler ZIP sits close to its unzipped-size gate, and the handler bundles the producer, engine and core, +# so a change there can push it over. Build it on those PRs, so that fails before merge, not in the release job. +on: + pull_request: + paths: + - "packages/aws-lambda/**" + - "packages/producer/src/**" + - "packages/engine/src/**" + - "packages/core/src/**" + - "packages/parsers/src/**" + - "bun.lock" + - ".github/workflows/lambda-handler-zip.yml" + +concurrency: + group: lambda-handler-zip-${{ github.ref }} + cancel-in-progress: true + +jobs: + changes: + name: Detect changes + runs-on: ubuntu-latest + timeout-minutes: 4 + outputs: + release_only: ${{ steps.release.outputs.release_only }} + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + fetch-depth: 0 + - name: Find a release-only diff + id: release + uses: ./.github/actions/release-only-diff + + handler-zip: + name: Handler ZIP size + needs: changes + # A release PR changes versions and notes only; a failed change detection must not skip the build. + if: ${{ !cancelled() && needs.changes.outputs.release_only != 'true' }} + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2 + - run: bun install --frozen-lockfile + - run: bun run build + - run: bun run --cwd packages/aws-lambda build:zip diff --git a/bun.lock b/bun.lock index 0a9aad4b80..d717162ec7 100644 --- a/bun.lock +++ b/bun.lock @@ -42,6 +42,7 @@ "tar": "^7.5.22", }, "devDependencies": { + "@hyperframes/engine": "workspace:*", "@types/node": "^25.0.10", "@types/tar": "^6.1.13", "aws-cdk-lib": "^2.130.0", diff --git a/docs/packages/aws-lambda.mdx b/docs/packages/aws-lambda.mdx index c3a8899b1b..b1d2d538d3 100644 --- a/docs/packages/aws-lambda.mdx +++ b/docs/packages/aws-lambda.mdx @@ -56,7 +56,7 @@ The Lambda adapter wraps the distributed producer primitives behind one dispatch All three phases must fit the configured per-invocation Lambda timeout, at most 15 minutes, and the function's temporary storage. Smaller chunks can reduce each worker's extraction and rendering work, but do not divide video extraction that still happens in Plan. See [long-video planning limits](/deploy/aws-lambda#long-video-planning-times-out) for FFmpeg frame-stall timeouts and how to split jobs that cannot fit. -The x86 handler ZIP uses `@sparticuz/chromium` by default, with a build-time fallback for bundling `chrome-headless-shell` directly when needed. The arm64 video image runs Chrome for Testing's linux-arm64 `chrome-headless-shell`. +The x86 handler ZIP uses `@sparticuz/chromium`; a `chrome-headless-shell` ZIP is over Lambda's 250 MiB unzipped limit. The arm64 video image runs Chrome for Testing's linux-arm64 `chrome-headless-shell`, with `@sparticuz/chromium`'s fonts so text resolves as on x86. ### Page network restriction diff --git a/docs/packages/cli.mdx b/docs/packages/cli.mdx index b89de293c9..1c9fa92fa5 100644 --- a/docs/packages/cli.mdx +++ b/docs/packages/cli.mdx @@ -3070,8 +3070,8 @@ deploy: | `--profile` | `AWS_PROFILE` | AWS profile name | | `--concurrency` | none | Lambda reserved concurrency. Without it, a new stack reserves none and each render runs up to 90% of the account's unreserved concurrency; a redeploy keeps the stack's current reservation; `none` removes it | | `--memory` | `10240` | Lambda memory in MB | -| `--chrome-source` | `sparticuz` | `sparticuz` or `chrome-headless-shell` | -| `--video-arch` | `arm64` | `arm64` runs the plan and the chunks on an arm64 container-image function, pushed to an ECR repository named `-video`; audio and assemble stay on x86. The image runs its own `chrome-headless-shell`; the handler ZIP it is built from needs `sparticuz`. `x86_64` runs everything on the x86 function | +| `--chrome-source` | `sparticuz` | `sparticuz`. `chrome-headless-shell` is refused: it does not fit a ZIP function, and the arm64 video image already runs it | +| `--video-arch` | `arm64` | `arm64` runs the plan and the chunks on an arm64 container-image function, pushed to an ECR repository named `-video`; audio and assemble stay on x86. The image runs its own `chrome-headless-shell`. `x86_64` runs everything on the x86 function | | `--skip-build` | off | Reuse the existing `handler.zip` | | `--no-warm` | warm | Skip the one warm-up call to the video function after deploy, which loads its container image so the first render is not the one that waits for it | diff --git a/examples/aws-lambda/README.md b/examples/aws-lambda/README.md index 92d1b57fee..51e0f50d0b 100644 --- a/examples/aws-lambda/README.md +++ b/examples/aws-lambda/README.md @@ -200,7 +200,7 @@ with `--profile ` or `AWS_PROFILE=`. | `LambdaMemoryMb` | `10240` | Lambda memory; Lambda allocates CPU proportionally. 10 GB recommended for 1080p. | | `LambdaTimeoutSec` | `900` | Per-invocation timeout. 15 min is Lambda's hard ceiling. | | `ReservedConcurrency` | `-1` | Hard cap on simultaneous Lambda invocations. `-1` = unreserved. Set to e.g. `50` to bound cost. | -| `ChromeSource` | `sparticuz` | Must match the `--source=` flag passed to `build-zip.ts`. | +| `ChromeSource` | `sparticuz` | Must match the `--source=` flag passed to `build-zip.ts`; a shell ZIP is over Lambda's limit. | | `ChunkInvocationAlarmThreshold` | `1000` | CloudWatch alarm threshold (RenderChunk invocations per hour). | ## Cleanup diff --git a/examples/aws-lambda/template.yaml b/examples/aws-lambda/template.yaml index 2f83385705..0b4582c75f 100644 --- a/examples/aws-lambda/template.yaml +++ b/examples/aws-lambda/template.yaml @@ -53,8 +53,9 @@ Parameters: Description: >- Which Chrome runtime the bundled ZIP was built with. Must match the `--source=` flag passed to `build-zip.ts`. The handler reads this - via the HYPERFRAMES_LAMBDA_CHROME_SOURCE env var at boot. The arm64 - video image always runs its own chrome-headless-shell. + via the HYPERFRAMES_LAMBDA_CHROME_SOURCE env var at boot. A + chrome-headless-shell ZIP is over Lambda's unzipped limit today; the + arm64 video image always runs its own chrome-headless-shell. ChunkInvocationAlarmThreshold: Type: Number diff --git a/packages/aws-lambda/README.md b/packages/aws-lambda/README.md index 5d35d42f69..a66d51c56e 100644 --- a/packages/aws-lambda/README.md +++ b/packages/aws-lambda/README.md @@ -90,10 +90,10 @@ when `sites//files.json` is missing, the render uses that site's The package supports two Chromium sources: -| Source | Default | Size | When to pick it | -| ------------------------------- | ------- | ------------------ | --------------------------------------------------------------------------------------------------------------------- | -| `@sparticuz/chromium` | yes | ~70 MiB compressed | Lambda. Decompresses into `/tmp` at runtime; the rest of the ecosystem already uses it for headless-Chrome-in-Lambda. | -| Bundled `chrome-headless-shell` | no | ~296 MB | Fallback. Used if `@sparticuz/chromium` ever drops `HeadlessExperimental.beginFrame` support. | +| Source | Default | Size | When to pick it | +| ------------------------------- | ------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------- | +| `@sparticuz/chromium` | yes | ~70 MiB compressed | Lambda. Decompresses into `/tmp` at runtime; the rest of the ecosystem already uses it for headless-Chrome-in-Lambda. | +| Bundled `chrome-headless-shell` | no | ~296 MB | Does not fit a ZIP function today. Kept for the case where `@sparticuz/chromium` drops `HeadlessExperimental.beginFrame`. | Pick the source at build time: @@ -105,14 +105,19 @@ bun run --cwd packages/aws-lambda build:zip -- --source=chrome-headless-shell The handler reads `HYPERFRAMES_LAMBDA_CHROME_SOURCE` at boot, and for `chrome-headless-shell` the executable from `HYPERFRAMES_LAMBDA_CHROME_PATH`. `examples/aws-lambda/template.yaml` and the CDK construct set both from the -Chrome source. The build stages the whole shell folder (the executable needs -its ICU data and `.pak` files beside it), which is over Lambda's 250 MiB -unzipped limit for a ZIP function, so the x86 ZIP ships `@sparticuz/chromium`. +Chrome source. The build stages the repo's pinned shell build, and only when +its folder matches the pinned digest. It stages the whole folder (the +executable needs its ICU data and `.pak` files beside it), which is over +Lambda's 250 MiB unzipped limit for a ZIP function, so the build fails its +size gate and the x86 ZIP ships `@sparticuz/chromium`. `hyperframes lambda +deploy` refuses `--chrome-source chrome-headless-shell` for the same reason. The arm64 video image (`image/Dockerfile`, `video` target) does not use `@sparticuz/chromium`: it installs Chrome for Testing's linux-arm64 `chrome-headless-shell` folder, pinned by version and sha256, with the shared -libraries and fonts it needs, and sets both env vars itself. +libraries it needs, and sets both env vars itself. It keeps `@sparticuz/chromium`'s +fonts and `fonts.conf` from the handler ZIP, so text resolves to the same fonts as +on the x86 function; CI compares the two on a mixed-script page. ## BeginFrame regression guard @@ -130,7 +135,9 @@ bun run --cwd packages/aws-lambda probe:beginframe:docker The probe boots `@sparticuz/chromium` inside `public.ecr.aws/lambda/nodejs:22` and asserts CDP `beginFrame` with `screenshot: true` returns a PNG buffer. Exit code 0 = green; non-zero = -fall back to bundling chrome-headless-shell directly via `--source=chrome-headless-shell`. +`@sparticuz/chromium` lost BeginFrame. `--source=chrome-headless-shell` does +not fit a ZIP function, so that function would move to a container image like +the arm64 video function. ## Building the ZIP @@ -150,7 +157,9 @@ designed to extract cleanly into Lambda's `/var/task/`. - Unzipped ≤ 248 MiB (in-house budget; Lambda hard ceiling is 250 MiB unzipped — AWS docs label this "250 MB" but use binary mebibytes) - Zipped ≤ 150 MiB (in-house budget; Lambda has no hard zipped cap for S3-deployed functions) -CI fails the PR if either is exceeded. +The sparticuz ZIP is about 1.5 MiB under the unzipped budget, and the handler +bundles the producer, engine and core. So the `Lambda handler ZIP` workflow +builds it on every PR that touches them, and fails the PR if either is exceeded. ## Running tests diff --git a/packages/aws-lambda/image/Dockerfile b/packages/aws-lambda/image/Dockerfile index f20990992c..9946cd3bd1 100644 --- a/packages/aws-lambda/image/Dockerfile +++ b/packages/aws-lambda/image/Dockerfile @@ -38,8 +38,9 @@ COPY --from=check /check.txt /hf-media-check.txt # The video function. /var/task comes from scripts/stage-arm64-image.ts as the `task` build context: # docker buildx build --platform linux/arm64 --target video --build-context task=/task packages/aws-lambda/image # Its Chrome is Chrome for Testing's linux-arm64 chrome-headless-shell, the whole folder: the executable -# alone finds no ICU data or .pak resources. The base has none of the shared libraries the shell links -# and no fonts, so they come from the base's own repository; this RUN needs an arm64 host or emulation. +# alone finds no ICU data or .pak resources. The base has none of the shared libraries the shell links, so +# they come from the base's own repository; this RUN needs an arm64 host or emulation. Its fonts are the +# x86 function's: stage-arm64-image.ts unpacks @sparticuz/chromium's fonts and fonts.conf into /var/task/fonts. # Bump the version and the archive's sha256 together, with the rest of the repo's pins. FROM ${BASE} AS video ARG CHROME_HEADLESS_SHELL_VERSION=158.0.8127.36 @@ -47,7 +48,7 @@ ARG CHROME_HEADLESS_SHELL_SHA256=176ee5c55486c22de43128871189061365ea28e881fc960 COPY fetch.sh /fetch.sh RUN . /fetch.sh && retry microdnf install -y --nodocs --setopt=install_weak_deps=0 --releasever=2023.12.20260914 \ alsa-lib at-spi2-atk at-spi2-core atk dbus-libs expat fontconfig libX11 libXcomposite libXdamage \ - libXext libXfixes libXrandr libxcb libxkbcommon liberation-fonts mesa-libgbm nspr nss nss-util \ + libXext libXfixes libXrandr libxcb libxkbcommon mesa-libgbm nspr nss nss-util \ systemd-libs unzip && \ curl -fsSL --retry 5 --retry-all-errors --retry-delay 3 --connect-timeout 20 --max-time 600 -o /tmp/chs.zip \ "https://storage.googleapis.com/chrome-for-testing-public/${CHROME_HEADLESS_SHELL_VERSION}/linux-arm64/chrome-headless-shell-linux-arm64.zip" && \ @@ -57,6 +58,7 @@ RUN . /fetch.sh && retry microdnf install -y --nodocs --setopt=install_weak_deps /opt/chrome-headless-shell-linux-arm64/chrome-headless-shell --version | grep -F "${CHROME_HEADLESS_SHELL_VERSION}" # src/chromium.ts launches this executable. ENV HYPERFRAMES_LAMBDA_CHROME_SOURCE=chrome-headless-shell \ - HYPERFRAMES_LAMBDA_CHROME_PATH=/opt/chrome-headless-shell-linux-arm64/chrome-headless-shell + HYPERFRAMES_LAMBDA_CHROME_PATH=/opt/chrome-headless-shell-linux-arm64/chrome-headless-shell \ + FONTCONFIG_PATH=/var/task/fonts COPY --from=task / /var/task/ CMD ["handler.handler"] diff --git a/packages/aws-lambda/image/smoke-video.mjs b/packages/aws-lambda/image/smoke-video.mjs index 4223764890..f7e3d33068 100644 --- a/packages/aws-lambda/image/smoke-video.mjs +++ b/packages/aws-lambda/image/smoke-video.mjs @@ -1,27 +1,74 @@ -// A handler mounted into the video image and invoked through its own Lambda bootstrap, which sets -// AWS_EXECUTION_ENV as Lambda does: the bundled ffmpeg answers and the image's chrome-headless-shell -// renders, found where src/chromium.ts finds it and launched with the engine's Lambda flags. +// A handler mounted into a Lambda Node 22 image and invoked through its own Lambda bootstrap, which sets +// AWS_EXECUTION_ENV as Lambda does. In the arm64 video image it runs the image's chrome-headless-shell, found +// where src/chromium.ts finds it; in the x86 base over the unzipped handler ZIP, @sparticuz/chromium. +// It reports the platform fonts each line of a mixed-script page renders with, so the two can be compared. import { execFileSync } from "node:child_process"; import puppeteer from "puppeteer-core"; +const LINES = { + latin: "sans-serif|Quick brown fox ÀÉÎÕÜ ß œ", + serif: "serif|Quick brown fox ÀÉÎÕÜ ß œ", + mono: "monospace|Quick brown fox 0123456789", + greekCyrillic: "sans-serif|Ελληνικά Кириллица", + cjk: "sans-serif|漢字 かな カナ 한국어", + arabic: "sans-serif|العربية", + emoji: "sans-serif|😀 🎬 ✅", +}; + +const textPage = Object.entries(LINES) + .map(([id, line]) => { + const [family, text] = line.split("|"); + return `

${text}

`; + }) + .join(""); + +async function launch() { + if (process.env.HYPERFRAMES_LAMBDA_CHROME_PATH) { + return puppeteer.launch({ + executablePath: process.env.HYPERFRAMES_LAMBDA_CHROME_PATH, + args: ["--no-sandbox", "--disable-setuid-sandbox", "--no-zygote", "--disable-dev-shm-usage"], + }); + } + const { default: chromium } = await import("@sparticuz/chromium"); + return puppeteer.launch({ executablePath: await chromium.executablePath(), args: chromium.args }); +} + +async function platformFonts(page) { + const cdp = await page.createCDPSession(); + await cdp.send("DOM.enable"); + await cdp.send("CSS.enable"); + const { root } = await cdp.send("DOM.getDocument", { depth: -1 }); + const fonts = {}; + for (const id of Object.keys(LINES)) { + const { nodeId } = await cdp.send("DOM.querySelector", { + nodeId: root.nodeId, + selector: `#${id}`, + }); + const { fonts: used } = await cdp.send("CSS.getPlatformFontsForNode", { nodeId }); + fonts[id] = used.map((font) => `${font.familyName}:${font.glyphCount}`).sort(); + } + return fonts; +} + export const handler = async () => { const ffmpeg = execFileSync("/var/task/bin/ffmpeg", ["-hide_banner", "-version"], { encoding: "utf-8", }).split("\n")[0]; - const browser = await puppeteer.launch({ - executablePath: process.env.HYPERFRAMES_LAMBDA_CHROME_PATH, - args: ["--no-sandbox", "--disable-setuid-sandbox", "--no-zygote", "--disable-dev-shm-usage"], - }); + const browser = await launch(); try { const page = await browser.newPage(); await page.setContent("
"); const shot = await page.screenshot({ clip: { x: 0, y: 0, width: 64, height: 64 } }); + await page.setViewport({ width: 640, height: 360 }); + await page.setContent(textPage); return { executionEnv: process.env.AWS_EXECUTION_ENV, arch: process.arch, ffmpeg, chrome: await browser.version(), screenshotBytes: shot.length, + fonts: await platformFonts(page), + textPng: Buffer.from(await page.screenshot()).toString("base64"), }; } finally { await browser.close(); diff --git a/packages/aws-lambda/package.json b/packages/aws-lambda/package.json index 7be449240f..18c459e757 100644 --- a/packages/aws-lambda/package.json +++ b/packages/aws-lambda/package.json @@ -87,6 +87,7 @@ "tar": "^7.5.22" }, "devDependencies": { + "@hyperframes/engine": "workspace:*", "@types/node": "^25.0.10", "@types/tar": "^6.1.13", "aws-cdk-lib": "^2.130.0", diff --git a/packages/aws-lambda/scripts/_chromeHeadlessShell.ts b/packages/aws-lambda/scripts/_chromeHeadlessShell.ts index 2a876db0dd..9fe04e2c91 100644 --- a/packages/aws-lambda/scripts/_chromeHeadlessShell.ts +++ b/packages/aws-lambda/scripts/_chromeHeadlessShell.ts @@ -1,62 +1,45 @@ -import { cpSync, existsSync, readdirSync } from "node:fs"; +import { cpSync, existsSync } from "node:fs"; import { join } from "node:path"; +import { + isPinnedHeadlessShell, + managedChromeVersion, +} from "@hyperframes/engine/chrome-host-ceiling"; import { BUNDLED_SHELL_DIR } from "../src/chromium.js"; /** - * Stage the newest linux64 chrome-headless-shell from the Puppeteer cache under `home` into + * Stage the repo's pinned linux64 chrome-headless-shell from the Puppeteer cache under `home` into * `//`. The whole folder: the executable alone exits at launch * ("Invalid file descriptor to ICU data received") without icudtl.dat and its .pak files. - * Returns the cached version it staged. + * Only the pinned build, and only when its folder matches the pinned digest. Returns the version. */ export function stageChromeHeadlessShell( stagingDir: string, home = process.env.HOME ?? "/root", + isPinned: (executable: string) => boolean = isPinnedHeadlessShell, ): string { - // The fallback path bundles the same chrome-headless-shell build the K8s deploy uses. It is - // fetched via `@puppeteer/browsers` into the host's `~/.cache/puppeteer/`; the build re-uses that - // cache rather than redownloading. - const baseDir = join(home, ".cache", "puppeteer", "chrome-headless-shell"); - if (!existsSync(baseDir)) { + const version = managedChromeVersion("linux"); + const cache = join(home, ".cache", "puppeteer"); + const folder = join( + cache, + "chrome-headless-shell", + `linux-${version}`, + "chrome-headless-shell-linux64", + ); + const executable = join(folder, "chrome-headless-shell"); + if (!existsSync(executable)) { throw new Error( - `[build-zip] chrome-headless-shell cache missing at ${baseDir}. Run\n` + - ` npx --yes @puppeteer/browsers install chrome-headless-shell@158.0.8127.36 --path ${home}/.cache/puppeteer\n` + + `[build-zip] chrome-headless-shell ${version} is not in ${cache}. Run\n` + + ` npx --yes @puppeteer/browsers install chrome-headless-shell@${version} --path ${cache}\n` + `before --source=chrome-headless-shell.`, ); } - // Sort by numeric semver descending. `sort().reverse()` is lexicographic, - // which silently picks "99.0.0" over "131.0.0" once Chrome ships - // three-digit majors that aren't strictly width-aligned. `compareSemver` - // returns negative/zero/positive on (a, b), so descending = `b - a`. - const versions = readdirSync(baseDir).sort((a, b) => compareSemver(b, a)); - for (const v of versions) { - const folder = join(baseDir, v, "chrome-headless-shell-linux64"); - if (existsSync(join(folder, "chrome-headless-shell"))) { - // cpSync keeps each file's mode, so the executable stays executable. - cpSync(folder, join(stagingDir, BUNDLED_SHELL_DIR), { recursive: true }); - return v; - } + if (!isPinned(executable)) { + throw new Error( + `[build-zip] ${folder} does not match the pinned chrome-headless-shell ${version} digest. ` + + `Delete it and install it again.`, + ); } - throw new Error(`[build-zip] no linux64 chrome-headless-shell binary found under ${baseDir}.`); -} - -/** - * Compare two semver-shaped strings like "131.0.6778.108". Treats any - * non-numeric directory name as `-Infinity` so it sorts to the bottom - * (Puppeteer's cache layout sometimes includes `latest` or branch tags). - * Used by `stageChromeHeadlessShell` to pick the newest cached Chrome - * without tripping on the lexicographic "99 > 131" trap. - */ -function compareSemver(a: string, b: string): number { - const partsA = a.split(".").map((s) => Number.parseInt(s, 10)); - const partsB = b.split(".").map((s) => Number.parseInt(s, 10)); - const len = Math.max(partsA.length, partsB.length); - for (let i = 0; i < len; i++) { - const ai = partsA[i] ?? 0; - const bi = partsB[i] ?? 0; - if (Number.isNaN(ai) && Number.isNaN(bi)) continue; - if (Number.isNaN(ai)) return -1; - if (Number.isNaN(bi)) return 1; - if (ai !== bi) return ai - bi; - } - return 0; + // cpSync keeps each file's mode, so the executable stays executable. + cpSync(folder, join(stagingDir, BUNDLED_SHELL_DIR), { recursive: true }); + return version; } diff --git a/packages/aws-lambda/scripts/build-zip.test.ts b/packages/aws-lambda/scripts/build-zip.test.ts index aaea726f49..1b40fd03a4 100644 --- a/packages/aws-lambda/scripts/build-zip.test.ts +++ b/packages/aws-lambda/scripts/build-zip.test.ts @@ -1,9 +1,18 @@ import { describe, expect, it } from "bun:test"; import { spawnSync } from "node:child_process"; -import { mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; +import { + existsSync, + mkdirSync, + mkdtempSync, + readFileSync, + rmSync, + statSync, + writeFileSync, +} from "node:fs"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { pathToFileURL } from "node:url"; +import { managedChromeVersion } from "@hyperframes/engine/chrome-host-ceiling"; import * as esbuild from "esbuild"; import { BUNDLED_SHELL_DIR, BUNDLED_SHELL_PATH } from "../src/chromium.js"; import { stageChromeHeadlessShell } from "./_chromeHeadlessShell.js"; @@ -79,10 +88,11 @@ describe("build-zip handler banner", () => { }); describe("build-zip chrome-headless-shell staging", () => { + // @puppeteer/browsers' layout: chrome-headless-shell/linux-/chrome-headless-shell-linux64/. function puppeteerCache(versions: Record>): string { const home = mkdtempSync(join(tmpdir(), "hf-shell-cache-")); for (const [version, files] of Object.entries(versions)) { - const folder = join(home, ".cache/puppeteer/chrome-headless-shell", version); + const folder = join(home, ".cache/puppeteer/chrome-headless-shell", `linux-${version}`); for (const [path, body] of Object.entries(files)) { const file = join(folder, "chrome-headless-shell-linux64", path); mkdirSync(dirname(file), { recursive: true }); @@ -91,11 +101,12 @@ describe("build-zip chrome-headless-shell staging", () => { } return home; } + const pinned = managedChromeVersion("linux"); - it("stages the whole newest shell folder where the template and the CDK stack point", () => { + it("stages the whole pinned shell folder where the template and the CDK stack point", () => { const home = puppeteerCache({ - "99.0.0.1": { "chrome-headless-shell": "old" }, - "158.0.8127.36": { + "999.0.0.1": { "chrome-headless-shell": "newer" }, + [pinned]: { "chrome-headless-shell": "exe", "icudtl.dat": "icu", "headless_lib_data.pak": "pak", @@ -103,8 +114,19 @@ describe("build-zip chrome-headless-shell staging", () => { }, }); const staging = mkdtempSync(join(tmpdir(), "hf-shell-staging-")); + const checked: string[] = []; try { - expect(stageChromeHeadlessShell(staging, home)).toBe("158.0.8127.36"); + const isPinned = (exe: string) => checked.push(exe) > 0; + expect(stageChromeHeadlessShell(staging, home, isPinned)).toBe(pinned); + expect(checked).toEqual([ + join( + home, + ".cache/puppeteer/chrome-headless-shell", + `linux-${pinned}`, + "chrome-headless-shell-linux64", + "chrome-headless-shell", + ), + ]); const shell = join(staging, BUNDLED_SHELL_DIR); // Without icudtl.dat beside it the shell exits at launch, so the folder ships whole. expect(readFileSync(join(shell, "icudtl.dat"), "utf-8")).toBe("icu"); @@ -121,11 +143,29 @@ describe("build-zip chrome-headless-shell staging", () => { } }); - it("names the install command when the cache is missing", () => { - const home = mkdtempSync(join(tmpdir(), "hf-shell-nocache-")); + it("refuses a cached folder that does not match the pinned digest", () => { + const home = puppeteerCache({ + [pinned]: { "chrome-headless-shell": "tampered" }, + }); + const staging = mkdtempSync(join(tmpdir(), "hf-shell-staging-")); + try { + expect(() => stageChromeHeadlessShell(staging, home)).toThrow(/does not match the pinned/); + expect(existsSync(join(staging, BUNDLED_SHELL_DIR))).toBe(false); + } finally { + rmSync(home, { recursive: true, force: true }); + rmSync(staging, { recursive: true, force: true }); + } + }); + + it("names the pinned install command when only another version is cached", () => { + const home = puppeteerCache({ + "999.0.0.1": { "chrome-headless-shell": "newer" }, + }); try { expect(() => stageChromeHeadlessShell(home, home)).toThrow( - /@puppeteer\/browsers install chrome-headless-shell@/, + new RegExp( + `@puppeteer/browsers install chrome-headless-shell@${pinned.replaceAll(".", "\\.")} `, + ), ); } finally { rmSync(home, { recursive: true, force: true }); diff --git a/packages/aws-lambda/scripts/build-zip.ts b/packages/aws-lambda/scripts/build-zip.ts index e059ae3748..1807857704 100644 --- a/packages/aws-lambda/scripts/build-zip.ts +++ b/packages/aws-lambda/scripts/build-zip.ts @@ -149,7 +149,6 @@ async function main(): Promise { // 5. Compute the unzipped size BEFORE zipping so we fail loud when over budget. const unzippedBytes = directorySizeBytes(stagingDir); - console.log(`[build-zip] unzipped staging size: ${formatBytes(unzippedBytes)}`); if (unzippedBytes > opts.maxUnzippedBytes) { throw new Error( `[build-zip] unzipped bundle ${formatBytes(unzippedBytes)} exceeds limit ${formatBytes( @@ -158,6 +157,10 @@ async function main(): Promise { `Switch --source to the lighter option, or move Chrome to a Lambda Layer.`, ); } + console.log( + `[build-zip] unzipped staging size: ${formatBytes(unzippedBytes)}, ` + + `${formatBytes(opts.maxUnzippedBytes - unzippedBytes)} under the limit`, + ); // 6. Build the ZIP. const zipPath = join(distDir, "handler.zip"); diff --git a/packages/aws-lambda/scripts/stage-arm64-image.test.ts b/packages/aws-lambda/scripts/stage-arm64-image.test.ts index 5159b398a6..43d46a2ff5 100644 --- a/packages/aws-lambda/scripts/stage-arm64-image.test.ts +++ b/packages/aws-lambda/scripts/stage-arm64-image.test.ts @@ -3,6 +3,7 @@ import { spawnSync } from "node:child_process"; import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; +import { brotliCompressSync } from "node:zlib"; import { BUNDLED_SHELL_DIR } from "../src/chromium.js"; import { stageArm64Image } from "./stage-arm64-image.js"; @@ -36,6 +37,16 @@ function pack(cmd: string, args: string[], cwd: string): void { expect(spawnSync(cmd, args, { cwd }).status).toBe(0); } +// @sparticuz/chromium's fonts.tar.br: fonts.conf beside a fonts/ folder. +function sparticuzFonts(): Buffer { + const dir = tree({ + "fonts.conf": "", + "fonts/Open_Sans/OpenSans-Regular.ttf": "ttf", + }); + pack("tar", ["-cf", "fonts.tar", "fonts.conf", "fonts"], dir); + return brotliCompressSync(readFileSync(join(dir, "fonts.tar"))); +} + function fixture(opts: { ffmpegMachine?: number; zipExtra?: Record } = {}) { const zipDir = tree({ "handler.mjs": "export const handler = () => {};", @@ -43,6 +54,7 @@ function fixture(opts: { ffmpegMachine?: number; zipExtra?: Record { - it("swaps the handler ZIP's x86 ffmpeg for the arm64 one and drops its x86 Chrome", async () => { + it("swaps the handler ZIP's x86 ffmpeg for the arm64 one and drops its x86 Chrome, not its fonts", async () => { const task = await stageArm64Image(fixture()); expect(readFileSync(join(task, "bin/ffmpeg")).readUInt16LE(18)).toBe(AARCH64); expect(readFileSync(join(task, "bin/ffprobe")).readUInt16LE(18)).toBe(AARCH64); expect(readFileSync(join(task, "lib/libgnutls.so.30"), "utf-8")).toBe("gnutls"); // The image installs its own chrome-headless-shell; the ZIP's x86 Chrome cannot run on arm64. expect(existsSync(join(task, "node_modules/@sparticuz/chromium/bin"))).toBe(false); + // Its fonts stay, where image/Dockerfile's FONTCONFIG_PATH points. + expect(readFileSync(join(task, "fonts/fonts.conf"), "utf-8")).toBe(""); + expect(readFileSync(join(task, "fonts/fonts/Open_Sans/OpenSans-Regular.ttf"), "utf-8")).toBe( + "ttf", + ); expect(readFileSync(join(task, "handler.mjs"), "utf-8")).toContain("handler"); }); diff --git a/packages/aws-lambda/scripts/stage-arm64-image.ts b/packages/aws-lambda/scripts/stage-arm64-image.ts index 81eb50f006..db611dab8c 100644 --- a/packages/aws-lambda/scripts/stage-arm64-image.ts +++ b/packages/aws-lambda/scripts/stage-arm64-image.ts @@ -1,12 +1,22 @@ #!/usr/bin/env bun // Stages /var/task for the arm64 video function image: the handler ZIP with its x86 ffmpeg swapped for the -// native ffmpeg tree (image/Dockerfile's artifact) and its x86 sparticuz Chrome dropped. The image installs -// its own chrome-headless-shell (image/Dockerfile's video target). +// native ffmpeg tree (image/Dockerfile's artifact) and its x86 sparticuz Chrome dropped, keeping sparticuz's fonts. +// The image installs its own chrome-headless-shell (image/Dockerfile's video target). // bun packages/aws-lambda/scripts/stage-arm64-image.ts --ffmpeg= [--zip=..] [--out=..] import { spawnSync } from "node:child_process"; -import { closeSync, existsSync, mkdirSync, openSync, readSync, rmSync } from "node:fs"; +import { + closeSync, + existsSync, + mkdirSync, + openSync, + readFileSync, + readSync, + rmSync, + writeFileSync, +} from "node:fs"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; +import { brotliDecompressSync } from "node:zlib"; import { BUNDLED_SHELL_DIR } from "../src/chromium.js"; export interface StageOptions { @@ -50,8 +60,15 @@ export async function stageArm64Image(opts: StageOptions): Promise { rmSync(join(task, "bin", "ffprobe")); run("tar", ["-xzf", opts.ffmpegTarball, "-C", task, "--strip-components=1"]); for (const bin of ["ffmpeg", "ffprobe"]) assertArm64Elf(join(task, "bin", bin)); + const sparticuzBin = join(task, "node_modules", "@sparticuz", "chromium", "bin"); + // Its fonts and fonts.conf, unpacked where @sparticuz/chromium unpacks them under /tmp, so text falls back alike. + const fontsTar = join(opts.outDir, "fonts.tar"); + writeFileSync(fontsTar, brotliDecompressSync(readFileSync(join(sparticuzBin, "fonts.tar.br")))); + mkdirSync(join(task, "fonts")); + run("tar", ["-xf", fontsTar, "-C", join(task, "fonts")]); + rmSync(fontsTar); // x86 Chrome cannot run here; the image's HYPERFRAMES_LAMBDA_CHROME_SOURCE never loads it. - rmSync(join(task, "node_modules", "@sparticuz", "chromium", "bin"), { recursive: true }); + rmSync(sparticuzBin, { recursive: true }); return task; } diff --git a/packages/aws-lambda/src/cdk/HyperframesRenderStack.ts b/packages/aws-lambda/src/cdk/HyperframesRenderStack.ts index fc91e969df..76dbcc36d1 100644 --- a/packages/aws-lambda/src/cdk/HyperframesRenderStack.ts +++ b/packages/aws-lambda/src/cdk/HyperframesRenderStack.ts @@ -43,7 +43,7 @@ export interface HyperframesRenderStackProps { lambdaTimeoutSec?: number; /** Lambda reserved concurrency cap. `undefined` = unreserved (account default). */ reservedConcurrency?: number; - /** Which Chrome runtime was bundled into the handler ZIP. Default `"sparticuz"`. */ + /** Which Chrome runtime was bundled into the handler ZIP. Default `"sparticuz"`; a shell ZIP is over Lambda's limit. */ chromeSource?: "sparticuz" | "chrome-headless-shell"; /** Threshold for the runaway-invocations alarm. Default 1000 invocations/hour. */ chunkInvocationAlarmThreshold?: number; diff --git a/packages/cli/src/commands/lambda/deploy.test.ts b/packages/cli/src/commands/lambda/deploy.test.ts index 37127eba16..e6d789a513 100644 --- a/packages/cli/src/commands/lambda/deploy.test.ts +++ b/packages/cli/src/commands/lambda/deploy.test.ts @@ -54,13 +54,6 @@ describe("runDeploy", () => { expect(samDeploy).not.toHaveBeenCalled(); }); - it("outside a checkout, refuses a Chrome source the prebuilt handler does not bundle", async () => { - await expect( - runDeploy({ videoArch: "x86_64", chromeSource: "chrome-headless-shell" }), - ).rejects.toThrow(/needs a hyperframes checkout/); - expect(samDeploy).not.toHaveBeenCalled(); - }); - it("with --video-arch x86_64, deploys the checkout's template with no video function", async () => { dir = mkdtempSync(join(tmpdir(), "hf-deploy-checkout-")); const samDir = join(dir, "examples", "aws-lambda"); @@ -144,13 +137,14 @@ describe("runDeploy", () => { }); }); - it.each([undefined, "arm64" as const])( - "refuses arm64 (--video-arch %s) with a handler ZIP the video image is not built from, naming x86_64", + it.each([undefined, "arm64" as const, "x86_64" as const])( + "refuses chrome-headless-shell (--video-arch %s) before building, since it cannot fit a ZIP function", async (videoArch) => { await expect(runDeploy({ videoArch, chromeSource: "chrome-headless-shell" })).rejects.toThrow( - /built from the sparticuz handler ZIP[\s\S]*--video-arch x86_64/, + /does not fit a Lambda ZIP function[\s\S]*arm64 video image/, ); expect(pushVideoImage).not.toHaveBeenCalled(); + expect(samDeploy).not.toHaveBeenCalled(); }, ); }); diff --git a/packages/cli/src/commands/lambda/deploy.ts b/packages/cli/src/commands/lambda/deploy.ts index 363cde11f9..297ff86ad6 100644 --- a/packages/cli/src/commands/lambda/deploy.ts +++ b/packages/cli/src/commands/lambda/deploy.ts @@ -49,10 +49,10 @@ export async function runDeploy(args: Partial = {}): Promise { warm: args.warm ?? true, }; - if (resolved.videoArch === "arm64" && resolved.chromeSource !== "sparticuz") { + if (resolved.chromeSource !== "sparticuz") { throw new Error( - "[lambda deploy] The arm64 video image brings its own chrome-headless-shell and is built " + - "from the sparticuz handler ZIP. Pass --video-arch x86_64 to deploy with --chrome-source chrome-headless-shell.", + "[lambda deploy] chrome-headless-shell does not fit a Lambda ZIP function, so the handler ZIP " + + "ships @sparticuz/chromium. The arm64 video image (--video-arch arm64, the default) runs chrome-headless-shell.", ); } const root = repoRoot(); @@ -147,14 +147,7 @@ function warmVideoFunction(functionName: string | undefined, args: DeployArgs): /** Checkout: build the ZIP from source. Otherwise: stage the adapter's pinned prebuilt ZIP. */ async function prepareSamDir(root: string | undefined, args: DeployArgs): Promise { - if (!root) { - if (args.chromeSource !== "sparticuz") { - throw new Error( - "[lambda deploy] The prebuilt handler bundles sparticuz Chrome; --chrome-source=chrome-headless-shell needs a hyperframes checkout.", - ); - } - return stagePrebuiltHandler(); - } + if (!root) return stagePrebuiltHandler(); const samDir = dirname(locateSamTemplate(root)); if (!args.skipBuild) { console.log(c.dim("→ Building handler ZIP")); diff --git a/scripts/ci/release-only-diff.test.mjs b/scripts/ci/release-only-diff.test.mjs index 76f29c6269..2c659e4e63 100644 --- a/scripts/ci/release-only-diff.test.mjs +++ b/scripts/ci/release-only-diff.test.mjs @@ -236,6 +236,7 @@ test("every heavy workflow asks the release-only action, and the decision is use "player-perf", "player-safari", "lambda-arm64-image", + "lambda-handler-zip", ]) assert.match(read(name), /uses: \.\/\.github\/actions\/release-only-diff/, name); const ci = read("ci");