@jsenv/server 12.3.0 → 12.3.1

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/main.js CHANGED
@@ -42,6 +42,7 @@ export { pluginContentNegotiationCheck } from "./src/content_negotiation/plugin_
42
42
 
43
43
  // others
44
44
  export { fetchUrl } from "./src/fetch_url.js"
45
+ export { fromFetchResponse } from "./src/from_fetch_response.js"
45
46
  export { composeTwoResponses } from "./src/internal/response_composition.js"
46
47
  export { pluginRessourceAliases } from "./src/ressource_aliases/plugin_ressource_aliases.js"
47
48
  export { pluginRequestWaitingCheck } from "./src/plugin_request_waiting_check.js"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/server",
3
- "version": "12.3.0",
3
+ "version": "12.3.1",
4
4
  "description": "Write your Node.js server using pure functions",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -0,0 +1,15 @@
1
+ export const fromFetchResponse = (fetchResponse) => {
2
+ const responseHeaders = {}
3
+ const headersToIgnore = ["connection"]
4
+ fetchResponse.headers.forEach((value, name) => {
5
+ if (!headersToIgnore.includes(name)) {
6
+ responseHeaders[name] = value
7
+ }
8
+ })
9
+ return {
10
+ status: fetchResponse.status,
11
+ statusText: fetchResponse.statusText,
12
+ headers: responseHeaders,
13
+ body: fetchResponse.body, // node-fetch assumed
14
+ }
15
+ }
@@ -1,3 +1,4 @@
1
+ import http from "node:http"
1
2
  import { Http2ServerResponse } from "node:http2"
2
3
  import { raceCallbacks } from "@jsenv/abort"
3
4
 
@@ -155,10 +156,13 @@ const writeHead = (responseStream, { status, statusText, headers }) => {
155
156
  return
156
157
  }
157
158
 
159
+ if (statusText === undefined) {
160
+ statusText = statusTextFromStatus(status)
161
+ }
162
+
158
163
  // nodejs strange signature for writeHead force this
159
164
  // https://nodejs.org/api/http.html#http_response_writehead_statuscode_statusmessage_headers
160
165
  if (
161
- statusText === undefined ||
162
166
  // https://github.com/nodejs/node/blob/79296dc2d02c0b9872bbfcbb89148ea036a546d0/lib/internal/http2/compat.js#L97
163
167
  responseIsHttp2ServerResponse
164
168
  ) {
@@ -169,6 +173,9 @@ const writeHead = (responseStream, { status, statusText, headers }) => {
169
173
  responseStream.writeHead(status, statusText, nodeHeaders)
170
174
  }
171
175
 
176
+ const statusTextFromStatus = (status) =>
177
+ http.STATUS_CODES[status] || "not specified"
178
+
172
179
  const headersToNodeHeaders = (headers, { ignoreConnectionHeader }) => {
173
180
  const nodeHeaders = {}
174
181
 
@@ -570,7 +570,7 @@ export const startServer = async ({
570
570
  return composeTwoResponses(
571
571
  {
572
572
  status: 500,
573
- statusText: statusToStatusText(500),
573
+ statusText: "Internal Server Error",
574
574
  headers: {
575
575
  // ensure error are not cached
576
576
  "cache-control": "no-store",
@@ -580,9 +580,21 @@ export const startServer = async ({
580
580
  propertiesFromError,
581
581
  )
582
582
  }
583
- const responseProperties = normalizeResponseProperties(
584
- result.returnValue,
585
- )
583
+
584
+ const {
585
+ status = 501,
586
+ statusText,
587
+ headers = {},
588
+ body,
589
+ ...rest
590
+ } = result.returnValue || {}
591
+ const responseProperties = {
592
+ status,
593
+ statusText,
594
+ headers,
595
+ body,
596
+ ...rest,
597
+ }
586
598
  return responseProperties
587
599
  }
588
600
 
@@ -742,38 +754,6 @@ const createNodeServer = async ({
742
754
  })
743
755
  }
744
756
 
745
- const normalizeResponseProperties = (returnValue) => {
746
- if (returnValue && returnValue.constructor.name === "Response") {
747
- const responseHeaders = {}
748
- const headersToIgnore = ["connection"]
749
- returnValue.headers.forEach((value, name) => {
750
- if (!headersToIgnore.includes(name)) {
751
- responseHeaders[name] = value
752
- }
753
- })
754
- return {
755
- status: returnValue.status,
756
- statusText: returnValue.statusText,
757
- headers: responseHeaders,
758
- body: returnValue.body, // node-fetch assumed
759
- }
760
- }
761
- const {
762
- status = 501,
763
- statusText = statusToStatusText(status),
764
- headers = {},
765
- body,
766
- ...rest
767
- } = returnValue || {}
768
- return {
769
- status,
770
- statusText,
771
- headers,
772
- body,
773
- ...rest,
774
- }
775
- }
776
-
777
757
  const generateResponseDescription = async ({
778
758
  requestToResponse,
779
759
  request,
@@ -797,10 +777,6 @@ const generateResponseDescription = async ({
797
777
  }
798
778
  }
799
779
  }
800
-
801
- const statusToStatusText = (status) =>
802
- http.STATUS_CODES[status] || "not specified"
803
-
804
780
  const testCanPushStream = (http2Stream) => {
805
781
  if (!http2Stream.pushAllowed) {
806
782
  return {