f045a6c894dc789a187f9c3224df4fcbe7d4fee96851576d89a802b80e2620c8091a5fd844b2fba2b8757cd5f3b81587e3dbc711f4a93bec562c55d91d4f08 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. var _a;
  6. import { globals } from '../common/platform.js';
  7. import { logOnceWebWorkerWarning } from '../common/worker/simpleWorker.js';
  8. const ttPolicy = (_a = window.trustedTypes) === null || _a === void 0 ? void 0 : _a.createPolicy('defaultWorkerFactory', { createScriptURL: value => value });
  9. function getWorker(label) {
  10. // Option for hosts to overwrite the worker script (used in the standalone editor)
  11. if (globals.MonacoEnvironment) {
  12. if (typeof globals.MonacoEnvironment.getWorker === 'function') {
  13. return globals.MonacoEnvironment.getWorker('workerMain.js', label);
  14. }
  15. if (typeof globals.MonacoEnvironment.getWorkerUrl === 'function') {
  16. const workerUrl = globals.MonacoEnvironment.getWorkerUrl('workerMain.js', label);
  17. return new Worker(ttPolicy ? ttPolicy.createScriptURL(workerUrl) : workerUrl, { name: label });
  18. }
  19. }
  20. // ESM-comment-begin
  21. // if (typeof require === 'function') {
  22. // // check if the JS lives on a different origin
  23. // const workerMain = require.toUrl('vs/base/worker/workerMain.js'); // explicitly using require.toUrl(), see https://github.com/microsoft/vscode/issues/107440#issuecomment-698982321
  24. // const workerUrl = getWorkerBootstrapUrl(workerMain, label);
  25. // return new Worker(ttPolicy ? ttPolicy.createScriptURL(workerUrl) as unknown as string : workerUrl, { name: label });
  26. // }
  27. // ESM-comment-end
  28. throw new Error(`You must define a function MonacoEnvironment.getWorkerUrl or MonacoEnvironment.getWorker`);
  29. }
  30. // ESM-comment-begin
  31. // export function getWorkerBootstrapUrl(scriptPath: string, label: string): string {
  32. // if (/^((http:)|(https:)|(file:))/.test(scriptPath) && scriptPath.substring(0, self.origin.length) !== self.origin) {
  33. // // this is the cross-origin case
  34. // // i.e. the webpage is running at a different origin than where the scripts are loaded from
  35. // const myPath = 'vs/base/worker/defaultWorkerFactory.js';
  36. // const workerBaseUrl = require.toUrl(myPath).slice(0, -myPath.length); // explicitly using require.toUrl(), see https://github.com/microsoft/vscode/issues/107440#issuecomment-698982321
  37. // const js = `/*${label}*/self.MonacoEnvironment={baseUrl: '${workerBaseUrl}'};const ttPolicy = self.trustedTypes?.createPolicy('defaultWorkerFactory', { createScriptURL: value => value });importScripts(ttPolicy?.createScriptURL('${scriptPath}') ?? '${scriptPath}');/*${label}*/`;
  38. // const blob = new Blob([js], { type: 'application/javascript' });
  39. // return URL.createObjectURL(blob);
  40. // }
  41. // return scriptPath + '#' + label;
  42. // }
  43. // ESM-comment-end
  44. function isPromiseLike(obj) {
  45. if (typeof obj.then === 'function') {
  46. return true;
  47. }
  48. return false;
  49. }
  50. /**
  51. * A worker that uses HTML5 web workers so that is has
  52. * its own global scope and its own thread.
  53. */
  54. class WebWorker {
  55. constructor(moduleId, id, label, onMessageCallback, onErrorCallback) {
  56. this.id = id;
  57. const workerOrPromise = getWorker(label);
  58. if (isPromiseLike(workerOrPromise)) {
  59. this.worker = workerOrPromise;
  60. }
  61. else {
  62. this.worker = Promise.resolve(workerOrPromise);
  63. }
  64. this.postMessage(moduleId, []);
  65. this.worker.then((w) => {
  66. w.onmessage = function (ev) {
  67. onMessageCallback(ev.data);
  68. };
  69. w.onmessageerror = onErrorCallback;
  70. if (typeof w.addEventListener === 'function') {
  71. w.addEventListener('error', onErrorCallback);
  72. }
  73. });
  74. }
  75. getId() {
  76. return this.id;
  77. }
  78. postMessage(message, transfer) {
  79. var _a;
  80. (_a = this.worker) === null || _a === void 0 ? void 0 : _a.then(w => w.postMessage(message, transfer));
  81. }
  82. dispose() {
  83. var _a;
  84. (_a = this.worker) === null || _a === void 0 ? void 0 : _a.then(w => w.terminate());
  85. this.worker = null;
  86. }
  87. }
  88. export class DefaultWorkerFactory {
  89. constructor(label) {
  90. this._label = label;
  91. this._webWorkerFailedBeforeError = false;
  92. }
  93. create(moduleId, onMessageCallback, onErrorCallback) {
  94. const workerId = (++DefaultWorkerFactory.LAST_WORKER_ID);
  95. if (this._webWorkerFailedBeforeError) {
  96. throw this._webWorkerFailedBeforeError;
  97. }
  98. return new WebWorker(moduleId, workerId, this._label || 'anonymous' + workerId, onMessageCallback, (err) => {
  99. logOnceWebWorkerWarning(err);
  100. this._webWorkerFailedBeforeError = err;
  101. onErrorCallback(err);
  102. });
  103. }
  104. }
  105. DefaultWorkerFactory.LAST_WORKER_ID = 0;