openpalm 0.9.8 → 0.9.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/lib/admin.ts DELETED
@@ -1,107 +0,0 @@
1
- import { loadAdminToken } from './env.ts';
2
-
3
- export const ADMIN_URL = process.env.OPENPALM_ADMIN_API_URL || 'http://localhost:8100';
4
-
5
- /**
6
- * Returns true if the admin health endpoint is reachable.
7
- */
8
- export async function isAdminReachable(): Promise<boolean> {
9
- try {
10
- const response = await fetch(`${ADMIN_URL}/health`, {
11
- method: 'GET',
12
- signal: AbortSignal.timeout(2000),
13
- });
14
- return response.ok;
15
- } catch {
16
- return false;
17
- }
18
- }
19
-
20
- /**
21
- * @deprecated Use isAdminReachable() instead. Kept for backward compatibility.
22
- */
23
- export const isStackRunning = isAdminReachable;
24
-
25
- /**
26
- * Makes an authenticated request to the admin API.
27
- * Throws if the response is not ok.
28
- */
29
- export async function adminRequest(path: string, init?: RequestInit): Promise<unknown> {
30
- const token = await loadAdminToken();
31
- if (!token) {
32
- throw new Error(
33
- 'No admin token found. Set OPENPALM_ADMIN_TOKEN in your environment or ' +
34
- `configure it in secrets.env via the setup wizard (${ADMIN_URL}/setup).`,
35
- );
36
- }
37
- const response = await fetch(`${ADMIN_URL}${path}`, {
38
- ...init,
39
- headers: {
40
- 'Content-Type': 'application/json',
41
- 'X-Requested-By': 'cli',
42
- 'X-Admin-Token': token,
43
- ...init?.headers,
44
- },
45
- signal: init?.signal ?? AbortSignal.timeout(120_000),
46
- });
47
-
48
- const text = await response.text();
49
- if (!response.ok) {
50
- throw new Error(text || `${response.status} ${response.statusText}`);
51
- }
52
-
53
- if (!text) return { ok: true };
54
- try {
55
- return JSON.parse(text) as unknown;
56
- } catch {
57
- return text;
58
- }
59
- }
60
-
61
- /**
62
- * Try to delegate an operation to the admin API.
63
- * Returns the result if admin is reachable and has a valid token.
64
- * Returns null if admin is unreachable or no token is available.
65
- * Attempts the request directly — no separate health check round-trip.
66
- */
67
- export async function tryAdminRequest(path: string, init?: RequestInit): Promise<unknown | null> {
68
- const token = await loadAdminToken();
69
- if (!token) return null;
70
-
71
- try {
72
- return await adminRequest(path, {
73
- ...init,
74
- signal: init?.signal ?? AbortSignal.timeout(10_000),
75
- });
76
- } catch {
77
- return null;
78
- }
79
- }
80
-
81
- /**
82
- * Waits for the admin health endpoint to become healthy (up to 120s).
83
- */
84
- export async function waitForAdminHealthy(): Promise<void> {
85
- const started = Date.now();
86
- while (Date.now() - started < 120_000) {
87
- if (await isAdminReachable()) {
88
- return;
89
- }
90
- await Bun.sleep(3000);
91
- }
92
- throw new Error('Admin did not become healthy within 120 seconds');
93
- }
94
-
95
- /**
96
- * Extracts service names from an admin containers/list response.
97
- */
98
- export function getServiceNames(status: unknown): string[] {
99
- if (!status || typeof status !== 'object' || !('containers' in status)) {
100
- return [];
101
- }
102
- const containers = (status as { containers?: unknown }).containers;
103
- if (!containers || typeof containers !== 'object') {
104
- return [];
105
- }
106
- return Object.keys(containers as Record<string, unknown>);
107
- }