From e502dc33eaad62c990dffd95ef49f816858e39f4 Mon Sep 17 00:00:00 2001 From: David Reed Date: Wed, 23 Nov 2022 13:34:38 -0500 Subject: [PATCH] export UVClient separate --- src/rewrite/index.js | 6 +----- src/rewrite/rewrite.html.js | 14 ++++++++++++++ src/uv.config.js | 1 + src/uv.handler.js | 23 ++++++++++++++++------- src/uv.sw.js | 7 ++++--- webpack.config.js | 3 +++ 6 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/rewrite/index.js b/src/rewrite/index.js index 1b8f82d..7508df1 100644 --- a/src/rewrite/index.js +++ b/src/rewrite/index.js @@ -34,7 +34,6 @@ import { } from './rewrite.script.js'; import { openDB } from 'idb'; import parsel from './parsel.js'; -import UVClient from '../client/index.js'; import BareClient from '@tomphttp/bare-client'; import EventEmitter from 'events'; @@ -53,6 +52,7 @@ class Ultraviolet { this.meta.origin ||= ''; this.bundleScript = options.bundle || '/uv.bundle.js'; this.handlerScript = options.handler || '/uv.handler.js'; + this.clientScript = options.client || '/uv.client.js'; this.configScript = options.config || '/uv.config.js'; this.meta.url ||= this.meta.base || ''; this.codec = Ultraviolet.codec; @@ -61,10 +61,6 @@ class Ultraviolet { this.js = new JS(this); this.parsel = parsel; this.openDB = this.constructor.openDB; - this.client = - typeof self !== 'undefined' - ? new UVClient(options.window || self) - : null; this.master = '__uv'; this.dataPrefix = '__uv$'; this.attributePrefix = '__uv'; diff --git a/src/rewrite/rewrite.html.js b/src/rewrite/rewrite.html.js index 43e88a8..06fb4bc 100644 --- a/src/rewrite/rewrite.html.js +++ b/src/rewrite/rewrite.html.js @@ -210,6 +210,7 @@ function injectHead(ctx) { function createInjection( handler = '/uv.handler.js', bundle = '/uv.bundle.js', + client = '/uv.client.js', config = '/uv.config.js', bareData = {}, cookies = '', @@ -251,6 +252,19 @@ function createInjection( }, ], }, + { + tagName: 'script', + nodeName: 'script', + childNodes: [], + attrs: [ + { name: 'src', value: client, skip: true }, + { + name: '__uv-script', + value: '1', + skip: true, + }, + ], + }, { tagName: 'script', nodeName: 'script', diff --git a/src/uv.config.js b/src/uv.config.js index dcf2c4a..9ee2a48 100644 --- a/src/uv.config.js +++ b/src/uv.config.js @@ -5,6 +5,7 @@ self.__uv$config = { encodeUrl: Ultraviolet.codec.xor.encode, decodeUrl: Ultraviolet.codec.xor.decode, handler: '/uv.handler.js', + client: '/uv.client.js', bundle: '/uv.bundle.js', config: '/uv.config.js', sw: '/uv.sw.js', diff --git a/src/uv.handler.js b/src/uv.handler.js index 1cf919a..64bf3bb 100644 --- a/src/uv.handler.js +++ b/src/uv.handler.js @@ -1,12 +1,21 @@ /** - * @typedef {typeof import("./rewrite/index.js").default} Ultraviolet + * @typedef {import("./rewrite/index.js").default} Ultraviolet */ /** - * @type {typeof import("./rewrite/index.js").default} + * @typedef {import("./client/index.js").default} UVClient + */ + +/** + * @type {typeof Ultraviolet} */ const Ultraviolet = globalThis.Ultraviolet; +/** + * @type {typeof UVClient} + */ +const UVClient = globalThis.UVClient; + if (!self.__uv) { __uvHook(self, self.__uv$config, self.__uv$config.bare); } @@ -23,16 +32,13 @@ function __uvHook(window, config = {}, bare = '/bare/') { const worker = !window.window; const master = '__uv'; const methodPrefix = '__uv$'; - const __uv = new Ultraviolet({ - ...config, - window, - }); + const __uv = new Ultraviolet(config); if (typeof config.construct === 'function') { config.construct(__uv, worker ? 'worker' : 'window'); } - const { client } = __uv; + const client = new UVClient(window); const { HTMLMediaElement, HTMLScriptElement, @@ -538,6 +544,7 @@ function __uvHook(window, config = {}, bare = '/bare/') { injectHead: __uv.createHtmlInject( __uv.handlerScript, __uv.bundleScript, + __uv.clientScript, __uv.configScript, __uv.bareData, __uv.cookieStr, @@ -742,6 +749,7 @@ function __uvHook(window, config = {}, bare = '/bare/') { injectHead: __uv.createHtmlInject( __uv.handlerScript, __uv.bundleScript, + __uv.clientScript, __uv.configScript, __uv.bareData, __uv.cookieStr, @@ -852,6 +860,7 @@ function __uvHook(window, config = {}, bare = '/bare/') { injectHead: __uv.createHtmlInject( __uv.handlerScript, __uv.bundleScript, + __uv.clientScript, __uv.configScript, __uv.bareData, __uv.cookieStr, diff --git a/src/uv.sw.js b/src/uv.sw.js index dda5cf2..47049d5 100644 --- a/src/uv.sw.js +++ b/src/uv.sw.js @@ -3,11 +3,11 @@ // This is to allow us to produce a generic bundle with no hard-coded paths. /** - * @typedef {typeof import("./rewrite/index.js").default} Ultraviolet + * @typedef {import("./rewrite/index.js").default} Ultraviolet */ /** - * @type {typeof import("./rewrite/index.js").default} + * @type {typeof Ultraviolet} */ const Ultraviolet = globalThis.Ultraviolet; @@ -178,7 +178,7 @@ class UVServiceWorker extends Ultraviolet.EventEmitter { switch (request.destination) { case 'script': case 'worker': - responseCtx.body = `if (!self.__uv && self.importScripts) importScripts('${ultraviolet.bundleScript}', '${ultraviolet.configScript}', '${ultraviolet.handlerScript}');\n`; + responseCtx.body = `if (!self.__uv && self.importScripts) importScripts('${ultraviolet.bundleScript}', '${ultraviolet.clientScript}', '${ultraviolet.configScript}', '${ultraviolet.handlerScript}');\n`; responseCtx.body += ultraviolet.js.rewrite( await response.text() ); @@ -203,6 +203,7 @@ class UVServiceWorker extends Ultraviolet.EventEmitter { injectHead: ultraviolet.createHtmlInject( ultraviolet.handlerScript, ultraviolet.bundleScript, + ultraviolet.clientScript, ultraviolet.configScript, this.bareClient.data, ultraviolet.cookie.serialize( diff --git a/webpack.config.js b/webpack.config.js index 4a1b8da..3535e81 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -14,6 +14,9 @@ const config = { bundle: fileURLToPath( new URL('./src/rewrite/index.js', import.meta.url) ), + client: fileURLToPath( + new URL('./src/client/index.js', import.meta.url) + ), handler: fileURLToPath(new URL('./src/uv.handler.js', import.meta.url)), sw: fileURLToPath(new URL('./src/uv.sw.js', import.meta.url)), },