@nxtedition/nxt-undici 5.0.2 → 5.1.0

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
@@ -14,6 +14,7 @@ export const interceptors = {
14
14
  proxy: (await import('./interceptor/proxy.js')).default,
15
15
  cache: (await import('./interceptor/cache.js')).default,
16
16
  requestId: (await import('./interceptor/request-id.js')).default,
17
+ dns: (await import('./interceptor/dns.js')).default,
17
18
  lookup: (await import('./interceptor/lookup.js')).default,
18
19
  }
19
20
 
@@ -57,6 +58,7 @@ function wrapDispatch(dispatcher) {
57
58
  interceptors.responseError(),
58
59
  interceptors.requestBodyFactory(),
59
60
  interceptors.log(),
61
+ interceptors.dns(),
60
62
  interceptors.lookup(),
61
63
  interceptors.requestId(),
62
64
  interceptors.responseRetry(),
@@ -97,6 +99,7 @@ function wrapDispatch(dispatcher) {
97
99
  error: opts.error ?? true,
98
100
  verify: opts.verify ?? true,
99
101
  logger: opts.logger ?? null,
102
+ dns: opts.dns ?? true,
100
103
  connect: opts.connect,
101
104
  lookup: opts.lookup ?? defaultLookup,
102
105
  maxRedirections: 0, // TODO (fix): Ugly hack to disable undici redirections.
@@ -129,7 +129,7 @@ function makeKey(opts) {
129
129
 
130
130
  const DEFAULT_CACHE_STORE = new MemoryCacheStore({ maxSize: 128 * 1024, maxEntrySize: 1024 })
131
131
 
132
- export default (opts) => (dispatch) => (opts, handler) => {
132
+ export default () => (dispatch) => (opts, handler) => {
133
133
  if (!opts.cache || opts.upgrade) {
134
134
  return dispatch(opts, handler)
135
135
  }
@@ -0,0 +1,22 @@
1
+ import net from 'node:net'
2
+ import { resolve4 } from 'node:dns/promises'
3
+
4
+ export default () => (dispatch) => {
5
+ return async (opts, handler) => {
6
+ if (!opts.dns) {
7
+ return dispatch(opts, handler)
8
+ }
9
+
10
+ const origin = new URL(opts.origin)
11
+
12
+ if (net.isIP(origin.hostname)) {
13
+ return dispatch(opts, handler)
14
+ }
15
+
16
+ const host = origin.host
17
+ const records = await resolve4(origin.hostname)
18
+ origin.hostname = records[Math.floor(Math.random() * records.length)]
19
+
20
+ return dispatch({ ...opts, origin, headers: { ...opts.headers, host } }, handler)
21
+ }
22
+ }
@@ -120,5 +120,5 @@ class Handler extends DecoratorHandler {
120
120
  }
121
121
  }
122
122
 
123
- export default (opts) => (dispatch) => (opts, handler) =>
123
+ export default () => (dispatch) => (opts, handler) =>
124
124
  opts.logger ? dispatch(opts, new Handler(opts, { handler })) : dispatch(opts, handler)
@@ -1,4 +1,4 @@
1
- export default (opts) => (dispatch) => (opts, handler) => {
1
+ export default () => (dispatch) => (opts, handler) => {
2
2
  const lookup = opts.lookup
3
3
 
4
4
  if (!lookup) {
@@ -149,7 +149,7 @@ function printIp(address, port) {
149
149
  return str
150
150
  }
151
151
 
152
- export default (opts) => (dispatch) => (opts, handler) => {
152
+ export default () => (dispatch) => (opts, handler) => {
153
153
  if (!opts.proxy) {
154
154
  return dispatch(opts, handler)
155
155
  }
@@ -184,5 +184,5 @@ function cleanRequestHeaders(headers, removeContent, unknownOrigin) {
184
184
  return ret
185
185
  }
186
186
 
187
- export default (opts) => (dispatch) => (opts, handler) =>
187
+ export default () => (dispatch) => (opts, handler) =>
188
188
  opts.follow ? dispatch(opts, new Handler(opts, { handler, dispatch })) : dispatch(opts, handler)
@@ -1,4 +1,4 @@
1
- export default (opts) => (dispatch) => (opts, handler) => {
1
+ export default () => (dispatch) => (opts, handler) => {
2
2
  if (typeof opts.body !== 'function') {
3
3
  return dispatch(opts, handler)
4
4
  }
@@ -11,7 +11,7 @@ function genReqId() {
11
11
  return `req-${nextReqId.toString(36)}`
12
12
  }
13
13
 
14
- export default (opts) => (dispatch) => (opts, handler) => {
14
+ export default () => (dispatch) => (opts, handler) => {
15
15
  const id = opts.id ? `${opts.id},${genReqId()}` : genReqId()
16
16
  return dispatch(
17
17
  {
@@ -129,7 +129,7 @@ class Handler extends DecoratorHandler {
129
129
  }
130
130
  }
131
131
 
132
- export default (opts) => (dispatch) => (opts, handler) =>
132
+ export default () => (dispatch) => (opts, handler) =>
133
133
  opts.error !== false && opts.throwOnError !== false
134
134
  ? dispatch(opts, new Handler(opts, { handler }))
135
135
  : dispatch(opts, handler)
@@ -31,7 +31,16 @@ class Handler extends DecoratorHandler {
31
31
 
32
32
  this.#handler = handler
33
33
  this.#dispatch = dispatch
34
- this.#opts = opts
34
+
35
+ if (typeof opts === 'number') {
36
+ this.#opts = { count: opts }
37
+ } else if (typeof opts === 'boolean') {
38
+ this.#opts = null
39
+ } else if (typeof opts === 'object') {
40
+ this.#opts = opts
41
+ } else {
42
+ throw new Error('invalid argument: opts')
43
+ }
35
44
  }
36
45
 
37
46
  onConnect(abort) {
@@ -149,7 +158,7 @@ class Handler extends DecoratorHandler {
149
158
  return
150
159
  }
151
160
 
152
- const retryPromise = retryFn(err, this.#retryCount++, { ...this.#opts.retry })
161
+ const retryPromise = retryFn(err, this.#retryCount, { ...this.#opts.retry })
153
162
  if (retryPromise == null) {
154
163
  this.#onError(err)
155
164
  return
@@ -164,6 +173,7 @@ class Handler extends DecoratorHandler {
164
173
  } else if (isDisturbed(this.#opts.body)) {
165
174
  this.#onError(this.#error)
166
175
  } else if (!this.#headersSent) {
176
+ this.#retryCount++
167
177
  this.#opts.logger?.debug({ retryCount: this.#retryCount }, 'retry response headers')
168
178
  this.#dispatch(this.#opts, this)
169
179
  } else {
@@ -181,6 +191,7 @@ class Handler extends DecoratorHandler {
181
191
  },
182
192
  }
183
193
 
194
+ this.#retryCount++
184
195
  this.#opts.logger?.debug({ retryCount: this.#retryCount }, 'retry response body')
185
196
  this.#dispatch(this.#opts, this)
186
197
  }
@@ -205,7 +216,7 @@ class Handler extends DecoratorHandler {
205
216
  }
206
217
  }
207
218
 
208
- export default (opts) => (dispatch) => (opts, handler) =>
219
+ export default () => (dispatch) => (opts, handler) =>
209
220
  // TODO (fix): HEAD, PUT, PATCH, DELETE, OPTIONS?
210
221
  opts.retry !== false && opts.method === 'GET' && !opts.upgrade
211
222
  ? dispatch(opts, new Handler(opts, { handler, dispatch }))
@@ -81,7 +81,7 @@ class Handler extends DecoratorHandler {
81
81
  }
82
82
  }
83
83
 
84
- export default (opts) => (dispatch) => (opts, handler) =>
84
+ export default () => (dispatch) => (opts, handler) =>
85
85
  !opts.upgrade && opts.verify !== false
86
86
  ? dispatch(opts, new Handler(opts, { handler }))
87
87
  : dispatch(opts, handler)
package/lib/utils.js CHANGED
@@ -263,10 +263,6 @@ export class DecoratorHandler {
263
263
  return this.#handler.onUpgrade?.(...args)
264
264
  }
265
265
 
266
- onResponseStarted(...args) {
267
- return this.#handler.onResponseStarted?.(...args)
268
- }
269
-
270
266
  onHeaders(...args) {
271
267
  return this.#handler.onHeaders?.(...args)
272
268
  }
@@ -278,10 +274,6 @@ export class DecoratorHandler {
278
274
  onComplete(...args) {
279
275
  return this.#handler.onComplete?.(...args)
280
276
  }
281
-
282
- onBodySent(...args) {
283
- return this.#handler.onBodySent?.(...args)
284
- }
285
277
  }
286
278
 
287
279
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "5.0.2",
3
+ "version": "5.1.0",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",
@@ -17,15 +17,12 @@
17
17
  "devDependencies": {
18
18
  "@types/node": "^22.10.1",
19
19
  "eslint": "^9.16.0",
20
- "eslint-config-prettier": "^9.1.0",
21
- "eslint-config-standard": "^17.0.0",
22
- "eslint-plugin-import": "^2.31.0",
23
20
  "eslint-plugin-n": "^17.14.0",
24
- "eslint-plugin-promise": "^7.2.1",
25
21
  "husky": "^9.1.7",
26
22
  "lint-staged": "^15.2.10",
27
23
  "pinst": "^3.0.0",
28
24
  "prettier": "^3.4.1",
25
+ "send": "^1.1.0",
29
26
  "tap": "^21.0.1"
30
27
  },
31
28
  "scripts": {
@@ -44,27 +41,5 @@
44
41
  "printWidth": 100,
45
42
  "semi": false,
46
43
  "singleQuote": true
47
- },
48
- "eslintConfig": {
49
- "parserOptions": {
50
- "ecmaFeatures": {
51
- "ecmaVersion": 2020
52
- }
53
- },
54
- "extends": [
55
- "standard",
56
- "prettier",
57
- "prettier/prettier"
58
- ],
59
- "rules": {
60
- "quotes": [
61
- "error",
62
- "single",
63
- {
64
- "avoidEscape": true,
65
- "allowTemplateLiterals": true
66
- }
67
- ]
68
- }
69
44
  }
70
45
  }