mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 08:32:45 +00:00
build-zip accepted whatever ffmpeg-static's install script downloaded as long as it had a Linux x86-64 ELF header. It now compares the binary's sha256 with the one pinned for the installed ffmpeg-static version and stops before the ZIP is built when they differ, or when a version has no pinned digest.
344 lines
14 KiB
TypeScript
344 lines
14 KiB
TypeScript
#!/usr/bin/env tsx
|
|
/**
|
|
* Build the AWS Lambda deployment ZIP.
|
|
*
|
|
* Pack layout (paths inside the ZIP are relative to Lambda's
|
|
* `/var/task/`):
|
|
*
|
|
* handler.mjs — bundled entry, set as Lambda's Handler
|
|
* handler.mjs.map — sourcemap (debugging aid; small)
|
|
* bin/ffmpeg — ffmpeg-static binary
|
|
* chrome-headless-shell-linux64/
|
|
* — fallback Chrome, the whole shell folder (only
|
|
* with --source=chrome-headless-shell)
|
|
* node_modules/@sparticuz/chromium/
|
|
* — primary Chrome (lives under node_modules so
|
|
* runtime `import("@sparticuz/chromium")`
|
|
* resolves; the package's own tarball stays
|
|
* inside).
|
|
*
|
|
* The handler bundle (esbuild) externalises modules whose binary assets
|
|
* must be present at runtime — `@sparticuz/chromium` for its bin tarball,
|
|
* `puppeteer-core` because Lambda runtime resolves it via Node module
|
|
* resolution from `node_modules/`. Everything else is inlined for cold
|
|
* start speed.
|
|
*
|
|
* Run:
|
|
* bun run --cwd packages/aws-lambda build:zip
|
|
* bun run --cwd packages/aws-lambda build:zip -- --source=chrome-headless-shell
|
|
*
|
|
* Outputs the resolved ZIP path + size to stdout and writes a sidecar
|
|
* JSON (`dist/handler.zip.manifest.json`) describing the contents.
|
|
*/
|
|
|
|
import { spawnSync } from "node:child_process";
|
|
import {
|
|
copyFileSync,
|
|
cpSync,
|
|
existsSync,
|
|
mkdirSync,
|
|
readdirSync,
|
|
rmSync,
|
|
statSync,
|
|
writeFileSync,
|
|
} from "node:fs";
|
|
import { dirname, join, resolve } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import * as esbuild from "esbuild";
|
|
import { BUNDLED_SHELL_DIR } from "../src/chromium.js";
|
|
import { stageChromeHeadlessShell } from "./_chromeHeadlessShell.js";
|
|
import { stageFfmpeg } from "./_ffmpeg.js";
|
|
import { formatBytes } from "./_formatBytes.js";
|
|
import { HANDLER_BANNER } from "./_handlerBanner.js";
|
|
import { RUNTIME_LOCK_DIR, assertRuntimeLockCurrent } from "./runtimeLock.js";
|
|
|
|
const scriptDir = dirname(fileURLToPath(import.meta.url));
|
|
const packageRoot = resolve(scriptDir, "..");
|
|
const monorepoRoot = resolve(packageRoot, "../..");
|
|
const distDir = join(packageRoot, "dist");
|
|
|
|
interface BuildOptions {
|
|
source: "sparticuz" | "chrome-headless-shell";
|
|
/** Hard upper bound on the unzipped bundle size in bytes (Lambda limit is 250 MiB). */
|
|
maxUnzippedBytes: number;
|
|
/** Hard upper bound on the ZIP file size in bytes. */
|
|
maxZippedBytes: number;
|
|
}
|
|
|
|
const DEFAULT_OPTIONS: BuildOptions = {
|
|
source: "sparticuz",
|
|
// Lambda's hard ceiling for ZIP-deployed functions is 250 MiB unzipped
|
|
// (AWS docs label it "250 MB" but the 262144000-byte value is 250
|
|
// binary mebibytes). We gate at 248 MiB to keep ~2 MiB of headroom —
|
|
// the sparticuz Chrome (~70 MiB) + ffmpeg (~80 MiB) + ffprobe (~62
|
|
// MiB) + bundled Node deps put us close to the ceiling. Chrome itself
|
|
// decompresses into Lambda's `/tmp` at cold start, which has its own
|
|
// 10 GiB budget, so the unzipped /var/task footprint above is what
|
|
// actually competes with Lambda's 250 MiB limit.
|
|
maxUnzippedBytes: 248 * 1024 * 1024,
|
|
// Lambda's only zipped-size cap is for direct console/CLI uploads (50
|
|
// MiB); S3-deployed functions are bounded by the unzipped ceiling. We
|
|
// gate at 150 MiB to flag a sudden bundle-size regression without
|
|
// false-failing on the natural ~100 MiB sparticuz + ffmpeg payload.
|
|
maxZippedBytes: 150 * 1024 * 1024,
|
|
};
|
|
|
|
function parseArgs(argv: string[]): BuildOptions {
|
|
const opts = { ...DEFAULT_OPTIONS };
|
|
for (const arg of argv.slice(2)) {
|
|
if (arg.startsWith("--source=")) {
|
|
const v = arg.slice("--source=".length);
|
|
if (v !== "sparticuz" && v !== "chrome-headless-shell") {
|
|
throw new Error(`--source must be 'sparticuz' or 'chrome-headless-shell' (got ${v})`);
|
|
}
|
|
opts.source = v;
|
|
} else if (arg.startsWith("--max-unzipped=")) {
|
|
opts.maxUnzippedBytes = Number.parseInt(arg.slice("--max-unzipped=".length), 10);
|
|
} else if (arg.startsWith("--max-zipped=")) {
|
|
opts.maxZippedBytes = Number.parseInt(arg.slice("--max-zipped=".length), 10);
|
|
} else if (arg === "--help") {
|
|
console.log(
|
|
"Usage: tsx build-zip.ts [--source=sparticuz|chrome-headless-shell]\n" +
|
|
" [--max-unzipped=<bytes>] [--max-zipped=<bytes>]",
|
|
);
|
|
process.exit(0);
|
|
} else {
|
|
throw new Error(`Unknown flag: ${arg}`);
|
|
}
|
|
}
|
|
return opts;
|
|
}
|
|
|
|
async function main(): Promise<void> {
|
|
const opts = parseArgs(process.argv);
|
|
const start = Date.now();
|
|
|
|
// Leave the package build in dist/: the release job zips after `bun run build`.
|
|
const stagingDir = join(distDir, "staging");
|
|
rmSync(stagingDir, { recursive: true, force: true });
|
|
rmSync(join(distDir, "handler.zip"), { force: true });
|
|
mkdirSync(stagingDir, { recursive: true });
|
|
|
|
console.log(`[build-zip] source=${opts.source}`);
|
|
|
|
// 1. Bundle the handler.
|
|
await bundleHandler(stagingDir);
|
|
|
|
// 2. Stage runtime modules (puppeteer-core + @sparticuz/chromium or the
|
|
// fallback chrome-headless-shell tar).
|
|
stageRuntimeModules(stagingDir, opts.source);
|
|
|
|
// 3. Stage the ffmpeg binary.
|
|
stageFfmpeg(stagingDir, resolveModuleDir("ffmpeg-static"), resolveModuleDir("ffprobe-static"));
|
|
|
|
// 3b. Stage the hyperframe runtime manifest + IIFE as siblings of
|
|
// handler.mjs. The producer's `hyperframeRuntimeLoader` checks
|
|
// SIBLING_MANIFEST_PATH first, so dropping the manifest alongside
|
|
// the bundled handler at /var/task/hyperframe.manifest.json lets
|
|
// renderChunk find it without needing PRODUCER_HYPERFRAME_MANIFEST_PATH.
|
|
stageHyperframeRuntime(stagingDir);
|
|
|
|
// 4. If we're on the chrome-headless-shell fallback, stage that binary.
|
|
if (opts.source === "chrome-headless-shell") {
|
|
const version = stageChromeHeadlessShell(stagingDir);
|
|
console.log(`[build-zip] staged chrome-headless-shell (${version}) → ${BUNDLED_SHELL_DIR}/`);
|
|
}
|
|
|
|
// 5. Compute the unzipped size BEFORE zipping so we fail loud when over budget.
|
|
const unzippedBytes = directorySizeBytes(stagingDir);
|
|
if (unzippedBytes > opts.maxUnzippedBytes) {
|
|
throw new Error(
|
|
`[build-zip] unzipped bundle ${formatBytes(unzippedBytes)} exceeds limit ${formatBytes(
|
|
opts.maxUnzippedBytes,
|
|
)} (Lambda ZIP ceiling: 250 MiB unzipped). ` +
|
|
`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");
|
|
zipDirectory(stagingDir, zipPath);
|
|
const zippedBytes = statSync(zipPath).size;
|
|
console.log(`[build-zip] zip size: ${formatBytes(zippedBytes)} → ${zipPath}`);
|
|
if (zippedBytes > opts.maxZippedBytes) {
|
|
throw new Error(
|
|
`[build-zip] zip ${formatBytes(zippedBytes)} exceeds ZIP size limit ${formatBytes(
|
|
opts.maxZippedBytes,
|
|
)}.`,
|
|
);
|
|
}
|
|
|
|
// 7. Sidecar manifest.
|
|
const manifest = {
|
|
builtAt: new Date().toISOString(),
|
|
durationMs: Date.now() - start,
|
|
source: opts.source,
|
|
unzippedBytes,
|
|
zippedBytes,
|
|
maxUnzippedBytes: opts.maxUnzippedBytes,
|
|
maxZippedBytes: opts.maxZippedBytes,
|
|
};
|
|
writeFileSync(join(distDir, "handler.zip.manifest.json"), JSON.stringify(manifest, null, 2));
|
|
|
|
// 8. Cleanup staging.
|
|
rmSync(stagingDir, { recursive: true, force: true });
|
|
console.log(`[build-zip] done in ${Date.now() - start}ms`);
|
|
}
|
|
|
|
async function bundleHandler(stagingDir: string): Promise<void> {
|
|
const entry = join(packageRoot, "src/handler.ts");
|
|
const outfile = join(stagingDir, "handler.mjs");
|
|
|
|
const workspaceAliasPlugin: esbuild.Plugin = {
|
|
name: "workspace-alias",
|
|
setup(build) {
|
|
build.onResolve({ filter: /^@hyperframes\/producer\/distributed$/ }, () => ({
|
|
path: resolve(monorepoRoot, "packages/producer/src/distributed.ts"),
|
|
}));
|
|
build.onResolve({ filter: /^@hyperframes\/producer$/ }, () => ({
|
|
path: resolve(monorepoRoot, "packages/producer/src/index.ts"),
|
|
}));
|
|
build.onResolve({ filter: /^@hyperframes\/engine$/ }, () => ({
|
|
path: resolve(monorepoRoot, "packages/engine/src/index.ts"),
|
|
}));
|
|
build.onResolve({ filter: /^@hyperframes\/engine\/alpha-blit$/ }, () => ({
|
|
path: resolve(monorepoRoot, "packages/engine/src/utils/alphaBlit.ts"),
|
|
}));
|
|
build.onResolve({ filter: /^@hyperframes\/engine\/shader-transitions$/ }, () => ({
|
|
path: resolve(monorepoRoot, "packages/engine/src/utils/shaderTransitions.ts"),
|
|
}));
|
|
build.onResolve({ filter: /^@hyperframes\/core$/ }, () => ({
|
|
path: resolve(monorepoRoot, "packages/core/src/index.ts"),
|
|
}));
|
|
build.onResolve({ filter: /^@hyperframes\/core\/lint$/ }, () => ({
|
|
path: resolve(monorepoRoot, "packages/core/src/lint/index.ts"),
|
|
}));
|
|
},
|
|
};
|
|
|
|
await esbuild.build({
|
|
bundle: true,
|
|
platform: "node",
|
|
target: "node22",
|
|
format: "esm",
|
|
// Externalise binary-shipped modules so node module resolution picks
|
|
// them up at runtime. esbuild would otherwise try to inline their
|
|
// postinstall-extracted binaries, which it cannot do.
|
|
external: [
|
|
"@sparticuz/chromium",
|
|
"puppeteer-core",
|
|
"puppeteer",
|
|
// AWS SDK v3 is pre-installed in the Lambda Node 22 runtime; mark
|
|
// external so we don't double-bundle 3+ MiB of SDK.
|
|
"@aws-sdk/client-lambda",
|
|
"@aws-sdk/client-s3",
|
|
],
|
|
plugins: [workspaceAliasPlugin],
|
|
minify: false,
|
|
// sourcemap=false: the ZIP is tight on Lambda's 250 MiB unzipped cap
|
|
// (Chrome ~70 MiB + ffmpeg ~80 MiB + ffprobe ~62 MiB + Node deps). A
|
|
// 4-5 MiB sourcemap puts us over. Re-enable for local debugging by passing
|
|
// --sourcemap; the bundle's stack traces stay readable enough without
|
|
// it because we don't minify.
|
|
sourcemap: false,
|
|
entryPoints: [entry],
|
|
outfile,
|
|
// See HANDLER_BANNER (_handlerBanner.ts) for why the ESM bundle needs the
|
|
// CJS require/__filename/__dirname shims.
|
|
banner: {
|
|
js: HANDLER_BANNER,
|
|
},
|
|
});
|
|
console.log(`[build-zip] bundled handler → ${outfile}`);
|
|
}
|
|
|
|
function stageRuntimeModules(stagingDir: string, source: BuildOptions["source"]): void {
|
|
// This runs in the publish job: node_modules come only from the committed lockfile, with
|
|
// lifecycle scripts off, and in the repo its direct versions must match bun.lock.
|
|
assertRuntimeLockCurrent(source);
|
|
const lockDir = join(RUNTIME_LOCK_DIR, source);
|
|
copyFileSync(join(lockDir, "package.json"), join(stagingDir, "package.json"));
|
|
copyFileSync(join(lockDir, "package-lock.json"), join(stagingDir, "package-lock.json"));
|
|
const result = spawnSync(
|
|
"npm",
|
|
["ci", "--ignore-scripts", "--no-audit", "--no-fund", "--omit=dev", "--omit=optional"],
|
|
{ cwd: stagingDir, stdio: "inherit" },
|
|
);
|
|
if (result.status !== 0) {
|
|
throw new Error(`[build-zip] npm ci into staging failed (status ${result.status})`);
|
|
}
|
|
rmSync(join(stagingDir, "package-lock.json"));
|
|
console.log(`[build-zip] staged node_modules via npm ci from runtime-lock/${source}`);
|
|
}
|
|
|
|
function resolveModuleDir(moduleName: string): string {
|
|
// Walk up from packageRoot to find a matching node_modules entry.
|
|
// Used for ffmpeg-static and ffprobe-static; the @sparticuz/chromium +
|
|
// puppeteer-core paths go through npm install instead.
|
|
let dir = packageRoot;
|
|
for (let i = 0; i < 5; i++) {
|
|
const candidate = join(dir, "node_modules", moduleName);
|
|
if (existsSync(candidate)) return candidate;
|
|
dir = dirname(dir);
|
|
}
|
|
throw new Error(
|
|
`[build-zip] could not resolve ${moduleName} from ${packageRoot} — run 'bun install' first.`,
|
|
);
|
|
}
|
|
|
|
function stageHyperframeRuntime(stagingDir: string): void {
|
|
const coreDist = resolve(monorepoRoot, "packages/core/dist");
|
|
const manifestSrc = join(coreDist, "hyperframe.manifest.json");
|
|
const iifeSrc = join(coreDist, "hyperframe.runtime.iife.js");
|
|
if (!existsSync(manifestSrc) || !existsSync(iifeSrc)) {
|
|
throw new Error(
|
|
`[build-zip] hyperframe runtime artifacts missing under ${coreDist}. ` +
|
|
`Run 'bun run --filter @hyperframes/core build:hyperframes-runtime:modular' first.`,
|
|
);
|
|
}
|
|
cpSync(manifestSrc, join(stagingDir, "hyperframe.manifest.json"));
|
|
cpSync(iifeSrc, join(stagingDir, "hyperframe.runtime.iife.js"));
|
|
console.log(`[build-zip] staged hyperframe.manifest.json + hyperframe.runtime.iife.js`);
|
|
}
|
|
|
|
function zipDirectory(sourceDir: string, zipPath: string): void {
|
|
const result = spawnSync("zip", ["-rq", zipPath, "."], { cwd: sourceDir, stdio: "inherit" });
|
|
if (result.status !== 0) {
|
|
throw new Error(`[build-zip] zip exited with status ${result.status}`);
|
|
}
|
|
}
|
|
|
|
function directorySizeBytes(dir: string): number {
|
|
// Use spawnSync (no shell) instead of execSync so `dir` is passed as
|
|
// an argv element rather than interpolated into a shell command —
|
|
// CodeQL's `js/shell-command-injected-from-environment` rule fires
|
|
// on the latter even with JSON-quoting. `du -sb` is Linux-only;
|
|
// build-zip is CI-side where Linux coreutils is present.
|
|
const result = spawnSync("du", ["-sb", dir], { encoding: "utf-8" });
|
|
if (result.status === 0 && result.stdout) {
|
|
const bytes = Number.parseInt(result.stdout.split(/\s+/)[0] ?? "0", 10);
|
|
if (!Number.isNaN(bytes)) return bytes;
|
|
}
|
|
return walkSize(dir);
|
|
}
|
|
|
|
function walkSize(dir: string): number {
|
|
let total = 0;
|
|
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
const full = join(dir, entry.name);
|
|
if (entry.isDirectory()) total += walkSize(full);
|
|
else if (entry.isFile()) total += statSync(full).size;
|
|
}
|
|
return total;
|
|
}
|
|
|
|
void main().catch((err) => {
|
|
console.error("[build-zip] failed:", err instanceof Error ? err.message : String(err));
|
|
if (err instanceof Error && err.stack) console.error(err.stack);
|
|
process.exit(1);
|
|
});
|