From 16aecc05f61532d31b92906ef8351c0c92722401 Mon Sep 17 00:00:00 2001 From: Jason <59297610+caracal-js@users.noreply.github.com> Date: Sat, 26 Feb 2022 16:35:37 -0500 Subject: [PATCH] a --- lib/uv.handler.js | 4 ++++ lib/uv.sw-handler.js | 1 + lib/uv.sw.js | 11 +++++++++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/uv.handler.js b/lib/uv.handler.js index bf06acc..e4b5c33 100644 --- a/lib/uv.handler.js +++ b/lib/uv.handler.js @@ -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, diff --git a/lib/uv.sw-handler.js b/lib/uv.sw-handler.js index 5557e6d..ffe57d1 100644 --- a/lib/uv.sw-handler.js +++ b/lib/uv.sw-handler.js @@ -1,6 +1,7 @@ importScripts('./uv.sw.js'); const sw = new UVServiceWorker(); + self.addEventListener('fetch', event => event.respondWith( sw.fetch(event) diff --git a/lib/uv.sw.js b/lib/uv.sw.js index c73ead9..8a72003 100644 --- a/lib/uv.sw.js +++ b/lib/uv.sw.js @@ -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, {