Files
npm-statuspage/src/npm.ts

47 lines
1.6 KiB
TypeScript
Raw Normal View History

export interface ProxyHost {
domain_names: string[];
forward_host: string;
forward_port: number;
enabled: boolean;
}
const NPM_BASE = process.env.NPM_BASE_URL ?? 'http://localhost:81';
const NPM_EMAIL = process.env.NPM_EMAIL ?? '';
const NPM_SECRET = process.env.NPM_SECRET ?? '';
const rawPatterns = (process.env.NPM_INCLUDE_PATTERNS ?? '\\.goonk\\.se$,\\.dev\\.xplwd\\.com$')
.split(',').map(p => new RegExp(p.trim()));
export const INCLUDE_PATTERNS = rawPatterns;
export function matchesIncludePatterns(domain: string): boolean {
return INCLUDE_PATTERNS.some(p => p.test(domain));
}
let token: string | null = null;
let tokenExpiry = 0;
async function getToken(): Promise<string> {
if (token && Date.now() < tokenExpiry) return token;
const res = await fetch(`${NPM_BASE}/api/tokens`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ identity: NPM_EMAIL, secret: NPM_SECRET }),
});
const data = await res.json() as { token: string; expires: string };
token = data.token;
tokenExpiry = new Date(data.expires).getTime() - 60_000;
return token;
}
// Every enabled proxy host, regardless of NPM_INCLUDE_PATTERNS — the admin
// UI needs the full set to let you force-show something the pattern would
// otherwise exclude. checker.ts applies the pattern (or an override) itself.
export async function fetchAllProxyHosts(): Promise<ProxyHost[]> {
const tok = await getToken();
const res = await fetch(`${NPM_BASE}/api/nginx/proxy-hosts`, {
headers: { Authorization: `Bearer ${tok}` },
});
const all = await res.json() as ProxyHost[];
return all.filter(h => h.enabled);
}