-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathappProxyBasePath.ts
More file actions
38 lines (31 loc) · 1.02 KB
/
appProxyBasePath.ts
File metadata and controls
38 lines (31 loc) · 1.02 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
const APP_PROXY_BASE_PATH_RE = /^\/@[^/]+\/[^/]+(?:\/[^/]+)?\/apps\/[^/]+(?:\/|$)/;
function hasUnsafeLeadingDoubleSlash(pathname: string): boolean {
return pathname.startsWith("//");
}
function stripTrailingSlash(pathname: string): string {
return pathname.length > 1 && pathname.endsWith("/") ? pathname.slice(0, -1) : pathname;
}
export function getAppProxyBasePathFromPathname(pathname: string): string | null {
if (hasUnsafeLeadingDoubleSlash(pathname)) {
return null;
}
const match = APP_PROXY_BASE_PATH_RE.exec(pathname);
if (!match) {
return null;
}
return stripTrailingSlash(match[0]);
}
export function stripAppProxyBasePath(pathname: string): {
basePath: string | null;
routePathname: string;
} {
const basePath = getAppProxyBasePathFromPathname(pathname);
if (!basePath) {
return { basePath: null, routePathname: pathname };
}
const routePathname = pathname.slice(basePath.length);
return {
basePath,
routePathname: routePathname.length === 0 ? "/" : routePathname,
};
}