@nxtedition/lib 15.1.0 → 15.1.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/http.js CHANGED
@@ -23,8 +23,6 @@ function genReqId() {
23
23
  return `req-${nextReqId.toString(36)}`
24
24
  }
25
25
 
26
- const kResolve = Symbol('resolve')
27
-
28
26
  function onTimeout() {
29
27
  this.destroy(new createError.RequestTimeout())
30
28
  }
@@ -33,18 +31,8 @@ function onRequestError(err) {
33
31
  this.log.error({ err }, 'request error')
34
32
  }
35
33
 
36
- function onRequestClose() {
37
- this.log.debug('request closed')
38
- }
39
-
40
- function onResponseError(err) {
41
- this.log.error({ err }, 'response error')
42
- this[kResolve](Promise.reject(err))
43
- }
44
-
45
- function onResponseClose() {
46
- this.log.debug('response closed')
47
- this[kResolve](null)
34
+ function onRequestEnd() {
35
+ this.log.debug('request end')
48
36
  }
49
37
 
50
38
  module.exports.request = async function request(ctx, next) {
@@ -82,16 +70,30 @@ module.exports.request = async function request(ctx, next) {
82
70
  reqLogger.trace('request started')
83
71
  }
84
72
 
85
- await Promise.all([
86
- next(),
87
- new Promise((resolve) => {
88
- res[kResolve] = resolve
89
-
90
- res.on('timeout', onTimeout).on('error', onResponseError).on('close', onResponseClose)
91
-
92
- req.on('timeout', onTimeout).on('error', onRequestError).on('close', onRequestClose)
93
- }),
94
- ])
73
+ next()?.catch((err) => res.destroy(err))
74
+
75
+ await new Promise((resolve, reject) => {
76
+ req.on('timeout', onTimeout).on('error', onRequestError).on('end', onRequestEnd)
77
+ res
78
+ .on('timeout', onTimeout)
79
+ .on('error', function (err) {
80
+ this.log.error({ err }, 'response error')
81
+ ac.abort(err)
82
+ reject(err)
83
+ })
84
+ .on('finish', function () {
85
+ this.log.debug('response finish')
86
+ ac.abort()
87
+ resolve(null)
88
+ })
89
+ // TODO (fix): Remove this once we can trust that
90
+ // node always emits error or finish.
91
+ .on('close', function () {
92
+ this.log.debug('response close')
93
+ ac.abort()
94
+ resolve(null)
95
+ })
96
+ })
95
97
 
96
98
  const responseTime = Math.round(performance.now() - startTime)
97
99
 
@@ -179,8 +181,6 @@ module.exports.request = async function request(ctx, next) {
179
181
  res.destroy()
180
182
  }
181
183
  }
182
-
183
- ac.abort(err)
184
184
  } finally {
185
185
  res.destroy()
186
186
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "15.1.0",
3
+ "version": "15.1.2",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "files": [
@@ -15,7 +15,7 @@ const maxInt = 2147483647
15
15
  class TimerEntry {
16
16
  constructor(key, refresh, delay) {
17
17
  this.key = key
18
- this.counter = null
18
+ this.counter = -1
19
19
 
20
20
  this.timer = setTimeout(refresh, delay)
21
21
  }
@@ -35,35 +35,45 @@ const fetchClient = new undici.Agent({
35
35
  class FetchEntry {
36
36
  constructor(key, refresh, { resource, options }) {
37
37
  this.key = key
38
- this.counter = null
38
+ this.counter = -1
39
+ this.ac = new AbortController()
39
40
 
40
41
  this.refresh = refresh
41
- this.ac = new AbortController()
42
- this.signal = this.ac.signal
43
42
  this.body = null
44
43
  this.status = null
45
44
  this.error = null
46
45
 
47
- request(resource, {
48
- ...options,
49
- signal: this.signal,
50
- dispatcher: fetchClient,
51
- })
52
- .then(async (res) => {
53
- try {
54
- // TODO (fix): max size...
55
- this.status = res.status
56
- this.headers = res.headers
57
- this.body = await res.text()
58
- } catch (err) {
59
- this.error = err
60
- }
61
- this.refresh()
62
- })
63
- .catch((err) => {
64
- this.error = err
65
- this.refresh()
46
+ try {
47
+ request(resource, {
48
+ ...options,
49
+ signal: this.ac.signal,
50
+ dispatcher: fetchClient,
66
51
  })
52
+ .then(async (res) => {
53
+ try {
54
+ // TODO (fix): max size...
55
+ this.status = res.statusCode
56
+ this.headers = res.headers
57
+ this.body = await res.text()
58
+ } catch (err) {
59
+ this.error = err
60
+ }
61
+
62
+ if (this.refresh) {
63
+ this.refresh()
64
+ }
65
+ })
66
+ .catch((err) => {
67
+ this.error = err
68
+
69
+ if (this.refresh) {
70
+ this.refresh()
71
+ }
72
+ })
73
+ } catch (err) {
74
+ this.error = err
75
+ this.refresh()
76
+ }
67
77
  }
68
78
 
69
79
  dispose() {
@@ -77,7 +87,7 @@ class FetchEntry {
77
87
  class RecordEntry {
78
88
  constructor(key, refresh, ds) {
79
89
  this.key = key
80
- this.counter = null
90
+ this.counter = -1
81
91
 
82
92
  this.refresh = refresh
83
93
  this.record = ds.record.getRecord(key)
@@ -105,7 +115,7 @@ class RecordEntry {
105
115
  class ObservableEntry {
106
116
  constructor(key, refresh, observable) {
107
117
  this.key = key
108
- this.counter = null
118
+ this.counter = -1
109
119
  this.value = kEmpty
110
120
  this.error = null
111
121
  this.refresh = refresh
@@ -131,7 +141,7 @@ class ObservableEntry {
131
141
  class PromiseEntry {
132
142
  constructor(key, refresh, promise) {
133
143
  this.key = key
134
- this.counter = null
144
+ this.counter = -1
135
145
  this.value = kEmpty
136
146
  this.error = null
137
147
  this.refresh = refresh