UNPKG

arvo-core

Version:

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

213 lines (212 loc) 9.72 kB
"use strict"; 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 }); var pako_1 = __importDefault(require("pako")); var uuid_1 = require("uuid"); var WildCardArvoSemanticVersion_1 = require("../ArvoContract/WildCardArvoSemanticVersion"); var utils_1 = require("../utils"); var schema_1 = require("./schema"); /** * Handles the creation and parsing of Arvo orchestration subjects. */ // biome-ignore lint/complexity/noStaticOnlyClass: This needs to be a static class to group methods together var ArvoOrchestrationSubject = /** @class */ (function () { function ArvoOrchestrationSubject() { } /** * Creates a new Arvo orchestration subject with basic required parameters. * This is a convenience method that wraps the more detailed {@link create} method. * * @param param - Configuration object for the orchestration subject * @param param.orchestator - Name identifier of the orchestrator * @param param.version - Version of the orchestrator. If null, defaults to {@link WildCardArvoSemanticVersion} * @param param.initiator - Identifier of the entity initiating the orchestration * @param param.meta - Optional metadata key-value pairs for additional orchestration context * @returns A base64 encoded string containing the compressed orchestration subject data * @throws Error if the provided parameters result in invalid subject content * * @example * ```typescript * const subject = ArvoOrchestrationSubject.new({ * orchestator: "mainProcess", * version: "1.0.0", * initiator: "systemA" * }); * * // With metadata * const subjectWithMeta = ArvoOrchestrationSubject.new({ * orchestator: "com.company.mainProcess", * version: "1.0.0", * initiator: "com.company.systemA", * meta: { * priority: "high", * environment: "production" * } * }); * ``` */ ArvoOrchestrationSubject.new = function (param) { var _a, _b, _c; return ArvoOrchestrationSubject.create({ orchestrator: { name: param.orchestator, version: (_a = param.version) !== null && _a !== void 0 ? _a : WildCardArvoSemanticVersion_1.WildCardArvoSemanticVersion, }, execution: { id: (0, uuid_1.v4)(), initiator: param.initiator, domain: (_b = param.domain) !== null && _b !== void 0 ? _b : null, }, meta: (_c = param.meta) !== null && _c !== void 0 ? _c : {}, }); }; /** * Creates a new orchestration subject string from an existing parent subject. * This method parses the parent subject, merges its metadata with new metadata (if available), * and creates a new subject with updated orchestrator information while maintaining * the relationship to the parent context. * * @param param - Configuration object for creating a new subject from a parent * @param param.orchestator - Name identifier of the new orchestrator * @param param.version - Version of the new orchestrator. If null, defaults to {@link WildCardArvoSemanticVersion} * @param param.subject - Base64 encoded string of the parent orchestration subject * @param param.meta - Optional additional metadata to merge with the parent's metadata * @returns A new base64 encoded string containing the compressed orchestration subject data * @throws Error if the parent subject is invalid or if the new parameters result in invalid subject content * * @example * ```typescript * // Create a parent subject * const parentSubject = ArvoOrchestrationSubject.new({ * orchestator: "parentProcess", * version: "1.0.0", * initiator: "systemA", * meta: { environment: "production" } * }); * * // Create a new subject from the parent * const childSubject = ArvoOrchestrationSubject.from({ * orchestator: "childProcess", * version: "2.0.0", * subject: parentSubject, * meta: { step: "processing" } // Will be merged with parent's metadata * }); * ``` */ ArvoOrchestrationSubject.from = function (param) { var _a, _b, _c, _d, _e; var parsedSubject = ArvoOrchestrationSubject.parse(param.subject); return ArvoOrchestrationSubject.new({ initiator: parsedSubject.orchestrator.name, version: (_a = param.version) !== null && _a !== void 0 ? _a : WildCardArvoSemanticVersion_1.WildCardArvoSemanticVersion, orchestator: param.orchestator, domain: param.domain === null ? undefined : ((_c = (_b = param.domain) !== null && _b !== void 0 ? _b : parsedSubject.execution.domain) !== null && _c !== void 0 ? _c : undefined), meta: __assign(__assign({}, ((_d = parsedSubject.meta) !== null && _d !== void 0 ? _d : {})), ((_e = param.meta) !== null && _e !== void 0 ? _e : {})), }); }; /** * Creates an Arvo orchestration subject from detailed content parameters. * The content is validated, compressed using zlib, and encoded in base64 format. * * @param param - Detailed orchestration subject content following the {@link ArvoOrchestrationSubjectContent} structure * @returns A base64 encoded string containing the compressed orchestration subject data * @throws Error if validation fails or compression encounters an error * * @example * ```typescript * const subject = ArvoOrchestrationSubject.create({ * orchestrator: { * name: "mainProcess", * version: "1.0.0" * }, * execution: { * id: "550e8400-e29b-41d4-a716-446655440000", * initiator: "systemA" * } * }); * ``` */ ArvoOrchestrationSubject.create = function (param) { try { var validationResult = schema_1.ArvoOrchestrationSubjectContentSchema.safeParse(param); if (!validationResult.success) { throw new Error("Invalid ArvoOrchestrationContextType: ".concat(validationResult.error)); } var jsonString = JSON.stringify(param); var compressed = pako_1.default.deflate(new TextEncoder().encode(jsonString)); return Buffer.from(compressed).toString('base64'); } catch (e) { throw new Error((0, utils_1.cleanString)("\n Error creating orchestration subject string from the provided context. \n Error -> ".concat(e.message, " \n Context -> ").concat(JSON.stringify(param, null, 2), "\n "))); } }; /** * Parses a base64 encoded orchestration subject string back into its structured content form. * Performs decompression, JSON parsing, and validation of the subject content. * * @param subject - Base64 encoded string representing the compressed orchestration subject * @returns The decoded and validated {@link ArvoOrchestrationSubjectContent} * @throws Error if decompression, parsing, or validation fails * * @example * ```typescript * const content = ArvoOrchestrationSubject.parse(encodedSubject); * console.log(content.orchestrator.name); * console.log(content.execution.id); * ``` */ ArvoOrchestrationSubject.parse = function (subject) { try { var compressed = Buffer.from(subject, 'base64'); var decompressed = pako_1.default.inflate(compressed); var jsonString = new TextDecoder().decode(decompressed); var parsed = JSON.parse(jsonString); var validationResult = schema_1.ArvoOrchestrationSubjectContentSchema.safeParse(parsed); if (!validationResult.success) { throw new Error("Invalid ArvoOrchestrationContextType: ".concat(validationResult.error)); } return parsed; } catch (e) { throw new Error((0, utils_1.cleanString)("\n Error parsing orchestration subject string to the context. \n Error -> ".concat(e.message, " \n subject -> ").concat(subject, "\n "))); } }; /** * Validates if a string represents a valid Arvo orchestration subject. * A valid subject must: * - Be base64 encoded * - Contain zlib-compressed JSON data * - Match the ArvoOrchestrationSubjectContent schema when decoded * - Include valid orchestrator and execution details * * Use this method for validating subjects before processing them in * orchestration workflows or when receiving subjects from external sources. * * @param data - The string to validate as an orchestration subject * @returns boolean - True if string is a valid orchestration subject, false otherwise */ ArvoOrchestrationSubject.isValid = function (data) { try { ArvoOrchestrationSubject.parse(data); return true; } catch (_a) { return false; } }; return ArvoOrchestrationSubject; }()); exports.default = ArvoOrchestrationSubject;