CezaryDanielNowak/CrossOriginWorker

Elaboration on `importScriptsFix` + fix for calling inside workers

Opened this issue · 0 comments

Hey, was checking out this wonderful horrible hack and I'm trying to understand what's going on here.
Could you elaborate a bit please?

const importScriptsFix = `const _importScripts = importScripts;
const _fixImports = (url) => new URL(url, '${workerPath.join('/') + '/'}').href;
importScripts = (...urls) => _importScripts(...urls.map(_fixImports));`;

I'm having an issue where a worker I'm loading with CrossOriginWorker also uses importScripts.
To make this package work I had to make the following change (will make a PR):

- originalWorkerUrl.includes(window.location.origin)
+ originalWorkerUrl.includes(self.location.href)

However, I have to use a different name for _fixImports and _importScripts in the worker scope, as I'm getting an error that these were already defined when I first used it in the main thread scope. I have no idea how those could leak over between scopes.

Is there any way to namespace those? I'd love to make a PR that fixes both of these issues, but I'm having trouble understanding what's exactly going on in the template literal.