awacke1/OpenAI-CLIP-JavaScript
2
1// π€ NOTE: This file creates a service worker that cross-origin-isolates the page (read more here: https://web.dev/coop-coep/)2// π Normally you'd set the COOP and COEP headers on the server (yum), but Github Pages said: "No burgers for you!" π3// π So, we're doing a cheeky pizza hack π to make this work without server-side headers.4 5/* π οΈ Edited version of: coi-serviceworker v0.1.6 - Guido Zuidhof, licensed under MIT π οΈ */6// π§ββοΈ We're using wizardry from here: https://github.com/gzuidhof/coi-serviceworker7 8if (typeof window === 'undefined') { 9 // π οΈ Service Worker Time! (No windows allowed in this club)10 11 // π Install event: "Let's skip the line and go straight to work!"12 self.addEventListener("install", () => self.skipWaiting());13 14 // π Activate event: "I claim all clients! Mine! All mine!" (Mwahaha π)15 self.addEventListener("activate", e => e.waitUntil(self.clients.claim()));16 17 // π΄ Handle fetch requests, like a master chef in the kitchen!18 async function handleFetch(request) {19 // π§ Special seasoning: If we're caching but the mode's not right, we pass.20 if (request.cache === "only-if-cached" && request.mode !== "same-origin") {21 return;22 }23 24 // πΆοΈ For no-cors requests, we're keeping it cool π with 'omit' credentials (Bug workarounds are fun, right? π)25 if (request.mode === "no-cors") { 26 request = new Request(request.url, {27 cache: request.cache,28 credentials: "omit",29 headers: request.headers,30 integrity: request.integrity,31 destination: request.destination,32 keepalive: request.keepalive,33 method: request.method,34 mode: request.mode,35 redirect: request.redirect,36 referrer: request.referrer,37 referrerPolicy: request.referrerPolicy,38 signal: request.signal,39 });40 }41 42 // π Fetching data like a worldwide explorer! π§43 let r = await fetch(request).catch(e => console.error(e));44 45 // π If the response status is zero, we return it β probably not what we were hoping for, but hey, it's something π€·ββοΈ46 if (r.status === 0) {47 return r;48 }49 50 // π© Magic header time! Setting the Cross-Origin rules like a boss π§ββοΈ51 const headers = new Headers(r.headers);52 headers.set("Cross-Origin-Embedder-Policy", "credentialless"); // π‘οΈ Or: 'require-corp', for those fancy users53 headers.set("Cross-Origin-Opener-Policy", "same-origin"); // π§± Keep it locked down and safe.54 55 return new Response(r.body, { status: r.status, statusText: r.statusText, headers });56 }57 58 // ποΈ Fetch event listener: "Don't worry, I've got this!" Handling requests like a pro π―59 self.addEventListener("fetch", function(e) {60 e.respondWith(handleFetch(e.request)); // π‘ respondWith must be synchronous, like a well-timed joke! (But it can wait for a promise π)61 });62 63} else {64 // π If we're running in a window (hello, browser!), we register the service worker like a superhero suiting up π¦ΈββοΈ65 (async function() {66 // β If we're already isolated, let's not double down on the isolation π67 if (window.crossOriginIsolated !== false) return;68 69 // ποΈ Registering the service worker like we're entering the coolest club in town!70 let registration = await navigator.serviceWorker.register(window.document.currentScript.src).catch(e => console.error("COOP/COEP Service Worker failed to register:", e));71 72 if (registration) {73 console.log("COOP/COEP Service Worker registered", registration.scope);74 75 // π When the service worker updates, we refresh the page like a fresh cup of coffee β76 registration.addEventListener("updatefound", () => {77 console.log("Reloading page to make use of updated COOP/COEP Service Worker.");78 window.location.reload();79 });80 81 // π If the service worker is active but not in control, we give the page a fresh reboot π82 if (registration.active && !navigator.serviceWorker.controller) {83 console.log("Reloading page to make use of COOP/COEP Service Worker.");84 window.location.reload();85 }86 }87 })();88}89 90// ποΈ Code to clean up: "Time to say goodbye!" Unregister the service worker and take out the trash π§Ή91// let registrations = await navigator.serviceWorker.getRegistrations();92// for(let registration of registrations) {93// await registration.unregister();94// }95 