mirror of
https://github.com/titaniumnetwork-dev/Ultraviolet.git
synced 2025-05-17 05:20:01 -04:00
clean up service worker code, remove useless variable
This commit is contained in:
parent
ab4e2f91c0
commit
cb717c95ad
4 changed files with 15 additions and 44 deletions
|
@ -209,16 +209,12 @@ export function injectHead(ctx) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createJsInject(
|
export function createJsInject(
|
||||||
bareURL = '',
|
|
||||||
bareData = {},
|
|
||||||
cookies = '',
|
cookies = '',
|
||||||
referrer = ''
|
referrer = ''
|
||||||
) {
|
) {
|
||||||
return (
|
return (
|
||||||
`self.__uv$bareData = ${JSON.stringify(bareData)};` +
|
|
||||||
`self.__uv$cookies = ${JSON.stringify(cookies)};` +
|
`self.__uv$cookies = ${JSON.stringify(cookies)};` +
|
||||||
`self.__uv$referrer = ${JSON.stringify(referrer)};` +
|
`self.__uv$referrer = ${JSON.stringify(referrer)};`
|
||||||
`self.__uv$bareURL = ${JSON.stringify(bareURL)}; `
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -227,8 +223,6 @@ export function createHtmlInject(
|
||||||
bundleScript,
|
bundleScript,
|
||||||
clientScript,
|
clientScript,
|
||||||
configScript,
|
configScript,
|
||||||
bareURL,
|
|
||||||
bareData,
|
|
||||||
cookies,
|
cookies,
|
||||||
referrer
|
referrer
|
||||||
) {
|
) {
|
||||||
|
@ -239,7 +233,7 @@ export function createHtmlInject(
|
||||||
childNodes: [
|
childNodes: [
|
||||||
{
|
{
|
||||||
nodeName: '#text',
|
nodeName: '#text',
|
||||||
value: createJsInject(bareURL, bareData, cookies, referrer),
|
value: createJsInject(cookies, referrer),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
attrs: [
|
attrs: [
|
||||||
|
|
|
@ -14,7 +14,7 @@ const uv = new UVServiceWorker();
|
||||||
self.addEventListener('fetch', event => {
|
self.addEventListener('fetch', event => {
|
||||||
event.respondWith(
|
event.respondWith(
|
||||||
(async ()=>{
|
(async ()=>{
|
||||||
if(event.request.url.startsWith(location.origin + __uv$config.prefix)) {
|
if (uv.route(event)) {
|
||||||
return await uv.fetch(event);
|
return await uv.fetch(event);
|
||||||
}
|
}
|
||||||
return await fetch(event.request);
|
return await fetch(event.request);
|
||||||
|
|
|
@ -13,15 +13,6 @@ const UVClient = self.UVClient;
|
||||||
*/
|
*/
|
||||||
const __uv$config = self.__uv$config;
|
const __uv$config = self.__uv$config;
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {import('@mercuryworkshop/bare-mux').BareManifest}
|
|
||||||
*/
|
|
||||||
const __uv$bareData = self.__uv$bareData;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {string}
|
|
||||||
*/
|
|
||||||
const __uv$bareURL = self.__uv$bareURL;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {string}
|
* @type {string}
|
||||||
|
@ -29,8 +20,6 @@ const __uv$bareURL = self.__uv$bareURL;
|
||||||
const __uv$cookies = self.__uv$cookies;
|
const __uv$cookies = self.__uv$cookies;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
typeof __uv$bareData !== 'object' ||
|
|
||||||
typeof __uv$bareURL !== 'string' ||
|
|
||||||
typeof __uv$cookies !== 'string'
|
typeof __uv$cookies !== 'string'
|
||||||
)
|
)
|
||||||
throw new TypeError('Unable to load global UV data');
|
throw new TypeError('Unable to load global UV data');
|
||||||
|
@ -572,8 +561,6 @@ function __uvHook(window) {
|
||||||
__uv.bundleScript,
|
__uv.bundleScript,
|
||||||
__uv.clientScript,
|
__uv.clientScript,
|
||||||
__uv.configScript,
|
__uv.configScript,
|
||||||
__uv$bareURL,
|
|
||||||
__uv$bareData,
|
|
||||||
cookieStr,
|
cookieStr,
|
||||||
window.location.href
|
window.location.href
|
||||||
),
|
),
|
||||||
|
@ -780,8 +767,6 @@ function __uvHook(window) {
|
||||||
__uv.bundleScript,
|
__uv.bundleScript,
|
||||||
__uv.clientScript,
|
__uv.clientScript,
|
||||||
__uv.configScript,
|
__uv.configScript,
|
||||||
__uv$bareURL,
|
|
||||||
__uv$bareData,
|
|
||||||
cookieStr,
|
cookieStr,
|
||||||
window.location.href
|
window.location.href
|
||||||
),
|
),
|
||||||
|
@ -892,8 +877,6 @@ function __uvHook(window) {
|
||||||
__uv.bundleScript,
|
__uv.bundleScript,
|
||||||
__uv.clientScript,
|
__uv.clientScript,
|
||||||
__uv.configScript,
|
__uv.configScript,
|
||||||
__uv$bareURL,
|
|
||||||
__uv$bareData,
|
|
||||||
cookieStr,
|
cookieStr,
|
||||||
window.location.href
|
window.location.href
|
||||||
),
|
),
|
||||||
|
|
30
src/uv.sw.js
30
src/uv.sw.js
|
@ -30,18 +30,22 @@ const emptyMethods = ['GET', 'HEAD'];
|
||||||
class UVServiceWorker extends Ultraviolet.EventEmitter {
|
class UVServiceWorker extends Ultraviolet.EventEmitter {
|
||||||
constructor(config = __uv$config) {
|
constructor(config = __uv$config) {
|
||||||
super();
|
super();
|
||||||
if (!config.bare) config.bare = '/bare/';
|
|
||||||
if (!config.prefix) config.prefix = '/service/';
|
if (!config.prefix) config.prefix = '/service/';
|
||||||
this.config = config;
|
this.config = config;
|
||||||
const addresses = (
|
|
||||||
Array.isArray(config.bare) ? config.bare : [config.bare]
|
|
||||||
).map((str) => new URL(str, location).toString());
|
|
||||||
this.address = addresses[~~(Math.random() * addresses.length)];
|
|
||||||
/**
|
/**
|
||||||
* @type {InstanceType<Ultraviolet['BareClient']>}
|
* @type {InstanceType<Ultraviolet['BareClient']>}
|
||||||
*/
|
*/
|
||||||
this.bareClient = new Ultraviolet.BareClient();
|
this.bareClient = new Ultraviolet.BareClient();
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {Event & {request: Request}} param0
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
route({ request }) {
|
||||||
|
if (request.url.startsWith(location.origin + this.config.prefix)) return true;
|
||||||
|
else return false;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {Event & {request: Request}} param0
|
* @param {Event & {request: Request}} param0
|
||||||
|
@ -57,7 +61,7 @@ class UVServiceWorker extends Ultraviolet.EventEmitter {
|
||||||
if (!request.url.startsWith(location.origin + this.config.prefix))
|
if (!request.url.startsWith(location.origin + this.config.prefix))
|
||||||
return await fetch(request);
|
return await fetch(request);
|
||||||
|
|
||||||
const ultraviolet = new Ultraviolet(this.config, this.address);
|
const ultraviolet = new Ultraviolet(this.config);
|
||||||
|
|
||||||
if (typeof this.config.construct === 'function') {
|
if (typeof this.config.construct === 'function') {
|
||||||
this.config.construct(ultraviolet, 'service');
|
this.config.construct(ultraviolet, 'service');
|
||||||
|
@ -72,7 +76,6 @@ class UVServiceWorker extends Ultraviolet.EventEmitter {
|
||||||
|
|
||||||
const requestCtx = new RequestContext(
|
const requestCtx = new RequestContext(
|
||||||
request,
|
request,
|
||||||
this,
|
|
||||||
ultraviolet,
|
ultraviolet,
|
||||||
!emptyMethods.includes(request.method.toUpperCase())
|
!emptyMethods.includes(request.method.toUpperCase())
|
||||||
? await request.blob()
|
? await request.blob()
|
||||||
|
@ -130,10 +133,7 @@ class UVServiceWorker extends Ultraviolet.EventEmitter {
|
||||||
method: requestCtx.method,
|
method: requestCtx.method,
|
||||||
body: requestCtx.body,
|
body: requestCtx.body,
|
||||||
credentials: requestCtx.credentials,
|
credentials: requestCtx.credentials,
|
||||||
mode:
|
mode: requestCtx.mode,
|
||||||
location.origin !== requestCtx.address.origin
|
|
||||||
? 'cors'
|
|
||||||
: requestCtx.mode,
|
|
||||||
cache: requestCtx.cache,
|
cache: requestCtx.cache,
|
||||||
redirect: requestCtx.redirect,
|
redirect: requestCtx.redirect,
|
||||||
});
|
});
|
||||||
|
@ -212,8 +212,6 @@ class UVServiceWorker extends Ultraviolet.EventEmitter {
|
||||||
.map((script) => JSON.stringify(script))
|
.map((script) => JSON.stringify(script))
|
||||||
.join(',');
|
.join(',');
|
||||||
responseCtx.body = `if (!self.__uv && self.importScripts) { ${ultraviolet.createJsInject(
|
responseCtx.body = `if (!self.__uv && self.importScripts) { ${ultraviolet.createJsInject(
|
||||||
this.address,
|
|
||||||
this.bareClient.manifest,
|
|
||||||
ultraviolet.cookie.serialize(
|
ultraviolet.cookie.serialize(
|
||||||
cookies,
|
cookies,
|
||||||
ultraviolet.meta,
|
ultraviolet.meta,
|
||||||
|
@ -248,8 +246,6 @@ class UVServiceWorker extends Ultraviolet.EventEmitter {
|
||||||
ultraviolet.bundleScript,
|
ultraviolet.bundleScript,
|
||||||
ultraviolet.clientScript,
|
ultraviolet.clientScript,
|
||||||
ultraviolet.configScript,
|
ultraviolet.configScript,
|
||||||
this.address,
|
|
||||||
this.bareClient.manifest,
|
|
||||||
ultraviolet.cookie.serialize(
|
ultraviolet.cookie.serialize(
|
||||||
cookies,
|
cookies,
|
||||||
ultraviolet.meta,
|
ultraviolet.meta,
|
||||||
|
@ -326,16 +322,14 @@ class RequestContext {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {Request} request
|
* @param {Request} request
|
||||||
* @param {UVServiceWorker} worker
|
|
||||||
* @param {Ultraviolet} ultraviolet
|
* @param {Ultraviolet} ultraviolet
|
||||||
* @param {BodyInit} body
|
* @param {BodyInit} body
|
||||||
*/
|
*/
|
||||||
constructor(request, worker, ultraviolet, body = null) {
|
constructor(request, ultraviolet, body = null) {
|
||||||
this.ultraviolet = ultraviolet;
|
this.ultraviolet = ultraviolet;
|
||||||
this.request = request;
|
this.request = request;
|
||||||
this.headers = Object.fromEntries(request.headers.entries());
|
this.headers = Object.fromEntries(request.headers.entries());
|
||||||
this.method = request.method;
|
this.method = request.method;
|
||||||
this.address = worker.address;
|
|
||||||
this.body = body || null;
|
this.body = body || null;
|
||||||
this.cache = request.cache;
|
this.cache = request.cache;
|
||||||
this.redirect = request.redirect;
|
this.redirect = request.redirect;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue