Skip to content

Commit

Permalink
[#54869] Add enable-threads script
Browse files Browse the repository at this point in the history
  • Loading branch information
GPlaczek committed Feb 12, 2024
1 parent 01b4cea commit 9ea9c75
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 10 deletions.
21 changes: 11 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ third_party_dist_dir := $(dist_dir)/third_party
assets_dir := $(project_dir)/src/assets
third_party_dir := $(project_dir)/third_party

js_virtualfs := $(third_party_dir)/vfs.js
js_virtualfs_dist := $(third_party_dist_dir)/vfs.js

index := $(project_dir)/src/index.html
index_dist := $(dist_dir)/index.html

enable_threads := $(third_party_dir)/enable_threads.js
enable_threads_dist := $(dist_dir)/enable_threads.js

resources := $(subst $(assets_dir),$(resources_dir),$(wildcard $(assets_dir)/*))
third_party_sources := $(subst $(third_party_dir),$(third_party_dist_dir),$(wildcard $(third_party_dir)/*.js))
third_party_sources__ := $(subst $(third_party_dir),$(third_party_dist_dir),$(wildcard $(third_party_dir)/*.js))
third_party_sources := $(filter-out $(third_party_dist_dir)/enable_threads.js,$(third_party_sources__))

wash := $(resources_dir)/wash
wash_md5 := $(resources_dir)/wash.md5
Expand All @@ -27,10 +28,10 @@ coreutils_url := https://github.com/antmicro/coreutils/releases/download/v0.1.0/
VERSION := $(shell cat $(project_dir)/src/VERSION)

.PHONY: standalone
standalone: embed $(resources) $(index_dist) $(wash) $(wash_md5) $(wasibox) $(coreutils)
standalone: embed $(resources) $(index_dist) $(enable_threads_dist) $(wash) $(wash_md5) $(wasibox) $(coreutils) $(third_party_sources)

.PHONY: embed
embed: $(js_virtualfs_dist) $(third_party_sources)
embed: $(third_party_dist_dir)/vfs.js $(third_party_dist_dir)/idb-keyval.js
tsc

.PHONY: test
Expand Down Expand Up @@ -58,15 +59,15 @@ $(resources_dir)/motd.txt: $(assets_dir)/motd.txt $(project_dir)/src/VERSION | $
VERSION="$(shell printf '%*s%s' $((25 - $(shell echo $(VERSION) | wc -c))) 25 $(VERSION))" \
envsubst <$(assets_dir)/motd.txt > $(resources_dir)/motd.txt

$(js_virtualfs_dist): $(js_virtualfs) | $(third_party_dist_dir)
cp $(js_virtualfs) $(js_virtualfs_dist)

$(third_party_dist_dir)/%.js: $(third_party_dir)/%.js | $(third_party_dist_dir)
cp $< $@

$(index_dist): $(index)
$(index_dist): $(project_dir)/src/index.html $(index) | $(dist_dir)
cp $(index) $(index_dist)

$(enable_threads_dist): $(third_party_dir)/enable_threads.js $(enable_threads) | $(dist_dir)
cp $(enable_threads) $(enable_threads_dist)

$(wash): | $(resources_dir)
wget -qO $(wash) $(wash_url) || { rm -f $(wash); exit 1; }

Expand Down
1 change: 1 addition & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<meta charset="utf-8" />
<title>Antmicro's WASM shell</title>
<link rel="icon" type="image/x-icon" href="resources/favicon.ico">
<script src="./enable_threads.js"></script>
</head>
<body style="background: black">
<script type="module">
Expand Down
75 changes: 75 additions & 0 deletions third_party/enable_threads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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.
// 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.

/* Edited version of: coi-serviceworker v0.1.6 - Guido Zuidhof, licensed under MIT */
// From here: https://github.com/gzuidhof/coi-serviceworker
if(typeof window === 'undefined') {
self.addEventListener("install", () => self.skipWaiting());
self.addEventListener("activate", e => e.waitUntil(self.clients.claim()));

async function handleFetch(request) {
if(request.cache === "only-if-cached" && request.mode !== "same-origin") {
return;
}

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#c7
request = new Request(request.url, {
cache: request.cache,
credentials: "omit",
headers: request.headers,
integrity: request.integrity,
destination: request.destination,
keepalive: request.keepalive,
method: request.method,
mode: request.mode,
redirect: request.redirect,
referrer: request.referrer,
referrerPolicy: request.referrerPolicy,
signal: request.signal,
});
}

let r = await fetch(request).catch(e => console.error(e));

if(r.status === 0) {
return r;
}

const headers = new Headers(r.headers);
headers.set("Cross-Origin-Embedder-Policy", "credentialless"); // or: require-corp
headers.set("Cross-Origin-Opener-Policy", "same-origin");

return new Response(r.body, { status: r.status, statusText: r.statusText, headers });
}

self.addEventListener("fetch", function(e) {
e.respondWith(handleFetch(e.request)); // respondWith must be executed synchonously (but can be passed a Promise)
});

} else {
(async function() {
if(window.crossOriginIsolated !== false) return;

let registration = await navigator.serviceWorker.register(window.document.currentScript.src).catch(e => console.error("COOP/COEP Service Worker failed to register:", e));
if(registration) {
console.log("COOP/COEP Service Worker registered", registration.scope);

registration.addEventListener("updatefound", () => {
console.log("Reloading page to make use of updated COOP/COEP Service Worker.");
window.location.reload();
});

// If the registration is active, but it's not controlling the page
if(registration.active && !navigator.serviceWorker.controller) {
console.log("Reloading page to make use of COOP/COEP Service Worker.");
window.location.reload();
}
}
})();
}

// Code to deregister:
// let registrations = await navigator.serviceWorker.getRegistrations();
// for(let registration of registrations) {
// await registration.unregister();
// }

0 comments on commit 9ea9c75

Please sign in to comment.