Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/supervisor/frontend/src/ide/ide-worker.ts
2501 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
/**
8
* Installs the proxy (shared) worker serving from the same origin to fetch content from the blobserve origin.
9
*/
10
export function install(): void {
11
if (window.Worker) {
12
const Worker = window.Worker;
13
window.Worker = <any>function (scriptUrl: string | URL, options?: WorkerOptions) {
14
return new Worker(proxyUrl(scriptUrl), options);
15
};
16
}
17
if (window.SharedWorker) {
18
const SharedWorker = window.SharedWorker;
19
window.SharedWorker = <any>function (scriptUrl: string, options?: string | SharedWorkerOptions) {
20
return new SharedWorker(proxyUrl(scriptUrl), options);
21
};
22
}
23
}
24
25
function proxyUrl(scriptUrl: string | URL | TrustedScriptURL): string {
26
scriptUrl =
27
scriptUrl instanceof URL
28
? scriptUrl
29
: new URL(typeof scriptUrl === "string" ? scriptUrl : scriptUrl.toString(), document.baseURI);
30
if (scriptUrl.origin !== location.origin || (scriptUrl.protocol !== "http:" && scriptUrl.protocol !== "https:")) {
31
return scriptUrl.toString();
32
}
33
return new URL(
34
"_supervisor/frontend/worker-proxy.js#" + encodeURI(scriptUrl.toString()),
35
document.baseURI,
36
).toString();
37
}
38
39