Share safe URL diagnostics

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-06-20 19:21:42 -07:00
1 parent c60d2b1de2
commit 52701266ac
3 files changed
+47

No files matched your search

+4
View File
@@ -103,6 +103,10 @@
"types": "./src/dpopCommon.ts",
"import": "./src/dpopCommon.ts"
},
"./urlDiagnostics": {
"types": "./src/urlDiagnostics.ts",
"import": "./src/urlDiagnostics.ts"
},
"./relayAuth": {
"types": "./src/relayAuth.ts",
"import": "./src/relayAuth.ts"
@@ -0,0 +1,24 @@
import { assert, describe, it } from "@effect/vitest";
import { getUrlDiagnostics } from "./urlDiagnostics.ts";
describe("getUrlDiagnostics", () => {
it("retains only input length, protocol, and hostname for valid URLs", () => {
const input =
"https://user:[email protected]:8443/private/path?access_token=secret#fragment";
assert.deepStrictEqual(getUrlDiagnostics(input), {
inputLength: input.length,
protocol: "https:",
hostname: "example.com",
});
});
it("returns only input length for invalid URLs", () => {
const input = "not a URL?access_token=secret";
assert.deepStrictEqual(getUrlDiagnostics(input), {
inputLength: input.length,
});
});
});
+19
View File
@@ -0,0 +1,19 @@
export interface UrlDiagnostics {
readonly inputLength: number;
readonly protocol?: string;
readonly hostname?: string;
}
export function getUrlDiagnostics(input: string): UrlDiagnostics {
const inputLength = input.length;
try {
const url = new URL(input);
return {
inputLength,
protocol: url.protocol,
hostname: url.hostname,
};
} catch {
return { inputLength };
}
}