@nxtedition/nxt-undici 6.2.4 → 6.2.5

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,41 @@ 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
+ super.onComplete(this.#trailers)
195
+ }
196
+ }
197
+
198
+ #maybeRetry(err, statusCode) {
199
+ if (this.#aborted || isDisturbed(this.#opts.body) || (this.#pos && !this.#etag)) {
200
+ this.#maybeError(err)
181
201
  return
182
202
  }
183
203
 
204
+ if (!err) {
205
+ // TOOD (fix): Avoid creating an Error and do onHeaders + onComplete.
206
+ const stackTraceLimit = Error.stackTraceLimit
207
+ Error.stackTraceLimit = 0
208
+ try {
209
+ err = createHttpError(statusCode ?? 500)
210
+ } finally {
211
+ Error.stackTraceLimit = stackTraceLimit
212
+ }
213
+ }
214
+
184
215
  let retryPromise
185
216
  try {
186
217
  retryPromise = retryFn(err, this.#retryCount, this.#opts)
@@ -189,7 +220,7 @@ class Handler extends DecoratorHandler {
189
220
  }
190
221
 
191
222
  if (retryPromise == null) {
192
- this.#onError(err)
223
+ this.#maybeError(err)
193
224
  return
194
225
  }
195
226
 
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.5",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",