@signaltree/events 7.6.1 → 8.0.0
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/README.md +12 -0
- package/package.json +4 -9
- package/dist/angular/handlers.cjs +0 -38
- package/dist/angular/index.cjs +0 -15
- package/dist/angular/optimistic-updates.cjs +0 -161
- package/dist/angular/websocket.service.cjs +0 -357
- package/dist/core/error-classification.cjs +0 -282
- package/dist/core/factory.cjs +0 -148
- package/dist/core/idempotency.cjs +0 -252
- package/dist/core/registry.cjs +0 -183
- package/dist/core/types.cjs +0 -41
- package/dist/core/validation.cjs +0 -185
- package/dist/index.cjs +0 -43
- package/dist/nestjs/base.subscriber.cjs +0 -287
- package/dist/nestjs/decorators.cjs +0 -35
- package/dist/nestjs/dlq.service.cjs +0 -249
- package/dist/nestjs/event-bus.module.cjs +0 -218
- package/dist/nestjs/event-bus.service.cjs +0 -318
- package/dist/nestjs/index.cjs +0 -34
- package/dist/nestjs/tokens.cjs +0 -14
- package/dist/testing/assertions.cjs +0 -172
- package/dist/testing/factories.cjs +0 -122
- package/dist/testing/helpers.cjs +0 -233
- package/dist/testing/index.cjs +0 -20
- package/dist/testing/mock-event-bus.cjs +0 -301
|
@@ -1,282 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Error Classification - Determine retry behavior for errors
|
|
5
|
-
*
|
|
6
|
-
* Provides:
|
|
7
|
-
* - Retryable vs non-retryable error classification
|
|
8
|
-
* - Error categories (transient, permanent, poison)
|
|
9
|
-
* - Retry configuration per error type
|
|
10
|
-
* - Custom error classifiers
|
|
11
|
-
*/
|
|
12
|
-
/**
|
|
13
|
-
* Default retry configurations by classification
|
|
14
|
-
*/
|
|
15
|
-
const DEFAULT_RETRY_CONFIGS = {
|
|
16
|
-
transient: {
|
|
17
|
-
maxAttempts: 5,
|
|
18
|
-
initialDelayMs: 1000,
|
|
19
|
-
maxDelayMs: 60000,
|
|
20
|
-
backoffMultiplier: 2,
|
|
21
|
-
jitter: 0.1
|
|
22
|
-
},
|
|
23
|
-
permanent: {
|
|
24
|
-
maxAttempts: 0,
|
|
25
|
-
initialDelayMs: 0,
|
|
26
|
-
maxDelayMs: 0,
|
|
27
|
-
backoffMultiplier: 1,
|
|
28
|
-
jitter: 0
|
|
29
|
-
},
|
|
30
|
-
poison: {
|
|
31
|
-
maxAttempts: 0,
|
|
32
|
-
initialDelayMs: 0,
|
|
33
|
-
maxDelayMs: 0,
|
|
34
|
-
backoffMultiplier: 1,
|
|
35
|
-
jitter: 0
|
|
36
|
-
},
|
|
37
|
-
unknown: {
|
|
38
|
-
maxAttempts: 3,
|
|
39
|
-
initialDelayMs: 2000,
|
|
40
|
-
maxDelayMs: 30000,
|
|
41
|
-
backoffMultiplier: 2,
|
|
42
|
-
jitter: 0.2
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
/**
|
|
46
|
-
* Known transient error patterns
|
|
47
|
-
*/
|
|
48
|
-
const TRANSIENT_ERROR_PATTERNS = [
|
|
49
|
-
// Network errors
|
|
50
|
-
/ECONNREFUSED/i, /ECONNRESET/i, /ETIMEDOUT/i, /ENETUNREACH/i, /EHOSTUNREACH/i, /ENOTFOUND/i, /socket hang up/i, /network error/i, /connection.*timeout/i, /request.*timeout/i,
|
|
51
|
-
// Database transient
|
|
52
|
-
/deadlock/i, /lock wait timeout/i, /too many connections/i, /connection pool exhausted/i, /temporarily unavailable/i,
|
|
53
|
-
// HTTP transient
|
|
54
|
-
/502 bad gateway/i, /503 service unavailable/i, /504 gateway timeout/i, /429 too many requests/i,
|
|
55
|
-
// Redis/Queue transient
|
|
56
|
-
/BUSY/i, /LOADING/i, /CLUSTERDOWN/i, /READONLY/i,
|
|
57
|
-
// Generic transient
|
|
58
|
-
/temporary failure/i, /try again/i, /retry/i, /throttl/i, /rate limit/i, /circuit breaker/i];
|
|
59
|
-
/**
|
|
60
|
-
* Known permanent error patterns
|
|
61
|
-
*/
|
|
62
|
-
const PERMANENT_ERROR_PATTERNS = [
|
|
63
|
-
// Auth errors
|
|
64
|
-
/unauthorized/i, /forbidden/i, /access denied/i, /permission denied/i, /invalid token/i, /token expired/i,
|
|
65
|
-
// Business logic
|
|
66
|
-
/not found/i, /already exists/i, /duplicate/i, /conflict/i, /invalid state/i, /precondition failed/i,
|
|
67
|
-
// HTTP permanent
|
|
68
|
-
/400 bad request/i, /401 unauthorized/i, /403 forbidden/i, /404 not found/i, /409 conflict/i, /422 unprocessable/i];
|
|
69
|
-
/**
|
|
70
|
-
* Known poison error patterns (send to DLQ immediately)
|
|
71
|
-
*/
|
|
72
|
-
const POISON_ERROR_PATTERNS = [
|
|
73
|
-
// Schema/Serialization
|
|
74
|
-
/invalid json/i, /json parse error/i, /unexpected token/i, /schema validation/i, /invalid event schema/i, /deserialization/i, /malformed/i,
|
|
75
|
-
// Data corruption
|
|
76
|
-
/data corruption/i, /checksum mismatch/i, /integrity error/i];
|
|
77
|
-
/**
|
|
78
|
-
* Error codes that indicate transient failures
|
|
79
|
-
*/
|
|
80
|
-
const TRANSIENT_ERROR_CODES = new Set(['ECONNREFUSED', 'ECONNRESET', 'ETIMEDOUT', 'ENETUNREACH', 'EHOSTUNREACH', 'ENOTFOUND', 'EPIPE', 'EAI_AGAIN']);
|
|
81
|
-
/**
|
|
82
|
-
* HTTP status codes that indicate transient failures
|
|
83
|
-
*/
|
|
84
|
-
const TRANSIENT_HTTP_STATUS = new Set([408, 429, 500, 502, 503, 504]);
|
|
85
|
-
/**
|
|
86
|
-
* HTTP status codes that indicate permanent failures
|
|
87
|
-
*/
|
|
88
|
-
const PERMANENT_HTTP_STATUS = new Set([400, 401, 403, 404, 405, 409, 410, 422]);
|
|
89
|
-
/**
|
|
90
|
-
* Create an error classifier
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
* ```typescript
|
|
94
|
-
* const classifier = createErrorClassifier({
|
|
95
|
-
* customClassifiers: [
|
|
96
|
-
* (error) => {
|
|
97
|
-
* if (error instanceof MyCustomTransientError) return 'transient';
|
|
98
|
-
* return null; // Let default classification handle it
|
|
99
|
-
* }
|
|
100
|
-
* ],
|
|
101
|
-
* retryConfigs: {
|
|
102
|
-
* transient: { maxAttempts: 10 }, // Override max attempts
|
|
103
|
-
* },
|
|
104
|
-
* });
|
|
105
|
-
*
|
|
106
|
-
* const result = classifier.classify(error);
|
|
107
|
-
* if (result.sendToDlq) {
|
|
108
|
-
* await dlqService.send(event, error, result.reason);
|
|
109
|
-
* }
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
|
-
function createErrorClassifier(config = {}) {
|
|
113
|
-
const customClassifiers = config.customClassifiers ?? [];
|
|
114
|
-
const defaultClassification = config.defaultClassification ?? 'unknown';
|
|
115
|
-
// Merge retry configs
|
|
116
|
-
const retryConfigs = {
|
|
117
|
-
transient: {
|
|
118
|
-
...DEFAULT_RETRY_CONFIGS.transient,
|
|
119
|
-
...config.retryConfigs?.transient
|
|
120
|
-
},
|
|
121
|
-
permanent: {
|
|
122
|
-
...DEFAULT_RETRY_CONFIGS.permanent,
|
|
123
|
-
...config.retryConfigs?.permanent
|
|
124
|
-
},
|
|
125
|
-
poison: {
|
|
126
|
-
...DEFAULT_RETRY_CONFIGS.poison,
|
|
127
|
-
...config.retryConfigs?.poison
|
|
128
|
-
},
|
|
129
|
-
unknown: {
|
|
130
|
-
...DEFAULT_RETRY_CONFIGS.unknown,
|
|
131
|
-
...config.retryConfigs?.unknown
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
function extractErrorInfo(error) {
|
|
135
|
-
if (error instanceof Error) {
|
|
136
|
-
const errWithCode = error;
|
|
137
|
-
return {
|
|
138
|
-
message: error.message,
|
|
139
|
-
name: error.name,
|
|
140
|
-
code: errWithCode.code,
|
|
141
|
-
status: errWithCode.status ?? errWithCode.statusCode ?? errWithCode.response?.status
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
if (typeof error === 'object' && error !== null) {
|
|
145
|
-
const obj = error;
|
|
146
|
-
return {
|
|
147
|
-
message: String(obj['message'] ?? obj['error'] ?? ''),
|
|
148
|
-
code: obj['code'],
|
|
149
|
-
status: obj['status'] ?? obj['statusCode']
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
return {
|
|
153
|
-
message: String(error)
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
function classifyByPatterns(message) {
|
|
157
|
-
// Check poison patterns first (most specific)
|
|
158
|
-
for (const pattern of POISON_ERROR_PATTERNS) {
|
|
159
|
-
if (pattern.test(message)) {
|
|
160
|
-
return 'poison';
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
// Check permanent patterns
|
|
164
|
-
for (const pattern of PERMANENT_ERROR_PATTERNS) {
|
|
165
|
-
if (pattern.test(message)) {
|
|
166
|
-
return 'permanent';
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
// Check transient patterns
|
|
170
|
-
for (const pattern of TRANSIENT_ERROR_PATTERNS) {
|
|
171
|
-
if (pattern.test(message)) {
|
|
172
|
-
return 'transient';
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
return null;
|
|
176
|
-
}
|
|
177
|
-
function classify(error) {
|
|
178
|
-
// 1. Try custom classifiers first
|
|
179
|
-
for (const classifier of customClassifiers) {
|
|
180
|
-
const result = classifier(error);
|
|
181
|
-
if (result !== null) {
|
|
182
|
-
return {
|
|
183
|
-
classification: result,
|
|
184
|
-
retryConfig: retryConfigs[result],
|
|
185
|
-
sendToDlq: result === 'poison' || result === 'permanent',
|
|
186
|
-
reason: `Custom classifier: ${result}`
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
const {
|
|
191
|
-
message,
|
|
192
|
-
code,
|
|
193
|
-
status,
|
|
194
|
-
name
|
|
195
|
-
} = extractErrorInfo(error);
|
|
196
|
-
// 2. Check error code
|
|
197
|
-
if (code && TRANSIENT_ERROR_CODES.has(code)) {
|
|
198
|
-
return {
|
|
199
|
-
classification: 'transient',
|
|
200
|
-
retryConfig: retryConfigs.transient,
|
|
201
|
-
sendToDlq: false,
|
|
202
|
-
reason: `Error code: ${code}`
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
// 3. Check HTTP status
|
|
206
|
-
if (status !== undefined) {
|
|
207
|
-
if (TRANSIENT_HTTP_STATUS.has(status)) {
|
|
208
|
-
return {
|
|
209
|
-
classification: 'transient',
|
|
210
|
-
retryConfig: retryConfigs.transient,
|
|
211
|
-
sendToDlq: false,
|
|
212
|
-
reason: `HTTP status: ${status}`
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
if (PERMANENT_HTTP_STATUS.has(status)) {
|
|
216
|
-
return {
|
|
217
|
-
classification: 'permanent',
|
|
218
|
-
retryConfig: retryConfigs.permanent,
|
|
219
|
-
sendToDlq: true,
|
|
220
|
-
reason: `HTTP status: ${status}`
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
// 4. Check error patterns
|
|
225
|
-
const patternResult = classifyByPatterns(message) ?? classifyByPatterns(name ?? '');
|
|
226
|
-
if (patternResult) {
|
|
227
|
-
return {
|
|
228
|
-
classification: patternResult,
|
|
229
|
-
retryConfig: retryConfigs[patternResult],
|
|
230
|
-
sendToDlq: patternResult === 'poison' || patternResult === 'permanent',
|
|
231
|
-
reason: `Pattern match: ${message.slice(0, 50)}`
|
|
232
|
-
};
|
|
233
|
-
}
|
|
234
|
-
// 5. Use default classification
|
|
235
|
-
return {
|
|
236
|
-
classification: defaultClassification,
|
|
237
|
-
retryConfig: retryConfigs[defaultClassification],
|
|
238
|
-
sendToDlq: defaultClassification === 'poison' || defaultClassification === 'permanent',
|
|
239
|
-
reason: 'No matching pattern, using default'
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
function isRetryable(error) {
|
|
243
|
-
const result = classify(error);
|
|
244
|
-
return result.classification === 'transient' || result.classification === 'unknown';
|
|
245
|
-
}
|
|
246
|
-
function calculateDelay(attempt, retryConfig) {
|
|
247
|
-
// Exponential backoff: initialDelay * multiplier^attempt
|
|
248
|
-
const baseDelay = retryConfig.initialDelayMs * Math.pow(retryConfig.backoffMultiplier, attempt);
|
|
249
|
-
// Cap at maxDelay
|
|
250
|
-
const cappedDelay = Math.min(baseDelay, retryConfig.maxDelayMs);
|
|
251
|
-
// Add jitter to prevent thundering herd
|
|
252
|
-
const jitter = cappedDelay * retryConfig.jitter * Math.random();
|
|
253
|
-
return Math.round(cappedDelay + jitter);
|
|
254
|
-
}
|
|
255
|
-
return {
|
|
256
|
-
classify,
|
|
257
|
-
isRetryable,
|
|
258
|
-
calculateDelay
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
/**
|
|
262
|
-
* Pre-configured error classifier instance
|
|
263
|
-
*/
|
|
264
|
-
const defaultErrorClassifier = createErrorClassifier();
|
|
265
|
-
/**
|
|
266
|
-
* Quick helper to check if error is retryable
|
|
267
|
-
*/
|
|
268
|
-
function isRetryableError(error) {
|
|
269
|
-
return defaultErrorClassifier.isRetryable(error);
|
|
270
|
-
}
|
|
271
|
-
/**
|
|
272
|
-
* Quick helper to classify error
|
|
273
|
-
*/
|
|
274
|
-
function classifyError(error) {
|
|
275
|
-
return defaultErrorClassifier.classify(error);
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
exports.DEFAULT_RETRY_CONFIGS = DEFAULT_RETRY_CONFIGS;
|
|
279
|
-
exports.classifyError = classifyError;
|
|
280
|
-
exports.createErrorClassifier = createErrorClassifier;
|
|
281
|
-
exports.defaultErrorClassifier = defaultErrorClassifier;
|
|
282
|
-
exports.isRetryableError = isRetryableError;
|
package/dist/core/factory.cjs
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var types = require('./types.cjs');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Generate a UUID v7 (time-sortable)
|
|
7
|
-
*
|
|
8
|
-
* UUID v7 format: timestamp (48 bits) + version (4 bits) + random (12 bits) + variant (2 bits) + random (62 bits)
|
|
9
|
-
*/
|
|
10
|
-
function generateEventId() {
|
|
11
|
-
// Get timestamp in milliseconds
|
|
12
|
-
const timestamp = Date.now();
|
|
13
|
-
// Convert to hex (12 characters for 48 bits)
|
|
14
|
-
const timestampHex = timestamp.toString(16).padStart(12, '0');
|
|
15
|
-
// Generate random bytes
|
|
16
|
-
const randomBytes = new Uint8Array(10);
|
|
17
|
-
crypto.getRandomValues(randomBytes);
|
|
18
|
-
// Convert to hex
|
|
19
|
-
const randomHex = Array.from(randomBytes).map(b => b.toString(16).padStart(2, '0')).join('');
|
|
20
|
-
// Construct UUID v7
|
|
21
|
-
// Format: xxxxxxxx-xxxx-7xxx-yxxx-xxxxxxxxxxxx
|
|
22
|
-
// Where x is timestamp/random and 7 is version, y is variant (8, 9, a, or b)
|
|
23
|
-
const uuid = [timestampHex.slice(0, 8),
|
|
24
|
-
// time_low
|
|
25
|
-
timestampHex.slice(8, 12),
|
|
26
|
-
// time_mid
|
|
27
|
-
'7' + randomHex.slice(0, 3),
|
|
28
|
-
// version (7) + random
|
|
29
|
-
(parseInt(randomHex.slice(3, 5), 16) & 0x3f | 0x80).toString(16).padStart(2, '0') + randomHex.slice(5, 7),
|
|
30
|
-
// variant + random
|
|
31
|
-
randomHex.slice(7, 19) // random
|
|
32
|
-
].join('-');
|
|
33
|
-
return uuid;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Generate a correlation ID (also UUID v7 for traceability)
|
|
37
|
-
*/
|
|
38
|
-
function generateCorrelationId() {
|
|
39
|
-
return generateEventId();
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Create a single event
|
|
43
|
-
*/
|
|
44
|
-
function createEvent(type, data, options) {
|
|
45
|
-
const id = options.id ?? generateEventId();
|
|
46
|
-
const correlationId = options.correlationId ?? generateCorrelationId();
|
|
47
|
-
const timestamp = options.timestamp ?? new Date().toISOString();
|
|
48
|
-
const actor = options.actor ?? {
|
|
49
|
-
id: 'system',
|
|
50
|
-
type: 'system'
|
|
51
|
-
};
|
|
52
|
-
const metadata = {
|
|
53
|
-
source: options.source,
|
|
54
|
-
environment: options.environment,
|
|
55
|
-
...options.metadata
|
|
56
|
-
};
|
|
57
|
-
return {
|
|
58
|
-
id,
|
|
59
|
-
type,
|
|
60
|
-
version: options.version ?? types.DEFAULT_EVENT_VERSION,
|
|
61
|
-
timestamp,
|
|
62
|
-
correlationId,
|
|
63
|
-
causationId: options.causationId,
|
|
64
|
-
actor,
|
|
65
|
-
metadata,
|
|
66
|
-
data,
|
|
67
|
-
priority: options.priority,
|
|
68
|
-
aggregate: options.aggregate
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Create an event factory with default configuration
|
|
73
|
-
*
|
|
74
|
-
* @example
|
|
75
|
-
* ```typescript
|
|
76
|
-
* const factory = createEventFactory({
|
|
77
|
-
* source: 'trade-service',
|
|
78
|
-
* environment: process.env.NODE_ENV || 'development',
|
|
79
|
-
* });
|
|
80
|
-
*
|
|
81
|
-
* const event = factory.create('TradeProposalCreated', {
|
|
82
|
-
* tradeId: '123',
|
|
83
|
-
* initiatorId: 'user-1',
|
|
84
|
-
* recipientId: 'user-2',
|
|
85
|
-
* }, {
|
|
86
|
-
* actor: { id: 'user-1', type: 'user' },
|
|
87
|
-
* priority: 'high',
|
|
88
|
-
* });
|
|
89
|
-
* ```
|
|
90
|
-
*/
|
|
91
|
-
function createEventFactory(config) {
|
|
92
|
-
// Thread-local-like storage for correlation ID
|
|
93
|
-
let currentCorrelationId;
|
|
94
|
-
const systemActor = config.systemActor ?? {
|
|
95
|
-
id: 'system',
|
|
96
|
-
type: 'system',
|
|
97
|
-
name: config.source
|
|
98
|
-
};
|
|
99
|
-
const generateId = config.generateId ?? generateEventId;
|
|
100
|
-
const generateCorrelation = config.generateCorrelationId ?? generateCorrelationId;
|
|
101
|
-
return {
|
|
102
|
-
create(type, data, options = {}) {
|
|
103
|
-
const id = options.id ?? generateId();
|
|
104
|
-
const correlationId = options.correlationId ?? currentCorrelationId ?? generateCorrelation();
|
|
105
|
-
const timestamp = options.timestamp ?? new Date().toISOString();
|
|
106
|
-
const actor = options.actor ?? systemActor;
|
|
107
|
-
const metadata = {
|
|
108
|
-
source: config.source,
|
|
109
|
-
environment: config.environment,
|
|
110
|
-
...options.metadata
|
|
111
|
-
};
|
|
112
|
-
return {
|
|
113
|
-
id,
|
|
114
|
-
type,
|
|
115
|
-
version: options.version ?? types.DEFAULT_EVENT_VERSION,
|
|
116
|
-
timestamp,
|
|
117
|
-
correlationId,
|
|
118
|
-
causationId: options.causationId,
|
|
119
|
-
actor,
|
|
120
|
-
metadata,
|
|
121
|
-
data,
|
|
122
|
-
priority: options.priority,
|
|
123
|
-
aggregate: options.aggregate
|
|
124
|
-
};
|
|
125
|
-
},
|
|
126
|
-
createFromCause(type, data, cause, options = {}) {
|
|
127
|
-
return this.create(type, data, {
|
|
128
|
-
...options,
|
|
129
|
-
correlationId: cause.correlationId,
|
|
130
|
-
causationId: cause.id
|
|
131
|
-
});
|
|
132
|
-
},
|
|
133
|
-
getCorrelationId() {
|
|
134
|
-
return currentCorrelationId;
|
|
135
|
-
},
|
|
136
|
-
setCorrelationId(id) {
|
|
137
|
-
currentCorrelationId = id;
|
|
138
|
-
},
|
|
139
|
-
clearCorrelationId() {
|
|
140
|
-
currentCorrelationId = undefined;
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
exports.createEvent = createEvent;
|
|
146
|
-
exports.createEventFactory = createEventFactory;
|
|
147
|
-
exports.generateCorrelationId = generateCorrelationId;
|
|
148
|
-
exports.generateEventId = generateEventId;
|
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* In-memory idempotency store
|
|
5
|
-
*
|
|
6
|
-
* Best for:
|
|
7
|
-
* - Development and testing
|
|
8
|
-
* - Single-instance deployments
|
|
9
|
-
* - Short-lived processes
|
|
10
|
-
*
|
|
11
|
-
* NOT recommended for:
|
|
12
|
-
* - Production multi-instance deployments
|
|
13
|
-
* - Long-running processes with many events
|
|
14
|
-
*/
|
|
15
|
-
class InMemoryIdempotencyStore {
|
|
16
|
-
records = new Map();
|
|
17
|
-
cleanupTimer;
|
|
18
|
-
defaultTtlMs;
|
|
19
|
-
defaultLockTtlMs;
|
|
20
|
-
maxRecords;
|
|
21
|
-
constructor(config = {}) {
|
|
22
|
-
this.defaultTtlMs = config.defaultTtlMs ?? 24 * 60 * 60 * 1000; // 24 hours
|
|
23
|
-
this.defaultLockTtlMs = config.defaultLockTtlMs ?? 30 * 1000; // 30 seconds
|
|
24
|
-
this.maxRecords = config.maxRecords ?? 100000;
|
|
25
|
-
if (config.cleanupIntervalMs !== 0) {
|
|
26
|
-
const interval = config.cleanupIntervalMs ?? 60 * 1000;
|
|
27
|
-
this.cleanupTimer = setInterval(() => this.cleanup(), interval);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
makeKey(eventId, consumer) {
|
|
31
|
-
return `${consumer}:${eventId}`;
|
|
32
|
-
}
|
|
33
|
-
async check(event, consumer, options = {}) {
|
|
34
|
-
const key = this.makeKey(event.id, consumer);
|
|
35
|
-
const record = this.records.get(key);
|
|
36
|
-
const now = Date.now();
|
|
37
|
-
// Check for existing record
|
|
38
|
-
if (record && record.expiresAt > now) {
|
|
39
|
-
// If processing and lock expired, treat as stale and allow reprocessing
|
|
40
|
-
if (record.status === 'processing') {
|
|
41
|
-
const lockExpired = record.startedAt.getTime() + this.defaultLockTtlMs < now;
|
|
42
|
-
if (lockExpired) {
|
|
43
|
-
// Lock expired, allow new processing attempt
|
|
44
|
-
if (options.acquireLock) {
|
|
45
|
-
const updated = {
|
|
46
|
-
...record,
|
|
47
|
-
startedAt: new Date(),
|
|
48
|
-
attempts: record.attempts + 1
|
|
49
|
-
};
|
|
50
|
-
this.records.set(key, updated);
|
|
51
|
-
return {
|
|
52
|
-
isDuplicate: false,
|
|
53
|
-
lockAcquired: true
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
return {
|
|
57
|
-
isDuplicate: false
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
// Still processing within lock period - treat as duplicate
|
|
61
|
-
return {
|
|
62
|
-
isDuplicate: true,
|
|
63
|
-
processedAt: record.startedAt
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
// Completed or failed record exists
|
|
67
|
-
return {
|
|
68
|
-
isDuplicate: true,
|
|
69
|
-
processedAt: record.completedAt ?? record.startedAt,
|
|
70
|
-
result: record.result
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
// No existing record or expired
|
|
74
|
-
if (options.acquireLock !== false) {
|
|
75
|
-
const ttlMs = options.lockTtlMs ?? this.defaultTtlMs;
|
|
76
|
-
const newRecord = {
|
|
77
|
-
eventId: event.id,
|
|
78
|
-
eventType: event.type,
|
|
79
|
-
startedAt: new Date(),
|
|
80
|
-
status: 'processing',
|
|
81
|
-
consumer,
|
|
82
|
-
attempts: 1,
|
|
83
|
-
expiresAt: now + ttlMs
|
|
84
|
-
};
|
|
85
|
-
this.records.set(key, newRecord);
|
|
86
|
-
this.enforceMaxRecords();
|
|
87
|
-
return {
|
|
88
|
-
isDuplicate: false,
|
|
89
|
-
lockAcquired: true
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
return {
|
|
93
|
-
isDuplicate: false
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
async markProcessing(event, consumer, ttlMs) {
|
|
97
|
-
const result = await this.check(event, consumer, {
|
|
98
|
-
acquireLock: true,
|
|
99
|
-
lockTtlMs: ttlMs ?? this.defaultLockTtlMs
|
|
100
|
-
});
|
|
101
|
-
return result.lockAcquired ?? false;
|
|
102
|
-
}
|
|
103
|
-
async markCompleted(event, consumer, result, ttlMs) {
|
|
104
|
-
const key = this.makeKey(event.id, consumer);
|
|
105
|
-
const record = this.records.get(key);
|
|
106
|
-
const now = Date.now();
|
|
107
|
-
const updated = {
|
|
108
|
-
eventId: event.id,
|
|
109
|
-
eventType: event.type,
|
|
110
|
-
startedAt: record?.startedAt ?? new Date(),
|
|
111
|
-
completedAt: new Date(),
|
|
112
|
-
status: 'completed',
|
|
113
|
-
result,
|
|
114
|
-
consumer,
|
|
115
|
-
attempts: record?.attempts ?? 1,
|
|
116
|
-
expiresAt: now + (ttlMs ?? this.defaultTtlMs)
|
|
117
|
-
};
|
|
118
|
-
this.records.set(key, updated);
|
|
119
|
-
}
|
|
120
|
-
async markFailed(event, consumer, error) {
|
|
121
|
-
const key = this.makeKey(event.id, consumer);
|
|
122
|
-
const record = this.records.get(key);
|
|
123
|
-
const now = Date.now();
|
|
124
|
-
const errorStr = error instanceof Error ? error.message : String(error);
|
|
125
|
-
const updated = {
|
|
126
|
-
eventId: event.id,
|
|
127
|
-
eventType: event.type,
|
|
128
|
-
startedAt: record?.startedAt ?? new Date(),
|
|
129
|
-
completedAt: new Date(),
|
|
130
|
-
status: 'failed',
|
|
131
|
-
error: errorStr,
|
|
132
|
-
consumer,
|
|
133
|
-
attempts: record?.attempts ?? 1,
|
|
134
|
-
expiresAt: now + this.defaultTtlMs
|
|
135
|
-
};
|
|
136
|
-
this.records.set(key, updated);
|
|
137
|
-
}
|
|
138
|
-
async releaseLock(event, consumer) {
|
|
139
|
-
const key = this.makeKey(event.id, consumer);
|
|
140
|
-
this.records.delete(key);
|
|
141
|
-
}
|
|
142
|
-
async getRecord(eventId, consumer) {
|
|
143
|
-
const key = this.makeKey(eventId, consumer);
|
|
144
|
-
const record = this.records.get(key);
|
|
145
|
-
if (!record || record.expiresAt < Date.now()) {
|
|
146
|
-
return null;
|
|
147
|
-
}
|
|
148
|
-
// Return without internal expiresAt field
|
|
149
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
150
|
-
const {
|
|
151
|
-
expiresAt: _,
|
|
152
|
-
...publicRecord
|
|
153
|
-
} = record;
|
|
154
|
-
return publicRecord;
|
|
155
|
-
}
|
|
156
|
-
async cleanup() {
|
|
157
|
-
const now = Date.now();
|
|
158
|
-
let cleaned = 0;
|
|
159
|
-
for (const [key, record] of Array.from(this.records.entries())) {
|
|
160
|
-
if (record.expiresAt < now) {
|
|
161
|
-
this.records.delete(key);
|
|
162
|
-
cleaned++;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
return cleaned;
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Enforce max records limit using LRU-like eviction
|
|
169
|
-
*/
|
|
170
|
-
enforceMaxRecords() {
|
|
171
|
-
if (this.records.size <= this.maxRecords) return;
|
|
172
|
-
// Sort by expires time (oldest first)
|
|
173
|
-
const entries = Array.from(this.records.entries()).sort((a, b) => a[1].expiresAt - b[1].expiresAt);
|
|
174
|
-
// Remove oldest 10%
|
|
175
|
-
const toRemove = Math.ceil(this.maxRecords * 0.1);
|
|
176
|
-
for (let i = 0; i < toRemove && i < entries.length; i++) {
|
|
177
|
-
this.records.delete(entries[i][0]);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Stop cleanup timer (for graceful shutdown)
|
|
182
|
-
*/
|
|
183
|
-
dispose() {
|
|
184
|
-
if (this.cleanupTimer) {
|
|
185
|
-
clearInterval(this.cleanupTimer);
|
|
186
|
-
this.cleanupTimer = undefined;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Clear all records (for testing)
|
|
191
|
-
*/
|
|
192
|
-
clear() {
|
|
193
|
-
this.records.clear();
|
|
194
|
-
}
|
|
195
|
-
/**
|
|
196
|
-
* Get stats (for monitoring)
|
|
197
|
-
*/
|
|
198
|
-
getStats() {
|
|
199
|
-
return {
|
|
200
|
-
size: this.records.size,
|
|
201
|
-
maxRecords: this.maxRecords
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Create an in-memory idempotency store
|
|
207
|
-
*
|
|
208
|
-
* @example
|
|
209
|
-
* ```typescript
|
|
210
|
-
* const store = createInMemoryIdempotencyStore({
|
|
211
|
-
* defaultTtlMs: 60 * 60 * 1000, // 1 hour
|
|
212
|
-
* maxRecords: 50000,
|
|
213
|
-
* });
|
|
214
|
-
*
|
|
215
|
-
* // In your subscriber
|
|
216
|
-
* const result = await store.check(event, 'my-subscriber');
|
|
217
|
-
* if (result.isDuplicate) {
|
|
218
|
-
* return result.result; // Return cached result
|
|
219
|
-
* }
|
|
220
|
-
*
|
|
221
|
-
* try {
|
|
222
|
-
* const processResult = await processEvent(event);
|
|
223
|
-
* await store.markCompleted(event, 'my-subscriber', processResult);
|
|
224
|
-
* return processResult;
|
|
225
|
-
* } catch (error) {
|
|
226
|
-
* await store.markFailed(event, 'my-subscriber', error);
|
|
227
|
-
* throw error;
|
|
228
|
-
* }
|
|
229
|
-
* ```
|
|
230
|
-
*/
|
|
231
|
-
function createInMemoryIdempotencyStore(config) {
|
|
232
|
-
return new InMemoryIdempotencyStore(config);
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* Generate an idempotency key from event
|
|
236
|
-
* Useful for custom implementations
|
|
237
|
-
*/
|
|
238
|
-
function generateIdempotencyKey(event, consumer) {
|
|
239
|
-
return `idempotency:${consumer}:${event.id}`;
|
|
240
|
-
}
|
|
241
|
-
/**
|
|
242
|
-
* Generate an idempotency key from correlation ID
|
|
243
|
-
* Useful for request-level idempotency
|
|
244
|
-
*/
|
|
245
|
-
function generateCorrelationKey(correlationId, operation) {
|
|
246
|
-
return `idempotency:correlation:${operation}:${correlationId}`;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
exports.InMemoryIdempotencyStore = InMemoryIdempotencyStore;
|
|
250
|
-
exports.createInMemoryIdempotencyStore = createInMemoryIdempotencyStore;
|
|
251
|
-
exports.generateCorrelationKey = generateCorrelationKey;
|
|
252
|
-
exports.generateIdempotencyKey = generateIdempotencyKey;
|