/** The addresses a mock server answers on this machine under `localhost`: the default and the wildcard binds. */ const LOCAL_BINDS = new Set(['127.0.0.1', 'localhost', '::1', '0.0.0.0', '::', '']); /** * The origin a mock server names in the URLs it hands out: `advertisedOrigin` when given (another container's name * for it), the bound address when `host` is one of its own, else `http://localhost:`. */ export function mockServerOrigin(port: number, host?: string, advertisedOrigin?: string): string { if (advertisedOrigin) return withoutTrailingSlashes(advertisedOrigin); if (host === undefined || LOCAL_BINDS.has(host)) return `http://localhost:${port}`; return `http://${host.includes(':') ? `[${host}]` : host}:${port}`; } function withoutTrailingSlashes(origin: string): string { let end = origin.length; while (end > 0 && origin[end - 1] === '/') end--; return origin.slice(0, end); }