arvo-core 2.0.8 → 2.0.10

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.
@@ -2,39 +2,86 @@ import ArvoEvent from '.';
2
2
  import { ArvoEventData, CloudEventExtension, CreateArvoEvent } from './types';
3
3
  import { ExecutionOpenTelemetryConfiguration } from '../OpenTelemetry/types';
4
4
  /**
5
- * Creates an ArvoEvent with the provided data and extensions.
5
+ * Creates a strongly-typed ArvoEvent with configurable telemetry options.
6
6
  *
7
- * This function creates a new ArvoEvent instance using the provided event data and optional extensions.
7
+ * @template TData - Event data type extending ArvoEventData
8
+ * @template TExtension - Cloud event extension type
9
+ * @template TType - String literal type for event type
8
10
  *
9
- * @template TData - The type of the event data, extending ArvoEventData.
10
- * @template TExtension - The type of the cloud event extension, extending CloudEventExtension.
11
- * @template TType - The type name of the event
11
+ * @param event - Event configuration and data
12
+ * @param [extensions] - Optional cloud event extensions
13
+ * @param [opentelemetry] - OpenTelemetry configuration with options:
14
+ * - disable - Completely disables telemetry if true
15
+ * - tracer - Custom OpenTelemetry tracer instance
12
16
  *
13
- * @param {CreateArvoEvent<TData>} event - The event data and metadata to create the ArvoEvent.
14
- * @param {TExtension} [extensions] - Optional cloud event extensions.
15
- * @param {ExecutionOpenTelemetryConfiguration} [opentelemetry] - Optional opentelemetry configuration object
17
+ * @returns ArvoEvent instance
16
18
  *
17
- * @returns {ArvoEvent<TData, TExtension>} The created ArvoEvent instance.
18
- *
19
- * @throws {Error} If there's an error during the creation process.
19
+ * @example
20
+ * ```typescript
21
+ * // With default telemetry
22
+ * const event = createArvoEvent({
23
+ * type: 'order.created',
24
+ * source: '/orders',
25
+ * subject: 'order-123',
26
+ * data: orderData
27
+ * });
20
28
  *
21
- * @remarks
22
- * - If no `id` is provided in the event object, a UUID v4 will be generated.
23
- * - If no `time` is provided, the current timestamp will be used.
24
- * - If no `datacontenttype` is provided, it defaults to `application/cloudevents+json;charset=UTF-8;profile=arvo`.
25
- * - If a non-compatible `datacontenttype` is provided, a warning will be logged to the span.
26
- * - The `source`, `subject`, `to`, `redirectto`, and `dataschema` fields are URI-encoded.
27
- * - If no `to` (null, undefined or empty string) is provided, then the `type` value is used by default
29
+ * // With disabled telemetry
30
+ * const event = createArvoEvent(
31
+ * {
32
+ * type: 'order.created',
33
+ * source: '/orders',
34
+ * subject: 'order-123',
35
+ * data: orderData
36
+ * },
37
+ * undefined,
38
+ * { disable: true }
39
+ * );
28
40
  *
29
- * @example
41
+ * // With custom tracer
30
42
  * const event = createArvoEvent(
31
43
  * {
32
- * type: 'com.example.event',
33
- * source: '/example/source',
34
- * subject: 'example-subject',
35
- * data: { key: 'value' }
44
+ * type: 'order.created',
45
+ * source: '/orders',
46
+ * subject: 'order-123',
47
+ * data: orderData
36
48
  * },
37
- * { customextension: 'value' },
49
+ * undefined,
50
+ * { tracer: customTracer }
38
51
  * );
52
+ * ```
53
+ *
54
+ * @remarks
55
+ * This function provides several key features:
56
+ *
57
+ * 1. **Type Safety**:
58
+ * - Ensures event data matches specified type
59
+ * - Validates extension structure
60
+ * - Provides type-safe event type strings
61
+ *
62
+ * 2. **Default Handling**:
63
+ * - Generates UUID if no ID provided
64
+ * - Sets current timestamp if no time provided
65
+ * - Uses default ArvoDataContentType if none specified
66
+ *
67
+ * 3. **URI Handling**:
68
+ * - Automatically encodes URI components (source, subject, to, redirectto, dataschema)
69
+ * - Validates URI format
70
+ *
71
+ * 4. **OpenTelemetry Integration**:
72
+ * - Creates spans for event creation
73
+ * - Tracks errors and warnings
74
+ * - Propagates trace context
75
+ *
76
+ * 5. **Validation**:
77
+ * - Checks data content type compatibility
78
+ * - Validates required fields
79
+ * - Ensures URI format correctness
80
+ *
81
+ * @see {@link ArvoEvent} For the structure of the created event
82
+ * @see {@link ArvoDataContentType} For supported content types
83
+ * @see {@link CloudEventExtension} For extension structure
39
84
  */
40
- export declare const createArvoEvent: <TData extends ArvoEventData, TExtension extends CloudEventExtension, TType extends string>(event: CreateArvoEvent<TData, TType>, extensions?: TExtension, opentelemetry?: ExecutionOpenTelemetryConfiguration) => ArvoEvent<TData, TExtension, TType>;
85
+ export declare const createArvoEvent: <TData extends ArvoEventData, TExtension extends CloudEventExtension, TType extends string>(event: CreateArvoEvent<TData, TType>, extensions?: TExtension, opentelemetry?: Partial<ExecutionOpenTelemetryConfiguration & {
86
+ disable: boolean;
87
+ }>) => ArvoEvent<TData, TExtension, TType>;
@@ -11,75 +11,144 @@ var utils_1 = require("../utils");
11
11
  var schema_1 = require("./schema");
12
12
  var uuid_1 = require("uuid");
13
13
  /**
14
- * Creates an ArvoEvent with the provided data and extensions.
14
+ * Internal generator function for creating ArvoEvent instances.
15
15
  *
16
- * This function creates a new ArvoEvent instance using the provided event data and optional extensions.
16
+ * @template TData - Type of the event data
17
+ * @template TExtension - Type of cloud event extensions
18
+ * @param {CreateArvoEvent<any, any>} event - The event configuration and data
19
+ * @param {any} extensions - Cloud event extensions
20
+ * @param {ReturnType<typeof currentOpenTelemetryHeaders>} otelHeaders - OpenTelemetry headers
21
+ * @returns {ArvoEvent<any, any, any>} A new ArvoEvent instance
17
22
  *
18
- * @template TData - The type of the event data, extending ArvoEventData.
19
- * @template TExtension - The type of the cloud event extension, extending CloudEventExtension.
20
- * @template TType - The type name of the event
21
- *
22
- * @param {CreateArvoEvent<TData>} event - The event data and metadata to create the ArvoEvent.
23
- * @param {TExtension} [extensions] - Optional cloud event extensions.
24
- * @param {ExecutionOpenTelemetryConfiguration} [opentelemetry] - Optional opentelemetry configuration object
23
+ * @remarks
24
+ * This function handles the actual creation of the ArvoEvent instance, including:
25
+ * - Generation of default values for optional fields
26
+ * - URI encoding of relevant fields
27
+ * - Validation of data content type
28
+ * - Integration with OpenTelemetry headers
29
+ * ```
30
+ */
31
+ var generator = function (event, extensions, otelHeaders) {
32
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
33
+ if (event.datacontenttype && event.datacontenttype !== schema_1.ArvoDataContentType) {
34
+ var warning = (0, utils_1.cleanString)("\n Warning! The provided datacontenttype(=".concat(event.datacontenttype, ")\n is not ArvoEvent compatible (=").concat(schema_1.ArvoDataContentType, "). There may \n be some limited functionality.\n "));
35
+ (0, OpenTelemetry_1.logToSpan)({
36
+ level: 'WARNING',
37
+ message: warning,
38
+ });
39
+ }
40
+ return new _1.default({
41
+ id: (_a = event.id) !== null && _a !== void 0 ? _a : (0, uuid_1.v4)(),
42
+ type: event.type,
43
+ accesscontrol: (_b = event.accesscontrol) !== null && _b !== void 0 ? _b : null,
44
+ executionunits: (_c = event.executionunits) !== null && _c !== void 0 ? _c : null,
45
+ traceparent: (_e = (_d = event.traceparent) !== null && _d !== void 0 ? _d : otelHeaders.traceparent) !== null && _e !== void 0 ? _e : null,
46
+ tracestate: (_g = (_f = event.tracestate) !== null && _f !== void 0 ? _f : otelHeaders.tracestate) !== null && _g !== void 0 ? _g : null,
47
+ datacontenttype: (_h = event.datacontenttype) !== null && _h !== void 0 ? _h : schema_1.ArvoDataContentType,
48
+ specversion: (_j = event.specversion) !== null && _j !== void 0 ? _j : '1.0',
49
+ time: (_k = event.time) !== null && _k !== void 0 ? _k : (0, utils_1.createTimestamp)(),
50
+ source: encodeURI(event.source),
51
+ subject: encodeURI(event.subject),
52
+ to: event.to ? encodeURI(event.to) : encodeURI(event.type),
53
+ redirectto: event.redirectto ? encodeURI(event.redirectto) : null,
54
+ dataschema: event.dataschema ? encodeURI(event.dataschema) : null,
55
+ }, event.data, extensions);
56
+ };
57
+ /**
58
+ * Creates a strongly-typed ArvoEvent with configurable telemetry options.
25
59
  *
26
- * @returns {ArvoEvent<TData, TExtension>} The created ArvoEvent instance.
60
+ * @template TData - Event data type extending ArvoEventData
61
+ * @template TExtension - Cloud event extension type
62
+ * @template TType - String literal type for event type
27
63
  *
28
- * @throws {Error} If there's an error during the creation process.
64
+ * @param event - Event configuration and data
65
+ * @param [extensions] - Optional cloud event extensions
66
+ * @param [opentelemetry] - OpenTelemetry configuration with options:
67
+ * - disable - Completely disables telemetry if true
68
+ * - tracer - Custom OpenTelemetry tracer instance
29
69
  *
30
- * @remarks
31
- * - If no `id` is provided in the event object, a UUID v4 will be generated.
32
- * - If no `time` is provided, the current timestamp will be used.
33
- * - If no `datacontenttype` is provided, it defaults to `application/cloudevents+json;charset=UTF-8;profile=arvo`.
34
- * - If a non-compatible `datacontenttype` is provided, a warning will be logged to the span.
35
- * - The `source`, `subject`, `to`, `redirectto`, and `dataschema` fields are URI-encoded.
36
- * - If no `to` (null, undefined or empty string) is provided, then the `type` value is used by default
70
+ * @returns ArvoEvent instance
37
71
  *
38
72
  * @example
73
+ * ```typescript
74
+ * // With default telemetry
75
+ * const event = createArvoEvent({
76
+ * type: 'order.created',
77
+ * source: '/orders',
78
+ * subject: 'order-123',
79
+ * data: orderData
80
+ * });
81
+ *
82
+ * // With disabled telemetry
83
+ * const event = createArvoEvent(
84
+ * {
85
+ * type: 'order.created',
86
+ * source: '/orders',
87
+ * subject: 'order-123',
88
+ * data: orderData
89
+ * },
90
+ * undefined,
91
+ * { disable: true }
92
+ * );
93
+ *
94
+ * // With custom tracer
39
95
  * const event = createArvoEvent(
40
96
  * {
41
- * type: 'com.example.event',
42
- * source: '/example/source',
43
- * subject: 'example-subject',
44
- * data: { key: 'value' }
97
+ * type: 'order.created',
98
+ * source: '/orders',
99
+ * subject: 'order-123',
100
+ * data: orderData
45
101
  * },
46
- * { customextension: 'value' },
102
+ * undefined,
103
+ * { tracer: customTracer }
47
104
  * );
105
+ * ```
106
+ *
107
+ * @remarks
108
+ * This function provides several key features:
109
+ *
110
+ * 1. **Type Safety**:
111
+ * - Ensures event data matches specified type
112
+ * - Validates extension structure
113
+ * - Provides type-safe event type strings
114
+ *
115
+ * 2. **Default Handling**:
116
+ * - Generates UUID if no ID provided
117
+ * - Sets current timestamp if no time provided
118
+ * - Uses default ArvoDataContentType if none specified
119
+ *
120
+ * 3. **URI Handling**:
121
+ * - Automatically encodes URI components (source, subject, to, redirectto, dataschema)
122
+ * - Validates URI format
123
+ *
124
+ * 4. **OpenTelemetry Integration**:
125
+ * - Creates spans for event creation
126
+ * - Tracks errors and warnings
127
+ * - Propagates trace context
128
+ *
129
+ * 5. **Validation**:
130
+ * - Checks data content type compatibility
131
+ * - Validates required fields
132
+ * - Ensures URI format correctness
133
+ *
134
+ * @see {@link ArvoEvent} For the structure of the created event
135
+ * @see {@link ArvoDataContentType} For supported content types
136
+ * @see {@link CloudEventExtension} For extension structure
48
137
  */
49
138
  var createArvoEvent = function (event, extensions, opentelemetry) {
50
- opentelemetry = opentelemetry !== null && opentelemetry !== void 0 ? opentelemetry : {
51
- tracer: (0, OpenTelemetry_1.fetchOpenTelemetryTracer)(),
52
- };
53
- var span = opentelemetry.tracer.startSpan("createArvoEvent<".concat(event.type, ">"), {});
139
+ var _a;
140
+ if (opentelemetry === null || opentelemetry === void 0 ? void 0 : opentelemetry.disable) {
141
+ return generator(event, extensions, {
142
+ traceparent: null,
143
+ tracestate: null,
144
+ });
145
+ }
146
+ var tracer = (_a = opentelemetry === null || opentelemetry === void 0 ? void 0 : opentelemetry.tracer) !== null && _a !== void 0 ? _a : (0, OpenTelemetry_1.fetchOpenTelemetryTracer)();
147
+ var span = tracer.startSpan("createArvoEvent<".concat(event.type, ">"), {});
54
148
  return api_1.context.with(api_1.trace.setSpan(api_1.context.active(), span), function () {
55
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
56
149
  span.setStatus({ code: api_1.SpanStatusCode.OK });
57
- var otelHeaders = (0, OpenTelemetry_1.currentOpenTelemetryHeaders)();
58
150
  try {
59
- if (event.datacontenttype &&
60
- event.datacontenttype !== schema_1.ArvoDataContentType) {
61
- var warning = (0, utils_1.cleanString)("\n Warning! The provided datacontenttype(=".concat(event.datacontenttype, ")\n is not ArvoEvent compatible (=").concat(schema_1.ArvoDataContentType, "). There may \n be some limited functionality.\n "));
62
- (0, OpenTelemetry_1.logToSpan)({
63
- level: 'WARNING',
64
- message: warning,
65
- });
66
- }
67
- return new _1.default({
68
- id: (_a = event.id) !== null && _a !== void 0 ? _a : (0, uuid_1.v4)(),
69
- type: event.type,
70
- accesscontrol: (_b = event.accesscontrol) !== null && _b !== void 0 ? _b : null,
71
- executionunits: (_c = event.executionunits) !== null && _c !== void 0 ? _c : null,
72
- traceparent: (_e = (_d = event.traceparent) !== null && _d !== void 0 ? _d : otelHeaders.traceparent) !== null && _e !== void 0 ? _e : null,
73
- tracestate: (_g = (_f = event.tracestate) !== null && _f !== void 0 ? _f : otelHeaders.tracestate) !== null && _g !== void 0 ? _g : null,
74
- datacontenttype: (_h = event.datacontenttype) !== null && _h !== void 0 ? _h : schema_1.ArvoDataContentType,
75
- specversion: (_j = event.specversion) !== null && _j !== void 0 ? _j : '1.0',
76
- time: (_k = event.time) !== null && _k !== void 0 ? _k : (0, utils_1.createTimestamp)(),
77
- source: encodeURI(event.source),
78
- subject: encodeURI(event.subject),
79
- to: event.to ? encodeURI(event.to) : encodeURI(event.type),
80
- redirectto: event.redirectto ? encodeURI(event.redirectto) : null,
81
- dataschema: event.dataschema ? encodeURI(event.dataschema) : null,
82
- }, event.data, extensions);
151
+ return generator(event, extensions, (0, OpenTelemetry_1.currentOpenTelemetryHeaders)());
83
152
  }
84
153
  catch (error) {
85
154
  (0, OpenTelemetry_1.exceptionToSpan)(error);
@@ -93,7 +93,7 @@ var ArvoEventFactory = /** @class */ (function () {
93
93
  if (!validationResult.success) {
94
94
  throw new Error("Accept Event data validation failed: ".concat(validationResult.error.message));
95
95
  }
96
- return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { traceparent: (_b = (_a = event.traceparent) !== null && _a !== void 0 ? _a : otelHeaders.traceparent) !== null && _b !== void 0 ? _b : undefined, tracestate: (_d = (_c = event.tracestate) !== null && _c !== void 0 ? _c : otelHeaders.tracestate) !== null && _d !== void 0 ? _d : undefined, type: _this.contract.accepts.type, datacontenttype: schema_1.ArvoDataContentType, dataschema: "".concat(_this.contract.uri, "/").concat(_this.contract.version), data: validationResult.data }), extensions, opentelemetry);
96
+ return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { traceparent: (_b = (_a = event.traceparent) !== null && _a !== void 0 ? _a : otelHeaders.traceparent) !== null && _b !== void 0 ? _b : undefined, tracestate: (_d = (_c = event.tracestate) !== null && _c !== void 0 ? _c : otelHeaders.tracestate) !== null && _d !== void 0 ? _d : undefined, type: _this.contract.accepts.type, datacontenttype: schema_1.ArvoDataContentType, dataschema: "".concat(_this.contract.uri, "/").concat(_this.contract.version), data: validationResult.data }), extensions, { disable: true });
97
97
  }
98
98
  catch (error) {
99
99
  (0, OpenTelemetry_1.exceptionToSpan)(error);
@@ -147,7 +147,7 @@ var ArvoEventFactory = /** @class */ (function () {
147
147
  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 contract available for ".concat(event.type);
148
148
  throw new Error("Emit Event data validation failed: ".concat(msg));
149
149
  }
150
- return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { 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, datacontenttype: schema_1.ArvoDataContentType, dataschema: "".concat(_this.contract.uri, "/").concat(_this.contract.version), data: validationResult.data }), extensions, opentelemetry);
150
+ return (0, helpers_1.createArvoEvent)(__assign(__assign({}, event), { 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, datacontenttype: schema_1.ArvoDataContentType, dataschema: "".concat(_this.contract.uri, "/").concat(_this.contract.version), data: validationResult.data }), extensions, { disable: true });
151
151
  }
152
152
  catch (error) {
153
153
  (0, OpenTelemetry_1.exceptionToSpan)(error);
@@ -200,7 +200,7 @@ var ArvoEventFactory = /** @class */ (function () {
200
200
  errorName: error.name,
201
201
  errorMessage: error.message,
202
202
  errorStack: (_e = error.stack) !== null && _e !== void 0 ? _e : null,
203
- }, datacontenttype: schema_1.ArvoDataContentType, dataschema: "".concat(_this.contract.uri, "/").concat(ArvoOrchestrationSubject_1.default.WildCardMachineVersion) }), extensions, opentelemetry);
203
+ }, datacontenttype: schema_1.ArvoDataContentType, dataschema: "".concat(_this.contract.uri, "/").concat(ArvoOrchestrationSubject_1.default.WildCardMachineVersion) }), extensions, { disable: true });
204
204
  }
205
205
  catch (error) {
206
206
  (0, OpenTelemetry_1.exceptionToSpan)(error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arvo-core",
3
- "version": "2.0.8",
3
+ "version": "2.0.10",
4
4
  "description": "This core package contains all the core classes and components of the Arvo Event Driven System",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {