Skip to content

Commit c3fcaef

Browse files
committed
add and use getWorkerBootstrapUrl, don't use default worker factory anymore
1 parent 9c2cc80 commit c3fcaef

4 files changed

Lines changed: 68 additions & 42 deletions

File tree

src/vs/base/worker/defaultWorkerFactory.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,31 @@ function getWorker(workerId: string, label: string): Worker | Promise<Worker> {
1919
// ESM-comment-begin
2020
if (typeof require === 'function') {
2121
// check if the JS lives on a different origin
22-
2322
const workerMain = require.toUrl('./' + workerId);
24-
if (/^(http:)|(https:)|(file:)/.test(workerMain)) {
25-
const currentUrl = String(window.location);
26-
const currentOrigin = currentUrl.substr(0, currentUrl.length - window.location.hash.length - window.location.search.length - window.location.pathname.length);
27-
if (workerMain.substring(0, currentOrigin.length) !== currentOrigin) {
28-
// this is the cross-origin case
29-
// i.e. the webpage is running at a different origin than where the scripts are loaded from
30-
const workerBaseUrl = workerMain.substr(0, workerMain.length - 'vs/base/worker/workerMain.js'.length);
31-
const js = `/*${label}*/self.MonacoEnvironment={baseUrl: '${workerBaseUrl}'};importScripts('${workerMain}');/*${label}*/`;
32-
const url = `data:text/javascript;charset=utf-8,${encodeURIComponent(js)}`;
33-
return new Worker(url);
34-
}
35-
}
36-
return new Worker(workerMain + '#' + label);
23+
const workerUrl = getWorkerBootstrapUrl(workerMain, label);
24+
return new Worker(workerUrl, { name: label });
3725
}
3826
// ESM-comment-end
3927
throw new Error(`You must define a function MonacoEnvironment.getWorkerUrl or MonacoEnvironment.getWorker`);
4028
}
4129

30+
export function getWorkerBootstrapUrl(scriptPath: string, label: string): string {
31+
if (/^(http:)|(https:)|(file:)/.test(scriptPath)) {
32+
const currentUrl = String(window.location);
33+
const currentOrigin = currentUrl.substr(0, currentUrl.length - window.location.hash.length - window.location.search.length - window.location.pathname.length);
34+
if (scriptPath.substring(0, currentOrigin.length) !== currentOrigin) {
35+
// this is the cross-origin case
36+
// i.e. the webpage is running at a different origin than where the scripts are loaded from
37+
const myPath = 'vs/base/worker/defaultWorkerFactory.js';
38+
const workerBaseUrl = require.toUrl(myPath).slice(0, -myPath.length);
39+
const js = `/*${label}*/self.MonacoEnvironment={baseUrl: '${workerBaseUrl}'};importScripts('${scriptPath}');/*${label}*/`;
40+
const url = `data:text/javascript;charset=utf-8,${encodeURIComponent(js)}`;
41+
return url;
42+
}
43+
}
44+
return scriptPath + '#' + label;
45+
}
46+
4247
function isPromiseLike<T>(obj: any): obj is PromiseLike<T> {
4348
if (typeof obj.then === 'function') {
4449
return true;

src/vs/workbench/services/extensions/browser/webWorkerExtensionHostStarter.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { DefaultWorkerFactory } from 'vs/base/worker/defaultWorkerFactory';
6+
import { getWorkerBootstrapUrl } from 'vs/base/worker/defaultWorkerFactory';
77
import { Emitter, Event } from 'vs/base/common/event';
8-
import { DisposableStore } from 'vs/base/common/lifecycle';
8+
import { DisposableStore, toDisposable } from 'vs/base/common/lifecycle';
99
import { IMessagePassingProtocol } from 'vs/base/parts/ipc/common/ipc';
1010
import { VSBuffer } from 'vs/base/common/buffer';
1111
import { createMessageOfType, MessageType, isMessageOfType } from 'vs/workbench/services/extensions/common/extensionHostProtocol';
@@ -49,23 +49,29 @@ export class WebWorkerExtensionHostStarter implements IExtensionHostStarter {
4949
if (!this._protocol) {
5050

5151
const emitter = new Emitter<VSBuffer>();
52-
const worker = new DefaultWorkerFactory('WorkerExtensionHost').create(
53-
'vs/workbench/services/extensions/worker/extensionHostWorker', data => {
54-
if (data instanceof ArrayBuffer) {
55-
emitter.fire(VSBuffer.wrap(new Uint8Array(data, 0, data.byteLength)));
56-
} else {
57-
console.warn('UNKNOWN data received', data);
58-
this._onDidExit.fire([77, 'UNKNOWN data received']);
59-
}
60-
}, err => {
61-
this._onDidExit.fire([81, err]);
62-
console.error(err);
52+
53+
const url = getWorkerBootstrapUrl(require.toUrl('../worker/extensionHostWorkerMain.js'), 'WorkerExtensionHost');
54+
const worker = new Worker(url);
55+
56+
worker.onmessage = (event) => {
57+
const { data } = event;
58+
if (!(data instanceof ArrayBuffer)) {
59+
console.warn('UNKNOWN data received', data);
60+
this._onDidExit.fire([77, 'UNKNOWN data received']);
61+
return;
6362
}
64-
);
63+
64+
emitter.fire(VSBuffer.wrap(new Uint8Array(data, 0, data.byteLength)));
65+
};
66+
67+
worker.onerror = (event) => {
68+
console.error(event.error);
69+
this._onDidExit.fire([81, event.error]);
70+
};
6571

6672
// keep for cleanup
6773
this._toDispose.add(emitter);
68-
this._toDispose.add(worker);
74+
this._toDispose.add(toDisposable(() => worker.terminate()));
6975

7076
const protocol: IMessagePassingProtocol = {
7177
onMessage: emitter.event,

src/vs/workbench/services/extensions/worker/extensionHostWorker.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { IRequestHandler } from 'vs/base/common/worker/simpleWorker';
76
import { IMessagePassingProtocol } from 'vs/base/parts/ipc/common/ipc';
87
import { VSBuffer } from 'vs/base/common/buffer';
98
import { Emitter } from 'vs/base/common/event';
@@ -38,21 +37,18 @@ const hostUtil = new class implements IHostUtils {
3837

3938
//todo@joh do not allow extensions to call postMessage and other globals...
4039

41-
class ExtensionWorker implements IRequestHandler {
42-
43-
// worker-contract
44-
readonly _requestHandlerBrand: any;
45-
readonly onmessage: (data: any) => any;
40+
class ExtensionWorker {
4641

4742
// protocol
4843
readonly protocol: IMessagePassingProtocol;
4944

50-
constructor(postMessage: (message: any, transfer?: Transferable[]) => any) {
45+
constructor() {
5146

5247
let emitter = new Emitter<VSBuffer>();
5348
let terminating = false;
5449

55-
this.onmessage = data => {
50+
onmessage = event => {
51+
const { data } = event;
5652
if (!(data instanceof ArrayBuffer)) {
5753
console.warn('UNKNOWN data received', data);
5854
return;
@@ -98,8 +94,8 @@ function connectToRenderer(protocol: IMessagePassingProtocol): Promise<IRenderer
9894
});
9995
}
10096

101-
export function create(postMessage: (message: any, transfer?: Transferable[]) => any): IRequestHandler {
102-
const res = new ExtensionWorker(postMessage);
97+
(function create(): void {
98+
const res = new ExtensionWorker();
10399

104100
connectToRenderer(res.protocol).then(data => {
105101

@@ -112,6 +108,4 @@ export function create(postMessage: (message: any, transfer?: Transferable[]) =>
112108

113109
onTerminate = () => extHostMain.terminate();
114110
});
115-
116-
return res;
117-
}
111+
})();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
(function () {
7+
8+
let MonacoEnvironment = (<any>self).MonacoEnvironment;
9+
let monacoBaseUrl = MonacoEnvironment && MonacoEnvironment.baseUrl ? MonacoEnvironment.baseUrl : '../../../../../';
10+
11+
if (typeof (<any>self).define !== 'function' || !(<any>self).define.amd) {
12+
importScripts(monacoBaseUrl + 'vs/loader.js');
13+
}
14+
15+
require.config({
16+
baseUrl: monacoBaseUrl,
17+
catchError: true
18+
});
19+
20+
require(['vs/workbench/services/extensions/worker/extensionHostWorker'], () => { }, err => console.error(err));
21+
})();

0 commit comments

Comments
 (0)