import { validateHeaderValue } from 'node:http'; export class ProxyError extends Error { constructor(code) { super(code); this.name = 'ProxyError'; this.code = code; } } export const refuse = code => { throw new ProxyError(code); }; export function validManagementKey(key) { if (typeof key !== 'string' || !/^[\x21-\x7e]{1,4096}$/.test(key)) return false; try { validateHeaderValue('Authorization', `Bearer ${key}`); return true; } catch { return false; } } export function proxyTiming(value) { const result = {}; for (const [field, max, code] of [['rotateGraceMinutes', 1440, 'proxy-rotate-grace-invalid'], ['firstOutputSeconds', 3600, 'proxy-first-output-invalid']]) { if (!Object.hasOwn(value, field)) continue; if (!Number.isInteger(value[field]) || value[field] < 1 || value[field] > max) refuse(code); result[field] = value[field]; } return result; }