@nxtedition/lib 21.0.15 → 21.0.17
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 +19 -45
- package/package.json +1 -1
package/http.js
CHANGED
|
@@ -52,20 +52,7 @@ export async function request(ctx, next) {
|
|
|
52
52
|
ctx.signal = ac.signal
|
|
53
53
|
ctx.method = req.method
|
|
54
54
|
ctx.query = ctx.url.search.length > 1 ? querystring.parse(ctx.url.search.slice(1)) : {}
|
|
55
|
-
ctx.logger =
|
|
56
|
-
req.log =
|
|
57
|
-
res.log =
|
|
58
|
-
logger.child({
|
|
59
|
-
req: {
|
|
60
|
-
id: req.id,
|
|
61
|
-
url: req.url,
|
|
62
|
-
scheme: ctx.url.protocol,
|
|
63
|
-
host: ctx.url.hostname,
|
|
64
|
-
port: ctx.url.port,
|
|
65
|
-
path: ctx.url.pathname,
|
|
66
|
-
query: ctx.query,
|
|
67
|
-
},
|
|
68
|
-
})
|
|
55
|
+
ctx.logger = req.log = res.log = logger.child({ req: { id: req.id, url: req.url } })
|
|
69
56
|
|
|
70
57
|
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
71
58
|
req.resume() // Dump the body if there is one.
|
|
@@ -80,28 +67,19 @@ export async function request(ctx, next) {
|
|
|
80
67
|
reqLogger.debug('request started')
|
|
81
68
|
}
|
|
82
69
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
// TODO (fix): Use 'end' once we can trust that
|
|
95
|
-
// 'end' or 'error' will always be emitted.
|
|
96
|
-
.on('close', function () {
|
|
97
|
-
this.log.debug('request closed')
|
|
98
|
-
resolve(null)
|
|
99
|
-
ac.abort()
|
|
100
|
-
})
|
|
101
|
-
}),
|
|
102
|
-
])
|
|
70
|
+
try {
|
|
71
|
+
await Promise.all([
|
|
72
|
+
next(),
|
|
73
|
+
new Promise((resolve, reject) => {
|
|
74
|
+
req.on('timeout', onRequestTimeout).on('error', reject)
|
|
75
|
+
res.on('timeout', onResponseTimeout).on('error', reject).on('close', resolve)
|
|
76
|
+
}),
|
|
77
|
+
])
|
|
78
|
+
} finally {
|
|
79
|
+
ac.abort()
|
|
80
|
+
}
|
|
103
81
|
|
|
104
|
-
const elapsedTime = Math.
|
|
82
|
+
const elapsedTime = Math.ceil(performance.now() - startTime)
|
|
105
83
|
|
|
106
84
|
if (isHealthcheck) {
|
|
107
85
|
// Do nothing...
|
|
@@ -116,7 +94,7 @@ export async function request(ctx, next) {
|
|
|
116
94
|
}
|
|
117
95
|
} catch (err) {
|
|
118
96
|
const statusCode = err.statusCode || err.$metadata?.httpStatusCode || 500
|
|
119
|
-
const responseTime = Math.
|
|
97
|
+
const responseTime = Math.ceil(performance.now() - startTime)
|
|
120
98
|
|
|
121
99
|
if (!res.headersSent && !res.destroyed) {
|
|
122
100
|
res.statusCode = statusCode
|
|
@@ -146,24 +124,20 @@ export async function request(ctx, next) {
|
|
|
146
124
|
res.write(JSON.stringify(err.body))
|
|
147
125
|
}
|
|
148
126
|
|
|
149
|
-
reqLogger = reqLogger.child({ res, err, responseTime })
|
|
150
|
-
|
|
151
127
|
if (statusCode < 500) {
|
|
152
|
-
reqLogger.warn('request failed')
|
|
128
|
+
reqLogger.warn({ res, err, responseTime }, 'request failed')
|
|
153
129
|
} else {
|
|
154
|
-
reqLogger.error('request error')
|
|
130
|
+
reqLogger.error({ res, err, responseTime }, 'request error')
|
|
155
131
|
}
|
|
156
132
|
|
|
157
133
|
res.end()
|
|
158
134
|
} else {
|
|
159
|
-
reqLogger = reqLogger.child({ res, err, responseTime })
|
|
160
|
-
|
|
161
135
|
if (req.aborted || !res.writableEnded || err.name === 'AbortError') {
|
|
162
|
-
reqLogger.debug('request aborted')
|
|
136
|
+
reqLogger.debug({ res, err, responseTime }, 'request aborted')
|
|
163
137
|
} else if (statusCode < 500) {
|
|
164
|
-
reqLogger.warn('request failed')
|
|
138
|
+
reqLogger.warn({ res, err, responseTime }, 'request failed')
|
|
165
139
|
} else {
|
|
166
|
-
reqLogger.error('request error')
|
|
140
|
+
reqLogger.error({ res, err, responseTime }, 'request error')
|
|
167
141
|
}
|
|
168
142
|
|
|
169
143
|
if (!res.writableEnded) {
|