Skip to content
Source
Sign inSign up
samwillis/jerry
Open History to see the last commit

web-assets.ts

42 lines1.3 KB

1import { readdir, readFile } from "node:fs/promises";
2import { extname } from "node:path";
3​
4const mime: Record<string, string> = {
5 ".js": "text/javascript",
6 ".css": "text/css",
7};
8​
9export async function loadWebAssets(output: string) {
10 const assets: Record<string, { body: Uint8Array; type: string }> = {};
11 const shell = await readFile(output + "/_shell.html");
12 for (const route of ["/", "/login", "/consent"]) {
13 assets[route] = { body: shell, type: "text/html; charset=utf-8" };
14 }
15 for (const file of await readdir(output + "/assets")) {
16 const type = mime[extname(file)];
17 if (!type) throw new Error("Unexpected web asset: " + file);
18 assets["/assets/" + file] = {
19 body: await readFile(output + "/assets/" + file),
20 type,
21 };
22 }
23 for (const [, path] of shell
24 .toString()
25 .matchAll(/(?:src|href)="(\/assets\/[^"]+)"/g)) {
26 if (!assets[path])
27 throw new Error("Start shell references a missing asset: " + path);
28 }
29 assets["/jol-logo.svg"] = {
30 body: await readFile("web/jol-logo.svg"),
31 type: "image/svg+xml",
32 };
33 assets["/Caveat.ttf"] = {
34 body: await readFile("../Jerry/Resources/Caveat.ttf"),
35 type: "font/ttf",
36 };
37 assets["/Caveat-OFL.txt"] = {
38 body: await readFile("../Jerry/Resources/Caveat-OFL.txt"),
39 type: "text/plain",
40 };
41 return assets;