srvx 0.8.13 → 0.8.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/static.d.mts +15 -0
- package/dist/static.mjs +10 -3
- package/package.json +1 -1
package/dist/static.d.mts
CHANGED
|
@@ -2,7 +2,22 @@ import { ServerMiddleware } from "./_chunks/types-hx5EhU1U.mjs";
|
|
|
2
2
|
|
|
3
3
|
//#region src/static.d.ts
|
|
4
4
|
interface ServeStaticOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The directory to serve static files from.
|
|
7
|
+
*/
|
|
5
8
|
dir: string;
|
|
9
|
+
/**
|
|
10
|
+
* The HTTP methods to allow for serving static files.
|
|
11
|
+
*/
|
|
12
|
+
methods?: string[];
|
|
13
|
+
/**
|
|
14
|
+
* A function to modify the HTML content before serving it.
|
|
15
|
+
*/
|
|
16
|
+
renderHTML?: (ctx: {
|
|
17
|
+
request: Request;
|
|
18
|
+
html: string;
|
|
19
|
+
filename: string;
|
|
20
|
+
}) => Response | Promise<Response>;
|
|
6
21
|
}
|
|
7
22
|
declare const serveStatic: (options: ServeStaticOptions) => ServerMiddleware;
|
|
8
23
|
//#endregion
|
package/dist/static.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { extname, join, resolve } from "node:path";
|
|
2
2
|
import { createReadStream } from "node:fs";
|
|
3
|
-
import { stat } from "node:fs/promises";
|
|
3
|
+
import { readFile, stat } from "node:fs/promises";
|
|
4
4
|
import { FastResponse } from "srvx";
|
|
5
5
|
import { createBrotliCompress, createGzip } from "node:zlib";
|
|
6
6
|
|
|
@@ -30,8 +30,9 @@ const COMMON_MIME_TYPES = {
|
|
|
30
30
|
};
|
|
31
31
|
const serveStatic = (options) => {
|
|
32
32
|
const dir = resolve(options.dir) + "/";
|
|
33
|
+
const methods = new Set((options.methods || ["GET", "HEAD"]).map((m) => m.toUpperCase()));
|
|
33
34
|
return async (req, next) => {
|
|
34
|
-
if (
|
|
35
|
+
if (!methods.has(req.method)) return next();
|
|
35
36
|
const path = new URL(req.url).pathname.slice(1).replace(/\/$/, "");
|
|
36
37
|
let paths;
|
|
37
38
|
if (path === "") paths = ["index.html"];
|
|
@@ -42,10 +43,16 @@ const serveStatic = (options) => {
|
|
|
42
43
|
if (!filePath.startsWith(dir)) continue;
|
|
43
44
|
const fileStat = await stat(filePath).catch(() => null);
|
|
44
45
|
if (fileStat?.isFile()) {
|
|
46
|
+
const fileExt = extname(filePath);
|
|
45
47
|
const headers = {
|
|
46
48
|
"Content-Length": fileStat.size.toString(),
|
|
47
|
-
"Content-Type": COMMON_MIME_TYPES[
|
|
49
|
+
"Content-Type": COMMON_MIME_TYPES[fileExt] || "application/octet-stream"
|
|
48
50
|
};
|
|
51
|
+
if (options.renderHTML && fileExt === ".html") return options.renderHTML({
|
|
52
|
+
html: await readFile(filePath, "utf8"),
|
|
53
|
+
filename: filePath,
|
|
54
|
+
request: req
|
|
55
|
+
});
|
|
49
56
|
let stream = createReadStream(filePath);
|
|
50
57
|
const acceptEncoding = req.headers.get("accept-encoding") || "";
|
|
51
58
|
if (acceptEncoding.includes("br")) {
|
package/package.json
CHANGED