diff --git a/package-lock.json b/package-lock.json index ba51b218..e9933126 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "3.2.0", "license": "MIT", "devDependencies": { - "@mercuryworkshop/bare-mux": "^2.0.0", + "@mercuryworkshop/bare-mux": "file:../bare-mux", "css-tree": "^2.3.1", "esbuild": "^0.18.11", "eslint": "^8.28.0", diff --git a/src/client/worker.js b/src/client/worker.js index f1dd84e1..fa9f5343 100644 --- a/src/client/worker.js +++ b/src/client/worker.js @@ -1,5 +1,6 @@ import EventEmitter from 'events'; import HookEvent from './hook.js'; +import { BareMuxConnection } from "@mercuryworkshop/bare-mux"; /** * @typedef {import('./index').default} UVClient @@ -33,10 +34,20 @@ class Workers extends EventEmitter { const event = new HookEvent({ url, options }, target, that); this.emit('worker', event); - if (event.intercepted) return event.returnValue; - return new event.target( - ...[event.data.url, event.data.options] - ); + 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; }, true ); diff --git a/src/uv.handler.js b/src/uv.handler.js index 21e56be7..eb13da7b 100644 --- a/src/uv.handler.js +++ b/src/uv.handler.js @@ -28,12 +28,30 @@ 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 __uvHook(window) { +function __uvHookReal(window, bareClient) { if ('__uv' in window && window.__uv instanceof Ultraviolet) return false; if (window.document && !!window.window) { @@ -51,9 +69,6 @@ function __uvHook(window) { config.construct(__uv, worker ? 'worker' : 'window'); }*/ - // websockets - const bareClient = new Ultraviolet.BareClient(); - const client = new UVClient(window, bareClient, worker); const { HTMLMediaElement, diff --git a/src/uv.sw.js b/src/uv.sw.js index 8aeedfad..ab2313b2 100644 --- a/src/uv.sw.js +++ b/src/uv.sw.js @@ -211,17 +211,18 @@ class UVServiceWorker extends Ultraviolet.EventEmitter { ] .map((script) => JSON.stringify(script)) .join(','); - responseCtx.body = `if (!self.__uv && self.importScripts) { ${ultraviolet.createJsInject( + responseCtx.body = `(async ()=>{if (!self.__uv && self.importScripts) { ${ultraviolet.createJsInject( ultraviolet.cookie.serialize( cookies, ultraviolet.meta, true ), request.referrer - )} importScripts(${scripts}); }\n`; + )} importScripts(${scripts}); await __uv$promise;}\n`; responseCtx.body += ultraviolet.js.rewrite( await response.text() ); + responseCtx.body += "\n})()"; } break; case 'style':