forked from ChelseaKR/permit-bearings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic-server.mjs
More file actions
47 lines (44 loc) · 1.57 KB
/
Copy pathstatic-server.mjs
File metadata and controls
47 lines (44 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { createReadStream, statSync } from "node:fs";
import { createServer } from "node:http";
import { extname, resolve } from "node:path";
import { createGzip } from "node:zlib";
const port = Number.parseInt(process.argv[2] || "4173", 10);
const root = resolve(".");
const types = {
".css": "text/css; charset=utf-8",
".html": "text/html; charset=utf-8",
".js": "text/javascript; charset=utf-8",
".json": "application/json; charset=utf-8",
".png": "image/png",
".svg": "image/svg+xml",
};
const compressible = new Set([".css", ".html", ".js", ".json", ".svg"]);
createServer((request, response) => {
const pathname = new URL(request.url, `http://${request.headers.host}`).pathname;
const requested = pathname === "/" ? "/index.html" : pathname;
const path = resolve(root, `.${requested}`);
if (!path.startsWith(`${root}/`)) {
response.writeHead(403).end();
return;
}
try {
const size = statSync(path).size;
const extension = extname(path);
const headers = {
"Content-Type": types[extension] || "application/octet-stream",
"Cache-Control": "no-store",
Vary: "Accept-Encoding",
};
const shouldGzip =
size > 1024 &&
compressible.has(extension) &&
request.headers["accept-encoding"]?.includes("gzip");
if (shouldGzip) headers["Content-Encoding"] = "gzip";
response.writeHead(200, headers);
const stream = createReadStream(path);
if (shouldGzip) stream.pipe(createGzip()).pipe(response);
else stream.pipe(response);
} catch {
response.writeHead(404).end();
}
}).listen(port, "127.0.0.1");