MCPcopy Create free account
hub / github.com/Microck/opencode-studio / discoverBackendPort

Function discoverBackendPort

client-next/src/lib/api.ts:19–51  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

17}
18
19async 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
53const envApiUrl = process.env.NEXT_PUBLIC_API_URL;
54

Callers 2

api.tsFile · 0.85
getApiBaseUrlFunction · 0.85

Calls 1

probeBackendUrlFunction · 0.85

Tested by

no test coverage detected