Skip to content

Commit

Permalink
fix dumb regressiobn
Browse files Browse the repository at this point in the history
  • Loading branch information
Percslol committed Aug 27, 2024
1 parent 074efd6 commit b243e43
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 43 deletions.
19 changes: 4 additions & 15 deletions src/client/worker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import EventEmitter from 'events';
import HookEvent from './hook.js';
import { BareMuxConnection } from "@mercuryworkshop/bare-mux";

/**
* @typedef {import('./index').default} UVClient
Expand Down Expand Up @@ -34,20 +33,10 @@ class Workers extends EventEmitter {
const event = new HookEvent({ url, options }, target, that);
this.emit('worker', event);

let worker;
if (event.intercepted) {
worker = event.returnValue;
} else {
worker = new event.target(
...[event.data.url, event.data.options]
);
}
const conn = new BareMuxConnection();
(async ()=>{
const port = await conn.getInnerPort();
worker.postMessage(port, [port]);
})();
return worker;
if (event.intercepted) return event.returnValue;
return new event.target(
...[event.data.url, event.data.options]
);
},
true
);
Expand Down
23 changes: 4 additions & 19 deletions src/uv.handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,12 @@ if (!self.__uv) __uvHook(self);

self.__uvHook = __uvHook;

function __uvHook(window) {
if (!window.window) {
window.__uv$promise = new Promise(resolve => {
window.onmessage = (e)=>{
if (e.data.__data instanceof MessagePort) {
__uvHookReal(window, new Ultraviolet.BareClient(e.data.__data));
delete window.onmessage;
resolve();
} else {
throw new Error("unreachable: e.data !== MessagePort");
}
}
});
} else {
__uvHookReal(window, new Ultraviolet.BareClient());
}
}

/**
*
* @param {typeof globalThis} window
* @returns
*/
function __uvHookReal(window, bareClient) {
function __uvHook(window) {
if ('__uv' in window && window.__uv instanceof Ultraviolet) return false;

if (window.document && !!window.window) {
Expand All @@ -68,6 +50,9 @@ function __uvHookReal(window, bareClient) {
/*if (typeof config.construct === 'function') {
config.construct(__uv, worker ? 'worker' : 'window');
}*/

// websockets
const bareClient = new Ultraviolet.BareClient();

const client = new UVClient(window, bareClient, worker);
const {
Expand Down
20 changes: 11 additions & 9 deletions src/uv.sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,20 @@ class UVServiceWorker extends Ultraviolet.EventEmitter {
]
.map((script) => JSON.stringify(script))
.join(',');
responseCtx.body = `(async ()=>{${ultraviolet.createJsInject(
ultraviolet.cookie.serialize(
cookies,
ultraviolet.meta,
true
),
request.referrer
)} importScripts(${scripts}); await __uv$promise;\n`;
responseCtx.body = `if (!self.__uv) {
${ultraviolet.createJsInject(
ultraviolet.cookie.serialize(
cookies,
ultraviolet.meta,
true
),
request.referrer
)}
importScripts(${scripts});
}\n`;
responseCtx.body += ultraviolet.js.rewrite(
await response.text()
);
responseCtx.body += "\n})()";
}
break;
case 'style':
Expand Down

0 comments on commit b243e43

Please sign in to comment.