Skip to content

Commit

Permalink
wip(remote-worker): use remote worker
Browse files Browse the repository at this point in the history
Try to use remote worker to load changes

Signed-off-by: Santiago Jimenez Giraldo <[email protected]>
  • Loading branch information
sago2k8 committed Oct 16, 2024
1 parent d7bfc4f commit b3728d2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
8 changes: 8 additions & 0 deletions frontend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { monacoYamlOptions } from './components/misc/PipelinesYamlEditor';
import * as monaco from 'monaco-editor';
import { loader, Monaco } from '@monaco-editor/react';
import { LicenseService } from './protogen/redpanda/api/console/v1alpha1/license_connect';
import './remoteWebWorker';

declare const __webpack_public_path__: string;

Expand Down Expand Up @@ -212,6 +213,13 @@ export const setup = memoizeOne((setupArgs: SetConfigArguments) => {

// Ensure yaml workers are being loaded locally as well
loader.init().then(async (monaco) => {
// const jsonWorker = await new CorsWorker(new URL('monaco-editor/esm/vs/language/json/json.worker', import.meta.url)).createWorker();
// const yamlWorker = await new CorsWorker(new URL('monaco-yaml/yaml.worker', import.meta.url)).createWorker();
// const tsWorker = await new CorsWorker(
// new URL('monaco-editor/esm/vs/language/typescript/ts.worker', import.meta.url)
// ).createWorker();
//
// const genericWorker = await new CorsWorker(new URL('monaco-editor/esm/vs/editor/editor.worker', import.meta.url)).createWorker();
// eslint-disable-next-line no-restricted-globals
window.MonacoEnvironment = {
getWorker(moduleId, label) {
Expand Down
55 changes: 55 additions & 0 deletions frontend/src/remoteWebWorker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Patch Worker to allow loading scripts from remote URLs
//
// It's a workaround for the fact that the Worker constructor
// accepts only local URLs, not remote URLs:
// https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker
//
// As a workaround this patched Worker constructor will
// use `importScripts` to load the remote script.
// https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts
//
// Compatibility: Chrome 4+, Firefox 4+, Safari 4+
//
// Usage:
// ```ts
// import "remote-web-worker";
// ```
typeof window !== 'undefined' &&
(Worker = ((BaseWorker: typeof Worker) =>

Check warning on line 18 in frontend/src/remoteWebWorker.ts

View workflow job for this annotation

GitHub Actions / lint-and-compile

Read-only global 'Worker' should not be modified

Check warning on line 18 in frontend/src/remoteWebWorker.ts

View workflow job for this annotation

GitHub Actions / lint-and-compile

Read-only global 'Worker' should not be modified
// eslint-disable-next-line no-global-assign
class Worker extends BaseWorker {
constructor(scriptURL: string | URL, options?: WorkerOptions) {
const url = String(scriptURL);
super(
// Check if the URL is remote
url.includes('://') && !url.startsWith(window.location.origin)
? // Launch the worker with an inline script that will use `importScripts`
// 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));

export {};

0 comments on commit b3728d2

Please sign in to comment.