@nxtedition/lib 15.1.1 → 15.1.3
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/http.js +26 -30
- 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
|
|
37
|
-
this.log.debug('request
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
|
|
@@ -106,8 +108,6 @@ module.exports.request = async function request(ctx, next) {
|
|
|
106
108
|
} else {
|
|
107
109
|
reqLogger.trace({ res, responseTime }, 'request completed')
|
|
108
110
|
}
|
|
109
|
-
|
|
110
|
-
ac.abort()
|
|
111
111
|
} catch (err) {
|
|
112
112
|
const reason = ac.signal.reason
|
|
113
113
|
const responseTime = Math.round(performance.now() - startTime)
|
|
@@ -179,10 +179,6 @@ module.exports.request = async function request(ctx, next) {
|
|
|
179
179
|
res.destroy()
|
|
180
180
|
}
|
|
181
181
|
}
|
|
182
|
-
|
|
183
|
-
ac.abort(err)
|
|
184
|
-
} finally {
|
|
185
|
-
res.destroy()
|
|
186
182
|
}
|
|
187
183
|
}
|
|
188
184
|
|