@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.
@@ -78,83 +78,79 @@ export default () => (dispatch) => {
78
78
  return dispatch(opts, handler)
79
79
  }
80
80
 
81
- try {
82
- const { host, hostname } = url
81
+ const { host, hostname } = url
83
82
 
84
- const now = getFastNow()
83
+ const now = getFastNow()
85
84
 
86
- let records = cache.get(hostname)
85
+ let records = cache.get(hostname)
87
86
 
88
- if (records == null || records.every((x) => x.expires < now)) {
89
- const [err, val] = await resolve(hostname, { ttl })
87
+ if (records == null || records.every((x) => x.expires < now)) {
88
+ const [err, val] = await resolve(hostname, { ttl })
90
89
 
91
- if (err) {
92
- throw err
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
- let record
94
+ records = val
95
+ } else if (records.some((x) => x.expires < now + 1e3)) {
96
+ resolve(hostname, { ttl })
97
+ }
101
98
 
102
- if (balance === 'hash') {
103
- HASHER ??= await xxhash()
99
+ let record
104
100
 
105
- const hash = HASHER.h32(url.pathname)
101
+ if (balance === 'hash') {
102
+ HASHER ??= await xxhash()
106
103
 
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
- }
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
- if (record == null) {
117
- records.sort(
118
- (a, b) => a.errored - b.errored || a.pending - b.pending || a.counter - b.counter,
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
- for (let i = 0; i < records.length; i++) {
122
- if (records[i].expires >= now) {
123
- record = records[i]
124
- break
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
- if (!record) {
130
- throw Object.assign(new Error(`No available DNS records found for ${hostname}`), {
131
- code: 'ENOTFOUND',
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
- url.hostname = record.address
134
+ url.hostname = record.address
136
135
 
137
- dispatch(
138
- { ...opts, origin: url.origin, headers: { ...opts.headers, host } },
139
- new Handler(handler, (err, statusCode) => {
140
- record.pending--
136
+ record.counter++
137
+ record.pending++
141
138
 
142
- if (err != null && err.name !== 'AbortError') {
143
- record.expires = 0
144
- } else if (statusCode != null && statusCode >= 500) {
145
- record.errored++
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
- if (err != null || statusCode >= 500) {
149
- record.timeout = getFastNow() + 10e3
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
- record.counter++
155
- record.pending++
156
- } catch (err) {
157
- handler.onError(err)
158
- }
150
+ if (err != null || statusCode >= 500) {
151
+ record.timeout = getFastNow() + 10e3
152
+ }
153
+ }),
154
+ )
159
155
  }
160
156
  }
@@ -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: performance.now(),
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.#timing.created
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.#timing.created
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.#timing.created
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.#timing.created
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.#timing.created
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.#timing.created
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 callback = (err, origin) => {
9
- if (err) {
10
- handler.onError(err)
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
- try {
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
- this.#dispatch(this.#opts, this)
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) => dispatch(opts, new RequestHandler(opts, resolve)))
193
+ return new Promise((resolve, reject) =>
194
+ dispatch(opts, new RequestHandler(opts, resolve))?.catch(reject),
195
+ )
194
196
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "6.4.11",
3
+ "version": "6.4.13",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",