Skip to content

Commit 47b6fdd

Browse files
committed
un nest sw handler in anura-sw, newSwOpfs -> newRootOpfs
1 parent 8a8e6b2 commit 47b6fdd

File tree

4 files changed

+88
-94
lines changed

4 files changed

+88
-94
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ submodules: .gitmodules
3434

3535
external-libs: build/libs/filer/filer.min.js build/libs/mime/mime.iife.js build/libs/nfsadapter/nfsadapter.js build/libs/comlink/comlink.min.mjs build/libs/workbox/version build/libs/idb-keyval/idb-keyval.js build/libs/fflate/browser.js bin/chimerix.ajs build/libs/libcurl/version build/libs/bare-mux/bare.cjs build/uv/uv.bundle.js build/libs/dreamland/all.js
3636
mkdir -p build/libs/
37+
3738
build/libs/libcurl/version: build/bootstrap
3839
mkdir -p build/libs/libcurl
3940
cp node_modules/libcurl.js/libcurl.mjs build/libs/libcurl/libcurl.mjs

public/anura-sw.js

Lines changed: 85 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const bootStrapFSReady = new Promise((res, rej) => {
3838
.get("bootFromOPFS")
3939
.then(async (res) => {
4040
if (res) {
41-
opfs = await LocalFS.newSwOPFS();
41+
opfs = await LocalFS.newRootOPFS();
4242
globalThis.anura = { fs: opfs }; // Stupid thing for AFSShell compat
4343
opfssh = new AFSShell();
4444
}
@@ -640,107 +640,100 @@ workbox.routing.registerRoute(
640640

641641
workbox.routing.registerRoute(
642642
/^(?!.*(\/config.json|\/MILESTONE|\/x86images\/|\/service\/))/,
643-
({ url }) => {
644-
if (new URL(url).origin !== self.location.origin) return false;
645-
return (async () => {
646-
await bootStrapFSReady;
647-
if (cacheenabled === undefined) {
648-
console.debug("retrieving cache value");
649-
let result = await idbKeyval.get("cacheenabled");
650-
if (result !== undefined || result !== null) {
651-
cacheenabled = result;
652-
}
653-
}
654-
if (
655-
(!cacheenabled && url.pathname === "/" && !navigator.onLine) ||
656-
(!cacheenabled && url.pathname === "/index.html" && !navigator.onLine)
657-
) {
658-
return new Response(offlineError(), {
659-
status: 500,
660-
headers: { "content-type": "text/html" },
661-
});
662-
}
663-
if (!cacheenabled) {
664-
const fetchResponse = await fetch(url);
665-
return new Response(await fetchResponse.arrayBuffer(), {
666-
headers: {
667-
...Object.fromEntries(fetchResponse.headers.entries()),
668-
...corsheaders,
669-
},
670-
});
671-
672-
return fetchResponse;
673-
}
674-
if (url.pathname === "/") {
675-
url.pathname = "/index.html";
676-
}
677-
if (url.password)
678-
return new Response(
679-
"<script>window.location.href = window.location.href</script>",
680-
{ headers: { "content-type": "text/html" } },
681-
);
682-
const basepath = "/anura_files";
683-
let path = decodeURI(url.pathname);
684-
685-
// Force Filer to be used in cache routes, as it does not require waiting for anura to be connected
686-
const fs = opfs || filerfs;
687-
const sh = opfssh || filersh;
643+
async (event) => {
644+
if (new URL(event.url).origin !== self.location.origin) return false;
645+
await bootStrapFSReady;
646+
if (cacheenabled === undefined) {
647+
console.debug("retrieving cache value");
648+
cacheenabled = await idbKeyval.get("cacheenabled");
649+
}
650+
if (
651+
(!cacheenabled && event.url.pathname === "/" && !navigator.onLine) ||
652+
(!cacheenabled &&
653+
event.url.pathname === "/index.html" &&
654+
!navigator.onLine)
655+
) {
656+
return new Response(offlineError(), {
657+
status: 500,
658+
headers: { "content-type": "text/html" },
659+
});
660+
}
661+
if (!cacheenabled) {
662+
const fetchResponse = await fetch(event.request);
663+
return new Response(await fetchResponse.arrayBuffer(), {
664+
headers: {
665+
...Object.fromEntries(fetchResponse.headers.entries()),
666+
...corsheaders,
667+
},
668+
});
669+
}
670+
if (event.url.pathname === "/") event.url.pathname = "/index.html";
671+
if (event.url.password)
672+
return new Response(
673+
"<script>window.location.href = window.location.href</script>",
674+
{ headers: { "content-type": "text/html" } },
675+
);
676+
const basepath = "/anura_files";
677+
let path = decodeURI(event.url.pathname);
688678

689-
const response = await serveFile(`${basepath}${path}`, fs, sh);
679+
// Force Filer to be used in cache routes, as it does not require waiting for anura to be connected
680+
const fs = opfs || filerfs;
681+
const sh = opfssh || filersh;
690682

691-
if (response.ok) {
692-
return response;
693-
} else {
694-
try {
695-
const fetchResponse = await fetch(url);
696-
// Promise so that we can return the response before we cache it, for faster response times
697-
return new Promise(async (resolve) => {
698-
const corsResponse = new Response(
699-
await fetchResponse.clone().arrayBuffer(),
700-
{
701-
headers: {
702-
...Object.fromEntries(fetchResponse.headers.entries()),
703-
...corsheaders,
704-
},
705-
},
706-
);
683+
const response = await serveFile(`${basepath}${path}`, fs, sh);
707684

708-
resolve(corsResponse);
709-
710-
if (fetchResponse.ok) {
711-
const buffer = await fetchResponse.clone().arrayBuffer();
712-
await sh.promises.mkdirp(
713-
`${basepath}${path.replace(/[^/]*$/g, "")}`,
714-
);
715-
// Explicitly use Filer's fs here, as
716-
// Buffers lose their inheritance when passed
717-
// to anura's fs, causing them to be treated as
718-
// strings
719-
await fs.promises.writeFile(
720-
`${basepath}${path}`,
721-
Buffer.from(buffer),
722-
);
723-
}
724-
}).catch((e) => {
725-
console.error("I hate this bug: ", e);
726-
});
727-
} catch (e) {
728-
return new Response(
729-
JSON.stringify({
730-
error: e.message,
731-
status: 500,
732-
}),
685+
if (response.ok) {
686+
return response;
687+
} else {
688+
try {
689+
const fetchResponse = await fetch(event.request);
690+
// Promise so that we can return the response before we cache it, for faster response times
691+
return new Promise(async (resolve) => {
692+
const corsResponse = new Response(
693+
await fetchResponse.clone().arrayBuffer(),
733694
{
734-
status: 500,
735695
headers: {
736-
"Content-Type": "application/json",
696+
...Object.fromEntries(fetchResponse.headers.entries()),
737697
...corsheaders,
738698
},
739699
},
740700
);
741-
}
701+
702+
resolve(corsResponse);
703+
704+
if (fetchResponse.ok) {
705+
const buffer = await fetchResponse.clone().arrayBuffer();
706+
await sh.promises.mkdirp(
707+
`${basepath}${path.replace(/[^/]*$/g, "")}`,
708+
);
709+
// Explicitly use Filer's fs here, as
710+
// Buffers lose their inheritance when passed
711+
// to anura's fs, causing them to be treated as
712+
// strings
713+
await fs.promises.writeFile(
714+
`${basepath}${path}`,
715+
Buffer.from(buffer),
716+
);
717+
}
718+
}).catch((e) => {
719+
console.error("I hate this bug: ", e);
720+
});
721+
} catch (e) {
722+
return new Response(
723+
JSON.stringify({
724+
error: e.message,
725+
status: 500,
726+
}),
727+
{
728+
status: 500,
729+
headers: {
730+
"Content-Type": "application/json",
731+
...corsheaders,
732+
},
733+
},
734+
);
742735
}
743-
})();
736+
}
744737
},
745738
);
746739

src/Anura.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Anura {
6161
}),
6262
);
6363
if (await (window as any).idbKeyval.get("bootFromOPFS")) {
64-
fsProvider = (await LocalFS.newSwOPFS()) as any;
64+
fsProvider = (await LocalFS.newRootOPFS()) as any;
6565
}
6666
const fs = new AnuraFilesystem([fsProvider]);
6767

src/api/LocalFS.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class LocalFS extends AFSProvider<LocalFSStats> {
193193

194194
return fs;
195195
}
196-
static async newSwOPFS() {
196+
static async newRootOPFS() {
197197
const anuraPath = "/";
198198
const dirHandle = await navigator.storage.getDirectory();
199199

0 commit comments

Comments
 (0)