@jsenv/server 12.5.6 → 12.5.9

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/server",
3
- "version": "12.5.6",
3
+ "version": "12.5.9",
4
4
  "description": "Write your Node.js server using pure functions",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -11,22 +11,21 @@ const cyan = "\x1b[36m"
11
11
 
12
12
  export const colorizeResponseStatus = (status) => {
13
13
  const statusType = statusToType(status)
14
-
15
14
  if (statusType === "information") return `${cyan}${status}${close}`
16
15
  if (statusType === "success") return `${green}${status}${close}`
17
16
  if (statusType === "redirection") return `${magenta}${status}${close}`
18
- if (statusType === "client-error") return `${yellow}${status}${close}`
19
- if (statusType === "server-error") return `${red}${status}${close}`
17
+ if (statusType === "client_error") return `${yellow}${status}${close}`
18
+ if (statusType === "server_error") return `${red}${status}${close}`
20
19
  return status
21
20
  }
22
21
 
23
22
  // https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
24
- const statusToType = (status) => {
23
+ export const statusToType = (status) => {
25
24
  if (statusIsInformation(status)) return "information"
26
25
  if (statusIsSuccess(status)) return "success"
27
26
  if (statusIsRedirection(status)) return "redirection"
28
- if (statusIsClientError(status)) return "client-error"
29
- if (statusIsServerError(status)) return "server-error"
27
+ if (statusIsClientError(status)) return "client_error"
28
+ if (statusIsServerError(status)) return "server_error"
30
29
  return "unknown"
31
30
  }
32
31
 
@@ -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
 
@@ -16,7 +16,10 @@ import {
16
16
  applyRedirectionToRequest,
17
17
  } from "./internal/request_factory.js"
18
18
  import { populateNodeResponse } from "./internal/populateNodeResponse.js"
19
- import { colorizeResponseStatus } from "./internal/colorizeResponseStatus.js"
19
+ import {
20
+ statusToType,
21
+ colorizeResponseStatus,
22
+ } from "./internal/colorizeResponseStatus.js"
20
23
  import { getServerOrigins } from "./internal/getServerOrigins.js"
21
24
  import { listen, stopListening } from "./internal/listen.js"
22
25
  import { composeTwoResponses } from "./internal/response_composition.js"
@@ -676,8 +679,15 @@ export const startServer = async ({
676
679
  onRequestHandled(requestNode)
677
680
  },
678
681
  onHeadersSent: ({ status, statusText }) => {
682
+ const statusType = statusToType(status)
679
683
  addRequestLog(requestNode, {
680
- type: "info",
684
+ type: {
685
+ information: "info",
686
+ success: "info",
687
+ redirection: "info",
688
+ client_error: "warn",
689
+ server_error: "error",
690
+ }[statusType],
681
691
  value: `${colorizeResponseStatus(status)} ${
682
692
  responseProperties.statusMessage || statusText
683
693
  }`,
@@ -728,12 +738,20 @@ export const startServer = async ({
728
738
  `${serverName} started at ${serverOrigin} (${serverOrigins.external})`,
729
739
  )
730
740
 
741
+ const addEffect = (callback) => {
742
+ const cleanup = callback()
743
+ if (typeof cleanup === "function") {
744
+ stopCallbackList.add(cleanup)
745
+ }
746
+ }
747
+
731
748
  return {
732
749
  getStatus: () => status,
733
750
  origin: serverOrigin,
734
751
  nodeServer,
735
752
  stop,
736
753
  stoppedPromise,
754
+ addEffect,
737
755
  }
738
756
  }
739
757