Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-protocol/src/util/parse-workspace-id.ts
2500 views
1
/**
2
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
const REGEX_WORKSPACE_ID = /[0-9a-z]{2,16}-[0-9a-z]{2,16}-[0-9a-z]{8,11}/;
8
const REGEX_WORKSPACE_ID_EXACT = new RegExp(`^${REGEX_WORKSPACE_ID.source}$`);
9
// We need to parse the workspace id precisely here to get the case '<some-str>-<port>-<wsid>.ws.' right
10
const REGEX_WORKSPACE_ID_FROM_HOSTNAME = new RegExp(`(${REGEX_WORKSPACE_ID.source})\.ws`);
11
12
const REGEX_WORKSPACE_ID_LEGACY = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/;
13
const REGEX_WORKSPACE_ID_LEGACY_EXACT = new RegExp(`^${REGEX_WORKSPACE_ID_LEGACY.source}$`);
14
const REGEX_WORKSPACE_ID_LEGACY_FROM_HOSTNAME = new RegExp(`(${REGEX_WORKSPACE_ID_LEGACY.source})\.ws`);
15
16
/**
17
* Hostname may be of the form:
18
* - moccasin-ferret-155799b3.ws-eu01.gitpod.io
19
* - 1234-moccasin-ferret-155799b3.ws-eu01.gitpod.io
20
* - webview-1234-moccasin-ferret-155799b3.ws-eu01.gitpod.io (or any other string replacing webview)
21
* @param hostname The hostname the request is headed to
22
*/
23
export const parseWorkspaceIdFromHostname = function (hostname: string) {
24
const match = REGEX_WORKSPACE_ID_FROM_HOSTNAME.exec(hostname);
25
if (match && match.length >= 2) {
26
return match[1];
27
} else {
28
const legacyMatch = REGEX_WORKSPACE_ID_LEGACY_FROM_HOSTNAME.exec(hostname);
29
if (legacyMatch && legacyMatch.length >= 2) {
30
return legacyMatch[1];
31
}
32
return undefined;
33
}
34
};
35
36
/** Equals UUIDv4 (and REGEX_WORKSPACE_ID_LEGACY!) */
37
const REGEX_INSTANCE_ID = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/;
38
const REGEX_INSTANCE_ID_EXACT = new RegExp(`^${REGEX_INSTANCE_ID.source}$`);
39
40
/**
41
* @param maybeId
42
* @returns
43
*/
44
export const matchesInstanceIdOrLegacyWorkspaceIdExactly = function (maybeId: string): boolean {
45
return REGEX_INSTANCE_ID_EXACT.test(maybeId) || REGEX_WORKSPACE_ID_LEGACY_EXACT.test(maybeId);
46
};
47
48
/**
49
* @param maybeWorkspaceId
50
* @returns
51
*/
52
export const matchesNewWorkspaceIdExactly = function (maybeWorkspaceId: string): boolean {
53
return REGEX_WORKSPACE_ID_EXACT.test(maybeWorkspaceId);
54
};
55
56
/**
57
* Matches both new and legacy workspace ids
58
*/
59
export const isWorkspaceId = function (maybeWorkspaceId?: string): boolean {
60
if (!maybeWorkspaceId) {
61
return false;
62
}
63
64
return matchesNewWorkspaceIdExactly(maybeWorkspaceId) || REGEX_WORKSPACE_ID_LEGACY_EXACT.test(maybeWorkspaceId);
65
};
66
67