@t8/serve 0.1.26 → 0.1.27
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 +8 -1
- package/dist/index.js +2 -0
- package/dist/run.cjs +2 -0
- package/dist/run.mjs +2 -0
- package/package.json +1 -1
- package/src/Config.ts +6 -0
- package/src/serve.ts +4 -0
package/README.md
CHANGED
|
@@ -170,7 +170,7 @@ test.afterAll(() => {
|
|
|
170
170
|
```ts
|
|
171
171
|
// tests/x/index.test.ts
|
|
172
172
|
import { test } from "@playwright/test";
|
|
173
|
-
import {
|
|
173
|
+
import { type Server, serve } from "@t8/serve";
|
|
174
174
|
|
|
175
175
|
let server: Server;
|
|
176
176
|
|
|
@@ -179,6 +179,13 @@ test.beforeAll(async () => {
|
|
|
179
179
|
path: "tests/x",
|
|
180
180
|
bundle: "src/index.tsx",
|
|
181
181
|
spa: true,
|
|
182
|
+
// Optional custom request handler
|
|
183
|
+
onRequest(req, res) {
|
|
184
|
+
if (req.url === "/items") {
|
|
185
|
+
res.writeHead(200, { "content-type": "application/json" });
|
|
186
|
+
res.end(JSON.stringify(["apple", "lemon", "cherry"]));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
182
189
|
});
|
|
183
190
|
});
|
|
184
191
|
|
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
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) {
|