@nxtedition/nxt-undici 5.0.3 → 5.1.1

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
@@ -14,7 +14,7 @@ export const interceptors = {
14
14
  proxy: (await import('./interceptor/proxy.js')).default,
15
15
  cache: (await import('./interceptor/cache.js')).default,
16
16
  requestId: (await import('./interceptor/request-id.js')).default,
17
- lookup: (await import('./interceptor/lookup.js')).default,
17
+ dns: (await import('./interceptor/dns.js')).default,
18
18
  }
19
19
 
20
20
  export { parseHeaders } from './utils.js'
@@ -57,7 +57,7 @@ function wrapDispatch(dispatcher) {
57
57
  interceptors.responseError(),
58
58
  interceptors.requestBodyFactory(),
59
59
  interceptors.log(),
60
- interceptors.lookup(),
60
+ interceptors.dns(),
61
61
  interceptors.requestId(),
62
62
  interceptors.responseRetry(),
63
63
  interceptors.responseVerify(),
@@ -97,6 +97,7 @@ function wrapDispatch(dispatcher) {
97
97
  error: opts.error ?? true,
98
98
  verify: opts.verify ?? true,
99
99
  logger: opts.logger ?? null,
100
+ dns: opts.dns ?? true,
100
101
  connect: opts.connect,
101
102
  lookup: opts.lookup ?? defaultLookup,
102
103
  maxRedirections: 0, // TODO (fix): Ugly hack to disable undici redirections.
@@ -0,0 +1,22 @@
1
+ import net from 'node:net'
2
+ import { resolve4 } from 'node:dns/promises'
3
+
4
+ export default () => (dispatch) => {
5
+ return async (opts, handler) => {
6
+ if (!opts.dns) {
7
+ return dispatch(opts, handler)
8
+ }
9
+
10
+ const origin = new URL(opts.origin)
11
+
12
+ if (net.isIP(origin.hostname)) {
13
+ return dispatch(opts, handler)
14
+ }
15
+
16
+ const host = origin.host
17
+ const records = await resolve4(origin.hostname)
18
+ origin.hostname = records[Math.floor(Math.random() * records.length)]
19
+
20
+ return dispatch({ ...opts, origin, headers: { ...opts.headers, host } }, handler)
21
+ }
22
+ }
@@ -45,13 +45,23 @@ class Handler extends DecoratorHandler {
45
45
  })
46
46
  }
47
47
 
48
- onUpgrade(statusCode, rawHeaders, socket, headers) {
49
- this.#logger.debug('upstream request upgraded')
48
+ onUpgrade(statusCode, rawHeaders, socket, headers = parseHeaders(rawHeaders)) {
49
+ this.#timing.headers = performance.now() - this.#created
50
+
51
+ this.#logger.debug(
52
+ {
53
+ ureq: { id: this.#opts.id, url: String(this.#opts.url) },
54
+ ures: { statusCode, headers },
55
+ elapsedTime: this.#timing.headers,
56
+ },
57
+ 'upstream request upgraded',
58
+ )
59
+
50
60
  socket.on('close', () => {
51
61
  this.#logger.debug('upstream request socket closed')
52
62
  })
53
63
 
54
- return this.#handler.onUpgrade(statusCode, rawHeaders, socket, headers)
64
+ return this.#handler.onUpgrade(statusCode, null, socket, headers)
55
65
  }
56
66
 
57
67
  onHeaders(statusCode, rawHeaders, resume, statusMessage, headers = parseHeaders(rawHeaders)) {
@@ -13,12 +13,12 @@ class Handler extends DecoratorHandler {
13
13
  this.#opts = proxyOpts
14
14
  }
15
15
 
16
- onUpgrade(statusCode, rawHeaders, socket) {
16
+ onUpgrade(statusCode, rawHeaders, socket, headers = parseHeaders(rawHeaders)) {
17
17
  return this.#handler.onUpgrade(
18
18
  statusCode,
19
19
  reduceHeaders(
20
20
  {
21
- headers: rawHeaders,
21
+ headers,
22
22
  httpVersion: this.#opts.httpVersion ?? this.#opts.req?.httpVersion,
23
23
  socket: this.#opts.socket,
24
24
  proxyName: this.#opts.name,
@@ -121,7 +121,7 @@ class Handler extends DecoratorHandler {
121
121
  assert(Number.isFinite(this.#pos))
122
122
  assert(this.#end == null || Number.isFinite(this.#end))
123
123
 
124
- return this.#onHeaders(statusCode, rawHeaders, resume, statusMessage, headers)
124
+ return this.#onHeaders(statusCode, null, resume, statusMessage, headers)
125
125
  } else if (statusCode === 206 || (this.#pos === 0 && statusCode === 200)) {
126
126
  assert(this.#etag != null || !this.#pos)
127
127
 
@@ -82,6 +82,6 @@ class Handler extends DecoratorHandler {
82
82
  }
83
83
 
84
84
  export default () => (dispatch) => (opts, handler) =>
85
- !opts.upgrade && opts.verify !== false
85
+ !opts.upgrade && opts.verify !== false && opts.method !== 'HEAD'
86
86
  ? dispatch(opts, new Handler(opts, { handler }))
87
87
  : dispatch(opts, handler)
package/lib/request.js CHANGED
@@ -159,7 +159,8 @@ export function request(dispatch, url, opts) {
159
159
  }
160
160
 
161
161
  if (opts == null && typeof url === 'object' && url != null) {
162
- opts = url
162
+ url.protocol ??= 'http:'
163
+ opts = { protocol: 'http:', ...url }
163
164
  }
164
165
 
165
166
  dispatch(opts, new RequestHandler(opts, resolve))
package/lib/utils.js CHANGED
@@ -263,10 +263,6 @@ export class DecoratorHandler {
263
263
  return this.#handler.onUpgrade?.(...args)
264
264
  }
265
265
 
266
- onResponseStarted(...args) {
267
- return this.#handler.onResponseStarted?.(...args)
268
- }
269
-
270
266
  onHeaders(...args) {
271
267
  return this.#handler.onHeaders?.(...args)
272
268
  }
@@ -278,10 +274,6 @@ export class DecoratorHandler {
278
274
  onComplete(...args) {
279
275
  return this.#handler.onComplete?.(...args)
280
276
  }
281
-
282
- onBodySent(...args) {
283
- return this.#handler.onBodySent?.(...args)
284
- }
285
277
  }
286
278
 
287
279
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "5.0.3",
3
+ "version": "5.1.1",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",
@@ -12,7 +12,7 @@
12
12
  "cache-control-parser": "^2.0.6",
13
13
  "http-errors": "^2.0.0",
14
14
  "lru-cache": "^11.0.2",
15
- "undici": "^7.0.0"
15
+ "undici": "^6.0.0"
16
16
  },
17
17
  "devDependencies": {
18
18
  "@types/node": "^22.10.1",
@@ -1,29 +0,0 @@
1
- export default () => (dispatch) => (opts, handler) => {
2
- const lookup = opts.lookup
3
-
4
- if (!lookup) {
5
- return dispatch(opts, handler)
6
- }
7
-
8
- const callback = (err, origin) => {
9
- if (err) {
10
- handler.onError(err)
11
- } else {
12
- dispatch({ ...opts, origin }, handler)
13
- }
14
- }
15
-
16
- try {
17
- const thenable = lookup(opts.origin, { signal: opts.signal }, callback)
18
- if (typeof thenable?.then === 'function') {
19
- thenable.then(
20
- (val) => callback(null, val),
21
- (err) => callback(err),
22
- )
23
- }
24
- } catch (err) {
25
- callback(err)
26
- }
27
-
28
- return true
29
- }