@nxtedition/nxt-undici 4.2.18 → 4.2.20

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
@@ -13,7 +13,6 @@ export const interceptors = {
13
13
  proxy: (await import('./interceptor/proxy.js')).default,
14
14
  cache: (await import('./interceptor/cache.js')).default,
15
15
  requestId: (await import('./interceptor/request-id.js')).default,
16
- dns: (await import('./interceptor/dns.js')).default,
17
16
  lookup: (await import('./interceptor/lookup.js')).default,
18
17
  }
19
18
 
@@ -31,7 +30,6 @@ function wrapDispatcher(dispatcher) {
31
30
  interceptors.responseError(),
32
31
  interceptors.requestBodyFactory(),
33
32
  interceptors.log(),
34
- interceptors.dns(),
35
33
  interceptors.lookup(),
36
34
  interceptors.requestId(),
37
35
  interceptors.responseRetry(),
@@ -59,7 +57,7 @@ function wrapDispatcher(dispatcher) {
59
57
  query: opts.query,
60
58
  headers,
61
59
  signal: opts.signal,
62
- reset: opts.reset ?? null,
60
+ reset: opts.reset ?? false,
63
61
  blocking: opts.blocking ?? null,
64
62
  headersTimeout: opts.headersTimeout,
65
63
  bodyTimeout: opts.bodyTimeout,
@@ -72,7 +70,6 @@ function wrapDispatcher(dispatcher) {
72
70
  error: opts.error ?? true,
73
71
  verify: opts.verify ?? true,
74
72
  logger: opts.logger ?? null,
75
- dns: opts.dns ?? false,
76
73
  connect: opts.connect,
77
74
  lookup: opts.lookup ?? defaultLookup,
78
75
  maxRedirections: 0, // TODO (fix): Ugly hack to disable undici redirections.
@@ -91,14 +91,41 @@ class Handler extends DecoratorHandler {
91
91
  }
92
92
 
93
93
  #decorateError(err) {
94
- return Object.assign(err, {
95
- url: new URL(this.#opts.path, this.#opts.origin).href,
96
- headers: this.#headers,
97
- reason: this.#body?.reason,
98
- code: this.#body?.code,
99
- error: this.#body?.error,
100
- body: typeof this.#body !== 'string' || this.#body.length < 1024 ? this.#body : undefined,
101
- })
94
+ try {
95
+ err.url ??= new URL(this.#opts.path, this.#opts.origin).href
96
+
97
+ err.req = {
98
+ method: this.#opts?.method,
99
+ headers: this.#opts?.headers,
100
+ body:
101
+ // TODO (fix): JSON.stringify POJO
102
+ typeof this.#opts?.body !== 'string' || this.#opts.body.length > 1024
103
+ ? undefined
104
+ : this.#opts.body,
105
+ }
106
+
107
+ err.res = {
108
+ headers: this.#headers,
109
+ // TODO (fix): JSON.stringify POJO
110
+ body: typeof this.#body !== 'string' || this.#body.length < 1024 ? undefined : this.#body,
111
+ }
112
+
113
+ if (this.#body) {
114
+ if (this.#body.reason != null) {
115
+ err.reason ??= this.#body.reason
116
+ }
117
+ if (this.#body.code != null) {
118
+ err.code ??= this.#body.code
119
+ }
120
+ if (this.#body.error != null) {
121
+ err.error ??= this.#body.errorq
122
+ }
123
+ }
124
+
125
+ return err
126
+ } catch {
127
+ return err
128
+ }
102
129
  }
103
130
  }
104
131
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "4.2.18",
3
+ "version": "4.2.20",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",
@@ -1,103 +0,0 @@
1
- import assert from 'node:assert'
2
- import net from 'node:net'
3
- import dns from 'node:dns'
4
- import { DecoratorHandler } from '../utils.js'
5
-
6
- const DEFAULT_RESOLVER = {
7
- lookup(hostname, options, callback) {
8
- dns.lookup(hostname, options, callback)
9
- },
10
- }
11
-
12
- class Handler extends DecoratorHandler {
13
- #handler
14
- #resolver
15
- #key
16
-
17
- constructor({ resolver, key }, { handler }) {
18
- super(handler)
19
-
20
- this.#handler = handler
21
- this.#resolver = resolver
22
- this.#key = key
23
- }
24
-
25
- onError(err) {
26
- if (
27
- err.code &&
28
- [
29
- 'ECONNRESET',
30
- 'ECONNREFUSED',
31
- 'ENOTFOUND',
32
- 'ENETDOWN',
33
- 'ENETUNREACH',
34
- 'EHOSTDOWN',
35
- 'EHOSTUNREACH',
36
- 'EHOSTNOTFOUND',
37
- 'ENODATA',
38
- 'EPIPE',
39
- ].includes(err.code)
40
- ) {
41
- this.#resolver.clear(this.#key)
42
- }
43
-
44
- return this.#handler.onError(err)
45
- }
46
- }
47
-
48
- export default (interceptorOpts) => (dispatch) => (opts, handler) => {
49
- const dns = opts.dns
50
-
51
- if (!dns) {
52
- return dispatch(opts, handler)
53
- }
54
-
55
- const {
56
- resolver = interceptorOpts?.resolver ?? DEFAULT_RESOLVER,
57
- family = interceptorOpts?.family,
58
- hints = interceptorOpts?.hints,
59
- order = interceptorOpts?.order ?? 'ipv4first',
60
- all = interceptorOpts?.all ?? true,
61
- } = dns
62
-
63
- assert(typeof resolver.lookup === 'function')
64
-
65
- const { hostname, host } = new URL(opts.origin)
66
-
67
- if (net.isIP(hostname) || opts.headers?.host) {
68
- return dispatch(opts, handler)
69
- }
70
-
71
- const callback = (err, val) => {
72
- if (err) {
73
- handler.onError(err)
74
- } else {
75
- const url = new URL(opts.origin)
76
- url.hostname = Array.isArray(val)
77
- ? val[Math.floor(val.length * Math.random())].address
78
- : (val?.address ?? val)
79
- dispatch(
80
- {
81
- ...opts,
82
- origin: url.origin,
83
- headers: { ...opts.headers, host },
84
- },
85
- resolver.clear ? new Handler({ resolver, key: hostname }, { handler }) : handler,
86
- )
87
- }
88
- }
89
-
90
- try {
91
- const thenable = resolver.lookup(hostname, { family, hints, order, all }, callback)
92
- if (typeof thenable?.then === 'function') {
93
- thenable.then(
94
- (val) => callback(null, val),
95
- (err) => callback(err),
96
- )
97
- }
98
- } catch (err) {
99
- handler.onError(err)
100
- }
101
-
102
- return true
103
- }