separate consts
This commit is contained in:
parent
015045f2c5
commit
5e71b8138b
1 changed files with 23 additions and 35 deletions
58
src/uv.sw.js
58
src/uv.sw.js
|
|
@ -10,6 +10,27 @@
|
||||||
*/
|
*/
|
||||||
const Ultraviolet = globalThis.Ultraviolet;
|
const Ultraviolet = globalThis.Ultraviolet;
|
||||||
|
|
||||||
|
const cspHeaders = [
|
||||||
|
'cross-origin-embedder-policy',
|
||||||
|
'cross-origin-opener-policy',
|
||||||
|
'cross-origin-resource-policy',
|
||||||
|
'content-security-policy',
|
||||||
|
'content-security-policy-report-only',
|
||||||
|
'expect-ct',
|
||||||
|
'feature-policy',
|
||||||
|
'origin-isolation',
|
||||||
|
'strict-transport-security',
|
||||||
|
'upgrade-insecure-requests',
|
||||||
|
'x-content-type-options',
|
||||||
|
'x-download-options',
|
||||||
|
'x-frame-options',
|
||||||
|
'x-permitted-cross-domain-policies',
|
||||||
|
'x-powered-by',
|
||||||
|
'x-xss-protection',
|
||||||
|
];
|
||||||
|
const emptyMethods = ['GET', 'HEAD'];
|
||||||
|
const emptyStatus = [204, 304];
|
||||||
|
|
||||||
class UVServiceWorker extends EventEmitter {
|
class UVServiceWorker extends EventEmitter {
|
||||||
constructor(config = __uv$config) {
|
constructor(config = __uv$config) {
|
||||||
super();
|
super();
|
||||||
|
|
@ -19,33 +40,6 @@ class UVServiceWorker extends EventEmitter {
|
||||||
typeof config.bare === 'string'
|
typeof config.bare === 'string'
|
||||||
? [new URL(config.bare, location)]
|
? [new URL(config.bare, location)]
|
||||||
: config.bare.map((str) => new URL(str, location));
|
: config.bare.map((str) => new URL(str, location));
|
||||||
this.headers = {
|
|
||||||
csp: [
|
|
||||||
'cross-origin-embedder-policy',
|
|
||||||
'cross-origin-opener-policy',
|
|
||||||
'cross-origin-resource-policy',
|
|
||||||
'content-security-policy',
|
|
||||||
'content-security-policy-report-only',
|
|
||||||
'expect-ct',
|
|
||||||
'feature-policy',
|
|
||||||
'origin-isolation',
|
|
||||||
'strict-transport-security',
|
|
||||||
'upgrade-insecure-requests',
|
|
||||||
'x-content-type-options',
|
|
||||||
'x-download-options',
|
|
||||||
'x-frame-options',
|
|
||||||
'x-permitted-cross-domain-policies',
|
|
||||||
'x-powered-by',
|
|
||||||
'x-xss-protection',
|
|
||||||
],
|
|
||||||
forward: ['accept-encoding', 'connection', 'content-length'],
|
|
||||||
};
|
|
||||||
this.method = {
|
|
||||||
empty: ['GET', 'HEAD'],
|
|
||||||
};
|
|
||||||
this.statusCode = {
|
|
||||||
empty: [204, 304],
|
|
||||||
};
|
|
||||||
this.config = config;
|
this.config = config;
|
||||||
/**
|
/**
|
||||||
* @type {InstanceType<Ultraviolet['BareClient']>}
|
* @type {InstanceType<Ultraviolet['BareClient']>}
|
||||||
|
|
@ -56,11 +50,6 @@ class UVServiceWorker extends EventEmitter {
|
||||||
this.browser = Ultraviolet.Bowser.getParser(
|
this.browser = Ultraviolet.Bowser.getParser(
|
||||||
self.navigator.userAgent
|
self.navigator.userAgent
|
||||||
).getBrowserName();
|
).getBrowserName();
|
||||||
|
|
||||||
if (this.browser === 'Firefox') {
|
|
||||||
this.headers.forward.push('user-agent');
|
|
||||||
this.headers.forward.push('content-type');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
|
@ -89,7 +78,7 @@ class UVServiceWorker extends EventEmitter {
|
||||||
request,
|
request,
|
||||||
this,
|
this,
|
||||||
ultraviolet,
|
ultraviolet,
|
||||||
!this.method.empty.includes(request.method.toUpperCase())
|
!emptyMethods.includes(request.method.toUpperCase())
|
||||||
? await request.blob()
|
? await request.blob()
|
||||||
: null
|
: null
|
||||||
);
|
);
|
||||||
|
|
@ -172,7 +161,7 @@ class UVServiceWorker extends EventEmitter {
|
||||||
this.emit('beforemod', resEvent);
|
this.emit('beforemod', resEvent);
|
||||||
if (resEvent.intercepted) return resEvent.returnValue;
|
if (resEvent.intercepted) return resEvent.returnValue;
|
||||||
|
|
||||||
for (const name of this.headers.csp) {
|
for (const name of cspHeaders) {
|
||||||
if (responseCtx.headers[name]) delete responseCtx.headers[name];
|
if (responseCtx.headers[name]) delete responseCtx.headers[name];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -312,7 +301,6 @@ class RequestContext {
|
||||||
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.forward = [...worker.headers.forward];
|
|
||||||
this.address = worker.address;
|
this.address = worker.address;
|
||||||
this.body = body || null;
|
this.body = body || null;
|
||||||
this.redirect = request.redirect;
|
this.redirect = request.redirect;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue