( options: StartDevServerOptions )
| 436 | * the app can honor, and returns the host port for the dev proxy to target. |
| 437 | */ |
| 438 | export async function startDevServer( |
| 439 | options: StartDevServerOptions |
| 440 | ): Promise<StartDevServerResult> { |
| 441 | // Reuse a live container for this service instead of rebuilding/running on |
| 442 | // every request. The dev server calls `startDevServer` per request; a |
| 443 | // container is a persistent server, so we hand back the running one. |
| 444 | const reuseKey = containerReuseKey(options); |
| 445 | const existing = runningContainers.get(reuseKey); |
| 446 | if (existing && existing.isRunning()) { |
| 447 | return existing.result; |
| 448 | } |
| 449 | if (existing) { |
| 450 | // Stale (exited) entry — clear it before starting a replacement. |
| 451 | runningContainers.delete(reuseKey); |
| 452 | } |
| 453 | |
| 454 | // Coalesce concurrent cold starts: if a start for this service is already in |
| 455 | // flight, wait on it rather than spawning a second container. |
| 456 | const inFlight = pendingContainers.get(reuseKey); |
| 457 | if (inFlight) { |
| 458 | return inFlight; |
| 459 | } |
| 460 | |
| 461 | const startPromise = startContainer(options, reuseKey).finally(() => { |
| 462 | pendingContainers.delete(reuseKey); |
| 463 | }); |
| 464 | pendingContainers.set(reuseKey, startPromise); |
| 465 | return startPromise; |
| 466 | } |
| 467 | |
| 468 | async function startContainer( |
| 469 | options: StartDevServerOptions, |
no test coverage detected