refactor: modularize browser client
This commit is contained in:
1 parent
9c1a6acbb5
commit
f061dd2f38
8 files changed
+1279
-1226
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,30 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import type { ServerResponse } from "node:http";
|
||||
|
||||
const CLIENT_ASSET_PREFIX = "/client/";
|
||||
|
||||
export function isClientAssetPath(pathname: string): boolean {
|
||||
return pathname.startsWith(CLIENT_ASSET_PREFIX);
|
||||
}
|
||||
|
||||
export async function handleClientAssetRequest(url: URL, res: ServerResponse): Promise<void> {
|
||||
const assetName = url.pathname.slice(CLIENT_ASSET_PREFIX.length);
|
||||
|
||||
if (!/^[a-z0-9-]+\.js$/i.test(assetName)) {
|
||||
res.statusCode = 404;
|
||||
res.end("Not found");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const assetUrl = new URL(`../client/${assetName}`, import.meta.url);
|
||||
const source = await readFile(assetUrl, "utf8");
|
||||
res.statusCode = 200;
|
||||
res.setHeader("content-type", "text/javascript; charset=utf-8");
|
||||
res.setHeader("cache-control", "no-cache");
|
||||
res.end(source);
|
||||
} catch {
|
||||
res.statusCode = 404;
|
||||
res.end("Not found");
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { APP_STYLES, CLIENT_SCRIPT } from "./app-assets.js";
|
||||
import { APP_STYLES } from "./app-assets.js";
|
||||
|
||||
export function htmlPage(): string {
|
||||
return `<!doctype html>
|
||||
@@ -154,9 +154,7 @@ ${APP_STYLES}
|
||||
</div>
|
||||
|
||||
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js" integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
|
||||
<script type="module">
|
||||
${CLIENT_SCRIPT}
|
||||
</script>
|
||||
<script type="module" src="/client/app.js"></script>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
@@ -1,3 +1,7 @@
|
||||
import {
|
||||
handleClientAssetRequest,
|
||||
isClientAssetPath
|
||||
} from "./client-assets.js";
|
||||
import { htmlPage } from "./page-template.js";
|
||||
import {
|
||||
handleNextcloudBlobRequest,
|
||||
@@ -16,6 +20,11 @@ export function createRequestHandler() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isClientAssetPath(url.pathname)) {
|
||||
await handleClientAssetRequest(url, res);
|
||||
return;
|
||||
}
|
||||
|
||||
if (url.pathname === "/api/nextcloud/list") {
|
||||
await handleNextcloudListRequest(url, res);
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user