arvo-event-handler 2.0.4 → 2.1.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/AbstractArvoEventHandler/index.d.ts +5 -18
- package/dist/ArvoEventHandler/helpers.d.ts +10 -21
- package/dist/ArvoEventHandler/helpers.js +10 -21
- package/dist/ArvoEventHandler/index.d.ts +56 -91
- package/dist/ArvoEventHandler/index.js +118 -135
- package/dist/ArvoEventHandler/types.d.ts +7 -30
- package/dist/ArvoEventRouter/helpers.d.ts +8 -25
- package/dist/ArvoEventRouter/helpers.js +8 -25
- package/dist/ArvoEventRouter/index.d.ts +47 -89
- package/dist/ArvoEventRouter/index.js +138 -155
- package/dist/ArvoEventRouter/types.d.ts +4 -22
- package/dist/MultiArvoEventHandler/index.d.ts +31 -96
- package/dist/MultiArvoEventHandler/index.js +75 -120
- package/dist/MultiArvoEventHandler/types.d.ts +6 -14
- package/dist/index.d.ts +2 -4
- package/dist/index.js +3 -5
- package/dist/types.d.ts +3 -0
- package/package.json +2 -2
- package/dist/OpenTelemetry/index.d.ts +0 -4
- package/dist/OpenTelemetry/index.js +0 -11
- package/dist/OpenTelemetry/types.d.ts +0 -48
- package/dist/OpenTelemetry/types.js +0 -2
- package/dist/OpenTelemetry/utils.d.ts +0 -87
- package/dist/OpenTelemetry/utils.js +0 -133
|
@@ -14,6 +14,17 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
14
14
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
15
|
};
|
|
16
16
|
})();
|
|
17
|
+
var __assign = (this && this.__assign) || function () {
|
|
18
|
+
__assign = Object.assign || function(t) {
|
|
19
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
+
s = arguments[i];
|
|
21
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
+
t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
17
28
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
29
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
30
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -55,180 +66,159 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
55
66
|
};
|
|
56
67
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
57
68
|
var arvo_core_1 = require("arvo-core");
|
|
58
|
-
var schema_1 = require("arvo-core/dist/ArvoEvent/schema");
|
|
59
69
|
var api_1 = require("@opentelemetry/api");
|
|
60
70
|
var utils_1 = require("../utils");
|
|
61
71
|
var AbstractArvoEventHandler_1 = __importDefault(require("../AbstractArvoEventHandler"));
|
|
62
|
-
var OpenTelemetry_1 = require("../OpenTelemetry");
|
|
63
|
-
var utils_2 = require("../OpenTelemetry/utils");
|
|
64
72
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
73
|
+
* ArvoEventHandler manages the execution and processing of events in accordance with
|
|
74
|
+
* Arvo contracts. This class serves as the cornerstone for event handling operations,
|
|
75
|
+
* integrating contract validation, telemetry management, and event processing.
|
|
68
76
|
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
77
|
+
* The handler implements a robust execution flow that ensures proper validation,
|
|
78
|
+
* versioning, and error handling while maintaining detailed telemetry through
|
|
79
|
+
* OpenTelemetry integration. It supports versioned contracts and handles routing
|
|
80
|
+
* of both successful and error events.
|
|
73
81
|
*/
|
|
74
82
|
var ArvoEventHandler = /** @class */ (function (_super) {
|
|
75
83
|
__extends(ArvoEventHandler, _super);
|
|
76
84
|
/**
|
|
77
|
-
*
|
|
85
|
+
* Initializes a new ArvoEventHandler instance with the specified contract and configuration.
|
|
86
|
+
* Validates handler implementations against contract versions during initialization.
|
|
78
87
|
*
|
|
79
|
-
*
|
|
88
|
+
* The constructor ensures that handler implementations exist for all supported contract
|
|
89
|
+
* versions and configures OpenTelemetry span attributes for monitoring event handling.
|
|
80
90
|
*
|
|
81
|
-
* @
|
|
82
|
-
*
|
|
83
|
-
* @remarks
|
|
84
|
-
* The constructor validates the source parameter against the CloudEventContextSchema.
|
|
85
|
-
* If no source is provided, it defaults to the contract's accepted event type.
|
|
91
|
+
* @param param - Handler configuration including contract, execution units, and handler implementations
|
|
92
|
+
* @throws When handler implementations are missing for any contract version
|
|
86
93
|
*/
|
|
87
94
|
function ArvoEventHandler(param) {
|
|
88
|
-
var _a
|
|
95
|
+
var _a;
|
|
96
|
+
var _b, _c;
|
|
89
97
|
var _this = _super.call(this) || this;
|
|
90
|
-
_this.openInferenceSpanKind = arvo_core_1.OpenInferenceSpanKind.CHAIN;
|
|
91
|
-
_this.arvoExecutionSpanKind = arvo_core_1.ArvoExecutionSpanKind.EVENT_HANDLER;
|
|
92
|
-
_this.openTelemetrySpanKind = api_1.SpanKind.INTERNAL;
|
|
93
98
|
_this.contract = param.contract;
|
|
94
99
|
_this.executionunits = param.executionunits;
|
|
95
|
-
_this.
|
|
96
|
-
|
|
97
|
-
var
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (error) {
|
|
101
|
-
throw new Error("The provided 'source' is not a valid string. Error: ".concat(error.message));
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
_this.source = (_a = param.source) !== null && _a !== void 0 ? _a : _this.contract.type;
|
|
105
|
-
_this.arvoExecutionSpanKind =
|
|
106
|
-
(_c = (_b = param.spanKind) === null || _b === void 0 ? void 0 : _b.arvoExecution) !== null && _c !== void 0 ? _c : _this.arvoExecutionSpanKind;
|
|
107
|
-
_this.openInferenceSpanKind =
|
|
108
|
-
(_e = (_d = param.spanKind) === null || _d === void 0 ? void 0 : _d.openInference) !== null && _e !== void 0 ? _e : _this.openInferenceSpanKind;
|
|
109
|
-
_this.openTelemetrySpanKind =
|
|
110
|
-
(_g = (_f = param.spanKind) === null || _f === void 0 ? void 0 : _f.openTelemetry) !== null && _g !== void 0 ? _g : _this.openTelemetrySpanKind;
|
|
111
|
-
for (var _i = 0, _h = Object.keys(_this.contract.versions); _i < _h.length; _i++) {
|
|
112
|
-
var contractVersions = _h[_i];
|
|
113
|
-
if (!_this._handler[contractVersions]) {
|
|
114
|
-
throw new Error("The event handler for contract (uri=".concat(_this.contract.uri, ") must contain a handler for version ").concat(contractVersions, "."));
|
|
100
|
+
_this.handler = param.handler;
|
|
101
|
+
for (var _i = 0, _d = Object.keys(_this.contract.versions); _i < _d.length; _i++) {
|
|
102
|
+
var contractVersions = _d[_i];
|
|
103
|
+
if (!_this.handler[contractVersions]) {
|
|
104
|
+
throw new Error("Contract ".concat(_this.contract.uri, " requires handler implementation for version ").concat(contractVersions));
|
|
115
105
|
}
|
|
116
106
|
}
|
|
107
|
+
_this.spanOptions = __assign(__assign({ kind: api_1.SpanKind.CONSUMER }, param.spanOptions), { attributes: __assign(__assign((_a = {}, _a[arvo_core_1.ArvoExecution.ATTR_SPAN_KIND] = arvo_core_1.ArvoExecutionSpanKind.EVENT_HANDLER, _a[arvo_core_1.OpenInference.ATTR_SPAN_KIND] = arvo_core_1.OpenInferenceSpanKind.CHAIN, _a), ((_c = (_b = param.spanOptions) === null || _b === void 0 ? void 0 : _b.attributes) !== null && _c !== void 0 ? _c : {})), { 'arvo.contract.uri': _this.contract.uri, 'arvo.handler.source': _this.source }) });
|
|
117
108
|
return _this;
|
|
118
109
|
}
|
|
110
|
+
Object.defineProperty(ArvoEventHandler.prototype, "source", {
|
|
111
|
+
/** The source identifier for events produced by this handler */
|
|
112
|
+
get: function () {
|
|
113
|
+
return this.contract.type;
|
|
114
|
+
},
|
|
115
|
+
enumerable: false,
|
|
116
|
+
configurable: true
|
|
117
|
+
});
|
|
119
118
|
/**
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
* @param event - The event to handle.
|
|
123
|
-
* @param opentelemetry - Configuration for OpenTelemetry integration, including tracing options
|
|
124
|
-
* and context inheritance settings. Default is inherit from event and internal tracer
|
|
125
|
-
* @returns A promise that resolves to an array of resulting ArvoEvents.
|
|
126
|
-
*
|
|
127
|
-
* This method performs the following steps:
|
|
128
|
-
* 1. Creates an OpenTelemetry span for the execution.
|
|
129
|
-
* 2. Validates the input event against the contract.
|
|
130
|
-
* 3. Executes the handler function.
|
|
131
|
-
* 4. Creates and returns the result event(s).
|
|
132
|
-
* 5. Handles any errors and creates an error event if necessary.
|
|
133
|
-
*
|
|
134
|
-
* All telemetry data is properly set and propagated throughout the execution.
|
|
135
|
-
*
|
|
136
|
-
* @example
|
|
137
|
-
* ```typescript
|
|
138
|
-
* const contract = createArvoContract({ ... })
|
|
139
|
-
* const handler = createArvoEventHandler({
|
|
140
|
-
* contract: contract,
|
|
141
|
-
* ...
|
|
142
|
-
* });
|
|
143
|
-
* const inputEvent: ArvoEvent<...> = createArvoEvent({ ... });
|
|
144
|
-
* const resultEvents = await handler.execute(inputEvent);
|
|
145
|
-
* ```
|
|
119
|
+
* Processes an event according to the contract specifications. The execution flow encompasses
|
|
120
|
+
* span management, validation, handler execution, and error processing phases.
|
|
146
121
|
*
|
|
147
|
-
*
|
|
122
|
+
* Event Routing Logic:
|
|
123
|
+
* - Success events: Routes based on priority (handler result -> redirectto -> source)
|
|
124
|
+
* - Error events: Always routes back to the source
|
|
148
125
|
*
|
|
149
|
-
*
|
|
126
|
+
* Telemetry Integration:
|
|
127
|
+
* - Creates and manages OpenTelemetry spans for execution tracking
|
|
128
|
+
* - Propagates trace context through the event chain
|
|
129
|
+
* - Records execution metrics and error details
|
|
150
130
|
*
|
|
151
|
-
*
|
|
152
|
-
* -
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
* 3. The `source` field from the input event (as a form of reply)
|
|
156
|
-
* - For system error events, the `to` field is always set to the `source` of the input event.
|
|
131
|
+
* Version Resolution:
|
|
132
|
+
* - Extracts version from event dataschema
|
|
133
|
+
* - Falls back to latest version if unspecified
|
|
134
|
+
* - Validates event data against versioned contract schema
|
|
157
135
|
*
|
|
158
|
-
*
|
|
136
|
+
* Error Handling:
|
|
137
|
+
* - Converts all errors to system error events
|
|
138
|
+
* - Maintains telemetry context for error scenarios
|
|
139
|
+
* - Ensures proper error event routing
|
|
159
140
|
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
162
|
-
*
|
|
163
|
-
* - Sets span attributes for input and output events
|
|
164
|
-
* - Propagates trace context to output events
|
|
165
|
-
* - Handles error cases and sets appropriate span status
|
|
141
|
+
* @param event - The event to process
|
|
142
|
+
* @param opentelemetry - Configuration for OpenTelemetry context inheritance
|
|
143
|
+
* @returns Promise resolving to array of result events or error event
|
|
166
144
|
*/
|
|
167
|
-
ArvoEventHandler.prototype.execute = function (
|
|
168
|
-
return __awaiter(this,
|
|
169
|
-
var span;
|
|
145
|
+
ArvoEventHandler.prototype.execute = function (event_1) {
|
|
146
|
+
return __awaiter(this, arguments, void 0, function (event, opentelemetry) {
|
|
170
147
|
var _this = this;
|
|
148
|
+
if (opentelemetry === void 0) { opentelemetry = {
|
|
149
|
+
inheritFrom: 'EVENT',
|
|
150
|
+
}; }
|
|
171
151
|
return __generator(this, function (_a) {
|
|
172
152
|
switch (_a.label) {
|
|
173
|
-
case 0:
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
153
|
+
case 0: return [4 /*yield*/, arvo_core_1.ArvoOpenTelemetry.getInstance().startActiveSpan({
|
|
154
|
+
name: 'ArvoEventHandler',
|
|
155
|
+
spanOptions: this.spanOptions,
|
|
156
|
+
disableSpanManagement: true,
|
|
157
|
+
context: opentelemetry.inheritFrom === 'EVENT'
|
|
158
|
+
? {
|
|
159
|
+
inheritFrom: 'TRACE_HEADERS',
|
|
160
|
+
traceHeaders: {
|
|
161
|
+
traceparent: event.traceparent,
|
|
162
|
+
tracestate: event.tracestate,
|
|
163
|
+
},
|
|
164
|
+
}
|
|
165
|
+
: {
|
|
166
|
+
inheritFrom: 'CONTEXT',
|
|
167
|
+
context: api_1.context.active(),
|
|
168
|
+
},
|
|
169
|
+
fn: function (span) { return __awaiter(_this, void 0, void 0, function () {
|
|
185
170
|
var otelSpanHeaders, parsedDataSchema, handlerContract, inputEventValidation, _handleOutput, outputs, eventFactory_1, result, error_1, eventFactory, result;
|
|
186
|
-
var _a, _b, _c, _d
|
|
187
|
-
return __generator(this, function (
|
|
188
|
-
switch (
|
|
171
|
+
var _a, _b, _c, _d;
|
|
172
|
+
return __generator(this, function (_e) {
|
|
173
|
+
switch (_e.label) {
|
|
189
174
|
case 0:
|
|
190
175
|
otelSpanHeaders = (0, arvo_core_1.currentOpenTelemetryHeaders)();
|
|
191
|
-
|
|
176
|
+
_e.label = 1;
|
|
192
177
|
case 1:
|
|
193
|
-
|
|
178
|
+
_e.trys.push([1, 3, 4, 5]);
|
|
194
179
|
span.setStatus({ code: api_1.SpanStatusCode.OK });
|
|
180
|
+
Object.entries(event.otelAttributes).forEach(function (_a) {
|
|
181
|
+
var key = _a[0], value = _a[1];
|
|
182
|
+
return span.setAttribute("to_process.0.".concat(key), value);
|
|
183
|
+
});
|
|
195
184
|
if (this.contract.type !== event.type) {
|
|
196
|
-
throw new Error("
|
|
185
|
+
throw new Error("Event type mismatch: Received '".concat(event.type, "', expected '").concat(this.contract.type, "'"));
|
|
197
186
|
}
|
|
198
187
|
(0, arvo_core_1.logToSpan)({
|
|
199
188
|
level: 'INFO',
|
|
200
|
-
message: "Event type
|
|
189
|
+
message: "Event type '".concat(event.type, "' validated against contract '").concat(this.contract.uri, "'"),
|
|
201
190
|
});
|
|
202
191
|
parsedDataSchema = arvo_core_1.EventDataschemaUtil.parse(event);
|
|
203
192
|
if (!(parsedDataSchema === null || parsedDataSchema === void 0 ? void 0 : parsedDataSchema.version)) {
|
|
204
193
|
(0, arvo_core_1.logToSpan)({
|
|
205
194
|
level: 'WARNING',
|
|
206
|
-
message: "
|
|
195
|
+
message: "Version resolution failed for event with dataschema '".concat(event.dataschema, "'. Defaulting to latest version (=").concat(this.contract.version('latest').version, ") of contract (uri=").concat(this.contract.uri, ")"),
|
|
207
196
|
});
|
|
208
197
|
}
|
|
209
198
|
handlerContract = this.contract.version((_a = parsedDataSchema === null || parsedDataSchema === void 0 ? void 0 : parsedDataSchema.version) !== null && _a !== void 0 ? _a : 'latest');
|
|
210
199
|
(0, arvo_core_1.logToSpan)({
|
|
211
200
|
level: 'INFO',
|
|
212
|
-
message: "
|
|
213
|
-
});
|
|
214
|
-
Object.entries(event.otelAttributes).forEach(function (_a) {
|
|
215
|
-
var key = _a[0], value = _a[1];
|
|
216
|
-
return span.setAttribute("to_process.0.".concat(key), value);
|
|
201
|
+
message: "Processing event with contract version ".concat(handlerContract.version),
|
|
217
202
|
});
|
|
218
203
|
inputEventValidation = handlerContract.accepts.schema.safeParse(event.data);
|
|
219
204
|
if (inputEventValidation.error) {
|
|
220
|
-
throw new Error("
|
|
205
|
+
throw new Error("Event payload validation failed: ".concat(inputEventValidation.error));
|
|
221
206
|
}
|
|
222
207
|
(0, arvo_core_1.logToSpan)({
|
|
223
208
|
level: 'INFO',
|
|
224
|
-
message: "Event payload validated against contract
|
|
209
|
+
message: "Event payload validated successfully against contract ".concat(arvo_core_1.EventDataschemaUtil.create(handlerContract)),
|
|
225
210
|
});
|
|
226
|
-
|
|
211
|
+
(0, arvo_core_1.logToSpan)({
|
|
212
|
+
level: 'INFO',
|
|
213
|
+
message: "Executing handler for event type '".concat(event.type, "'"),
|
|
214
|
+
});
|
|
215
|
+
return [4 /*yield*/, this.handler[handlerContract.version]({
|
|
227
216
|
event: event,
|
|
228
217
|
source: this.source,
|
|
218
|
+
span: span,
|
|
229
219
|
})];
|
|
230
220
|
case 2:
|
|
231
|
-
_handleOutput =
|
|
221
|
+
_handleOutput = _e.sent();
|
|
232
222
|
if (!_handleOutput)
|
|
233
223
|
return [2 /*return*/, []];
|
|
234
224
|
outputs = [];
|
|
@@ -239,11 +229,10 @@ var ArvoEventHandler = /** @class */ (function (_super) {
|
|
|
239
229
|
outputs = [_handleOutput];
|
|
240
230
|
}
|
|
241
231
|
eventFactory_1 = (0, arvo_core_1.createArvoEventFactory)(handlerContract);
|
|
242
|
-
result = (0, utils_1.eventHandlerOutputEventCreator)(outputs, otelSpanHeaders, this.source, event, this.executionunits, function (param,
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
});
|
|
232
|
+
result = (0, utils_1.eventHandlerOutputEventCreator)(outputs, otelSpanHeaders, this.source, event, this.executionunits, function (param, extensions) { return eventFactory_1.emits(param, extensions); });
|
|
233
|
+
(0, arvo_core_1.logToSpan)({
|
|
234
|
+
level: 'INFO',
|
|
235
|
+
message: "Event processing completed successfully. Generated ".concat(result.length, " event(s)"),
|
|
247
236
|
});
|
|
248
237
|
(0, arvo_core_1.logToSpan)({
|
|
249
238
|
level: 'INFO',
|
|
@@ -251,12 +240,12 @@ var ArvoEventHandler = /** @class */ (function (_super) {
|
|
|
251
240
|
});
|
|
252
241
|
return [2 /*return*/, result];
|
|
253
242
|
case 3:
|
|
254
|
-
error_1 =
|
|
243
|
+
error_1 = _e.sent();
|
|
255
244
|
eventFactory = (0, arvo_core_1.createArvoEventFactory)(this.contract.version('latest'));
|
|
256
245
|
(0, arvo_core_1.exceptionToSpan)(error_1);
|
|
257
246
|
span.setStatus({
|
|
258
247
|
code: api_1.SpanStatusCode.ERROR,
|
|
259
|
-
message: error_1.message,
|
|
248
|
+
message: "Event processing failed: ".concat(error_1.message),
|
|
260
249
|
});
|
|
261
250
|
result = eventFactory.systemError({
|
|
262
251
|
source: this.source,
|
|
@@ -269,7 +258,7 @@ var ArvoEventHandler = /** @class */ (function (_super) {
|
|
|
269
258
|
traceparent: (_b = otelSpanHeaders.traceparent) !== null && _b !== void 0 ? _b : undefined,
|
|
270
259
|
tracestate: (_c = otelSpanHeaders.tracestate) !== null && _c !== void 0 ? _c : undefined,
|
|
271
260
|
accesscontrol: (_d = event.accesscontrol) !== null && _d !== void 0 ? _d : undefined,
|
|
272
|
-
}, {}
|
|
261
|
+
}, {});
|
|
273
262
|
Object.entries(result.otelAttributes).forEach(function (_a) {
|
|
274
263
|
var key = _a[0], value = _a[1];
|
|
275
264
|
return span.setAttribute("to_emit.0.".concat(key), value);
|
|
@@ -281,7 +270,8 @@ var ArvoEventHandler = /** @class */ (function (_super) {
|
|
|
281
270
|
case 5: return [2 /*return*/];
|
|
282
271
|
}
|
|
283
272
|
});
|
|
284
|
-
}); }
|
|
273
|
+
}); },
|
|
274
|
+
})];
|
|
285
275
|
case 1: return [2 /*return*/, _a.sent()];
|
|
286
276
|
}
|
|
287
277
|
});
|
|
@@ -289,19 +279,12 @@ var ArvoEventHandler = /** @class */ (function (_super) {
|
|
|
289
279
|
};
|
|
290
280
|
Object.defineProperty(ArvoEventHandler.prototype, "systemErrorSchema", {
|
|
291
281
|
/**
|
|
292
|
-
* Provides
|
|
293
|
-
*
|
|
294
|
-
* @returns An object containing the error event type and schema.
|
|
295
|
-
*
|
|
296
|
-
* @remarks
|
|
297
|
-
* This getter defines the structure for system error events that may be emitted
|
|
298
|
-
* when an unexpected error occurs during event handling. The error event type
|
|
299
|
-
* is prefixed with 'sys.' followed by the contract's accepted event type and '.error'.
|
|
300
|
-
* The schema used for these error events is the standard ArvoErrorSchema.
|
|
282
|
+
* Provides access to the system error event schema configuration.
|
|
283
|
+
* The schema defines the structure of error events emitted during execution failures.
|
|
301
284
|
*
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
*
|
|
285
|
+
* Error events follow the naming convention: sys.<contract-type>.error
|
|
286
|
+
* For example, a contract handling 'user.created' events will emit error events
|
|
287
|
+
* with the type 'sys.user.created.error'.
|
|
305
288
|
*/
|
|
306
289
|
get: function () {
|
|
307
290
|
return this.contract.systemError;
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ArvoContract, ArvoEvent, CreateArvoEvent,
|
|
1
|
+
import { Span, SpanOptions } from '@opentelemetry/api';
|
|
2
|
+
import { ArvoContract, ArvoEvent, CreateArvoEvent, VersionedArvoContract, ArvoSemanticVersion } from 'arvo-core';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
/**
|
|
5
5
|
* Represents the input for an ArvoEvent handler function.
|
|
6
|
-
* @template TAccepts - The type of ArvoContractRecord that the handler accepts.
|
|
7
6
|
*/
|
|
8
7
|
export type ArvoEventHandlerFunctionInput<TContract extends VersionedArvoContract<any, any>> = {
|
|
9
8
|
/** The ArvoEvent object. */
|
|
10
9
|
event: ArvoEvent<z.infer<TContract['accepts']['schema']>, Record<string, any>, TContract['accepts']['type']>;
|
|
11
10
|
/** The source field data of the handler */
|
|
12
11
|
source: string;
|
|
12
|
+
/** The OpenTelemetry span */
|
|
13
|
+
span: Span;
|
|
13
14
|
};
|
|
14
15
|
/**
|
|
15
16
|
* Represents the output of an ArvoEvent handler function.
|
|
16
|
-
* @template TContract - The type of ArvoContract that the handler is associated with.
|
|
17
17
|
*/
|
|
18
18
|
export type ArvoEventHandlerFunctionOutput<TContract extends VersionedArvoContract<any, any>> = {
|
|
19
|
-
[K in keyof TContract['emits']]:
|
|
19
|
+
[K in keyof TContract['emits']]: Pick<CreateArvoEvent<z.infer<TContract['emits'][K]>, K & string>, 'id' | 'time' | 'type' | 'data' | 'to' | 'accesscontrol' | 'redirectto'> & {
|
|
20
20
|
/**
|
|
21
21
|
* An optional override for the execution units of this specific event.
|
|
22
22
|
*
|
|
@@ -31,29 +31,14 @@ export type ArvoEventHandlerFunctionOutput<TContract extends VersionedArvoContra
|
|
|
31
31
|
}[keyof TContract['emits']];
|
|
32
32
|
/**
|
|
33
33
|
* Defines the structure of an ArvoEvent handler function.
|
|
34
|
-
* @template TContract - The type of ArvoContract that the handler is associated with.
|
|
35
34
|
*/
|
|
36
35
|
export type ArvoEventHandlerFunction<TContract extends ArvoContract> = {
|
|
37
36
|
[V in ArvoSemanticVersion & keyof TContract['versions']]: (params: ArvoEventHandlerFunctionInput<VersionedArvoContract<TContract, V>>) => Promise<Array<ArvoEventHandlerFunctionOutput<VersionedArvoContract<TContract, V>>> | ArvoEventHandlerFunctionOutput<VersionedArvoContract<TContract, V>> | void>;
|
|
38
37
|
};
|
|
39
38
|
/**
|
|
40
39
|
* Interface for an ArvoEvent handler.
|
|
41
|
-
* @template T - The type of the contract (defaults to string).
|
|
42
|
-
* @template TAccepts - The type of ArvoContractRecord that the handler accepts.
|
|
43
|
-
* @template TEmits - The type of ArvoContractRecord that the handler emits.
|
|
44
40
|
*/
|
|
45
41
|
export interface IArvoEventHandler<TContract extends ArvoContract> {
|
|
46
|
-
/**
|
|
47
|
-
* An override source for emitted events.
|
|
48
|
-
* @remarks
|
|
49
|
-
* When provided, this value will be used as the source for emitted events
|
|
50
|
-
* instead of the `contract.accepts.type`. Use this very carefully as it may
|
|
51
|
-
* reduce system transparency and make event tracking more difficult.
|
|
52
|
-
*
|
|
53
|
-
* It's recommended to rely on the default source (`contract.accepts.type`)
|
|
54
|
-
* whenever possible to maintain consistent and traceable event chains.
|
|
55
|
-
*/
|
|
56
|
-
source?: string;
|
|
57
42
|
/**
|
|
58
43
|
* The contract for the handler defining its input and outputs as well as the description.
|
|
59
44
|
*/
|
|
@@ -70,15 +55,7 @@ export interface IArvoEventHandler<TContract extends ArvoContract> {
|
|
|
70
55
|
*/
|
|
71
56
|
handler: ArvoEventHandlerFunction<TContract>;
|
|
72
57
|
/**
|
|
73
|
-
* The OpenTelemetry span
|
|
74
|
-
* executor.
|
|
75
|
-
* @param [openInference] - The OpenInference span kind. Default is "CHAIN"
|
|
76
|
-
* @param [arvoExecution] - The ArvoExecution span kind. Default is "EVENT_HANDLER"
|
|
77
|
-
* @param [openTelemetry] - The OpenTelemetry span kind. Default is "INTERNAL"
|
|
58
|
+
* The OpenTelemetry span options
|
|
78
59
|
*/
|
|
79
|
-
|
|
80
|
-
openInference?: OpenInferenceSpanKind;
|
|
81
|
-
arvoExecution?: ArvoExecutionSpanKind;
|
|
82
|
-
openTelemetry?: SpanKind;
|
|
83
|
-
};
|
|
60
|
+
spanOptions?: SpanOptions;
|
|
84
61
|
}
|
|
@@ -1,36 +1,19 @@
|
|
|
1
1
|
import { ArvoEventRouter } from '.';
|
|
2
2
|
import { IArvoEventRouter } from './types';
|
|
3
3
|
/**
|
|
4
|
-
* Creates
|
|
4
|
+
* Creates a new ArvoEventRouter instance with the provided configuration.
|
|
5
|
+
* Validates source format and ensures unique handlers per event type.
|
|
5
6
|
*
|
|
6
|
-
* @param
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* @throws
|
|
10
|
-
* @throws {Error} If the provided source is an invalid string.
|
|
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
11
|
*
|
|
12
12
|
* @example
|
|
13
13
|
* const router = createArvoEventRouter({
|
|
14
|
-
* source: '
|
|
15
|
-
* handlers: [
|
|
14
|
+
* source: 'payment.service',
|
|
15
|
+
* handlers: [paymentHandler, notificationHandler],
|
|
16
16
|
* executionunits: 10
|
|
17
17
|
* });
|
|
18
|
-
*
|
|
19
|
-
* @remarks
|
|
20
|
-
* This function is a factory method that simplifies the creation of an ArvoEventRouter instance.
|
|
21
|
-
* It encapsulates the instantiation process, allowing for a more concise and readable way to create routers.
|
|
22
|
-
*
|
|
23
|
-
* The `IArvoEventRouter` parameter should include:
|
|
24
|
-
* - `source`: (optional) A string identifying the source of the router. Used to match the `event.to` field.
|
|
25
|
-
* - `handlers`: An array of ArvoEventHandler instances that the router will use to process events.
|
|
26
|
-
* - `executionunits`: A number representing the default execution cost of the function.
|
|
27
|
-
*
|
|
28
|
-
* The created ArvoEventRouter will:
|
|
29
|
-
* - Validate the source string if provided.
|
|
30
|
-
* - Check for and prevent duplicate handlers for the same event type.
|
|
31
|
-
* - Set up internal data structures for efficient event routing.
|
|
32
|
-
*
|
|
33
|
-
* @see {@link ArvoEventRouter} for more details on the router's functionality.
|
|
34
|
-
* @see {@link IArvoEventRouter} for the structure of the configuration object.
|
|
35
18
|
*/
|
|
36
19
|
export declare const createArvoEventRouter: (param: IArvoEventRouter) => ArvoEventRouter;
|
|
@@ -3,37 +3,20 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.createArvoEventRouter = void 0;
|
|
4
4
|
var _1 = require(".");
|
|
5
5
|
/**
|
|
6
|
-
* Creates
|
|
6
|
+
* Creates a new ArvoEventRouter instance with the provided configuration.
|
|
7
|
+
* Validates source format and ensures unique handlers per event type.
|
|
7
8
|
*
|
|
8
|
-
* @param
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* @throws
|
|
12
|
-
* @throws {Error} If the provided source is an invalid string.
|
|
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
13
|
*
|
|
14
14
|
* @example
|
|
15
15
|
* const router = createArvoEventRouter({
|
|
16
|
-
* source: '
|
|
17
|
-
* handlers: [
|
|
16
|
+
* source: 'payment.service',
|
|
17
|
+
* handlers: [paymentHandler, notificationHandler],
|
|
18
18
|
* executionunits: 10
|
|
19
19
|
* });
|
|
20
|
-
*
|
|
21
|
-
* @remarks
|
|
22
|
-
* This function is a factory method that simplifies the creation of an ArvoEventRouter instance.
|
|
23
|
-
* It encapsulates the instantiation process, allowing for a more concise and readable way to create routers.
|
|
24
|
-
*
|
|
25
|
-
* The `IArvoEventRouter` parameter should include:
|
|
26
|
-
* - `source`: (optional) A string identifying the source of the router. Used to match the `event.to` field.
|
|
27
|
-
* - `handlers`: An array of ArvoEventHandler instances that the router will use to process events.
|
|
28
|
-
* - `executionunits`: A number representing the default execution cost of the function.
|
|
29
|
-
*
|
|
30
|
-
* The created ArvoEventRouter will:
|
|
31
|
-
* - Validate the source string if provided.
|
|
32
|
-
* - Check for and prevent duplicate handlers for the same event type.
|
|
33
|
-
* - Set up internal data structures for efficient event routing.
|
|
34
|
-
*
|
|
35
|
-
* @see {@link ArvoEventRouter} for more details on the router's functionality.
|
|
36
|
-
* @see {@link IArvoEventRouter} for the structure of the configuration object.
|
|
37
20
|
*/
|
|
38
21
|
var createArvoEventRouter = function (param) {
|
|
39
22
|
return new _1.ArvoEventRouter(param);
|