@jsenv/server 17.6.1 → 17.6.3
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 +47 -48
- package/dist/jsenv_server.js +1336 -1270
- package/package.json +2 -2
- package/src/content_negotiation/pick_content_encoding.js +11 -1
- package/src/content_negotiation/pick_content_language.js +11 -1
- package/src/content_negotiation/pick_content_type.js +11 -1
- package/src/content_negotiation/pick_content_version.js +1 -1
- package/src/interfacing_with_node/from_node_request.js +154 -178
- package/src/interfacing_with_node/observable_from_file_handle.js +1 -18
- package/src/interfacing_with_node/observable_from_node_stream.js +29 -65
- package/src/interfacing_with_node/write_node_response.js +59 -103
- package/src/internal/allowed_hosts.js +85 -0
- package/src/internal/escape_html.js +11 -0
- package/src/internal/{headersFromObject.js → headers_from_object.js} +2 -2
- package/src/internal/listen.js +32 -5
- package/src/internal/{listenRequest.js → listen_request.js} +1 -1
- package/src/internal/{listenServerConnectionError.js → listen_server_connection_error.js} +1 -1
- package/src/internal/{multiple-header.js → multiple_header.js} +32 -10
- package/src/internal/reason_phrase.js +17 -0
- package/src/internal/response_composition.js +11 -2
- package/src/internal/secure_server.js +22 -0
- package/src/internal/server_origins.js +78 -0
- package/src/internal/{server-polyglot.js → server_polyglot.js} +13 -38
- package/src/internal/track_server_pending_connections.js +50 -0
- package/src/internal/track_server_pending_requests.js +47 -0
- package/src/plugins/cors/server_plugin_cors.js +39 -6
- package/src/plugins/default_body_4xx_5xx/server_plugin_default_body_4xx_5xx.js +37 -29
- package/src/plugins/error_handler/server_plugin_error_handler.js +26 -10
- package/src/plugins/filesystem/fetch_directory.js +16 -3
- package/src/plugins/filesystem/fetch_file.js +80 -21
- package/src/plugins/filesystem/filesystem_error_to_response.js +12 -6
- package/src/plugins/request_aliases/server_plugin_request_aliases.js +8 -0
- package/src/plugins/response_acceptance_check/server_plugin_response_acceptance_check.js +11 -6
- package/src/plugins/route_inspector/server_plugin_route_inspector.js +11 -5
- package/src/plugins/server_plugins_controller.js +27 -13
- package/src/progressive_response.js +24 -0
- package/src/router/permissions.js +52 -0
- package/src/router/router.js +18 -80
- package/src/server_events.js +3 -1
- package/src/server_timing/timing_header.js +1 -1
- package/src/start_server.js +266 -416
- package/src/internal/headersFromString.js +0 -21
- package/src/internal/headersToObject.js +0 -7
- package/src/internal/headersToString.js +0 -16
- package/src/internal/listenClientError.js +0 -27
- package/src/internal/trackServerPendingConnections.js +0 -145
- package/src/internal/trackServerPendingRequests.js +0 -68
- package/src/request_body_handling.js +0 -22
- /package/src/internal/{colorizeResponseStatus.js → colorize_response_status.js} +0 -0
- /package/src/internal/{listenEvent.js → listen_event.js} +0 -0
- /package/src/internal/{normalizeHeaderName.js → normalize_header_name.js} +0 -0
- /package/src/internal/{normalizeHeaderValue.js → normalize_header_value.js} +0 -0
package/README.md
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
# server [](https://www.npmjs.com/package/@jsenv/server)
|
|
2
2
|
|
|
3
|
-
> A
|
|
4
|
-
|
|
5
|
-
`@jsenv/server` simplifies server development with a declarative API that handles common web server needs like routing, content negotiation, file serving, and real-time communication.
|
|
3
|
+
> A Node.js HTTP server with declarative routing, content negotiation, file serving, WebSocket and Server-Sent Events.
|
|
6
4
|
|
|
7
5
|
```js
|
|
8
6
|
import { startServer } from "@jsenv/server";
|
|
@@ -12,7 +10,7 @@ await startServer({
|
|
|
12
10
|
routes: [
|
|
13
11
|
{
|
|
14
12
|
endpoint: "GET *",
|
|
15
|
-
|
|
13
|
+
fetch: () => new Response("Hello world"),
|
|
16
14
|
},
|
|
17
15
|
],
|
|
18
16
|
});
|
|
@@ -20,16 +18,15 @@ await startServer({
|
|
|
20
18
|
|
|
21
19
|
# Features
|
|
22
20
|
|
|
23
|
-
-
|
|
24
|
-
-
|
|
25
|
-
-
|
|
26
|
-
-
|
|
27
|
-
-
|
|
28
|
-
-
|
|
29
|
-
-
|
|
30
|
-
-
|
|
31
|
-
-
|
|
32
|
-
- [ ] Request authentification with JWT, OAuth etc
|
|
21
|
+
- Declarative routing: `"GET /users/:id"`, the first route returning a response wins
|
|
22
|
+
- 404, 405, 406, 415 and OPTIONS responses derived from the routes
|
|
23
|
+
- Content negotiation for media type, language, version and encoding
|
|
24
|
+
- File serving with etag/mtime client cache and compression
|
|
25
|
+
- WebSocket and Server-Sent Events, with a broadcast helper
|
|
26
|
+
- HTTPS, with http → https redirection; http2 as an option
|
|
27
|
+
- CORS, server timing and error pages as plugins
|
|
28
|
+
- Host header check (DNS rebinding) and bounded request bodies
|
|
29
|
+
- A route inspector at `/.internal/route_inspector`
|
|
33
30
|
|
|
34
31
|
# Installation
|
|
35
32
|
|
|
@@ -37,14 +34,11 @@ await startServer({
|
|
|
37
34
|
npm install @jsenv/server
|
|
38
35
|
```
|
|
39
36
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
- Node.js 22.13.1 or higher
|
|
43
|
-
- ES modules support
|
|
37
|
+
Requires Node.js 22.13.1 or higher.
|
|
44
38
|
|
|
45
|
-
# Quick
|
|
39
|
+
# Quick examples
|
|
46
40
|
|
|
47
|
-
**
|
|
41
|
+
**API server**
|
|
48
42
|
|
|
49
43
|
```js
|
|
50
44
|
import { startServer } from "@jsenv/server";
|
|
@@ -54,37 +48,44 @@ await startServer({
|
|
|
54
48
|
routes: [
|
|
55
49
|
{
|
|
56
50
|
endpoint: "GET /api/users",
|
|
57
|
-
|
|
51
|
+
fetch: () => Response.json([{ id: 1, name: "John" }]),
|
|
58
52
|
},
|
|
59
53
|
{
|
|
60
54
|
endpoint: "GET /api/users/:id",
|
|
61
|
-
|
|
55
|
+
fetch: (request) =>
|
|
62
56
|
Response.json({ id: request.params.id, name: "John" }),
|
|
63
57
|
},
|
|
64
58
|
{
|
|
65
|
-
endpoint: "
|
|
66
|
-
|
|
59
|
+
endpoint: "POST /api/users",
|
|
60
|
+
acceptedMediaTypes: ["application/json"],
|
|
61
|
+
fetch: async (request) => {
|
|
62
|
+
const user = await request.json();
|
|
63
|
+
return Response.json(user, { status: 201 });
|
|
64
|
+
},
|
|
67
65
|
},
|
|
68
66
|
],
|
|
69
67
|
});
|
|
70
68
|
```
|
|
71
69
|
|
|
72
|
-
**Static
|
|
70
|
+
**Static file server**
|
|
73
71
|
|
|
74
72
|
```js
|
|
75
|
-
import {
|
|
73
|
+
import { createFileSystemFetch, startServer } from "@jsenv/server";
|
|
76
74
|
|
|
77
75
|
await startServer({
|
|
78
76
|
routes: [
|
|
79
77
|
{
|
|
80
78
|
endpoint: "GET *",
|
|
81
|
-
|
|
79
|
+
fetch: createFileSystemFetch(import.meta.resolve("./public/"), {
|
|
80
|
+
etagEnabled: true,
|
|
81
|
+
compressionEnabled: true,
|
|
82
|
+
}),
|
|
82
83
|
},
|
|
83
84
|
],
|
|
84
85
|
});
|
|
85
86
|
```
|
|
86
87
|
|
|
87
|
-
**HTTPS
|
|
88
|
+
**HTTPS server**
|
|
88
89
|
|
|
89
90
|
```js
|
|
90
91
|
import { readFileSync } from "node:fs";
|
|
@@ -95,16 +96,10 @@ await startServer({
|
|
|
95
96
|
certificate: readFileSync(new URL("./server.crt", import.meta.url), "utf8"),
|
|
96
97
|
privateKey: readFileSync(new URL("./server.key", import.meta.url), "utf8"),
|
|
97
98
|
},
|
|
98
|
-
allowHttpRequestOnHttps: true, // will disable https redirection and let you handle http request
|
|
99
99
|
routes: [
|
|
100
100
|
{
|
|
101
101
|
endpoint: "GET *",
|
|
102
|
-
|
|
103
|
-
const clientUsesHttp = request.origin.startsWith("http:");
|
|
104
|
-
return new Response(
|
|
105
|
-
clientUsesHttp ? `Welcome http user` : `Welcome https user`,
|
|
106
|
-
);
|
|
107
|
-
},
|
|
102
|
+
fetch: () => new Response("Welcome"),
|
|
108
103
|
},
|
|
109
104
|
],
|
|
110
105
|
});
|
|
@@ -112,16 +107,20 @@ await startServer({
|
|
|
112
107
|
|
|
113
108
|
# Documentation
|
|
114
109
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
|
118
|
-
|
|
|
119
|
-
| [
|
|
120
|
-
| [
|
|
121
|
-
| [
|
|
122
|
-
| [Serving files](./docs/serving_files.md) | Static
|
|
123
|
-
| [Content negotiation](./docs/content_negotiation.md) |
|
|
124
|
-
| [
|
|
125
|
-
| [Server
|
|
126
|
-
| [
|
|
127
|
-
| [
|
|
110
|
+
Every option is described by the JSDoc of the export (`startServer`, `createFileSystemFetch`, `serverPluginCORS`, …): hover it in your editor. The pages below explain how the pieces fit together.
|
|
111
|
+
|
|
112
|
+
| Topic | Description |
|
|
113
|
+
| ---------------------------------------------------- | ------------------------------------------------------ |
|
|
114
|
+
| [Handling requests](./docs/handling_requests.md) | Routes, the request object, the response formats |
|
|
115
|
+
| [Handling errors](./docs/handling_errors.md) | What happens when a route throws, error pages |
|
|
116
|
+
| [Plugins](./docs/plugins.md) | The hooks a server plugin can implement |
|
|
117
|
+
| [Serving files](./docs/serving_files.md) | Static files with client cache and compression |
|
|
118
|
+
| [Content negotiation](./docs/content_negotiation.md) | Media type, language, version and encoding negotiation |
|
|
119
|
+
| [WebSocket](./docs/websocket.md) | Accepting WebSocket connections |
|
|
120
|
+
| [Server-Sent Events](./docs/server_sent_events.md) | Pushing events to clients |
|
|
121
|
+
| [CORS](./docs/cors.md) | Cross-Origin Resource Sharing |
|
|
122
|
+
| [HTTPS](./docs/https.md) | HTTPS, http redirection and http2 |
|
|
123
|
+
| [Server timing](./docs/server_timing.md) | Reporting where the server spent its time |
|
|
124
|
+
| [Lifecycle](./docs/lifecycle.md) | Starting, stopping, keeping the process alive |
|
|
125
|
+
| [Security](./docs/security.md) | What the server protects, what it leaves to you |
|
|
126
|
+
| [Cluster](./docs/cluster.md) | One server per CPU core |
|