@superhero/http-request 4.0.2 → 4.0.4

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.
Files changed (2) hide show
  1. package/index.js +57 -14
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -334,7 +334,7 @@ export default class Request
334
334
  upstream.headers = { ...headers }
335
335
 
336
336
  delete upstream.headers[HEADER_STATUS]
337
- Object.defineProperty(headers, SENSITIVE_HEADERS, { value:headers[SENSITIVE_HEADERS] })
337
+ Object.defineProperty(upstream.headers, SENSITIVE_HEADERS, { enumerable:false, value:headers[SENSITIVE_HEADERS] })
338
338
 
339
339
  this.#resolveOnResponse(options, method, url, accept, reject, upstream)
340
340
  })
@@ -388,7 +388,7 @@ export default class Request
388
388
  if((response.status >= 400 && false === !!options.doNotThrowOnErrorStatus)
389
389
  || (response.status >= 300 && false === !!options.doNotThrowOnRedirectStatus && response.status < 400))
390
390
  {
391
- const error = new Error(`Invalid HTTP status ${response.status} ${method} -> ${url}`)
391
+ const error = new Error(`Invalid HTTP status ${response.status} ${method} ${url}`)
392
392
  error.code = 'E_HTTP_REQUEST_INVALID_RESPONSE_STATUS'
393
393
  error.response = response
394
394
 
@@ -667,7 +667,7 @@ export default class Request
667
667
  */
668
668
  #onStreamError(method, url, response, reason)
669
669
  {
670
- const error = new Error(`Downstream error [${reason.code}] ${method} -> ${url}`)
670
+ const error = new Error(`Downstream error [${reason.code}] ${method} ${url}`)
671
671
  error.code = 'E_HTTP_REQUEST_DOWNSTREAM_ERROR'
672
672
  error.cause = reason
673
673
  error.response = response
@@ -689,23 +689,66 @@ export default class Request
689
689
  */
690
690
  #onStreamEnd(method, url, response, accept, reject)
691
691
  {
692
- if(response.headers['content-type']?.startsWith('application/json'))
692
+ try
693
693
  {
694
- try
694
+ const contentType = response.headers['content-type']
695
+
696
+ if(contentType?.startsWith('application/json'))
695
697
  {
696
- response.body = JSON.parse(response.body || '{}')
698
+ response.body = this.#contentTypeApplicationJson(response.body)
697
699
  }
698
- catch(reason)
700
+ else if(contentType?.startsWith('text/event-stream'))
699
701
  {
700
- const error = new TypeError(`Invalid JSON format ${method} -> ${url}`)
701
- error.code = 'E_HTTP_REQUEST_INVALID_RESPONSE_BODY_FORMAT'
702
- error.cause = reason
703
- error.response = response
704
- reject(error)
705
- return
702
+ response.body = this.#contentTypeTextEventStream(response.body)
706
703
  }
707
704
  }
708
-
705
+ catch(reason)
706
+ {
707
+ const error = new TypeError(`Invalid response body format ${method} ${url}`)
708
+ error.code = 'E_HTTP_REQUEST_INVALID_RESPONSE_BODY_FORMAT'
709
+ error.cause = reason
710
+ error.response = response
711
+ reject(error)
712
+ return
713
+ }
714
+
709
715
  accept(response)
710
716
  }
717
+
718
+ #contentTypeApplicationJson(body)
719
+ {
720
+ try
721
+ {
722
+ return JSON.parse(body || '{}')
723
+ }
724
+ catch(reason)
725
+ {
726
+ const error = new TypeError(`Invalid JSON format`)
727
+ error.cause = reason
728
+ throw error
729
+ }
730
+ }
731
+
732
+ #contentTypeTextEventStream(body)
733
+ {
734
+ return body.split('\n\n').map((fields) =>
735
+ {
736
+ return Object.fromEntries(fields.split('\n').map((field) =>
737
+ {
738
+ const
739
+ separator = field.indexOf(':'),
740
+ param = field.slice(0, separator),
741
+ value = field.slice(separator + 1).trim()
742
+
743
+ try
744
+ {
745
+ return [ param, JSON.parse(value)]
746
+ }
747
+ catch(reason)
748
+ {
749
+ return [ param, value ]
750
+ }
751
+ }))
752
+ })
753
+ }
711
754
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superhero/http-request",
3
- "version": "4.0.2",
3
+ "version": "4.0.4",
4
4
  "description": "HTTP request component supporting HTTP 1.1 and HTTP 2.0",
5
5
  "keywords": [
6
6
  "http request",