UNPKG

arvo-core

Version:

This core package contains all the core classes and components of the Arvo Event Driven System

157 lines (156 loc) 9.81 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ArvoOrchestratorEventFactory = void 0; var _1 = __importDefault(require(".")); var helpers_1 = require("../ArvoEvent/helpers"); var schema_1 = require("../ArvoEvent/schema"); var ArvoOrchestrationSubject_1 = __importDefault(require("../ArvoOrchestrationSubject")); var OpenTelemetry_1 = require("../OpenTelemetry"); var utils_1 = require("../utils"); var utils_2 = require("./utils"); /** * Factory class for creating and validating orchestrator-specific events with managed subject hierarchies. * Extends ArvoEventFactory with parent-child subject relationship handling and orchestration flows. * * @example * ```typescript * const contract = createArvoOrchestratorContract({ ... }); * * const factory = createArvoOrchestratorEventFactory(contract.version('1.0.0')); * ``` */ var ArvoOrchestratorEventFactory = /** @class */ (function (_super) { __extends(ArvoOrchestratorEventFactory, _super); function ArvoOrchestratorEventFactory(contract) { var _this = this; var _a; if (((_a = contract.metadata) === null || _a === void 0 ? void 0 : _a.contractType) !== 'ArvoOrchestratorContract') { throw new Error('This factory can only be used for ArvoOrchestratorContract'); } _this = _super.call(this, contract) || this; _this._name = 'ArvoOrchestratorEventFactory'; return _this; } /** * Initializes a new orchestration event, handling parent-child subject relationships. * - If parentSubject$$ is provided, creates a child subject * - If no parent, creates a new root orchestration subject * * @param event - Event configuration without type/schema/subject * @param [extensions] - Optional additional properties * @returns Validated orchestration event with proper subject hierarchy * * @throws Error if event validation fails */ ArvoOrchestratorEventFactory.prototype.init = function (event, extensions) { var _this = this; return OpenTelemetry_1.ArvoOpenTelemetry.getInstance().startActiveSpan({ name: "".concat(this._name, ".init"), spanOptions: (0, utils_2.createSpanOptions)(this.contract), fn: function (span) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; var otelHeaders = (0, OpenTelemetry_1.currentOpenTelemetryHeaders)(); var validationResult = _this.contract.accepts.schema.safeParse(event.data); if (!validationResult.success) { throw new Error("Init Event data validation failed: ".concat(validationResult.error.message)); } var parentSubject = validationResult.data.parentSubject$$; var eventSubject = (_a = event.subject) !== null && _a !== void 0 ? _a : (parentSubject ? ArvoOrchestrationSubject_1.default.from({ orchestator: _this.contract.accepts.type, subject: parentSubject, version: _this.contract.version, meta: Object.fromEntries(Object.entries({ redirectto: (_b = event.redirectto) !== null && _b !== void 0 ? _b : ArvoOrchestrationSubject_1.default.parse(parentSubject).execution.initiator, }).filter(function (item) { return Boolean(item[1]); })), }) : ArvoOrchestrationSubject_1.default.new({ orchestator: _this.contract.accepts.type, initiator: event.source, version: _this.contract.version, domain: event.domain === null ? undefined : ((_d = (_c = event.domain) !== null && _c !== void 0 ? _c : _this.contract.domain) !== null && _d !== void 0 ? _d : undefined), meta: event.redirectto ? { redirectto: event.redirectto, } : undefined, })); var generatedEvent = (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { subject: eventSubject, traceparent: (_f = (_e = event.traceparent) !== null && _e !== void 0 ? _e : otelHeaders.traceparent) !== null && _f !== void 0 ? _f : undefined, tracestate: (_h = (_g = event.tracestate) !== null && _g !== void 0 ? _g : otelHeaders.tracestate) !== null && _h !== void 0 ? _h : undefined, type: _this.contract.accepts.type, datacontenttype: schema_1.ArvoDataContentType, dataschema: utils_1.EventDataschemaUtil.create(_this.contract), data: validationResult.data, domain: event.domain === null ? undefined : ((_k = (_j = event.domain) !== null && _j !== void 0 ? _j : _this.contract.domain) !== null && _k !== void 0 ? _k : undefined) }), extensions, { disable: true }); span.setAttributes(generatedEvent.otelAttributes); return generatedEvent; }, }); }; /** * Creates a completion event for the orchestration flow. * Uses the contract's configured complete event type from metadata. * * @param event - Completion event configuration * @param [extensions] - Optional additional properties * @returns Validated completion event * * @throws Error if event validation fails or complete event type not configured */ ArvoOrchestratorEventFactory.prototype.complete = function (event, extensions) { var _this = this; return OpenTelemetry_1.ArvoOpenTelemetry.getInstance().startActiveSpan({ name: "".concat(this._name, ".complete"), spanOptions: (0, utils_2.createSpanOptions)(this.contract), fn: function (span) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o; var otelHeaders = (0, OpenTelemetry_1.currentOpenTelemetryHeaders)(); var validationResult = (_b = (_a = _this.contract.emits) === null || _a === void 0 ? void 0 : _a[_this.contract.metadata.completeEventType]) === null || _b === void 0 ? void 0 : _b.safeParse(event.data); if (!(validationResult === null || validationResult === void 0 ? void 0 : validationResult.success)) { var msg = (_d = (_c = validationResult === null || validationResult === void 0 ? void 0 : validationResult.error) === null || _c === void 0 ? void 0 : _c.message) !== null && _d !== void 0 ? _d : "No schema available for ".concat(_this.contract.metadata.completeEventType); throw new Error("Emit Event data validation failed: ".concat(msg)); } var eventType = _this.contract.metadata.completeEventType; var eventSubject = (_e = event.subject) !== null && _e !== void 0 ? _e : ArvoOrchestrationSubject_1.default.new({ initiator: event.source, version: _this.contract.version, orchestator: eventType, domain: event.domain === null ? undefined : ((_g = (_f = event.domain) !== null && _f !== void 0 ? _f : _this.contract.domain) !== null && _g !== void 0 ? _g : undefined), meta: event.redirectto ? { redirectto: event.redirectto, } : undefined, }); var generatedEvent = (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { subject: eventSubject, type: eventType, traceparent: (_j = (_h = event.traceparent) !== null && _h !== void 0 ? _h : otelHeaders.traceparent) !== null && _j !== void 0 ? _j : undefined, tracestate: (_l = (_k = event.tracestate) !== null && _k !== void 0 ? _k : otelHeaders.tracestate) !== null && _l !== void 0 ? _l : undefined, datacontenttype: schema_1.ArvoDataContentType, dataschema: utils_1.EventDataschemaUtil.create(_this.contract), data: validationResult.data, domain: event.domain === null ? undefined : ((_o = (_m = event.domain) !== null && _m !== void 0 ? _m : _this.contract.domain) !== null && _o !== void 0 ? _o : undefined) }), extensions, { disable: true }); span.setAttributes(generatedEvent.otelAttributes); return generatedEvent; }, }); }; return ArvoOrchestratorEventFactory; }(_1.default)); exports.ArvoOrchestratorEventFactory = ArvoOrchestratorEventFactory;