forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
41 lines (33 loc) · 1.3 KB
/
Copy pathproxy.ts
File metadata and controls
41 lines (33 loc) · 1.3 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
41
import { deprecated } from './deprecated';
export function isValidProxyUrl(key: string | undefined) {
if (!key) {
return true;
}
return isHttpOrHttps(key) || isProxyUrlRelative(key);
}
export function isHttpOrHttps(key: string | undefined) {
return /^http(s)?:\/\//.test(key || '');
}
export function isProxyUrlRelative(key: string) {
return key.startsWith('/');
}
export function proxyUrlToAbsoluteURL(url: string | undefined): string {
if (!url) {
return '';
}
return isProxyUrlRelative(url) ? new URL(url, window.location.origin).toString() : url;
}
/**
* @deprecated Use `buildRequestUrl` from @clerk/backend
*/
export function getRequestUrl({ request, relativePath }: { request: Request; relativePath?: string }): URL {
deprecated('getRequestUrl', 'Use `buildRequestUrl` from @clerk/backend instead.');
const { headers, url: initialUrl } = request;
const url = new URL(initialUrl);
const host = headers.get('X-Forwarded-Host') ?? headers.get('host') ?? (headers as any)['host'] ?? url.host;
// X-Forwarded-Proto could be 'https, http'
let protocol =
(headers.get('X-Forwarded-Proto') ?? (headers as any)['X-Forwarded-Proto'])?.split(',')[0] ?? url.protocol;
protocol = protocol.replace(/[:/]/, '');
return new URL(relativePath || url.pathname, `${protocol}://${host}`);
}