export UVClient separate

This commit is contained in:
David Reed 2022-11-23 13:34:38 -05:00
parent fddd5cd698
commit e502dc33ea
No known key found for this signature in database
GPG key ID: 2211691D8A1EE72F
6 changed files with 39 additions and 15 deletions

View file

@ -34,7 +34,6 @@ import {
} from './rewrite.script.js'; } from './rewrite.script.js';
import { openDB } from 'idb'; import { openDB } from 'idb';
import parsel from './parsel.js'; import parsel from './parsel.js';
import UVClient from '../client/index.js';
import BareClient from '@tomphttp/bare-client'; import BareClient from '@tomphttp/bare-client';
import EventEmitter from 'events'; import EventEmitter from 'events';
@ -53,6 +52,7 @@ class Ultraviolet {
this.meta.origin ||= ''; this.meta.origin ||= '';
this.bundleScript = options.bundle || '/uv.bundle.js'; this.bundleScript = options.bundle || '/uv.bundle.js';
this.handlerScript = options.handler || '/uv.handler.js'; this.handlerScript = options.handler || '/uv.handler.js';
this.clientScript = options.client || '/uv.client.js';
this.configScript = options.config || '/uv.config.js'; this.configScript = options.config || '/uv.config.js';
this.meta.url ||= this.meta.base || ''; this.meta.url ||= this.meta.base || '';
this.codec = Ultraviolet.codec; this.codec = Ultraviolet.codec;
@ -61,10 +61,6 @@ class Ultraviolet {
this.js = new JS(this); this.js = new JS(this);
this.parsel = parsel; this.parsel = parsel;
this.openDB = this.constructor.openDB; this.openDB = this.constructor.openDB;
this.client =
typeof self !== 'undefined'
? new UVClient(options.window || self)
: null;
this.master = '__uv'; this.master = '__uv';
this.dataPrefix = '__uv$'; this.dataPrefix = '__uv$';
this.attributePrefix = '__uv'; this.attributePrefix = '__uv';

View file

@ -210,6 +210,7 @@ function injectHead(ctx) {
function createInjection( function createInjection(
handler = '/uv.handler.js', handler = '/uv.handler.js',
bundle = '/uv.bundle.js', bundle = '/uv.bundle.js',
client = '/uv.client.js',
config = '/uv.config.js', config = '/uv.config.js',
bareData = {}, bareData = {},
cookies = '', 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', tagName: 'script',
nodeName: 'script', nodeName: 'script',

View file

@ -5,6 +5,7 @@ self.__uv$config = {
encodeUrl: Ultraviolet.codec.xor.encode, encodeUrl: Ultraviolet.codec.xor.encode,
decodeUrl: Ultraviolet.codec.xor.decode, decodeUrl: Ultraviolet.codec.xor.decode,
handler: '/uv.handler.js', handler: '/uv.handler.js',
client: '/uv.client.js',
bundle: '/uv.bundle.js', bundle: '/uv.bundle.js',
config: '/uv.config.js', config: '/uv.config.js',
sw: '/uv.sw.js', sw: '/uv.sw.js',

View file

@ -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; const Ultraviolet = globalThis.Ultraviolet;
/**
* @type {typeof UVClient}
*/
const UVClient = globalThis.UVClient;
if (!self.__uv) { if (!self.__uv) {
__uvHook(self, self.__uv$config, self.__uv$config.bare); __uvHook(self, self.__uv$config, self.__uv$config.bare);
} }
@ -23,16 +32,13 @@ function __uvHook(window, config = {}, bare = '/bare/') {
const worker = !window.window; const worker = !window.window;
const master = '__uv'; const master = '__uv';
const methodPrefix = '__uv$'; const methodPrefix = '__uv$';
const __uv = new Ultraviolet({ const __uv = new Ultraviolet(config);
...config,
window,
});
if (typeof config.construct === 'function') { if (typeof config.construct === 'function') {
config.construct(__uv, worker ? 'worker' : 'window'); config.construct(__uv, worker ? 'worker' : 'window');
} }
const { client } = __uv; const client = new UVClient(window);
const { const {
HTMLMediaElement, HTMLMediaElement,
HTMLScriptElement, HTMLScriptElement,
@ -538,6 +544,7 @@ function __uvHook(window, config = {}, bare = '/bare/') {
injectHead: __uv.createHtmlInject( injectHead: __uv.createHtmlInject(
__uv.handlerScript, __uv.handlerScript,
__uv.bundleScript, __uv.bundleScript,
__uv.clientScript,
__uv.configScript, __uv.configScript,
__uv.bareData, __uv.bareData,
__uv.cookieStr, __uv.cookieStr,
@ -742,6 +749,7 @@ function __uvHook(window, config = {}, bare = '/bare/') {
injectHead: __uv.createHtmlInject( injectHead: __uv.createHtmlInject(
__uv.handlerScript, __uv.handlerScript,
__uv.bundleScript, __uv.bundleScript,
__uv.clientScript,
__uv.configScript, __uv.configScript,
__uv.bareData, __uv.bareData,
__uv.cookieStr, __uv.cookieStr,
@ -852,6 +860,7 @@ function __uvHook(window, config = {}, bare = '/bare/') {
injectHead: __uv.createHtmlInject( injectHead: __uv.createHtmlInject(
__uv.handlerScript, __uv.handlerScript,
__uv.bundleScript, __uv.bundleScript,
__uv.clientScript,
__uv.configScript, __uv.configScript,
__uv.bareData, __uv.bareData,
__uv.cookieStr, __uv.cookieStr,

View file

@ -3,11 +3,11 @@
// This is to allow us to produce a generic bundle with no hard-coded paths. // 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; const Ultraviolet = globalThis.Ultraviolet;
@ -178,7 +178,7 @@ class UVServiceWorker extends Ultraviolet.EventEmitter {
switch (request.destination) { switch (request.destination) {
case 'script': case 'script':
case 'worker': 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( responseCtx.body += ultraviolet.js.rewrite(
await response.text() await response.text()
); );
@ -203,6 +203,7 @@ class UVServiceWorker extends Ultraviolet.EventEmitter {
injectHead: ultraviolet.createHtmlInject( injectHead: ultraviolet.createHtmlInject(
ultraviolet.handlerScript, ultraviolet.handlerScript,
ultraviolet.bundleScript, ultraviolet.bundleScript,
ultraviolet.clientScript,
ultraviolet.configScript, ultraviolet.configScript,
this.bareClient.data, this.bareClient.data,
ultraviolet.cookie.serialize( ultraviolet.cookie.serialize(

View file

@ -14,6 +14,9 @@ const config = {
bundle: fileURLToPath( bundle: fileURLToPath(
new URL('./src/rewrite/index.js', import.meta.url) 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)), handler: fileURLToPath(new URL('./src/uv.handler.js', import.meta.url)),
sw: fileURLToPath(new URL('./src/uv.sw.js', import.meta.url)), sw: fileURLToPath(new URL('./src/uv.sw.js', import.meta.url)),
}, },