@nxtedition/nxt-undici 4.2.21 → 4.2.23
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 +0 -3
- package/lib/interceptor/log.js +13 -20
- package/package.json +1 -1
- package/lib/interceptor/dns.js +0 -125
package/lib/index.js
CHANGED
|
@@ -13,7 +13,6 @@ export const interceptors = {
|
|
|
13
13
|
proxy: (await import('./interceptor/proxy.js')).default,
|
|
14
14
|
cache: (await import('./interceptor/cache.js')).default,
|
|
15
15
|
requestId: (await import('./interceptor/request-id.js')).default,
|
|
16
|
-
dns: (await import('./interceptor/dns.js')).default,
|
|
17
16
|
lookup: (await import('./interceptor/lookup.js')).default,
|
|
18
17
|
}
|
|
19
18
|
|
|
@@ -31,7 +30,6 @@ function wrapDispatcher(dispatcher) {
|
|
|
31
30
|
interceptors.responseError(),
|
|
32
31
|
interceptors.requestBodyFactory(),
|
|
33
32
|
interceptors.log(),
|
|
34
|
-
interceptors.dns(),
|
|
35
33
|
interceptors.lookup(),
|
|
36
34
|
interceptors.requestId(),
|
|
37
35
|
interceptors.responseRetry(),
|
|
@@ -72,7 +70,6 @@ function wrapDispatcher(dispatcher) {
|
|
|
72
70
|
error: opts.error ?? true,
|
|
73
71
|
verify: opts.verify ?? true,
|
|
74
72
|
logger: opts.logger ?? null,
|
|
75
|
-
dns: opts.dns ?? true,
|
|
76
73
|
connect: opts.connect,
|
|
77
74
|
lookup: opts.lookup ?? defaultLookup,
|
|
78
75
|
maxRedirections: 0, // TODO (fix): Ugly hack to disable undici redirections.
|
package/lib/interceptor/log.js
CHANGED
|
@@ -9,13 +9,12 @@ class Handler extends DecoratorHandler {
|
|
|
9
9
|
#aborted = false
|
|
10
10
|
#pos = 0
|
|
11
11
|
#created = 0
|
|
12
|
-
#now = 0
|
|
13
12
|
#timing = {
|
|
14
13
|
created: -1,
|
|
15
14
|
connect: -1,
|
|
16
15
|
headers: -1,
|
|
17
16
|
data: -1,
|
|
18
|
-
|
|
17
|
+
end: -1,
|
|
19
18
|
}
|
|
20
19
|
|
|
21
20
|
constructor(opts, { handler }) {
|
|
@@ -26,21 +25,19 @@ class Handler extends DecoratorHandler {
|
|
|
26
25
|
this.#logger = opts.logger
|
|
27
26
|
|
|
28
27
|
this.#created = performance.now()
|
|
29
|
-
this.#now += this.#created
|
|
30
28
|
}
|
|
31
29
|
|
|
32
30
|
onConnect(abort) {
|
|
33
31
|
this.#pos = 0
|
|
34
32
|
this.#abort = abort
|
|
35
33
|
|
|
36
|
-
this.#timing.connect = performance.now() - this.#
|
|
37
|
-
this.#now += this.#timing.connect
|
|
38
|
-
|
|
34
|
+
this.#timing.connect = performance.now() - this.#created
|
|
39
35
|
this.#timing.headers = -1
|
|
40
36
|
this.#timing.data = -1
|
|
41
|
-
this.#timing.
|
|
37
|
+
this.#timing.end = -1
|
|
42
38
|
|
|
43
|
-
this.#logger.
|
|
39
|
+
this.#logger = this.#logger.child({ ureq: this.#opts })
|
|
40
|
+
this.#logger.debug('upstream request started')
|
|
44
41
|
|
|
45
42
|
return this.#handler.onConnect((reason) => {
|
|
46
43
|
this.#aborted = true
|
|
@@ -58,8 +55,7 @@ class Handler extends DecoratorHandler {
|
|
|
58
55
|
}
|
|
59
56
|
|
|
60
57
|
onHeaders(statusCode, rawHeaders, resume, statusMessage, headers = parseHeaders(rawHeaders)) {
|
|
61
|
-
this.#timing.headers = performance.now() - this.#
|
|
62
|
-
this.#now += this.#timing.headers
|
|
58
|
+
this.#timing.headers = performance.now() - this.#created
|
|
63
59
|
|
|
64
60
|
this.#logger.debug(
|
|
65
61
|
{
|
|
@@ -75,8 +71,7 @@ class Handler extends DecoratorHandler {
|
|
|
75
71
|
|
|
76
72
|
onData(chunk) {
|
|
77
73
|
if (this.#timing.data === -1) {
|
|
78
|
-
this.#timing.data = performance.now() - this.#
|
|
79
|
-
this.#now += this.#timing.data
|
|
74
|
+
this.#timing.data = performance.now() - this.#created
|
|
80
75
|
}
|
|
81
76
|
|
|
82
77
|
this.#pos += chunk.length
|
|
@@ -85,17 +80,16 @@ class Handler extends DecoratorHandler {
|
|
|
85
80
|
}
|
|
86
81
|
|
|
87
82
|
onComplete(rawTrailers) {
|
|
88
|
-
this.#timing.
|
|
83
|
+
this.#timing.end = performance.now() - this.#created
|
|
89
84
|
|
|
90
85
|
this.#logger.debug(
|
|
91
86
|
{
|
|
92
|
-
ureq: { id: this.#opts.id, url: this.#opts.url },
|
|
93
87
|
ures: {
|
|
94
88
|
bytesRead: this.#pos,
|
|
95
|
-
bytesReadPerSecond: (this.#pos * 1e3) / this.#timing.
|
|
89
|
+
bytesReadPerSecond: (this.#pos * 1e3) / (this.#timing.end - this.#timing.data),
|
|
96
90
|
timing: this.#timing,
|
|
97
91
|
},
|
|
98
|
-
elapsedTime: this.#timing.
|
|
92
|
+
elapsedTime: this.#timing.end,
|
|
99
93
|
},
|
|
100
94
|
'upstream request completed',
|
|
101
95
|
)
|
|
@@ -104,16 +98,15 @@ class Handler extends DecoratorHandler {
|
|
|
104
98
|
}
|
|
105
99
|
|
|
106
100
|
onError(err) {
|
|
107
|
-
this.#timing.
|
|
101
|
+
this.#timing.end = performance.now() - this.#created
|
|
108
102
|
|
|
109
103
|
const data = {
|
|
110
|
-
ureq: this.#opts,
|
|
111
104
|
ures: {
|
|
112
105
|
bytesRead: this.#pos,
|
|
113
|
-
bytesReadPerSecond: (this.#pos * 1e3) / this.#timing.
|
|
106
|
+
bytesReadPerSecond: (this.#pos * 1e3) / (this.#timing.end - this.#timing.data),
|
|
114
107
|
timing: this.#timing,
|
|
115
108
|
},
|
|
116
|
-
elapsedTime: this.#timing.
|
|
109
|
+
elapsedTime: this.#timing.end,
|
|
117
110
|
err,
|
|
118
111
|
}
|
|
119
112
|
|
package/package.json
CHANGED
package/lib/interceptor/dns.js
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import { DecoratorHandler } from '../utils.js'
|
|
2
|
-
import net from 'net'
|
|
3
|
-
import { resolve4 } from 'node:dns/promises'
|
|
4
|
-
|
|
5
|
-
let fastNow = Date.now()
|
|
6
|
-
setInterval(() => {
|
|
7
|
-
fastNow = Date.now()
|
|
8
|
-
}, 500)
|
|
9
|
-
|
|
10
|
-
class Record {
|
|
11
|
-
address = ''
|
|
12
|
-
expires = 0
|
|
13
|
-
errored = 0
|
|
14
|
-
|
|
15
|
-
constructor({ address, ttl }) {
|
|
16
|
-
this.address = address
|
|
17
|
-
this.expires = fastNow + (ttl ?? 60) * 1e3
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
class Handler extends DecoratorHandler {
|
|
22
|
-
#handler
|
|
23
|
-
#record
|
|
24
|
-
|
|
25
|
-
constructor({ record }, { handler }) {
|
|
26
|
-
super(handler)
|
|
27
|
-
|
|
28
|
-
this.#handler = handler
|
|
29
|
-
this.#record = record
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
onError(err) {
|
|
33
|
-
if (
|
|
34
|
-
[
|
|
35
|
-
'ECONNRESET',
|
|
36
|
-
'ECONNREFUSED',
|
|
37
|
-
'ENOTFOUND',
|
|
38
|
-
'ENETDOWN',
|
|
39
|
-
'ENETUNREACH',
|
|
40
|
-
'EHOSTDOWN',
|
|
41
|
-
'EHOSTUNREACH',
|
|
42
|
-
'EHOSTNOTFOUND',
|
|
43
|
-
'ENODATA',
|
|
44
|
-
'EPIPE',
|
|
45
|
-
'UND_ERR_CONNECT_TIMEOUT',
|
|
46
|
-
].includes(err.code) ||
|
|
47
|
-
[503].includes(err.statusCode)
|
|
48
|
-
) {
|
|
49
|
-
this.#record.errored = fastNow
|
|
50
|
-
|
|
51
|
-
// TODO (fix): For how long do we "blacklist" the record?
|
|
52
|
-
|
|
53
|
-
if (err.code === 'UND_ERR_CONNECT_TIMEOUT') {
|
|
54
|
-
// We don't expect this address to ever work again...
|
|
55
|
-
this.#record.expires = Infinity
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return super.onError(err)
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export default (interceptorOpts) => (dispatch) => {
|
|
64
|
-
/** @type {Map<string, Array<Record>>} */
|
|
65
|
-
const dnsCache = new Map()
|
|
66
|
-
|
|
67
|
-
return async (opts, handler) => {
|
|
68
|
-
if (!opts.dns) {
|
|
69
|
-
return dispatch(opts, handler)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const { protocol, port, hostname, host } = new URL(opts.origin)
|
|
73
|
-
|
|
74
|
-
if (net.isIP(hostname) || opts.headers?.host || !port || !protocol) {
|
|
75
|
-
return dispatch(opts, handler)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const now = Date.now()
|
|
79
|
-
try {
|
|
80
|
-
/** @type {Array|undefined} */
|
|
81
|
-
let records = dnsCache.get(hostname)
|
|
82
|
-
|
|
83
|
-
if (!records?.some((record) => record.expires > now && !record.errored)) {
|
|
84
|
-
// TODO (fix): Re-use old records while fetching new ones or if fetching fails?
|
|
85
|
-
// TODO (fix): Background refresh + health checks?
|
|
86
|
-
// TODO (fix): What about old "blacklisted" records?
|
|
87
|
-
// TODO (fix): What about ipv6?
|
|
88
|
-
|
|
89
|
-
records = await resolve4(hostname, { ttl: true })
|
|
90
|
-
records = records.map((record) => new Record(record))
|
|
91
|
-
|
|
92
|
-
if (records.length > 0) {
|
|
93
|
-
// TODO (fix): Clear old hostnames?
|
|
94
|
-
dnsCache.set(hostname, records)
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (records.length === 0) {
|
|
99
|
-
return dispatch(opts, handler)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// TODO (perf): sort + Math.random is a bit naive...
|
|
103
|
-
records.sort((a, b) =>
|
|
104
|
-
a.errored !== b.errored ? a.errored - b.errored : Math.random() - 0.5,
|
|
105
|
-
)
|
|
106
|
-
|
|
107
|
-
const record = records.find((record) => record.expires > now)
|
|
108
|
-
|
|
109
|
-
if (!record) {
|
|
110
|
-
return dispatch(opts, handler)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
return dispatch(
|
|
114
|
-
{
|
|
115
|
-
...opts,
|
|
116
|
-
origin: `${protocol}//${record.address}:${port}`,
|
|
117
|
-
headers: { ...opts.headers, host },
|
|
118
|
-
},
|
|
119
|
-
new Handler({ record }, { handler }),
|
|
120
|
-
)
|
|
121
|
-
} catch (err) {
|
|
122
|
-
handler.onError(err)
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|