@nxtedition/nxt-undici 6.0.12 → 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 +48 -48
- 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,22 +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
|
-
|
|
54
|
-
|
|
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
|
+
|
|
55
62
|
cache.set(hostname, ret)
|
|
56
|
-
|
|
57
|
-
})
|
|
58
|
-
.finally(() => {
|
|
63
|
+
|
|
59
64
|
promises.delete(hostname)
|
|
60
|
-
|
|
65
|
+
|
|
66
|
+
resolve(ret)
|
|
67
|
+
}),
|
|
68
|
+
)
|
|
61
69
|
promises.set(hostname, promise)
|
|
62
70
|
}
|
|
63
71
|
return promise
|
|
@@ -74,44 +82,34 @@ export default () => (dispatch) => {
|
|
|
74
82
|
return dispatch(opts, handler)
|
|
75
83
|
}
|
|
76
84
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const now = getFastNow()
|
|
85
|
+
try {
|
|
86
|
+
const { host, hostname } = origin
|
|
80
87
|
|
|
81
|
-
|
|
88
|
+
const now = getFastNow()
|
|
82
89
|
|
|
83
|
-
|
|
84
|
-
records = await resolve(hostname)
|
|
85
|
-
} else if (records.some((x) => x.expires < now + 1e3)) {
|
|
86
|
-
resolve(hostname).catch(noop)
|
|
87
|
-
}
|
|
90
|
+
let records = cache.get(hostname)?.value
|
|
88
91
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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)
|
|
92
96
|
}
|
|
93
97
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return a.counter - b.counter
|
|
99
|
-
})
|
|
98
|
+
records.sort(
|
|
99
|
+
(a, b) => a.errored - b.errored || a.pending - b.pending || a.counter - b.counter,
|
|
100
|
+
)
|
|
100
101
|
|
|
101
|
-
|
|
102
|
+
const record = records.find((x) => x.expires >= now)
|
|
102
103
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
104
|
+
if (!record) {
|
|
105
|
+
throw Object.assign(new Error(`No available DNS records found for ${hostname}`), {
|
|
106
|
+
code: 'ENOTFOUND',
|
|
107
|
+
})
|
|
108
|
+
}
|
|
108
109
|
|
|
109
|
-
|
|
110
|
+
origin.hostname = record.address
|
|
110
111
|
|
|
111
|
-
|
|
112
|
-
record.pending++
|
|
113
|
-
try {
|
|
114
|
-
return dispatch(
|
|
112
|
+
dispatch(
|
|
115
113
|
{ ...opts, origin, headers: { ...opts.headers, host } },
|
|
116
114
|
new Handler(handler, (err, statusCode) => {
|
|
117
115
|
record.pending--
|
|
@@ -123,9 +121,11 @@ export default () => (dispatch) => {
|
|
|
123
121
|
}
|
|
124
122
|
}),
|
|
125
123
|
)
|
|
124
|
+
|
|
125
|
+
record.counter++
|
|
126
|
+
record.pending++
|
|
126
127
|
} catch (err) {
|
|
127
|
-
|
|
128
|
-
throw err
|
|
128
|
+
handler.onError(err)
|
|
129
129
|
}
|
|
130
130
|
}
|
|
131
131
|
}
|