Skip to content

Commit

Permalink
coi - sharedarraybuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
ibelem committed Oct 11, 2023
1 parent ab7d67b commit 8c1ede6
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
<script src="https://ibelem.github.io/onnxruntime-web-dist/1.16/web/dist/ort.js"></script>
<script src="./coi-serviceworker.js"></script>
</body>

</html>
6 changes: 5 additions & 1 deletion src/lib/assets/css/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,10 @@ padding: 2px 4px 2px 4px;
margin-top: 4px;
}

#webnn_npu {
margin-top: 0px;
}

.group .block label {
margin-left: 0px;
width: 52px;
Expand Down Expand Up @@ -850,7 +854,7 @@ padding: 2px 4px 2px 4px;

.config .models label {
margin: 0px 4px 0px 0px;
width: 96px;
width: 94px;
}

.result .q div.m {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/ConfigDataTypes.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
{#if dataTypes}
<div class="title">
<label class="" title="Toggle data types">
<label class="" title="Toggle operand types">
<input type="checkbox" on:change={() => toggleDataTypes()} />
Operand Type
</label>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/ConfigModels.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@
</script>
<div class="title">
<label class="" title="Toggle data types">
<label class="" title="Toggle operand types">
<input type="checkbox" on:change={() => toggleDataTypes()} />
Operand Type
</label>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/Results.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<div class="q title _{selectedBackends.length}">
<div class="m">Model</div>
<div class="mt">Model Type</div>
<div class="dt">Data Type</div>
<div class="dt">Operand Type</div>
{#if selectedBackends && selectedBackends.length > 0}
{#each selectedBackends as backend}
<div class="backend">
Expand Down
118 changes: 118 additions & 0 deletions static/coi-serviceworker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*! coi-serviceworker v0.1.7 - Guido Zuidhof and contributors, licensed under MIT */
let coepCredentialless = false;
if (typeof window === 'undefined') {
self.addEventListener("install", () => self.skipWaiting());
self.addEventListener("activate", (event) => event.waitUntil(self.clients.claim()));

self.addEventListener("message", (ev) => {
if (!ev.data) {
return;
} else if (ev.data.type === "deregister") {
self.registration
.unregister()
.then(() => {
return self.clients.matchAll();
})
.then(clients => {
clients.forEach((client) => client.navigate(client.url));
});
} else if (ev.data.type === "coepCredentialless") {
coepCredentialless = ev.data.value;
}
});

self.addEventListener("fetch", function (event) {
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(request)
.then((response) => {
if (response.status === 0) {
return response;
}

const newHeaders = new Headers(response.headers);
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");

return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: newHeaders,
});
})
.catch((e) => console.error(e))
);
});

} else {
(() => {
// You can customize the behavior of this script through a global `coi` variable.
const coi = {
shouldRegister: () => true,
shouldDeregister: () => false,
coepCredentialless: () => (window.chrome !== undefined || window.netscape !== undefined),
doReload: () => window.location.reload(),
quiet: false,
...window.coi
};

const n = navigator;

if (n.serviceWorker && n.serviceWorker.controller) {
n.serviceWorker.controller.postMessage({
type: "coepCredentialless",
value: coi.coepCredentialless(),
});

if (coi.shouldDeregister()) {
n.serviceWorker.controller.postMessage({ type: "deregister" });
}
}

// If we're already coi: do nothing. Perhaps it's due to this script doing its job, or COOP/COEP are
// already set from the origin server. Also if the browser has no notion of crossOriginIsolated, just give up here.
if (window.crossOriginIsolated !== false || !coi.shouldRegister()) return;

if (!window.isSecureContext) {
!coi.quiet && console.log("COOP/COEP Service Worker not registered, a secure context is required.");
return;
}

// In some environments (e.g. Chrome incognito mode) this won't be available
if (n.serviceWorker) {
n.serviceWorker.register(window.document.currentScript.src).then(
(registration) => {
!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();
});

// If the registration is active, but it's not controlling the page
if (registration.active && !n.serviceWorker.controller) {
!coi.quiet && console.log("Reloading page to make use of COOP/COEP Service Worker.");
coi.doReload();
}
},
(err) => {
!coi.quiet && console.error("COOP/COEP Service Worker failed to register:", err);
}
);
}
})();
}
2 changes: 2 additions & 0 deletions static/coi-serviceworker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default defineConfig({
cors: true,
headers: {
"Cache-Control": "max-age=31536000,immutable",
"Access-Control-Allow-Origin": "*",
"Cross-Origin-Opener-Policy": "same-origin",
"Cross-Origin-Embedder-Policy": "require-corp"
}
Expand Down

0 comments on commit 8c1ede6

Please sign in to comment.