@nxtedition/nxt-undici 4.2.21 → 4.2.22

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 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.
@@ -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
- complete: -1,
17
+ end: -1,
19
18
  }
20
19
 
21
20
  constructor(opts, { handler }) {
@@ -26,19 +25,16 @@ 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.#now
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.complete = -1
37
+ this.#timing.end = -1
42
38
 
43
39
  this.#logger.debug({ ureq: this.#opts }, 'upstream request started')
44
40
 
@@ -58,8 +54,7 @@ class Handler extends DecoratorHandler {
58
54
  }
59
55
 
60
56
  onHeaders(statusCode, rawHeaders, resume, statusMessage, headers = parseHeaders(rawHeaders)) {
61
- this.#timing.headers = performance.now() - this.#now
62
- this.#now += this.#timing.headers
57
+ this.#timing.headers = performance.now() - this.#created
63
58
 
64
59
  this.#logger.debug(
65
60
  {
@@ -75,8 +70,7 @@ class Handler extends DecoratorHandler {
75
70
 
76
71
  onData(chunk) {
77
72
  if (this.#timing.data === -1) {
78
- this.#timing.data = performance.now() - this.#now
79
- this.#now += this.#timing.data
73
+ this.#timing.data = performance.now() - this.#created
80
74
  }
81
75
 
82
76
  this.#pos += chunk.length
@@ -85,17 +79,17 @@ class Handler extends DecoratorHandler {
85
79
  }
86
80
 
87
81
  onComplete(rawTrailers) {
88
- this.#timing.complete = performance.now() - this.#created
82
+ this.#timing.end = performance.now() - this.#created
89
83
 
90
84
  this.#logger.debug(
91
85
  {
92
86
  ureq: { id: this.#opts.id, url: this.#opts.url },
93
87
  ures: {
94
88
  bytesRead: this.#pos,
95
- bytesReadPerSecond: (this.#pos * 1e3) / this.#timing.complete,
89
+ bytesReadPerSecond: (this.#pos * 1e3) / (this.#timing.end - this.#timing.data),
96
90
  timing: this.#timing,
97
91
  },
98
- elapsedTime: this.#timing.complete,
92
+ elapsedTime: this.#timing.end,
99
93
  },
100
94
  'upstream request completed',
101
95
  )
@@ -104,16 +98,16 @@ class Handler extends DecoratorHandler {
104
98
  }
105
99
 
106
100
  onError(err) {
107
- this.#timing.complete = performance.now() - this.#created
101
+ this.#timing.end = performance.now() - this.#created
108
102
 
109
103
  const data = {
110
104
  ureq: this.#opts,
111
105
  ures: {
112
106
  bytesRead: this.#pos,
113
- bytesReadPerSecond: (this.#pos * 1e3) / this.#timing.complete,
107
+ bytesReadPerSecond: (this.#pos * 1e3) / (this.#timing.end - this.#timing.data),
114
108
  timing: this.#timing,
115
109
  },
116
- elapsedTime: this.#timing.complete,
110
+ elapsedTime: this.#timing.end,
117
111
  err,
118
112
  }
119
113
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "4.2.21",
3
+ "version": "4.2.22",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",
@@ -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
- }