-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathgitpod-host-url.ts
175 lines (139 loc) · 5.36 KB
/
gitpod-host-url.ts
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License-AGPL.txt in the project root for license information.
*/
const URL = require('url').URL || window.URL;
import { log } from './logging';
export interface UrlChange {
(old: URL): Partial<URL>
}
export type UrlUpdate = UrlChange | Partial<URL>;
const baseWorkspaceIDRegex = "(([a-f][0-9a-f]{7}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})|([0-9a-z]{2,16}-[0-9a-z]{2,16}-[0-9a-z]{8}))";
// this pattern matches v4 UUIDs as well as the new generated workspace ids (e.g. pink-panda-ns35kd21)
const workspaceIDRegex = RegExp(`^${baseWorkspaceIDRegex}$`);
// this pattern matches URL prefixes of workspaces
const workspaceUrlPrefixRegex = RegExp(`^([0-9]{4,6}-)?${baseWorkspaceIDRegex}\\.`);
export class GitpodHostUrl {
readonly url: URL;
constructor(urlParam?: string | URL) {
if (urlParam === undefined || typeof urlParam === 'string') {
this.url = new URL(urlParam || 'https://gitpod.io');
this.url.search = '';
this.url.hash = '';
this.url.pathname = '';
} else if (urlParam instanceof URL) {
this.url = urlParam;
} else {
log.error('Unexpected urlParam', { urlParam });
}
}
static fromWorkspaceUrl(url: string) {
return new GitpodHostUrl(new URL(url));
}
withWorkspacePrefix(workspaceId: string, region: string) {
return this.withDomainPrefix(`${workspaceId}.ws-${region}.`);
}
withDomainPrefix(prefix: string): GitpodHostUrl {
return this.with(url => ({ host: prefix + url.host }));;
}
withoutWorkspacePrefix(): GitpodHostUrl {
if (!this.url.host.match(workspaceUrlPrefixRegex)) {
// URL has no workspace prefix
return this;
}
return this.withoutDomainPrefix(2);
}
withoutDomainPrefix(removeSegmentsCount: number): GitpodHostUrl {
return this.with(url => ({ host: url.host.split('.').splice(removeSegmentsCount).join('.') }));
}
with(urlUpdate: UrlUpdate) {
const update = typeof urlUpdate === 'function' ? urlUpdate(this.url) : urlUpdate;
const addSlashToPath = update.pathname && update.pathname.length > 0 && !update.pathname.startsWith('/');
if (addSlashToPath) {
update.pathname = '/' + update.pathname;
}
const result = Object.assign(new URL(this.toString()), update);
return new GitpodHostUrl(result);
}
withApi(urlUpdate?: UrlUpdate) {
const updated = urlUpdate ? this.with(urlUpdate) : this;
return updated.with(url => ({ pathname: `/api${url.pathname}` }));
}
withContext(contextUrl: string) {
return this.with(url => ({ hash: contextUrl }));
}
asWebsocket(): GitpodHostUrl {
return this.with(url => ({ protocol: url.protocol === 'https:' ? 'wss:' : 'ws:' }));
}
asDashboard(): GitpodHostUrl {
return this.with(url => ({ pathname: '/' }));
}
asLogin(): GitpodHostUrl {
return this.with(url => ({ pathname: '/login' }));
}
asUpgradeSubscription(): GitpodHostUrl {
return this.with(url => ({ pathname: '/plans' }));
}
asAccessControl(): GitpodHostUrl {
return this.with(url => ({ pathname: '/integrations' }));
}
asSettings(): GitpodHostUrl {
return this.with(url => ({ pathname: '/settings' }));
}
asPreferences(): GitpodHostUrl {
return this.with(url => ({ pathname: '/preferences' }));
}
asGraphQLApi(): GitpodHostUrl {
return this.with(url => ({ pathname: '/graphql/' }));
}
asStart(workspaceId = this.workspaceId): GitpodHostUrl {
return this.withoutWorkspacePrefix().with({
pathname: '/start/',
hash: '#' + workspaceId
});
}
asWorkspaceAuth(instanceID: string, redirect?: boolean): GitpodHostUrl {
return this.with(url => ({ pathname: `/api/auth/workspace-cookie/${instanceID}`, search: redirect ? "redirect" : "" }));
}
toString() {
return this.url.toString();
}
toStringWoRootSlash() {
let result = this.toString();
if (result.endsWith('/')) {
result = result.slice(0, result.length - 1);
}
return result;
}
get workspaceId(): string | undefined {
const hostSegs = this.url.host.split(".");
if (hostSegs.length > 1) {
const matchResults = hostSegs[0].match(workspaceIDRegex);
if (matchResults) {
// URL has a workspace prefix
// port prefixes are excluded
return matchResults[0];
}
}
const pathSegs = this.url.pathname.split("/")
if (pathSegs.length > 3 && pathSegs[1] === "workspace") {
return pathSegs[2];
}
return undefined;
}
get blobServe(): boolean {
const hostSegments = this.url.host.split(".");
if (hostSegments[0] === 'blobserve') {
return true;
}
const pathSegments = this.url.pathname.split("/")
return pathSegments[0] === "blobserve";
}
asSorry(message: string) {
return this.with({ pathname: '/sorry', hash: message });
}
asApiLogout(): GitpodHostUrl {
return this.withApi(url => ({ pathname: '/logout/' }));
}
}