@nxtedition/nxt-undici 6.4.18 → 6.4.19
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 +3 -3
- package/lib/interceptor/dns.js +59 -55
- package/lib/interceptor/request-body-factory.js +6 -13
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -120,7 +120,7 @@ function wrapDispatch(dispatcher) {
|
|
|
120
120
|
headersTimeout: opts.timeout?.headers ?? opts.headersTimeout,
|
|
121
121
|
bodyTimeout: opts.timeout?.body ?? opts.bodyTimeout,
|
|
122
122
|
idempotent: opts.idempotent,
|
|
123
|
-
retry: opts.retry ??
|
|
123
|
+
retry: opts.retry ?? 8,
|
|
124
124
|
proxy: opts.proxy ?? false,
|
|
125
125
|
cache: opts.cache ?? false,
|
|
126
126
|
upgrade: opts.upgrade ?? false,
|
|
@@ -133,7 +133,7 @@ function wrapDispatch(dispatcher) {
|
|
|
133
133
|
lookup: opts.lookup ?? defaultLookup,
|
|
134
134
|
},
|
|
135
135
|
handler,
|
|
136
|
-
)
|
|
136
|
+
)
|
|
137
137
|
},
|
|
138
138
|
)
|
|
139
139
|
dispatcherCache.set(dispatcher, wrappedDispatcher)
|
|
@@ -142,7 +142,7 @@ function wrapDispatch(dispatcher) {
|
|
|
142
142
|
}
|
|
143
143
|
|
|
144
144
|
export function dispatch(dispatcher, opts, handler) {
|
|
145
|
-
return wrapDispatch(dispatcher)(opts, handler)
|
|
145
|
+
return wrapDispatch(dispatcher)(opts, handler)
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
export function request(url, opts) {
|
package/lib/interceptor/dns.js
CHANGED
|
@@ -66,7 +66,7 @@ export default () => (dispatch) => {
|
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
return async (opts, handler) => {
|
|
69
|
-
if (!opts
|
|
69
|
+
if (!opts.dns || !opts.origin) {
|
|
70
70
|
return dispatch(opts, handler)
|
|
71
71
|
}
|
|
72
72
|
|
|
@@ -74,83 +74,87 @@ export default () => (dispatch) => {
|
|
|
74
74
|
const url = new URL(opts.path ?? '', opts.origin)
|
|
75
75
|
const balance = opts.dns.balance
|
|
76
76
|
|
|
77
|
-
|
|
77
|
+
const { host, hostname, pathname } = url
|
|
78
|
+
|
|
79
|
+
if (net.isIP(hostname)) {
|
|
78
80
|
return dispatch(opts, handler)
|
|
79
81
|
}
|
|
80
82
|
|
|
81
|
-
|
|
83
|
+
try {
|
|
84
|
+
const now = getFastNow()
|
|
82
85
|
|
|
83
|
-
|
|
86
|
+
let records = cache.get(hostname)
|
|
84
87
|
|
|
85
|
-
|
|
88
|
+
if (records == null || records.every((x) => x.expires < now)) {
|
|
89
|
+
const [err, val] = await resolve(hostname, { ttl })
|
|
86
90
|
|
|
87
|
-
|
|
88
|
-
|
|
91
|
+
if (err) {
|
|
92
|
+
throw Object.assign(new Error('lookup failed: ' + err.message), { cause: err })
|
|
93
|
+
}
|
|
89
94
|
|
|
90
|
-
|
|
91
|
-
|
|
95
|
+
records = val
|
|
96
|
+
} else if (records.some((x) => x.expires < now + 1e3)) {
|
|
97
|
+
resolve(hostname, { ttl })
|
|
92
98
|
}
|
|
93
99
|
|
|
94
|
-
|
|
95
|
-
} else if (records.some((x) => x.expires < now + 1e3)) {
|
|
96
|
-
resolve(hostname, { ttl })
|
|
97
|
-
}
|
|
100
|
+
let record
|
|
98
101
|
|
|
99
|
-
|
|
102
|
+
if (balance === 'hash') {
|
|
103
|
+
HASHER ??= await xxhash()
|
|
100
104
|
|
|
101
|
-
|
|
102
|
-
HASHER ??= await xxhash()
|
|
105
|
+
const hash = HASHER.h32(pathname)
|
|
103
106
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
break
|
|
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
|
+
}
|
|
111
113
|
}
|
|
112
114
|
}
|
|
113
|
-
}
|
|
114
115
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
116
|
+
if (record == null) {
|
|
117
|
+
records.sort(
|
|
118
|
+
(a, b) => a.errored - b.errored || a.pending - b.pending || a.counter - b.counter,
|
|
119
|
+
)
|
|
119
120
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
121
|
+
for (let i = 0; i < records.length; i++) {
|
|
122
|
+
if (records[i].expires >= now) {
|
|
123
|
+
record = records[i]
|
|
124
|
+
break
|
|
125
|
+
}
|
|
124
126
|
}
|
|
125
127
|
}
|
|
126
|
-
}
|
|
127
128
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
129
|
+
if (!record) {
|
|
130
|
+
throw Object.assign(new Error(`No available DNS records found for ${hostname}`), {
|
|
131
|
+
code: 'ENOTFOUND',
|
|
132
|
+
})
|
|
133
|
+
}
|
|
133
134
|
|
|
134
|
-
|
|
135
|
+
url.hostname = record.address
|
|
135
136
|
|
|
136
|
-
|
|
137
|
-
|
|
137
|
+
record.counter++
|
|
138
|
+
record.pending++
|
|
138
139
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
140
|
+
return dispatch(
|
|
141
|
+
{ ...opts, origin: url.origin, headers: { ...opts.headers, host } },
|
|
142
|
+
new Handler(handler, (err, statusCode) => {
|
|
143
|
+
record.pending--
|
|
143
144
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
145
|
+
if (err != null && err.name !== 'AbortError') {
|
|
146
|
+
record.expires = 0
|
|
147
|
+
} else if (statusCode != null && statusCode >= 500) {
|
|
148
|
+
record.errored++
|
|
149
|
+
}
|
|
149
150
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
151
|
+
if (err != null || statusCode >= 500) {
|
|
152
|
+
record.timeout = getFastNow() + 10e3
|
|
153
|
+
}
|
|
154
|
+
}),
|
|
155
|
+
)
|
|
156
|
+
} catch (err) {
|
|
157
|
+
handler.onError(err)
|
|
158
|
+
}
|
|
155
159
|
}
|
|
156
160
|
}
|
|
@@ -1,13 +1,6 @@
|
|
|
1
|
-
export default () => (dispatch) => (opts, handler) =>
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
if (typeof body?.then !== 'function') {
|
|
9
|
-
return dispatch({ ...opts, body }, handler)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return body.then((body) => dispatch({ ...opts, body }, handler))
|
|
13
|
-
}
|
|
1
|
+
export default () => (dispatch) => (opts, handler) =>
|
|
2
|
+
typeof opts.body !== 'function'
|
|
3
|
+
? dispatch(opts, handler)
|
|
4
|
+
: Promise.resolve(opts.body(opts))
|
|
5
|
+
.then((body) => dispatch({ ...opts, body }, handler))
|
|
6
|
+
.catch((err) => handler.onError(err))
|