CoolFace
Apppublic

awacke1/OpenAI-CLIP-JavaScript

sourceHugging Faceupdated 2y agoView on Hugging Face
2likes
enable-threads.js95 linesDownload Raw Back to root
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