@dudousxd/nestjs-durable 0.20.0 → 0.22.0

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/dist/index.js CHANGED
@@ -293,7 +293,7 @@ DurableWorkerModule = _ts_decorate([
293
293
  ], DurableWorkerModule);
294
294
 
295
295
  // src/durable.module.ts
296
- import { DURABLE_OPTIONS, DURABLE_OPTIONS_CANONICAL as DURABLE_OPTIONS_CANONICAL4, STATE_STORE, STATE_STORE_CANONICAL as STATE_STORE_CANONICAL2, TRANSPORT, TRANSPORT_CANONICAL as TRANSPORT_CANONICAL2, WorkflowEngine as WorkflowEngine5 } from "@dudousxd/nestjs-durable-core";
296
+ import { DURABLE_OPTIONS, DURABLE_OPTIONS_CANONICAL as DURABLE_OPTIONS_CANONICAL5, STATE_STORE, STATE_STORE_CANONICAL as STATE_STORE_CANONICAL3, TRANSPORT, TRANSPORT_CANONICAL as TRANSPORT_CANONICAL2, WorkflowEngine as WorkflowEngine5 } from "@dudousxd/nestjs-durable-core";
297
297
  import { Module as Module2 } from "@nestjs/common";
298
298
  import { DiscoveryModule as DiscoveryModule2, ModuleRef } from "@nestjs/core";
299
299
 
@@ -435,8 +435,8 @@ EntityService = _ts_decorate3([
435
435
  ])
436
436
  ], EntityService);
437
437
 
438
- // src/timer-poller.ts
439
- import { DURABLE_OPTIONS_CANONICAL as DURABLE_OPTIONS_CANONICAL2, WorkflowEngine as WorkflowEngine2, runSchedules } from "@dudousxd/nestjs-durable-core";
438
+ // src/retention-poller.ts
439
+ import { DURABLE_OPTIONS_CANONICAL as DURABLE_OPTIONS_CANONICAL2, STATE_STORE_CANONICAL, TERMINAL_RUN_STATUSES, parseDuration } from "@dudousxd/nestjs-durable-core";
440
440
  import { Inject as Inject3, Injectable as Injectable4 } from "@nestjs/common";
441
441
  function _ts_decorate4(decorators, target, key, desc) {
442
442
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -455,6 +455,113 @@ function _ts_param3(paramIndex, decorator) {
455
455
  };
456
456
  }
457
457
  __name(_ts_param3, "_ts_param");
458
+ var DEFAULT_SWEEP_INTERVAL_MS = 6e4;
459
+ var DEFAULT_BATCH_SIZE = 1e3;
460
+ var MAX_BATCHES_PER_POLICY = 100;
461
+ function validateRetention(retention) {
462
+ const seen = /* @__PURE__ */ new Set();
463
+ for (const policy of retention.policies) {
464
+ if (policy.statuses.length === 0) {
465
+ throw new Error("durable retention: each policy must list at least one status");
466
+ }
467
+ if (policy.maxAge == null && policy.maxCount == null) {
468
+ throw new Error("durable retention: each policy must set maxAge and/or maxCount");
469
+ }
470
+ if (policy.maxAge != null) parseDuration(policy.maxAge);
471
+ for (const status of policy.statuses) {
472
+ if (!TERMINAL_RUN_STATUSES.includes(status)) {
473
+ throw new Error(`durable retention: status "${status}" is not terminal; only ${TERMINAL_RUN_STATUSES.join(", ")} can be pruned`);
474
+ }
475
+ if (seen.has(status)) {
476
+ throw new Error(`durable retention: status "${status}" appears in more than one policy; status sets must be disjoint`);
477
+ }
478
+ seen.add(status);
479
+ }
480
+ }
481
+ }
482
+ __name(validateRetention, "validateRetention");
483
+ var RetentionPoller = class {
484
+ static {
485
+ __name(this, "RetentionPoller");
486
+ }
487
+ store;
488
+ options;
489
+ timer;
490
+ sweeping = false;
491
+ constructor(store, options) {
492
+ this.store = store;
493
+ this.options = options;
494
+ }
495
+ async onApplicationBootstrap() {
496
+ if (this.options.worker === false) return;
497
+ const retention = this.options.retention;
498
+ if (!retention || retention.policies.length === 0) return;
499
+ validateRetention(retention);
500
+ if (typeof this.store.pruneTerminalRuns !== "function") {
501
+ console.warn("[nestjs-durable] `retention` is configured but the store adapter does not implement pruneTerminalRuns; retention is disabled.");
502
+ return;
503
+ }
504
+ await this.sweep();
505
+ const intervalMs = retention.sweepInterval != null ? parseDuration(retention.sweepInterval) : DEFAULT_SWEEP_INTERVAL_MS;
506
+ if (intervalMs > 0) {
507
+ this.timer = setInterval(() => void this.sweep(), intervalMs);
508
+ this.timer.unref?.();
509
+ }
510
+ }
511
+ onModuleDestroy() {
512
+ if (this.timer) clearInterval(this.timer);
513
+ }
514
+ async sweep() {
515
+ if (this.sweeping) return;
516
+ const retention = this.options.retention;
517
+ const prune = this.store.pruneTerminalRuns;
518
+ if (!retention || typeof prune !== "function") return;
519
+ this.sweeping = true;
520
+ try {
521
+ const batchSize = retention.batchSize ?? DEFAULT_BATCH_SIZE;
522
+ const now = Date.now();
523
+ for (const policy of retention.policies) {
524
+ for (let batch = 0; batch < MAX_BATCHES_PER_POLICY; batch++) {
525
+ const deleted = await prune.call(this.store, policy, now, batchSize);
526
+ if (deleted < batchSize) break;
527
+ }
528
+ }
529
+ } finally {
530
+ this.sweeping = false;
531
+ }
532
+ }
533
+ };
534
+ RetentionPoller = _ts_decorate4([
535
+ Injectable4(),
536
+ _ts_param3(0, Inject3(STATE_STORE_CANONICAL)),
537
+ _ts_param3(1, Inject3(DURABLE_OPTIONS_CANONICAL2)),
538
+ _ts_metadata4("design:type", Function),
539
+ _ts_metadata4("design:paramtypes", [
540
+ typeof StateStore === "undefined" ? Object : StateStore,
541
+ typeof DurableModuleOptions === "undefined" ? Object : DurableModuleOptions
542
+ ])
543
+ ], RetentionPoller);
544
+
545
+ // src/timer-poller.ts
546
+ import { DURABLE_OPTIONS_CANONICAL as DURABLE_OPTIONS_CANONICAL3, WorkflowEngine as WorkflowEngine2, runSchedules } from "@dudousxd/nestjs-durable-core";
547
+ import { Inject as Inject4, Injectable as Injectable5 } from "@nestjs/common";
548
+ function _ts_decorate5(decorators, target, key, desc) {
549
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
550
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
551
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
552
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
553
+ }
554
+ __name(_ts_decorate5, "_ts_decorate");
555
+ function _ts_metadata5(k, v) {
556
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
557
+ }
558
+ __name(_ts_metadata5, "_ts_metadata");
559
+ function _ts_param4(paramIndex, decorator) {
560
+ return function(target, key) {
561
+ decorator(target, key, paramIndex);
562
+ };
563
+ }
564
+ __name(_ts_param4, "_ts_param");
458
565
  var TimerPoller = class {
459
566
  static {
460
567
  __name(this, "TimerPoller");
@@ -499,11 +606,11 @@ var TimerPoller = class {
499
606
  }
500
607
  }
501
608
  };
502
- TimerPoller = _ts_decorate4([
503
- Injectable4(),
504
- _ts_param3(1, Inject3(DURABLE_OPTIONS_CANONICAL2)),
505
- _ts_metadata4("design:type", Function),
506
- _ts_metadata4("design:paramtypes", [
609
+ TimerPoller = _ts_decorate5([
610
+ Injectable5(),
611
+ _ts_param4(1, Inject4(DURABLE_OPTIONS_CANONICAL3)),
612
+ _ts_metadata5("design:type", Function),
613
+ _ts_metadata5("design:paramtypes", [
507
614
  typeof WorkflowEngine2 === "undefined" ? Object : WorkflowEngine2,
508
615
  typeof DurableModuleOptions === "undefined" ? Object : DurableModuleOptions
509
616
  ])
@@ -513,8 +620,8 @@ TimerPoller = _ts_decorate4([
513
620
  var CONTEXT_ACCESSOR = Symbol.for("@dudousxd/nestjs-context:accessor");
514
621
 
515
622
  // src/workflow.registrar.ts
516
- import { DURABLE_OPTIONS_CANONICAL as DURABLE_OPTIONS_CANONICAL3, STATE_STORE_CANONICAL, WorkflowEngine as WorkflowEngine3, parseDuration, workflowName } from "@dudousxd/nestjs-durable-core";
517
- import { Inject as Inject4, Injectable as Injectable5 } from "@nestjs/common";
623
+ import { DURABLE_OPTIONS_CANONICAL as DURABLE_OPTIONS_CANONICAL4, STATE_STORE_CANONICAL as STATE_STORE_CANONICAL2, WorkflowEngine as WorkflowEngine3, parseDuration as parseDuration2, workflowName } from "@dudousxd/nestjs-durable-core";
624
+ import { Inject as Inject5, Injectable as Injectable6 } from "@nestjs/common";
518
625
  import { DiscoveryService as DiscoveryService3, MetadataScanner as MetadataScanner3 } from "@nestjs/core";
519
626
 
520
627
  // src/input-validation.ts
@@ -557,23 +664,23 @@ function isStepInterceptor(target) {
557
664
  __name(isStepInterceptor, "isStepInterceptor");
558
665
 
559
666
  // src/workflow.registrar.ts
560
- function _ts_decorate5(decorators, target, key, desc) {
667
+ function _ts_decorate6(decorators, target, key, desc) {
561
668
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
562
669
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
563
670
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
564
671
  return c > 3 && r && Object.defineProperty(target, key, r), r;
565
672
  }
566
- __name(_ts_decorate5, "_ts_decorate");
567
- function _ts_metadata5(k, v) {
673
+ __name(_ts_decorate6, "_ts_decorate");
674
+ function _ts_metadata6(k, v) {
568
675
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
569
676
  }
570
- __name(_ts_metadata5, "_ts_metadata");
571
- function _ts_param4(paramIndex, decorator) {
677
+ __name(_ts_metadata6, "_ts_metadata");
678
+ function _ts_param5(paramIndex, decorator) {
572
679
  return function(target, key) {
573
680
  decorator(target, key, paramIndex);
574
681
  };
575
682
  }
576
- __name(_ts_param4, "_ts_param");
683
+ __name(_ts_param5, "_ts_param");
577
684
  var WorkflowRegistrar = class {
578
685
  static {
579
686
  __name(this, "WorkflowRegistrar");
@@ -626,11 +733,11 @@ var WorkflowRegistrar = class {
626
733
  const validateInput = meta.validateInput ?? (meta.inputSchema ? classValidatorInput(meta.inputSchema) : void 0);
627
734
  const eventBatch = meta.debounce ? {
628
735
  mode: "debounce",
629
- windowMs: parseDuration(meta.debounce)
736
+ windowMs: parseDuration2(meta.debounce)
630
737
  } : meta.batch ? {
631
738
  mode: "batch",
632
739
  maxSize: meta.batch.maxSize,
633
- windowMs: parseDuration(meta.batch.within)
740
+ windowMs: parseDuration2(meta.batch.within)
634
741
  } : void 0;
635
742
  const workflowCtor = workflow.constructor;
636
743
  this.engine.register(meta.name, meta.version, (ctx, input) => workflow.run(ctx, input), {
@@ -686,12 +793,12 @@ var WorkflowRegistrar = class {
686
793
  });
687
794
  }
688
795
  };
689
- WorkflowRegistrar = _ts_decorate5([
690
- Injectable5(),
691
- _ts_param4(3, Inject4(STATE_STORE_CANONICAL)),
692
- _ts_param4(4, Inject4(DURABLE_OPTIONS_CANONICAL3)),
693
- _ts_metadata5("design:type", Function),
694
- _ts_metadata5("design:paramtypes", [
796
+ WorkflowRegistrar = _ts_decorate6([
797
+ Injectable6(),
798
+ _ts_param5(3, Inject5(STATE_STORE_CANONICAL2)),
799
+ _ts_param5(4, Inject5(DURABLE_OPTIONS_CANONICAL4)),
800
+ _ts_metadata6("design:type", Function),
801
+ _ts_metadata6("design:paramtypes", [
695
802
  typeof DiscoveryService3 === "undefined" ? Object : DiscoveryService3,
696
803
  typeof MetadataScanner3 === "undefined" ? Object : MetadataScanner3,
697
804
  typeof WorkflowEngine3 === "undefined" ? Object : WorkflowEngine3,
@@ -703,18 +810,18 @@ WorkflowRegistrar = _ts_decorate5([
703
810
  // src/workflow.service.ts
704
811
  import { randomUUID } from "node:crypto";
705
812
  import { WorkflowEngine as WorkflowEngine4 } from "@dudousxd/nestjs-durable-core";
706
- import { Injectable as Injectable6 } from "@nestjs/common";
707
- function _ts_decorate6(decorators, target, key, desc) {
813
+ import { Injectable as Injectable7 } from "@nestjs/common";
814
+ function _ts_decorate7(decorators, target, key, desc) {
708
815
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
709
816
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
710
817
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
711
818
  return c > 3 && r && Object.defineProperty(target, key, r), r;
712
819
  }
713
- __name(_ts_decorate6, "_ts_decorate");
714
- function _ts_metadata6(k, v) {
820
+ __name(_ts_decorate7, "_ts_decorate");
821
+ function _ts_metadata7(k, v) {
715
822
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
716
823
  }
717
- __name(_ts_metadata6, "_ts_metadata");
824
+ __name(_ts_metadata7, "_ts_metadata");
718
825
  var WorkflowService = class {
719
826
  static {
720
827
  __name(this, "WorkflowService");
@@ -754,22 +861,22 @@ var WorkflowService = class {
754
861
  return this.engine.publishEvent(name, payload, opts);
755
862
  }
756
863
  };
757
- WorkflowService = _ts_decorate6([
758
- Injectable6(),
759
- _ts_metadata6("design:type", Function),
760
- _ts_metadata6("design:paramtypes", [
864
+ WorkflowService = _ts_decorate7([
865
+ Injectable7(),
866
+ _ts_metadata7("design:type", Function),
867
+ _ts_metadata7("design:paramtypes", [
761
868
  typeof WorkflowEngine4 === "undefined" ? Object : WorkflowEngine4
762
869
  ])
763
870
  ], WorkflowService);
764
871
 
765
872
  // src/durable.module.ts
766
- function _ts_decorate7(decorators, target, key, desc) {
873
+ function _ts_decorate8(decorators, target, key, desc) {
767
874
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
768
875
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
769
876
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
770
877
  return c > 3 && r && Object.defineProperty(target, key, r), r;
771
878
  }
772
- __name(_ts_decorate7, "_ts_decorate");
879
+ __name(_ts_decorate8, "_ts_decorate");
773
880
  function resolveAccessor(moduleRef) {
774
881
  try {
775
882
  return moduleRef.get(CONTEXT_ACCESSOR, {
@@ -815,13 +922,13 @@ var DurableModule = class _DurableModule {
815
922
  }
816
923
  static forRoot(options) {
817
924
  return _DurableModule.build({
818
- provide: DURABLE_OPTIONS_CANONICAL4,
925
+ provide: DURABLE_OPTIONS_CANONICAL5,
819
926
  useValue: options
820
927
  });
821
928
  }
822
929
  static forRootAsync(options) {
823
930
  return _DurableModule.build({
824
- provide: DURABLE_OPTIONS_CANONICAL4,
931
+ provide: DURABLE_OPTIONS_CANONICAL5,
825
932
  useFactory: options.useFactory,
826
933
  inject: options.inject ?? []
827
934
  });
@@ -836,23 +943,23 @@ var DurableModule = class _DurableModule {
836
943
  providers: [
837
944
  optionsProvider,
838
945
  {
839
- provide: STATE_STORE_CANONICAL2,
946
+ provide: STATE_STORE_CANONICAL3,
840
947
  useFactory: /* @__PURE__ */ __name((options) => options.store, "useFactory"),
841
948
  inject: [
842
- DURABLE_OPTIONS_CANONICAL4
949
+ DURABLE_OPTIONS_CANONICAL5
843
950
  ]
844
951
  },
845
952
  {
846
953
  provide: TRANSPORT_CANONICAL2,
847
954
  useFactory: /* @__PURE__ */ __name((options) => options.transport ?? null, "useFactory"),
848
955
  inject: [
849
- DURABLE_OPTIONS_CANONICAL4
956
+ DURABLE_OPTIONS_CANONICAL5
850
957
  ]
851
958
  },
852
959
  // Legacy back-compat aliases (deprecated tokens still resolve to the same instances):
853
960
  {
854
961
  provide: STATE_STORE,
855
- useExisting: STATE_STORE_CANONICAL2
962
+ useExisting: STATE_STORE_CANONICAL3
856
963
  },
857
964
  {
858
965
  provide: TRANSPORT,
@@ -860,7 +967,7 @@ var DurableModule = class _DurableModule {
860
967
  },
861
968
  {
862
969
  provide: DURABLE_OPTIONS,
863
- useExisting: DURABLE_OPTIONS_CANONICAL4
970
+ useExisting: DURABLE_OPTIONS_CANONICAL5
864
971
  },
865
972
  {
866
973
  provide: WorkflowEngine5,
@@ -897,9 +1004,9 @@ var DurableModule = class _DurableModule {
897
1004
  return engine;
898
1005
  }, "useFactory"),
899
1006
  inject: [
900
- STATE_STORE_CANONICAL2,
1007
+ STATE_STORE_CANONICAL3,
901
1008
  TRANSPORT_CANONICAL2,
902
- DURABLE_OPTIONS_CANONICAL4,
1009
+ DURABLE_OPTIONS_CANONICAL5,
903
1010
  ModuleRef
904
1011
  ]
905
1012
  },
@@ -907,22 +1014,23 @@ var DurableModule = class _DurableModule {
907
1014
  EntityService,
908
1015
  WorkflowRegistrar,
909
1016
  DurableStepRegistrar,
910
- TimerPoller
1017
+ TimerPoller,
1018
+ RetentionPoller
911
1019
  ],
912
1020
  exports: [
913
1021
  WorkflowService,
914
1022
  EntityService,
915
1023
  WorkflowEngine5,
916
1024
  STATE_STORE,
917
- STATE_STORE_CANONICAL2,
1025
+ STATE_STORE_CANONICAL3,
918
1026
  TRANSPORT,
919
1027
  TRANSPORT_CANONICAL2,
920
- DURABLE_OPTIONS_CANONICAL4
1028
+ DURABLE_OPTIONS_CANONICAL5
921
1029
  ]
922
1030
  };
923
1031
  }
924
1032
  };
925
- DurableModule = _ts_decorate7([
1033
+ DurableModule = _ts_decorate8([
926
1034
  Module2({})
927
1035
  ], DurableModule);
928
1036
  export {