Skip to content

Commit

Permalink
clean up service worker code, remove useless variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Percslol committed May 8, 2024
1 parent ab4e2f9 commit cb717c9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 44 deletions.
10 changes: 2 additions & 8 deletions src/rewrite/rewrite.html.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,12 @@ export function injectHead(ctx) {
}

export function createJsInject(
bareURL = '',
bareData = {},
cookies = '',
referrer = ''
) {
return (
`self.__uv$bareData = ${JSON.stringify(bareData)};` +
`self.__uv$cookies = ${JSON.stringify(cookies)};` +
`self.__uv$referrer = ${JSON.stringify(referrer)};` +
`self.__uv$bareURL = ${JSON.stringify(bareURL)}; `
`self.__uv$referrer = ${JSON.stringify(referrer)};`
);
}

Expand All @@ -227,8 +223,6 @@ export function createHtmlInject(
bundleScript,
clientScript,
configScript,
bareURL,
bareData,
cookies,
referrer
) {
Expand All @@ -239,7 +233,7 @@ export function createHtmlInject(
childNodes: [
{
nodeName: '#text',
value: createJsInject(bareURL, bareData, cookies, referrer),
value: createJsInject(cookies, referrer),
},
],
attrs: [
Expand Down
2 changes: 1 addition & 1 deletion src/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const uv = new UVServiceWorker();
self.addEventListener('fetch', event => {
event.respondWith(
(async ()=>{
if(event.request.url.startsWith(location.origin + __uv$config.prefix)) {
if (uv.route(event)) {
return await uv.fetch(event);
}
return await fetch(event.request);
Expand Down
17 changes: 0 additions & 17 deletions src/uv.handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,13 @@ const UVClient = self.UVClient;
*/
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}
*/
const __uv$cookies = self.__uv$cookies;

if (
typeof __uv$bareData !== 'object' ||
typeof __uv$bareURL !== 'string' ||
typeof __uv$cookies !== 'string'
)
throw new TypeError('Unable to load global UV data');
Expand Down Expand Up @@ -572,8 +561,6 @@ function __uvHook(window) {
__uv.bundleScript,
__uv.clientScript,
__uv.configScript,
__uv$bareURL,
__uv$bareData,
cookieStr,
window.location.href
),
Expand Down Expand Up @@ -780,8 +767,6 @@ function __uvHook(window) {
__uv.bundleScript,
__uv.clientScript,
__uv.configScript,
__uv$bareURL,
__uv$bareData,
cookieStr,
window.location.href
),
Expand Down Expand Up @@ -892,8 +877,6 @@ function __uvHook(window) {
__uv.bundleScript,
__uv.clientScript,
__uv.configScript,
__uv$bareURL,
__uv$bareData,
cookieStr,
window.location.href
),
Expand Down
30 changes: 12 additions & 18 deletions src/uv.sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,22 @@ const emptyMethods = ['GET', 'HEAD'];
class UVServiceWorker extends Ultraviolet.EventEmitter {
constructor(config = __uv$config) {
super();
if (!config.bare) config.bare = '/bare/';
if (!config.prefix) config.prefix = '/service/';
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']>}
*/
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
Expand All @@ -57,7 +61,7 @@ class UVServiceWorker extends Ultraviolet.EventEmitter {
if (!request.url.startsWith(location.origin + this.config.prefix))
return await fetch(request);

const ultraviolet = new Ultraviolet(this.config, this.address);
const ultraviolet = new Ultraviolet(this.config);

if (typeof this.config.construct === 'function') {
this.config.construct(ultraviolet, 'service');
Expand All @@ -72,7 +76,6 @@ class UVServiceWorker extends Ultraviolet.EventEmitter {

const requestCtx = new RequestContext(
request,
this,
ultraviolet,
!emptyMethods.includes(request.method.toUpperCase())
? await request.blob()
Expand Down Expand Up @@ -130,10 +133,7 @@ class UVServiceWorker extends Ultraviolet.EventEmitter {
method: requestCtx.method,
body: requestCtx.body,
credentials: requestCtx.credentials,
mode:
location.origin !== requestCtx.address.origin
? 'cors'
: requestCtx.mode,
mode: requestCtx.mode,
cache: requestCtx.cache,
redirect: requestCtx.redirect,
});
Expand Down Expand Up @@ -212,8 +212,6 @@ class UVServiceWorker extends Ultraviolet.EventEmitter {
.map((script) => JSON.stringify(script))
.join(',');
responseCtx.body = `if (!self.__uv && self.importScripts) { ${ultraviolet.createJsInject(
this.address,
this.bareClient.manifest,
ultraviolet.cookie.serialize(
cookies,
ultraviolet.meta,
Expand Down Expand Up @@ -248,8 +246,6 @@ class UVServiceWorker extends Ultraviolet.EventEmitter {
ultraviolet.bundleScript,
ultraviolet.clientScript,
ultraviolet.configScript,
this.address,
this.bareClient.manifest,
ultraviolet.cookie.serialize(
cookies,
ultraviolet.meta,
Expand Down Expand Up @@ -326,16 +322,14 @@ class RequestContext {
/**
*
* @param {Request} request
* @param {UVServiceWorker} worker
* @param {Ultraviolet} ultraviolet
* @param {BodyInit} body
*/
constructor(request, worker, ultraviolet, body = null) {
constructor(request, ultraviolet, body = null) {
this.ultraviolet = ultraviolet;
this.request = request;
this.headers = Object.fromEntries(request.headers.entries());
this.method = request.method;
this.address = worker.address;
this.body = body || null;
this.cache = request.cache;
this.redirect = request.redirect;
Expand Down

0 comments on commit cb717c9

Please sign in to comment.