Skip to content

Commit

Permalink
start intercepting workers bare-clients with 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Percslol committed Jul 11, 2024
1 parent 7751e9e commit b56c840
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions src/client/worker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import EventEmitter from 'events';
import HookEvent from './hook.js';
import { BareMuxConnection } from "@mercuryworkshop/bare-mux";

/**
* @typedef {import('./index').default} UVClient
Expand Down Expand Up @@ -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
);
Expand Down
23 changes: 19 additions & 4 deletions src/uv.handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/uv.sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down

0 comments on commit b56c840

Please sign in to comment.