diff --git a/packages/shared/src/advertisedEndpoint.test.ts b/packages/shared/src/advertisedEndpoint.test.ts new file mode 100644 index 0000000000..8c336bfe28 --- /dev/null +++ b/packages/shared/src/advertisedEndpoint.test.ts @@ -0,0 +1,55 @@ +import { describe, expect, it } from "vite-plus/test"; + +import { + AdvertisedEndpointProtocolError, + AdvertisedEndpointUrlParseError, + normalizeHttpBaseUrl, +} from "./advertisedEndpoint.ts"; + +const captureError = (run: () => unknown): unknown => { + try { + run(); + } catch (cause) { + return cause; + } + throw new Error("Expected operation to throw"); +}; + +describe("advertised endpoints", () => { + it("normalizes websocket endpoints to an HTTP base URL", () => { + expect(normalizeHttpBaseUrl("wss://relay.example.com/path?query=value#fragment")).toBe( + "https://relay.example.com/", + ); + }); + + it("preserves URL parser failures without retaining their raw input", () => { + const input = "not a URL"; + const error = captureError(() => normalizeHttpBaseUrl(input)); + + expect(error).toBeInstanceOf(AdvertisedEndpointUrlParseError); + expect(error).toMatchObject({ inputLength: input.length }); + expect(error).not.toHaveProperty("input"); + expect((error as AdvertisedEndpointUrlParseError).cause).toBeInstanceOf(Error); + expect((error as Error).message).toBe("Invalid advertised endpoint URL."); + }); + + it("reports unsupported protocols without inventing a cause", () => { + const input = + "ftp://endpoint-user:endpoint-password@relay.example.com/private/path?token=secret#fragment"; + const error = captureError(() => normalizeHttpBaseUrl(input)); + + expect(error).toBeInstanceOf(AdvertisedEndpointProtocolError); + expect(error).toMatchObject({ + inputLength: input.length, + inputProtocol: "ftp:", + inputHostname: "relay.example.com", + protocol: "ftp:", + }); + expect(error).not.toHaveProperty("input"); + expect((error as Error & { cause?: unknown }).cause).toBeUndefined(); + expect((error as Error).message).toBe("Endpoint must use HTTP or HTTPS. Received ftp:"); + expect((error as Error).message).not.toContain("endpoint-password"); + expect((error as Error).message).not.toContain("/private/path"); + expect((error as Error).message).not.toContain("token=secret"); + }); +}); diff --git a/packages/shared/src/advertisedEndpoint.ts b/packages/shared/src/advertisedEndpoint.ts index 314d8272c8..9021aa739f 100644 --- a/packages/shared/src/advertisedEndpoint.ts +++ b/packages/shared/src/advertisedEndpoint.ts @@ -6,6 +6,48 @@ import type { AdvertisedEndpointSource, AdvertisedEndpointStatus, } from "@t3tools/contracts"; +import * as Schema from "effect/Schema"; + +import { getUrlDiagnostics } from "./urlDiagnostics.ts"; + +const AdvertisedEndpointInputDiagnosticFields = { + inputLength: Schema.Number, + inputProtocol: Schema.optional(Schema.String), + inputHostname: Schema.optional(Schema.String), +}; + +function advertisedEndpointInputDiagnostics(input: string) { + const diagnostics = getUrlDiagnostics(input); + return { + inputLength: diagnostics.inputLength, + ...(diagnostics.protocol === undefined ? {} : { inputProtocol: diagnostics.protocol }), + ...(diagnostics.hostname === undefined ? {} : { inputHostname: diagnostics.hostname }), + }; +} + +export class AdvertisedEndpointUrlParseError extends Schema.TaggedErrorClass()( + "AdvertisedEndpointUrlParseError", + { + ...AdvertisedEndpointInputDiagnosticFields, + cause: Schema.Defect(), + }, +) { + override get message(): string { + return "Invalid advertised endpoint URL."; + } +} + +export class AdvertisedEndpointProtocolError extends Schema.TaggedErrorClass()( + "AdvertisedEndpointProtocolError", + { + ...AdvertisedEndpointInputDiagnosticFields, + protocol: Schema.String, + }, +) { + override get message(): string { + return `Endpoint must use HTTP or HTTPS. Received ${this.protocol}`; + } +} export interface CreateAdvertisedEndpointInput { readonly id: string; @@ -22,7 +64,15 @@ export interface CreateAdvertisedEndpointInput { } export function normalizeHttpBaseUrl(rawValue: string): string { - const url = new URL(rawValue); + let url: URL; + try { + url = new URL(rawValue); + } catch (cause) { + throw new AdvertisedEndpointUrlParseError({ + ...advertisedEndpointInputDiagnostics(rawValue), + cause, + }); + } if (url.protocol === "ws:") { url.protocol = "http:"; } else if (url.protocol === "wss:") { @@ -30,7 +80,10 @@ export function normalizeHttpBaseUrl(rawValue: string): string { } if (url.protocol !== "http:" && url.protocol !== "https:") { - throw new Error(`Endpoint must use HTTP or HTTPS. Received ${url.protocol}`); + throw new AdvertisedEndpointProtocolError({ + ...advertisedEndpointInputDiagnostics(rawValue), + protocol: url.protocol, + }); } url.pathname = "/";