@sourceregistry/node-webserver 1.7.5 → 1.7.6
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 +79 -18
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,21 +1,37 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
1
3
|
# @sourceregistry/node-webserver
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
[](https://www.npmjs.com/package/@sourceregistry/node-webserver)
|
|
8
|
+
[](https://www.npmjs.com/package/@sourceregistry/node-webserver)
|
|
9
|
+
[](https://jsr.io/@sourceregistry/node-webserver)
|
|
10
|
+
[](./LICENSE)
|
|
11
|
+
[](https://nodejs.org)
|
|
12
|
+
[](https://github.com/SourceRegistry/node-webserver/actions/workflows/ci.yml)
|
|
13
|
+
[](https://github.com/SourceRegistry/node-webserver/issues)
|
|
14
|
+
|
|
15
|
+
Typed router · Middleware · Route enhancers · WebSockets · SSE · Static files · CORS · Rate limiting · Security headers
|
|
16
|
+
|
|
17
|
+
[Docs](https://sourceregistry.github.io/node-webserver/) · [npm](https://www.npmjs.com/package/@sourceregistry/node-webserver) · [JSR](https://jsr.io/@sourceregistry/node-webserver) · [Issues](https://github.com/SourceRegistry/node-webserver/issues)
|
|
7
18
|
|
|
8
|
-
|
|
19
|
+
</div>
|
|
9
20
|
|
|
10
|
-
|
|
21
|
+
---
|
|
11
22
|
|
|
12
|
-
|
|
13
|
-
|
|
23
|
+
## Features
|
|
24
|
+
|
|
25
|
+
- Typed router with path params and nested routers
|
|
26
|
+
- Middleware with short-circuit support
|
|
14
27
|
- Route enhancers for typed request-scoped context
|
|
15
|
-
- Router lifecycle hooks
|
|
16
|
-
- WebSocket routing
|
|
28
|
+
- Router lifecycle hooks — `pre()` and `post()`
|
|
29
|
+
- WebSocket routing with enhancer support
|
|
30
|
+
- Server-Sent Events via `sse()`
|
|
17
31
|
- Cookie helpers
|
|
18
|
-
-
|
|
32
|
+
- Static file serving with path traversal protection
|
|
33
|
+
- Built-in middleware: CORS, rate limiting, security headers, request IDs, timeouts
|
|
34
|
+
- HTTPS support
|
|
19
35
|
- Safer defaults for host handling and WebSocket upgrade validation
|
|
20
36
|
|
|
21
37
|
## Installation
|
|
@@ -34,16 +50,54 @@ import { WebServer, json, text } from "@sourceregistry/node-webserver";
|
|
|
34
50
|
const app = new WebServer();
|
|
35
51
|
|
|
36
52
|
app.GET("/", () => text("hello world"));
|
|
53
|
+
app.GET("/health", () => json({ ok: true }));
|
|
37
54
|
|
|
38
|
-
app.
|
|
39
|
-
|
|
40
|
-
}));
|
|
55
|
+
app.listen(3000, () => console.log("listening on http://127.0.0.1:3000"));
|
|
56
|
+
```
|
|
41
57
|
|
|
42
|
-
|
|
43
|
-
|
|
58
|
+
## Overview
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import {
|
|
62
|
+
WebServer, Router, enhance, json, text, html, sse,
|
|
63
|
+
CORS, RateLimiter, RequestId, Security, Timeout,
|
|
64
|
+
error, redirect
|
|
65
|
+
} from "@sourceregistry/node-webserver";
|
|
66
|
+
|
|
67
|
+
const app = new WebServer({
|
|
68
|
+
locals: (event) => ({ requestId: crypto.randomUUID() }),
|
|
69
|
+
security: { trustedProxies: ["127.0.0.1"], maxRequestBodySize: 1024 * 1024 }
|
|
44
70
|
});
|
|
71
|
+
|
|
72
|
+
// Built-in middleware
|
|
73
|
+
app.useMiddleware(RequestId.assign());
|
|
74
|
+
app.useMiddleware(Security.headers());
|
|
75
|
+
app.useMiddleware(CORS.policy({ origin: ["https://app.example.com"], credentials: true }));
|
|
76
|
+
app.useMiddleware(RateLimiter.slidingWindowLimit({ windowMs: 60_000, max: 100 }));
|
|
77
|
+
app.useMiddleware(Timeout.deadline({ ms: 5000 }));
|
|
78
|
+
|
|
79
|
+
// Typed route enhancers
|
|
80
|
+
const withAuth = async (event) => {
|
|
81
|
+
const token = event.request.headers.get("authorization");
|
|
82
|
+
if (!token) error(401, { message: "Unauthorized" });
|
|
83
|
+
return { user: await verifyToken(token) };
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
app.GET("/me", enhance(
|
|
87
|
+
async (event) => json(event.context.user),
|
|
88
|
+
withAuth,
|
|
89
|
+
));
|
|
90
|
+
|
|
91
|
+
// Nested routers
|
|
92
|
+
const api = new Router();
|
|
93
|
+
api.GET("/status", () => json({ ok: true }));
|
|
94
|
+
app.use("/api", api);
|
|
95
|
+
|
|
96
|
+
app.listen(3000);
|
|
45
97
|
```
|
|
46
98
|
|
|
99
|
+
---
|
|
100
|
+
|
|
47
101
|
## Core Concepts
|
|
48
102
|
|
|
49
103
|
### Create a server
|
|
@@ -722,13 +776,20 @@ For a production-oriented baseline with:
|
|
|
722
776
|
|
|
723
777
|
see [examples/public-baseline.ts](./examples/public-baseline.ts).
|
|
724
778
|
|
|
779
|
+
---
|
|
780
|
+
|
|
725
781
|
## Development
|
|
726
782
|
|
|
727
783
|
```bash
|
|
728
|
-
npm test
|
|
784
|
+
npm test # run tests
|
|
785
|
+
npm run test:ui # vitest UI
|
|
786
|
+
npm run test:coverage
|
|
729
787
|
npm run build
|
|
788
|
+
npm run docs:build # generate TypeDoc
|
|
730
789
|
```
|
|
731
790
|
|
|
791
|
+
---
|
|
792
|
+
|
|
732
793
|
## License
|
|
733
794
|
|
|
734
|
-
Apache-2.0
|
|
795
|
+
[Apache-2.0](./LICENSE) © [Alexander Slaa](https://github.com/SourceRegistry)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sourceregistry/node-webserver",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.6",
|
|
4
4
|
"description": "TypeScript web server for Node.js with web-standard Request and Response APIs",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs.js",
|
|
@@ -105,6 +105,7 @@
|
|
|
105
105
|
{
|
|
106
106
|
"assets": [
|
|
107
107
|
"package.json",
|
|
108
|
+
"package-lock.json",
|
|
108
109
|
"jsr.json",
|
|
109
110
|
"CHANGELOG.md"
|
|
110
111
|
],
|