@bernierllc/message-queue 0.2.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/CHANGELOG.md +17 -0
- package/LICENSE +7 -0
- package/README.md +399 -0
- package/dist/core/message-queue.d.ts +78 -0
- package/dist/core/message-queue.d.ts.map +1 -0
- package/dist/core/message-queue.js +373 -0
- package/dist/core/message-queue.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/types/index.d.ts +78 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +18 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/validation.d.ts +30 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +201 -0
- package/dist/utils/validation.js.map +1 -0
- package/examples/basic-usage.ts +197 -0
- package/jest.config.cjs +18 -0
- package/package.json +53 -0
- package/src/core/message-queue.ts +444 -0
- package/src/index.ts +16 -0
- package/src/types/index.ts +95 -0
- package/src/utils/validation.ts +231 -0
- package/tests/message-queue.test.ts +398 -0
- package/tests/setup.ts +16 -0
- package/tests/validation.test.ts +302 -0
- package/tsconfig.json +28 -0
|
@@ -0,0 +1,302 @@
|
|
|
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
|
+
generateId,
|
|
11
|
+
validateMessage,
|
|
12
|
+
validateConfig,
|
|
13
|
+
validateFilter,
|
|
14
|
+
validateMessageId,
|
|
15
|
+
validateQueueName
|
|
16
|
+
} from '../src/utils/validation';
|
|
17
|
+
import { Message, MessagePriority, QueueConfig } from '../src/types';
|
|
18
|
+
|
|
19
|
+
describe('Validation Utilities', () => {
|
|
20
|
+
describe('generateId', () => {
|
|
21
|
+
it('should generate unique IDs', () => {
|
|
22
|
+
const id1 = generateId();
|
|
23
|
+
const id2 = generateId();
|
|
24
|
+
|
|
25
|
+
expect(id1).toBeDefined();
|
|
26
|
+
expect(id2).toBeDefined();
|
|
27
|
+
expect(id1).not.toBe(id2);
|
|
28
|
+
expect(id1).toMatch(/^msg_\d+_[a-z0-9]+$/);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('validateMessage', () => {
|
|
33
|
+
const validMessage: Message = {
|
|
34
|
+
id: 'msg_1234567890_abc123',
|
|
35
|
+
content: 'test content',
|
|
36
|
+
priority: MessagePriority.NORMAL,
|
|
37
|
+
timestamp: new Date(),
|
|
38
|
+
retryCount: 0,
|
|
39
|
+
maxRetries: 3
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
it('should validate a correct message', () => {
|
|
43
|
+
const result = validateMessage(validMessage);
|
|
44
|
+
expect(result.isValid).toBe(true);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('should reject message without ID', () => {
|
|
48
|
+
const invalidMessage = { ...validMessage, id: '' };
|
|
49
|
+
const result = validateMessage(invalidMessage);
|
|
50
|
+
expect(result.isValid).toBe(false);
|
|
51
|
+
expect(result.error).toBe('Message ID is required and must be a string');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('should reject message without content', () => {
|
|
55
|
+
const invalidMessage = { ...validMessage, content: null };
|
|
56
|
+
const result = validateMessage(invalidMessage);
|
|
57
|
+
expect(result.isValid).toBe(false);
|
|
58
|
+
expect(result.error).toBe('Message content is required');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should reject message with invalid priority', () => {
|
|
62
|
+
const invalidMessage = { ...validMessage, priority: 999 };
|
|
63
|
+
const result = validateMessage(invalidMessage);
|
|
64
|
+
expect(result.isValid).toBe(false);
|
|
65
|
+
expect(result.error).toBe('Invalid message priority');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('should reject message with invalid timestamp', () => {
|
|
69
|
+
const invalidMessage = { ...validMessage, timestamp: new Date('invalid') };
|
|
70
|
+
const result = validateMessage(invalidMessage);
|
|
71
|
+
expect(result.isValid).toBe(false);
|
|
72
|
+
expect(result.error).toBe('Message timestamp must be a valid Date');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should reject message with invalid expiration', () => {
|
|
76
|
+
const invalidMessage = { ...validMessage, expiresAt: new Date('invalid') };
|
|
77
|
+
const result = validateMessage(invalidMessage);
|
|
78
|
+
expect(result.isValid).toBe(false);
|
|
79
|
+
expect(result.error).toBe('Message expiration must be a valid Date');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('should reject message with negative retry count', () => {
|
|
83
|
+
const invalidMessage = { ...validMessage, retryCount: -1 };
|
|
84
|
+
const result = validateMessage(invalidMessage);
|
|
85
|
+
expect(result.isValid).toBe(false);
|
|
86
|
+
expect(result.error).toBe('Retry count must be a non-negative number');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('should reject message with negative max retries', () => {
|
|
90
|
+
const invalidMessage = { ...validMessage, maxRetries: -1 };
|
|
91
|
+
const result = validateMessage(invalidMessage);
|
|
92
|
+
expect(result.isValid).toBe(false);
|
|
93
|
+
expect(result.error).toBe('Max retries must be a non-negative number');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('should reject message with invalid metadata', () => {
|
|
97
|
+
const invalidMessage = { ...validMessage, metadata: 'not an object' as any };
|
|
98
|
+
const result = validateMessage(invalidMessage);
|
|
99
|
+
expect(result.isValid).toBe(false);
|
|
100
|
+
expect(result.error).toBe('Message metadata must be an object');
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe('validateConfig', () => {
|
|
105
|
+
const validConfig: QueueConfig = {
|
|
106
|
+
name: 'test-queue',
|
|
107
|
+
maxSize: 100,
|
|
108
|
+
defaultPriority: MessagePriority.NORMAL,
|
|
109
|
+
enableRetries: true,
|
|
110
|
+
maxRetries: 3,
|
|
111
|
+
retryDelay: 1000
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
it('should validate a correct config', () => {
|
|
115
|
+
const result = validateConfig(validConfig);
|
|
116
|
+
expect(result.name).toBe('test-queue');
|
|
117
|
+
expect(result.maxSize).toBe(100);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('should throw error for empty name', () => {
|
|
121
|
+
expect(() => validateConfig({ ...validConfig, name: '' }))
|
|
122
|
+
.toThrow('Queue name is required and must be a string');
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('should throw error for negative max size', () => {
|
|
126
|
+
expect(() => validateConfig({ ...validConfig, maxSize: -1 }))
|
|
127
|
+
.toThrow('Max size must be a positive number');
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('should throw error for invalid default priority', () => {
|
|
131
|
+
expect(() => validateConfig({ ...validConfig, defaultPriority: 999 as any }))
|
|
132
|
+
.toThrow('Invalid default priority');
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should throw error for negative default TTL', () => {
|
|
136
|
+
expect(() => validateConfig({ ...validConfig, defaultTtl: -1 }))
|
|
137
|
+
.toThrow('Default TTL must be a positive number');
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('should throw error for negative max retries', () => {
|
|
141
|
+
expect(() => validateConfig({ ...validConfig, maxRetries: -1 }))
|
|
142
|
+
.toThrow('Max retries must be a non-negative number');
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('should throw error for negative retry delay', () => {
|
|
146
|
+
expect(() => validateConfig({ ...validConfig, retryDelay: -1 }))
|
|
147
|
+
.toThrow('Retry delay must be a non-negative number');
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('should set default values', () => {
|
|
151
|
+
const minimalConfig = { name: 'test' };
|
|
152
|
+
const result = validateConfig(minimalConfig);
|
|
153
|
+
expect(result.defaultPriority).toBe(MessagePriority.NORMAL);
|
|
154
|
+
expect(result.enableRetries).toBe(false);
|
|
155
|
+
expect(result.maxRetries).toBe(0);
|
|
156
|
+
expect(result.retryDelay).toBe(1000);
|
|
157
|
+
expect(result.retryMaxDelay).toBe(10000); // 10x retryDelay
|
|
158
|
+
expect(result.retryJitter).toBe(true);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('should validate retry max delay', () => {
|
|
162
|
+
const configWithMaxDelay = { ...validConfig, retryMaxDelay: 5000 };
|
|
163
|
+
const result = validateConfig(configWithMaxDelay);
|
|
164
|
+
expect(result.retryMaxDelay).toBe(5000);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('should throw error for negative retry max delay', () => {
|
|
168
|
+
expect(() => validateConfig({ ...validConfig, retryMaxDelay: -1 }))
|
|
169
|
+
.toThrow('Retry max delay must be a non-negative number');
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('should throw error when retry max delay is less than retry delay', () => {
|
|
173
|
+
expect(() => validateConfig({ ...validConfig, retryDelay: 1000, retryMaxDelay: 500 }))
|
|
174
|
+
.toThrow('Retry max delay must be greater than or equal to retry delay');
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('should validate retry jitter setting', () => {
|
|
178
|
+
const configWithJitter = { ...validConfig, retryJitter: false };
|
|
179
|
+
const result = validateConfig(configWithJitter);
|
|
180
|
+
expect(result.retryJitter).toBe(false);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
describe('validateFilter', () => {
|
|
185
|
+
it('should validate a correct filter', () => {
|
|
186
|
+
const filter = {
|
|
187
|
+
priority: MessagePriority.HIGH,
|
|
188
|
+
limit: 10,
|
|
189
|
+
offset: 0
|
|
190
|
+
};
|
|
191
|
+
const result = validateFilter(filter);
|
|
192
|
+
expect(result.isValid).toBe(true);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('should reject invalid priority', () => {
|
|
196
|
+
const filter = { priority: 999 };
|
|
197
|
+
const result = validateFilter(filter);
|
|
198
|
+
expect(result.isValid).toBe(false);
|
|
199
|
+
expect(result.error).toBe('Invalid priority filter');
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('should reject invalid from timestamp', () => {
|
|
203
|
+
const filter = { fromTimestamp: new Date('invalid') };
|
|
204
|
+
const result = validateFilter(filter);
|
|
205
|
+
expect(result.isValid).toBe(false);
|
|
206
|
+
expect(result.error).toBe('From timestamp must be a valid Date');
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('should reject invalid to timestamp', () => {
|
|
210
|
+
const filter = { toTimestamp: new Date('invalid') };
|
|
211
|
+
const result = validateFilter(filter);
|
|
212
|
+
expect(result.isValid).toBe(false);
|
|
213
|
+
expect(result.error).toBe('To timestamp must be a valid Date');
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('should reject negative limit', () => {
|
|
217
|
+
const filter = { limit: -1 };
|
|
218
|
+
const result = validateFilter(filter);
|
|
219
|
+
expect(result.isValid).toBe(false);
|
|
220
|
+
expect(result.error).toBe('Limit must be a positive number');
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it('should reject negative offset', () => {
|
|
224
|
+
const filter = { offset: -1 };
|
|
225
|
+
const result = validateFilter(filter);
|
|
226
|
+
expect(result.isValid).toBe(false);
|
|
227
|
+
expect(result.error).toBe('Offset must be a non-negative number');
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it('should reject invalid metadata', () => {
|
|
231
|
+
const filter = { metadata: 'not an object' as any };
|
|
232
|
+
const result = validateFilter(filter);
|
|
233
|
+
expect(result.isValid).toBe(false);
|
|
234
|
+
expect(result.error).toBe('Metadata filter must be an object');
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
describe('validateMessageId', () => {
|
|
239
|
+
it('should validate a correct message ID', () => {
|
|
240
|
+
const result = validateMessageId('msg_1234567890_abc123');
|
|
241
|
+
expect(result.isValid).toBe(true);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('should reject empty message ID', () => {
|
|
245
|
+
const result = validateMessageId('');
|
|
246
|
+
expect(result.isValid).toBe(false);
|
|
247
|
+
expect(result.error).toBe('Message ID is required and must be a string');
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it('should reject non-string message ID', () => {
|
|
251
|
+
const result = validateMessageId(123 as any);
|
|
252
|
+
expect(result.isValid).toBe(false);
|
|
253
|
+
expect(result.error).toBe('Message ID is required and must be a string');
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('should reject short message ID', () => {
|
|
257
|
+
const result = validateMessageId('short');
|
|
258
|
+
expect(result.isValid).toBe(false);
|
|
259
|
+
expect(result.error).toBe('Message ID must be at least 10 characters long');
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
describe('validateQueueName', () => {
|
|
264
|
+
it('should validate a correct queue name', () => {
|
|
265
|
+
const result = validateQueueName('test-queue');
|
|
266
|
+
expect(result.isValid).toBe(true);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it('should reject empty queue name', () => {
|
|
270
|
+
const result = validateQueueName('');
|
|
271
|
+
expect(result.isValid).toBe(false);
|
|
272
|
+
expect(result.error).toBe('Queue name is required and must be a string');
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it('should reject non-string queue name', () => {
|
|
276
|
+
const result = validateQueueName(123 as any);
|
|
277
|
+
expect(result.isValid).toBe(false);
|
|
278
|
+
expect(result.error).toBe('Queue name is required and must be a string');
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it('should reject too long queue name', () => {
|
|
282
|
+
const longName = 'a'.repeat(101);
|
|
283
|
+
const result = validateQueueName(longName);
|
|
284
|
+
expect(result.isValid).toBe(false);
|
|
285
|
+
expect(result.error).toBe('Queue name must be between 1 and 100 characters');
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it('should reject queue name with invalid characters', () => {
|
|
289
|
+
const result = validateQueueName('test@queue');
|
|
290
|
+
expect(result.isValid).toBe(false);
|
|
291
|
+
expect(result.error).toBe('Queue name can only contain letters, numbers, underscores, and hyphens');
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('should accept valid queue names', () => {
|
|
295
|
+
const validNames = ['test', 'test-queue', 'test_queue', 'test123', 'TestQueue'];
|
|
296
|
+
validNames.forEach(name => {
|
|
297
|
+
const result = validateQueueName(name);
|
|
298
|
+
expect(result.isValid).toBe(true);
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "./dist",
|
|
5
|
+
"rootDir": "./src",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationMap": true,
|
|
8
|
+
"sourceMap": true,
|
|
9
|
+
"strict": true,
|
|
10
|
+
"noImplicitAny": true,
|
|
11
|
+
"strictNullChecks": true,
|
|
12
|
+
"strictFunctionTypes": true,
|
|
13
|
+
"noImplicitReturns": true,
|
|
14
|
+
"noFallthroughCasesInSwitch": true,
|
|
15
|
+
"noUncheckedIndexedAccess": true,
|
|
16
|
+
"exactOptionalPropertyTypes": true
|
|
17
|
+
},
|
|
18
|
+
"include": [
|
|
19
|
+
"src/**/*"
|
|
20
|
+
],
|
|
21
|
+
"exclude": [
|
|
22
|
+
"node_modules",
|
|
23
|
+
"dist",
|
|
24
|
+
"tests",
|
|
25
|
+
"**/*.test.ts",
|
|
26
|
+
"**/*.spec.ts"
|
|
27
|
+
]
|
|
28
|
+
}
|