Skip to content

Commit

Permalink
Feature/browser compatibility (#307)
Browse files Browse the repository at this point in the history
* fix: compatible with chrome 86

* fix: upgrade vscode-web version

* chore: fix indent whitespace
  • Loading branch information
conwnet authored May 24, 2021
1 parent 63bb55b commit 344e0b3
Show file tree
Hide file tree
Showing 9 changed files with 2,343 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"lib": "lib"
},
"devDependencies": {
"@github1s/vscode-web": "0.1.7",
"@github1s/vscode-web": "0.1.8",
"@typescript-eslint/eslint-plugin": "^4.15.0",
"@typescript-eslint/parser": "^4.15.0",
"chokidar": "^3.5.1",
Expand Down
3 changes: 2 additions & 1 deletion vscode-web-github1s/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@github1s/vscode-web",
"version": "0.1.7",
"version": "0.1.8",
"description": "VS Code web for GitHub1s",
"author": "github1s",
"license": "MIT",
Expand Down Expand Up @@ -38,6 +38,7 @@
"xterm-addon-webgl": "0.10.0-beta.1"
},
"devDependencies": {
"@types/trusted-types": "^2.0.0",
"npm-run-all": "^4.1.5"
}
}
101 changes: 101 additions & 0 deletions vscode-web-github1s/src/vs/base/worker/workerMain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

(function () {

const MonacoEnvironment = (<any>self).MonacoEnvironment;
const monacoBaseUrl = MonacoEnvironment && MonacoEnvironment.baseUrl ? MonacoEnvironment.baseUrl : '../../../';

const trustedTypesPolicy = (
typeof self.trustedTypes?.createPolicy === 'function'
? self.trustedTypes?.createPolicy('amdLoader', {
createScriptURL: value => value,
createScript: (_, ...args: string[]) => {
// workaround a chrome issue not allowing to create new functions
// see https://github.com/w3c/webappsec-trusted-types/wiki/Trusted-Types-for-function-constructor
const fnArgs = args.slice(0, -1).join(',');
const fnBody = args.pop()!.toString();
const body = `(function anonymous(${fnArgs}) {\n${fnBody}\n})`;
return body;
}
})
: undefined
);

function loadAMDLoader() {
return new Promise<void>((resolve, reject) => {
if (typeof (<any>self).define === 'function' && (<any>self).define.amd) {
return resolve();
}
const loaderSrc: string | TrustedScriptURL = monacoBaseUrl + 'vs/loader.js';

const isCrossOrigin = (/^((http:)|(https:)|(file:))/.test(loaderSrc) && loaderSrc.substring(0, self.origin.length) !== self.origin);
if (!isCrossOrigin) {
// use `fetch` if possible because `importScripts`
// is synchronous and can lead to deadlocks on Safari
fetch(loaderSrc).then((response) => {
if (response.status !== 200) {
throw new Error(response.statusText);
}
return response.text();
}).then((text) => {
text = `${text}\n//# sourceURL=${loaderSrc}`;
const func = (
trustedTypesPolicy
// below codes are changed by github1s
// fix error in webworker for old browsers
? self.eval(trustedTypesPolicy.createScript('', text).toString())
// above codes are changed by github1s
: new Function(text)
);
func.call(self);
resolve();
}).then(undefined, reject);
return;
}

if (trustedTypesPolicy) {
importScripts(trustedTypesPolicy.createScriptURL(loaderSrc) as unknown as string);
} else {
importScripts(loaderSrc as string);
}
resolve();
});
}

const loadCode = function (moduleId: string) {
loadAMDLoader().then(() => {
require.config({
baseUrl: monacoBaseUrl,
catchError: true,
trustedTypesPolicy,
});
require([moduleId], function (ws) {
setTimeout(function () {
let messageHandler = ws.create((msg: any, transfer?: Transferable[]) => {
(<any>self).postMessage(msg, transfer);
}, null);

self.onmessage = (e: MessageEvent) => messageHandler.onmessage(e.data);
while (beforeReadyMessages.length > 0) {
self.onmessage(beforeReadyMessages.shift()!);
}
}, 0);
});
});
};

let isFirstMessage = true;
let beforeReadyMessages: MessageEvent[] = [];
self.onmessage = (message: MessageEvent) => {
if (!isFirstMessage) {
beforeReadyMessages.push(message);
return;
}

isFirstMessage = false;
loadCode(message.data);
};
})();
Loading

0 comments on commit 344e0b3

Please sign in to comment.