UNPKG

@beignet/core

Version:

Core framework primitives for Beignet

42 lines (36 loc) 1.05 kB
import type { TrustedProxyConfig } from "./trusted-proxy.js"; export function requireTrustedProxyHeaderName( name: string, optionName: string, ): string { const header = name.trim(); if (!header) { throw new Error(`${optionName} must be a non-empty header name.`); } try { new Headers().get(header); } catch { throw new Error(`${optionName} must be a valid HTTP header name.`); } return header; } export function assertValidTrustedProxyConfig( config: TrustedProxyConfig, ): void { if (!config) return; if (config.hostHeader !== undefined && config.hostHeader !== false) { requireTrustedProxyHeaderName(config.hostHeader, "trustedProxy.hostHeader"); } if (config.protocolHeader !== undefined && config.protocolHeader !== false) { requireTrustedProxyHeaderName( config.protocolHeader, "trustedProxy.protocolHeader", ); } if (typeof config.clientIp === "object") { requireTrustedProxyHeaderName( config.clientIp.header, "trustedProxy.clientIp.header", ); } }