This commit is contained in:
Jason 2022-02-26 16:35:37 -05:00
parent 070295ab3b
commit 16aecc05f6
3 changed files with 14 additions and 2 deletions

View file

@ -17,6 +17,10 @@ async function __uvHook(window, config = {}, bare = '/bare/') {
window,
});
if (typeof config.construct === 'function') {
config.construct(__uv, worker ? 'worker' : 'window');
};
const { client } = __uv;
const {
HTMLMediaElement,

View file

@ -1,6 +1,7 @@
importScripts('./uv.sw.js');
const sw = new UVServiceWorker();
self.addEventListener('fetch', event =>
event.respondWith(
sw.fetch(event)

View file

@ -54,6 +54,11 @@ class UVServiceWorker extends EventEmitter {
try {
const ultraviolet = new Ultraviolet(this.config);
if (typeof this.config.construct === 'function') {
this.config.construct(ultraviolet, 'service');
};
const db = await ultraviolet.cookie.db();
ultraviolet.meta.origin = location.origin;
@ -105,6 +110,10 @@ class UVServiceWorker extends EventEmitter {
};
const responseCtx = new ResponseContext(requestCtx, response, this);
const resEvent = new HookEvent(responseCtx, null, null);
this.emit('beforemod', resEvent);
if (resEvent.intercepted) return resEvent.returnValue;
for (const name of this.headers.csp) {
if (responseCtx.headers[name]) delete responseCtx.headers[name];
@ -166,9 +175,7 @@ class UVServiceWorker extends EventEmitter {
responseCtx.headers['content-type'] = 'text/event-stream';
};
const resEvent = new HookEvent(responseCtx, null, null);
this.emit('response', resEvent);
if (resEvent.intercepted) return resEvent.returnValue;
return new Response(responseCtx.body, {