@nxtedition/nxt-undici 1.2.0 → 1.2.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
@@ -127,7 +127,7 @@ class Readable extends stream.Readable {
127
127
 
128
128
  const dispatchers = {
129
129
  requestBody: require('./interceptor/request-body.js'),
130
- dump: require('./interceptor/dump.js'),
130
+ abort: require('./interceptor/abort.js'),
131
131
  catch: require('./interceptor/catch.js'),
132
132
  responseContent: require('./interceptor/response-content.js'),
133
133
  requestContent: require('./interceptor/request-content.js'),
@@ -143,27 +143,40 @@ const dispatchers = {
143
143
  }
144
144
 
145
145
  async function request(url, opts) {
146
+ // TODO (fix): More argument validation...
147
+
146
148
  if (typeof url === 'string') {
147
149
  url = new URL(url)
148
150
  } else if (url instanceof URL) {
149
151
  // Do nothing...
150
152
  } else if (typeof url.origin === 'string' && typeof (url.path ?? url.pathname) === 'string') {
151
153
  // Do nothing...
152
- } else {
153
- throw new Error('missing url')
154
154
  }
155
155
 
156
156
  if (opts == null && typeof url === 'object' && url != null) {
157
157
  opts = url
158
158
  }
159
159
 
160
+ if (url) {
161
+ // Do nothing...
162
+ } else if (typeof opts.url === 'string') {
163
+ url = new URL(opts.url)
164
+ } else if (url.url instanceof URL) {
165
+ url = opts.url
166
+ } else if (typeof opts.origin === 'string' && typeof (opts.path ?? opts.pathname) === 'string') {
167
+ url = opts
168
+ } else {
169
+ throw new Error('missing url')
170
+ }
171
+
160
172
  const method = opts.method ?? (opts.body ? 'POST' : 'GET')
161
173
  const idempotent = opts.idempotent ?? (method === 'GET' || method === 'HEAD')
162
174
 
163
175
  let headers
164
176
  if (Array.isArray(opts.headers)) {
165
177
  headers = parseHeaders(opts.headers)
166
- } else {
178
+ } else if (opts.headers != null) {
179
+ // TODO (fix): Object.values(opts.headers)?
167
180
  headers = opts.headers
168
181
  }
169
182
 
@@ -204,7 +217,7 @@ async function request(url, opts) {
204
217
  dispatch = (opts, handler) => dispatcher.dispatch(opts, handler)
205
218
  dispatch = dispatchers.catch(dispatch)
206
219
  dispatch = dispatchers.requestBody(dispatch)
207
- dispatch = dispatchers.dump(dispatch)
220
+ dispatch = dispatchers.abort(dispatch)
208
221
  dispatch = dispatchers.requestId(dispatch)
209
222
  dispatch = dispatchers.log(dispatch)
210
223
  dispatch = dispatchers.responseRetry(dispatch)
@@ -235,7 +248,6 @@ async function request(url, opts) {
235
248
  headersTimeout: opts.headersTimeout,
236
249
  bodyTimeout: opts.bodyTimeout,
237
250
  idempotent,
238
- dump: opts.dump ?? 'abort',
239
251
  signal: opts.signal,
240
252
  retry: opts.retry ?? 8,
241
253
  proxy: opts.proxy,
@@ -1,25 +1,17 @@
1
1
  const { AbortError } = require('../utils')
2
2
 
3
- // TODO (fix): Configurable timeout
4
- // TODO (fix): Dump?
5
-
6
3
  class Handler {
7
4
  constructor(opts, { handler }) {
8
5
  this.opts = opts
9
6
  this.handler = handler
10
7
  this.pos = 0
11
8
  this.reason = null
12
- this.timeout = null
13
9
  }
14
10
 
15
11
  onConnect(abort) {
16
12
  this.abort = abort
17
13
  this.handler.onConnect((reason) => {
18
14
  this.reason = reason ?? new AbortError()
19
- this.timeout = setTimeout(() => {
20
- this.timeout = null
21
- this.abort(this.reason)
22
- }, this.opts.dump?.timeout ?? 10e3)
23
15
  })
24
16
  }
25
17
 
@@ -73,14 +65,8 @@ class Handler {
73
65
  }
74
66
 
75
67
  onFinally(err, rawTrailers) {
76
- if (this.timeout) {
77
- clearTimeout(this.timeout)
78
- this.timeout = null
79
- }
80
-
81
68
  return err == null ? this.handler.onComplete(rawTrailers) : this.handler.onError(err)
82
69
  }
83
70
  }
84
71
 
85
- module.exports = (dispatch) => (opts, handler) =>
86
- opts.dump ? dispatch(opts, new Handler(opts, { handler })) : dispatch(opts, handler)
72
+ module.exports = (dispatch) => (opts, handler) => dispatch(opts, new Handler(opts, { handler }))
@@ -42,7 +42,7 @@ class Handler {
42
42
  this.logger.debug(
43
43
  {
44
44
  ures: { statusCode, headers: parseHeaders(rawHeaders) },
45
- elapsedTime: this.startTime - performance.now(),
45
+ elapsedTime: performance.now() - this.startTime,
46
46
  },
47
47
  'upstream request response',
48
48
  )
@@ -56,7 +56,7 @@ class Handler {
56
56
 
57
57
  onComplete(rawTrailers) {
58
58
  this.logger.debug(
59
- { bytesRead: this.pos, elapsedTime: this.startTime - performance.now() },
59
+ { bytesRead: this.pos, elapsedTime: performance.now() - this.startTime },
60
60
  'upstream request completed',
61
61
  )
62
62
  return this.handler.onComplete(rawTrailers)
@@ -65,12 +65,12 @@ class Handler {
65
65
  onError(err) {
66
66
  if (this.aborted) {
67
67
  this.logger.debug(
68
- { bytesRead: this.pos, elapsedTime: this.startTime - performance.now(), err },
68
+ { bytesRead: this.pos, elapsedTime: performance.now() - this.startTime, err },
69
69
  'upstream request aborted',
70
70
  )
71
71
  } else {
72
72
  this.logger.error(
73
- { bytesRead: this.pos, elapsedTime: this.startTime - performance.now(), err },
73
+ { bytesRead: this.pos, elapsedTime: performance.now() - this.startTime, err },
74
74
  'upstream request failed',
75
75
  )
76
76
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",