also looking at this
Promote main to production: signed-out chrome and the subscriber tables
#17
54 files
+4252
-1353
Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.
| @@ -70,6 +70,48 @@ async function waitFor<T>(read: () => Promise<T> | T, timeout = 12_000): Promise | ||
| 70 | 70 | return value |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | /** | |
| 74 | * Wait for Chrome to publish its DevTools port, and say something useful when it | |
| 75 | * does not. | |
| 76 | * | |
| 77 | * This used to be a plain `waitFor(() => existsSync(portFile), 10_000)` against a | |
| 78 | * Chrome spawned with stderr discarded, so every launch failure — missing shared | |
| 79 | * library, unwritable profile, sandbox refusal, OOM — arrived as the same four | |
| 80 | * words, `Timed out after 10000ms`, with the one line that would have explained it | |
| 81 | * thrown away. It failed twice in three CI runs and was undiagnosable both times. | |
| 82 | * | |
| 83 | * Two changes: the process is polled as well as the file, so a Chrome that exits | |
| 84 | * reports immediately instead of burning the whole timeout first; and whatever it | |
| 85 | * wrote to stderr is attached to the error. | |
| 86 | */ | |
| 87 | async function waitForDevToolsPort( | |
| 88 | portFile: string, | |
| 89 | chrome: { exitCode: number | null }, | |
| 90 | stderr: () => string, | |
| 91 | chromeBinary: string, | |
| 92 | timeout = 30_000, | |
| 93 | ): Promise<void> { | |
| 94 | const started = Date.now() | |
| 95 | const describe = (headline: string): string => { | |
| 96 | const tail = stderr().trim().split('\n').slice(-12).join('\n') | |
| 97 | return [ | |
| 98 | headline, | |
| 99 | ` binary: ${chromeBinary}`, | |
| 100 | ` profile: ${portFile.replace(/\/DevToolsActivePort$/, '')}`, | |
| 101 | tail ? ` stderr:\n${tail.replace(/^/gm, ' ')}` : ' stderr: (nothing was written)', | |
| 102 | ].join('\n') | |
| 103 | } | |
| 104 | ||
| 105 | while (!existsSync(portFile)) { | |
| 106 | // A dead Chrome will never write the file, so there is nothing to wait for. | |
| 107 | if (chrome.exitCode !== null) | |
| 108 | throw new Error(describe(`Chrome exited with code ${chrome.exitCode} before opening a debugging port.`)) | |
| 109 | if (Date.now() - started >= timeout) | |
| 110 | throw new Error(describe(`Chrome did not open a debugging port within ${timeout}ms.`)) | |
| 111 | await Bun.sleep(100) | |
| 112 | } | |
| 113 | } | |
| 114 | ||
| 73 | 115 | class Cdp { |
| 74 | 116 | private id = 0 |
| 75 | 117 | private pending = new Map<number, Pending>() |
| @@ -237,8 +279,9 @@ async function main(): Promise<void> { | ||
| 237 | 279 | }, |
| 238 | 280 | }) |
| 239 | 281 | const profile = mkdtempSync(join(tmpdir(), 'hq-visual-')) |
| 282 | const chromeBinary = chromePath() | |
| 240 | 283 | const chrome = Bun.spawn([ |
| 241 | chromePath(), | |
| 284 | chromeBinary, | |
| 242 | 285 | '--headless=new', |
| 243 | 286 | '--remote-debugging-port=0', |
| 244 | 287 | `--user-data-dir=${profile}`, |
| @@ -255,13 +298,28 @@ async function main(): Promise<void> { | ||
| 255 | 298 | '--disable-dev-shm-usage', |
| 256 | 299 | '--disable-gpu', |
| 257 | 300 | 'about:blank', |
| 258 | ], { stdout: 'ignore', stderr: 'ignore' }) | |
| 301 | ], { stdout: 'ignore', stderr: 'pipe' }) | |
| 302 | ||
| 303 | // Drained continuously rather than read on failure: a chatty Chrome can fill the | |
| 304 | // pipe buffer and block on its own output, which would look exactly like the | |
| 305 | // startup hang this is here to explain. | |
| 306 | let chromeStderr = '' | |
| 307 | const drainingStderr = (async () => { | |
| 308 | try { | |
| 309 | const decoder = new TextDecoder() | |
| 310 | for await (const chunk of chrome.stderr as ReadableStream<Uint8Array>) | |
| 311 | chromeStderr += decoder.decode(chunk, { stream: true }) | |
| 312 | } | |
| 313 | catch { | |
| 314 | // The process was killed mid-read; whatever arrived is still worth having. | |
| 315 | } | |
| 316 | })() | |
| 259 | 317 | |
| 260 | 318 | const failures: string[] = [] |
| 261 | 319 | let screenshots = 0 |
| 262 | 320 | try { |
| 263 | 321 | const portFile = join(profile, 'DevToolsActivePort') |
| 264 | await waitFor(() => existsSync(portFile), 10_000) | |
| 322 | await waitForDevToolsPort(portFile, chrome, () => chromeStderr, chromeBinary) | |
| 265 | 323 | const port = Number(readFileSync(portFile, 'utf8').split('\n')[0]) |
| 266 | 324 | const targets = await waitFor(async () => { |
| 267 | 325 | const items = await fetch(`http://127.0.0.1:${port}/json/list`).then(response => response.json()).catch(() => []) |
| @@ -321,6 +379,7 @@ async function main(): Promise<void> { | ||
| 321 | 379 | finally { |
| 322 | 380 | chrome.kill() |
| 323 | 381 | await chrome.exited |
| 382 | await drainingStderr | |
| 324 | 383 | server.stop(true) |
| 325 | 384 | rmSync(profile, { force: true, recursive: true }) |
| 326 | 385 | } |