@nxtedition/nxt-undici 3.3.1 → 3.3.3
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/cache.js
CHANGED
|
@@ -193,7 +193,9 @@ export default (opts) => (dispatch) => (opts, handler) => {
|
|
|
193
193
|
} catch (err) {
|
|
194
194
|
handler.onError(err)
|
|
195
195
|
}
|
|
196
|
+
|
|
197
|
+
return true
|
|
196
198
|
} else {
|
|
197
|
-
dispatch(opts, new CacheHandler({ handler, store, key: makeKey(opts) }))
|
|
199
|
+
return dispatch(opts, new CacheHandler({ handler, store, key: makeKey(opts) }))
|
|
198
200
|
}
|
|
199
201
|
}
|
package/lib/interceptor/dns.js
CHANGED
|
@@ -60,35 +60,37 @@ export default (interceptorOpts) => (dispatch) => (opts, handler) => {
|
|
|
60
60
|
const { hostname } = new URL(opts.origin)
|
|
61
61
|
|
|
62
62
|
if (net.isIP(hostname)) {
|
|
63
|
-
dispatch(opts, handler)
|
|
64
|
-
}
|
|
65
|
-
const callback = (err, val) => {
|
|
66
|
-
if (err) {
|
|
67
|
-
handler.onConnect(() => {})
|
|
68
|
-
handler.onError(err)
|
|
69
|
-
} else {
|
|
70
|
-
const url = new URL(opts.origin)
|
|
71
|
-
url.hostname = Array.isArray(val)
|
|
72
|
-
? val[Math.floor(val.length * Math.random())].address
|
|
73
|
-
: val?.address ?? val
|
|
74
|
-
dispatch(
|
|
75
|
-
{ ...opts, origin: url.origin },
|
|
76
|
-
resolver.clear ? new Handler({ resolver, key: hostname }, { handler }) : handler,
|
|
77
|
-
)
|
|
78
|
-
}
|
|
79
|
-
}
|
|
63
|
+
return dispatch(opts, handler)
|
|
64
|
+
}
|
|
80
65
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (typeof thenable?.then === 'function') {
|
|
84
|
-
thenable.then(
|
|
85
|
-
(val) => callback(null, val),
|
|
86
|
-
(err) => callback(err),
|
|
87
|
-
)
|
|
88
|
-
}
|
|
89
|
-
} catch (err) {
|
|
66
|
+
const callback = (err, val) => {
|
|
67
|
+
if (err) {
|
|
90
68
|
handler.onConnect(() => {})
|
|
91
69
|
handler.onError(err)
|
|
70
|
+
} else {
|
|
71
|
+
const url = new URL(opts.origin)
|
|
72
|
+
url.hostname = Array.isArray(val)
|
|
73
|
+
? val[Math.floor(val.length * Math.random())].address
|
|
74
|
+
: val?.address ?? val
|
|
75
|
+
dispatch(
|
|
76
|
+
{ ...opts, origin: url.origin },
|
|
77
|
+
resolver.clear ? new Handler({ resolver, key: hostname }, { handler }) : handler,
|
|
78
|
+
)
|
|
92
79
|
}
|
|
93
80
|
}
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
const thenable = resolver.lookup(hostname, { family, hints, order, all }, callback)
|
|
84
|
+
if (typeof thenable?.then === 'function') {
|
|
85
|
+
thenable.then(
|
|
86
|
+
(val) => callback(null, val),
|
|
87
|
+
(err) => callback(err),
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
} catch (err) {
|
|
91
|
+
handler.onConnect(() => {})
|
|
92
|
+
handler.onError(err)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return true
|
|
94
96
|
}
|
package/lib/interceptor/log.js
CHANGED
|
@@ -7,8 +7,14 @@ class Handler extends DecoratorHandler {
|
|
|
7
7
|
#aborted = false
|
|
8
8
|
#logger
|
|
9
9
|
#pos
|
|
10
|
-
#timing
|
|
11
|
-
|
|
10
|
+
#timing = {
|
|
11
|
+
created: performance.now(),
|
|
12
|
+
connect: -1,
|
|
13
|
+
headers: -1,
|
|
14
|
+
data: -1,
|
|
15
|
+
complete: -1,
|
|
16
|
+
error: -1,
|
|
17
|
+
}
|
|
12
18
|
|
|
13
19
|
constructor(opts, { handler }) {
|
|
14
20
|
super(handler)
|
|
@@ -21,13 +27,7 @@ class Handler extends DecoratorHandler {
|
|
|
21
27
|
onConnect(abort) {
|
|
22
28
|
this.#pos = 0
|
|
23
29
|
this.#abort = abort
|
|
24
|
-
this.#timing =
|
|
25
|
-
connect: performance.now() - this.#startTime,
|
|
26
|
-
headers: -1,
|
|
27
|
-
data: -1,
|
|
28
|
-
complete: -1,
|
|
29
|
-
error: -1,
|
|
30
|
-
}
|
|
30
|
+
this.#timing.connect = performance.now() - this.#timing.created
|
|
31
31
|
|
|
32
32
|
this.#logger.debug({ ureq: this.#opts }, 'upstream request started')
|
|
33
33
|
|
|
@@ -47,7 +47,7 @@ class Handler extends DecoratorHandler {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
onHeaders(statusCode, rawHeaders, resume, statusMessage, headers = parseHeaders(rawHeaders)) {
|
|
50
|
-
this.#timing.headers = performance.now() - this.#timing.connect - this.#
|
|
50
|
+
this.#timing.headers = performance.now() - this.#timing.connect - this.#timing.created
|
|
51
51
|
|
|
52
52
|
this.#logger.debug(
|
|
53
53
|
{
|
|
@@ -62,7 +62,7 @@ class Handler extends DecoratorHandler {
|
|
|
62
62
|
|
|
63
63
|
onData(chunk) {
|
|
64
64
|
if (this.#timing.data === -1) {
|
|
65
|
-
this.#timing.data = performance.now() - this.#timing.headers - this.#
|
|
65
|
+
this.#timing.data = performance.now() - this.#timing.headers - this.#timing.created
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
this.#pos += chunk.length
|
|
@@ -71,7 +71,7 @@ class Handler extends DecoratorHandler {
|
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
onComplete(rawTrailers) {
|
|
74
|
-
this.#timing.complete = performance.now() - this.#timing.data - this.#
|
|
74
|
+
this.#timing.complete = performance.now() - this.#timing.data - this.#timing.created
|
|
75
75
|
|
|
76
76
|
this.#logger.debug(
|
|
77
77
|
{ elapsedTime: this.#timing.complete, bytesRead: this.#pos, timing: this.#timing },
|
|
@@ -82,7 +82,7 @@ class Handler extends DecoratorHandler {
|
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
onError(err) {
|
|
85
|
-
this.#timing.error = performance.now() - this.#timing.data - this.#
|
|
85
|
+
this.#timing.error = performance.now() - this.#timing.data - this.#timing.created
|
|
86
86
|
|
|
87
87
|
if (this.#aborted) {
|
|
88
88
|
this.#logger.debug(
|
|
@@ -16,11 +16,13 @@ export default (opts) => (dispatch) => (opts, handler) => {
|
|
|
16
16
|
handler.onError(err)
|
|
17
17
|
},
|
|
18
18
|
)
|
|
19
|
+
return true
|
|
19
20
|
} else {
|
|
20
|
-
dispatch({ ...opts, body }, handler)
|
|
21
|
+
return dispatch({ ...opts, body }, handler)
|
|
21
22
|
}
|
|
22
23
|
} catch (err) {
|
|
23
24
|
handler.onConnect(() => {})
|
|
24
25
|
handler.onError(err)
|
|
26
|
+
return true
|
|
25
27
|
}
|
|
26
28
|
}
|
package/lib/utils.js
CHANGED
|
@@ -3,6 +3,8 @@ import cacheControlParser from 'cache-control-parser'
|
|
|
3
3
|
import stream from 'node:stream'
|
|
4
4
|
import { util } from '@nxtedition/undici'
|
|
5
5
|
|
|
6
|
+
const noop = () => {}
|
|
7
|
+
|
|
6
8
|
export function parseCacheControl(str) {
|
|
7
9
|
return str ? cacheControlParser.parse(str) : null
|
|
8
10
|
}
|
|
@@ -250,6 +252,7 @@ export function bodyLength(body) {
|
|
|
250
252
|
|
|
251
253
|
export class DecoratorHandler {
|
|
252
254
|
#handler
|
|
255
|
+
#onConnectCalled = false
|
|
253
256
|
|
|
254
257
|
constructor(handler) {
|
|
255
258
|
if (typeof handler !== 'object' || handler === null) {
|
|
@@ -259,10 +262,15 @@ export class DecoratorHandler {
|
|
|
259
262
|
}
|
|
260
263
|
|
|
261
264
|
onConnect(...args) {
|
|
265
|
+
this.#onConnectCalled = true
|
|
262
266
|
return this.#handler.onConnect?.(...args)
|
|
263
267
|
}
|
|
264
268
|
|
|
265
269
|
onError(...args) {
|
|
270
|
+
if (!this.#onConnectCalled) {
|
|
271
|
+
this.#onConnectCalled = true
|
|
272
|
+
this.#handler.onConnect?.(noop)
|
|
273
|
+
}
|
|
266
274
|
return this.#handler.onError?.(...args)
|
|
267
275
|
}
|
|
268
276
|
|