@nxtedition/nxt-undici 1.0.2 → 1.0.4

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
@@ -115,7 +115,7 @@ async function request(url, opts) {
115
115
  }
116
116
 
117
117
  headers = {
118
- 'request-id': `req-${genReqId()}`,
118
+ 'request-id': genReqId(),
119
119
  'user-agent': opts.userAgent ?? globalThis.userAgent,
120
120
  ...headers,
121
121
  }
@@ -1,11 +1,23 @@
1
1
  const { parseHeaders } = require('../utils')
2
- const xuid = require('xuid')
3
2
  const { performance } = require('perf_hooks')
4
3
 
4
+ // https://github.com/fastify/fastify/blob/main/lib/reqIdGenFactory.js
5
+ // 2,147,483,647 (2^31 − 1) stands for max SMI value (an internal optimization of V8).
6
+ // With this upper bound, if you'll be generating 1k ids/sec, you're going to hit it in ~25 days.
7
+ // This is very likely to happen in real-world applications, hence the limit is enforced.
8
+ // Growing beyond this value will make the id generation slower and cause a deopt.
9
+ // In the worst cases, it will become a float, losing accuracy.
10
+ const maxInt = 2147483647
11
+ let nextReqId = Math.floor(Math.random() * maxInt)
12
+ function genReqId() {
13
+ nextReqId = (nextReqId + 1) & maxInt
14
+ return `req-${nextReqId.toString(36)}`
15
+ }
16
+
5
17
  class Handler {
6
18
  constructor(opts, { handler }) {
7
19
  this.handler = handler
8
- this.opts = opts.id ? opts : { ...opts, id: xuid() }
20
+ this.opts = opts.id ? opts : { ...opts, id: genReqId() }
9
21
  this.abort = null
10
22
  this.aborted = false
11
23
  this.logger = opts.logger.child({ ureq: { id: opts.id } })
@@ -10,7 +10,9 @@ class Handler {
10
10
  this.aborted = false
11
11
  this.reason = null
12
12
 
13
- this.count = 0
13
+ this.retryCount = 0
14
+ this.retryPromise = null
15
+
14
16
  this.pos = 0
15
17
  this.end = null
16
18
  this.error = null
@@ -110,31 +112,30 @@ class Handler {
110
112
  }
111
113
 
112
114
  onError(err) {
113
- if (this.timeout) {
114
- clearTimeout(this.timeout)
115
- this.timeout = null
116
- }
117
-
118
115
  if (!this.resume || this.aborted || !this.etag || isDisturbed(this.opts.body)) {
119
116
  return this.handler.onError(err)
120
117
  }
121
118
 
122
- const retryPromise = retryFn(err, this.count++, this.opts)
119
+ // TODO (fix): abort signal?
120
+ const retryPromise = retryFn(err, this.retryCount++, this.opts)
123
121
  if (retryPromise == null) {
124
122
  return this.handler.onError(err)
125
123
  }
126
124
 
127
125
  retryPromise
128
126
  .then(() => {
129
- this.timeout = null
130
- try {
131
- this.dispatch(this.opts, this)
132
- } catch (err2) {
133
- this.handler.onError(err)
127
+ if (!this.aborted) {
128
+ try {
129
+ this.dispatch(this.opts, this)
130
+ } catch (err2) {
131
+ this.handler.onError(new AggregateError([err, err2]))
132
+ }
134
133
  }
135
134
  })
136
135
  .catch((err) => {
137
- this.handler.onError(err)
136
+ if (!this.aborted) {
137
+ this.handler.onError(err)
138
+ }
138
139
  })
139
140
 
140
141
  this.error = err
@@ -8,9 +8,9 @@ class Handler {
8
8
  this.abort = null
9
9
  this.aborted = false
10
10
  this.reason = null
11
- this.timeout = null
12
- this.count = 0
13
- this.retryAfter = null
11
+
12
+ this.retryCount = 0
13
+ this.retryPromise = null
14
14
 
15
15
  this.handler.onConnect((reason) => {
16
16
  this.aborted = true
@@ -52,31 +52,30 @@ class Handler {
52
52
  }
53
53
 
54
54
  onError(err) {
55
- if (this.timeout) {
56
- clearTimeout(this.timeout)
57
- this.timeout = null
58
- }
59
-
60
55
  if (this.aborted || isDisturbed(this.opts.body)) {
61
56
  return this.handler.onError(err)
62
57
  }
63
58
 
64
- const retryPromise = retryFn(err, this.count++, this.opts)
59
+ // TODO (fix): abort signal?
60
+ const retryPromise = retryFn(err, this.retryCount++, this.opts)
65
61
  if (retryPromise == null) {
66
62
  return this.handler.onError(err)
67
63
  }
68
64
 
69
65
  retryPromise
70
66
  .then(() => {
71
- this.timeout = null
72
- try {
73
- this.dispatch(this.opts, this)
74
- } catch (err2) {
75
- this.handler.onError(err)
67
+ if (!this.aborted) {
68
+ try {
69
+ this.dispatch(this.opts, this)
70
+ } catch (err2) {
71
+ this.handler.onError(new AggregateError([err, err2]))
72
+ }
76
73
  }
77
74
  })
78
75
  .catch((err) => {
79
- this.handler.onError(err)
76
+ if (!this.aborted) {
77
+ this.handler.onError(err)
78
+ }
80
79
  })
81
80
 
82
81
  this.opts.logger?.debug('retrying response')
@@ -10,8 +10,7 @@ class Handler {
10
10
  this.aborted = false
11
11
  this.reason = null
12
12
 
13
- this.timeout = null
14
- this.count = 0
13
+ this.retryCount = 0
15
14
  this.retryPromise = null
16
15
 
17
16
  this.handler.onConnect((reason) => {
@@ -47,7 +46,8 @@ class Handler {
47
46
 
48
47
  const err = createError(statusCode, { headers: parseHeaders(rawHeaders) })
49
48
 
50
- const retryPromise = retryFn(err, this.count++, this.opts)
49
+ // TODO (fix): abort signal?
50
+ const retryPromise = retryFn(err, this.retryCount++, this.opts)
51
51
  if (retryPromise == null) {
52
52
  return this.handler.onHeaders(statusCode, rawHeaders, resume, statusMessage)
53
53
  }
@@ -70,26 +70,24 @@ class Handler {
70
70
  }
71
71
 
72
72
  onError(err) {
73
- if (this.timeout) {
74
- clearTimeout(this.timeout)
75
- this.timeout = null
76
- }
77
-
78
73
  if (this.retryPromise == null || this.aborted || isDisturbed(this.opts.body)) {
79
74
  return this.handler.onError(err)
80
75
  }
81
76
 
82
77
  this.retryPromise
83
78
  .then(() => {
84
- this.timeout = null
85
- try {
86
- this.dispatch(this.opts, this)
87
- } catch (err) {
88
- this.handler.onError(err)
79
+ if (!this.aborted) {
80
+ try {
81
+ this.dispatch(this.opts, this)
82
+ } catch (err2) {
83
+ this.handler.onError(new AggregateError([err, err2]))
84
+ }
89
85
  }
90
86
  })
91
87
  .catch((err) => {
92
- this.handler.onError(err)
88
+ if (!this.aborted) {
89
+ this.handler.onError(err)
90
+ }
93
91
  })
94
92
  this.retryPromise = null
95
93
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/nxt-undici",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "main": "lib/index.js",