@nxtedition/nxt-undici 6.3.21 → 6.4.1
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 +41 -9
- package/package.json +3 -2
package/lib/interceptor/dns.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import net from 'node:net'
|
|
2
2
|
import * as dns from 'node:dns'
|
|
3
3
|
import { DecoratorHandler, getFastNow } from '../utils.js'
|
|
4
|
+
import xxhash from 'xxhash-wasm'
|
|
5
|
+
|
|
6
|
+
let HASHER
|
|
4
7
|
|
|
5
8
|
class Handler extends DecoratorHandler {
|
|
6
9
|
#callback
|
|
@@ -48,6 +51,7 @@ export default () => (dispatch) => {
|
|
|
48
51
|
pending: 0,
|
|
49
52
|
errored: 0,
|
|
50
53
|
counter: 0,
|
|
54
|
+
timeout: 0,
|
|
51
55
|
}))
|
|
52
56
|
|
|
53
57
|
cache.set(hostname, val)
|
|
@@ -67,14 +71,15 @@ export default () => (dispatch) => {
|
|
|
67
71
|
}
|
|
68
72
|
|
|
69
73
|
const ttl = opts.dns.ttl ?? 2e3
|
|
70
|
-
const
|
|
74
|
+
const url = new URL(opts.path ?? '', opts.origin)
|
|
75
|
+
const balance = opts.dns.balance
|
|
71
76
|
|
|
72
|
-
if (net.isIP(
|
|
77
|
+
if (net.isIP(url.hostname)) {
|
|
73
78
|
return dispatch(opts, handler)
|
|
74
79
|
}
|
|
75
80
|
|
|
76
81
|
try {
|
|
77
|
-
const { host, hostname } =
|
|
82
|
+
const { host, hostname } = url
|
|
78
83
|
|
|
79
84
|
const now = getFastNow()
|
|
80
85
|
|
|
@@ -92,11 +97,34 @@ export default () => (dispatch) => {
|
|
|
92
97
|
resolve(hostname, { ttl })
|
|
93
98
|
}
|
|
94
99
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
)
|
|
100
|
+
let record
|
|
101
|
+
|
|
102
|
+
if (balance === 'hash') {
|
|
103
|
+
HASHER ??= await xxhash()
|
|
104
|
+
|
|
105
|
+
const hash = HASHER.h32(url.href)
|
|
106
|
+
|
|
107
|
+
for (let i = 0; i < records.length; i++) {
|
|
108
|
+
const idx = (hash + i) % records.length
|
|
109
|
+
if (records[idx].expires >= now && records[idx].timeout < now) {
|
|
110
|
+
record = records[idx]
|
|
111
|
+
break
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
98
115
|
|
|
99
|
-
|
|
116
|
+
if (record == null) {
|
|
117
|
+
records.sort(
|
|
118
|
+
(a, b) => a.errored - b.errored || a.pending - b.pending || a.counter - b.counter,
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
for (let i = 0; i < records.length; i++) {
|
|
122
|
+
if (records[i].expires >= now) {
|
|
123
|
+
record = records[i]
|
|
124
|
+
break
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
100
128
|
|
|
101
129
|
if (!record) {
|
|
102
130
|
throw Object.assign(new Error(`No available DNS records found for ${hostname}`), {
|
|
@@ -104,10 +132,10 @@ export default () => (dispatch) => {
|
|
|
104
132
|
})
|
|
105
133
|
}
|
|
106
134
|
|
|
107
|
-
|
|
135
|
+
url.hostname = record.address
|
|
108
136
|
|
|
109
137
|
dispatch(
|
|
110
|
-
{ ...opts, origin, headers: { ...opts.headers, host } },
|
|
138
|
+
{ ...opts, origin: url.origin, headers: { ...opts.headers, host } },
|
|
111
139
|
new Handler(handler, (err, statusCode) => {
|
|
112
140
|
record.pending--
|
|
113
141
|
|
|
@@ -116,6 +144,10 @@ export default () => (dispatch) => {
|
|
|
116
144
|
} else if (statusCode != null && statusCode >= 500) {
|
|
117
145
|
record.errored++
|
|
118
146
|
}
|
|
147
|
+
|
|
148
|
+
if (err != null || statusCode >= 500) {
|
|
149
|
+
record.timeout = getFastNow() + 10e3
|
|
150
|
+
}
|
|
119
151
|
}),
|
|
120
152
|
)
|
|
121
153
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/nxt-undici",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.4.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Robert Nagy <robert.nagy@boffins.se>",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
"@nxtedition/undici": "^11.0.0",
|
|
13
13
|
"cache-control-parser": "^2.0.6",
|
|
14
14
|
"fast-querystring": "^1.1.2",
|
|
15
|
-
"http-errors": "^2.0.0"
|
|
15
|
+
"http-errors": "^2.0.0",
|
|
16
|
+
"xxhash-wasm": "^1.1.0"
|
|
16
17
|
},
|
|
17
18
|
"devDependencies": {
|
|
18
19
|
"@types/node": "^22.13.10",
|