@bernierllc/message-queue 1.1.4 → 1.4.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.
@@ -1,444 +0,0 @@
1
- /*
2
- Copyright (c) 2025 Bernier LLC
3
-
4
- This file is licensed to the client under a limited-use license.
5
- The client may use and modify this code *only within the scope of the project it was delivered for*.
6
- Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
7
- */
8
-
9
- import {
10
- Message,
11
- MessagePriority,
12
- QueueConfig,
13
- QueueStats,
14
- QueueResult,
15
- MessageFilter,
16
- QueueEvent,
17
- QueueEventHandler,
18
- QueueOptions,
19
- QueueInfo
20
- } from '../types';
21
- import { generateId, validateMessage, validateConfig } from '../utils/validation';
22
- import { calculateRetryDelay, shouldRetry, RetryPolicyOptions } from '@bernierllc/retry-policy';
23
-
24
- export class MessageQueue {
25
- private messages: Message[] = [];
26
- private eventHandlers: QueueEventHandler[] = [];
27
- private config: QueueConfig;
28
- private options: QueueOptions;
29
- private stats: QueueStats;
30
- private isActive: boolean = true;
31
- private createdAt: Date;
32
- private lastActivity: Date;
33
-
34
- constructor(config: QueueConfig, options: QueueOptions = {}) {
35
- this.config = validateConfig(config);
36
- this.options = {
37
- enableEvents: true,
38
- enableStats: true,
39
- enablePersistence: false,
40
- ...options
41
- };
42
-
43
- this.createdAt = new Date();
44
- this.lastActivity = new Date();
45
- this.stats = this.initializeStats();
46
- }
47
-
48
- /**
49
- * Add a message to the queue
50
- */
51
- addMessage(content: any, priority: MessagePriority = MessagePriority.NORMAL, metadata?: Record<string, any>): QueueResult {
52
- if (!this.isActive) {
53
- return {
54
- success: false,
55
- error: 'Queue is not active'
56
- };
57
- }
58
-
59
- // Check if queue is full
60
- if (this.config.maxSize && this.messages.length >= this.config.maxSize) {
61
- this.emitEvent({
62
- type: 'queue_full',
63
- timestamp: new Date(),
64
- data: { queueSize: this.messages.length, maxSize: this.config.maxSize }
65
- });
66
-
67
- return {
68
- success: false,
69
- error: 'Queue is full'
70
- } as QueueResult;
71
- }
72
-
73
- const message: Message = {
74
- id: generateId(),
75
- content,
76
- priority,
77
- timestamp: new Date(),
78
- metadata: metadata || undefined,
79
- retryCount: 0,
80
- maxRetries: this.config.maxRetries || 0
81
- };
82
-
83
- // Set expiration if TTL is configured
84
- if (this.config.defaultTtl) {
85
- message.expiresAt = new Date(Date.now() + this.config.defaultTtl);
86
- }
87
-
88
- // Validate message
89
- const validationResult = validateMessage(message);
90
- if (!validationResult.isValid) {
91
- return {
92
- success: false,
93
- error: validationResult.error
94
- };
95
- }
96
-
97
- // Add message to queue (maintain priority order)
98
- this.insertMessageByPriority(message);
99
-
100
- this.updateStats();
101
- this.lastActivity = new Date();
102
-
103
- this.emitEvent({
104
- type: 'message_added',
105
- messageId: message.id,
106
- timestamp: new Date(),
107
- data: { priority, queueSize: this.messages.length }
108
- });
109
-
110
- return {
111
- success: true,
112
- messageId: message.id,
113
- stats: this.options.enableStats ? this.getStats() : undefined
114
- } as QueueResult;
115
- }
116
-
117
- /**
118
- * Get the next message from the queue
119
- */
120
- getNextMessage(): Message | null {
121
- if (!this.isActive || this.messages.length === 0) {
122
- return null;
123
- }
124
-
125
- // Remove expired messages
126
- this.removeExpiredMessages();
127
-
128
- if (this.messages.length === 0) {
129
- return null;
130
- }
131
-
132
- const message = this.messages.shift()!;
133
- this.updateStats();
134
- this.lastActivity = new Date();
135
-
136
- return message;
137
- }
138
-
139
- /**
140
- * Process a message and handle retries
141
- */
142
- async processMessage(processor: (message: Message) => Promise<boolean>): Promise<QueueResult> {
143
- const message = this.getNextMessage();
144
- if (!message) {
145
- return {
146
- success: false,
147
- error: 'No messages available'
148
- };
149
- }
150
-
151
- const startTime = Date.now();
152
- let success = false;
153
-
154
- try {
155
- success = await processor(message);
156
- } catch (error) {
157
- success = false;
158
- }
159
-
160
- const processingTime = Date.now() - startTime;
161
-
162
- if (success) {
163
- this.stats.processedMessages++;
164
- this.stats.averageProcessingTime =
165
- (this.stats.averageProcessingTime * (this.stats.processedMessages - 1) + processingTime) / this.stats.processedMessages;
166
-
167
- this.emitEvent({
168
- type: 'message_processed',
169
- messageId: message.id,
170
- timestamp: new Date(),
171
- data: { processingTime }
172
- });
173
- } else {
174
- // Handle retry logic using @bernierllc/retry-policy
175
- if (this.config.enableRetries && message.retryCount! < message.maxRetries!) {
176
- message.retryCount!++;
177
-
178
- // Use retry-policy for backoff calculation
179
- const retryOptions: RetryPolicyOptions = {
180
- maxRetries: message.maxRetries!,
181
- initialDelayMs: this.config.retryDelay || 1000,
182
- maxDelayMs: this.config.retryMaxDelay || (this.config.retryDelay || 1000) * 10,
183
- jitter: this.config.retryJitter !== false
184
- };
185
-
186
- const delay = calculateRetryDelay(message.retryCount!, retryOptions);
187
-
188
- // Add back to queue with calculated delay
189
- setTimeout(() => {
190
- // Check if we've exceeded max retries after this attempt
191
- if (message.retryCount! >= message.maxRetries!) {
192
- // Message has exceeded max retries, mark as failed
193
- this.stats.failedMessages++;
194
- this.emitEvent({
195
- type: 'message_failed',
196
- messageId: message.id,
197
- timestamp: new Date(),
198
- data: { retryCount: message.retryCount, maxRetries: message.maxRetries }
199
- });
200
- } else {
201
- // Re-add to queue for next retry
202
- this.insertMessageByPriority(message);
203
- this.updateStats();
204
- }
205
- }, delay);
206
- } else {
207
- // Message failed and retries are disabled or max retries exceeded
208
- this.stats.failedMessages++;
209
-
210
- this.emitEvent({
211
- type: 'message_failed',
212
- messageId: message.id,
213
- timestamp: new Date(),
214
- data: { retryCount: message.retryCount, maxRetries: message.maxRetries }
215
- });
216
- }
217
- }
218
-
219
- this.updateStats();
220
- this.lastActivity = new Date();
221
-
222
- return {
223
- success: true,
224
- stats: this.options.enableStats ? this.getStats() : undefined
225
- } as QueueResult;
226
- }
227
-
228
- /**
229
- * Get messages matching a filter
230
- */
231
- getMessages(filter: MessageFilter = {}): Message[] {
232
- let filteredMessages = [...this.messages];
233
-
234
- if (filter.priority !== undefined) {
235
- filteredMessages = filteredMessages.filter(msg => msg.priority === filter.priority);
236
- }
237
-
238
- if (filter.fromTimestamp) {
239
- filteredMessages = filteredMessages.filter(msg => msg.timestamp >= filter.fromTimestamp!);
240
- }
241
-
242
- if (filter.toTimestamp) {
243
- filteredMessages = filteredMessages.filter(msg => msg.timestamp <= filter.toTimestamp!);
244
- }
245
-
246
- if (filter.metadata) {
247
- filteredMessages = filteredMessages.filter(msg => {
248
- if (!msg.metadata) return false;
249
- return Object.entries(filter.metadata!).every(([key, value]) =>
250
- msg.metadata![key] === value
251
- );
252
- });
253
- }
254
-
255
- // Apply pagination
256
- const offset = filter.offset || 0;
257
- const limit = filter.limit || filteredMessages.length;
258
-
259
- return filteredMessages.slice(offset, offset + limit);
260
- }
261
-
262
- /**
263
- * Remove a specific message from the queue
264
- */
265
- removeMessage(messageId: string): QueueResult {
266
- const index = this.messages.findIndex(msg => msg.id === messageId);
267
- if (index === -1) {
268
- return {
269
- success: false,
270
- error: 'Message not found'
271
- };
272
- }
273
-
274
- this.messages.splice(index, 1);
275
- this.updateStats();
276
- this.lastActivity = new Date();
277
-
278
- return {
279
- success: true,
280
- stats: this.options.enableStats ? this.getStats() : undefined
281
- } as QueueResult;
282
- }
283
-
284
- /**
285
- * Clear all messages from the queue
286
- */
287
- clear(): QueueResult {
288
- const messageCount = this.messages.length;
289
- this.messages = [];
290
- this.updateStats();
291
- this.lastActivity = new Date();
292
-
293
- this.emitEvent({
294
- type: 'queue_empty',
295
- timestamp: new Date(),
296
- data: { clearedCount: messageCount }
297
- });
298
-
299
- return {
300
- success: true,
301
- stats: this.options.enableStats ? this.getStats() : undefined
302
- } as QueueResult;
303
- }
304
-
305
- /**
306
- * Get queue statistics
307
- */
308
- getStats(): QueueStats {
309
- return { ...this.stats };
310
- }
311
-
312
- /**
313
- * Get queue information
314
- */
315
- getInfo(): QueueInfo {
316
- return {
317
- name: this.config.name,
318
- config: { ...this.config },
319
- stats: this.getStats(),
320
- isActive: this.isActive,
321
- createdAt: this.createdAt,
322
- lastActivity: this.lastActivity
323
- };
324
- }
325
-
326
- /**
327
- * Add event handler
328
- */
329
- onEvent(handler: QueueEventHandler): void {
330
- if (this.options.enableEvents) {
331
- this.eventHandlers.push(handler);
332
- }
333
- }
334
-
335
- /**
336
- * Remove event handler
337
- */
338
- offEvent(handler: QueueEventHandler): void {
339
- const index = this.eventHandlers.indexOf(handler);
340
- if (index !== -1) {
341
- this.eventHandlers.splice(index, 1);
342
- }
343
- }
344
-
345
- /**
346
- * Pause the queue
347
- */
348
- pause(): void {
349
- this.isActive = false;
350
- }
351
-
352
- /**
353
- * Resume the queue
354
- */
355
- resume(): void {
356
- this.isActive = true;
357
- }
358
-
359
- /**
360
- * Get queue size
361
- */
362
- get size(): number {
363
- return this.messages.length;
364
- }
365
-
366
- /**
367
- * Check if queue is empty
368
- */
369
- get isEmpty(): boolean {
370
- return this.messages.length === 0;
371
- }
372
-
373
- /**
374
- * Check if queue is full
375
- */
376
- get isFull(): boolean {
377
- return this.config.maxSize ? this.messages.length >= this.config.maxSize : false;
378
- }
379
-
380
- // Private methods
381
-
382
- private insertMessageByPriority(message: Message): void {
383
- // Insert message in priority order (highest priority first)
384
- let insertIndex = 0;
385
- for (let i = 0; i < this.messages.length; i++) {
386
- const currentMessage = this.messages[i];
387
- if (currentMessage && currentMessage.priority < message.priority) {
388
- insertIndex = i;
389
- break;
390
- }
391
- insertIndex = i + 1;
392
- }
393
-
394
- this.messages.splice(insertIndex, 0, message);
395
- }
396
-
397
- private removeExpiredMessages(): void {
398
- const now = new Date();
399
- this.messages = this.messages.filter(msg =>
400
- !msg.expiresAt || msg.expiresAt > now
401
- );
402
- }
403
-
404
- private updateStats(): void {
405
- if (!this.options.enableStats) return;
406
-
407
- this.stats.totalMessages = this.messages.length;
408
- this.stats.pendingMessages = this.messages.length;
409
-
410
- // Calculate oldest message age
411
- if (this.messages.length > 0) {
412
- const oldestMessage = this.messages.reduce((oldest, current) =>
413
- current.timestamp < oldest.timestamp ? current : oldest
414
- );
415
- this.stats.oldestMessageAge = Date.now() - oldestMessage.timestamp.getTime();
416
- } else {
417
- this.stats.oldestMessageAge = 0;
418
- }
419
- }
420
-
421
- private initializeStats(): QueueStats {
422
- return {
423
- totalMessages: 0,
424
- pendingMessages: 0,
425
- processedMessages: 0,
426
- failedMessages: 0,
427
- averageProcessingTime: 0,
428
- oldestMessageAge: 0
429
- };
430
- }
431
-
432
- private emitEvent(event: QueueEvent): void {
433
- if (!this.options.enableEvents) return;
434
-
435
- this.eventHandlers.forEach(handler => {
436
- try {
437
- handler(event);
438
- } catch (error) {
439
- // Silently handle handler errors to prevent queue disruption
440
- console.error('Error in queue event handler:', error);
441
- }
442
- });
443
- }
444
- }
package/src/index.ts DELETED
@@ -1,16 +0,0 @@
1
- /*
2
- Copyright (c) 2025 Bernier LLC
3
-
4
- This file is licensed to the client under a limited-use license.
5
- The client may use and modify this code *only within the scope of the project it was delivered for*.
6
- Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
7
- */
8
-
9
- // Core Types
10
- export * from './types';
11
-
12
- // Core Message Queue
13
- export { MessageQueue } from './core/message-queue';
14
-
15
- // Validation Utilities
16
- export * from './utils/validation';
@@ -1,95 +0,0 @@
1
- /*
2
- Copyright (c) 2025 Bernier LLC
3
-
4
- This file is licensed to the client under a limited-use license.
5
- The client may use and modify this code *only within the scope of the project it was delivered for*.
6
- Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
7
- */
8
-
9
- export interface Message {
10
- id: string;
11
- content: any;
12
- priority: MessagePriority;
13
- timestamp: Date;
14
- expiresAt?: Date | undefined;
15
- metadata?: Record<string, any> | undefined;
16
- retryCount?: number | undefined;
17
- maxRetries?: number | undefined;
18
- }
19
-
20
- export enum MessagePriority {
21
- LOW = 0,
22
- NORMAL = 1,
23
- HIGH = 2,
24
- URGENT = 3
25
- }
26
-
27
- export interface QueueConfig {
28
- name: string;
29
- maxSize?: number | undefined;
30
- defaultPriority?: MessagePriority | undefined;
31
- defaultTtl?: number | undefined; // Time to live in milliseconds
32
- enableRetries?: boolean | undefined;
33
- maxRetries?: number | undefined;
34
- retryDelay?: number | undefined; // Base delay between retries in milliseconds
35
- retryMaxDelay?: number | undefined; // Maximum delay between retries in milliseconds
36
- retryJitter?: boolean | undefined; // Whether to add jitter to retry delays
37
- }
38
-
39
- export interface QueueStats {
40
- totalMessages: number;
41
- pendingMessages: number;
42
- processedMessages: number;
43
- failedMessages: number;
44
- averageProcessingTime: number;
45
- oldestMessageAge: number;
46
- }
47
-
48
- export interface QueueResult {
49
- success: boolean;
50
- messageId?: string | undefined;
51
- error?: string | undefined;
52
- stats?: QueueStats | undefined;
53
- }
54
-
55
- export interface MessageFilter {
56
- priority?: MessagePriority;
57
- fromTimestamp?: Date;
58
- toTimestamp?: Date;
59
- metadata?: Record<string, any>;
60
- limit?: number;
61
- offset?: number;
62
- }
63
-
64
- export interface QueueEvent {
65
- type: 'message_added' | 'message_processed' | 'message_failed' | 'queue_full' | 'queue_empty';
66
- messageId?: string;
67
- timestamp: Date;
68
- data?: any;
69
- }
70
-
71
- export type QueueEventHandler = (event: QueueEvent) => void;
72
-
73
- export interface QueueOptions {
74
- enableEvents?: boolean;
75
- enableStats?: boolean;
76
- enablePersistence?: boolean;
77
- persistenceConfig?: PersistenceConfig;
78
- }
79
-
80
- export interface PersistenceConfig {
81
- type: 'memory' | 'file' | 'database';
82
- path?: string;
83
- connectionString?: string;
84
- autoSave?: boolean;
85
- saveInterval?: number; // milliseconds
86
- }
87
-
88
- export interface QueueInfo {
89
- name: string;
90
- config: QueueConfig;
91
- stats: QueueStats;
92
- isActive: boolean;
93
- createdAt: Date;
94
- lastActivity: Date;
95
- }