@signaltree/events 7.3.6 → 7.4.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/dist/angular/handlers.cjs +38 -0
- package/dist/angular/handlers.js +35 -0
- package/dist/angular/index.cjs +15 -0
- package/dist/angular/index.js +3 -0
- package/dist/angular/optimistic-updates.cjs +161 -0
- package/dist/angular/optimistic-updates.js +159 -0
- package/{angular.cjs.js → dist/angular/websocket.service.cjs} +0 -194
- package/{angular.esm.js → dist/angular/websocket.service.js} +1 -191
- package/dist/core/error-classification.cjs +282 -0
- package/dist/core/error-classification.js +276 -0
- package/{factory.cjs.js → dist/core/factory.cjs} +3 -40
- package/{factory.esm.js → dist/core/factory.js} +2 -37
- package/dist/core/idempotency.cjs +252 -0
- package/dist/core/idempotency.js +247 -0
- package/dist/core/registry.cjs +183 -0
- package/dist/core/registry.js +180 -0
- package/dist/core/types.cjs +41 -0
- package/dist/core/types.js +38 -0
- package/{index.cjs.js → dist/core/validation.cjs} +1 -23
- package/{index.esm.js → dist/core/validation.js} +1 -4
- package/dist/index.cjs +43 -0
- package/dist/index.js +7 -0
- package/dist/nestjs/base.subscriber.cjs +287 -0
- package/dist/nestjs/base.subscriber.js +287 -0
- package/dist/nestjs/decorators.cjs +35 -0
- package/dist/nestjs/decorators.js +32 -0
- package/dist/nestjs/dlq.service.cjs +249 -0
- package/dist/nestjs/dlq.service.js +249 -0
- package/dist/nestjs/event-bus.module.cjs +152 -0
- package/dist/nestjs/event-bus.module.js +152 -0
- package/dist/nestjs/event-bus.service.cjs +243 -0
- package/dist/nestjs/event-bus.service.js +243 -0
- package/dist/nestjs/index.cjs +33 -0
- package/dist/nestjs/index.js +6 -0
- package/dist/nestjs/tokens.cjs +14 -0
- package/dist/nestjs/tokens.js +9 -0
- package/dist/testing/assertions.cjs +172 -0
- package/dist/testing/assertions.js +169 -0
- package/dist/testing/factories.cjs +122 -0
- package/dist/testing/factories.js +119 -0
- package/dist/testing/helpers.cjs +233 -0
- package/dist/testing/helpers.js +227 -0
- package/dist/testing/index.cjs +20 -0
- package/dist/testing/index.js +4 -0
- package/dist/testing/mock-event-bus.cjs +237 -0
- package/dist/testing/mock-event-bus.js +234 -0
- package/package.json +35 -23
- package/angular.d.ts +0 -1
- package/idempotency.cjs.js +0 -713
- package/idempotency.esm.js +0 -701
- package/index.d.ts +0 -1
- package/nestjs.cjs.js +0 -951
- package/nestjs.d.ts +0 -1
- package/nestjs.esm.js +0 -944
- package/testing.cjs.js +0 -755
- package/testing.d.ts +0 -1
- package/testing.esm.js +0 -743
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory idempotency store
|
|
3
|
+
*
|
|
4
|
+
* Best for:
|
|
5
|
+
* - Development and testing
|
|
6
|
+
* - Single-instance deployments
|
|
7
|
+
* - Short-lived processes
|
|
8
|
+
*
|
|
9
|
+
* NOT recommended for:
|
|
10
|
+
* - Production multi-instance deployments
|
|
11
|
+
* - Long-running processes with many events
|
|
12
|
+
*/
|
|
13
|
+
class InMemoryIdempotencyStore {
|
|
14
|
+
records = new Map();
|
|
15
|
+
cleanupTimer;
|
|
16
|
+
defaultTtlMs;
|
|
17
|
+
defaultLockTtlMs;
|
|
18
|
+
maxRecords;
|
|
19
|
+
constructor(config = {}) {
|
|
20
|
+
this.defaultTtlMs = config.defaultTtlMs ?? 24 * 60 * 60 * 1000; // 24 hours
|
|
21
|
+
this.defaultLockTtlMs = config.defaultLockTtlMs ?? 30 * 1000; // 30 seconds
|
|
22
|
+
this.maxRecords = config.maxRecords ?? 100000;
|
|
23
|
+
if (config.cleanupIntervalMs !== 0) {
|
|
24
|
+
const interval = config.cleanupIntervalMs ?? 60 * 1000;
|
|
25
|
+
this.cleanupTimer = setInterval(() => this.cleanup(), interval);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
makeKey(eventId, consumer) {
|
|
29
|
+
return `${consumer}:${eventId}`;
|
|
30
|
+
}
|
|
31
|
+
async check(event, consumer, options = {}) {
|
|
32
|
+
const key = this.makeKey(event.id, consumer);
|
|
33
|
+
const record = this.records.get(key);
|
|
34
|
+
const now = Date.now();
|
|
35
|
+
// Check for existing record
|
|
36
|
+
if (record && record.expiresAt > now) {
|
|
37
|
+
// If processing and lock expired, treat as stale and allow reprocessing
|
|
38
|
+
if (record.status === 'processing') {
|
|
39
|
+
const lockExpired = record.startedAt.getTime() + this.defaultLockTtlMs < now;
|
|
40
|
+
if (lockExpired) {
|
|
41
|
+
// Lock expired, allow new processing attempt
|
|
42
|
+
if (options.acquireLock) {
|
|
43
|
+
const updated = {
|
|
44
|
+
...record,
|
|
45
|
+
startedAt: new Date(),
|
|
46
|
+
attempts: record.attempts + 1
|
|
47
|
+
};
|
|
48
|
+
this.records.set(key, updated);
|
|
49
|
+
return {
|
|
50
|
+
isDuplicate: false,
|
|
51
|
+
lockAcquired: true
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
isDuplicate: false
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
// Still processing within lock period - treat as duplicate
|
|
59
|
+
return {
|
|
60
|
+
isDuplicate: true,
|
|
61
|
+
processedAt: record.startedAt
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
// Completed or failed record exists
|
|
65
|
+
return {
|
|
66
|
+
isDuplicate: true,
|
|
67
|
+
processedAt: record.completedAt ?? record.startedAt,
|
|
68
|
+
result: record.result
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
// No existing record or expired
|
|
72
|
+
if (options.acquireLock !== false) {
|
|
73
|
+
const ttlMs = options.lockTtlMs ?? this.defaultTtlMs;
|
|
74
|
+
const newRecord = {
|
|
75
|
+
eventId: event.id,
|
|
76
|
+
eventType: event.type,
|
|
77
|
+
startedAt: new Date(),
|
|
78
|
+
status: 'processing',
|
|
79
|
+
consumer,
|
|
80
|
+
attempts: 1,
|
|
81
|
+
expiresAt: now + ttlMs
|
|
82
|
+
};
|
|
83
|
+
this.records.set(key, newRecord);
|
|
84
|
+
this.enforceMaxRecords();
|
|
85
|
+
return {
|
|
86
|
+
isDuplicate: false,
|
|
87
|
+
lockAcquired: true
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
isDuplicate: false
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
async markProcessing(event, consumer, ttlMs) {
|
|
95
|
+
const result = await this.check(event, consumer, {
|
|
96
|
+
acquireLock: true,
|
|
97
|
+
lockTtlMs: ttlMs ?? this.defaultLockTtlMs
|
|
98
|
+
});
|
|
99
|
+
return result.lockAcquired ?? false;
|
|
100
|
+
}
|
|
101
|
+
async markCompleted(event, consumer, result, ttlMs) {
|
|
102
|
+
const key = this.makeKey(event.id, consumer);
|
|
103
|
+
const record = this.records.get(key);
|
|
104
|
+
const now = Date.now();
|
|
105
|
+
const updated = {
|
|
106
|
+
eventId: event.id,
|
|
107
|
+
eventType: event.type,
|
|
108
|
+
startedAt: record?.startedAt ?? new Date(),
|
|
109
|
+
completedAt: new Date(),
|
|
110
|
+
status: 'completed',
|
|
111
|
+
result,
|
|
112
|
+
consumer,
|
|
113
|
+
attempts: record?.attempts ?? 1,
|
|
114
|
+
expiresAt: now + (ttlMs ?? this.defaultTtlMs)
|
|
115
|
+
};
|
|
116
|
+
this.records.set(key, updated);
|
|
117
|
+
}
|
|
118
|
+
async markFailed(event, consumer, error) {
|
|
119
|
+
const key = this.makeKey(event.id, consumer);
|
|
120
|
+
const record = this.records.get(key);
|
|
121
|
+
const now = Date.now();
|
|
122
|
+
const errorStr = error instanceof Error ? error.message : String(error);
|
|
123
|
+
const updated = {
|
|
124
|
+
eventId: event.id,
|
|
125
|
+
eventType: event.type,
|
|
126
|
+
startedAt: record?.startedAt ?? new Date(),
|
|
127
|
+
completedAt: new Date(),
|
|
128
|
+
status: 'failed',
|
|
129
|
+
error: errorStr,
|
|
130
|
+
consumer,
|
|
131
|
+
attempts: record?.attempts ?? 1,
|
|
132
|
+
expiresAt: now + this.defaultTtlMs
|
|
133
|
+
};
|
|
134
|
+
this.records.set(key, updated);
|
|
135
|
+
}
|
|
136
|
+
async releaseLock(event, consumer) {
|
|
137
|
+
const key = this.makeKey(event.id, consumer);
|
|
138
|
+
this.records.delete(key);
|
|
139
|
+
}
|
|
140
|
+
async getRecord(eventId, consumer) {
|
|
141
|
+
const key = this.makeKey(eventId, consumer);
|
|
142
|
+
const record = this.records.get(key);
|
|
143
|
+
if (!record || record.expiresAt < Date.now()) {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
// Return without internal expiresAt field
|
|
147
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
148
|
+
const {
|
|
149
|
+
expiresAt: _,
|
|
150
|
+
...publicRecord
|
|
151
|
+
} = record;
|
|
152
|
+
return publicRecord;
|
|
153
|
+
}
|
|
154
|
+
async cleanup() {
|
|
155
|
+
const now = Date.now();
|
|
156
|
+
let cleaned = 0;
|
|
157
|
+
for (const [key, record] of Array.from(this.records.entries())) {
|
|
158
|
+
if (record.expiresAt < now) {
|
|
159
|
+
this.records.delete(key);
|
|
160
|
+
cleaned++;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return cleaned;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Enforce max records limit using LRU-like eviction
|
|
167
|
+
*/
|
|
168
|
+
enforceMaxRecords() {
|
|
169
|
+
if (this.records.size <= this.maxRecords) return;
|
|
170
|
+
// Sort by expires time (oldest first)
|
|
171
|
+
const entries = Array.from(this.records.entries()).sort((a, b) => a[1].expiresAt - b[1].expiresAt);
|
|
172
|
+
// Remove oldest 10%
|
|
173
|
+
const toRemove = Math.ceil(this.maxRecords * 0.1);
|
|
174
|
+
for (let i = 0; i < toRemove && i < entries.length; i++) {
|
|
175
|
+
this.records.delete(entries[i][0]);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Stop cleanup timer (for graceful shutdown)
|
|
180
|
+
*/
|
|
181
|
+
dispose() {
|
|
182
|
+
if (this.cleanupTimer) {
|
|
183
|
+
clearInterval(this.cleanupTimer);
|
|
184
|
+
this.cleanupTimer = undefined;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Clear all records (for testing)
|
|
189
|
+
*/
|
|
190
|
+
clear() {
|
|
191
|
+
this.records.clear();
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Get stats (for monitoring)
|
|
195
|
+
*/
|
|
196
|
+
getStats() {
|
|
197
|
+
return {
|
|
198
|
+
size: this.records.size,
|
|
199
|
+
maxRecords: this.maxRecords
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Create an in-memory idempotency store
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* ```typescript
|
|
208
|
+
* const store = createInMemoryIdempotencyStore({
|
|
209
|
+
* defaultTtlMs: 60 * 60 * 1000, // 1 hour
|
|
210
|
+
* maxRecords: 50000,
|
|
211
|
+
* });
|
|
212
|
+
*
|
|
213
|
+
* // In your subscriber
|
|
214
|
+
* const result = await store.check(event, 'my-subscriber');
|
|
215
|
+
* if (result.isDuplicate) {
|
|
216
|
+
* return result.result; // Return cached result
|
|
217
|
+
* }
|
|
218
|
+
*
|
|
219
|
+
* try {
|
|
220
|
+
* const processResult = await processEvent(event);
|
|
221
|
+
* await store.markCompleted(event, 'my-subscriber', processResult);
|
|
222
|
+
* return processResult;
|
|
223
|
+
* } catch (error) {
|
|
224
|
+
* await store.markFailed(event, 'my-subscriber', error);
|
|
225
|
+
* throw error;
|
|
226
|
+
* }
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
function createInMemoryIdempotencyStore(config) {
|
|
230
|
+
return new InMemoryIdempotencyStore(config);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Generate an idempotency key from event
|
|
234
|
+
* Useful for custom implementations
|
|
235
|
+
*/
|
|
236
|
+
function generateIdempotencyKey(event, consumer) {
|
|
237
|
+
return `idempotency:${consumer}:${event.id}`;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Generate an idempotency key from correlation ID
|
|
241
|
+
* Useful for request-level idempotency
|
|
242
|
+
*/
|
|
243
|
+
function generateCorrelationKey(correlationId, operation) {
|
|
244
|
+
return `idempotency:correlation:${operation}:${correlationId}`;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export { InMemoryIdempotencyStore, createInMemoryIdempotencyStore, generateCorrelationKey, generateIdempotencyKey };
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Event Registry class - manages all registered event types
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* const registry = createEventRegistry();
|
|
9
|
+
*
|
|
10
|
+
* // Register events
|
|
11
|
+
* registry.register({
|
|
12
|
+
* type: 'TradeProposalCreated',
|
|
13
|
+
* schema: TradeProposalCreatedSchema,
|
|
14
|
+
* priority: 'high',
|
|
15
|
+
* description: 'Emitted when a user proposes a trade',
|
|
16
|
+
* category: 'trade',
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* // Validate events
|
|
20
|
+
* const validated = registry.validate(rawEvent);
|
|
21
|
+
*
|
|
22
|
+
* // Get schema for event type
|
|
23
|
+
* const schema = registry.getSchema('TradeProposalCreated');
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
class EventRegistry {
|
|
27
|
+
events = new Map();
|
|
28
|
+
config;
|
|
29
|
+
constructor(config = {}) {
|
|
30
|
+
this.config = {
|
|
31
|
+
strict: config.strict ?? false,
|
|
32
|
+
warnOnDeprecated: config.warnOnDeprecated ?? true
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Register an event type with its schema
|
|
37
|
+
*/
|
|
38
|
+
register(event) {
|
|
39
|
+
if (this.events.has(event.type)) {
|
|
40
|
+
throw new Error(`Event type '${event.type}' is already registered`);
|
|
41
|
+
}
|
|
42
|
+
this.events.set(event.type, event);
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Register multiple events at once
|
|
47
|
+
*/
|
|
48
|
+
registerMany(events) {
|
|
49
|
+
for (const event of events) {
|
|
50
|
+
this.register(event);
|
|
51
|
+
}
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Get schema for an event type
|
|
56
|
+
*/
|
|
57
|
+
getSchema(type) {
|
|
58
|
+
return this.events.get(type)?.schema;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Get registered event info
|
|
62
|
+
*/
|
|
63
|
+
getEvent(type) {
|
|
64
|
+
return this.events.get(type);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Check if event type is registered
|
|
68
|
+
*/
|
|
69
|
+
has(type) {
|
|
70
|
+
return this.events.has(type);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Get default priority for event type
|
|
74
|
+
*/
|
|
75
|
+
getPriority(type) {
|
|
76
|
+
return this.events.get(type)?.priority ?? 'normal';
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Validate an event against its registered schema
|
|
80
|
+
*
|
|
81
|
+
* @throws Error if event type is unknown (in strict mode) or validation fails
|
|
82
|
+
*/
|
|
83
|
+
validate(event) {
|
|
84
|
+
if (typeof event !== 'object' || event === null) {
|
|
85
|
+
throw new Error('Event must be an object');
|
|
86
|
+
}
|
|
87
|
+
const eventObj = event;
|
|
88
|
+
const type = eventObj['type'];
|
|
89
|
+
if (!type) {
|
|
90
|
+
throw new Error('Event must have a type field');
|
|
91
|
+
}
|
|
92
|
+
const registered = this.events.get(type);
|
|
93
|
+
if (!registered) {
|
|
94
|
+
if (this.config.strict) {
|
|
95
|
+
throw new Error(`Unknown event type: ${type}`);
|
|
96
|
+
}
|
|
97
|
+
// In non-strict mode, return as-is (no validation)
|
|
98
|
+
return event;
|
|
99
|
+
}
|
|
100
|
+
// Warn about deprecated events
|
|
101
|
+
if (registered.deprecated && this.config.warnOnDeprecated) {
|
|
102
|
+
console.warn(`[EventRegistry] Event '${type}' is deprecated. ${registered.deprecationMessage ?? ''}`);
|
|
103
|
+
}
|
|
104
|
+
const result = registered.schema.safeParse(event);
|
|
105
|
+
if (!result.success) {
|
|
106
|
+
const errors = result.error.issues.map(i => `${i.path.join('.')}: ${i.message}`);
|
|
107
|
+
throw new Error(`Event validation failed for '${type}': ${errors.join(', ')}`);
|
|
108
|
+
}
|
|
109
|
+
return result.data;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Check if event is valid without throwing
|
|
113
|
+
*/
|
|
114
|
+
isValid(event) {
|
|
115
|
+
try {
|
|
116
|
+
this.validate(event);
|
|
117
|
+
return true;
|
|
118
|
+
} catch {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Get all registered event types
|
|
124
|
+
*/
|
|
125
|
+
getAllTypes() {
|
|
126
|
+
return Array.from(this.events.keys());
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Get all registered events
|
|
130
|
+
*/
|
|
131
|
+
getAll() {
|
|
132
|
+
return Array.from(this.events.values());
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Get events by category
|
|
136
|
+
*/
|
|
137
|
+
getByCategory(category) {
|
|
138
|
+
return this.getAll().filter(e => e.category === category);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Get event catalog for documentation
|
|
142
|
+
*/
|
|
143
|
+
getCatalog() {
|
|
144
|
+
return this.getAll().map(e => ({
|
|
145
|
+
type: e.type,
|
|
146
|
+
category: e.category,
|
|
147
|
+
priority: e.priority,
|
|
148
|
+
description: e.description,
|
|
149
|
+
deprecated: e.deprecated ?? false
|
|
150
|
+
}));
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Export registry as JSON schema (for external tools)
|
|
154
|
+
*/
|
|
155
|
+
toJSONSchema() {
|
|
156
|
+
const schemas = {};
|
|
157
|
+
for (const [type, event] of Array.from(this.events.entries())) {
|
|
158
|
+
// Note: This is a simplified JSON schema export
|
|
159
|
+
// For full compatibility, use zod-to-json-schema package
|
|
160
|
+
schemas[type] = {
|
|
161
|
+
type: 'object',
|
|
162
|
+
description: event.description,
|
|
163
|
+
deprecated: event.deprecated,
|
|
164
|
+
priority: event.priority,
|
|
165
|
+
category: event.category
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
170
|
+
title: 'Event Registry',
|
|
171
|
+
definitions: schemas
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Create a new event registry
|
|
177
|
+
*/
|
|
178
|
+
function createEventRegistry(config) {
|
|
179
|
+
return new EventRegistry(config);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
exports.EventRegistry = EventRegistry;
|
|
183
|
+
exports.createEventRegistry = createEventRegistry;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event Registry class - manages all registered event types
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* const registry = createEventRegistry();
|
|
7
|
+
*
|
|
8
|
+
* // Register events
|
|
9
|
+
* registry.register({
|
|
10
|
+
* type: 'TradeProposalCreated',
|
|
11
|
+
* schema: TradeProposalCreatedSchema,
|
|
12
|
+
* priority: 'high',
|
|
13
|
+
* description: 'Emitted when a user proposes a trade',
|
|
14
|
+
* category: 'trade',
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* // Validate events
|
|
18
|
+
* const validated = registry.validate(rawEvent);
|
|
19
|
+
*
|
|
20
|
+
* // Get schema for event type
|
|
21
|
+
* const schema = registry.getSchema('TradeProposalCreated');
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
class EventRegistry {
|
|
25
|
+
events = new Map();
|
|
26
|
+
config;
|
|
27
|
+
constructor(config = {}) {
|
|
28
|
+
this.config = {
|
|
29
|
+
strict: config.strict ?? false,
|
|
30
|
+
warnOnDeprecated: config.warnOnDeprecated ?? true
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Register an event type with its schema
|
|
35
|
+
*/
|
|
36
|
+
register(event) {
|
|
37
|
+
if (this.events.has(event.type)) {
|
|
38
|
+
throw new Error(`Event type '${event.type}' is already registered`);
|
|
39
|
+
}
|
|
40
|
+
this.events.set(event.type, event);
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Register multiple events at once
|
|
45
|
+
*/
|
|
46
|
+
registerMany(events) {
|
|
47
|
+
for (const event of events) {
|
|
48
|
+
this.register(event);
|
|
49
|
+
}
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Get schema for an event type
|
|
54
|
+
*/
|
|
55
|
+
getSchema(type) {
|
|
56
|
+
return this.events.get(type)?.schema;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Get registered event info
|
|
60
|
+
*/
|
|
61
|
+
getEvent(type) {
|
|
62
|
+
return this.events.get(type);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Check if event type is registered
|
|
66
|
+
*/
|
|
67
|
+
has(type) {
|
|
68
|
+
return this.events.has(type);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Get default priority for event type
|
|
72
|
+
*/
|
|
73
|
+
getPriority(type) {
|
|
74
|
+
return this.events.get(type)?.priority ?? 'normal';
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Validate an event against its registered schema
|
|
78
|
+
*
|
|
79
|
+
* @throws Error if event type is unknown (in strict mode) or validation fails
|
|
80
|
+
*/
|
|
81
|
+
validate(event) {
|
|
82
|
+
if (typeof event !== 'object' || event === null) {
|
|
83
|
+
throw new Error('Event must be an object');
|
|
84
|
+
}
|
|
85
|
+
const eventObj = event;
|
|
86
|
+
const type = eventObj['type'];
|
|
87
|
+
if (!type) {
|
|
88
|
+
throw new Error('Event must have a type field');
|
|
89
|
+
}
|
|
90
|
+
const registered = this.events.get(type);
|
|
91
|
+
if (!registered) {
|
|
92
|
+
if (this.config.strict) {
|
|
93
|
+
throw new Error(`Unknown event type: ${type}`);
|
|
94
|
+
}
|
|
95
|
+
// In non-strict mode, return as-is (no validation)
|
|
96
|
+
return event;
|
|
97
|
+
}
|
|
98
|
+
// Warn about deprecated events
|
|
99
|
+
if (registered.deprecated && this.config.warnOnDeprecated) {
|
|
100
|
+
console.warn(`[EventRegistry] Event '${type}' is deprecated. ${registered.deprecationMessage ?? ''}`);
|
|
101
|
+
}
|
|
102
|
+
const result = registered.schema.safeParse(event);
|
|
103
|
+
if (!result.success) {
|
|
104
|
+
const errors = result.error.issues.map(i => `${i.path.join('.')}: ${i.message}`);
|
|
105
|
+
throw new Error(`Event validation failed for '${type}': ${errors.join(', ')}`);
|
|
106
|
+
}
|
|
107
|
+
return result.data;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Check if event is valid without throwing
|
|
111
|
+
*/
|
|
112
|
+
isValid(event) {
|
|
113
|
+
try {
|
|
114
|
+
this.validate(event);
|
|
115
|
+
return true;
|
|
116
|
+
} catch {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Get all registered event types
|
|
122
|
+
*/
|
|
123
|
+
getAllTypes() {
|
|
124
|
+
return Array.from(this.events.keys());
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Get all registered events
|
|
128
|
+
*/
|
|
129
|
+
getAll() {
|
|
130
|
+
return Array.from(this.events.values());
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Get events by category
|
|
134
|
+
*/
|
|
135
|
+
getByCategory(category) {
|
|
136
|
+
return this.getAll().filter(e => e.category === category);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Get event catalog for documentation
|
|
140
|
+
*/
|
|
141
|
+
getCatalog() {
|
|
142
|
+
return this.getAll().map(e => ({
|
|
143
|
+
type: e.type,
|
|
144
|
+
category: e.category,
|
|
145
|
+
priority: e.priority,
|
|
146
|
+
description: e.description,
|
|
147
|
+
deprecated: e.deprecated ?? false
|
|
148
|
+
}));
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Export registry as JSON schema (for external tools)
|
|
152
|
+
*/
|
|
153
|
+
toJSONSchema() {
|
|
154
|
+
const schemas = {};
|
|
155
|
+
for (const [type, event] of Array.from(this.events.entries())) {
|
|
156
|
+
// Note: This is a simplified JSON schema export
|
|
157
|
+
// For full compatibility, use zod-to-json-schema package
|
|
158
|
+
schemas[type] = {
|
|
159
|
+
type: 'object',
|
|
160
|
+
description: event.description,
|
|
161
|
+
deprecated: event.deprecated,
|
|
162
|
+
priority: event.priority,
|
|
163
|
+
category: event.category
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
168
|
+
title: 'Event Registry',
|
|
169
|
+
definitions: schemas
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Create a new event registry
|
|
175
|
+
*/
|
|
176
|
+
function createEventRegistry(config) {
|
|
177
|
+
return new EventRegistry(config);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export { EventRegistry, createEventRegistry };
|
|
@@ -0,0 +1,41 @@
|
|
|
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
|
+
exports.DEFAULT_EVENT_VERSION = DEFAULT_EVENT_VERSION;
|
|
41
|
+
exports.EVENT_PRIORITIES = EVENT_PRIORITIES;
|
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
export { DEFAULT_EVENT_VERSION, EVENT_PRIORITIES };
|