()
| 107 | } |
| 108 | |
| 109 | async function main(): Promise<void> { |
| 110 | const opts = parseArgs(process.argv); |
| 111 | const start = Date.now(); |
| 112 | |
| 113 | rmSync(distDir, { recursive: true, force: true }); |
| 114 | mkdirSync(distDir, { recursive: true }); |
| 115 | |
| 116 | const stagingDir = join(distDir, "staging"); |
| 117 | mkdirSync(stagingDir, { recursive: true }); |
| 118 | |
| 119 | console.log(`[build-zip] source=${opts.source}`); |
| 120 | |
| 121 | // 1. Bundle the handler. |
| 122 | await bundleHandler(stagingDir); |
| 123 | |
| 124 | // 2. Stage runtime modules (puppeteer-core + @sparticuz/chromium or the |
| 125 | // fallback chrome-headless-shell tar). |
| 126 | stageRuntimeModules(stagingDir, opts.source); |
| 127 | |
| 128 | // 3. Stage the ffmpeg binary. |
| 129 | stageFfmpeg(stagingDir); |
| 130 | |
| 131 | // 3b. Stage the hyperframe runtime manifest + IIFE as siblings of |
| 132 | // handler.mjs. The producer's `hyperframeRuntimeLoader` checks |
| 133 | // SIBLING_MANIFEST_PATH first, so dropping the manifest alongside |
| 134 | // the bundled handler at /var/task/hyperframe.manifest.json lets |
| 135 | // renderChunk find it without needing PRODUCER_HYPERFRAME_MANIFEST_PATH. |
| 136 | stageHyperframeRuntime(stagingDir); |
| 137 | |
| 138 | // 4. If we're on the chrome-headless-shell fallback, stage that binary. |
| 139 | if (opts.source === "chrome-headless-shell") { |
| 140 | stageChromeHeadlessShell(stagingDir); |
| 141 | } |
| 142 | |
| 143 | // 5. Compute the unzipped size BEFORE zipping so we fail loud when over budget. |
| 144 | const unzippedBytes = directorySizeBytes(stagingDir); |
| 145 | console.log(`[build-zip] unzipped staging size: ${formatBytes(unzippedBytes)}`); |
| 146 | if (unzippedBytes > opts.maxUnzippedBytes) { |
| 147 | throw new Error( |
| 148 | `[build-zip] unzipped bundle ${formatBytes(unzippedBytes)} exceeds limit ${formatBytes( |
| 149 | opts.maxUnzippedBytes, |
| 150 | )} (Lambda ZIP ceiling: 250 MiB unzipped). ` + |
| 151 | `Switch --source to the lighter option, or move Chrome to a Lambda Layer.`, |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | // 6. Build the ZIP. |
| 156 | const zipPath = join(distDir, "handler.zip"); |
| 157 | zipDirectory(stagingDir, zipPath); |
| 158 | const zippedBytes = statSync(zipPath).size; |
| 159 | console.log(`[build-zip] zip size: ${formatBytes(zippedBytes)} → ${zipPath}`); |
| 160 | if (zippedBytes > opts.maxZippedBytes) { |
| 161 | throw new Error( |
| 162 | `[build-zip] zip ${formatBytes(zippedBytes)} exceeds ZIP size limit ${formatBytes( |
| 163 | opts.maxZippedBytes, |
| 164 | )}.`, |
| 165 | ); |
| 166 | } |
no test coverage detected