@rotorsoft/act 1.11.0 → 1.12.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
@@ -827,6 +827,76 @@ var AutocloseController = class {
827
827
  }
828
828
  };
829
829
 
830
+ // src/internal/autoclose-policy.ts
831
+ import { z as z2 } from "zod";
832
+ var MIN_AFTER_MS = 6e4;
833
+ var AfterSchema = z2.object({
834
+ days: z2.number({ message: "autocloses: after.days must be a number" }).positive("autocloses: after.days must be > 0")
835
+ }).refine((o) => o.days * 864e5 >= MIN_AFTER_MS, {
836
+ message: `autocloses: after resolves to < ${MIN_AFTER_MS}ms (one minute), which is too short to be a meaningful retention window`
837
+ });
838
+ var IsSchema = z2.union([
839
+ z2.string().min(1, "autocloses: is must be a non-empty event name"),
840
+ z2.array(
841
+ z2.string().min(1, "autocloses: is entries must be non-empty event names")
842
+ ).min(1, "autocloses: is must include at least one event name").readonly()
843
+ ]);
844
+ var ReachesSchema = z2.number({ message: "autocloses: reaches must be a number" }).int("autocloses: reaches must be an integer").min(1, "autocloses: reaches must be >= 1");
845
+ var OrBlockSchema = z2.object({
846
+ after: AfterSchema.optional(),
847
+ is: IsSchema.optional(),
848
+ reaches: ReachesSchema.optional()
849
+ }).strict().refine(
850
+ (o) => o.after !== void 0 || o.is !== void 0 || o.reaches !== void 0,
851
+ {
852
+ message: "autocloses: `or` block must include at least one of after / is / reaches"
853
+ }
854
+ );
855
+ var AutoclosePolicySchema = z2.object({
856
+ after: AfterSchema.optional(),
857
+ is: IsSchema.optional(),
858
+ reaches: ReachesSchema.optional(),
859
+ or: OrBlockSchema.optional()
860
+ }).strict().refine(
861
+ (o) => o.after !== void 0 || o.is !== void 0 || o.reaches !== void 0 || o.or !== void 0,
862
+ {
863
+ message: "autocloses: at least one of after / is / reaches / or must be specified \u2014 empty `{}` is a misconfiguration"
864
+ }
865
+ );
866
+ function compile_after(after) {
867
+ const after_ms = after.days * 864e5;
868
+ return (_stream, head) => Date.now() - head.created.getTime() >= after_ms;
869
+ }
870
+ function compile_is(is) {
871
+ const set = new Set(typeof is === "string" ? [is] : is);
872
+ return (_stream, head) => set.has(head.name);
873
+ }
874
+ function compile_reaches(reaches) {
875
+ return (_stream, _head, count) => count >= reaches;
876
+ }
877
+ function compile_autoclose_policy(options) {
878
+ const parsed = AutoclosePolicySchema.parse(options);
879
+ const and_preds = [];
880
+ if (parsed.after) and_preds.push(compile_after(parsed.after));
881
+ if (parsed.is) and_preds.push(compile_is(parsed.is));
882
+ if (parsed.reaches) and_preds.push(compile_reaches(parsed.reaches));
883
+ const or_preds = [];
884
+ if (parsed.or) {
885
+ if (parsed.or.after) or_preds.push(compile_after(parsed.or.after));
886
+ if (parsed.or.is) or_preds.push(compile_is(parsed.or.is));
887
+ if (parsed.or.reaches) or_preds.push(compile_reaches(parsed.or.reaches));
888
+ }
889
+ return (stream, head, count) => {
890
+ if (and_preds.length > 0 && and_preds.every((p) => p(stream, head, count))) {
891
+ return true;
892
+ }
893
+ if (or_preds.some((p) => p(stream, head, count))) {
894
+ return true;
895
+ }
896
+ return false;
897
+ };
898
+ }
899
+
830
900
  // src/internal/build-classify.ts
831
901
  var ALL_LANES = /* @__PURE__ */ Symbol("act-1103/all-lanes");
832
902
  function classify_registry(registry, states) {
@@ -3650,10 +3720,17 @@ function action_builder(state2) {
3650
3720
  internal.disclose = disclose;
3651
3721
  return builder;
3652
3722
  },
3653
- autocloses(predicate) {
3654
- if (typeof predicate !== "function") {
3723
+ autocloses(predicate_or_policy) {
3724
+ let predicate;
3725
+ if (typeof predicate_or_policy === "function") {
3726
+ predicate = predicate_or_policy;
3727
+ } else if (predicate_or_policy !== null && typeof predicate_or_policy === "object") {
3728
+ predicate = compile_autoclose_policy(
3729
+ predicate_or_policy
3730
+ );
3731
+ } else {
3655
3732
  throw new Error(
3656
- ".autocloses(predicate) requires a function; got " + typeof predicate
3733
+ ".autocloses(...) requires a function or a policy object; got " + typeof predicate_or_policy
3657
3734
  );
3658
3735
  }
3659
3736
  internal.autoclose = predicate;