-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsegment-url.js
More file actions
40 lines (36 loc) · 1.08 KB
/
Copy pathsegment-url.js
File metadata and controls
40 lines (36 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function rot13(input) {
return input.replace(/[a-zA-Z]/g, (char) => {
const base = char <= "Z" ? 65 : 97;
return String.fromCharCode(((char.charCodeAt(0) - base + 13) % 26) + base);
});
}
function decodeQueryParam(value) {
if (!value) return "";
return Buffer.from(rot13(decodeURIComponent(value.slice(8))), "base64").toString("utf8");
}
function readHostFromParam(paramValue) {
for (const entry of paramValue.split(",")) {
const at = entry.indexOf("@");
if (at >= 0) return entry.slice(at + 1);
}
return null;
}
export function hasEncodedSegmentParams(url) {
try {
const parsed = new URL(url);
return parsed.searchParams.has("_ctump") && parsed.searchParams.has("_ctuph");
} catch {
return false;
}
}
export function decodeSegmentUrl(segmentUrl) {
let parsed;
try {
parsed = new URL(segmentUrl);
} catch {
return null;
}
const host = readHostFromParam(decodeQueryParam(parsed.searchParams.get("_ctump")));
const path = decodeQueryParam(parsed.searchParams.get("_ctuph"));
return host && path ? `https://${host}${path}` : null;
}