@nxtedition/nxt-undici 5.1.0 → 5.1.2

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
@@ -15,7 +15,6 @@ export const interceptors = {
15
15
  cache: (await import('./interceptor/cache.js')).default,
16
16
  requestId: (await import('./interceptor/request-id.js')).default,
17
17
  dns: (await import('./interceptor/dns.js')).default,
18
- lookup: (await import('./interceptor/lookup.js')).default,
19
18
  }
20
19
 
21
20
  export { parseHeaders } from './utils.js'
@@ -59,7 +58,6 @@ function wrapDispatch(dispatcher) {
59
58
  interceptors.requestBodyFactory(),
60
59
  interceptors.log(),
61
60
  interceptors.dns(),
62
- interceptors.lookup(),
63
61
  interceptors.requestId(),
64
62
  interceptors.responseRetry(),
65
63
  interceptors.responseVerify(),
@@ -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
@@ -6,13 +6,7 @@ import { BodyReadable as Readable } from './readable.js'
6
6
  function noop() {}
7
7
 
8
8
  export class RequestHandler {
9
- constructor(opts, resolve) {
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,19 +143,34 @@ export class RequestHandler {
149
143
  }
150
144
 
151
145
  export function request(dispatch, url, opts) {
152
- return new Promise((resolve) => {
153
- if (typeof url === 'string') {
154
- opts = { url: new URL(url), ...opts }
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
- if (opts == null && typeof url === 'object' && url != null) {
162
- opts = url
163
- }
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
+
162
+ if (!origin) {
163
+ const protocol = url.protocol ?? 'http:'
164
+ const host = url.host ?? `${url.hostname}:${url.port ?? (protocol === 'https:' ? 443 : 80)}`
165
+ origin = `${protocol}//${host}`
166
+ }
167
+
168
+ let path = url.path
169
+ if (!path) {
170
+ path = url.search ? `${url.pathname}${url.search}` : url
171
+ }
172
+
173
+ opts = { ...opts, origin, path }
164
174
 
165
- dispatch(opts, new RequestHandler(opts, resolve))
166
- })
175
+ return new Promise((resolve) => dispatch(opts, new RequestHandler(opts, resolve)))
167
176
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "5.1.0",
3
+ "version": "5.1.2",
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
- }