@nxtedition/lib 15.1.1 → 15.1.2

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/http.js +26 -26
  2. package/package.json +1 -1
package/http.js CHANGED
@@ -23,8 +23,6 @@ function genReqId() {
23
23
  return `req-${nextReqId.toString(36)}`
24
24
  }
25
25
 
26
- const kResolve = Symbol('resolve')
27
-
28
26
  function onTimeout() {
29
27
  this.destroy(new createError.RequestTimeout())
30
28
  }
@@ -33,18 +31,8 @@ function onRequestError(err) {
33
31
  this.log.error({ err }, 'request error')
34
32
  }
35
33
 
36
- function onRequestClose() {
37
- this.log.debug('request closed')
38
- }
39
-
40
- function onResponseError(err) {
41
- this.log.error({ err }, 'response error')
42
- this[kResolve](Promise.reject(err))
43
- }
44
-
45
- function onResponseClose() {
46
- this.log.debug('response closed')
47
- this[kResolve](null)
34
+ function onRequestEnd() {
35
+ this.log.debug('request end')
48
36
  }
49
37
 
50
38
  module.exports.request = async function request(ctx, next) {
@@ -82,16 +70,30 @@ module.exports.request = async function request(ctx, next) {
82
70
  reqLogger.trace('request started')
83
71
  }
84
72
 
85
- await Promise.all([
86
- next(),
87
- new Promise((resolve) => {
88
- res[kResolve] = resolve
89
-
90
- res.on('timeout', onTimeout).on('error', onResponseError).on('close', onResponseClose)
91
-
92
- req.on('timeout', onTimeout).on('error', onRequestError).on('close', onRequestClose)
93
- }),
94
- ])
73
+ next()?.catch((err) => res.destroy(err))
74
+
75
+ await new Promise((resolve, reject) => {
76
+ req.on('timeout', onTimeout).on('error', onRequestError).on('end', onRequestEnd)
77
+ res
78
+ .on('timeout', onTimeout)
79
+ .on('error', function (err) {
80
+ this.log.error({ err }, 'response error')
81
+ ac.abort(err)
82
+ reject(err)
83
+ })
84
+ .on('finish', function () {
85
+ this.log.debug('response finish')
86
+ ac.abort()
87
+ resolve(null)
88
+ })
89
+ // TODO (fix): Remove this once we can trust that
90
+ // node always emits error or finish.
91
+ .on('close', function () {
92
+ this.log.debug('response close')
93
+ ac.abort()
94
+ resolve(null)
95
+ })
96
+ })
95
97
 
96
98
  const responseTime = Math.round(performance.now() - startTime)
97
99
 
@@ -179,8 +181,6 @@ module.exports.request = async function request(ctx, next) {
179
181
  res.destroy()
180
182
  }
181
183
  }
182
-
183
- ac.abort(err)
184
184
  } finally {
185
185
  res.destroy()
186
186
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "15.1.1",
3
+ "version": "15.1.2",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "files": [