arvo-event-handler 3.0.10 → 3.0.13

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.
@@ -25,9 +25,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.setupArvoMachine = setupArvoMachine;
27
27
  var arvo_core_1 = require("arvo-core");
28
- var uuid_1 = require("uuid");
29
28
  var xstate_1 = require("xstate");
30
29
  var _1 = __importDefault(require("."));
30
+ var servicesValidation_1 = require("../ArvoOrchestrationUtils/servicesValidation");
31
31
  var object_1 = require("../utils/object");
32
32
  var utils_1 = require("./utils");
33
33
  /**
@@ -194,8 +194,7 @@ var utils_1 = require("./utils");
194
194
  * 4. Using XState features like `emit` bound with Arvo contracts for event emitting and event handling via transitions
195
195
  */
196
196
  function setupArvoMachine(param) {
197
- var _a;
198
- var _b, _c;
197
+ var _a, _b;
199
198
  var createConfigErrorMessage = function (type) {
200
199
  return (0, arvo_core_1.cleanString)("\n Configuration Error: '".concat(type, "' not supported in Arvo machines\n \n Arvo machines do not support XState ").concat(type === 'actor' ? 'actors' : 'delay transitions', " as they introduce asynchronous behavior.\n \n To fix:\n 1. Remove the '").concat(type, "' configuration\n 2. Use Arvo's event-driven patterns instead for asynchronous operations\n "));
201
200
  };
@@ -205,18 +204,11 @@ function setupArvoMachine(param) {
205
204
  if (param.delays) {
206
205
  throw new Error(createConfigErrorMessage('delays'));
207
206
  }
208
- if ((_b = param.actions) === null || _b === void 0 ? void 0 : _b.enqueueArvoEvent) {
207
+ if ((_a = param.actions) === null || _a === void 0 ? void 0 : _a.enqueueArvoEvent) {
209
208
  throw new Error((0, arvo_core_1.cleanString)("\n Configuration Error: Reserved action name 'enqueueArvoEvent'\n \n 'enqueueArvoEvent' is an internal Arvo system action and cannot be overridden.\n \n To fix: Use a different name for your action, such as:\n - 'queueCustomEvent'\n - 'scheduleEvent'\n - 'dispatchEvent'\n "));
210
209
  }
211
- var __areServiceContractsUnique = (0, utils_1.areServiceContractsUnique)(param.contracts.services);
212
- if (!__areServiceContractsUnique.result) {
213
- throw new Error("The service contracts must have unique URIs. Multiple versions of the same contract are not allow. The contracts '".concat(__areServiceContractsUnique.keys[0], "' and '").concat(__areServiceContractsUnique.keys[1], "' have the same URI '").concat(__areServiceContractsUnique.contractUri, "'"));
214
- }
215
- var __checkIfSelfIsAService = (0, utils_1.areServiceContractsUnique)(__assign(__assign({}, param.contracts.services), (_a = {}, _a[(0, uuid_1.v4)()] = param.contracts.self, _a)));
216
- if (!__checkIfSelfIsAService.result) {
217
- throw new Error("Circular dependency detected: Machine with URI '".concat(param.contracts.self.uri, "' is registered as service '").concat(__checkIfSelfIsAService.keys[1], "'. Self-referential services create execution loops and are prohibited."));
218
- }
219
- var combinedActions = __assign(__assign({}, ((_c = param.actions) !== null && _c !== void 0 ? _c : {})), { enqueueArvoEvent: (0, xstate_1.assign)(function (_a, param) {
210
+ (0, servicesValidation_1.servicesValidation)(param.contracts, 'machine');
211
+ var combinedActions = __assign(__assign({}, ((_b = param.actions) !== null && _b !== void 0 ? _b : {})), { enqueueArvoEvent: (0, xstate_1.assign)(function (_a, param) {
220
212
  var _b, _c, _d, _e, _f;
221
213
  var context = _a.context;
222
214
  return (__assign(__assign({}, (context !== null && context !== void 0 ? context : {})), { arvo$$: __assign(__assign({}, ((_b = context === null || context === void 0 ? void 0 : context.arvo$$) !== null && _b !== void 0 ? _b : {})), { volatile$$: __assign(__assign({}, ((_d = (_c = context === null || context === void 0 ? void 0 : context.arvo$$) === null || _c === void 0 ? void 0 : _c.volatile$$) !== null && _d !== void 0 ? _d : {})), { eventQueue$$: __spreadArray(__spreadArray([], (((_f = (_e = context === null || context === void 0 ? void 0 : context.arvo$$) === null || _e === void 0 ? void 0 : _e.volatile$$) === null || _f === void 0 ? void 0 : _f.eventQueue$$) || []), true), [param], false) }) }) }));
@@ -1,4 +1,3 @@
1
- import type { ArvoContract, VersionedArvoContract } from 'arvo-core';
2
1
  import type { MachineConfig } from 'xstate';
3
2
  /**
4
3
  * Detects if an XState machine configuration contains any parallel states.
@@ -22,19 +21,3 @@ import type { MachineConfig } from 'xstate';
22
21
  * const hasParallel = detectParallelStates(machine) // Returns true
23
22
  */
24
23
  export declare const detectParallelStates: (config?: MachineConfig<any, any, any, any, any, any, any, any, any, any, any>) => boolean;
25
- /**
26
- * Validates that all service contracts in a collection have unique URIs.
27
- *
28
- * Iterates through the provided contracts and checks if any URI appears more than once.
29
- * Multiple versions of the same contract (with the same URI) are not allowed.
30
- *
31
- * @param contracts - A record mapping contract keys to their respective ArvoContract objects
32
- * @returns An object with a boolean result indicating if all contracts are unique, and the error keys if not
33
- */
34
- export declare const areServiceContractsUnique: (contracts: Record<string, ArvoContract | VersionedArvoContract<any, any>>) => {
35
- result: false;
36
- keys: [string, string];
37
- contractUri: string;
38
- } | {
39
- result: true;
40
- };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.areServiceContractsUnique = exports.detectParallelStates = void 0;
3
+ exports.detectParallelStates = void 0;
4
4
  /**
5
5
  * Detects if an XState machine configuration contains any parallel states.
6
6
  * Uses a stack-based approach for efficient traversal of the state hierarchy.
@@ -41,30 +41,3 @@ var detectParallelStates = function (config) {
41
41
  return false;
42
42
  };
43
43
  exports.detectParallelStates = detectParallelStates;
44
- /**
45
- * Validates that all service contracts in a collection have unique URIs.
46
- *
47
- * Iterates through the provided contracts and checks if any URI appears more than once.
48
- * Multiple versions of the same contract (with the same URI) are not allowed.
49
- *
50
- * @param contracts - A record mapping contract keys to their respective ArvoContract objects
51
- * @returns An object with a boolean result indicating if all contracts are unique, and the error keys if not
52
- */
53
- var areServiceContractsUnique = function (contracts) {
54
- var uriToKeyMap = {};
55
- for (var _i = 0, _a = Object.entries(contracts); _i < _a.length; _i++) {
56
- var _b = _a[_i], key = _b[0], contract = _b[1];
57
- if (uriToKeyMap[contract.uri]) {
58
- return {
59
- result: false,
60
- keys: [key, uriToKeyMap[contract.uri]],
61
- contractUri: contract.uri,
62
- };
63
- }
64
- uriToKeyMap[contract.uri] = key;
65
- }
66
- return {
67
- result: true,
68
- };
69
- };
70
- exports.areServiceContractsUnique = areServiceContractsUnique;
@@ -22,7 +22,7 @@ export type CreateSystemErrorEventsParams = {
22
22
  /**
23
23
  * Creates system error events
24
24
  */
25
- export declare const createSystemErrorEvents: ({ error, event, otelHeaders, orchestrationParentSubject, initEventId, selfContract, systemErrorDomain, executionunits, source, domain, handlerType, }: CreateSystemErrorEventsParams & {
25
+ export declare const createSystemErrorEvents: ({ error, event, otelHeaders, orchestrationParentSubject: _orchestrationParentSubject, initEventId, selfContract, systemErrorDomain, executionunits, source, domain, handlerType, }: CreateSystemErrorEventsParams & {
26
26
  error: Error;
27
27
  }) => ArvoEvent[];
28
28
  export declare const handleOrchestrationErrors: (_handlerType: ArvoOrchestrationHandlerType, param: CreateSystemErrorEventsParams & {
@@ -58,22 +58,26 @@ var types_1 = require("./types");
58
58
  * Creates system error events
59
59
  */
60
60
  var createSystemErrorEvents = function (_a) {
61
- var _b, _c, _d, _e;
62
- var error = _a.error, event = _a.event, otelHeaders = _a.otelHeaders, orchestrationParentSubject = _a.orchestrationParentSubject, initEventId = _a.initEventId, selfContract = _a.selfContract, systemErrorDomain = _a.systemErrorDomain, executionunits = _a.executionunits, source = _a.source, domain = _a.domain, handlerType = _a.handlerType;
61
+ var _b, _c, _d, _e, _f;
62
+ var error = _a.error, event = _a.event, otelHeaders = _a.otelHeaders, _orchestrationParentSubject = _a.orchestrationParentSubject, initEventId = _a.initEventId, selfContract = _a.selfContract, systemErrorDomain = _a.systemErrorDomain, executionunits = _a.executionunits, source = _a.source, domain = _a.domain, handlerType = _a.handlerType;
63
63
  // In case of none transaction errors like errors from
64
64
  // the machine or the event creation etc, the are workflow
65
65
  // error and shuold be handled by the workflow. Then are
66
66
  // called system error and must be sent
67
67
  // to the initiator. In as good of a format as possible
68
68
  var parsedEventSubject = null;
69
- try {
70
- parsedEventSubject = arvo_core_1.ArvoOrchestrationSubject.parse(event.subject);
71
- }
72
- catch (e) {
73
- (0, arvo_core_1.logToSpan)({
74
- level: 'WARNING',
75
- message: "Unable to parse event subject: ".concat(e.message),
76
- });
69
+ var orchestrationParentSubject = null;
70
+ if (handlerType === 'orchestrator' || handlerType === 'resumable') {
71
+ orchestrationParentSubject = _orchestrationParentSubject;
72
+ try {
73
+ parsedEventSubject = arvo_core_1.ArvoOrchestrationSubject.parse(event.subject);
74
+ }
75
+ catch (e) {
76
+ (0, arvo_core_1.logToSpan)({
77
+ level: 'WARNING',
78
+ message: "Unable to parse event subject: ".concat(e.message),
79
+ });
80
+ }
77
81
  }
78
82
  var domainSets = new Set(systemErrorDomain
79
83
  ? systemErrorDomain.map(function (item) {
@@ -86,8 +90,8 @@ var createSystemErrorEvents = function (_a) {
86
90
  })
87
91
  : [event.domain, domain, null]);
88
92
  var result = [];
89
- for (var _i = 0, _f = Array.from(domainSets); _i < _f.length; _i++) {
90
- var _dom = _f[_i];
93
+ for (var _i = 0, _g = Array.from(domainSets); _i < _g.length; _i++) {
94
+ var _dom = _g[_i];
91
95
  var factoryBuilder = handlerType === 'handler' ? arvo_core_1.createArvoEventFactory : arvo_core_1.createArvoOrchestratorEventFactory;
92
96
  result.push(factoryBuilder(selfContract).systemError({
93
97
  source: source,
@@ -98,11 +102,11 @@ var createSystemErrorEvents = function (_a) {
98
102
  subject: orchestrationParentSubject !== null && orchestrationParentSubject !== void 0 ? orchestrationParentSubject : event.subject,
99
103
  // The system error must always go back to
100
104
  // the source which initiated it
101
- to: (_b = parsedEventSubject === null || parsedEventSubject === void 0 ? void 0 : parsedEventSubject.execution.initiator) !== null && _b !== void 0 ? _b : event.source,
105
+ to: (_c = (_b = parsedEventSubject === null || parsedEventSubject === void 0 ? void 0 : parsedEventSubject.execution) === null || _b === void 0 ? void 0 : _b.initiator) !== null && _c !== void 0 ? _c : event.source,
102
106
  error: error,
103
- traceparent: (_c = otelHeaders.traceparent) !== null && _c !== void 0 ? _c : undefined,
104
- tracestate: (_d = otelHeaders.tracestate) !== null && _d !== void 0 ? _d : undefined,
105
- accesscontrol: (_e = event.accesscontrol) !== null && _e !== void 0 ? _e : undefined,
107
+ traceparent: (_d = otelHeaders.traceparent) !== null && _d !== void 0 ? _d : undefined,
108
+ tracestate: (_e = otelHeaders.tracestate) !== null && _e !== void 0 ? _e : undefined,
109
+ accesscontrol: (_f = event.accesscontrol) !== null && _f !== void 0 ? _f : undefined,
106
110
  executionunits: executionunits,
107
111
  // If there is initEventID then use that.
108
112
  // Otherwise, use event id. If the error is in init event
@@ -105,7 +105,7 @@ var ArvoResumable = /** @class */ (function () {
105
105
  this.contracts = param.contracts;
106
106
  this.handler = param.handler;
107
107
  this.systemErrorDomain = param.systemErrorDomain;
108
- this.spanOptions = __assign(__assign({ kind: api_1.SpanKind.PRODUCER }, param.spanOptions), { attributes: __assign(__assign((_a = {}, _a[arvo_core_1.ArvoExecution.ATTR_SPAN_KIND] = arvo_core_1.ArvoExecutionSpanKind.ORCHESTRATOR, _a[arvo_core_1.OpenInference.ATTR_SPAN_KIND] = arvo_core_1.OpenInferenceSpanKind.CHAIN, _a), ((_d = (_c = param.spanOptions) === null || _c === void 0 ? void 0 : _c.attributes) !== null && _d !== void 0 ? _d : {})), { 'arvo.handler.source': this.source, 'arvo.contract.uri': this.contracts.self.uri }) });
108
+ this.spanOptions = __assign(__assign({ kind: api_1.SpanKind.PRODUCER }, param.spanOptions), { attributes: __assign(__assign((_a = {}, _a[arvo_core_1.ArvoExecution.ATTR_SPAN_KIND] = arvo_core_1.ArvoExecutionSpanKind.RESUMABLE, _a[arvo_core_1.OpenInference.ATTR_SPAN_KIND] = arvo_core_1.OpenInferenceSpanKind.CHAIN, _a), ((_d = (_c = param.spanOptions) === null || _c === void 0 ? void 0 : _c.attributes) !== null && _d !== void 0 ? _d : {})), { 'arvo.handler.source': this.source, 'arvo.contract.uri': this.contracts.self.uri }) });
109
109
  }
110
110
  Object.defineProperty(ArvoResumable.prototype, "requiresResourceLocking", {
111
111
  get: function () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arvo-event-handler",
3
- "version": "3.0.10",
3
+ "version": "3.0.13",
4
4
  "description": "Type-safe event handler system with versioning, telemetry, and contract validation for distributed Arvo event-driven architectures, featuring routing and multi-handler support.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -46,7 +46,7 @@
46
46
  "dependencies": {
47
47
  "@opentelemetry/api": "^1.9.0",
48
48
  "@opentelemetry/core": "^1.30.1",
49
- "arvo-core": "^3.0.10",
49
+ "arvo-core": "^3.0.13",
50
50
  "uuid": "^11.1.0",
51
51
  "xstate": "^5.23.0",
52
52
  "zod": "^3.25.74",