@nxtedition/nxt-undici 1.2.1 → 1.3.0

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'),
@@ -151,14 +151,24 @@ async function request(url, opts) {
151
151
  // Do nothing...
152
152
  } else if (typeof url.origin === 'string' && typeof (url.path ?? url.pathname) === 'string') {
153
153
  // Do nothing...
154
- } else {
155
- throw new Error('missing url')
156
154
  }
157
155
 
158
156
  if (opts == null && typeof url === 'object' && url != null) {
159
157
  opts = url
160
158
  }
161
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
+
162
172
  const method = opts.method ?? (opts.body ? 'POST' : 'GET')
163
173
  const idempotent = opts.idempotent ?? (method === 'GET' || method === 'HEAD')
164
174
 
@@ -207,7 +217,7 @@ async function request(url, opts) {
207
217
  dispatch = (opts, handler) => dispatcher.dispatch(opts, handler)
208
218
  dispatch = dispatchers.catch(dispatch)
209
219
  dispatch = dispatchers.requestBody(dispatch)
210
- dispatch = dispatchers.dump(dispatch)
220
+ dispatch = dispatchers.abort(dispatch)
211
221
  dispatch = dispatchers.requestId(dispatch)
212
222
  dispatch = dispatchers.log(dispatch)
213
223
  dispatch = dispatchers.responseRetry(dispatch)
@@ -238,7 +248,6 @@ async function request(url, opts) {
238
248
  headersTimeout: opts.headersTimeout,
239
249
  bodyTimeout: opts.bodyTimeout,
240
250
  idempotent,
241
- dump: opts.dump ?? {},
242
251
  signal: opts.signal,
243
252
  retry: opts.retry ?? 8,
244
253
  proxy: opts.proxy,
@@ -1,25 +1,17 @@
1
1
  const { AbortError } = require('../utils')
2
2
 
3
- // TODO (fix): Configrable max body size
4
- // TODO (fix): Dump true?
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 }))
@@ -9,12 +9,20 @@ class Handler {
9
9
  this.aborted = false
10
10
  this.logger = opts.logger.child({ ureq: opts })
11
11
  this.pos = 0
12
- this.startTime = 0
12
+ this.stats = {
13
+ start: -1,
14
+ end: -1,
15
+ headers: -1,
16
+ firstBodySent: -1,
17
+ lastBodySent: -1,
18
+ firstBodyReceived: -1,
19
+ lastBodyReceived: -1,
20
+ }
13
21
  }
14
22
 
15
23
  onConnect(abort) {
16
24
  this.abort = abort
17
- this.startTime = performance.now()
25
+ this.stats.start = performance.now()
18
26
  this.logger.debug('upstream request started')
19
27
  this.handler.onConnect((reason) => {
20
28
  this.aborted = true
@@ -31,18 +39,26 @@ class Handler {
31
39
  }
32
40
 
33
41
  onBodySent(chunk) {
42
+ if (this.stats.firstBodySent === -1) {
43
+ this.stats.firstBodySent = this.stats.start - performance.now()
44
+ }
45
+
34
46
  return this.handler.onBodySent(chunk)
35
47
  }
36
48
 
37
49
  onRequestSent() {
50
+ this.stats.lastBodySent = this.stats.start - performance.now()
51
+
38
52
  return this.handler.onRequestSent()
39
53
  }
40
54
 
41
55
  onHeaders(statusCode, rawHeaders, resume, statusMessage) {
56
+ this.stats.headers = this.stats.start - performance.now()
57
+
42
58
  this.logger.debug(
43
59
  {
44
60
  ures: { statusCode, headers: parseHeaders(rawHeaders) },
45
- elapsedTime: performance.now() - this.startTime,
61
+ elapsedTime: performance.now() - this.stats.start,
46
62
  },
47
63
  'upstream request response',
48
64
  )
@@ -50,29 +66,29 @@ class Handler {
50
66
  }
51
67
 
52
68
  onData(chunk) {
69
+ if (this.stats.firstBodyReceived === -1) {
70
+ this.stats.firstBodyReceived = this.stats.start - performance.now()
71
+ }
72
+
53
73
  this.pos += chunk.length
54
74
  return this.handler.onData(chunk)
55
75
  }
56
76
 
57
77
  onComplete(rawTrailers) {
58
- this.logger.debug(
59
- { bytesRead: this.pos, elapsedTime: performance.now() - this.startTime },
60
- 'upstream request completed',
61
- )
78
+ this.stats.lastBodyReceived = this.stats.start - performance.now()
79
+ this.stats.end = this.stats.lastBodyReceived
80
+
81
+ this.logger.debug({ bytesRead: this.pos, stats: this.stats }, 'upstream request completed')
62
82
  return this.handler.onComplete(rawTrailers)
63
83
  }
64
84
 
65
85
  onError(err) {
86
+ this.stats.end = this.stats.start - performance.now()
87
+
66
88
  if (this.aborted) {
67
- this.logger.debug(
68
- { bytesRead: this.pos, elapsedTime: performance.now() - this.startTime, err },
69
- 'upstream request aborted',
70
- )
89
+ this.logger.debug({ bytesRead: this.pos, stats: this.stats, err }, 'upstream request aborted')
71
90
  } else {
72
- this.logger.error(
73
- { bytesRead: this.pos, elapsedTime: performance.now() - this.startTime, err },
74
- 'upstream request failed',
75
- )
91
+ this.logger.error({ bytesRead: this.pos, err }, 'upstream request failed')
76
92
  }
77
93
  return this.handler.onError(err)
78
94
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",