feat: switch to a central singleton handler

This also adds support for Headscale TLS installations
This commit is contained in:
Aarnav Tale
2025-03-17 22:21:16 -04:00
parent 43e06987ad
commit 6108de52e7
35 changed files with 339 additions and 399 deletions
+35 -39
View File
@@ -1,4 +1,6 @@
import log, { noContext } from '~/utils/log';
import { request } from 'undici';
import { hp_getConfig, hp_getSingleton } from '~server/context/global';
import log from '~server/utils/log';
export class HeadscaleError extends Error {
status: number;
@@ -19,24 +21,18 @@ export class FatalError extends Error {
}
}
interface HeadscaleContext {
url: string;
}
declare const global: typeof globalThis & { __hs_context: HeadscaleContext };
export async function healthcheck() {
const prefix = __hs_context.url;
log.debug('APIC', 'GET /health');
const health = new URL('health', prefix);
const response = await fetch(health.toString(), {
const health = new URL('health', hp_getConfig().headscale.url);
const response = await request(health.toString(), {
dispatcher: hp_getSingleton('api_agent'),
headers: {
Accept: 'application/json',
},
});
// Intentionally not catching
return response.status === 200;
return response.statusCode === 200;
}
export async function pull<T>(url: string, key: string) {
@@ -44,26 +40,26 @@ export async function pull<T>(url: string, key: string) {
throw new Error('Missing API key, could this be a cookie setting issue?');
}
const prefix = __hs_context.url;
const prefix = hp_getConfig().headscale.url;
log.debug('APIC', 'GET %s', `${prefix}/api/${url}`);
const response = await fetch(`${prefix}/api/${url}`, {
const response = await request(`${prefix}/api/${url}`, {
dispatcher: hp_getSingleton('api_agent'),
headers: {
Authorization: `Bearer ${key}`,
},
});
if (!response.ok) {
if (response.statusCode >= 400) {
log.debug(
'APIC',
'GET %s failed with status %d',
`${prefix}/api/${url}`,
response.status,
response.statusCode,
);
throw new HeadscaleError(await response.text(), response.status);
throw new HeadscaleError(await response.body.text(), response.statusCode);
}
return response.json() as Promise<T>;
return response.body.json() as Promise<T>;
}
export async function post<T>(url: string, key: string, body?: unknown) {
@@ -71,10 +67,10 @@ export async function post<T>(url: string, key: string, body?: unknown) {
throw new Error('Missing API key, could this be a cookie setting issue?');
}
const prefix = __hs_context.url;
const prefix = hp_getConfig().headscale.url;
log.debug('APIC', 'POST %s', `${prefix}/api/${url}`);
const response = await fetch(`${prefix}/api/${url}`, {
const response = await request(`${prefix}/api/${url}`, {
dispatcher: hp_getSingleton('api_agent'),
method: 'POST',
body: body ? JSON.stringify(body) : undefined,
headers: {
@@ -82,17 +78,17 @@ export async function post<T>(url: string, key: string, body?: unknown) {
},
});
if (!response.ok) {
if (response.statusCode >= 400) {
log.debug(
'APIC',
'POST %s failed with status %d',
`${prefix}/api/${url}`,
response.status,
response.statusCode,
);
throw new HeadscaleError(await response.text(), response.status);
throw new HeadscaleError(await response.body.text(), response.statusCode);
}
return response.json() as Promise<T>;
return response.body.json() as Promise<T>;
}
export async function put<T>(url: string, key: string, body?: unknown) {
@@ -100,10 +96,10 @@ export async function put<T>(url: string, key: string, body?: unknown) {
throw new Error('Missing API key, could this be a cookie setting issue?');
}
const prefix = __hs_context.url;
const prefix = hp_getConfig().headscale.url;
log.debug('APIC', 'PUT %s', `${prefix}/api/${url}`);
const response = await fetch(`${prefix}/api/${url}`, {
const response = await request(`${prefix}/api/${url}`, {
dispatcher: hp_getSingleton('api_agent'),
method: 'PUT',
body: body ? JSON.stringify(body) : undefined,
headers: {
@@ -111,17 +107,17 @@ export async function put<T>(url: string, key: string, body?: unknown) {
},
});
if (!response.ok) {
if (response.statusCode >= 400) {
log.debug(
'APIC',
'PUT %s failed with status %d',
`${prefix}/api/${url}`,
response.status,
response.statusCode,
);
throw new HeadscaleError(await response.text(), response.status);
throw new HeadscaleError(await response.body.text(), response.statusCode);
}
return response.json() as Promise<T>;
return response.body.json() as Promise<T>;
}
export async function del<T>(url: string, key: string) {
@@ -129,25 +125,25 @@ export async function del<T>(url: string, key: string) {
throw new Error('Missing API key, could this be a cookie setting issue?');
}
const prefix = __hs_context.url;
const prefix = hp_getConfig().headscale.url;
log.debug('APIC', 'DELETE %s', `${prefix}/api/${url}`);
const response = await fetch(`${prefix}/api/${url}`, {
const response = await request(`${prefix}/api/${url}`, {
dispatcher: hp_getSingleton('api_agent'),
method: 'DELETE',
headers: {
Authorization: `Bearer ${key}`,
},
});
if (!response.ok) {
if (response.statusCode >= 400) {
log.debug(
'APIC',
'DELETE %s failed with status %d',
`${prefix}/api/${url}`,
response.status,
response.statusCode,
);
throw new HeadscaleError(await response.text(), response.status);
throw new HeadscaleError(await response.body.text(), response.statusCode);
}
return response.json() as Promise<T>;
return response.body.json() as Promise<T>;
}