@signaltree/events 7.3.1

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