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