@nxtedition/nxt-undici 6.4.11 → 6.4.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 +53 -57
- package/lib/interceptor/log.js +9 -7
- package/lib/interceptor/lookup.js +5 -20
- package/lib/interceptor/redirect.js +5 -1
- package/lib/interceptor/request-body-factory.js +1 -8
- package/lib/interceptor/response-retry.js +2 -2
- package/lib/request.js +3 -1
- package/package.json +1 -1
package/lib/interceptor/dns.js
CHANGED
|
@@ -78,83 +78,79 @@ export default () => (dispatch) => {
|
|
|
78
78
|
return dispatch(opts, handler)
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
|
|
82
|
-
const { host, hostname } = url
|
|
81
|
+
const { host, hostname } = url
|
|
83
82
|
|
|
84
|
-
|
|
83
|
+
const now = getFastNow()
|
|
85
84
|
|
|
86
|
-
|
|
85
|
+
let records = cache.get(hostname)
|
|
87
86
|
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
if (records == null || records.every((x) => x.expires < now)) {
|
|
88
|
+
const [err, val] = await resolve(hostname, { ttl })
|
|
90
89
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
records = val
|
|
96
|
-
} else if (records.some((x) => x.expires < now + 1e3)) {
|
|
97
|
-
resolve(hostname, { ttl })
|
|
90
|
+
if (err) {
|
|
91
|
+
throw err
|
|
98
92
|
}
|
|
99
93
|
|
|
100
|
-
|
|
94
|
+
records = val
|
|
95
|
+
} else if (records.some((x) => x.expires < now + 1e3)) {
|
|
96
|
+
resolve(hostname, { ttl })
|
|
97
|
+
}
|
|
101
98
|
|
|
102
|
-
|
|
103
|
-
HASHER ??= await xxhash()
|
|
99
|
+
let record
|
|
104
100
|
|
|
105
|
-
|
|
101
|
+
if (balance === 'hash') {
|
|
102
|
+
HASHER ??= await xxhash()
|
|
106
103
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
104
|
+
const hash = HASHER.h32(url.pathname)
|
|
105
|
+
|
|
106
|
+
for (let i = 0; i < records.length; i++) {
|
|
107
|
+
const idx = (hash + i) % records.length
|
|
108
|
+
if (records[idx].expires >= now && records[idx].timeout < now) {
|
|
109
|
+
record = records[idx]
|
|
110
|
+
break
|
|
113
111
|
}
|
|
114
112
|
}
|
|
113
|
+
}
|
|
115
114
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
115
|
+
if (record == null) {
|
|
116
|
+
records.sort(
|
|
117
|
+
(a, b) => a.errored - b.errored || a.pending - b.pending || a.counter - b.counter,
|
|
118
|
+
)
|
|
120
119
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
120
|
+
for (let i = 0; i < records.length; i++) {
|
|
121
|
+
if (records[i].expires >= now) {
|
|
122
|
+
record = records[i]
|
|
123
|
+
break
|
|
126
124
|
}
|
|
127
125
|
}
|
|
126
|
+
}
|
|
128
127
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
128
|
+
if (!record) {
|
|
129
|
+
throw Object.assign(new Error(`No available DNS records found for ${hostname}`), {
|
|
130
|
+
code: 'ENOTFOUND',
|
|
131
|
+
})
|
|
132
|
+
}
|
|
134
133
|
|
|
135
|
-
|
|
134
|
+
url.hostname = record.address
|
|
136
135
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
new Handler(handler, (err, statusCode) => {
|
|
140
|
-
record.pending--
|
|
136
|
+
record.counter++
|
|
137
|
+
record.pending++
|
|
141
138
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
139
|
+
return dispatch(
|
|
140
|
+
{ ...opts, origin: url.origin, headers: { ...opts.headers, host } },
|
|
141
|
+
new Handler(handler, (err, statusCode) => {
|
|
142
|
+
record.pending--
|
|
147
143
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
144
|
+
if (err != null && err.name !== 'AbortError') {
|
|
145
|
+
record.expires = 0
|
|
146
|
+
} else if (statusCode != null && statusCode >= 500) {
|
|
147
|
+
record.errored++
|
|
148
|
+
}
|
|
153
149
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
150
|
+
if (err != null || statusCode >= 500) {
|
|
151
|
+
record.timeout = getFastNow() + 10e3
|
|
152
|
+
}
|
|
153
|
+
}),
|
|
154
|
+
)
|
|
159
155
|
}
|
|
160
156
|
}
|
package/lib/interceptor/log.js
CHANGED
|
@@ -7,8 +7,9 @@ class Handler extends DecoratorHandler {
|
|
|
7
7
|
#abort
|
|
8
8
|
#aborted = false
|
|
9
9
|
#pos = 0
|
|
10
|
+
#created = performance.now()
|
|
10
11
|
#timing = {
|
|
11
|
-
created:
|
|
12
|
+
created: -1,
|
|
12
13
|
connect: -1,
|
|
13
14
|
headers: -1,
|
|
14
15
|
data: -1,
|
|
@@ -29,13 +30,14 @@ class Handler extends DecoratorHandler {
|
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
this.#logger.debug('upstream request started')
|
|
33
|
+
this.#timing.created = this.#created + performance.timeOrigin
|
|
32
34
|
}
|
|
33
35
|
|
|
34
36
|
onConnect(abort) {
|
|
35
37
|
this.#pos = 0
|
|
36
38
|
this.#abort = abort
|
|
37
39
|
|
|
38
|
-
this.#timing.connect = performance.now() - this.#
|
|
40
|
+
this.#timing.connect = performance.now() - this.#created
|
|
39
41
|
this.#timing.headers = -1
|
|
40
42
|
this.#timing.data = -1
|
|
41
43
|
this.#timing.end = -1
|
|
@@ -47,7 +49,7 @@ class Handler extends DecoratorHandler {
|
|
|
47
49
|
}
|
|
48
50
|
|
|
49
51
|
onUpgrade(statusCode, headers, socket) {
|
|
50
|
-
this.#timing.headers = performance.now() - this.#
|
|
52
|
+
this.#timing.headers = performance.now() - this.#created
|
|
51
53
|
|
|
52
54
|
this.#logger.debug(
|
|
53
55
|
{
|
|
@@ -65,7 +67,7 @@ class Handler extends DecoratorHandler {
|
|
|
65
67
|
}
|
|
66
68
|
|
|
67
69
|
onHeaders(statusCode, headers, resume) {
|
|
68
|
-
this.#timing.headers = performance.now() - this.#
|
|
70
|
+
this.#timing.headers = performance.now() - this.#created
|
|
69
71
|
this.#statusCode = statusCode
|
|
70
72
|
this.#headers = headers
|
|
71
73
|
|
|
@@ -74,7 +76,7 @@ class Handler extends DecoratorHandler {
|
|
|
74
76
|
|
|
75
77
|
onData(chunk) {
|
|
76
78
|
if (this.#timing.data === -1) {
|
|
77
|
-
this.#timing.data = performance.now() - this.#
|
|
79
|
+
this.#timing.data = performance.now() - this.#created
|
|
78
80
|
}
|
|
79
81
|
|
|
80
82
|
this.#pos += chunk.length
|
|
@@ -83,7 +85,7 @@ class Handler extends DecoratorHandler {
|
|
|
83
85
|
}
|
|
84
86
|
|
|
85
87
|
onComplete() {
|
|
86
|
-
this.#timing.end = performance.now() - this.#
|
|
88
|
+
this.#timing.end = performance.now() - this.#created
|
|
87
89
|
|
|
88
90
|
const data = {
|
|
89
91
|
ureq: this.#opts,
|
|
@@ -109,7 +111,7 @@ class Handler extends DecoratorHandler {
|
|
|
109
111
|
}
|
|
110
112
|
|
|
111
113
|
onError(err) {
|
|
112
|
-
this.#timing.end = performance.now() - this.#
|
|
114
|
+
this.#timing.end = performance.now() - this.#created
|
|
113
115
|
|
|
114
116
|
const data = {
|
|
115
117
|
ures: {
|
|
@@ -1,29 +1,14 @@
|
|
|
1
|
-
export default () => (dispatch) => (opts, handler) => {
|
|
1
|
+
export default () => (dispatch) => async (opts, handler) => {
|
|
2
2
|
const lookup = opts.lookup
|
|
3
3
|
|
|
4
4
|
if (!lookup) {
|
|
5
5
|
return dispatch(opts, handler)
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} else if (!origin) {
|
|
12
|
-
handler.onError(new Error('invalid origin: ' + origin))
|
|
13
|
-
} else {
|
|
14
|
-
dispatch({ ...opts, origin }, handler)
|
|
15
|
-
}
|
|
8
|
+
const origin = await lookup(opts.origin, { signal: opts.signal ?? undefined })
|
|
9
|
+
if (!origin) {
|
|
10
|
+
throw new Error('invalid origin: ' + origin)
|
|
16
11
|
}
|
|
17
12
|
|
|
18
|
-
|
|
19
|
-
const thenable = lookup(opts.origin, { signal: opts.signal ?? undefined }, callback)
|
|
20
|
-
if (typeof thenable?.then === 'function') {
|
|
21
|
-
thenable.then(
|
|
22
|
-
(val) => callback(null, val),
|
|
23
|
-
(err) => callback(err),
|
|
24
|
-
)
|
|
25
|
-
}
|
|
26
|
-
} catch (err) {
|
|
27
|
-
callback(err)
|
|
28
|
-
}
|
|
13
|
+
return dispatch({ ...opts, origin }, handler)
|
|
29
14
|
}
|
|
@@ -143,7 +143,11 @@ class Handler extends DecoratorHandler {
|
|
|
143
143
|
|
|
144
144
|
this.#location = null
|
|
145
145
|
|
|
146
|
-
|
|
146
|
+
try {
|
|
147
|
+
this.#dispatch(this.#opts, this)?.catch((err) => this.onError(err))
|
|
148
|
+
} catch (err) {
|
|
149
|
+
this.onError(err)
|
|
150
|
+
}
|
|
147
151
|
} else {
|
|
148
152
|
super.onComplete(trailers)
|
|
149
153
|
}
|
|
@@ -9,12 +9,5 @@ export default () => (dispatch) => (opts, handler) => {
|
|
|
9
9
|
return dispatch({ ...opts, body }, handler)
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
body.then(
|
|
13
|
-
(body) => {
|
|
14
|
-
dispatch({ ...opts, body }, handler)
|
|
15
|
-
},
|
|
16
|
-
(err) => {
|
|
17
|
-
handler.onError(err)
|
|
18
|
-
},
|
|
19
|
-
)
|
|
12
|
+
return body.then((body) => dispatch({ ...opts, body }, handler))
|
|
20
13
|
}
|
|
@@ -280,7 +280,7 @@ class Handler extends DecoratorHandler {
|
|
|
280
280
|
this.#retryCount++
|
|
281
281
|
this.#retryError = err
|
|
282
282
|
|
|
283
|
-
this.#dispatch(this.#opts, this)
|
|
283
|
+
return this.#dispatch(this.#opts, this)
|
|
284
284
|
} else {
|
|
285
285
|
assert(Number.isFinite(this.#pos))
|
|
286
286
|
assert(this.#end == null || (Number.isFinite(this.#end) && this.#end > 0))
|
|
@@ -292,7 +292,7 @@ class Handler extends DecoratorHandler {
|
|
|
292
292
|
this.#retryCount++
|
|
293
293
|
this.#retryError = err
|
|
294
294
|
|
|
295
|
-
this.#dispatch(this.#opts, this)
|
|
295
|
+
return this.#dispatch(this.#opts, this)
|
|
296
296
|
}
|
|
297
297
|
})
|
|
298
298
|
.catch((err) => {
|
package/lib/request.js
CHANGED
|
@@ -190,5 +190,7 @@ export function request(dispatch, url, opts) {
|
|
|
190
190
|
body: isStream(opts?.body) && opts.body.closed ? null : opts?.body,
|
|
191
191
|
}
|
|
192
192
|
|
|
193
|
-
return new Promise((resolve) =>
|
|
193
|
+
return new Promise((resolve, reject) =>
|
|
194
|
+
dispatch(opts, new RequestHandler(opts, resolve))?.catch(reject),
|
|
195
|
+
)
|
|
194
196
|
}
|