Skip to content

Commit

Permalink
add polyfill for workers
Browse files Browse the repository at this point in the history
  • Loading branch information
eokoneyo committed Mar 14, 2024
1 parent 1f22027 commit 4e4159e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,6 @@
"remark-gfm": "1.0.0",
"remark-parse-no-trim": "^8.0.4",
"remark-stringify": "^8.0.3",
"remote-web-worker": "^0.0.9",
"require-in-the-middle": "^7.2.1",
"reselect": "^4.1.8",
"resize-observer-polyfill": "1.5.1",
Expand Down
1 change: 0 additions & 1 deletion packages/kbn-monaco/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ SHARED_DEPS = [
"@npm//monaco-editor",
"@npm//monaco-yaml",
"@npm//js-levenshtein",
"@npm//remote-web-worker",
]

webpack_cli(
Expand Down
1 change: 0 additions & 1 deletion packages/kbn-monaco/src/register_globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* Side Public License, v 1.
*/

import 'remote-web-worker';
import { XJsonLang } from './xjson';
import { PainlessLang } from './painless';
import { SQLLang } from './sql';
Expand Down
42 changes: 42 additions & 0 deletions packages/kbn-ui-shared-deps-src/src/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,45 @@ if (typeof window.Event === 'object') {

require('whatwg-fetch');
require('symbol-observable');

/**
* @description Adapted from https://github.com/jantimon/remote-web-worker/blob/main/src/index.ts
* with a modification to check that the requested worker url isn't already a blob, because of how workers are loaded for ace editor
*/
typeof window !== 'undefined' &&
// eslint-disable-next-line no-global-assign
(Worker = ((BaseWorker) =>
class Worker extends BaseWorker {
constructor(scriptURL, options) {
const url = String(scriptURL);
super(
// Check if the URL is remote
url.includes('://') && !url.startsWith(window.location.origin) && !url.startsWith('blob:') // to bootstrap the actual script to work around the same origin policy.
? URL.createObjectURL(
new Blob(
[
// Replace the `importScripts` function with
// a patched version that will resolve relative URLs
// to the remote script URL.
//
// Without a patched `importScripts` Webpack 5 generated worker chunks will fail with the following error:
//
// Uncaught (in promise) DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope':
// The script at 'http://some.domain/worker.1e0e1e0e.js' failed to load.
//
// For minification, the inlined variable names are single letters:
// i = original importScripts
// a = arguments
// u = URL
`importScripts=((i)=>(...a)=>i(...a.map((u)=>''+new URL(u,"${url}"))))(importScripts);importScripts("${url}")`,
],
{
type: 'text/javascript',
}
)
)
: scriptURL,
options
);
}
})(Worker));
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -26958,11 +26958,6 @@ remark-stringify@^8.0.3:
unherit "^1.0.4"
xtend "^4.0.1"

remote-web-worker@^0.0.9:
version "0.0.9"
resolved "https://registry.yarnpkg.com/remote-web-worker/-/remote-web-worker-0.0.9.tgz#3a96a84063e6c1a5fa3792abe0fd97eb7a97aab3"
integrity sha512-TguJhQVWeeL2zWqLQpTfq/uIOpXRHHq7SI6fw6xTIJfSN/ojiKUqV16oAhPD8zH71MFy3FHqSoQEx2cks5gcAA==

remove-accents@0.4.2:
version "0.4.2"
resolved "https://registry.yarnpkg.com/remove-accents/-/remove-accents-0.4.2.tgz#0a43d3aaae1e80db919e07ae254b285d9e1c7bb5"
Expand Down

0 comments on commit 4e4159e

Please sign in to comment.