@bepalo/router 1.1.8 → 1.1.10
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 +38 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -126,12 +126,14 @@ interface RouterContext {
|
|
|
126
126
|
address?: SocketAddress | null; // Client address
|
|
127
127
|
response?: Response; // Final response
|
|
128
128
|
error?: Error; // Caught error
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
129
|
+
found: {
|
|
130
|
+
hooks: boolean; // Whether hooks were found
|
|
131
|
+
afters: boolean; // Whether afters were found
|
|
132
|
+
filters: boolean; // Whether filters were found
|
|
133
|
+
handlers: boolean; // Whether handlers were found
|
|
134
|
+
fallbacks: boolean; // Whether fallbacks were found
|
|
135
|
+
catchers: boolean; // Whether catchers were found
|
|
136
|
+
};
|
|
135
137
|
}
|
|
136
138
|
```
|
|
137
139
|
|
|
@@ -256,6 +258,8 @@ import {
|
|
|
256
258
|
parseUploadStreaming, // multi-part-form-data and file upload stream parser
|
|
257
259
|
} from "@bepalo/router";
|
|
258
260
|
|
|
261
|
+
const router = new Router();
|
|
262
|
+
|
|
259
263
|
// Usage examples
|
|
260
264
|
router.handle("GET /text", () => text("Hello World"));
|
|
261
265
|
router.handle("GET /html", () => html("<h1>Title</h1>"));
|
|
@@ -263,10 +267,12 @@ router.handle("GET /json", () => json({ data: "value" }));
|
|
|
263
267
|
router.handle("GET /status", () => status(204, null)); // No Content
|
|
264
268
|
router.handle("GET /intro.mp4", () => blob(Bun.file("./intro.mp4")));
|
|
265
269
|
router.handle("GET /download", () => octetStream(Bun.file("./intro.mp4")));
|
|
270
|
+
|
|
266
271
|
router.handle<CTXCookie>("GET /cookie", [
|
|
267
272
|
parseCookie(),
|
|
268
273
|
(req, { cookie }) => json({ cookie }),
|
|
269
274
|
]);
|
|
275
|
+
|
|
270
276
|
router.handle<CTXBody>("POST /cookie", [
|
|
271
277
|
parseBody(),
|
|
272
278
|
(req, { body }) => {
|
|
@@ -282,12 +288,14 @@ router.handle<CTXBody>("POST /cookie", [
|
|
|
282
288
|
});
|
|
283
289
|
},
|
|
284
290
|
]);
|
|
291
|
+
|
|
285
292
|
router.handle("DELETE /cookie/:name", [
|
|
286
293
|
(req, ctx) =>
|
|
287
294
|
status(200, "OK", {
|
|
288
295
|
headers: [clearCookie(ctx.params.name, { path: "/" })],
|
|
289
296
|
}),
|
|
290
297
|
]);
|
|
298
|
+
|
|
291
299
|
router.handle<CTXUpload>("POST /upload", [
|
|
292
300
|
(req, ctx) => {
|
|
293
301
|
let file: Bun.BunFile;
|
|
@@ -441,7 +449,10 @@ import {
|
|
|
441
449
|
} from "@bepalo/router";
|
|
442
450
|
|
|
443
451
|
const router = new Router<RouterContext & CTXAddress>({
|
|
444
|
-
defaultHeaders: [
|
|
452
|
+
defaultHeaders: () => [
|
|
453
|
+
["X-Powered-By", "@bepalo/router"],
|
|
454
|
+
["Date", new Date().toUTCString()]
|
|
455
|
+
],
|
|
445
456
|
});
|
|
446
457
|
|
|
447
458
|
// Global CORS
|
|
@@ -449,18 +460,27 @@ router.filter("*", [
|
|
|
449
460
|
cors({
|
|
450
461
|
origins: ["http://localhost:3000", "https://example.com"],
|
|
451
462
|
methods: ["GET", "POST", "PUT", "DELETE"],
|
|
463
|
+
allowedHeaders: ["Content-Type", "Authorization"],
|
|
452
464
|
credentials: true,
|
|
453
465
|
}),
|
|
454
466
|
]);
|
|
455
467
|
|
|
456
468
|
// Rate limiting for API
|
|
457
|
-
router.filter(
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
469
|
+
router.filter(
|
|
470
|
+
[
|
|
471
|
+
"GET /api/.**",
|
|
472
|
+
"POST /api/.**",
|
|
473
|
+
"PUT /api/.**",
|
|
474
|
+
"PATCH /api/.**",
|
|
475
|
+
"DELETE /api/.**",
|
|
476
|
+
],
|
|
477
|
+
[
|
|
478
|
+
limitRate({
|
|
479
|
+
key: (req, ctx) => ctx.address.address,
|
|
480
|
+
maxTokens: 100,
|
|
481
|
+
refillRate: 10, // 10 tokens per second
|
|
482
|
+
setXRateLimitHeaders: true,
|
|
483
|
+
}),
|
|
464
484
|
]);
|
|
465
485
|
|
|
466
486
|
// Routes
|
|
@@ -504,7 +524,10 @@ router.fallback("*", () => json({ error: "Not found" }, { status: 404 }));
|
|
|
504
524
|
Bun.serve({
|
|
505
525
|
port: 3000,
|
|
506
526
|
async fetch(req, server) {
|
|
507
|
-
const address = server.requestIP(req) as SocketAddress;
|
|
527
|
+
const address = server.requestIP(req) as SocketAddress | null;
|
|
528
|
+
if(!address) {
|
|
529
|
+
throw new Error("null client address");
|
|
530
|
+
}
|
|
508
531
|
return await router.respond(req, { address });
|
|
509
532
|
},
|
|
510
533
|
});
|