srvx 0.8.12 → 0.8.14

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.
@@ -20,7 +20,9 @@ type URLInit = {
20
20
  * - Changes to `searchParams` will be discarded as we don't track them.
21
21
  */
22
22
  declare const FastURL: {
23
- new (url: string | URLInit): URL;
23
+ new (url: string | URLInit): URL & {
24
+ _url: URL;
25
+ };
24
26
  };
25
27
  //#endregion
26
28
  export { FastURL as FastURL$2 };
@@ -1,5 +1,5 @@
1
1
  import { BunFetchHandler, Server, ServerOptions } from "../_chunks/types-hx5EhU1U.mjs";
2
- import { FastURL$2 as FastURL } from "../_chunks/_url-BQeTFJZ1.mjs";
2
+ import { FastURL$2 as FastURL } from "../_chunks/_url-D-jAQyRZ.mjs";
3
3
  import * as bun from "bun";
4
4
 
5
5
  //#region src/adapters/bun.d.ts
@@ -1,5 +1,5 @@
1
1
  import { DenoFetchHandler, Server, ServerOptions } from "../_chunks/types-hx5EhU1U.mjs";
2
- import { FastURL$2 as FastURL } from "../_chunks/_url-BQeTFJZ1.mjs";
2
+ import { FastURL$2 as FastURL } from "../_chunks/_url-D-jAQyRZ.mjs";
3
3
 
4
4
  //#region src/adapters/deno.d.ts
5
5
  declare const FastResponse: typeof globalThis.Response;
@@ -1,5 +1,5 @@
1
1
  import { FetchHandler, NodeHttpHandler, NodeServerRequest, NodeServerResponse, Server, ServerOptions, ServerRequest } from "../_chunks/types-hx5EhU1U.mjs";
2
- import { FastURL$2 as FastURL } from "../_chunks/_url-BQeTFJZ1.mjs";
2
+ import { FastURL$2 as FastURL } from "../_chunks/_url-D-jAQyRZ.mjs";
3
3
  import NodeHttp from "node:http";
4
4
  import { Readable } from "node:stream";
5
5
 
@@ -69,6 +69,7 @@ function streamBody(stream, nodeRes) {
69
69
  //#endregion
70
70
  //#region src/adapters/_node/url.ts
71
71
  var NodeRequestURL = class extends FastURL {
72
+ #req;
72
73
  constructor({ req }) {
73
74
  const path = req.url || "/";
74
75
  if (path[0] === "/") {
@@ -84,6 +85,14 @@ var NodeRequestURL = class extends FastURL {
84
85
  search
85
86
  });
86
87
  } else super(path);
88
+ this.#req = req;
89
+ }
90
+ get pathname() {
91
+ return super.pathname;
92
+ }
93
+ set pathname(value) {
94
+ this._url.pathname = value;
95
+ this.#req.url = this._url.pathname + this._url.search;
87
96
  }
88
97
  };
89
98
 
package/dist/cli.mjs CHANGED
@@ -233,7 +233,7 @@ async function interceptListen(cb) {
233
233
  };
234
234
  }
235
235
  async function version() {
236
- const version$1 = "0.8.12";
236
+ const version$1 = "0.8.13";
237
237
  return `srvx ${version$1}\n${runtime()}`;
238
238
  }
239
239
  function runtime() {
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 (req.method !== "GET" && req.method !== "HEAD") return next();
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[extname(filePath)] || "application/octet-stream"
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "srvx",
3
- "version": "0.8.12",
3
+ "version": "0.8.14",
4
4
  "description": "Universal Server API based on web platform standards. Works seamlessly with Deno, Bun and Node.js.",
5
5
  "homepage": "https://srvx.h3.dev",
6
6
  "repository": "h3js/srvx",