CoolFace
Apppublic

radames/OpenAI-CLIP-JavaScript

sourceHugging Faceupdated 3y agoView on Hugging Face
7likes
enable-threads.js76 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/) which allows us to use wasm threads.2// Normally you would set the COOP and COEP headers on the server to do this, but Github Pages doesn't allow this, so this is a hack to do that.3 4/* Edited version of: coi-serviceworker v0.1.6 - Guido Zuidhof, licensed under MIT */5// From here: https://github.com/gzuidhof/coi-serviceworker6if(typeof window === 'undefined') {7  self.addEventListener("install", () => self.skipWaiting());8  self.addEventListener("activate", e => e.waitUntil(self.clients.claim()));9 10  async function handleFetch(request) {11    if(request.cache === "only-if-cached" && request.mode !== "same-origin") {12      return;13    }14    15    if(request.mode === "no-cors") { // We need to set `credentials` to "omit" for no-cors requests, per this comment: https://bugs.chromium.org/p/chromium/issues/detail?id=1309901#c716      request = new Request(request.url, {17        cache: request.cache,18        credentials: "omit",19        headers: request.headers,20        integrity: request.integrity,21        destination: request.destination,22        keepalive: request.keepalive,23        method: request.method,24        mode: request.mode,25        redirect: request.redirect,26        referrer: request.referrer,27        referrerPolicy: request.referrerPolicy,28        signal: request.signal,29      });30    }31    32    let r = await fetch(request).catch(e => console.error(e));33    34    if(r.status === 0) {35      return r;36    }37 38    const headers = new Headers(r.headers);39    headers.set("Cross-Origin-Embedder-Policy", "credentialless"); // or: require-corp40    headers.set("Cross-Origin-Opener-Policy", "same-origin");41    42    return new Response(r.body, { status: r.status, statusText: r.statusText, headers });43  }44 45  self.addEventListener("fetch", function(e) {46    e.respondWith(handleFetch(e.request)); // respondWith must be executed synchonously (but can be passed a Promise)47  });48  49} else {50  (async function() {51    if(window.crossOriginIsolated !== false) return;52 53    let registration = await navigator.serviceWorker.register(window.document.currentScript.src).catch(e => console.error("COOP/COEP Service Worker failed to register:", e));54    if(registration) {55      console.log("COOP/COEP Service Worker registered", registration.scope);56 57      registration.addEventListener("updatefound", () => {58        console.log("Reloading page to make use of updated COOP/COEP Service Worker.");59        window.location.reload();60      });61 62      // If the registration is active, but it's not controlling the page63      if(registration.active && !navigator.serviceWorker.controller) {64        console.log("Reloading page to make use of COOP/COEP Service Worker.");65        window.location.reload();66      }67    }68  })();69}70 71// Code to deregister:72// let registrations = await navigator.serviceWorker.getRegistrations();73// for(let registration of registrations) {74//   await registration.unregister();75// }76