@nxtedition/nxt-undici 6.0.10 → 6.0.12

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.
@@ -8,19 +8,29 @@ const MAX_TTL = 10e3
8
8
 
9
9
  class Handler extends DecoratorHandler {
10
10
  #callback
11
+ #statusCode
11
12
 
12
13
  constructor(handler, callback) {
13
14
  super(handler)
14
15
  this.#callback = callback
15
16
  }
16
17
 
18
+ onHeaders(statusCode, headers, resume) {
19
+ this.#statusCode = statusCode
20
+ return super.onHeaders(statusCode, headers, resume)
21
+ }
22
+
17
23
  onComplete(trailers) {
18
- this.#callback(null)
24
+ this.#callback(null, this.#statusCode)
19
25
  super.onComplete(trailers)
20
26
  }
21
27
 
22
28
  onError(err) {
23
- this.#callback(err)
29
+ if (err?.statusCode == null) {
30
+ this.#callback(err)
31
+ } else if (err.statusCode) {
32
+ this.#callback(null, err.statusCode)
33
+ }
24
34
  super.onError(err)
25
35
  }
26
36
  }
@@ -35,14 +45,15 @@ export default () => (dispatch) => {
35
45
  promise = resolve4(hostname, { ttl: true })
36
46
  .then((records) => {
37
47
  const now = getFastNow()
38
- const prev = cache.get(hostname)
39
- const next = records.map(({ address, ttl }) => ({
48
+ const ret = records.map(({ address, ttl }) => ({
40
49
  address,
41
50
  expires: now + Math.min(MAX_TTL, 1e3 * ttl),
42
- stats: prev?.find((x) => x.address === address)?.stats || { pending: 0, errored: 0 },
51
+ pending: 0,
52
+ errored: 0,
53
+ counter: 0,
43
54
  }))
44
- cache.set(hostname, next)
45
- return next
55
+ cache.set(hostname, ret)
56
+ return ret
46
57
  })
47
58
  .finally(() => {
48
59
  promises.delete(hostname)
@@ -69,22 +80,22 @@ export default () => (dispatch) => {
69
80
 
70
81
  let records = cache.get(hostname)
71
82
 
72
- if (records == null || records.every((x) => x.stats.errored || x.expires < now)) {
83
+ if (records == null || records.every((x) => x.expires < now)) {
73
84
  records = await resolve(hostname)
74
- } else if (records.some((x) => x.errored || x.expires < now + 1e3)) {
85
+ } else if (records.some((x) => x.expires < now + 1e3)) {
75
86
  resolve(hostname).catch(noop)
76
87
  }
77
88
 
78
89
  records.sort((a, b) => {
79
- if (a.stats.errored !== b.stats.errored) {
80
- return a.stats.errored - b.stats.errored
90
+ if (a.errored !== b.errored) {
91
+ return a.errored - b.errored
81
92
  }
82
93
 
83
- if (a.stats.pending !== b.stats.pending) {
84
- return a.stats.pending - b.stats.pending
94
+ if (a.pending !== b.pending) {
95
+ return a.pending - b.pending
85
96
  }
86
97
 
87
- return 0
98
+ return a.counter - b.counter
88
99
  })
89
100
 
90
101
  const record = records.find((x) => x.expires >= now)
@@ -97,25 +108,23 @@ export default () => (dispatch) => {
97
108
 
98
109
  origin.hostname = record.address
99
110
 
100
- record.stats.pending++
111
+ record.counter++
112
+ record.pending++
101
113
  try {
102
114
  return dispatch(
103
115
  { ...opts, origin, headers: { ...opts.headers, host } },
104
- new Handler(handler, (err) => {
105
- record.stats.pending--
106
- if (err == null) {
107
- record.stats.errored = 0
108
- } else if (err.name === 'AbortError') {
109
- // Do nothing...
110
- } else if (err.statusCode == null || err.statusCode >= 500) {
111
- record.stats.errored++
112
- } else {
113
- record.stats.errored = 0
116
+ new Handler(handler, (err, statusCode) => {
117
+ record.pending--
118
+
119
+ if (statusCode != null && statusCode >= 500) {
120
+ record.errored++
121
+ } else if (err != null && err.name !== 'AbortError') {
122
+ record.expires = 0
114
123
  }
115
124
  }),
116
125
  )
117
126
  } catch (err) {
118
- record.stats.pending--
127
+ record.pending--
119
128
  throw err
120
129
  }
121
130
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "6.0.10",
3
+ "version": "6.0.12",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",