@t8/serve 0.1.26 → 0.1.28

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/README.md CHANGED
@@ -111,6 +111,13 @@ let server = await serve({
111
111
  path: "app",
112
112
  bundle: true, // or input path, or `{ input, output, dir }`
113
113
  spa: true,
114
+ // Optional custom request handler (e.g. for simple APIs or API mocks)
115
+ onRequest(req, res) {
116
+ if (req.url === "/items") {
117
+ res.writeHead(200, { "content-type": "application/json" });
118
+ res.end(JSON.stringify(["apple", "lemon", "cherry"]));
119
+ }
120
+ },
114
121
  });
115
122
 
116
123
  // Stop
@@ -170,7 +177,7 @@ test.afterAll(() => {
170
177
  ```ts
171
178
  // tests/x/index.test.ts
172
179
  import { test } from "@playwright/test";
173
- import { serve, type Server } from "@t8/serve";
180
+ import { type Server, serve } from "@t8/serve";
174
181
 
175
182
  let server: Server;
176
183
 
package/dist/index.js CHANGED
@@ -94,6 +94,8 @@ async function serve(config = {}) {
94
94
  await bundle(config);
95
95
  return new Promise((resolve) => {
96
96
  let server = createServer(async (req, res) => {
97
+ await config.onRequest?.(req, res);
98
+ if (res.headersSent) return;
97
99
  let filePath = await getFilePath(req.url, config);
98
100
  if (filePath === void 0) {
99
101
  res.writeHead(404, { "content-type": "text/plain" });
package/dist/run.cjs CHANGED
@@ -97,6 +97,8 @@ async function serve(config = {}) {
97
97
  await bundle(config);
98
98
  return new Promise((resolve) => {
99
99
  let server = (0, import_node_http.createServer)(async (req, res) => {
100
+ await config.onRequest?.(req, res);
101
+ if (res.headersSent) return;
100
102
  let filePath = await getFilePath(req.url, config);
101
103
  if (filePath === void 0) {
102
104
  res.writeHead(404, { "content-type": "text/plain" });
package/dist/run.mjs CHANGED
@@ -96,6 +96,8 @@ async function serve(config = {}) {
96
96
  await bundle(config);
97
97
  return new Promise((resolve) => {
98
98
  let server = createServer(async (req, res) => {
99
+ await config.onRequest?.(req, res);
100
+ if (res.headersSent) return;
99
101
  let filePath = await getFilePath(req.url, config);
100
102
  if (filePath === void 0) {
101
103
  res.writeHead(404, { "content-type": "text/plain" });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t8/serve",
3
- "version": "0.1.26",
3
+ "version": "0.1.28",
4
4
  "description": "Simple static file server + bundler, primarily for demo apps and tests, manual or automated",
5
5
  "keywords": [
6
6
  "node",
package/src/Config.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { IncomingMessage, ServerResponse } from "node:http";
1
2
  import type { BundleConfig } from "./BundleConfig";
2
3
 
3
4
  export type Config = {
@@ -38,4 +39,9 @@ export type Config = {
38
39
  * If `string`, it's equivalent to `input` in `{ input, ouput: "index.js" }`.
39
40
  */
40
41
  bundle?: boolean | string | BundleConfig | undefined;
42
+ /** Custom request handler. */
43
+ onRequest?: (
44
+ req?: IncomingMessage,
45
+ res?: ServerResponse<IncomingMessage>,
46
+ ) => void | Promise<void>;
41
47
  };
package/src/serve.ts CHANGED
@@ -14,6 +14,10 @@ export async function serve(config: Config = {}): Promise<Server> {
14
14
 
15
15
  return new Promise((resolve) => {
16
16
  let server = createServer(async (req, res) => {
17
+ await config.onRequest?.(req, res);
18
+
19
+ if (res.headersSent) return;
20
+
17
21
  let filePath = await getFilePath(req.url, config);
18
22
 
19
23
  if (filePath === undefined) {