@nxtedition/nxt-undici 6.2.4 → 6.2.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.
@@ -1,6 +1,9 @@
1
1
  import assert from 'node:assert'
2
2
  import tp from 'node:timers/promises'
3
3
  import { DecoratorHandler, isDisturbed, parseRangeHeader } from '../utils.js'
4
+ import createHttpError from 'http-errors'
5
+
6
+ function noop() {}
4
7
 
5
8
  // TODO (fix): What about onUpgrade?
6
9
  class Handler extends DecoratorHandler {
@@ -12,6 +15,8 @@ class Handler extends DecoratorHandler {
12
15
  #errorSent = false
13
16
 
14
17
  #statusCode = 0
18
+ #headers = null
19
+ #trailers = null
15
20
 
16
21
  #abort
17
22
  #aborted = false
@@ -41,6 +46,8 @@ class Handler extends DecoratorHandler {
41
46
 
42
47
  onConnect(abort) {
43
48
  this.#statusCode = 0
49
+ this.#headers = null
50
+ this.#trailers = null
44
51
 
45
52
  if (!this.#headersSent) {
46
53
  this.#pos = null
@@ -69,6 +76,9 @@ class Handler extends DecoratorHandler {
69
76
  }
70
77
 
71
78
  onHeaders(statusCode, headers, resume) {
79
+ this.#statusCode = statusCode
80
+ this.#headers = headers
81
+
72
82
  if (this.#error == null) {
73
83
  assert(this.#etag == null)
74
84
  assert(this.#pos == null)
@@ -107,7 +117,6 @@ class Handler extends DecoratorHandler {
107
117
  this.#end = contentLength
108
118
  this.#etag = headers.etag
109
119
  } else if (statusCode >= 400) {
110
- this.#statusCode = statusCode
111
120
  return true
112
121
  } else {
113
122
  return this.#onHeaders(statusCode, headers, resume)
@@ -168,19 +177,43 @@ class Handler extends DecoratorHandler {
168
177
  }
169
178
 
170
179
  onComplete(trailers) {
180
+ this.#trailers = trailers
181
+
171
182
  if (this.#statusCode >= 400) {
172
- this.#maybeRetry({ statusCode: this.#statusCode })
183
+ this.#maybeRetry(null, this.#statusCode)
173
184
  } else {
174
185
  super.onComplete(trailers)
175
186
  }
176
187
  }
177
188
 
178
- #maybeRetry(err) {
179
- if (this.#aborted || isDisturbed(this.#opts.body) || (this.#pos && !this.#etag)) {
189
+ #maybeError(err) {
190
+ if (err) {
180
191
  this.#onError(err)
192
+ } else {
193
+ this.#onHeaders(this.#statusCode, this.#headers, noop)
194
+ if (!this.#aborted) {
195
+ super.onComplete(this.#trailers)
196
+ }
197
+ }
198
+ }
199
+
200
+ #maybeRetry(err, statusCode) {
201
+ if (this.#aborted || isDisturbed(this.#opts.body) || (this.#pos && !this.#etag)) {
202
+ this.#maybeError(err)
181
203
  return
182
204
  }
183
205
 
206
+ if (!err) {
207
+ // TOOD (fix): Avoid creating an Error and do onHeaders + onComplete.
208
+ const stackTraceLimit = Error.stackTraceLimit
209
+ Error.stackTraceLimit = 0
210
+ try {
211
+ err = createHttpError(statusCode ?? 500)
212
+ } finally {
213
+ Error.stackTraceLimit = stackTraceLimit
214
+ }
215
+ }
216
+
184
217
  let retryPromise
185
218
  try {
186
219
  retryPromise = retryFn(err, this.#retryCount, this.#opts)
@@ -189,7 +222,7 @@ class Handler extends DecoratorHandler {
189
222
  }
190
223
 
191
224
  if (retryPromise == null) {
192
- this.#onError(err)
225
+ this.#maybeError(err)
193
226
  return
194
227
  }
195
228
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "6.2.4",
3
+ "version": "6.2.6",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",