@jsenv/server 12.5.5 → 12.5.8
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/package.json +4 -3
- package/src/internal/populateNodeResponse.js +28 -28
- package/src/startServer.js +15 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsenv/server",
|
|
3
|
-
"version": "12.5.
|
|
3
|
+
"version": "12.5.8",
|
|
4
4
|
"description": "Write your Node.js server using pure functions",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
"node": ">=16.13.0"
|
|
17
17
|
},
|
|
18
18
|
"publishConfig": {
|
|
19
|
-
"access": "public"
|
|
19
|
+
"access": "public",
|
|
20
|
+
"registry": "https://registry.npmjs.org"
|
|
20
21
|
},
|
|
21
22
|
"type": "module",
|
|
22
23
|
"exports": {
|
|
@@ -67,4 +68,4 @@
|
|
|
67
68
|
"playwright": "1.18.1",
|
|
68
69
|
"prettier": "2.5.1"
|
|
69
70
|
}
|
|
70
|
-
}
|
|
71
|
+
}
|
|
@@ -44,6 +44,34 @@ export const populateNodeResponse = async (
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
await new Promise((resolve) => {
|
|
47
|
+
const observable = bodyMethods.asObservable()
|
|
48
|
+
const subscription = observable.subscribe({
|
|
49
|
+
next: (data) => {
|
|
50
|
+
try {
|
|
51
|
+
responseStream.write(data)
|
|
52
|
+
} catch (e) {
|
|
53
|
+
// Something inside Node.js sometimes puts stream
|
|
54
|
+
// in a state where .write() throw despites nodeResponse.destroyed
|
|
55
|
+
// being undefined and "close" event not being emitted.
|
|
56
|
+
// I have tested if we are the one calling destroy
|
|
57
|
+
// (I have commented every .destroy() call)
|
|
58
|
+
// but issue still occurs
|
|
59
|
+
// For the record it's "hard" to reproduce but can be by running
|
|
60
|
+
// a lot of tests against a browser in the context of @jsenv/core testing
|
|
61
|
+
if (e.code === "ERR_HTTP2_INVALID_STREAM") {
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
responseStream.emit("error", e)
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
error: (value) => {
|
|
68
|
+
responseStream.emit("error", value)
|
|
69
|
+
},
|
|
70
|
+
complete: () => {
|
|
71
|
+
responseStream.end()
|
|
72
|
+
},
|
|
73
|
+
})
|
|
74
|
+
|
|
47
75
|
raceCallbacks(
|
|
48
76
|
{
|
|
49
77
|
abort: (cb) => {
|
|
@@ -104,34 +132,6 @@ export const populateNodeResponse = async (
|
|
|
104
132
|
raceEffects[winner.name](winner.data)
|
|
105
133
|
},
|
|
106
134
|
)
|
|
107
|
-
|
|
108
|
-
const observable = bodyMethods.asObservable()
|
|
109
|
-
const subscription = observable.subscribe({
|
|
110
|
-
next: (data) => {
|
|
111
|
-
try {
|
|
112
|
-
responseStream.write(data)
|
|
113
|
-
} catch (e) {
|
|
114
|
-
// Something inside Node.js sometimes puts stream
|
|
115
|
-
// in a state where .write() throw despites nodeResponse.destroyed
|
|
116
|
-
// being undefined and "close" event not being emitted.
|
|
117
|
-
// I have tested if we are the one calling destroy
|
|
118
|
-
// (I have commented every .destroy() call)
|
|
119
|
-
// but issue still occurs
|
|
120
|
-
// For the record it's "hard" to reproduce but can be by running
|
|
121
|
-
// a lot of tests against a browser in the context of @jsenv/core testing
|
|
122
|
-
if (e.code === "ERR_HTTP2_INVALID_STREAM") {
|
|
123
|
-
return
|
|
124
|
-
}
|
|
125
|
-
responseStream.emit("error", e)
|
|
126
|
-
}
|
|
127
|
-
},
|
|
128
|
-
error: (value) => {
|
|
129
|
-
responseStream.emit("error", value)
|
|
130
|
-
},
|
|
131
|
-
complete: () => {
|
|
132
|
-
responseStream.end()
|
|
133
|
-
},
|
|
134
|
-
})
|
|
135
135
|
})
|
|
136
136
|
}
|
|
137
137
|
|
package/src/startServer.js
CHANGED
|
@@ -291,9 +291,12 @@ export const startServer = async ({
|
|
|
291
291
|
// keep buffering until root request write logs for everyone
|
|
292
292
|
return
|
|
293
293
|
}
|
|
294
|
+
const prefixLines = (string, prefix) => {
|
|
295
|
+
return string.replace(/^(?!\s*$)/gm, prefix)
|
|
296
|
+
}
|
|
294
297
|
const writeLog = ({ type, value }, depth) => {
|
|
295
298
|
if (depth > 0) {
|
|
296
|
-
value = value
|
|
299
|
+
value = prefixLines(value, " ".repeat(depth))
|
|
297
300
|
}
|
|
298
301
|
logger[type](value)
|
|
299
302
|
}
|
|
@@ -303,7 +306,7 @@ export const startServer = async ({
|
|
|
303
306
|
const middleLogs = requestNode.logs
|
|
304
307
|
writeLog(firstLog, depth)
|
|
305
308
|
middleLogs.forEach((log) => {
|
|
306
|
-
writeLog(log, depth
|
|
309
|
+
writeLog(log, depth)
|
|
307
310
|
})
|
|
308
311
|
requestNode.children.forEach((child) => {
|
|
309
312
|
visitRequestNodeToLog(child, depth + 1)
|
|
@@ -672,11 +675,11 @@ export const startServer = async ({
|
|
|
672
675
|
})
|
|
673
676
|
onRequestHandled(requestNode)
|
|
674
677
|
},
|
|
675
|
-
onHeadersSent: ({ status, statusText
|
|
678
|
+
onHeadersSent: ({ status, statusText }) => {
|
|
676
679
|
addRequestLog(requestNode, {
|
|
677
680
|
type: "info",
|
|
678
681
|
value: `${colorizeResponseStatus(status)} ${
|
|
679
|
-
statusMessage || statusText
|
|
682
|
+
responseProperties.statusMessage || statusText
|
|
680
683
|
}`,
|
|
681
684
|
})
|
|
682
685
|
},
|
|
@@ -725,12 +728,20 @@ export const startServer = async ({
|
|
|
725
728
|
`${serverName} started at ${serverOrigin} (${serverOrigins.external})`,
|
|
726
729
|
)
|
|
727
730
|
|
|
731
|
+
const addEffect = (callback) => {
|
|
732
|
+
const cleanup = callback()
|
|
733
|
+
if (typeof cleanup === "function") {
|
|
734
|
+
stopCallbackList.add(cleanup)
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
|
|
728
738
|
return {
|
|
729
739
|
getStatus: () => status,
|
|
730
740
|
origin: serverOrigin,
|
|
731
741
|
nodeServer,
|
|
732
742
|
stop,
|
|
733
743
|
stoppedPromise,
|
|
744
|
+
addEffect,
|
|
734
745
|
}
|
|
735
746
|
}
|
|
736
747
|
|