@nxtedition/nxt-undici 1.2.2 → 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.
@@ -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.2",
3
+ "version": "1.3.0",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",