arvo-event-handler 2.3.3 → 3.0.1

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 (78) hide show
  1. package/dist/AbstractArvoEventHandler/index.d.ts +1 -1
  2. package/dist/ArvoEventHandler/helpers.d.ts +40 -6
  3. package/dist/ArvoEventHandler/helpers.js +40 -6
  4. package/dist/ArvoEventHandler/index.d.ts +78 -49
  5. package/dist/ArvoEventHandler/index.js +151 -82
  6. package/dist/ArvoEventHandler/types.d.ts +25 -2
  7. package/dist/ArvoMachine/createMachine.d.ts +208 -0
  8. package/dist/ArvoMachine/createMachine.js +283 -0
  9. package/dist/ArvoMachine/index.d.ts +93 -0
  10. package/dist/ArvoMachine/index.js +160 -0
  11. package/dist/ArvoMachine/types.d.ts +194 -0
  12. package/dist/ArvoMachine/utils.d.ts +40 -0
  13. package/dist/ArvoMachine/utils.js +70 -0
  14. package/dist/ArvoOrchestrator/error.d.ts +16 -0
  15. package/dist/ArvoOrchestrator/error.js +43 -0
  16. package/dist/ArvoOrchestrator/factory.d.ts +28 -0
  17. package/dist/ArvoOrchestrator/factory.js +56 -0
  18. package/dist/ArvoOrchestrator/index.d.ts +69 -0
  19. package/dist/ArvoOrchestrator/index.js +597 -0
  20. package/dist/ArvoOrchestrator/types.d.ts +98 -0
  21. package/dist/ArvoResumable/factory.d.ts +50 -0
  22. package/dist/ArvoResumable/factory.js +70 -0
  23. package/dist/ArvoResumable/index.d.ts +141 -0
  24. package/dist/ArvoResumable/index.js +694 -0
  25. package/dist/ArvoResumable/types.d.ts +147 -0
  26. package/dist/ArvoResumable/types.js +2 -0
  27. package/dist/MachineExecutionEngine/index.d.ts +29 -0
  28. package/dist/MachineExecutionEngine/index.js +132 -0
  29. package/dist/MachineExecutionEngine/interface.d.ts +14 -0
  30. package/dist/MachineExecutionEngine/interface.js +2 -0
  31. package/dist/MachineExecutionEngine/types.d.ts +14 -0
  32. package/dist/MachineExecutionEngine/types.js +2 -0
  33. package/dist/MachineMemory/Simple.d.ts +51 -0
  34. package/dist/MachineMemory/Simple.js +158 -0
  35. package/dist/MachineMemory/TelemetredSimple.d.ts +51 -0
  36. package/dist/MachineMemory/TelemetredSimple.js +230 -0
  37. package/dist/MachineMemory/interface.d.ts +57 -0
  38. package/dist/MachineMemory/interface.js +2 -0
  39. package/dist/MachineMemory/utils.d.ts +1 -0
  40. package/dist/MachineMemory/utils.js +18 -0
  41. package/dist/MachineRegistry/index.d.ts +37 -0
  42. package/dist/MachineRegistry/index.js +87 -0
  43. package/dist/MachineRegistry/interface.d.ts +21 -0
  44. package/dist/MachineRegistry/interface.js +2 -0
  45. package/dist/SyncEventResource/index.d.ts +110 -0
  46. package/dist/SyncEventResource/index.js +280 -0
  47. package/dist/SyncEventResource/types.d.ts +2 -0
  48. package/dist/SyncEventResource/types.js +2 -0
  49. package/dist/index.d.ts +26 -8
  50. package/dist/index.js +39 -16
  51. package/dist/utils/SimpleEventBroker/helper.d.ts +166 -0
  52. package/dist/utils/SimpleEventBroker/helper.js +276 -0
  53. package/dist/utils/SimpleEventBroker/index.d.ts +96 -0
  54. package/dist/utils/SimpleEventBroker/index.js +259 -0
  55. package/dist/utils/SimpleEventBroker/types.d.ts +6 -0
  56. package/dist/utils/SimpleEventBroker/types.js +2 -0
  57. package/dist/utils/SimpleEventBroker/utils.d.ts +1 -0
  58. package/dist/utils/SimpleEventBroker/utils.js +10 -0
  59. package/dist/{utils.d.ts → utils/index.d.ts} +3 -36
  60. package/dist/utils/index.js +91 -0
  61. package/dist/utils/object/index.d.ts +37 -0
  62. package/dist/utils/object/index.js +63 -0
  63. package/package.json +5 -3
  64. package/dist/ArvoEventRouter/helpers.d.ts +0 -19
  65. package/dist/ArvoEventRouter/helpers.js +0 -22
  66. package/dist/ArvoEventRouter/index.d.ts +0 -89
  67. package/dist/ArvoEventRouter/index.js +0 -268
  68. package/dist/ArvoEventRouter/types.d.ts +0 -36
  69. package/dist/ArvoEventRouter/utils.d.ts +0 -30
  70. package/dist/ArvoEventRouter/utils.js +0 -43
  71. package/dist/MultiArvoEventHandler/helpers.d.ts +0 -48
  72. package/dist/MultiArvoEventHandler/helpers.js +0 -56
  73. package/dist/MultiArvoEventHandler/index.d.ts +0 -68
  74. package/dist/MultiArvoEventHandler/index.js +0 -205
  75. package/dist/MultiArvoEventHandler/types.d.ts +0 -64
  76. package/dist/utils.js +0 -191
  77. /package/dist/{ArvoEventRouter → ArvoMachine}/types.js +0 -0
  78. /package/dist/{MultiArvoEventHandler → ArvoOrchestrator}/types.js +0 -0
@@ -1,8 +1,7 @@
1
1
  import { type SpanOptions } from '@opentelemetry/api';
2
- import { type ArvoEvent, type CreateArvoEvent, type OpenTelemetryHeaders } from 'arvo-core';
3
- import type { ArvoEventHandlerFunctionOutput } from './ArvoEventHandler/types';
4
- import type { MultiArvoEventHandlerFunctionOutput } from './MultiArvoEventHandler/types';
5
- import type { ArvoEventHandlerOpenTelemetryOptions } from './types';
2
+ import type { ArvoEvent } from 'arvo-core';
3
+ import type { ArvoEventHandlerOpenTelemetryOptions } from '../types';
4
+ export declare function isError(e: unknown): e is Error;
6
5
  /**
7
6
  * Checks if the item is null or undefined.
8
7
  *
@@ -46,38 +45,6 @@ export declare function coalesce<T>(...values: (T | null | undefined)[]): T | un
46
45
  * console.log(result); // Output: 'default'
47
46
  */
48
47
  export declare function coalesceOrDefault<T>(values: (T | null | undefined)[], _default: NonNullable<T>): NonNullable<T>;
49
- /**
50
- * Creates ArvoEvents from event handler output.
51
- *
52
- * @param events - An array of event handler function outputs.
53
- * @param otelSpanHeaders - OpenTelemetry headers for tracing.
54
- * @param source - The source of the event.
55
- * @param defaultAccessControl - The default access control string for the events
56
- * @param originalEvent - The original ArvoEvent that triggered the handler.
57
- * @param handlerExectionUnits - The number of execution units for the handler.
58
- * @param factory - A function to create ArvoEvents.
59
- * @returns An array of ArvoEvents created from the handler output.
60
- */
61
- export declare const eventHandlerOutputEventCreator: (events: Array<ArvoEventHandlerFunctionOutput<any> | MultiArvoEventHandlerFunctionOutput>, otelSpanHeaders: OpenTelemetryHeaders, source: string, originalEvent: ArvoEvent, handlerExectionUnits: number, factory: (param: CreateArvoEvent<any, any> & {
62
- to: string;
63
- }, extensions?: Record<string, string | number | boolean>) => ArvoEvent<any, any, any>) => ArvoEvent<any, any, any>[];
64
- export declare const handleArvoEventHandlerCommonError: (error: Error, otelSpanHeaders: OpenTelemetryHeaders, type: string, source: string, originalEvent: ArvoEvent, handlerExectionUnits: number, factory: (param: CreateArvoEvent<any, any> & {
65
- to: string;
66
- }, extensions?: Record<string, string | number | boolean>) => ArvoEvent<any, any, any>) => ArvoEvent<any, any, any>[];
67
- /**
68
- * Validates if a string contains only uppercase or lowercase alphanumeric characters.
69
- *
70
- * This function checks if the input string consists solely of:
71
- * - Lowercase letters (a-z)
72
- * - Numbers (0-9)
73
- * - Dot (.)
74
- *
75
- * It does not allow any special characters, spaces, or other non-alphanumeric characters.
76
- *
77
- * @param input - The string to be validated.
78
- * @returns True if the string contains only alphanumeric characters, false otherwise.
79
- */
80
- export declare function isLowerAlphanumeric(input: string): boolean;
81
48
  export declare const createEventHandlerTelemetryConfig: (name: string, options: SpanOptions, contextConfig: ArvoEventHandlerOpenTelemetryOptions, event: ArvoEvent) => {
82
49
  name: string;
83
50
  disableSpanManagement: boolean;
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createEventHandlerTelemetryConfig = void 0;
4
+ exports.isError = isError;
5
+ exports.isNullOrUndefined = isNullOrUndefined;
6
+ exports.getValueOrDefault = getValueOrDefault;
7
+ exports.coalesce = coalesce;
8
+ exports.coalesceOrDefault = coalesceOrDefault;
9
+ var api_1 = require("@opentelemetry/api");
10
+ function isError(e) {
11
+ return e instanceof Error || (typeof e === 'object' && e !== null && 'message' in e);
12
+ }
13
+ /**
14
+ * Checks if the item is null or undefined.
15
+ *
16
+ * @param item - The value to check.
17
+ * @returns True if the item is null or undefined, false otherwise.
18
+ */
19
+ function isNullOrUndefined(item) {
20
+ return item === null || item === undefined;
21
+ }
22
+ /**
23
+ * Returns the provided value if it's not null or undefined; otherwise, returns the default value.
24
+ *
25
+ * @template T - The type of the value and default value.
26
+ * @param value - The value to check.
27
+ * @param defaultValue - The default value to return if the provided value is null or undefined.
28
+ * @returns The provided value if it's not null or undefined; otherwise, the default value.
29
+ */
30
+ function getValueOrDefault(value, defaultValue) {
31
+ return isNullOrUndefined(value) ? defaultValue : value;
32
+ }
33
+ /**
34
+ * Returns the first non-null and non-undefined value from the provided arguments.
35
+ * If all arguments are null or undefined, returns undefined.
36
+ *
37
+ * @template T - The type of the values.
38
+ * @param values - The values to coalesce.
39
+ * @returns The first non-null and non-undefined value, or undefined if all are null or undefined.
40
+ */
41
+ function coalesce() {
42
+ var values = [];
43
+ for (var _i = 0; _i < arguments.length; _i++) {
44
+ values[_i] = arguments[_i];
45
+ }
46
+ for (var _a = 0, values_1 = values; _a < values_1.length; _a++) {
47
+ var value = values_1[_a];
48
+ if (!isNullOrUndefined(value)) {
49
+ return value;
50
+ }
51
+ }
52
+ return undefined;
53
+ }
54
+ /**
55
+ * Returns the first non-null and non-undefined value from the provided array of values.
56
+ * If all values in the array are null or undefined, returns the specified default value.
57
+ *
58
+ * @template T - The type of the values and the default value.
59
+ * @param values - An array of values to coalesce.
60
+ * @param _default - The default value to return if all values in the array are null or undefined.
61
+ * @returns The first non-null and non-undefined value from the array, or the default value if all are null or undefined.
62
+ *
63
+ * @example
64
+ * const result = coalesceOrDefault([null, undefined, 'hello', 'world'], 'default');
65
+ * console.log(result); // Output: 'hello'
66
+ *
67
+ * @example
68
+ * const result = coalesceOrDefault([null, undefined], 'default');
69
+ * console.log(result); // Output: 'default'
70
+ */
71
+ function coalesceOrDefault(values, _default) {
72
+ return getValueOrDefault(coalesce.apply(void 0, values), _default);
73
+ }
74
+ var createEventHandlerTelemetryConfig = function (name, options, contextConfig, event) { return ({
75
+ name: name,
76
+ disableSpanManagement: true,
77
+ spanOptions: options,
78
+ context: contextConfig.inheritFrom === 'EVENT'
79
+ ? {
80
+ inheritFrom: 'TRACE_HEADERS',
81
+ traceHeaders: {
82
+ traceparent: event.traceparent,
83
+ tracestate: event.tracestate,
84
+ },
85
+ }
86
+ : {
87
+ inheritFrom: 'CONTEXT',
88
+ context: api_1.context.active(),
89
+ },
90
+ }); };
91
+ exports.createEventHandlerTelemetryConfig = createEventHandlerTelemetryConfig;
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Represents a path and its corresponding value in an object.
3
+ * @property {string[]} path - An array of strings representing the path to the value.
4
+ * @property {any} value - The value found at the specified path.
5
+ */
6
+ export type PathValue = {
7
+ path: string[];
8
+ value: any;
9
+ };
10
+ /**
11
+ * Recursively retrieves all paths and their corresponding values from an object.
12
+ *
13
+ * @param {Record<string, any>} obj - The object from which paths are extracted.
14
+ * @returns {PathValue[]} An array of PathValue, each containing a path and its corresponding value.
15
+ * @throws {Error} Will throw an error if `obj` is not a string or an object.
16
+ *
17
+ * @example
18
+ * const obj = { a: { b: 1 }, c: 2 };
19
+ * const paths = getAllPaths(obj);
20
+ * // paths = [
21
+ * // { path: ['a', 'b'], value: 1 },
22
+ * // { path: ['c'], value: 2 }
23
+ * // ]
24
+ */
25
+ export declare function getAllPaths(obj: Record<string, any>): PathValue[];
26
+ /**
27
+ * Converts a PathValue object to a string representation.
28
+ *
29
+ * @param {PathValue} item - The PathValue object to convert.
30
+ * @returns {string} A string representation of the PathValue.
31
+ *
32
+ * @example
33
+ * const pathValue = { path: ['a', 'b'], value: 1 };
34
+ * const result = pathValueToString(pathValue);
35
+ * // result = "#a.#b.1"
36
+ */
37
+ export declare const pathValueToString: (item: PathValue) => string;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pathValueToString = void 0;
4
+ exports.getAllPaths = getAllPaths;
5
+ /**
6
+ * Recursively retrieves all paths and their corresponding values from an object.
7
+ *
8
+ * @param {Record<string, any>} obj - The object from which paths are extracted.
9
+ * @returns {PathValue[]} An array of PathValue, each containing a path and its corresponding value.
10
+ * @throws {Error} Will throw an error if `obj` is not a string or an object.
11
+ *
12
+ * @example
13
+ * const obj = { a: { b: 1 }, c: 2 };
14
+ * const paths = getAllPaths(obj);
15
+ * // paths = [
16
+ * // { path: ['a', 'b'], value: 1 },
17
+ * // { path: ['c'], value: 2 }
18
+ * // ]
19
+ */
20
+ function getAllPaths(obj) {
21
+ if (!obj || !(typeof obj === 'string' || typeof obj === 'object')) {
22
+ throw new Error("[getAllPaths] the 'obj' type must be 'string' or 'object'. The given obj is '".concat(obj, "' of type '").concat(typeof obj, "'"));
23
+ }
24
+ var result = [];
25
+ var stack = [{ obj: obj, path: [] }];
26
+ while (stack.length > 0) {
27
+ var stack_item = stack.pop();
28
+ if (!stack_item)
29
+ continue;
30
+ var currentObj = stack_item.obj, currentPath = stack_item.path;
31
+ if (typeof currentObj !== 'object' || currentObj === null) {
32
+ result.push({ path: currentPath, value: currentObj });
33
+ }
34
+ else {
35
+ for (var key in currentObj) {
36
+ // biome-ignore lint/suspicious/noPrototypeBuiltins: no reason to entertain this lint issue
37
+ if (currentObj.hasOwnProperty(key)) {
38
+ stack.push({ obj: currentObj[key], path: currentPath.concat(key) });
39
+ }
40
+ }
41
+ }
42
+ }
43
+ return result;
44
+ }
45
+ /**
46
+ * Converts a PathValue object to a string representation.
47
+ *
48
+ * @param {PathValue} item - The PathValue object to convert.
49
+ * @returns {string} A string representation of the PathValue.
50
+ *
51
+ * @example
52
+ * const pathValue = { path: ['a', 'b'], value: 1 };
53
+ * const result = pathValueToString(pathValue);
54
+ * // result = "#a.#b.1"
55
+ */
56
+ var pathValueToString = function (item) {
57
+ if (!(item.path || []).length) {
58
+ return item.value.toString();
59
+ }
60
+ var pathString = item.path.map(function (i) { return "#".concat(i); }).join('.');
61
+ return "".concat(pathString, ".").concat(item.value);
62
+ };
63
+ exports.pathValueToString = pathValueToString;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arvo-event-handler",
3
- "version": "2.3.3",
3
+ "version": "3.0.1",
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": {
@@ -10,7 +10,8 @@
10
10
  "test": "jest --passWithNoTests --runInBand --detectOpenHandles --forceExit",
11
11
  "lint": "biome check --fix",
12
12
  "format": "biome format --fix",
13
- "doc": "npx typedoc"
13
+ "doc": "npx typedoc",
14
+ "otel": "docker run --rm -e COLLECTOR_ZIPKIN_HOST_PORT=:9411 -p 16686:16686 -p 4317:4317 -p 4318:4318 -p 9411:9411 jaegertracing/all-in-one:latest"
14
15
  },
15
16
  "keywords": ["arvo", "event-driven architecture", "xorca", "core", "cloudevent", "opentelemetry", "orchestrator"],
16
17
  "author": "Saad Ahmad <saadkwi12@hotmail.com>",
@@ -45,8 +46,9 @@
45
46
  "dependencies": {
46
47
  "@opentelemetry/api": "^1.9.0",
47
48
  "@opentelemetry/core": "^1.30.1",
48
- "arvo-core": "^2.3.3",
49
+ "arvo-core": "^3.0.1",
49
50
  "uuid": "^11.1.0",
51
+ "xstate": "^5.20.1",
50
52
  "zod": "^3.25.67"
51
53
  },
52
54
  "engines": {
@@ -1,19 +0,0 @@
1
- import { ArvoEventRouter } from '.';
2
- import type { IArvoEventRouter } from './types';
3
- /**
4
- * Creates a new ArvoEventRouter instance with the provided configuration.
5
- * Validates source format and ensures unique handlers per event type.
6
- *
7
- * @param param Configuration for router initialization including source,
8
- * handlers, and execution metrics
9
- * @returns Configured ArvoEventRouter instance
10
- * @throws When handlers have duplicate event types or source format is invalid
11
- *
12
- * @example
13
- * const router = createArvoEventRouter({
14
- * source: 'payment.service',
15
- * handlers: [paymentHandler, notificationHandler],
16
- * executionunits: 10
17
- * });
18
- */
19
- export declare const createArvoEventRouter: (param: IArvoEventRouter) => ArvoEventRouter;
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createArvoEventRouter = void 0;
4
- var _1 = require(".");
5
- /**
6
- * Creates a new ArvoEventRouter instance with the provided configuration.
7
- * Validates source format and ensures unique handlers per event type.
8
- *
9
- * @param param Configuration for router initialization including source,
10
- * handlers, and execution metrics
11
- * @returns Configured ArvoEventRouter instance
12
- * @throws When handlers have duplicate event types or source format is invalid
13
- *
14
- * @example
15
- * const router = createArvoEventRouter({
16
- * source: 'payment.service',
17
- * handlers: [paymentHandler, notificationHandler],
18
- * executionunits: 10
19
- * });
20
- */
21
- var createArvoEventRouter = function (param) { return new _1.ArvoEventRouter(param); };
22
- exports.createArvoEventRouter = createArvoEventRouter;
@@ -1,89 +0,0 @@
1
- import { type SpanOptions } from '@opentelemetry/api';
2
- import { type ArvoContract, ArvoEvent } from 'arvo-core';
3
- import AbstractArvoEventHandler from '../AbstractArvoEventHandler';
4
- import type ArvoEventHandler from '../ArvoEventHandler';
5
- import type { ArvoEventHandlerOpenTelemetryOptions } from '../types';
6
- import type { IArvoEventRouter } from './types';
7
- /**
8
- * ArvoEventRouter manages event routing and execution within the Arvo event system. It directs
9
- * incoming events to appropriate handlers based on event type while maintaining telemetry
10
- * and error handling.
11
- *
12
- * The router enforces contract validation, manages execution costs, and provides comprehensive
13
- * telemetry via OpenTelemetry integration. It handles event lifecycle management from initial
14
- * receipt through processing and response generation.
15
- *
16
- * @example
17
- * ```typescript
18
- * const router = createArvoEventRouter({
19
- * source: "payment.service",
20
- * executionunits: 1,
21
- * handlers: [paymentProcessedHandler, paymentFailedHandler]
22
- * });
23
- *
24
- * // Route an incoming event
25
- * const results = await router.execute(incomingEvent);
26
- * ```
27
- */
28
- export declare class ArvoEventRouter extends AbstractArvoEventHandler {
29
- /** Source identifier for the router used in event routing */
30
- readonly source: string;
31
- /** Registry mapping event types to their handlers */
32
- readonly handlersMap: Record<string, ArvoEventHandler<ArvoContract>>;
33
- /**
34
- * Computational cost metric for router operations.
35
- * Used for resource tracking and billing calculations.
36
- */
37
- readonly executionunits: number;
38
- /**
39
- * The OpenTelemetry span options
40
- */
41
- readonly spanOptions: SpanOptions;
42
- /**
43
- * Creates an ArvoEventRouter instance with specified configuration.
44
- *
45
- * @param param - Router configuration containing source, handlers, and execution parameters
46
- *
47
- * @throws {Error} When source contains invalid characters (non-alphanumeric)
48
- * @throws {Error} When multiple handlers are registered for the same event type
49
- */
50
- constructor(param: IArvoEventRouter);
51
- /**
52
- * Routes and executes an event through its appropriate handler. Creates a telemetry span,
53
- * validates the event destination, finds a matching handler, and processes the event.
54
- * Handles routing errors, missing handlers, and execution failures by returning error
55
- * events with telemetry context. Tracks performance through execution units and span
56
- * propagation.
57
- *
58
- * @param event The event to be routed and processed
59
- * @param opentelemetry Configuration for telemetry context inheritance
60
- * @returns Promise resolving to resulting events or error events
61
- *
62
- * @throws {ConfigViolation} When event destination does not match router's source
63
- * @throws {ConfigViolation} When no registered handler exists for the event type
64
- * @throws Other Violation error which are thrown by the registered event handlers
65
- */
66
- execute(event: ArvoEvent, opentelemetry?: ArvoEventHandlerOpenTelemetryOptions): Promise<{
67
- events: ArvoEvent[];
68
- }>;
69
- /**
70
- * System error schema configuration.
71
- * Error events follow format: sys.<handler-source>.error
72
- */
73
- get systemErrorSchema(): {
74
- type: string;
75
- schema: import("zod").ZodObject<{
76
- errorName: import("zod").ZodString;
77
- errorMessage: import("zod").ZodString;
78
- errorStack: import("zod").ZodNullable<import("zod").ZodString>;
79
- }, "strip", import("zod").ZodTypeAny, {
80
- errorName: string;
81
- errorMessage: string;
82
- errorStack: string | null;
83
- }, {
84
- errorName: string;
85
- errorMessage: string;
86
- errorStack: string | null;
87
- }>;
88
- };
89
- }