/** Where the local server listens. The port is chosen at every launch. */ export interface LocalEndpoint { readonly host: string; readonly port: number; } export const LOOPBACK = '127.0.0.1'; export function localEndpoint(port: number): LocalEndpoint { if (!Number.isInteger(port) || port < 1 || port > 65535) throw new RangeError(`port must be an integer from 1 to 65535, got ${port}`); return { host: LOOPBACK, port }; } /** `host:port` of the local server. */ export function hostPort(endpoint: LocalEndpoint): string { return `${endpoint.host}:${endpoint.port}`; } /** * Local URL standing in for a Gaijin URL. The original host becomes the first path segment, * so the local server can tell which Gaijin service a request was meant for even when two * services use the same path (char and inventory both use "/char"). */ export function localUrl(endpoint: LocalEndpoint, originalHost: string, originalPath: string, scheme: 'http' | 'ws' = 'http'): string { const path = originalPath.startsWith('/') ? originalPath : `/${originalPath}`; return `${scheme}://${hostPort(endpoint)}/${originalHost}${path}`; }