UNPKG

@beignet/core

Version:

Core framework primitives for Beignet

47 lines 1.58 kB
export class InvalidRequestUrlError extends Error { constructor() { super("req.url must be an absolute HTTP or HTTPS URL."); this.name = "InvalidRequestUrlError"; } } export function parseHttpRequestUrl(value) { try { const url = new URL(value); if (url.protocol !== "http:" && url.protocol !== "https:") { throw new InvalidRequestUrlError(); } return url; } catch (error) { if (error instanceof InvalidRequestUrlError) throw error; throw new InvalidRequestUrlError(); } } export function requireTrustedProxyHeaderName(name, optionName) { 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) { 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"); } } //# sourceMappingURL=trusted-proxy-internal.js.map