coi-serviceworker
Advanced tools
Comparing version 0.1.6 to 0.1.7
@@ -1,2 +0,3 @@ | ||
/*! coi-serviceworker v0.1.6 - Guido Zuidhof, licensed under MIT */ | ||
/*! coi-serviceworker v0.1.7 - Guido Zuidhof and contributors, licensed under MIT */ | ||
let coepCredentialless = false; | ||
if (typeof window === 'undefined') { | ||
@@ -7,3 +8,5 @@ self.addEventListener("install", () => self.skipWaiting()); | ||
self.addEventListener("message", (ev) => { | ||
if (ev.data && ev.data.type === "deregister") { | ||
if (!ev.data) { | ||
return; | ||
} else if (ev.data.type === "deregister") { | ||
self.registration | ||
@@ -17,2 +20,4 @@ .unregister() | ||
}); | ||
} else if (ev.data.type === "coepCredentialless") { | ||
coepCredentialless = ev.data.value; | ||
} | ||
@@ -22,8 +27,14 @@ }); | ||
self.addEventListener("fetch", function (event) { | ||
if (event.request.cache === "only-if-cached" && event.request.mode !== "same-origin") { | ||
const r = event.request; | ||
if (r.cache === "only-if-cached" && r.mode !== "same-origin") { | ||
return; | ||
} | ||
const request = (coepCredentialless && r.mode === "no-cors") | ||
? new Request(r, { | ||
credentials: "omit", | ||
}) | ||
: r; | ||
event.respondWith( | ||
fetch(event.request) | ||
fetch(request) | ||
.then((response) => { | ||
@@ -35,3 +46,8 @@ if (response.status === 0) { | ||
const newHeaders = new Headers(response.headers); | ||
newHeaders.set("Cross-Origin-Embedder-Policy", "require-corp"); | ||
newHeaders.set("Cross-Origin-Embedder-Policy", | ||
coepCredentialless ? "credentialless" : "require-corp" | ||
); | ||
if (!coepCredentialless) { | ||
newHeaders.set("Cross-Origin-Resource-Policy", "cross-origin"); | ||
} | ||
newHeaders.set("Cross-Origin-Opener-Policy", "same-origin"); | ||
@@ -55,10 +71,19 @@ | ||
shouldDeregister: () => false, | ||
coepCredentialless: () => !(window.chrome || window.netscape), | ||
doReload: () => window.location.reload(), | ||
quiet: false, | ||
...window.coi | ||
} | ||
}; | ||
const n = navigator; | ||
if (coi.shouldDeregister() && n.serviceWorker && n.serviceWorker.controller) { | ||
n.serviceWorker.controller.postMessage({ type: "deregister" }); | ||
if (n.serviceWorker && n.serviceWorker.controller) { | ||
n.serviceWorker.controller.postMessage({ | ||
type: "coepCredentialless", | ||
value: coi.coepCredentialless(), | ||
}); | ||
if (coi.shouldDeregister()) { | ||
n.serviceWorker.controller.postMessage({ type: "deregister" }); | ||
} | ||
} | ||
@@ -79,7 +104,7 @@ | ||
(registration) => { | ||
!coi.quiet & console.log("COOP/COEP Service Worker registered", registration.scope); | ||
!coi.quiet && console.log("COOP/COEP Service Worker registered", registration.scope); | ||
registration.addEventListener("updatefound", () => { | ||
!coi.quiet && console.log("Reloading page to make use of updated COOP/COEP Service Worker."); | ||
coi.doReload() | ||
coi.doReload(); | ||
}); | ||
@@ -90,3 +115,3 @@ | ||
!coi.quiet && console.log("Reloading page to make use of COOP/COEP Service Worker."); | ||
coi.doReload() | ||
coi.doReload(); | ||
} | ||
@@ -93,0 +118,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
/*! coi-serviceworker v0.1.6 - Guido Zuidhof, licensed under MIT */ | ||
"undefined"==typeof window?(self.addEventListener("install",(()=>self.skipWaiting())),self.addEventListener("activate",(e=>e.waitUntil(self.clients.claim()))),self.addEventListener("message",(e=>{e.data&&"deregister"===e.data.type&&self.registration.unregister().then((()=>self.clients.matchAll())).then((e=>{e.forEach((e=>e.navigate(e.url)))}))})),self.addEventListener("fetch",(function(e){"only-if-cached"===e.request.cache&&"same-origin"!==e.request.mode||e.respondWith(fetch(e.request).then((e=>{if(0===e.status)return e;const r=new Headers(e.headers);return r.set("Cross-Origin-Embedder-Policy","require-corp"),r.set("Cross-Origin-Opener-Policy","same-origin"),new Response(e.body,{status:e.status,statusText:e.statusText,headers:r})})).catch((e=>console.error(e))))}))):(()=>{const e={shouldRegister:()=>!0,shouldDeregister:()=>!1,doReload:()=>window.location.reload(),quiet:!1,...window.coi},r=navigator;e.shouldDeregister()&&r.serviceWorker&&r.serviceWorker.controller&&r.serviceWorker.controller.postMessage({type:"deregister"}),!1===window.crossOriginIsolated&&e.shouldRegister()&&(window.isSecureContext?r.serviceWorker&&r.serviceWorker.register(window.document.currentScript.src).then((t=>{e.quiet,console.log("COOP/COEP Service Worker registered",t.scope),t.addEventListener("updatefound",(()=>{!e.quiet&&console.log("Reloading page to make use of updated COOP/COEP Service Worker."),e.doReload()})),t.active&&!r.serviceWorker.controller&&(!e.quiet&&console.log("Reloading page to make use of COOP/COEP Service Worker."),e.doReload())}),(r=>{!e.quiet&&console.error("COOP/COEP Service Worker failed to register:",r)})):!e.quiet&&console.log("COOP/COEP Service Worker not registered, a secure context is required."))})(); | ||
/*! coi-serviceworker v0.1.7 - Guido Zuidhof and contributors, licensed under MIT */ | ||
let coepCredentialless=!1;"undefined"==typeof window?(self.addEventListener("install",(()=>self.skipWaiting())),self.addEventListener("activate",(e=>e.waitUntil(self.clients.claim()))),self.addEventListener("message",(e=>{e.data&&("deregister"===e.data.type?self.registration.unregister().then((()=>self.clients.matchAll())).then((e=>{e.forEach((e=>e.navigate(e.url)))})):"coepCredentialless"===e.data.type&&(coepCredentialless=e.data.value))})),self.addEventListener("fetch",(function(e){const r=e.request;if("only-if-cached"===r.cache&&"same-origin"!==r.mode)return;const s=coepCredentialless&&"no-cors"===r.mode?new Request(r,{credentials:"omit"}):r;e.respondWith(fetch(s).then((e=>{if(0===e.status)return e;const r=new Headers(e.headers);return r.set("Cross-Origin-Embedder-Policy",coepCredentialless?"credentialless":"require-corp"),coepCredentialless||r.set("Cross-Origin-Resource-Policy","cross-origin"),r.set("Cross-Origin-Opener-Policy","same-origin"),new Response(e.body,{status:e.status,statusText:e.statusText,headers:r})})).catch((e=>console.error(e))))}))):(()=>{const e={shouldRegister:()=>!0,shouldDeregister:()=>!1,coepCredentialless:()=>!(window.chrome||window.netscape),doReload:()=>window.location.reload(),quiet:!1,...window.coi},r=navigator;r.serviceWorker&&r.serviceWorker.controller&&(r.serviceWorker.controller.postMessage({type:"coepCredentialless",value:e.coepCredentialless()}),e.shouldDeregister()&&r.serviceWorker.controller.postMessage({type:"deregister"})),!1===window.crossOriginIsolated&&e.shouldRegister()&&(window.isSecureContext?r.serviceWorker&&r.serviceWorker.register(window.document.currentScript.src).then((s=>{!e.quiet&&console.log("COOP/COEP Service Worker registered",s.scope),s.addEventListener("updatefound",(()=>{!e.quiet&&console.log("Reloading page to make use of updated COOP/COEP Service Worker."),e.doReload()})),s.active&&!r.serviceWorker.controller&&(!e.quiet&&console.log("Reloading page to make use of COOP/COEP Service Worker."),e.doReload())}),(r=>{!e.quiet&&console.error("COOP/COEP Service Worker failed to register:",r)})):!e.quiet&&console.log("COOP/COEP Service Worker not registered, a secure context is required."))})(); |
{ | ||
"name": "coi-serviceworker", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "Cross-Origin Isolation through a service worker", | ||
@@ -5,0 +5,0 @@ "main": "coi-serviceworker.js", |
@@ -47,2 +47,5 @@ # coi-serviceworker | ||
shouldDeregister: () => false, | ||
// A function that is run to decide whether to use "Cross-Origin-Embedder-Policy: credentialless" or not. | ||
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Embedder-Policy#browser_compatibility | ||
coepCredentialless: () => !(window.chrome || window.netscape), | ||
// Override this if you want to prompt the user and do reload at your own leisure. Maybe show the user a message saying: | ||
@@ -49,0 +52,0 @@ // "Click OK to refresh the page to enable <...>" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12017
105
65