UNPKG

@vigilbase/cli

Version:

Vigilbase CLI — security scanners, agent tools, and Vigilbase One from the terminal.

74 lines (73 loc) • 3.6 kB
// GENERATED from packages/api-contract/src/core/headers.ts by scripts/vendor-api-contract.mjs — do not edit /** Header names and builders shared by every product on api.vigilbase.com. */ export const REQUEST_ID_HEADER = 'X-Request-Id'; export const IDEMPOTENCY_HEADER = 'Idempotency-Key'; /** Informational only: the major version is in the path. */ export const API_VERSION_HEADER = 'API-Version'; export const RATE_LIMIT_HEADER = 'RateLimit'; export const RATE_LIMIT_POLICY_HEADER = 'RateLimit-Policy'; export const RETRY_AFTER_HEADER = 'Retry-After'; export const DEPRECATION_HEADER = 'Deprecation'; export const SUNSET_HEADER = 'Sunset'; export const LINK_HEADER = 'Link'; // ---------------------------------------------------------------- request IDs /** * Inbound request IDs are echoed into logs, problem bodies and responses, so * only a narrow, inert alphabet is accepted: 8 to 128 characters, no * whitespace, quotes, separators or control characters. OS's own pattern * (observability/src/request.ts) is a subset, so its IDs pass here too. */ export const REQUEST_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9_-]{7,127}$/; export function isValidRequestId(value) { return typeof value === 'string' && REQUEST_ID_PATTERN.test(value); } export function newRequestId() { return `req_${crypto.randomUUID().replace(/-/g, '')}`; } /** The caller's `X-Request-Id` when it is well-formed, otherwise a new one. */ export function resolveRequestId(headers) { const inbound = headers.get(REQUEST_ID_HEADER)?.trim(); return isValidRequestId(inbound) ? inbound : newRequestId(); } const POLICY_NAME = /^[a-z0-9][a-z0-9_-]{0,63}$/; export function rateLimitHeaders(state) { // The name is a structured-field string; a narrow alphabet needs no escaping. if (!POLICY_NAME.test(state.policy)) throw new Error(`invalid rate-limit policy name: ${state.policy}`); const int = (value) => Math.max(0, Math.ceil(value)); const headers = { [RATE_LIMIT_POLICY_HEADER]: `"${state.policy}";q=${int(state.quota)};w=${int(state.windowSeconds)}`, [RATE_LIMIT_HEADER]: `"${state.policy}";r=${int(state.remaining)};t=${int(state.resetSeconds)}`, }; if (state.retryAfterSeconds !== undefined && state.retryAfterSeconds > 0) headers[RETRY_AFTER_HEADER] = String(int(state.retryAfterSeconds)); return headers; } /** * `Deprecation: @<epoch seconds>` (RFC 9745 §2.1, a structured-field Date, not * the pre-RFC `true`), `Sunset: <IMF-fixdate>` (RFC 8594) and `Link` relations * `successor-version` and `deprecation` (RFC 9745 §3). */ export function deprecationHeaders(deprecation) { const at = deprecation.at.getTime(); if (!Number.isFinite(at)) throw new Error('deprecation date is invalid'); const headers = { [DEPRECATION_HEADER]: `@${Math.floor(at / 1000)}` }; if (deprecation.sunset) { const sunset = deprecation.sunset.getTime(); if (!Number.isFinite(sunset) || sunset < at) throw new Error('sunset must be a valid date no earlier than the deprecation'); headers[SUNSET_HEADER] = deprecation.sunset.toUTCString(); } const links = [ deprecation.successor ? `<${linkTarget(deprecation.successor)}>; rel="successor-version"` : null, deprecation.docs ? `<${linkTarget(deprecation.docs)}>; rel="deprecation"; type="text/html"` : null, ].filter((link) => link !== null); if (links.length) headers[LINK_HEADER] = links.join(', '); return headers; } function linkTarget(url) { const parsed = new URL(url); return parsed.href; }