@signaltree/events 7.3.5 → 7.3.6

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/factory.cjs.js ADDED
@@ -0,0 +1,185 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Core event types - framework-agnostic definitions
5
+ */
6
+ /**
7
+ * Priority configuration with SLA targets
8
+ */
9
+ const EVENT_PRIORITIES = {
10
+ critical: {
11
+ sla: 100,
12
+ weight: 10
13
+ },
14
+ // < 100ms
15
+ high: {
16
+ sla: 500,
17
+ weight: 7
18
+ },
19
+ // < 500ms
20
+ normal: {
21
+ sla: 2000,
22
+ weight: 5
23
+ },
24
+ // < 2s
25
+ low: {
26
+ sla: 30000,
27
+ weight: 3
28
+ },
29
+ // < 30s
30
+ bulk: {
31
+ sla: 300000,
32
+ weight: 1
33
+ } // < 5min
34
+ };
35
+ const DEFAULT_EVENT_VERSION = {
36
+ major: 1,
37
+ minor: 0
38
+ };
39
+
40
+ /**
41
+ * Generate a UUID v7 (time-sortable)
42
+ *
43
+ * UUID v7 format: timestamp (48 bits) + version (4 bits) + random (12 bits) + variant (2 bits) + random (62 bits)
44
+ */
45
+ function generateEventId() {
46
+ // Get timestamp in milliseconds
47
+ const timestamp = Date.now();
48
+ // Convert to hex (12 characters for 48 bits)
49
+ const timestampHex = timestamp.toString(16).padStart(12, '0');
50
+ // Generate random bytes
51
+ const randomBytes = new Uint8Array(10);
52
+ crypto.getRandomValues(randomBytes);
53
+ // Convert to hex
54
+ const randomHex = Array.from(randomBytes).map(b => b.toString(16).padStart(2, '0')).join('');
55
+ // Construct UUID v7
56
+ // Format: xxxxxxxx-xxxx-7xxx-yxxx-xxxxxxxxxxxx
57
+ // Where x is timestamp/random and 7 is version, y is variant (8, 9, a, or b)
58
+ const uuid = [timestampHex.slice(0, 8),
59
+ // time_low
60
+ timestampHex.slice(8, 12),
61
+ // time_mid
62
+ '7' + randomHex.slice(0, 3),
63
+ // version (7) + random
64
+ (parseInt(randomHex.slice(3, 5), 16) & 0x3f | 0x80).toString(16).padStart(2, '0') + randomHex.slice(5, 7),
65
+ // variant + random
66
+ randomHex.slice(7, 19) // random
67
+ ].join('-');
68
+ return uuid;
69
+ }
70
+ /**
71
+ * Generate a correlation ID (also UUID v7 for traceability)
72
+ */
73
+ function generateCorrelationId() {
74
+ return generateEventId();
75
+ }
76
+ /**
77
+ * Create a single event
78
+ */
79
+ function createEvent(type, data, options) {
80
+ const id = options.id ?? generateEventId();
81
+ const correlationId = options.correlationId ?? generateCorrelationId();
82
+ const timestamp = options.timestamp ?? new Date().toISOString();
83
+ const actor = options.actor ?? {
84
+ id: 'system',
85
+ type: 'system'
86
+ };
87
+ const metadata = {
88
+ source: options.source,
89
+ environment: options.environment,
90
+ ...options.metadata
91
+ };
92
+ return {
93
+ id,
94
+ type,
95
+ version: options.version ?? DEFAULT_EVENT_VERSION,
96
+ timestamp,
97
+ correlationId,
98
+ causationId: options.causationId,
99
+ actor,
100
+ metadata,
101
+ data,
102
+ priority: options.priority,
103
+ aggregate: options.aggregate
104
+ };
105
+ }
106
+ /**
107
+ * Create an event factory with default configuration
108
+ *
109
+ * @example
110
+ * ```typescript
111
+ * const factory = createEventFactory({
112
+ * source: 'trade-service',
113
+ * environment: process.env.NODE_ENV || 'development',
114
+ * });
115
+ *
116
+ * const event = factory.create('TradeProposalCreated', {
117
+ * tradeId: '123',
118
+ * initiatorId: 'user-1',
119
+ * recipientId: 'user-2',
120
+ * }, {
121
+ * actor: { id: 'user-1', type: 'user' },
122
+ * priority: 'high',
123
+ * });
124
+ * ```
125
+ */
126
+ function createEventFactory(config) {
127
+ // Thread-local-like storage for correlation ID
128
+ let currentCorrelationId;
129
+ const systemActor = config.systemActor ?? {
130
+ id: 'system',
131
+ type: 'system',
132
+ name: config.source
133
+ };
134
+ const generateId = config.generateId ?? generateEventId;
135
+ const generateCorrelation = config.generateCorrelationId ?? generateCorrelationId;
136
+ return {
137
+ create(type, data, options = {}) {
138
+ const id = options.id ?? generateId();
139
+ const correlationId = options.correlationId ?? currentCorrelationId ?? generateCorrelation();
140
+ const timestamp = options.timestamp ?? new Date().toISOString();
141
+ const actor = options.actor ?? systemActor;
142
+ const metadata = {
143
+ source: config.source,
144
+ environment: config.environment,
145
+ ...options.metadata
146
+ };
147
+ return {
148
+ id,
149
+ type,
150
+ version: options.version ?? DEFAULT_EVENT_VERSION,
151
+ timestamp,
152
+ correlationId,
153
+ causationId: options.causationId,
154
+ actor,
155
+ metadata,
156
+ data,
157
+ priority: options.priority,
158
+ aggregate: options.aggregate
159
+ };
160
+ },
161
+ createFromCause(type, data, cause, options = {}) {
162
+ return this.create(type, data, {
163
+ ...options,
164
+ correlationId: cause.correlationId,
165
+ causationId: cause.id
166
+ });
167
+ },
168
+ getCorrelationId() {
169
+ return currentCorrelationId;
170
+ },
171
+ setCorrelationId(id) {
172
+ currentCorrelationId = id;
173
+ },
174
+ clearCorrelationId() {
175
+ currentCorrelationId = undefined;
176
+ }
177
+ };
178
+ }
179
+
180
+ exports.DEFAULT_EVENT_VERSION = DEFAULT_EVENT_VERSION;
181
+ exports.EVENT_PRIORITIES = EVENT_PRIORITIES;
182
+ exports.createEvent = createEvent;
183
+ exports.createEventFactory = createEventFactory;
184
+ exports.generateCorrelationId = generateCorrelationId;
185
+ exports.generateEventId = generateEventId;