File size: 7,263 Bytes
057da7d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | (function () {
const cacheName = "sls4all-service-worker";
const cacheKey = "/cache/v1";
var allCookiesForOrigins = null;
var defaultParseOptions = {
decodeValues: true,
map: false,
};
function isNonEmptyString(str) {
return typeof str === "string" && !!str.trim();
}
function parseString(setCookieValue, options) {
var parts = setCookieValue.split(";").filter(isNonEmptyString);
var nameValuePairStr = parts.shift();
var parsed = parseNameValuePair(nameValuePairStr);
var name = parsed.name;
var value = parsed.value;
options = options
? Object.assign({}, defaultParseOptions, options)
: defaultParseOptions;
try {
value = options.decodeValues ? decodeURIComponent(value) : value;
} catch {
}
var cookie = {
name: name,
value: value,
};
parts.forEach(function (part) {
var sides = part.split("=");
var key = sides.shift().trimLeft().toLowerCase();
var value = sides.join("=");
if (key === "expires") {
cookie.expires = new Date(value);
} else if (key === "max-age") {
cookie.maxAge = parseInt(value, 10);
} else if (key === "secure") {
cookie.secure = true;
} else if (key === "httponly") {
cookie.httpOnly = true;
} else if (key === "samesite") {
cookie.sameSite = value;
} else if (key === "partitioned") {
cookie.partitioned = true;
} else {
cookie[key] = value;
}
});
return cookie;
}
function parseNameValuePair(nameValuePairStr) {
var name = "";
var value = "";
var nameValueArr = nameValuePairStr.split("=");
if (nameValueArr.length > 1) {
name = nameValueArr.shift();
value = nameValueArr.join("=");
} else {
value = nameValuePairStr;
}
return { name: name, value: value };
}
function splitCookiesString(cookiesString) {
if (Array.isArray(cookiesString)) {
return cookiesString;
}
if (typeof cookiesString !== "string") {
return [];
}
var cookiesStrings = [];
var pos = 0;
var start;
var ch;
var lastComma;
var nextStart;
var cookiesSeparatorFound;
function skipWhitespace() {
while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))) {
pos += 1;
}
return pos < cookiesString.length;
}
function notSpecialChar() {
ch = cookiesString.charAt(pos);
return ch !== "=" && ch !== ";" && ch !== ",";
}
while (pos < cookiesString.length) {
start = pos;
cookiesSeparatorFound = false;
while (skipWhitespace()) {
ch = cookiesString.charAt(pos);
if (ch === ",") {
lastComma = pos;
pos += 1;
skipWhitespace();
nextStart = pos;
while (pos < cookiesString.length && notSpecialChar()) {
pos += 1;
}
if (pos < cookiesString.length && cookiesString.charAt(pos) === "=") {
cookiesSeparatorFound = true;
pos = nextStart;
cookiesStrings.push(cookiesString.substring(start, lastComma));
start = pos;
} else {
pos = lastComma + 1;
}
} else {
pos += 1;
}
}
if (!cookiesSeparatorFound || pos >= cookiesString.length) {
cookiesStrings.push(cookiesString.substring(start, cookiesString.length));
}
}
return cookiesStrings;
}
function getPseudoOrigin(url) {
const remoteText = '/remote-printer/';
const remoteIndex = url.indexOf(remoteText);
if (remoteIndex != -1) {
var pos = url.indexOf('/', remoteIndex + remoteText.length);
if (pos == -1)
pos = url.length;
return url.substring(0, pos);
}
else
return new URL(url).origin
}
async function wrapFetch(request) {
if (allCookiesForOrigins === null) {
const cache = await caches.open(cacheName);
const data = await caches.match(cacheKey);
allCookiesForOrigins = (data != null ? JSON.parse(await data.text()) : null) || {};
}
var pseudoOrigin = getPseudoOrigin(request.url);
var xcookies = allCookiesForOrigins[pseudoOrigin];
if (xcookies == null)
allCookiesForOrigins[pseudoOrigin] = xcookies = {};
const siteMeta = request.headers.get('sec-fetch-site');
const sameOrigin = new URL(request.url).origin === self.location.origin;
const isSameSite = siteMeta
? (siteMeta === 'same-origin' || siteMeta === 'same-site')
: sameOrigin;
if (isSameSite) {
const headers = new Headers(request.headers);
for (const [key, value] of Object.entries(xcookies))
headers.append("X-Cookie", key + "=" + encodeURIComponent(value));
request = new Request(request, { headers: headers, mode: 'same-origin', cache: 'default' });
}
const response = await fetch(request);
if (response.type !== 'basic')
return response;
const headers = new Headers(response.headers);
const setCookie = headers.get("X-Set-Cookie");
if (setCookie) {
const cookies = splitCookiesString(setCookie);
for (const cookie of cookies) {
const item = parseString(cookie);
if (item && item.name) {
xcookies[item.name] = item.value;
}
}
const cache = await caches.open(cacheName);
await cache.put(cacheKey, new Response(JSON.stringify(allCookiesForOrigins)));
}
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: headers
});
}
self.addEventListener('install', (event) => {
event.waitUntil(self.skipWaiting());
});
self.addEventListener("activate", (event) => {
event.waitUntil((async () => {
await clients.claim();
if (self.registration.navigationPreload) {
await self.registration.navigationPreload.disable();
}
})());
});
self.addEventListener('fetch', (event) => {
event.respondWith(wrapFetch(event.request));
});
})(); |