import pkg from '../package.json' with { type: 'json' }; /** Where a service whose logs show our traffic can reach us. */ export const DEFAULT_CONTACT = 'https://nibling.dev/contact'; /** The User-Agent every request nibling originates carries, so a service can tell us about runaway traffic. */ export function userAgent(contact: string = DEFAULT_CONTACT): string { return `nibling.dev/${pkg.version} (+${contact})`; } export const USER_AGENT = userAgent(); /** Browsers own the User-Agent header, and setting it would turn simple requests into preflighted ones. */ export function canSetUserAgent(): boolean { const scope = globalThis as { document?: unknown; WorkerGlobalScope?: unknown }; return scope.document === undefined && scope.WorkerGlobalScope === undefined; } /** `headers` with the User-Agent set when this runtime lets us set it. */ export function withUserAgent(headers?: HeadersInit): Headers { const out = new Headers(headers); if (canSetUserAgent()) out.set('user-agent', USER_AGENT); return out; }