@nxtedition/lib 21.0.18 → 21.0.19
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 +18 -21
- package/package.json +1 -1
package/http.js
CHANGED
|
@@ -24,22 +24,17 @@ export function genReqId() {
|
|
|
24
24
|
return `req-${nextReqId.toString(36)}`
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
let
|
|
28
|
-
let resTimeoutError
|
|
27
|
+
let timeoutError
|
|
29
28
|
|
|
30
|
-
function
|
|
31
|
-
this.destroy((
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function onResponseTimeout() {
|
|
35
|
-
this.destroy((resTimeoutError ??= new createError.RequestTimeout()))
|
|
29
|
+
function onTimeout() {
|
|
30
|
+
this.destroy((timeoutError ??= new createError.RequestTimeout()))
|
|
36
31
|
}
|
|
37
32
|
|
|
38
33
|
export async function request(ctx, next) {
|
|
39
34
|
const { req, res, logger } = ctx
|
|
40
35
|
const startTime = performance.now()
|
|
41
36
|
|
|
42
|
-
const ac = new AbortController()
|
|
37
|
+
const ac = ctx.signal !== undefined ? null : new AbortController()
|
|
43
38
|
|
|
44
39
|
let reqLogger = logger
|
|
45
40
|
try {
|
|
@@ -49,11 +44,14 @@ export async function request(ctx, next) {
|
|
|
49
44
|
}
|
|
50
45
|
|
|
51
46
|
ctx.id = req.id = res.id = req.headers['request-id'] || genReqId()
|
|
52
|
-
ctx.signal = ac.signal
|
|
53
47
|
ctx.method = req.method
|
|
54
48
|
ctx.query = ctx.url.search.length > 1 ? querystring.parse(ctx.url.search.slice(1)) : {}
|
|
55
49
|
ctx.logger = req.log = res.log = logger.child({ req: { id: req.id, url: req.url } })
|
|
56
50
|
|
|
51
|
+
if (ac) {
|
|
52
|
+
ctx.signal = ac.signal
|
|
53
|
+
}
|
|
54
|
+
|
|
57
55
|
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
58
56
|
req.resume() // Dump the body if there is one.
|
|
59
57
|
}
|
|
@@ -67,17 +65,12 @@ export async function request(ctx, next) {
|
|
|
67
65
|
reqLogger.debug('request started')
|
|
68
66
|
}
|
|
69
67
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}),
|
|
77
|
-
])
|
|
78
|
-
} finally {
|
|
79
|
-
ac.abort()
|
|
80
|
-
}
|
|
68
|
+
await Promise.all([
|
|
69
|
+
next(),
|
|
70
|
+
new Promise((resolve, reject) => {
|
|
71
|
+
res.on('timeout', onTimeout).on('error', reject).on('close', resolve)
|
|
72
|
+
}),
|
|
73
|
+
])
|
|
81
74
|
|
|
82
75
|
const elapsedTime = Math.ceil(performance.now() - startTime)
|
|
83
76
|
|
|
@@ -93,6 +86,8 @@ export async function request(ctx, next) {
|
|
|
93
86
|
reqLogger.debug({ res, elapsedTime }, 'request completed')
|
|
94
87
|
}
|
|
95
88
|
} catch (err) {
|
|
89
|
+
ac?.abort(err)
|
|
90
|
+
|
|
96
91
|
const statusCode = err.statusCode || err.$metadata?.httpStatusCode || 500
|
|
97
92
|
const responseTime = Math.ceil(performance.now() - startTime)
|
|
98
93
|
|
|
@@ -380,6 +375,8 @@ export async function upgrade(ctx, next) {
|
|
|
380
375
|
|
|
381
376
|
reqLogger.debug('stream completed')
|
|
382
377
|
} catch (err) {
|
|
378
|
+
ac?.abort(err)
|
|
379
|
+
|
|
383
380
|
const statusCode = err.statusCode || 500
|
|
384
381
|
|
|
385
382
|
if (aborted || err.name === 'AbortError' || err.code === 'ERR_STREAM_PREMATURE_CLOSE') {
|