@rotorsoft/act 2.1.14 → 2.1.16

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 (36) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/@types/act.d.ts.map +1 -1
  3. package/dist/@types/builders/event-builder.d.ts +3 -11
  4. package/dist/@types/builders/event-builder.d.ts.map +1 -1
  5. package/dist/@types/internal/sensitive.d.ts +4 -2
  6. package/dist/@types/internal/sensitive.d.ts.map +1 -1
  7. package/dist/@types/internal/zod.d.ts +6 -0
  8. package/dist/@types/internal/zod.d.ts.map +1 -1
  9. package/dist/@types/types/act.d.ts +4 -3
  10. package/dist/@types/types/act.d.ts.map +1 -1
  11. package/dist/@types/types/registry.d.ts +2 -2
  12. package/dist/adapter.cjs +3 -3
  13. package/dist/adapter.js +2 -2
  14. package/dist/{chunk-WDIBO6AW.cjs → chunk-3WHD4VPP.cjs} +54 -54
  15. package/dist/{chunk-WDIBO6AW.cjs.map → chunk-3WHD4VPP.cjs.map} +1 -1
  16. package/dist/{chunk-SCH4GLWM.js → chunk-CWWTVYIN.js} +3 -3
  17. package/dist/{chunk-EIGZVFT7.js → chunk-GHMCESJA.js} +2 -2
  18. package/dist/{chunk-GDPBPAML.js → chunk-KAUON2M3.js} +1 -1
  19. package/dist/chunk-KAUON2M3.js.map +1 -0
  20. package/dist/{chunk-UEGKXCSQ.cjs → chunk-LA2BPKNS.cjs} +1 -1
  21. package/dist/chunk-LA2BPKNS.cjs.map +1 -0
  22. package/dist/{chunk-DCWPJFXR.cjs → chunk-QITJ63HG.cjs} +11 -11
  23. package/dist/{chunk-DCWPJFXR.cjs.map → chunk-QITJ63HG.cjs.map} +1 -1
  24. package/dist/index.cjs +504 -503
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.js +15 -14
  27. package/dist/index.js.map +1 -1
  28. package/dist/test/index.cjs +9 -9
  29. package/dist/test/index.js +3 -3
  30. package/dist/types/index.cjs +2 -2
  31. package/dist/types/index.js +1 -1
  32. package/package.json +2 -2
  33. package/dist/chunk-GDPBPAML.js.map +0 -1
  34. package/dist/chunk-UEGKXCSQ.cjs.map +0 -1
  35. /package/dist/{chunk-SCH4GLWM.js.map → chunk-CWWTVYIN.js.map} +0 -0
  36. /package/dist/{chunk-EIGZVFT7.js.map → chunk-GHMCESJA.js.map} +0 -0
@@ -3,13 +3,13 @@
3
3
 
4
4
 
5
5
 
6
- var _chunkUEGKXCSQcjs = require('./chunk-UEGKXCSQ.cjs');
6
+ var _chunkLA2BPKNScjs = require('./chunk-LA2BPKNS.cjs');
7
7
 
8
8
  // src/adapter.ts
9
9
  function isStreamName(source, target) {
10
10
  return source === target || !source.startsWith("^") && !source.endsWith("$");
11
11
  }
12
- _chunkUEGKXCSQcjs.__name.call(void 0, isStreamName, "isStreamName");
12
+ _chunkLA2BPKNScjs.__name.call(void 0, isStreamName, "isStreamName");
13
13
  function checkAppends(appends) {
14
14
  const seen = /* @__PURE__ */ new Set();
15
15
  for (const { stream, msgs } of appends) {
@@ -20,33 +20,33 @@ function checkAppends(appends) {
20
20
  seen.add(stream);
21
21
  }
22
22
  }
23
- _chunkUEGKXCSQcjs.__name.call(void 0, checkAppends, "checkAppends");
23
+ _chunkLA2BPKNScjs.__name.call(void 0, checkAppends, "checkAppends");
24
24
  function concurrencyError(append, head) {
25
- return new (0, _chunkUEGKXCSQcjs.ConcurrencyError)(
25
+ return new (0, _chunkLA2BPKNScjs.ConcurrencyError)(
26
26
  append.stream,
27
27
  head,
28
28
  append.msgs,
29
29
  _nullishCoalesce(append.expectedVersion, () => ( -1))
30
30
  );
31
31
  }
32
- _chunkUEGKXCSQcjs.__name.call(void 0, concurrencyError, "concurrencyError");
32
+ _chunkLA2BPKNScjs.__name.call(void 0, concurrencyError, "concurrencyError");
33
33
  function checkVersion(append, head) {
34
34
  if (typeof append.expectedVersion === "number" && head !== append.expectedVersion)
35
35
  throw concurrencyError(append, head);
36
36
  }
37
- _chunkUEGKXCSQcjs.__name.call(void 0, checkVersion, "checkVersion");
37
+ _chunkLA2BPKNScjs.__name.call(void 0, checkVersion, "checkVersion");
38
38
  function seedEvent(snapshot, meta) {
39
39
  return {
40
- name: snapshot !== void 0 ? _chunkUEGKXCSQcjs.SNAP_EVENT : _chunkUEGKXCSQcjs.TOMBSTONE_EVENT,
40
+ name: snapshot !== void 0 ? _chunkLA2BPKNScjs.SNAP_EVENT : _chunkLA2BPKNScjs.TOMBSTONE_EVENT,
41
41
  data: _nullishCoalesce(snapshot, () => ( {})),
42
42
  meta: _nullishCoalesce(meta, () => ( { correlation: "", causation: {} }))
43
43
  };
44
44
  }
45
- _chunkUEGKXCSQcjs.__name.call(void 0, seedEvent, "seedEvent");
45
+ _chunkLA2BPKNScjs.__name.call(void 0, seedEvent, "seedEvent");
46
46
  function fairSlots(lagging) {
47
47
  return lagging >= 2 ? Math.max(1, Math.floor(lagging / 4)) : 0;
48
48
  }
49
- _chunkUEGKXCSQcjs.__name.call(void 0, fairSlots, "fairSlots");
49
+ _chunkLA2BPKNScjs.__name.call(void 0, fairSlots, "fairSlots");
50
50
  function parseNotification(raw) {
51
51
  if (!raw || typeof raw !== "object") return void 0;
52
52
  const { stream, events } = raw;
@@ -56,7 +56,7 @@ function parseNotification(raw) {
56
56
  );
57
57
  return kept.length ? { stream, events: kept.map(({ id, name }) => ({ id, name })) } : void 0;
58
58
  }
59
- _chunkUEGKXCSQcjs.__name.call(void 0, parseNotification, "parseNotification");
59
+ _chunkLA2BPKNScjs.__name.call(void 0, parseNotification, "parseNotification");
60
60
 
61
61
 
62
62
 
@@ -67,4 +67,4 @@ _chunkUEGKXCSQcjs.__name.call(void 0, parseNotification, "parseNotification");
67
67
 
68
68
 
69
69
  exports.isStreamName = isStreamName; exports.checkAppends = checkAppends; exports.concurrencyError = concurrencyError; exports.checkVersion = checkVersion; exports.seedEvent = seedEvent; exports.fairSlots = fairSlots; exports.parseNotification = parseNotification;
70
- //# sourceMappingURL=chunk-DCWPJFXR.cjs.map
70
+ //# sourceMappingURL=chunk-QITJ63HG.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["/home/runner/work/act-root/act-root/libs/act/dist/chunk-DCWPJFXR.cjs","../src/adapter.ts"],"names":[],"mappings":"AAAA;AACE;AACA;AACA;AACA;AACF,wDAA6B;AAC7B;AACA;ACmCO,SAAS,YAAA,CAAa,MAAA,EAAgB,MAAA,EAA0B;AACrE,EAAA,OACE,OAAA,IAAW,OAAA,GAAW,CAAC,MAAA,CAAO,UAAA,CAAW,GAAG,EAAA,GAAK,CAAC,MAAA,CAAO,QAAA,CAAS,GAAG,CAAA;AAEzE;AAJgB,sCAAA,YAAA,EAAA,cAAA,CAAA;AAUT,SAAS,YAAA,CACd,OAAA,EAIM;AACN,EAAA,MAAM,KAAA,kBAAO,IAAI,GAAA,CAAY,CAAA;AAC7B,EAAA,IAAA,CAAA,MAAW,EAAE,MAAA,EAAQ,KAAK,EAAA,GAAK,OAAA,EAAS;AACtC,IAAA,GAAA,CAAI,CAAC,IAAA,CAAK,MAAA;AACR,MAAA,MAAM,IAAI,KAAA,CAAM,CAAA,uBAAA,EAA0B,MAAM,CAAA,eAAA,CAAiB,CAAA;AACnE,IAAA,GAAA,CAAI,IAAA,CAAK,GAAA,CAAI,MAAM,CAAA;AACjB,MAAA,MAAM,IAAI,KAAA,CAAM,CAAA,gBAAA,EAAmB,MAAM,CAAA,gBAAA,CAAkB,CAAA;AAC7D,IAAA,IAAA,CAAK,GAAA,CAAI,MAAM,CAAA;AAAA,EACjB;AACF;AAdgB,sCAAA,YAAA,EAAA,cAAA,CAAA;AAqBT,SAAS,gBAAA,CACd,MAAA,EAKA,IAAA,EACkB;AAClB,EAAA,OAAO,IAAI,uCAAA;AAAA,IACT,MAAA,CAAO,MAAA;AAAA,IACP,IAAA;AAAA,IACA,MAAA,CAAO,IAAA;AAAA,qBACP,MAAA,CAAO,eAAA,UAAmB,CAAA;AAAA,EAC5B,CAAA;AACF;AAdgB,sCAA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AA,EAAA,GACC,CAAC,CAAC,EAAA,GACF,OAAO,EAAA,IAAM,SAAA,GACb,OAAQ,CAAA,CAAuB,GAAA,IAAO,SAAA,GACtC,OAAQ,CAAA,CAAyB,KAAA,IAAS;AAAA,EAC9C,CAAA;AACA,EAAA,OAAO,IAAA,CAAK,OAAA,EACR,EAAE,MAAA,EAAQ,MAAA,EAAQ,IAAA,CAAK,GAAA,CAAI,CAAC,EAAE,EAAA,EAAI,KAAK,CAAA,EAAA,GAAA,CAAO,EAAE,EAAA,EAAI,KAAK,CAAA,CAAE,EAAE,EAAA,EAC7D,KAAA,CAAA;AACN;AAdgB,sCAAA,iBAAA,EAAA,mBAAA,CAAA;ADjFhB;AACA;AACE;AACA;AACA;AACA;AACA;AACA;AACA;AACF,wQAAC","file":"/home/runner/work/act-root/act-root/libs/act/dist/chunk-DCWPJFXR.cjs","sourcesContent":[null,"/**\n * @packageDocumentation\n * @module act/adapter\n *\n * The rules a `Store` implementation calls, so each one lives once in core and\n * a third-party store can't drift from it. Each is a pure function of its\n * arguments. Import them by name, or as\n * `import * as adapter from \"@rotorsoft/act/adapter\"`. Writing a store:\n * https://rotorsoft.github.io/act-root/docs/guides/writing-a-store\n */\nimport {\n ConcurrencyError,\n type EventMeta,\n type Message,\n type Schema,\n type Schemas,\n SNAP_EVENT,\n type StoreNotification,\n TOMBSTONE_EVENT,\n} from \"./types/index.js\";\n\n/**\n * True when `source` is a stream's **name**, matched exactly. Only an anchored\n * source, one that starts with `^` or ends with `$`, is a **pattern** and gets\n * compiled as a RegExp. A name can hold any other character, so\n * `user+tag@example.com` and `order(1)` are names, and a pattern always says\n * that it is one.\n *\n * Pass the subscription's `target` too when it's known: a target reading itself\n * holds a stream's name, whatever characters that name has.\n *\n * The single source of truth for this across correlate (which decides what a\n * source marks), the drain fetch path, the close check, and the adapters, so\n * they all agree.\n *\n * @example\n * ```typescript\n * isStreamName(\"Board\"); // → true (exact lookup)\n * isStreamName(\"a+b\"); // → true (exact lookup)\n * isStreamName(\"^(A|B)$\"); // → false (compile as RegExp)\n * ```\n */\nexport function isStreamName(source: string, target?: string): boolean {\n return (\n source === target || (!source.startsWith(\"^\") && !source.endsWith(\"$\"))\n );\n}\n\n/**\n * What every store refuses in a `commit`: an append with no events, and a\n * stream named twice. In core so each adapter calls the one rule.\n */\nexport function checkAppends(\n appends: readonly {\n readonly stream: string;\n readonly msgs: readonly unknown[];\n }[]\n): void {\n const seen = new Set<string>();\n for (const { stream, msgs } of appends) {\n if (!msgs.length)\n throw new Error(`commit: the append to \"${stream}\" has no events`);\n if (seen.has(stream))\n throw new Error(`commit: stream \"${stream}\" is named twice`);\n seen.add(stream);\n }\n}\n\n/**\n * The error a stale append raises, given the head the store found. A writer\n * that named no `expectedVersion` reads as `-1`: only a race between the\n * version check and the insert gets one here.\n */\nexport function concurrencyError(\n append: {\n readonly stream: string;\n readonly msgs: readonly unknown[];\n readonly expectedVersion?: number;\n },\n head: number\n): ConcurrencyError {\n return new ConcurrencyError(\n append.stream,\n head,\n append.msgs as Message<Schemas, keyof Schemas>[],\n append.expectedVersion ?? -1\n );\n}\n\n/**\n * What every store refuses as a stale writer: an append whose\n * `expectedVersion` is not the stream's head. In core so each adapter calls\n * the one rule before it appends anything.\n */\nexport function checkVersion(\n append: {\n readonly stream: string;\n readonly msgs: readonly unknown[];\n readonly expectedVersion?: number;\n },\n head: number\n): void {\n if (\n typeof append.expectedVersion === \"number\" &&\n head !== append.expectedVersion\n )\n throw concurrencyError(append, head);\n}\n\n/**\n * The event a full `truncate` seeds a stream with at version 0: a snapshot\n * of the given state, or a tombstone when there is none.\n */\nexport function seedEvent(\n snapshot?: Schema,\n meta?: EventMeta\n): { name: string; data: Schema; meta: EventMeta } {\n return {\n name: snapshot !== undefined ? SNAP_EVENT : TOMBSTONE_EVENT,\n data: snapshot ?? {},\n meta: meta ?? { correlation: \"\", causation: {} },\n };\n}\n\n/**\n * How many of `lagging` claim slots ignore priority, so sustained high-priority\n * load can't starve a low-priority stream forever. Each store's `claim` calls\n * it, and the rule stays out of the `claim` signature.\n */\nexport function fairSlots(lagging: number): number {\n return lagging >= 2 ? Math.max(1, Math.floor(lagging / 4)) : 0;\n}\n\n/**\n * The commit notification in a payload a store or broker received, or\n * `undefined` when there is none: `stream` must be a string, and only\n * `{ id: number, name: string }` events are kept, stripped of anything else.\n * A payload with no such event is not a notification. In core so every\n * listener reads a foreign or version-skewed payload the same way.\n */\nexport function parseNotification(raw: unknown): StoreNotification | undefined {\n if (!raw || typeof raw !== \"object\") return undefined;\n const { stream, events } = raw as { stream?: unknown; events?: unknown };\n if (typeof stream !== \"string\" || !Array.isArray(events)) return undefined;\n const kept = events.filter(\n (e): e is { id: number; name: string } =>\n !!e &&\n typeof e === \"object\" &&\n typeof (e as { id?: unknown }).id === \"number\" &&\n typeof (e as { name?: unknown }).name === \"string\"\n );\n return kept.length\n ? { stream, events: kept.map(({ id, name }) => ({ id, name })) }\n : undefined;\n}\n"]}
1
+ {"version":3,"sources":["/home/runner/work/act-root/act-root/libs/act/dist/chunk-QITJ63HG.cjs","../src/adapter.ts"],"names":[],"mappings":"AAAA;AACE;AACA;AACA;AACA;AACF,wDAA6B;AAC7B;AACA;ACmCO,SAAS,YAAA,CAAa,MAAA,EAAgB,MAAA,EAA0B;AACrE,EAAA,OACE,OAAA,IAAW,OAAA,GAAW,CAAC,MAAA,CAAO,UAAA,CAAW,GAAG,EAAA,GAAK,CAAC,MAAA,CAAO,QAAA,CAAS,GAAG,CAAA;AAEzE;AAJgB,sCAAA,YAAA,EAAA,cAAA,CAAA;AAUT,SAAS,YAAA,CACd,OAAA,EAIM;AACN,EAAA,MAAM,KAAA,kBAAO,IAAI,GAAA,CAAY,CAAA;AAC7B,EAAA,IAAA,CAAA,MAAW,EAAE,MAAA,EAAQ,KAAK,EAAA,GAAK,OAAA,EAAS;AACtC,IAAA,GAAA,CAAI,CAAC,IAAA,CAAK,MAAA;AACR,MAAA,MAAM,IAAI,KAAA,CAAM,CAAA,uBAAA,EA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K,GAAA,CAAI,CAAA,EAAG,IAAA,CAAK,KAAA,CAAM,QAAA,EAAU,CAAC,CAAC,EAAA,EAAI,CAAA;AAC/D;AAFgB,sCAAA,SAAA,EAAA,WAAA,CAAA;AAWT,SAAS,iBAAA,CAAkB,GAAA,EAA6C;AAC7E,EAAA,GAAA,CAAI,CAAC,IAAA,GAAO,OAAO,IAAA,IAAQ,QAAA,EAAU,OAAO,KAAA,CAAA;AAC5C,EAAA,MAAM,EAAE,MAAA,EAAQ,OAAO,EAAA,EAAI,GAAA;AAC3B,EAAA,GAAA,CAAI,OAAO,OAAA,IAAW,SAAA,GAAY,CAAC,KAAA,CAAM,OAAA,CAAQ,MAAM,CAAA,EAAG,OAAO,KAAA,CAAA;AACjE,EAAA,MAAM,KAAA,EAAO,MAAA,CAAO,MAAA;AAAA,IAClB,CAAC,CAAA,EAAA,GACC,CAAC,CAAC,EAAA,GACF,OAAO,EAAA,IAAM,SAAA,GACb,OAAQ,CAAA,CAAuB,GAAA,IAAO,SAAA,GACtC,OAAQ,CAAA,CAAyB,KAAA,IAAS;AAAA,EAC9C,CAAA;AACA,EAAA,OAAO,IAAA,CAAK,OAAA,EACR,EAAE,MAAA,EAAQ,MAAA,EAAQ,IAAA,CAAK,GAAA,CAAI,CAAC,EAAE,EAAA,EAAI,KAAK,CAAA,EAAA,GAAA,CAAO,EAAE,EAAA,EAAI,KAAK,CAAA,CAAE,EAAE,EAAA,EAC7D,KAAA,CAAA;AACN;AAdgB,sCAAA,iBAAA,EAAA,mBAAA,CAAA;ADjFhB;AACA;AACE;AACA;AACA;AACA;AACA;AACA;AACA;AACF,wQAAC","file":"/home/runner/work/act-root/act-root/libs/act/dist/chunk-QITJ63HG.cjs","sourcesContent":[null,"/**\n * @packageDocumentation\n * @module act/adapter\n *\n * The rules a `Store` implementation calls, so each one lives once in core and\n * a third-party store can't drift from it. Each is a pure function of its\n * arguments. Import them by name, or as\n * `import * as adapter from \"@rotorsoft/act/adapter\"`. Writing a store:\n * https://rotorsoft.github.io/act-root/docs/guides/writing-a-store\n */\nimport {\n ConcurrencyError,\n type EventMeta,\n type Message,\n type Schema,\n type Schemas,\n SNAP_EVENT,\n type StoreNotification,\n TOMBSTONE_EVENT,\n} from \"./types/index.js\";\n\n/**\n * True when `source` is a stream's **name**, matched exactly. Only an anchored\n * source, one that starts with `^` or ends with `$`, is a **pattern** and gets\n * compiled as a RegExp. A name can hold any other character, so\n * `user+tag@example.com` and `order(1)` are names, and a pattern always says\n * that it is one.\n *\n * Pass the subscription's `target` too when it's known: a target reading itself\n * holds a stream's name, whatever characters that name has.\n *\n * The single source of truth for this across correlate (which decides what a\n * source marks), the drain fetch path, the close check, and the adapters, so\n * they all agree.\n *\n * @example\n * ```typescript\n * isStreamName(\"Board\"); // → true (exact lookup)\n * isStreamName(\"a+b\"); // → true (exact lookup)\n * isStreamName(\"^(A|B)$\"); // → false (compile as RegExp)\n * ```\n */\nexport function isStreamName(source: string, target?: string): boolean {\n return (\n source === target || (!source.startsWith(\"^\") && !source.endsWith(\"$\"))\n );\n}\n\n/**\n * What every store refuses in a `commit`: an append with no events, and a\n * stream named twice. In core so each adapter calls the one rule.\n */\nexport function checkAppends(\n appends: readonly {\n readonly stream: string;\n readonly msgs: readonly unknown[];\n }[]\n): void {\n const seen = new Set<string>();\n for (const { stream, msgs } of appends) {\n if (!msgs.length)\n throw new Error(`commit: the append to \"${stream}\" has no events`);\n if (seen.has(stream))\n throw new Error(`commit: stream \"${stream}\" is named twice`);\n seen.add(stream);\n }\n}\n\n/**\n * The error a stale append raises, given the head the store found. A writer\n * that named no `expectedVersion` reads as `-1`: only a race between the\n * version check and the insert gets one here.\n */\nexport function concurrencyError(\n append: {\n readonly stream: string;\n readonly msgs: readonly unknown[];\n readonly expectedVersion?: number;\n },\n head: number\n): ConcurrencyError {\n return new ConcurrencyError(\n append.stream,\n head,\n append.msgs as Message<Schemas, keyof Schemas>[],\n append.expectedVersion ?? -1\n );\n}\n\n/**\n * What every store refuses as a stale writer: an append whose\n * `expectedVersion` is not the stream's head. In core so each adapter calls\n * the one rule before it appends anything.\n */\nexport function checkVersion(\n append: {\n readonly stream: string;\n readonly msgs: readonly unknown[];\n readonly expectedVersion?: number;\n },\n head: number\n): void {\n if (\n typeof append.expectedVersion === \"number\" &&\n head !== append.expectedVersion\n )\n throw concurrencyError(append, head);\n}\n\n/**\n * The event a full `truncate` seeds a stream with at version 0: a snapshot\n * of the given state, or a tombstone when there is none.\n */\nexport function seedEvent(\n snapshot?: Schema,\n meta?: EventMeta\n): { name: string; data: Schema; meta: EventMeta } {\n return {\n name: snapshot !== undefined ? SNAP_EVENT : TOMBSTONE_EVENT,\n data: snapshot ?? {},\n meta: meta ?? { correlation: \"\", causation: {} },\n };\n}\n\n/**\n * How many of `lagging` claim slots ignore priority, so sustained high-priority\n * load can't starve a low-priority stream forever. Each store's `claim` calls\n * it, and the rule stays out of the `claim` signature.\n */\nexport function fairSlots(lagging: number): number {\n return lagging >= 2 ? Math.max(1, Math.floor(lagging / 4)) : 0;\n}\n\n/**\n * The commit notification in a payload a store or broker received, or\n * `undefined` when there is none: `stream` must be a string, and only\n * `{ id: number, name: string }` events are kept, stripped of anything else.\n * A payload with no such event is not a notification. In core so every\n * listener reads a foreign or version-skewed payload the same way.\n */\nexport function parseNotification(raw: unknown): StoreNotification | undefined {\n if (!raw || typeof raw !== \"object\") return undefined;\n const { stream, events } = raw as { stream?: unknown; events?: unknown };\n if (typeof stream !== \"string\" || !Array.isArray(events)) return undefined;\n const kept = events.filter(\n (e): e is { id: number; name: string } =>\n !!e &&\n typeof e === \"object\" &&\n typeof (e as { id?: unknown }).id === \"number\" &&\n typeof (e as { name?: unknown }).name === \"string\"\n );\n return kept.length\n ? { stream, events: kept.map(({ id, name }) => ({ id, name })) }\n : undefined;\n}\n"]}