UNPKG

bun-ws-router

Version:

Lightweight client/server WebSocket router for Bun with type-safe Zod/Valibot validation.

59 lines 2.06 kB
// SPDX-FileCopyrightText: 2025-present Kriasoft // SPDX-License-Identifier: MIT /** * Validates protocolPrefix doesn't contain spaces or commas (RFC 6455). * Throws TypeError if invalid. */ export function validateProtocolPrefix(prefix) { if (!/^[^\s,]+$/.test(prefix)) { throw new TypeError(`Invalid protocolPrefix: "${prefix}" (must not contain spaces/commas)`); } } /** * Attaches auth token to URL as query parameter. */ export function attachTokenToUrl(url, token, queryParam) { const urlObj = typeof url === "string" ? new URL(url) : new URL(url.href); urlObj.searchParams.set(queryParam, token); return urlObj; } /** * Merges user protocols with auth token protocol. * Returns combined protocols array with deduplication. */ export function mergeProtocols(userProtocols, token, protocolPrefix, protocolPosition) { // Normalize user protocols to array const normalized = userProtocols === undefined ? [] : Array.isArray(userProtocols) ? userProtocols : [userProtocols]; // Generate token protocol if token exists const tokenProtocol = token !== null && token !== undefined ? `${protocolPrefix}${token}` : null; // Combine based on position const combined = tokenProtocol === null ? normalized : protocolPosition === "prepend" ? [tokenProtocol, ...normalized] : [...normalized, tokenProtocol]; // Deduplicate preserving first occurrence const seen = new Set(); const deduplicated = combined.filter((p) => { if (seen.has(p)) return false; seen.add(p); return true; }); // Filter out empty strings (prevent malformed header) const filtered = deduplicated.filter((p) => p !== ""); return filtered.length > 0 ? filtered : undefined; } /** * Retrieves auth token (handles sync/async getToken). */ export async function getAuthToken(getToken) { if (!getToken) return undefined; return await getToken(); } //# sourceMappingURL=auth.js.map