@nxtedition/nxt-undici 4.2.20 → 4.2.21

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
@@ -13,6 +13,7 @@ export const interceptors = {
13
13
  proxy: (await import('./interceptor/proxy.js')).default,
14
14
  cache: (await import('./interceptor/cache.js')).default,
15
15
  requestId: (await import('./interceptor/request-id.js')).default,
16
+ dns: (await import('./interceptor/dns.js')).default,
16
17
  lookup: (await import('./interceptor/lookup.js')).default,
17
18
  }
18
19
 
@@ -30,6 +31,7 @@ function wrapDispatcher(dispatcher) {
30
31
  interceptors.responseError(),
31
32
  interceptors.requestBodyFactory(),
32
33
  interceptors.log(),
34
+ interceptors.dns(),
33
35
  interceptors.lookup(),
34
36
  interceptors.requestId(),
35
37
  interceptors.responseRetry(),
@@ -58,7 +60,7 @@ function wrapDispatcher(dispatcher) {
58
60
  headers,
59
61
  signal: opts.signal,
60
62
  reset: opts.reset ?? false,
61
- blocking: opts.blocking ?? null,
63
+ blocking: opts.blocking ?? opts.method !== 'HEAD',
62
64
  headersTimeout: opts.headersTimeout,
63
65
  bodyTimeout: opts.bodyTimeout,
64
66
  idempotent: opts.idempotent,
@@ -70,6 +72,7 @@ function wrapDispatcher(dispatcher) {
70
72
  error: opts.error ?? true,
71
73
  verify: opts.verify ?? true,
72
74
  logger: opts.logger ?? null,
75
+ dns: opts.dns ?? true,
73
76
  connect: opts.connect,
74
77
  lookup: opts.lookup ?? defaultLookup,
75
78
  maxRedirections: 0, // TODO (fix): Ugly hack to disable undici redirections.
@@ -0,0 +1,125 @@
1
+ import { DecoratorHandler } from '../utils.js'
2
+ import net from 'net'
3
+ import { resolve4 } from 'node:dns/promises'
4
+
5
+ let fastNow = Date.now()
6
+ setInterval(() => {
7
+ fastNow = Date.now()
8
+ }, 500)
9
+
10
+ class Record {
11
+ address = ''
12
+ expires = 0
13
+ errored = 0
14
+
15
+ constructor({ address, ttl }) {
16
+ this.address = address
17
+ this.expires = fastNow + (ttl ?? 60) * 1e3
18
+ }
19
+ }
20
+
21
+ class Handler extends DecoratorHandler {
22
+ #handler
23
+ #record
24
+
25
+ constructor({ record }, { handler }) {
26
+ super(handler)
27
+
28
+ this.#handler = handler
29
+ this.#record = record
30
+ }
31
+
32
+ onError(err) {
33
+ if (
34
+ [
35
+ 'ECONNRESET',
36
+ 'ECONNREFUSED',
37
+ 'ENOTFOUND',
38
+ 'ENETDOWN',
39
+ 'ENETUNREACH',
40
+ 'EHOSTDOWN',
41
+ 'EHOSTUNREACH',
42
+ 'EHOSTNOTFOUND',
43
+ 'ENODATA',
44
+ 'EPIPE',
45
+ 'UND_ERR_CONNECT_TIMEOUT',
46
+ ].includes(err.code) ||
47
+ [503].includes(err.statusCode)
48
+ ) {
49
+ this.#record.errored = fastNow
50
+
51
+ // TODO (fix): For how long do we "blacklist" the record?
52
+
53
+ if (err.code === 'UND_ERR_CONNECT_TIMEOUT') {
54
+ // We don't expect this address to ever work again...
55
+ this.#record.expires = Infinity
56
+ }
57
+ }
58
+
59
+ return super.onError(err)
60
+ }
61
+ }
62
+
63
+ export default (interceptorOpts) => (dispatch) => {
64
+ /** @type {Map<string, Array<Record>>} */
65
+ const dnsCache = new Map()
66
+
67
+ return async (opts, handler) => {
68
+ if (!opts.dns) {
69
+ return dispatch(opts, handler)
70
+ }
71
+
72
+ const { protocol, port, hostname, host } = new URL(opts.origin)
73
+
74
+ if (net.isIP(hostname) || opts.headers?.host || !port || !protocol) {
75
+ return dispatch(opts, handler)
76
+ }
77
+
78
+ const now = Date.now()
79
+ try {
80
+ /** @type {Array|undefined} */
81
+ let records = dnsCache.get(hostname)
82
+
83
+ if (!records?.some((record) => record.expires > now && !record.errored)) {
84
+ // TODO (fix): Re-use old records while fetching new ones or if fetching fails?
85
+ // TODO (fix): Background refresh + health checks?
86
+ // TODO (fix): What about old "blacklisted" records?
87
+ // TODO (fix): What about ipv6?
88
+
89
+ records = await resolve4(hostname, { ttl: true })
90
+ records = records.map((record) => new Record(record))
91
+
92
+ if (records.length > 0) {
93
+ // TODO (fix): Clear old hostnames?
94
+ dnsCache.set(hostname, records)
95
+ }
96
+ }
97
+
98
+ if (records.length === 0) {
99
+ return dispatch(opts, handler)
100
+ }
101
+
102
+ // TODO (perf): sort + Math.random is a bit naive...
103
+ records.sort((a, b) =>
104
+ a.errored !== b.errored ? a.errored - b.errored : Math.random() - 0.5,
105
+ )
106
+
107
+ const record = records.find((record) => record.expires > now)
108
+
109
+ if (!record) {
110
+ return dispatch(opts, handler)
111
+ }
112
+
113
+ return dispatch(
114
+ {
115
+ ...opts,
116
+ origin: `${protocol}//${record.address}:${port}`,
117
+ headers: { ...opts.headers, host },
118
+ },
119
+ new Handler({ record }, { handler }),
120
+ )
121
+ } catch (err) {
122
+ handler.onError(err)
123
+ }
124
+ }
125
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "4.2.20",
3
+ "version": "4.2.21",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",