arvo-event-handler 1.0.3 → 1.0.4

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/CHANGELOG.md CHANGED
@@ -15,7 +15,7 @@
15
15
  ## [1.0.0] - 2024-09-10
16
16
 
17
17
  - First version rrelease
18
+
18
19
  ## [1.0.2] - 2024-09-10
19
20
 
20
21
  - Added ArvoEventRouter as a mechanism to group ArvoEventHandlers
21
-
package/README.md CHANGED
@@ -55,7 +55,6 @@ There 2 main types of event handlers in Arvo event driven system
55
55
  - [ArvoEventRouter](src/ArvoEventRouter/README.md) is designed to route ArvoEvents to appropriate ArvoEventHandlers. It provides a centralized mechanism for managing and executing multiple event handlers based on event types.
56
56
  - [MultiArvoEventHandler](src/MultiArvoEventHandler/README.md) is a flexible and powerful event handling class designed to process multiple event types across different ArvoContracts. This handler offers greater versatility compared to the more specialized `ArvoEventHandler`, as it's not bound to a specific contract or event type.
57
57
 
58
-
59
58
  ## Getting Started
60
59
 
61
60
  To start using Arvo Event Handlers in your project:
@@ -75,7 +74,7 @@ import {
75
74
  createArvoEventHandler,
76
75
  createMultiArvoEventHandler,
77
76
  createArvoEventRouter,
78
- } from 'arvo-event-handler'
77
+ } from 'arvo-event-handler';
79
78
  ```
80
79
 
81
80
  - Begin defining your events, contracts, and handlers using the provided classes.
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -46,17 +35,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
46
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
36
  }
48
37
  };
49
- var __rest = (this && this.__rest) || function (s, e) {
50
- var t = {};
51
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
52
- t[p] = s[p];
53
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
54
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
55
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
56
- t[p[i]] = s[p[i]];
57
- }
58
- return t;
59
- };
60
38
  Object.defineProperty(exports, "__esModule", { value: true });
61
39
  var arvo_core_1 = require("arvo-core");
62
40
  var schema_1 = require("arvo-core/dist/ArvoEvent/schema");
@@ -182,7 +160,6 @@ var ArvoEventHandler = /** @class */ (function () {
182
160
  eventFactory = (0, arvo_core_1.createArvoEventFactory)(this.contract);
183
161
  return [4 /*yield*/, api_1.context.with(api_1.trace.setSpan(api_1.context.active(), span), function () { return __awaiter(_this, void 0, void 0, function () {
184
162
  var otelSpanHeaders, inputEventValidation, _handleOutput, outputs, error_1, result;
185
- var _this = this;
186
163
  return __generator(this, function (_a) {
187
164
  switch (_a.label) {
188
165
  case 0:
@@ -214,19 +191,12 @@ var ArvoEventHandler = /** @class */ (function () {
214
191
  else {
215
192
  outputs = [_handleOutput];
216
193
  }
217
- return [2 /*return*/, outputs.map(function (output, index) {
218
- var __extensions = output.__extensions, handlerResult = __rest(output, ["__extensions"]);
219
- var result = eventFactory.emits(__assign(__assign({}, handlerResult), { traceparent: otelSpanHeaders.traceparent || undefined, tracestate: otelSpanHeaders.tracestate || undefined, source: _this.source, subject: event.subject,
220
- // The user should be able to override the `to` field
221
- // If that is not present then the 'redirectto' field
222
- // is referred to. Then, after all else, 'source' field
223
- // is used as a form of reply.
224
- to: (0, utils_1.coalesceOrDefault)([handlerResult.to, event.redirectto], event.source), executionunits: (0, utils_1.coalesce)(handlerResult.executionunits, _this.executionunits) }), __extensions);
225
- Object.entries(result.otelAttributes).forEach(function (_a) {
226
- var key = _a[0], value = _a[1];
227
- return span.setAttribute("to_emit.".concat(index, ".").concat(key), value);
228
- });
229
- return result;
194
+ return [2 /*return*/, (0, utils_1.eventHandlerOutputEventCreator)(outputs, otelSpanHeaders, this.source, event, this.executionunits, function () {
195
+ var args = [];
196
+ for (var _i = 0; _i < arguments.length; _i++) {
197
+ args[_i] = arguments[_i];
198
+ }
199
+ return eventFactory.emits.apply(eventFactory, args);
230
200
  })];
231
201
  case 3:
232
202
  error_1 = _a.sent();
@@ -40,9 +40,9 @@ exports.ArvoEventRouter = void 0;
40
40
  var arvo_core_1 = require("arvo-core");
41
41
  var utils_1 = require("../utils");
42
42
  var api_1 = require("@opentelemetry/api");
43
- var OpenTelemetry_1 = require("../OpenTelemetry");
44
43
  var utils_2 = require("./utils");
45
44
  var schema_1 = require("arvo-core/dist/ArvoEvent/schema");
45
+ var utils_3 = require("../OpenTelemetry/utils");
46
46
  /**
47
47
  * ArvoEventRouter class handles routing of ArvoEvents to appropriate event handlers.
48
48
  */
@@ -130,28 +130,17 @@ var ArvoEventRouter = /** @class */ (function () {
130
130
  */
131
131
  ArvoEventRouter.prototype.execute = function (event) {
132
132
  return __awaiter(this, void 0, void 0, function () {
133
- var spanName, spanOptions, span, inheritedContext;
134
- var _a;
133
+ var span;
135
134
  var _this = this;
136
- var _b;
137
- return __generator(this, function (_c) {
138
- switch (_c.label) {
135
+ var _a;
136
+ return __generator(this, function (_b) {
137
+ switch (_b.label) {
139
138
  case 0:
140
- spanName = "ArvoEventRouter.source<".concat((_b = this.source) !== null && _b !== void 0 ? _b : 'arvo.event.router', ">.execute<").concat(event.type, ">");
141
- spanOptions = {
139
+ span = (0, utils_3.createSpanFromEvent)("ArvoEventRouter.source<".concat((_a = this.source) !== null && _a !== void 0 ? _a : 'arvo.event.router', ">.execute<").concat(event.type, ">"), event, {
142
140
  kind: this.openTelemetrySpanKind,
143
- attributes: (_a = {},
144
- _a[arvo_core_1.OpenInference.ATTR_SPAN_KIND] = this.openInferenceSpanKind,
145
- _a[arvo_core_1.ArvoExecution.ATTR_SPAN_KIND] = this.arvoExecutionSpanKind,
146
- _a),
147
- };
148
- if (event.traceparent) {
149
- inheritedContext = (0, OpenTelemetry_1.extractContext)(event.traceparent, event.tracestate);
150
- span = OpenTelemetry_1.ArvoEventHandlerTracer.startSpan(spanName, spanOptions, inheritedContext);
151
- }
152
- else {
153
- span = OpenTelemetry_1.ArvoEventHandlerTracer.startSpan(spanName, spanOptions);
154
- }
141
+ openInference: this.openInferenceSpanKind,
142
+ arvoExecution: this.arvoExecutionSpanKind,
143
+ });
155
144
  return [4 /*yield*/, api_1.context.with(api_1.trace.setSpan(api_1.context.active(), span), function () { return __awaiter(_this, void 0, void 0, function () {
156
145
  var otelSpanHeaders, newEvent, results, error_1, result;
157
146
  var _this = this;
@@ -232,7 +221,7 @@ var ArvoEventRouter = /** @class */ (function () {
232
221
  }
233
222
  });
234
223
  }); })];
235
- case 1: return [2 /*return*/, _c.sent()];
224
+ case 1: return [2 /*return*/, _b.sent()];
236
225
  }
237
226
  });
238
227
  });
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -46,23 +35,12 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
46
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
36
  }
48
37
  };
49
- var __rest = (this && this.__rest) || function (s, e) {
50
- var t = {};
51
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
52
- t[p] = s[p];
53
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
54
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
55
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
56
- t[p[i]] = s[p[i]];
57
- }
58
- return t;
59
- };
60
38
  Object.defineProperty(exports, "__esModule", { value: true });
61
39
  var api_1 = require("@opentelemetry/api");
62
40
  var arvo_core_1 = require("arvo-core");
63
41
  var schema_1 = require("arvo-core/dist/ArvoEvent/schema");
64
- var OpenTelemetry_1 = require("../OpenTelemetry");
65
42
  var utils_1 = require("../utils");
43
+ var utils_2 = require("../OpenTelemetry/utils");
66
44
  /**
67
45
  * Represents a Multi ArvoEvent handler that can process multiple event types.
68
46
  *
@@ -155,30 +133,18 @@ var MultiArvoEventHandler = /** @class */ (function () {
155
133
  */
156
134
  MultiArvoEventHandler.prototype.execute = function (event) {
157
135
  return __awaiter(this, void 0, void 0, function () {
158
- var spanName, spanOptions, span, inheritedContext;
159
- var _a;
136
+ var span;
160
137
  var _this = this;
161
- return __generator(this, function (_b) {
162
- switch (_b.label) {
138
+ return __generator(this, function (_a) {
139
+ switch (_a.label) {
163
140
  case 0:
164
- spanName = "MutliArvoEventHandler.source<".concat(this.source, ">.execute<").concat(event.type, ">");
165
- spanOptions = {
141
+ span = (0, utils_2.createSpanFromEvent)("MutliArvoEventHandler.source<".concat(this.source, ">.execute<").concat(event.type, ">"), event, {
166
142
  kind: this.openTelemetrySpanKind,
167
- attributes: (_a = {},
168
- _a[arvo_core_1.OpenInference.ATTR_SPAN_KIND] = this.openInferenceSpanKind,
169
- _a[arvo_core_1.ArvoExecution.ATTR_SPAN_KIND] = this.arvoExecutionSpanKind,
170
- _a),
171
- };
172
- if (event.traceparent) {
173
- inheritedContext = (0, OpenTelemetry_1.extractContext)(event.traceparent, event.tracestate);
174
- span = OpenTelemetry_1.ArvoEventHandlerTracer.startSpan(spanName, spanOptions, inheritedContext);
175
- }
176
- else {
177
- span = OpenTelemetry_1.ArvoEventHandlerTracer.startSpan(spanName, spanOptions);
178
- }
143
+ openInference: this.openInferenceSpanKind,
144
+ arvoExecution: this.arvoExecutionSpanKind,
145
+ });
179
146
  return [4 /*yield*/, api_1.context.with(api_1.trace.setSpan(api_1.context.active(), span), function () { return __awaiter(_this, void 0, void 0, function () {
180
147
  var otelSpanHeaders, _handlerOutput, outputs, error_1, result;
181
- var _this = this;
182
148
  var _a, _b, _c;
183
149
  return __generator(this, function (_d) {
184
150
  switch (_d.label) {
@@ -210,19 +176,12 @@ var MultiArvoEventHandler = /** @class */ (function () {
210
176
  else {
211
177
  outputs = [_handlerOutput];
212
178
  }
213
- return [2 /*return*/, outputs.map(function (output, index) {
214
- var __extensions = output.__extensions, handlerResult = __rest(output, ["__extensions"]);
215
- var result = (0, arvo_core_1.createArvoEvent)(__assign(__assign({}, handlerResult), { traceparent: otelSpanHeaders.traceparent || undefined, tracestate: otelSpanHeaders.tracestate || undefined, source: _this.source, subject: event.subject,
216
- // The user should be able to override the `to` field
217
- // If that is not present then the 'redirectto' field
218
- // is referred to. Then, after all else, 'source' field
219
- // is used as a form of reply.
220
- to: (0, utils_1.coalesceOrDefault)([handlerResult.to, event.redirectto], event.source), executionunits: (0, utils_1.coalesce)(handlerResult.executionunits, _this.executionunits) }), __extensions);
221
- Object.entries(result.otelAttributes).forEach(function (_a) {
222
- var key = _a[0], value = _a[1];
223
- return span.setAttribute("to_emit.".concat(index, ".").concat(key), value);
224
- });
225
- return result;
179
+ return [2 /*return*/, (0, utils_1.eventHandlerOutputEventCreator)(outputs, otelSpanHeaders, this.source, event, this.executionunits, function () {
180
+ var args = [];
181
+ for (var _i = 0; _i < arguments.length; _i++) {
182
+ args[_i] = arguments[_i];
183
+ }
184
+ return arvo_core_1.createArvoEvent.apply(void 0, args);
226
185
  })];
227
186
  case 3:
228
187
  error_1 = _d.sent();
@@ -235,8 +194,6 @@ var MultiArvoEventHandler = /** @class */ (function () {
235
194
  type: "sys.".concat(this.source, ".error"),
236
195
  source: this.source,
237
196
  subject: event.subject,
238
- // The system error must always got back to
239
- // the source
240
197
  to: event.source,
241
198
  executionunits: this.executionunits,
242
199
  traceparent: (_a = otelSpanHeaders.traceparent) !== null && _a !== void 0 ? _a : undefined,
@@ -259,7 +216,7 @@ var MultiArvoEventHandler = /** @class */ (function () {
259
216
  }
260
217
  });
261
218
  }); })];
262
- case 1: return [2 /*return*/, _b.sent()];
219
+ case 1: return [2 /*return*/, _a.sent()];
263
220
  }
264
221
  });
265
222
  });
@@ -1,4 +1,11 @@
1
+ import { Span, SpanKind } from '@opentelemetry/api';
2
+ import { ArvoEvent, ArvoExecutionSpanKind, OpenInferenceSpanKind } from 'arvo-core';
1
3
  export declare function getPackageInfo(): {
2
4
  name: string;
3
5
  version: string;
4
6
  };
7
+ export declare const createSpanFromEvent: (spanName: string, event: ArvoEvent, spanKinds: {
8
+ kind: SpanKind;
9
+ openInference: OpenInferenceSpanKind;
10
+ arvoExecution: ArvoExecutionSpanKind;
11
+ }) => Span;
@@ -23,9 +23,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.createSpanFromEvent = void 0;
26
27
  exports.getPackageInfo = getPackageInfo;
28
+ var arvo_core_1 = require("arvo-core");
27
29
  var fs = __importStar(require("fs"));
28
30
  var path = __importStar(require("path"));
31
+ var _1 = require(".");
29
32
  function getPackageInfo() {
30
33
  try {
31
34
  // Read the package.json file
@@ -42,3 +45,23 @@ function getPackageInfo() {
42
45
  return { name: 'Unknown', version: 'Unknown' };
43
46
  }
44
47
  }
48
+ var createSpanFromEvent = function (spanName, event, spanKinds) {
49
+ var _a;
50
+ var spanOptions = {
51
+ kind: spanKinds.kind,
52
+ attributes: (_a = {},
53
+ _a[arvo_core_1.OpenInference.ATTR_SPAN_KIND] = spanKinds.openInference,
54
+ _a[arvo_core_1.ArvoExecution.ATTR_SPAN_KIND] = spanKinds.arvoExecution,
55
+ _a),
56
+ };
57
+ var span;
58
+ if (event.traceparent) {
59
+ var inheritedContext = (0, _1.extractContext)(event.traceparent, event.tracestate);
60
+ span = _1.ArvoEventHandlerTracer.startSpan(spanName, spanOptions, inheritedContext);
61
+ }
62
+ else {
63
+ span = _1.ArvoEventHandlerTracer.startSpan(spanName, spanOptions);
64
+ }
65
+ return span;
66
+ };
67
+ exports.createSpanFromEvent = createSpanFromEvent;
package/dist/utils.d.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import { ArvoEvent, CreateArvoEvent, OpenTelemetryHeaders } from "arvo-core";
2
+ import { ArvoEventHandlerFunctionOutput } from "./ArvoEventHandler/types";
3
+ import { MultiArvoEventHandlerFunctionOutput } from "./MultiArvoEventHandler/types";
1
4
  /**
2
5
  * Checks if the item is null or undefined.
3
6
  *
@@ -41,3 +44,17 @@ export declare function coalesce<T>(...values: (T | null | undefined)[]): T | un
41
44
  * console.log(result); // Output: 'default'
42
45
  */
43
46
  export declare function coalesceOrDefault<T>(values: (T | null | undefined)[], _default: NonNullable<T>): NonNullable<T>;
47
+ /**
48
+ * Creates ArvoEvents from event handler output.
49
+ *
50
+ * @param events - An array of event handler function outputs.
51
+ * @param otelSpanHeaders - OpenTelemetry headers for tracing.
52
+ * @param source - The source of the event.
53
+ * @param originalEvent - The original ArvoEvent that triggered the handler.
54
+ * @param handlerExectionUnits - The number of execution units for the handler.
55
+ * @param factory - A function to create ArvoEvents.
56
+ * @returns An array of ArvoEvents created from the handler output.
57
+ */
58
+ export declare const eventHandlerOutputEventCreator: (events: Array<ArvoEventHandlerFunctionOutput<any> | MultiArvoEventHandlerFunctionOutput>, otelSpanHeaders: OpenTelemetryHeaders, source: string, originalEvent: ArvoEvent, handlerExectionUnits: number, factory: (param: CreateArvoEvent<any, any> & {
59
+ to: string;
60
+ }, extensions?: Record<string, string | number | boolean>) => ArvoEvent<any, any, any>) => ArvoEvent<any, any, any>[];
package/dist/utils.js CHANGED
@@ -1,9 +1,33 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __rest = (this && this.__rest) || function (s, e) {
14
+ var t = {};
15
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
16
+ t[p] = s[p];
17
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
18
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
19
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
20
+ t[p[i]] = s[p[i]];
21
+ }
22
+ return t;
23
+ };
2
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
+ exports.eventHandlerOutputEventCreator = void 0;
3
26
  exports.isNullOrUndefined = isNullOrUndefined;
4
27
  exports.getValueOrDefault = getValueOrDefault;
5
28
  exports.coalesce = coalesce;
6
29
  exports.coalesceOrDefault = coalesceOrDefault;
30
+ var api_1 = require("@opentelemetry/api");
7
31
  /**
8
32
  * Checks if the item is null or undefined.
9
33
  *
@@ -65,3 +89,30 @@ function coalesce() {
65
89
  function coalesceOrDefault(values, _default) {
66
90
  return getValueOrDefault(coalesce.apply(void 0, values), _default);
67
91
  }
92
+ /**
93
+ * Creates ArvoEvents from event handler output.
94
+ *
95
+ * @param events - An array of event handler function outputs.
96
+ * @param otelSpanHeaders - OpenTelemetry headers for tracing.
97
+ * @param source - The source of the event.
98
+ * @param originalEvent - The original ArvoEvent that triggered the handler.
99
+ * @param handlerExectionUnits - The number of execution units for the handler.
100
+ * @param factory - A function to create ArvoEvents.
101
+ * @returns An array of ArvoEvents created from the handler output.
102
+ */
103
+ var eventHandlerOutputEventCreator = function (events, otelSpanHeaders, source, originalEvent, handlerExectionUnits, factory) {
104
+ return events.map(function (item, index) {
105
+ var __extensions = item.__extensions, handlerResult = __rest(item, ["__extensions"]);
106
+ var result = factory(__assign(__assign({}, handlerResult), { traceparent: otelSpanHeaders.traceparent || undefined, tracestate: otelSpanHeaders.tracestate || undefined, source: source, subject: originalEvent.subject,
107
+ // prioritise returned 'to', 'redirectto' and then
108
+ // 'source'
109
+ to: coalesceOrDefault([handlerResult.to, originalEvent.redirectto], originalEvent.source), executionunits: coalesce(handlerResult.executionunits, handlerExectionUnits) }), __extensions);
110
+ Object.entries(result.otelAttributes).forEach(function (_a) {
111
+ var _b;
112
+ var key = _a[0], value = _a[1];
113
+ return (_b = api_1.trace.getActiveSpan()) === null || _b === void 0 ? void 0 : _b.setAttribute("to_emit.".concat(index, ".").concat(key), value);
114
+ });
115
+ return result;
116
+ });
117
+ };
118
+ exports.eventHandlerOutputEventCreator = eventHandlerOutputEventCreator;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arvo-event-handler",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "This package contains class and function for event handlers in an Arvo Event Driven system",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {