fix: use globals to avoid loader race conditions

This commit is contained in:
Aarnav Tale
2025-02-20 10:45:52 -05:00
parent f5436f5ee3
commit f982217dd0
5 changed files with 60 additions and 97 deletions
+9 -6
View File
@@ -9,8 +9,6 @@ let runtimeYaml: Document | undefined = undefined;
let runtimeConfig: HeadscaleConfig | undefined = undefined;
let runtimePath: string | undefined = undefined;
let runtimeMode: 'rw' | 'ro' | 'no' = 'no';
let runtimeStrict = true;
const runtimeLock = mutex();
export type ConfigModes =
@@ -42,9 +40,12 @@ export function hs_getConfig(): ConfigModes {
};
}
export async function hs_loadConfig(context: HeadplaneConfig) {
export async function hs_loadConfig(path?: string, strict?: boolean) {
if (runtimeConfig !== undefined) {
return;
}
runtimeLock.acquire();
const path = context.headscale.config_path;
if (!path) {
runtimeLock.release();
return;
@@ -62,8 +63,7 @@ export async function hs_loadConfig(context: HeadplaneConfig) {
return;
}
runtimeStrict = context.headscale.config_strict ?? true;
const config = validateConfig(rawConfig, runtimeStrict);
const config = validateConfig(rawConfig, strict ?? true);
if (!config) {
runtimeMode = 'no';
}
@@ -194,3 +194,6 @@ export async function hs_patchConfig(patches: PatchConfig[]) {
await writeFile(runtimePath, runtimeYaml.toString(), 'utf8');
runtimeLock.release();
}
// IMPORTANT THIS IS A SIDE EFFECT ON INIT
hs_loadConfig(__hs_context.config_path, __hs_context.config_strict);
+8 -34
View File
@@ -1,7 +1,5 @@
import log, { noContext } from '~/utils/log';
import { AppContext } from '~server/context/app';
type Context = AppContext['context'];
export class HeadscaleError extends Error {
status: number;
@@ -21,21 +19,13 @@ export class FatalError extends Error {
}
}
let context: Context | undefined = undefined;
export function hp_storeContext(ctx: Context) {
if (context) {
return;
}
context = ctx;
interface HeadscaleContext {
url: string;
}
declare const global: typeof globalThis & { __hs_context: HeadscaleContext };
export async function healthcheck() {
if (!context) {
throw noContext();
}
const prefix = context.headscale.url;
const prefix = __hs_context.url;
log.debug('APIC', 'GET /health');
const health = new URL('health', prefix);
@@ -50,15 +40,11 @@ export async function healthcheck() {
}
export async function pull<T>(url: string, key: string) {
if (!context) {
throw noContext();
}
if (!key || key === 'undefined' || key.length === 0) {
throw new Error('Missing API key, could this be a cookie setting issue?');
}
const prefix = context.headscale.url;
const prefix = __hs_context.url;
log.debug('APIC', 'GET %s', `${prefix}/api/${url}`);
const response = await fetch(`${prefix}/api/${url}`, {
@@ -81,15 +67,11 @@ export async function pull<T>(url: string, key: string) {
}
export async function post<T>(url: string, key: string, body?: unknown) {
if (!context) {
throw noContext();
}
if (!key || key === 'undefined' || key.length === 0) {
throw new Error('Missing API key, could this be a cookie setting issue?');
}
const prefix = context.headscale.url;
const prefix = __hs_context.url;
log.debug('APIC', 'POST %s', `${prefix}/api/${url}`);
const response = await fetch(`${prefix}/api/${url}`, {
@@ -114,15 +96,11 @@ export async function post<T>(url: string, key: string, body?: unknown) {
}
export async function put<T>(url: string, key: string, body?: unknown) {
if (!context) {
throw noContext();
}
if (!key || key === 'undefined' || key.length === 0) {
throw new Error('Missing API key, could this be a cookie setting issue?');
}
const prefix = context.headscale.url;
const prefix = __hs_context.url;
log.debug('APIC', 'PUT %s', `${prefix}/api/${url}`);
const response = await fetch(`${prefix}/api/${url}`, {
@@ -147,15 +125,11 @@ export async function put<T>(url: string, key: string, body?: unknown) {
}
export async function del<T>(url: string, key: string) {
if (!context) {
throw noContext();
}
if (!key || key === 'undefined' || key.length === 0) {
throw new Error('Missing API key, could this be a cookie setting issue?');
}
const prefix = context.headscale.url;
const prefix = __hs_context.url;
log.debug('APIC', 'DELETE %s', `${prefix}/api/${url}`);
const response = await fetch(`${prefix}/api/${url}`, {
+16 -42
View File
@@ -1,8 +1,4 @@
import {
Session,
SessionStorage,
createCookieSessionStorage,
} from 'react-router';
import { Session, createCookieSessionStorage } from 'react-router';
export type SessionData = {
hsApiKey: string;
@@ -23,42 +19,28 @@ type SessionFlashData = {
error: string;
};
type SessionStore = SessionStorage<SessionData, SessionFlashData>;
// TODO: Add args to this function to allow custom domain/config
let sessionStorage: SessionStore | null = null;
export function initSessionManager(secret: string, secure: boolean) {
if (sessionStorage) {
return;
}
sessionStorage = createCookieSessionStorage<SessionData, SessionFlashData>({
cookie: {
name: 'hp_sess',
httpOnly: true,
maxAge: 60 * 60 * 24, // 24 hours
path: '/',
sameSite: 'lax',
secrets: [secret],
secure,
},
});
}
// TODO: Domain config in cookies
const sessionStorage = createCookieSessionStorage<
SessionData,
SessionFlashData
>({
cookie: {
name: 'hp_sess',
httpOnly: true,
maxAge: 60 * 60 * 24, // 24 hours
path: '/',
sameSite: 'lax',
secrets: [__cookie_context.cookie_secret],
secure: __cookie_context.cookie_secure,
},
});
export function getSession(cookie: string | null) {
if (!sessionStorage) {
throw new Error('Session manager not initialized');
}
return sessionStorage.getSession(cookie);
}
export type ServerSession = Session<SessionData, SessionFlashData>;
export async function auth(request: Request) {
if (!sessionStorage) {
return false;
}
const cookie = request.headers.get('Cookie');
const session = await sessionStorage.getSession(cookie);
if (!session.has('hsApiKey')) {
@@ -69,17 +51,9 @@ export async function auth(request: Request) {
}
export function destroySession(session: Session) {
if (!sessionStorage) {
throw new Error('Session manager not initialized');
}
return sessionStorage.destroySession(session);
}
export function commitSession(session: Session, opts?: { maxAge?: number }) {
if (!sessionStorage) {
throw new Error('Session manager not initialized');
}
return sessionStorage.commitSession(session, opts);
}