@nxtedition/nxt-undici 5.1.1 → 5.1.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/index.js +2 -4
- package/lib/interceptor/log.js +13 -13
- package/lib/request.js +28 -21
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -72,13 +72,11 @@ function wrapDispatch(dispatcher) {
|
|
|
72
72
|
headers['user-agent'] = userAgent
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
const url = opts.url ? new URL(opts.url) : null
|
|
76
|
-
|
|
77
75
|
return dispatch(
|
|
78
76
|
{
|
|
79
77
|
id: opts.id,
|
|
80
|
-
origin: opts.origin
|
|
81
|
-
path: opts.path
|
|
78
|
+
origin: opts.origin,
|
|
79
|
+
path: opts.path,
|
|
82
80
|
method: opts.method ?? (opts.body ? 'POST' : 'GET'),
|
|
83
81
|
body: opts.body,
|
|
84
82
|
query: opts.query,
|
package/lib/interceptor/log.js
CHANGED
|
@@ -17,12 +17,15 @@ class Handler extends DecoratorHandler {
|
|
|
17
17
|
end: -1,
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
#statusCode
|
|
21
|
+
#headers
|
|
22
|
+
|
|
20
23
|
constructor(opts, { handler }) {
|
|
21
24
|
super(handler)
|
|
22
25
|
|
|
23
26
|
this.#handler = handler
|
|
24
27
|
this.#opts = opts
|
|
25
|
-
this.#logger = opts.logger
|
|
28
|
+
this.#logger = opts.logger.#logger.child({ ureq: opts })
|
|
26
29
|
|
|
27
30
|
this.#created = performance.now()
|
|
28
31
|
}
|
|
@@ -36,7 +39,6 @@ class Handler extends DecoratorHandler {
|
|
|
36
39
|
this.#timing.data = -1
|
|
37
40
|
this.#timing.end = -1
|
|
38
41
|
|
|
39
|
-
this.#logger = this.#logger.child({ ureq: this.#opts })
|
|
40
42
|
this.#logger.debug('upstream request started')
|
|
41
43
|
|
|
42
44
|
return this.#handler.onConnect((reason) => {
|
|
@@ -50,7 +52,6 @@ class Handler extends DecoratorHandler {
|
|
|
50
52
|
|
|
51
53
|
this.#logger.debug(
|
|
52
54
|
{
|
|
53
|
-
ureq: { id: this.#opts.id, url: String(this.#opts.url) },
|
|
54
55
|
ures: { statusCode, headers },
|
|
55
56
|
elapsedTime: this.#timing.headers,
|
|
56
57
|
},
|
|
@@ -67,14 +68,8 @@ class Handler extends DecoratorHandler {
|
|
|
67
68
|
onHeaders(statusCode, rawHeaders, resume, statusMessage, headers = parseHeaders(rawHeaders)) {
|
|
68
69
|
this.#timing.headers = performance.now() - this.#created
|
|
69
70
|
|
|
70
|
-
this.#
|
|
71
|
-
|
|
72
|
-
ureq: { id: this.#opts.id, url: String(this.#opts.url) },
|
|
73
|
-
ures: { statusCode, headers },
|
|
74
|
-
elapsedTime: this.#timing.headers,
|
|
75
|
-
},
|
|
76
|
-
'upstream request response',
|
|
77
|
-
)
|
|
71
|
+
this.#statusCode = statusCode
|
|
72
|
+
this.#headers = headers
|
|
78
73
|
|
|
79
74
|
return this.#handler.onHeaders(statusCode, null, resume, statusMessage, headers)
|
|
80
75
|
}
|
|
@@ -95,9 +90,11 @@ class Handler extends DecoratorHandler {
|
|
|
95
90
|
this.#logger.debug(
|
|
96
91
|
{
|
|
97
92
|
ures: {
|
|
93
|
+
statusCode: this.#statusCode,
|
|
94
|
+
headers: this.#headers,
|
|
95
|
+
timing: this.#timing,
|
|
98
96
|
bytesRead: this.#pos,
|
|
99
97
|
bytesReadPerSecond: (this.#pos * 1e3) / (this.#timing.end - this.#timing.data),
|
|
100
|
-
timing: this.#timing,
|
|
101
98
|
},
|
|
102
99
|
elapsedTime: this.#timing.end,
|
|
103
100
|
},
|
|
@@ -111,10 +108,13 @@ class Handler extends DecoratorHandler {
|
|
|
111
108
|
this.#timing.end = performance.now() - this.#created
|
|
112
109
|
|
|
113
110
|
const data = {
|
|
111
|
+
ureq: this.#opts,
|
|
114
112
|
ures: {
|
|
113
|
+
statusCode: this.#statusCode,
|
|
114
|
+
headers: this.#headers,
|
|
115
|
+
timing: this.#timing,
|
|
115
116
|
bytesRead: this.#pos,
|
|
116
117
|
bytesReadPerSecond: (this.#pos * 1e3) / (this.#timing.end - this.#timing.data),
|
|
117
|
-
timing: this.#timing,
|
|
118
118
|
},
|
|
119
119
|
elapsedTime: this.#timing.end,
|
|
120
120
|
err,
|
package/lib/request.js
CHANGED
|
@@ -6,13 +6,7 @@ import { BodyReadable as Readable } from './readable.js'
|
|
|
6
6
|
function noop() {}
|
|
7
7
|
|
|
8
8
|
export class RequestHandler {
|
|
9
|
-
constructor(
|
|
10
|
-
if (!opts || typeof opts !== 'object') {
|
|
11
|
-
throw new InvalidArgumentError('invalid opts')
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const { signal, method, body, highWaterMark } = opts
|
|
15
|
-
|
|
9
|
+
constructor({ signal, method, body, highWaterMark }, resolve) {
|
|
16
10
|
try {
|
|
17
11
|
if (typeof resolve !== 'function') {
|
|
18
12
|
throw new InvalidArgumentError('invalid resolve')
|
|
@@ -149,20 +143,33 @@ export class RequestHandler {
|
|
|
149
143
|
}
|
|
150
144
|
|
|
151
145
|
export function request(dispatch, url, opts) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
} else if (url instanceof URL) {
|
|
156
|
-
opts = { url, ...opts }
|
|
157
|
-
} else if (typeof url.origin === 'string' && typeof (url.path ?? url.pathname) === 'string') {
|
|
158
|
-
opts = opts ? { ...url, ...opts } : url
|
|
159
|
-
}
|
|
146
|
+
if (!url || (typeof url !== 'string' && typeof url !== 'object' && !(url instanceof URL))) {
|
|
147
|
+
throw new InvalidArgumentError('invalid url')
|
|
148
|
+
}
|
|
160
149
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
150
|
+
if (opts != null && typeof opts !== 'object') {
|
|
151
|
+
throw new InvalidArgumentError('invalid opts')
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (typeof url === 'string') {
|
|
155
|
+
url = new URL(url)
|
|
156
|
+
} else if (typeof url === 'object' && url != null && opts == null) {
|
|
157
|
+
opts = url
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
let origin = url.origin
|
|
161
|
+
if (!origin) {
|
|
162
|
+
const protocol = url.protocol ?? 'http:'
|
|
163
|
+
const host = url.host ?? `${url.hostname}:${url.port ?? (protocol === 'https:' ? 443 : 80)}`
|
|
164
|
+
origin = `${protocol}//${host}`
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
let path = url.path
|
|
168
|
+
if (!path) {
|
|
169
|
+
path = url.search ? `${url.pathname}${url.search}` : url.pathname
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
opts = { ...opts, origin, path }
|
|
165
173
|
|
|
166
|
-
|
|
167
|
-
})
|
|
174
|
+
return new Promise((resolve) => dispatch(opts, new RequestHandler(opts, resolve)))
|
|
168
175
|
}
|