🌳
pt0/deployF/dockerF/guardBundleValidAwaitAI.mts
1import fs from 'fs'
4// esbuild emits `await init_X()` inside non-async __esm factories when a TLA-containing circular
5// dep enters the graph — bundle then crashes at boot with "SyntaxError: Unexpected reserved word".
6// Fast-fail at build time instead of shipping a broken bundle.
7export const guardBundleValidAwait = ({bundleAbsPath}: {bundleAbsPath: string}) => {
8 const lines = fs.readFileSync(bundleAbsPath, 'utf8').split('\n')
9 const bad: string[] = []
10 let curFactory: string | null = null, curSync = false
11 for (const line of lines) {
12 const m = line.match(/^ (async )?"([^"]+)"\(\) \{/)
13 if (m) {
14 curFactory = m[2]
15 curSync = !m[1]
16 }
17 if (curSync && /^ await init_/.test(line)) {
18 bad.push(`${curFactory}: ${line.trim()}`)
19 }
20 }
21 if (bad.length) {
22 throPtErr('esbuild emitted await-in-sync-fn (TLA cycle pulled into bundle)', {bad: bad.slice(0, 10), bundleAbsPath})
23 }