@nxtedition/nxt-undici 6.2.0 → 6.2.3
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 +9 -28
- package/package.json +1 -1
package/lib/interceptor/dns.js
CHANGED
|
@@ -28,33 +28,18 @@ class Handler extends DecoratorHandler {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
class DefaultCache {
|
|
32
|
-
#map = new Map()
|
|
33
|
-
|
|
34
|
-
set(hostname, records) {
|
|
35
|
-
this.#map.set(hostname, records)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
get(hostname) {
|
|
39
|
-
return this.#map.get(hostname)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
delete(hostname) {
|
|
43
|
-
this.#map.delete(hostname)
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const DEFAULT_CACHE = new DefaultCache()
|
|
48
|
-
|
|
49
31
|
export default () => (dispatch) => {
|
|
32
|
+
const cache = new Map()
|
|
50
33
|
const promises = new Map()
|
|
51
34
|
|
|
52
|
-
function resolve(hostname, {
|
|
35
|
+
function resolve(hostname, { logger }) {
|
|
53
36
|
let promise = promises.get(hostname)
|
|
54
37
|
if (!promise) {
|
|
55
38
|
logger?.debug({ dns: { hostname } }, 'lookup started')
|
|
56
39
|
promise = new Promise((resolve) =>
|
|
57
|
-
resolve4(hostname, { ttl: true }, (err, records) => {
|
|
40
|
+
dns.resolve4(hostname, { ttl: true }, (err, records) => {
|
|
41
|
+
promises.delete(hostname)
|
|
42
|
+
|
|
58
43
|
if (err) {
|
|
59
44
|
logger?.error({ err, dns: { hostname } }, 'lookup failed')
|
|
60
45
|
resolve([err, null])
|
|
@@ -74,8 +59,6 @@ export default () => (dispatch) => {
|
|
|
74
59
|
|
|
75
60
|
resolve([null, val])
|
|
76
61
|
}
|
|
77
|
-
|
|
78
|
-
assert(promises.delete(hostname))
|
|
79
62
|
}),
|
|
80
63
|
)
|
|
81
64
|
promises.set(hostname, promise)
|
|
@@ -96,17 +79,15 @@ export default () => (dispatch) => {
|
|
|
96
79
|
|
|
97
80
|
try {
|
|
98
81
|
const { host, hostname } = origin
|
|
99
|
-
const cache = opts.dns.cache ?? DEFAULT_CACHE
|
|
100
82
|
|
|
101
83
|
const now = getFastNow()
|
|
102
84
|
|
|
103
|
-
let records = cache.get(hostname)
|
|
104
|
-
|
|
105
|
-
const resolve4 = opts.dns.resolve4 || dns.resolve4
|
|
106
85
|
const logger = opts.dns.logger ?? opts.logger
|
|
107
86
|
|
|
87
|
+
let records = cache.get(hostname)
|
|
88
|
+
|
|
108
89
|
if (records == null || records.every((x) => x.expires < now)) {
|
|
109
|
-
const [err, val] = await resolve(hostname, {
|
|
90
|
+
const [err, val] = await resolve(hostname, { logger })
|
|
110
91
|
|
|
111
92
|
if (err) {
|
|
112
93
|
throw err
|
|
@@ -116,7 +97,7 @@ export default () => (dispatch) => {
|
|
|
116
97
|
|
|
117
98
|
records = val
|
|
118
99
|
} else if (records.some((x) => x.expires < now + 1e3)) {
|
|
119
|
-
resolve(hostname, {
|
|
100
|
+
resolve(hostname, { logger })
|
|
120
101
|
}
|
|
121
102
|
|
|
122
103
|
records.sort(
|