@nxtedition/lib 15.1.4 → 15.1.6
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 +44 -52
- package/package.json +1 -1
package/http.js
CHANGED
|
@@ -23,24 +23,11 @@ function genReqId() {
|
|
|
23
23
|
return `req-${nextReqId.toString(36)}`
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
function onTimeout() {
|
|
27
|
-
this.destroy(new createError.RequestTimeout())
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function onRequestError(err) {
|
|
31
|
-
this.log.error({ err }, 'request error')
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function onRequestEnd() {
|
|
35
|
-
this.log.debug('request end')
|
|
36
|
-
}
|
|
37
|
-
|
|
38
26
|
module.exports.request = async function request(ctx, next) {
|
|
39
27
|
const { req, res, logger } = ctx
|
|
40
28
|
const startTime = performance.now()
|
|
41
29
|
|
|
42
30
|
const ac = new AbortController()
|
|
43
|
-
const signal = ac.signal
|
|
44
31
|
|
|
45
32
|
let reqLogger = logger
|
|
46
33
|
try {
|
|
@@ -51,7 +38,7 @@ module.exports.request = async function request(ctx, next) {
|
|
|
51
38
|
|
|
52
39
|
ctx.id = req.id = req.headers['request-id'] || genReqId()
|
|
53
40
|
ctx.logger = req.log = res.log = logger.child({ req: { id: req.id, url: req.url } })
|
|
54
|
-
ctx.signal = signal
|
|
41
|
+
ctx.signal = ac.signal
|
|
55
42
|
ctx.method = req.method
|
|
56
43
|
ctx.query = ctx.url.search.length > 1 ? querystring.parse(ctx.url.search.slice(1)) : {}
|
|
57
44
|
|
|
@@ -63,37 +50,49 @@ module.exports.request = async function request(ctx, next) {
|
|
|
63
50
|
|
|
64
51
|
const isHealthcheck = ctx.url.pathname === '/healthcheck'
|
|
65
52
|
|
|
66
|
-
reqLogger =
|
|
53
|
+
reqLogger = logger.child({ req })
|
|
67
54
|
if (!isHealthcheck) {
|
|
68
55
|
reqLogger.debug('request started')
|
|
69
56
|
} else {
|
|
70
57
|
reqLogger.trace('request started')
|
|
71
58
|
}
|
|
72
59
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
60
|
+
await Promise.all([
|
|
61
|
+
next(),
|
|
62
|
+
new Promise((resolve, reject) => {
|
|
63
|
+
req
|
|
64
|
+
.on('timeout', function () {
|
|
65
|
+
this.destroy(new createError.RequestTimeout())
|
|
66
|
+
})
|
|
67
|
+
.on('error', function (err) {
|
|
68
|
+
this.log.error({ err }, 'request error')
|
|
69
|
+
})
|
|
70
|
+
.on('end', function () {
|
|
71
|
+
this.log.trace('request end')
|
|
72
|
+
})
|
|
73
|
+
.on('close', function () {
|
|
74
|
+
this.log.trace('request close')
|
|
75
|
+
})
|
|
76
|
+
res
|
|
77
|
+
.on('timeout', function () {
|
|
78
|
+
this.destroy(new createError.RequestTimeout())
|
|
79
|
+
})
|
|
80
|
+
.on('error', function (err) {
|
|
81
|
+
this.log.error({ err }, 'response error')
|
|
82
|
+
})
|
|
83
|
+
.on('finish', function () {
|
|
84
|
+
this.log.trace('response finish')
|
|
85
|
+
})
|
|
86
|
+
.on('close', function () {
|
|
87
|
+
this.log.trace('response close')
|
|
88
|
+
if (this.errored) {
|
|
89
|
+
reject(this.errored)
|
|
90
|
+
} else {
|
|
91
|
+
resolve(null)
|
|
92
|
+
}
|
|
93
|
+
})
|
|
94
|
+
}),
|
|
95
|
+
])
|
|
97
96
|
|
|
98
97
|
const responseTime = Math.round(performance.now() - startTime)
|
|
99
98
|
|
|
@@ -108,20 +107,11 @@ module.exports.request = async function request(ctx, next) {
|
|
|
108
107
|
} else {
|
|
109
108
|
reqLogger.trace({ res, responseTime }, 'request completed')
|
|
110
109
|
}
|
|
110
|
+
|
|
111
|
+
ac.abort()
|
|
111
112
|
} catch (err) {
|
|
112
|
-
const reason = ac.signal.reason
|
|
113
113
|
const responseTime = Math.round(performance.now() - startTime)
|
|
114
114
|
|
|
115
|
-
req.on('error', (err) => {
|
|
116
|
-
if (res.statusCode > 500 || err.code !== 'ECONNRESET') {
|
|
117
|
-
reqLogger.warn({ err }, 'request error')
|
|
118
|
-
}
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
res.on('error', (err) => {
|
|
122
|
-
reqLogger.warn({ err }, 'request error')
|
|
123
|
-
})
|
|
124
|
-
|
|
125
115
|
if (!res.headersSent && !res.destroyed) {
|
|
126
116
|
res.statusCode = err.statusCode || 500
|
|
127
117
|
|
|
@@ -150,7 +140,7 @@ module.exports.request = async function request(ctx, next) {
|
|
|
150
140
|
res.write(JSON.stringify(err.body))
|
|
151
141
|
}
|
|
152
142
|
|
|
153
|
-
reqLogger = reqLogger.child({ res, err,
|
|
143
|
+
reqLogger = reqLogger.child({ res, err, responseTime })
|
|
154
144
|
|
|
155
145
|
if (res.statusCode < 500) {
|
|
156
146
|
reqLogger.warn('request failed')
|
|
@@ -162,7 +152,7 @@ module.exports.request = async function request(ctx, next) {
|
|
|
162
152
|
|
|
163
153
|
res.end()
|
|
164
154
|
} else {
|
|
165
|
-
reqLogger = reqLogger.child({ res, err,
|
|
155
|
+
reqLogger = reqLogger.child({ res, err, responseTime })
|
|
166
156
|
|
|
167
157
|
if (req.aborted || !res.writableEnded || err.name === 'AbortError') {
|
|
168
158
|
reqLogger.debug('request aborted')
|
|
@@ -179,6 +169,8 @@ module.exports.request = async function request(ctx, next) {
|
|
|
179
169
|
res.destroy()
|
|
180
170
|
}
|
|
181
171
|
}
|
|
172
|
+
|
|
173
|
+
ac.abort(err)
|
|
182
174
|
}
|
|
183
175
|
}
|
|
184
176
|
|