MrNoOne07 commited on
Commit
bf4b8f9
·
1 Parent(s): 88754fb

Serve static assets in Hugging Face adapter

Browse files
Files changed (1) hide show
  1. scripts/serve-hf.mjs +68 -1
scripts/serve-hf.mjs CHANGED
@@ -1,13 +1,80 @@
1
  import { createServer } from "node:http";
 
 
 
 
2
 
3
  const port = Number(process.env.PORT ?? 7860);
4
  const host = process.env.HOST ?? "0.0.0.0";
 
5
 
6
  const { default: app } = await import("../dist/server/server.js");
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  const server = createServer(async (req, res) => {
9
  const origin = `http://${req.headers.host ?? `${host}:${port}`}`;
10
- const request = new Request(new URL(req.url ?? "/", origin), {
 
 
 
 
 
 
11
  method: req.method,
12
  headers: req.headers,
13
  body: req.method === "GET" || req.method === "HEAD" ? undefined : req,
 
1
  import { createServer } from "node:http";
2
+ import { createReadStream } from "node:fs";
3
+ import { stat } from "node:fs/promises";
4
+ import { extname, join, normalize, sep } from "node:path";
5
+ import { fileURLToPath } from "node:url";
6
 
7
  const port = Number(process.env.PORT ?? 7860);
8
  const host = process.env.HOST ?? "0.0.0.0";
9
+ const clientDir = fileURLToPath(new URL("../dist/client/", import.meta.url));
10
 
11
  const { default: app } = await import("../dist/server/server.js");
12
 
13
+ const contentTypes = {
14
+ ".css": "text/css; charset=utf-8",
15
+ ".js": "text/javascript; charset=utf-8",
16
+ ".svg": "image/svg+xml",
17
+ ".jpg": "image/jpeg",
18
+ ".jpeg": "image/jpeg",
19
+ ".png": "image/png",
20
+ ".webp": "image/webp",
21
+ ".ico": "image/x-icon",
22
+ ".json": "application/json; charset=utf-8",
23
+ };
24
+
25
+ function resolveStaticPath(pathname) {
26
+ if (!pathname.startsWith("/assets/") && pathname !== "/favicon.svg") {
27
+ return undefined;
28
+ }
29
+
30
+ const relativePath = normalize(decodeURIComponent(pathname).replace(/^\/+/, ""));
31
+ const filePath = join(clientDir, relativePath);
32
+ const clientRoot = clientDir.endsWith(sep) ? clientDir : `${clientDir}${sep}`;
33
+
34
+ if (!filePath.startsWith(clientRoot)) {
35
+ return undefined;
36
+ }
37
+
38
+ return filePath;
39
+ }
40
+
41
+ async function serveStaticAsset(req, res, pathname) {
42
+ const filePath = resolveStaticPath(pathname);
43
+ if (!filePath) return false;
44
+
45
+ try {
46
+ const fileStat = await stat(filePath);
47
+ if (!fileStat.isFile()) return false;
48
+
49
+ res.writeHead(200, {
50
+ "content-type": contentTypes[extname(filePath)] ?? "application/octet-stream",
51
+ "content-length": fileStat.size,
52
+ "cache-control": pathname.startsWith("/assets/")
53
+ ? "public, max-age=31536000, immutable"
54
+ : "public, max-age=3600",
55
+ });
56
+
57
+ if (req.method === "HEAD") {
58
+ res.end();
59
+ return true;
60
+ }
61
+
62
+ createReadStream(filePath).pipe(res);
63
+ return true;
64
+ } catch {
65
+ return false;
66
+ }
67
+ }
68
+
69
  const server = createServer(async (req, res) => {
70
  const origin = `http://${req.headers.host ?? `${host}:${port}`}`;
71
+ const url = new URL(req.url ?? "/", origin);
72
+
73
+ if (await serveStaticAsset(req, res, url.pathname)) {
74
+ return;
75
+ }
76
+
77
+ const request = new Request(url, {
78
  method: req.method,
79
  headers: req.headers,
80
  body: req.method === "GET" || req.method === "HEAD" ? undefined : req,