Skip to content

Commit ef9ed1e

Browse files
juliusmarmingecodex
andcommitted
Share safe URL diagnostics
Co-authored-by: codex <[email protected]>
1 parent 922123f commit ef9ed1e

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎packages/shared/package.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@
103103
"types": "./src/dpopCommon.ts",
104104
"import": "./src/dpopCommon.ts"
105105
},
106+
"./urlDiagnostics": {
107+
"types": "./src/urlDiagnostics.ts",
108+
"import": "./src/urlDiagnostics.ts"
109+
},
106110
"./relayAuth": {
107111
"types": "./src/relayAuth.ts",
108112
"import": "./src/relayAuth.ts"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { assert, describe, it } from "@effect/vitest";
2+
3+
import { getUrlDiagnostics } from "./urlDiagnostics.ts";
4+
5+
describe("getUrlDiagnostics", () => {
6+
it("retains only input length, protocol, and hostname for valid URLs", () => {
7+
const input =
8+
"https://user:[email protected]:8443/private/path?access_token=secret#fragment";
9+
10+
assert.deepStrictEqual(getUrlDiagnostics(input), {
11+
inputLength: input.length,
12+
protocol: "https:",
13+
hostname: "example.com",
14+
});
15+
});
16+
17+
it("returns only input length for invalid URLs", () => {
18+
const input = "not a URL?access_token=secret";
19+
20+
assert.deepStrictEqual(getUrlDiagnostics(input), {
21+
inputLength: input.length,
22+
});
23+
});
24+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export interface UrlDiagnostics {
2+
readonly inputLength: number;
3+
readonly protocol?: string;
4+
readonly hostname?: string;
5+
}
6+
7+
export function getUrlDiagnostics(input: string): UrlDiagnostics {
8+
const inputLength = input.length;
9+
try {
10+
const url = new URL(input);
11+
return {
12+
inputLength,
13+
protocol: url.protocol,
14+
hostname: url.hostname,
15+
};
16+
} catch {
17+
return { inputLength };
18+
}
19+
}

0 commit comments

Comments
 (0)