process-readiness.mjs

766 B · raw

1import { existsSync, readFileSync } from "node:fs";23export async function waitForFile(path, child, timeoutMs, pollMs = 50) {4  const deadline = Date.now() + timeoutMs;5  while (!existsSync(path)) {6    if (child.exitCode !== null) {7      throw new Error(`endpoint exited before publishing ${path} (${child.exitCode}); ${diagnostics(child)}`);8    }9    if (Date.now() > deadline) {10      throw new Error(`timed out after ${timeoutMs}ms waiting for ${path}; ${diagnostics(child)}`);11    }12    await new Promise((resolve) => setTimeout(resolve, pollMs));13  }14  return readFileSync(path, "utf8").trim();15}1617function diagnostics(child) {18  return `stdout=${JSON.stringify(child.diagnostics?.stdout ?? "")} stderr=${JSON.stringify(child.diagnostics?.stderr ?? "")}`;19}