@nxtedition/lib 28.0.7 → 28.0.8

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 +38 -29
  2. package/package.json +2 -2
  3. package/sequence.js +5 -9
  4. package/shared.js +12 -1
package/app.js CHANGED
@@ -635,19 +635,7 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
635
635
  stats$ = rxjs.timer(0, 10e3).pipe(rx.map(() => ({})))
636
636
  }
637
637
 
638
- const memoryUsageBC = new BroadcastChannel('nxt:memoryUsage').unref()
639
-
640
- let memoryUsageMap
641
- if (isMainThread) {
642
- memoryUsageMap = new Map()
643
- memoryUsageBC.onmessage = ({ data: { data, id } }) => {
644
- memoryUsageMap.set(id, data)
645
- }
646
- }
647
-
648
- setInterval(() => {
649
- memoryUsageBC.postMessage({ data: process.memoryUsage(), id: serviceWorkerId })
650
- }, 1e3).unref()
638
+ let statsMap
651
639
 
652
640
  const startTime = Date.now()
653
641
  stats$ = stats$.pipe(
@@ -679,12 +667,21 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
679
667
  totalArrayBuffers: 0,
680
668
  }
681
669
 
682
- if (memoryUsageMap) {
683
- for (const memoryUsage of memoryUsageMap.values()) {
684
- memory.totalHeapTotal += memoryUsage.heapTotal
685
- memory.totalHeapUsed += memoryUsage.heapUsed
686
- memory.totalExternal += memoryUsage.external
687
- memory.totalArrayBuffers += memoryUsage.arrayBuffers
670
+ const http = {
671
+ userAgent,
672
+ pending: globalThis._nxt_lib_http_pending?.size,
673
+ totalPending: 0,
674
+ }
675
+
676
+ const undici = {
677
+ sockets: globalThis.__undici_sockets?.size ?? 0,
678
+ totalSockets: 0,
679
+ }
680
+
681
+ if (statsMap) {
682
+ for (const stats of statsMap.values()) {
683
+ http.totalPending += stats.http?.pending ?? 0
684
+ undici.totalSockets += stats.undici?.sockets ?? 0
688
685
  }
689
686
  }
690
687
 
@@ -700,13 +697,8 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
700
697
  resourceLimits,
701
698
  utilization: performance.eventLoopUtilization?.(elu2, elu1),
702
699
  heap: v8.getHeapStatistics(),
703
- http: {
704
- userAgent,
705
- pending: globalThis._nxt_lib_http_pending?.size,
706
- },
707
- undici: {
708
- sockets: globalThis.__undici_sockets?.size ?? 0,
709
- },
700
+ http,
701
+ undici,
710
702
  }
711
703
  }),
712
704
  ),
@@ -726,11 +718,20 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
726
718
  rx.refCount(),
727
719
  )
728
720
 
721
+ const statsBC = new BroadcastChannel('nxt:app:stats').unref()
722
+ if (isMainThread) {
723
+ statsMap = new Map()
724
+ statsBC.onmessage = ({ data: { data, id } }) => {
725
+ statsMap.set(id, data)
726
+ }
727
+ }
728
+
729
729
  monitorProviders.stats$ = stats$
730
730
 
731
731
  if (process.env.NODE_ENV === 'production') {
732
732
  appDestroyers.unshift(
733
733
  stats$.pipe(rx.auditTime(10e3)).subscribe((stats) => {
734
+ statsBC.postMessage({ id: threadId, data: stats })
734
735
  logger.debug(stats, 'STATS')
735
736
  }),
736
737
  )
@@ -779,7 +780,7 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
779
780
  rx.repeatWhen((complete$) => complete$.pipe(rx.delay(10e3))),
780
781
  ),
781
782
  stats$.pipe(
782
- rx.map(({ memory, heap, utilization, undici }) => {
783
+ rx.map(({ memory, heap, utilization, undici, http }) => {
783
784
  const messages = []
784
785
 
785
786
  if (memory?.containerLimit) {
@@ -812,8 +813,16 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
812
813
  if (undici) {
813
814
  messages.push({
814
815
  id: 'app:undici_upstream_sockets',
815
- level: undici.sockets > 8192 ? 50 : undici.sockets > 4096 ? 40 : 30,
816
- msg: `Undici: ${undici.sockets} upstream connected`,
816
+ level: undici.totalSockets > 8192 ? 50 : undici.totalSockets > 4096 ? 40 : 30,
817
+ msg: `Undici: ${undici.totalSockets} upstream connected`,
818
+ })
819
+ }
820
+
821
+ if (http) {
822
+ messages.push({
823
+ id: 'app:http_pending_requests',
824
+ level: http.totalPending > 8192 ? 50 : http.totalPending > 4096 ? 40 : 30,
825
+ msg: `HTTP: ${http.totalPending} pending requests`,
817
826
  })
818
827
  }
819
828
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "28.0.7",
3
+ "version": "28.0.8",
4
4
  "license": "UNLICENSED",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
@@ -92,5 +92,5 @@
92
92
  "pino": ">=7.0.0",
93
93
  "rxjs": "^7.0.0"
94
94
  },
95
- "gitHead": "545d4052432b61ff13ba81fa5a69644cf4901103"
95
+ "gitHead": "5d6fb19ef1936e749aadecc51007ddda71dcf0a3"
96
96
  }
package/sequence.js CHANGED
@@ -98,8 +98,8 @@ export class Sequence {
98
98
  this.#identity = identity
99
99
  for (const part of value) {
100
100
  if (typeof part === 'string') {
101
- const [sequenceStr, id] = part.split(ID_SEP)
102
- const sequence = parseInt(sequenceStr)
101
+ const [sequenceStr, id] = part.split(ID_SEP, 2)
102
+ const sequence = Number(sequenceStr)
103
103
  this.#parts.push(id, sequence)
104
104
  this.#identity = null
105
105
  this.#count += part
@@ -116,15 +116,15 @@ export class Sequence {
116
116
  }
117
117
  assert(this.#identity === 0 || this.#parts.length > 0)
118
118
  } else if (typeof value === 'string') {
119
- const [countStr, token] = value.split('-')
119
+ const [countStr, token] = value.split('-', 2)
120
120
  if (token) {
121
121
  for (const str of token.split('_')) {
122
122
  const [sequenceStr, id] = str.split(ID_SEP)
123
- const sequence = parseInt(sequenceStr)
123
+ const sequence = Number(sequenceStr)
124
124
  this.#parts.push(id, sequence)
125
125
  }
126
126
  }
127
- this.#count = parseInt(countStr)
127
+ this.#count = Number(countStr)
128
128
  this.#value = value
129
129
  } else if (value instanceof Sequence) {
130
130
  this.#count = value.#count
@@ -139,10 +139,6 @@ export class Sequence {
139
139
  throw new Error('invalid sequence count')
140
140
  }
141
141
 
142
- if (!Array.isArray(this.#parts)) {
143
- throw new Error('invalid sequence parts')
144
- }
145
-
146
142
  {
147
143
  let count = 0
148
144
  for (let n = 0; n < this.#parts.length; n += 2) {
package/shared.js CHANGED
@@ -18,11 +18,22 @@ const HWM_COUNT = 1024 // 1024 items
18
18
  * @returns {{sharedState: SharedArrayBuffer, sharedBuffer: SharedArrayBuffer}}
19
19
  */
20
20
  export function alloc(size) {
21
+ if (!Number.isInteger(size)) {
22
+ throw new TypeError('size must be a positive integer')
23
+ }
24
+ if (size <= 0) {
25
+ throw new RangeError('size must be a positive integer')
26
+ }
27
+ if (size >= 2 ** 31 - 8) {
28
+ throw new RangeError('size exceeds maximum of 2GB minus header size')
29
+ }
30
+
21
31
  return {
22
32
  // A small buffer for sharing state (read/write pointers).
23
33
  sharedState: new SharedArrayBuffer(128),
24
34
  // The main buffer for transferring data.
25
- sharedBuffer: new SharedArrayBuffer(size),
35
+ // We need another 8 bytes for entry headers.
36
+ sharedBuffer: new SharedArrayBuffer(size + 8),
26
37
  }
27
38
  }
28
39