@nxtedition/nxt-undici 6.0.11 → 6.0.13
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/interceptor/dns.js +51 -53
- package/package.json +1 -1
package/lib/interceptor/dns.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import net from 'node:net'
|
|
2
|
-
import { resolve4 } from 'node:dns
|
|
2
|
+
import { resolve4 } from 'node:dns'
|
|
3
3
|
import { DecoratorHandler, getFastNow } from '../utils.js'
|
|
4
4
|
|
|
5
|
-
function noop() {}
|
|
6
|
-
|
|
7
5
|
const MAX_TTL = 10e3
|
|
8
6
|
|
|
9
7
|
class Handler extends DecoratorHandler {
|
|
@@ -42,21 +40,32 @@ export default () => (dispatch) => {
|
|
|
42
40
|
function resolve(hostname) {
|
|
43
41
|
let promise = promises.get(hostname)
|
|
44
42
|
if (!promise) {
|
|
45
|
-
promise =
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
43
|
+
promise = new Promise((resolve) =>
|
|
44
|
+
resolve4(hostname, { ttl: true }, (err, records) => {
|
|
45
|
+
let ret
|
|
46
|
+
|
|
47
|
+
if (err) {
|
|
48
|
+
ret = { error: err }
|
|
49
|
+
} else {
|
|
50
|
+
const now = getFastNow()
|
|
51
|
+
ret = {
|
|
52
|
+
value: records.map(({ address, ttl }) => ({
|
|
53
|
+
address,
|
|
54
|
+
expires: now + Math.min(MAX_TTL, 1e3 * ttl),
|
|
55
|
+
pending: 0,
|
|
56
|
+
errored: 0,
|
|
57
|
+
counter: 0,
|
|
58
|
+
})),
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
54
62
|
cache.set(hostname, ret)
|
|
55
|
-
|
|
56
|
-
})
|
|
57
|
-
.finally(() => {
|
|
63
|
+
|
|
58
64
|
promises.delete(hostname)
|
|
59
|
-
|
|
65
|
+
|
|
66
|
+
resolve(ret)
|
|
67
|
+
}),
|
|
68
|
+
)
|
|
60
69
|
promises.set(hostname, promise)
|
|
61
70
|
}
|
|
62
71
|
return promise
|
|
@@ -73,61 +82,50 @@ export default () => (dispatch) => {
|
|
|
73
82
|
return dispatch(opts, handler)
|
|
74
83
|
}
|
|
75
84
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const now = getFastNow()
|
|
79
|
-
|
|
80
|
-
let records = cache.get(hostname)
|
|
85
|
+
try {
|
|
86
|
+
const { host, hostname } = origin
|
|
81
87
|
|
|
82
|
-
|
|
83
|
-
records = await resolve(hostname)
|
|
84
|
-
} else if (records.some((x) => x.expires < now + 1e3)) {
|
|
85
|
-
resolve(hostname).catch(noop)
|
|
86
|
-
}
|
|
88
|
+
const now = getFastNow()
|
|
87
89
|
|
|
88
|
-
|
|
89
|
-
if (a.errored !== b.errored) {
|
|
90
|
-
return a.errored - b.errored
|
|
91
|
-
}
|
|
90
|
+
let records = cache.get(hostname)?.value
|
|
92
91
|
|
|
93
|
-
if (
|
|
94
|
-
|
|
92
|
+
if (records == null || records.every((x) => x.expires < now)) {
|
|
93
|
+
records = await resolve(hostname)
|
|
94
|
+
} else if (records.some((x) => x.expires < now + 1e3)) {
|
|
95
|
+
resolve(hostname)
|
|
95
96
|
}
|
|
96
97
|
|
|
97
|
-
|
|
98
|
-
|
|
98
|
+
records.sort(
|
|
99
|
+
(a, b) => a.errored - b.errored || a.pending - b.pending || a.counter - b.counter,
|
|
100
|
+
)
|
|
99
101
|
|
|
100
|
-
|
|
102
|
+
const record = records.find((x) => x.expires >= now)
|
|
101
103
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
104
|
+
if (!record) {
|
|
105
|
+
throw Object.assign(new Error(`No available DNS records found for ${hostname}`), {
|
|
106
|
+
code: 'ENOTFOUND',
|
|
107
|
+
})
|
|
108
|
+
}
|
|
107
109
|
|
|
108
|
-
|
|
110
|
+
origin.hostname = record.address
|
|
109
111
|
|
|
110
|
-
|
|
111
|
-
try {
|
|
112
|
-
return dispatch(
|
|
112
|
+
dispatch(
|
|
113
113
|
{ ...opts, origin, headers: { ...opts.headers, host } },
|
|
114
114
|
new Handler(handler, (err, statusCode) => {
|
|
115
115
|
record.pending--
|
|
116
116
|
|
|
117
|
-
if (
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
} else {
|
|
121
|
-
record.errored = 0
|
|
122
|
-
}
|
|
123
|
-
} else if (err.name !== 'AbortError') {
|
|
117
|
+
if (statusCode != null && statusCode >= 500) {
|
|
118
|
+
record.errored++
|
|
119
|
+
} else if (err != null && err.name !== 'AbortError') {
|
|
124
120
|
record.expires = 0
|
|
125
121
|
}
|
|
126
122
|
}),
|
|
127
123
|
)
|
|
124
|
+
|
|
125
|
+
record.counter++
|
|
126
|
+
record.pending++
|
|
128
127
|
} catch (err) {
|
|
129
|
-
|
|
130
|
-
throw err
|
|
128
|
+
handler.onError(err)
|
|
131
129
|
}
|
|
132
130
|
}
|
|
133
131
|
}
|