()
| 17 | } |
| 18 | |
| 19 | async function discoverBackendPort(): Promise<string> { |
| 20 | if (cachedApiUrl) return cachedApiUrl; |
| 21 | if (resolvingApiUrl) return resolvingApiUrl; |
| 22 | |
| 23 | resolvingApiUrl = (async () => { |
| 24 | const preferred = [envApiUrl, DEFAULT_API_URL].filter(Boolean) as string[]; |
| 25 | for (const candidate of preferred) { |
| 26 | const ok = await probeBackendUrl(candidate); |
| 27 | if (ok) { |
| 28 | cachedApiUrl = ok; |
| 29 | return ok; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | for (let i = 0; i < MAX_PORT_TRIES; i++) { |
| 34 | const port = BACKEND_BASE_PORT + i; |
| 35 | const testUrl = `http://127.0.0.1:${port}/api`; |
| 36 | const ok = await probeBackendUrl(testUrl); |
| 37 | if (ok) { |
| 38 | cachedApiUrl = ok; |
| 39 | return ok; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | throw new Error(`Cannot find backend server on ports ${BACKEND_BASE_PORT}-${BACKEND_BASE_PORT + MAX_PORT_TRIES - 1}`); |
| 44 | })(); |
| 45 | |
| 46 | try { |
| 47 | return await resolvingApiUrl; |
| 48 | } finally { |
| 49 | resolvingApiUrl = null; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | const envApiUrl = process.env.NEXT_PUBLIC_API_URL; |
| 54 |
no test coverage detected