Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Ludicrous
Path: blob/main/corrosion/lib/browser/worker.js
1223 views
1
function createWorkerRewriter(ctx = {}) {
2
return function rewriteWorker() {
3
if (ctx.window.Worker) {
4
ctx.window.Worker = new Proxy(ctx.window.Worker, {
5
construct: (target, args) => {
6
if (args[0]) {
7
if (args[0].trim().startsWith(`blob:${ctx.window.location.origin}`)) {
8
const xhr = new ctx.originalXhr();
9
xhr.open('GET', args[0], false);
10
xhr.send();
11
const script = ctx.js.process(xhr.responseText, ctx.location.origin + args[0].trim().slice(`blob:${ctx.window.location.origin}`.length));
12
const blob = new Blob([ script ], { type: 'application/javascript' });
13
args[0] = URL.createObjectURL(blob);
14
} else {
15
args[0] = ctx.url.wrap(args[0], ctx.meta);
16
};
17
};
18
return Reflect.construct(target, args);
19
},
20
});
21
};
22
if (ctx.serviceWorker && ctx.window.importScripts) {
23
ctx.window.importScripts = new Proxy(ctx.window.importScripts, {
24
apply: (target, that, args) => {
25
if (args[0]) args[0] = ctx.url.wrap(args[0], ctx.meta);
26
return Reflect.apply(target, that, args);
27
},
28
});
29
};
30
};
31
};
32
33
module.exports = createWorkerRewriter;
34