@nxtedition/lib 17.0.1 → 17.1.1

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.
Files changed (4) hide show
  1. package/app.js +2 -0
  2. package/errors.js +11 -5
  3. package/package.json +2 -1
  4. package/timers.js +92 -0
package/app.js CHANGED
@@ -145,6 +145,8 @@ export function makeApp(appConfig, onTerminate) {
145
145
 
146
146
  terminated = true
147
147
 
148
+ logger.info('terminate')
149
+
148
150
  ac.abort()
149
151
 
150
152
  if (onTerminate) {
package/errors.js CHANGED
@@ -83,11 +83,13 @@ export function serializeError(error) {
83
83
  message = msg,
84
84
  errors,
85
85
  code,
86
+ exitCode = code,
86
87
  signal,
88
+ signalCode = signal,
87
89
  cause,
88
90
  body,
89
- statusCode,
90
- status = statusCode,
91
+ status,
92
+ statusCode = status,
91
93
  headers,
92
94
  ...properties
93
95
  } = error
@@ -96,6 +98,10 @@ export function serializeError(error) {
96
98
  signal = SIGNALS[signal] ?? signal
97
99
  }
98
100
 
101
+ if (typeof signalCode === 'number') {
102
+ signalCode = SIGNALS[signalCode] ?? signalCode
103
+ }
104
+
99
105
  errors = Array.isArray(errors) ? errors.map(serializeError) : undefined
100
106
  cause = cause ? serializeError(cause) : undefined
101
107
 
@@ -106,9 +112,9 @@ export function serializeError(error) {
106
112
  ...properties,
107
113
  message,
108
114
  type,
109
- code,
110
- signal,
111
- status,
115
+ exitCode,
116
+ signalCode,
117
+ statusCode,
112
118
  headers,
113
119
  data,
114
120
  cause,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "17.0.1",
3
+ "version": "17.1.1",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
@@ -20,6 +20,7 @@
20
20
  "logger.js",
21
21
  "mime.js",
22
22
  "proxy.js",
23
+ "timers.js",
23
24
  "trace.js",
24
25
  "weakCache.js",
25
26
  "couch.js",
package/timers.js ADDED
@@ -0,0 +1,92 @@
1
+ let fastNow = Date.now()
2
+ let fastNowTimeout
3
+
4
+ const fastTimers = []
5
+
6
+ function onTimeout() {
7
+ fastNow = Date.now()
8
+
9
+ let len = fastTimers.length
10
+ let idx = 0
11
+ while (idx < len) {
12
+ const timer = fastTimers[idx]
13
+
14
+ if (timer.state === 0) {
15
+ timer.state = fastNow + timer.delay
16
+ } else if (timer.state > 0 && fastNow >= timer.state) {
17
+ timer.state = -1
18
+ timer.callback(timer.opaque)
19
+ }
20
+
21
+ if (timer.state === -1) {
22
+ timer.state = -2
23
+ if (idx !== len - 1) {
24
+ fastTimers[idx] = fastTimers.pop()
25
+ } else {
26
+ fastTimers.pop()
27
+ }
28
+ len -= 1
29
+ } else {
30
+ idx += 1
31
+ }
32
+ }
33
+
34
+ if (fastTimers.length > 0) {
35
+ refreshTimeout()
36
+ }
37
+ }
38
+
39
+ function refreshTimeout() {
40
+ if (fastNowTimeout && fastNowTimeout.refresh) {
41
+ fastNowTimeout.refresh()
42
+ } else {
43
+ clearTimeout(fastNowTimeout)
44
+ fastNowTimeout = setTimeout(onTimeout, 1e3)
45
+ if (fastNowTimeout.unref) {
46
+ fastNowTimeout.unref()
47
+ }
48
+ }
49
+ }
50
+
51
+ class Timeout {
52
+ constructor(callback, delay, opaque) {
53
+ this.callback = callback
54
+ this.delay = delay
55
+ this.opaque = opaque
56
+
57
+ // -2 not in timer list
58
+ // -1 in timer list but inactive
59
+ // 0 in timer list waiting for time
60
+ // > 0 in timer list waiting for time to expire
61
+ this.state = -2
62
+
63
+ this.refresh()
64
+ }
65
+
66
+ refresh() {
67
+ if (this.state === -2) {
68
+ fastTimers.push(this)
69
+ if (!fastNowTimeout || fastTimers.length === 1) {
70
+ refreshTimeout()
71
+ }
72
+ }
73
+
74
+ this.state = 0
75
+ }
76
+
77
+ clear() {
78
+ this.state = -1
79
+ }
80
+ }
81
+
82
+ export function setTimeout(callback, delay, opaque) {
83
+ return delay < 1e3 ? setTimeout(callback, delay, opaque) : new Timeout(callback, delay, opaque)
84
+ }
85
+
86
+ export function clearTimeout(timeout) {
87
+ if (timeout instanceof Timeout) {
88
+ timeout.clear()
89
+ } else {
90
+ clearTimeout(timeout)
91
+ }
92
+ }