@nxtedition/lib 12.1.14 → 13.0.1
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 +12 -38
- package/package.json +1 -1
package/http.js
CHANGED
|
@@ -3,7 +3,6 @@ const { performance } = require('perf_hooks')
|
|
|
3
3
|
const requestTarget = require('request-target')
|
|
4
4
|
const querystring = require('fast-querystring')
|
|
5
5
|
const assert = require('assert')
|
|
6
|
-
const { AbortError } = require('./errors')
|
|
7
6
|
const compose = require('koa-compose')
|
|
8
7
|
const http = require('http')
|
|
9
8
|
const fp = require('lodash/fp')
|
|
@@ -25,8 +24,6 @@ function genReqId() {
|
|
|
25
24
|
return `req-${nextReqId.toString(36)}`
|
|
26
25
|
}
|
|
27
26
|
|
|
28
|
-
function noop() {}
|
|
29
|
-
|
|
30
27
|
module.exports.request = async function request(ctx, next) {
|
|
31
28
|
const { req, res, logger } = ctx
|
|
32
29
|
const startTime = performance.now()
|
|
@@ -62,32 +59,17 @@ module.exports.request = async function request(ctx, next) {
|
|
|
62
59
|
throw new createError.BadRequest()
|
|
63
60
|
}
|
|
64
61
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
reject(new createError.RequestTimeout())
|
|
69
|
-
}
|
|
62
|
+
const onClose = () => ac.abort()
|
|
63
|
+
const onTimeout = () => res.destroy(new createError.RequestTimeout())
|
|
64
|
+
const onError = (err) => ac.abort(err)
|
|
70
65
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
if (!this.writableEnded) {
|
|
78
|
-
reject(new AbortError())
|
|
79
|
-
} else {
|
|
80
|
-
resolve(null)
|
|
81
|
-
}
|
|
82
|
-
})
|
|
83
|
-
.on('error', reject)
|
|
84
|
-
.on('timeout', onTimeout)
|
|
85
|
-
}),
|
|
86
|
-
next(),
|
|
87
|
-
])
|
|
66
|
+
res.on('close', onClose).on('timeout', onTimeout).on('error', onError)
|
|
67
|
+
|
|
68
|
+
req.on('close', onClose).on('timeout', onTimeout).on('error', onError)
|
|
69
|
+
|
|
70
|
+
await next()
|
|
88
71
|
|
|
89
72
|
assert(res.writableEnded)
|
|
90
|
-
assert(res.destroyed)
|
|
91
73
|
assert(res.statusCode)
|
|
92
74
|
|
|
93
75
|
const responseTime = Math.round(performance.now() - startTime)
|
|
@@ -146,13 +128,13 @@ module.exports.request = async function request(ctx, next) {
|
|
|
146
128
|
res.write(JSON.stringify(err.body))
|
|
147
129
|
}
|
|
148
130
|
|
|
149
|
-
res.end()
|
|
150
|
-
|
|
151
131
|
if (res.statusCode >= 500) {
|
|
152
132
|
reqLogger.error('request error')
|
|
153
133
|
} else if (res.statusCode >= 400) {
|
|
154
134
|
reqLogger.warn('request failed')
|
|
155
135
|
}
|
|
136
|
+
|
|
137
|
+
res.end()
|
|
156
138
|
} else {
|
|
157
139
|
if (req.aborted || err.name === 'AbortError') {
|
|
158
140
|
reqLogger.debug('request aborted')
|
|
@@ -161,17 +143,9 @@ module.exports.request = async function request(ctx, next) {
|
|
|
161
143
|
} else {
|
|
162
144
|
reqLogger.error('request error')
|
|
163
145
|
}
|
|
146
|
+
|
|
147
|
+
res.destroy()
|
|
164
148
|
}
|
|
165
|
-
} finally {
|
|
166
|
-
queueMicrotask(() => {
|
|
167
|
-
if (!req.destroyed) {
|
|
168
|
-
req.on('error', noop).destroy()
|
|
169
|
-
}
|
|
170
|
-
if (!res.destroyed) {
|
|
171
|
-
res.on('error', noop).destroy()
|
|
172
|
-
}
|
|
173
|
-
ac.abort()
|
|
174
|
-
})
|
|
175
149
|
}
|
|
176
150
|
}
|
|
177
151
|
|