@nxtedition/lib 28.0.10 → 28.0.12

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 (3) hide show
  1. package/app.js +75 -63
  2. package/cache.js +1 -1
  3. package/package.json +3 -3
package/app.js CHANGED
@@ -30,8 +30,6 @@ import { makeCouch } from './couch.js'
30
30
  import { makeTemplateCompiler } from '@nxtedition/template'
31
31
  import { makeDeepstream } from './deepstream.js'
32
32
  import * as rxjs from 'rxjs'
33
- import rx from 'rxjs/operators'
34
- import { performance } from 'perf_hooks'
35
33
  import hashString from './hash.js'
36
34
  import { makeTrace } from './trace.js'
37
35
  import { compose, createServer } from './http.js'
@@ -624,32 +622,30 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
624
622
  stats$ = appConfig.stats
625
623
  } else if (typeof appConfig.stats === 'function') {
626
624
  stats$ = rxjs.timer(0, 10e3).pipe(
627
- rx.exhaustMap(() => {
625
+ rxjs.exhaustMap(() => {
628
626
  const ret = appConfig.stats({ ds, couch, logger })
629
627
  return ret?.then || ret?.subscribe ? ret : rxjs.of(ret)
630
628
  }),
631
629
  )
632
630
  } else if (typeof appConfig.stats === 'object') {
633
- stats$ = rxjs.timer(0, 10e3).pipe(rx.map(() => appConfig.stats))
631
+ stats$ = rxjs.timer(0, 10e3).pipe(rxjs.map(() => appConfig.stats))
634
632
  } else {
635
- stats$ = rxjs.timer(0, 10e3).pipe(rx.map(() => ({})))
633
+ stats$ = rxjs.timer(0, 10e3).pipe(rxjs.map(() => ({})))
636
634
  }
637
635
 
638
636
  let statsMap
639
637
 
638
+ monitorProviders.stats$ = new rxjs.BehaviorSubject({})
639
+
640
640
  const startTime = Date.now()
641
- stats$ = stats$.pipe(
642
- rx.map((x) => ({
643
- ...x,
644
- uptime: Date.now() - startTime,
645
- timestamp: Date.now(),
646
- })),
647
- rx.auditTime(1e3),
648
- rx.withLatestFrom(
649
- rxjs.timer(0, 1e3).pipe(
650
- rx.map(() => performance.eventLoopUtilization?.()),
651
- rx.pairwise(),
652
- rx.map(([elu1, elu2]) => {
641
+
642
+ appDestroyers.unshift(
643
+ rxjs
644
+ .timer(0, 1e3)
645
+ .pipe(
646
+ rxjs.map(() => performance.eventLoopUtilization?.()),
647
+ rxjs.pairwise(),
648
+ rxjs.map(([elu1, elu2]) => {
653
649
  const mem = process.memoryUsage()
654
650
  const cpu = process.cpuUsage()
655
651
 
@@ -691,7 +687,10 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
691
687
  }
692
688
  }
693
689
 
690
+ const now = Date.now()
694
691
  return {
692
+ uptime: now - startTime,
693
+ timestamp: now,
695
694
  ds: ds?.stats,
696
695
  couch: couch?.stats,
697
696
  lag: toobusy?.lag(),
@@ -707,39 +706,52 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
707
706
  undici,
708
707
  }
709
708
  }),
710
- ),
711
- ),
712
- rx.map(([serviceStats, appStats]) => ({
713
- ...appStats,
714
- [serviceName]: serviceStats,
715
- })),
716
- rx.retryWhen((err$) =>
717
- err$.pipe(
718
- rx.tap((err) => logger.error({ err }, 'monitor.stats')),
719
- rx.delay(10e3),
720
- ),
721
- ),
722
- rx.startWith({}),
723
- rx.publishReplay(1),
724
- rx.refCount(),
709
+ rxjs.withLatestFrom(stats$.pipe(rxjs.startWith({}))),
710
+ rxjs.map(([appStats, serviceStats]) => ({
711
+ ...appStats,
712
+ // TODO (fix): [serviceName] is not great and can collide.
713
+ [serviceName]: serviceStats,
714
+ })),
715
+ rxjs.retry({
716
+ delay(err, retryCount) {
717
+ logger.error({ err, retryCount }, 'monitor.stats$ failed')
718
+ return rxjs.timer(10e3)
719
+ },
720
+ }),
721
+ )
722
+ .subscribe(monitorProviders.stats$),
725
723
  )
726
724
 
727
725
  const statsBC = new BroadcastChannel('nxt:app:stats').unref()
726
+
728
727
  if (isMainThread) {
729
728
  statsMap = new Map()
730
729
  statsBC.onmessage = ({ data: { data, id } }) => {
731
- statsMap.set(id, data)
730
+ if (data != null) {
731
+ statsMap.set(id, data)
732
+ } else {
733
+ statsMap.delete(id)
734
+ }
732
735
  }
733
736
  }
734
737
 
735
- monitorProviders.stats$ = stats$
738
+ appDestroyers.unshift(
739
+ monitorProviders.stats$
740
+ .subscribe((stats) => {
741
+ statsBC.postMessage({ id: threadId, data: stats })
742
+ })
743
+ .add(() => {
744
+ statsBC.postMessage({ id: threadId, data: undefined })
745
+ }),
746
+ )
736
747
 
737
748
  if (process.env.NODE_ENV === 'production') {
738
749
  appDestroyers.unshift(
739
- stats$.pipe(rx.auditTime(10e3)).subscribe((stats) => {
740
- statsBC.postMessage({ id: threadId, data: stats })
741
- logger.debug(stats, 'STATS')
742
- }),
750
+ monitorProviders.stats$
751
+ .pipe(rxjs.auditTime(10e3))
752
+ .subscribe((stats) => {
753
+ logger.debug(stats, 'STATS')
754
+ })
743
755
  )
744
756
  }
745
757
  }
@@ -755,11 +767,11 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
755
767
  return ret?.then || ret?.subscribe ? ret : rxjs.of(ret)
756
768
  })
757
769
  .pipe(
758
- rx.catchError((err) => rxjs.of({ warnings: [err.message] })),
759
- rx.repeatWhen(() => rxjs.timer(10e3)),
770
+ rxjs.catchError((err) => rxjs.of({ warnings: [err.message] })),
771
+ rxjs.repeatWhen(() => rxjs.timer(10e3)),
760
772
  )
761
773
  } else if (appConfig.status && typeof appConfig.status === 'object') {
762
- status$ = rxjs.timer(0, 10e3).pipe(rx.exhaustMap(() => appConfig.status))
774
+ status$ = rxjs.timer(0, 10e3).pipe(rxjs.exhaustMap(() => appConfig.status))
763
775
  } else {
764
776
  status$ = rxjs.of({})
765
777
  }
@@ -768,9 +780,9 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
768
780
  .combineLatest(
769
781
  [
770
782
  status$.pipe(
771
- rx.filter(Boolean),
772
- rx.map((xs) => (Array.isArray(xs) ? { messages: xs } : xs)),
773
- rx.catchError((err) => {
783
+ rxjs.filter(Boolean),
784
+ rxjs.map((xs) => (Array.isArray(xs) ? { messages: xs } : xs)),
785
+ rxjs.catchError((err) => {
774
786
  logger.error({ err }, 'monitor.status')
775
787
  return rxjs.of([
776
788
  {
@@ -781,12 +793,12 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
781
793
  },
782
794
  ])
783
795
  }),
784
- rx.startWith([]),
785
- rx.distinctUntilChanged(fp.isEqual),
786
- rx.repeatWhen((complete$) => complete$.pipe(rx.delay(10e3))),
796
+ rxjs.startWith([]),
797
+ rxjs.distinctUntilChanged(fp.isEqual),
798
+ rxjs.repeatWhen((complete$) => complete$.pipe(rxjs.delay(10e3))),
787
799
  ),
788
800
  stats$.pipe(
789
- rx.map(({ memory, heap, utilization, undici, http }) => {
801
+ rxjs.map(({ memory, heap, utilization, undici, http }) => {
790
802
  const messages = []
791
803
 
792
804
  if (memory?.containerLimit) {
@@ -836,7 +848,7 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
836
848
  }),
837
849
  ),
838
850
  toobusy?.appLag$.pipe(
839
- rx.map((lag) =>
851
+ rxjs.map((lag) =>
840
852
  lag == null
841
853
  ? []
842
854
  : [
@@ -850,7 +862,7 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
850
862
  ),
851
863
  ) ?? rxjs.of([]),
852
864
  underPressure?.pressure$?.pipe(
853
- rx.map((pressure) =>
865
+ rxjs.map((pressure) =>
854
866
  pressure == null
855
867
  ? []
856
868
  : [
@@ -865,7 +877,7 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
865
877
  ) ?? rxjs.of([]),
866
878
  couch
867
879
  ? rxjs.timer(0, 10e3).pipe(
868
- rx.exhaustMap(async () => {
880
+ rxjs.exhaustMap(async () => {
869
881
  try {
870
882
  await couch.up()
871
883
  return [
@@ -886,8 +898,8 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
886
898
  ]
887
899
  }
888
900
  }),
889
- rx.startWith([]),
890
- rx.distinctUntilChanged(fp.isEqual),
901
+ rxjs.startWith([]),
902
+ rxjs.distinctUntilChanged(fp.isEqual),
891
903
  )
892
904
  : rxjs.of({}),
893
905
  ds
@@ -915,7 +927,7 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
915
927
  : rxjs.of([]),
916
928
  ds
917
929
  ? rxjs.timer(0, 10e3).pipe(
918
- rx.exhaustMap(async () => {
930
+ rxjs.exhaustMap(async () => {
919
931
  const messages = []
920
932
 
921
933
  if (ds.stats.record.records > 100e3) {
@@ -971,8 +983,8 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
971
983
  ].filter(Boolean),
972
984
  )
973
985
  .pipe(
974
- rx.auditTime(1e3),
975
- rx.map(([status, lag, couch, ds]) => {
986
+ rxjs.auditTime(1e3),
987
+ rxjs.map(([status, lag, couch, ds]) => {
976
988
  const messages = [
977
989
  lag,
978
990
  couch,
@@ -1007,21 +1019,21 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
1007
1019
 
1008
1020
  return { ...status, messages, timestamp: Date.now() }
1009
1021
  }),
1010
- rx.catchError((err) => {
1022
+ rxjs.catchError((err) => {
1011
1023
  logger.error({ err }, 'monitor.status')
1012
1024
  return rxjs.of({
1013
1025
  messages: [{ id: 'app:monitor_status', level: 50, code: err.code, msg: err.message }],
1014
1026
  })
1015
1027
  }),
1016
- rx.repeatWhen((complete$) => complete$.pipe(rx.delay(10e3))),
1017
- rx.startWith({}),
1018
- rx.distinctUntilChanged(fp.isEqual),
1019
- rx.publishReplay(1),
1020
- rx.refCount(),
1028
+ rxjs.repeatWhen((complete$) => complete$.pipe(rxjs.delay(10e3))),
1029
+ rxjs.startWith({}),
1030
+ rxjs.distinctUntilChanged(fp.isEqual),
1031
+ rxjs.publishReplay(1),
1032
+ rxjs.refCount(),
1021
1033
  )
1022
1034
 
1023
1035
  const loggerSubscription = status$
1024
- .pipe(rx.auditTime(1e3), rx.pluck('messages'), rx.startWith([]), rx.pairwise())
1036
+ .pipe(rxjs.auditTime(1e3), rxjs.pluck('messages'), rxjs.startWith([]), rxjs.pairwise())
1025
1037
  .subscribe(([prev, next]) => {
1026
1038
  for (const { level, msg: status, ...message } of fp.differenceBy('id', next, prev)) {
1027
1039
  if (level >= 50) {
package/cache.js CHANGED
@@ -291,7 +291,7 @@ export class AsyncCache {
291
291
  throw new TypeError('ttl must be a finite number')
292
292
  }
293
293
 
294
- const stale = now + Math.max(ttl, this.#stale(value, key) ?? 0)
294
+ const stale = ttl + (this.#stale(value, key) ?? 0)
295
295
  if (!Number.isFinite(stale)) {
296
296
  throw new TypeError('stale must be a finite number')
297
297
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "28.0.10",
3
+ "version": "28.0.12",
4
4
  "license": "UNLICENSED",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
@@ -52,7 +52,7 @@
52
52
  "@elastic/transport": "^8.9.3",
53
53
  "@nxtedition/nxt-undici": "^7.1.9",
54
54
  "@nxtedition/sched": "^1.0.2",
55
- "@nxtedition/template": "^1.0.9",
55
+ "@nxtedition/template": "^1.0.10",
56
56
  "@nxtedition/weak-cache": "^1.0.2",
57
57
  "diff": "5.2.0",
58
58
  "fast-querystring": "^1.1.2",
@@ -92,5 +92,5 @@
92
92
  "pino": ">=7.0.0",
93
93
  "rxjs": "^7.0.0"
94
94
  },
95
- "gitHead": "e9be8f39c7bbf874619d29249809959ca6f7950b"
95
+ "gitHead": "e0d096f01e85551f3caac31e96f8d144f4c17f5a"
96
96
  }