(origin: string, token: string)
| 78 | } |
| 79 | |
| 80 | async function fetchConnections(origin: string, token: string): Promise<ConnectionInfo[]> { |
| 81 | const controller = new AbortController(); |
| 82 | const timeout = setTimeout(() => { |
| 83 | controller.abort(); |
| 84 | }, FETCH_TIMEOUT_MS); |
| 85 | try { |
| 86 | const res = await fetch(`${origin}/api/v1/connections`, { |
| 87 | headers: authHeaders(token), |
| 88 | signal: controller.signal, |
| 89 | }); |
| 90 | if (!res.ok) { |
| 91 | throw new Error(`Failed to list clients: HTTP ${String(res.status)} from ${origin}.`); |
| 92 | } |
| 93 | const body = (await res.json()) as ConnectionsEnvelope; |
| 94 | if (body.code !== 0) { |
| 95 | throw new Error(`Failed to list clients: ${body.msg}`); |
| 96 | } |
| 97 | return body.data?.connections ?? []; |
| 98 | } catch (error) { |
| 99 | if (error instanceof Error && error.name === 'AbortError') { |
| 100 | throw new Error(`Timed out listing clients from ${origin}.`); |
| 101 | } |
| 102 | throw error; |
| 103 | } finally { |
| 104 | clearTimeout(timeout); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | function formatTable(connections: ConnectionInfo[]): string { |
| 109 | if (connections.length === 0) { |
no test coverage detected