import { existsSync, readFileSync } from "node:fs"; export async function waitForFile(path, child, timeoutMs, pollMs = 50) { const deadline = Date.now() + timeoutMs; while (!existsSync(path)) { if (child.exitCode !== null) { throw new Error(`endpoint exited before publishing ${path} (${child.exitCode}); ${diagnostics(child)}`); } if (Date.now() > deadline) { throw new Error(`timed out after ${timeoutMs}ms waiting for ${path}; ${diagnostics(child)}`); } await new Promise((resolve) => setTimeout(resolve, pollMs)); } return readFileSync(path, "utf8").trim(); } function diagnostics(child) { return `stdout=${JSON.stringify(child.diagnostics?.stdout ?? "")} stderr=${JSON.stringify(child.diagnostics?.stderr ?? "")}`; }