@jsenv/server 13.0.0 → 13.1.0
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 +16 -49
- package/package.json +3 -2
- package/src/internal/request_factory.js +3 -1
- package/src/internal/server-polyglot.js +1 -0
- package/src/main.js +1 -1
- package/src/service_controller.js +1 -0
- package/src/start_server.js +962 -0
- package/src/server.js +0 -896
package/README.md
CHANGED
|
@@ -24,34 +24,7 @@ await startServer({
|
|
|
24
24
|
})
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
-
#
|
|
28
|
-
|
|
29
|
-
_Code starting a server and using [node-fetch](https://github.com/node-fetch/node-fetch) to send a request on that server:_
|
|
30
|
-
|
|
31
|
-
```js
|
|
32
|
-
import fetch from "node-fetch"
|
|
33
|
-
import { startServer } from "@jsenv/server"
|
|
34
|
-
|
|
35
|
-
const server = await startServer({
|
|
36
|
-
services: [
|
|
37
|
-
{
|
|
38
|
-
handleRequest: () => {
|
|
39
|
-
return {
|
|
40
|
-
status: 200,
|
|
41
|
-
headers: {
|
|
42
|
-
"content-type": "text/plain",
|
|
43
|
-
},
|
|
44
|
-
body: "Hello world",
|
|
45
|
-
}
|
|
46
|
-
},
|
|
47
|
-
},
|
|
48
|
-
],
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
const response = await fetch(server.origin)
|
|
52
|
-
const responseBodyAsText = await response.text()
|
|
53
|
-
console.log(responseBodyAsText) // "Hello world"
|
|
54
|
-
```
|
|
27
|
+
# Examples
|
|
55
28
|
|
|
56
29
|
_Code starting a server with 2 request handlers:_
|
|
57
30
|
|
|
@@ -73,7 +46,7 @@ const server = await startServer({
|
|
|
73
46
|
if (request.ressource === "/") {
|
|
74
47
|
return { status: 200 }
|
|
75
48
|
}
|
|
76
|
-
return null
|
|
49
|
+
return null
|
|
77
50
|
},
|
|
78
51
|
},
|
|
79
52
|
{
|
|
@@ -101,8 +74,8 @@ import { startServer } from "@jsenv/server"
|
|
|
101
74
|
|
|
102
75
|
await startServer({
|
|
103
76
|
protocol: "https",
|
|
104
|
-
certificate:
|
|
105
|
-
privateKey:
|
|
77
|
+
certificate: readFileSync(new URL("./server.crt", import.meta.url), "utf8"),
|
|
78
|
+
privateKey: readFileSync(new URL("./server.key", import.meta.url), "utf8"),
|
|
106
79
|
allowHttpRequestOnHttps: true,
|
|
107
80
|
services: [
|
|
108
81
|
{
|
|
@@ -120,11 +93,6 @@ await startServer({
|
|
|
120
93
|
},
|
|
121
94
|
],
|
|
122
95
|
})
|
|
123
|
-
|
|
124
|
-
function readFileSyncAsString(relativeUrl) {
|
|
125
|
-
const fileUrl = new URL(relativeUrl, import.meta.url)
|
|
126
|
-
return String(readFileSync(fileUrl))
|
|
127
|
-
}
|
|
128
96
|
```
|
|
129
97
|
|
|
130
98
|
_Code starting a server for static files:_
|
|
@@ -137,9 +105,7 @@ await startServer({
|
|
|
137
105
|
{
|
|
138
106
|
handleRequest: async (request) => {
|
|
139
107
|
const fileUrl = new URL(request.ressource.slice(1), import.meta.url)
|
|
140
|
-
const response = await fetchFileSystem(fileUrl,
|
|
141
|
-
...request,
|
|
142
|
-
})
|
|
108
|
+
const response = await fetchFileSystem(fileUrl, request)
|
|
143
109
|
return response
|
|
144
110
|
},
|
|
145
111
|
},
|
|
@@ -149,16 +115,17 @@ await startServer({
|
|
|
149
115
|
|
|
150
116
|
# Documentation
|
|
151
117
|
|
|
152
|
-
- [Handling requests](./docs/handling_requests
|
|
153
|
-
- [Handling errors](./docs/handling_errors
|
|
154
|
-
- [Server timing](./docs/server_timing
|
|
155
|
-
- [CORS](./docs/cors
|
|
156
|
-
- [https](./docs/https
|
|
157
|
-
- [Serving files](./docs/serving_files
|
|
158
|
-
- [Content negotiation](./docs/content_negotiation
|
|
159
|
-
- [
|
|
160
|
-
- [
|
|
161
|
-
- [
|
|
118
|
+
- [Handling requests](./docs/handling_requests.md)
|
|
119
|
+
- [Handling errors](./docs/handling_errors.md)
|
|
120
|
+
- [Server timing](./docs/server_timing.md)
|
|
121
|
+
- [CORS](./docs/cors.md)
|
|
122
|
+
- [https](./docs/https.md)
|
|
123
|
+
- [Serving files](./docs/serving_files.md)
|
|
124
|
+
- [Content negotiation](./docs/content_negotiation.md)
|
|
125
|
+
- [Websocket](./docs/websocket.md)
|
|
126
|
+
- [Server Sent Events](./docs/server_sent_events.md)
|
|
127
|
+
- [Cluster](./docs/cluster.md)
|
|
128
|
+
- [Http2 push](./docs/http2_push.md)
|
|
162
129
|
|
|
163
130
|
# Installation
|
|
164
131
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsenv/server",
|
|
3
|
-
"version": "13.
|
|
3
|
+
"version": "13.1.0",
|
|
4
4
|
"description": "Write your Node.js server using pure functions",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -37,7 +37,8 @@
|
|
|
37
37
|
"@jsenv/abort": "4.2.3",
|
|
38
38
|
"@jsenv/log": "3.1.0",
|
|
39
39
|
"@jsenv/url-meta": "7.0.0",
|
|
40
|
-
"@jsenv/utils": "2.0.1"
|
|
40
|
+
"@jsenv/utils": "2.0.1",
|
|
41
|
+
"ws": "8.8.1"
|
|
41
42
|
},
|
|
42
43
|
"devDependencies": {
|
|
43
44
|
"@jsenv/fetch": "../fetch/",
|
|
@@ -6,7 +6,9 @@ export const fromNodeRequest = (nodeRequest, { serverOrigin, signal }) => {
|
|
|
6
6
|
const body = nodeStreamToObservable(nodeRequest)
|
|
7
7
|
|
|
8
8
|
let requestOrigin
|
|
9
|
-
if (nodeRequest.
|
|
9
|
+
if (nodeRequest.upgrade) {
|
|
10
|
+
requestOrigin = serverOrigin
|
|
11
|
+
} else if (nodeRequest.authority) {
|
|
10
12
|
requestOrigin = nodeRequest.connection.encrypted
|
|
11
13
|
? `https://${nodeRequest.authority}`
|
|
12
14
|
: `http://${nodeRequest.authority}`
|
|
@@ -62,6 +62,7 @@ export const createPolyglotServer = async ({
|
|
|
62
62
|
|
|
63
63
|
// The async part is just to lazyly import "http2" or "https"
|
|
64
64
|
// so that these module are parsed only if used.
|
|
65
|
+
// https://nodejs.org/api/tls.html#tlscreatesecurecontextoptions
|
|
65
66
|
const createSecureServer = async ({
|
|
66
67
|
certificate,
|
|
67
68
|
privateKey,
|
package/src/main.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Ideally this file should be kept simple to help discovering codebase progressively.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
export { startServer } from "./
|
|
7
|
+
export { startServer } from "./start_server.js"
|
|
8
8
|
export { setupRoutes } from "./service_composition/routing.js"
|
|
9
9
|
export { readRequestBody } from "./readRequestBody.js"
|
|
10
10
|
export { fetchFileSystem } from "./fetch_filesystem.js"
|