@nxtedition/nxt-undici 6.4.18 → 6.4.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/lib/index.js CHANGED
@@ -120,7 +120,7 @@ function wrapDispatch(dispatcher) {
120
120
  headersTimeout: opts.timeout?.headers ?? opts.headersTimeout,
121
121
  bodyTimeout: opts.timeout?.body ?? opts.bodyTimeout,
122
122
  idempotent: opts.idempotent,
123
- retry: opts.retry ?? 4,
123
+ retry: opts.retry ?? 8,
124
124
  proxy: opts.proxy ?? false,
125
125
  cache: opts.cache ?? false,
126
126
  upgrade: opts.upgrade ?? false,
@@ -133,7 +133,7 @@ function wrapDispatch(dispatcher) {
133
133
  lookup: opts.lookup ?? defaultLookup,
134
134
  },
135
135
  handler,
136
- )?.catch((err) => handler.onError(err))
136
+ )
137
137
  },
138
138
  )
139
139
  dispatcherCache.set(dispatcher, wrappedDispatcher)
@@ -142,7 +142,7 @@ function wrapDispatch(dispatcher) {
142
142
  }
143
143
 
144
144
  export function dispatch(dispatcher, opts, handler) {
145
- return wrapDispatch(dispatcher)(opts, handler)?.catch((err) => handler.onError(err))
145
+ return wrapDispatch(dispatcher)(opts, handler)
146
146
  }
147
147
 
148
148
  export function request(url, opts) {
@@ -66,7 +66,7 @@ export default () => (dispatch) => {
66
66
  }
67
67
 
68
68
  return async (opts, handler) => {
69
- if (!opts || !opts.dns || !opts.origin) {
69
+ if (!opts.dns || !opts.origin) {
70
70
  return dispatch(opts, handler)
71
71
  }
72
72
 
@@ -74,83 +74,87 @@ export default () => (dispatch) => {
74
74
  const url = new URL(opts.path ?? '', opts.origin)
75
75
  const balance = opts.dns.balance
76
76
 
77
- if (net.isIP(url.hostname)) {
77
+ const { host, hostname, pathname } = url
78
+
79
+ if (net.isIP(hostname)) {
78
80
  return dispatch(opts, handler)
79
81
  }
80
82
 
81
- const { host, hostname } = url
83
+ try {
84
+ const now = getFastNow()
82
85
 
83
- const now = getFastNow()
86
+ let records = cache.get(hostname)
84
87
 
85
- let records = cache.get(hostname)
88
+ if (records == null || records.every((x) => x.expires < now)) {
89
+ const [err, val] = await resolve(hostname, { ttl })
86
90
 
87
- if (records == null || records.every((x) => x.expires < now)) {
88
- const [err, val] = await resolve(hostname, { ttl })
91
+ if (err) {
92
+ throw Object.assign(new Error('lookup failed: ' + err.message), { cause: err })
93
+ }
89
94
 
90
- if (err) {
91
- throw Object.assign(new Error('lookup failed: ' + err.message), { cause: err })
95
+ records = val
96
+ } else if (records.some((x) => x.expires < now + 1e3)) {
97
+ resolve(hostname, { ttl })
92
98
  }
93
99
 
94
- records = val
95
- } else if (records.some((x) => x.expires < now + 1e3)) {
96
- resolve(hostname, { ttl })
97
- }
100
+ let record
98
101
 
99
- let record
102
+ if (balance === 'hash') {
103
+ HASHER ??= await xxhash()
100
104
 
101
- if (balance === 'hash') {
102
- HASHER ??= await xxhash()
105
+ const hash = HASHER.h32(pathname)
103
106
 
104
- const hash = HASHER.h32(url.pathname)
105
-
106
- for (let i = 0; i < records.length; i++) {
107
- const idx = (hash + i) % records.length
108
- if (records[idx].expires >= now && records[idx].timeout < now) {
109
- record = records[idx]
110
- break
107
+ for (let i = 0; i < records.length; i++) {
108
+ const idx = (hash + i) % records.length
109
+ if (records[idx].expires >= now && records[idx].timeout < now) {
110
+ record = records[idx]
111
+ break
112
+ }
111
113
  }
112
114
  }
113
- }
114
115
 
115
- if (record == null) {
116
- records.sort(
117
- (a, b) => a.errored - b.errored || a.pending - b.pending || a.counter - b.counter,
118
- )
116
+ if (record == null) {
117
+ records.sort(
118
+ (a, b) => a.errored - b.errored || a.pending - b.pending || a.counter - b.counter,
119
+ )
119
120
 
120
- for (let i = 0; i < records.length; i++) {
121
- if (records[i].expires >= now) {
122
- record = records[i]
123
- break
121
+ for (let i = 0; i < records.length; i++) {
122
+ if (records[i].expires >= now) {
123
+ record = records[i]
124
+ break
125
+ }
124
126
  }
125
127
  }
126
- }
127
128
 
128
- if (!record) {
129
- throw Object.assign(new Error(`No available DNS records found for ${hostname}`), {
130
- code: 'ENOTFOUND',
131
- })
132
- }
129
+ if (!record) {
130
+ throw Object.assign(new Error(`No available DNS records found for ${hostname}`), {
131
+ code: 'ENOTFOUND',
132
+ })
133
+ }
133
134
 
134
- url.hostname = record.address
135
+ url.hostname = record.address
135
136
 
136
- record.counter++
137
- record.pending++
137
+ record.counter++
138
+ record.pending++
138
139
 
139
- return dispatch(
140
- { ...opts, origin: url.origin, headers: { ...opts.headers, host } },
141
- new Handler(handler, (err, statusCode) => {
142
- record.pending--
140
+ return dispatch(
141
+ { ...opts, origin: url.origin, headers: { ...opts.headers, host } },
142
+ new Handler(handler, (err, statusCode) => {
143
+ record.pending--
143
144
 
144
- if (err != null && err.name !== 'AbortError') {
145
- record.expires = 0
146
- } else if (statusCode != null && statusCode >= 500) {
147
- record.errored++
148
- }
145
+ if (err != null && err.name !== 'AbortError') {
146
+ record.expires = 0
147
+ } else if (statusCode != null && statusCode >= 500) {
148
+ record.errored++
149
+ }
149
150
 
150
- if (err != null || statusCode >= 500) {
151
- record.timeout = getFastNow() + 10e3
152
- }
153
- }),
154
- )
151
+ if (err != null || statusCode >= 500) {
152
+ record.timeout = getFastNow() + 10e3
153
+ }
154
+ }),
155
+ )
156
+ } catch (err) {
157
+ handler.onError(err)
158
+ }
155
159
  }
156
160
  }
@@ -1,13 +1,6 @@
1
- export default () => (dispatch) => (opts, handler) => {
2
- if (typeof opts.body !== 'function') {
3
- return dispatch(opts, handler)
4
- }
5
-
6
- const body = opts.body({ signal: opts.signal ?? undefined })
7
-
8
- if (typeof body?.then !== 'function') {
9
- return dispatch({ ...opts, body }, handler)
10
- }
11
-
12
- return body.then((body) => dispatch({ ...opts, body }, handler))
13
- }
1
+ export default () => (dispatch) => (opts, handler) =>
2
+ typeof opts.body !== 'function'
3
+ ? dispatch(opts, handler)
4
+ : Promise.resolve(opts.body(opts))
5
+ .then((body) => dispatch({ ...opts, body }, handler))
6
+ .catch((err) => handler.onError(err))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "6.4.18",
3
+ "version": "6.4.19",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",