-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmessage-queue.zod.ts
More file actions
500 lines (437 loc) · 14.2 KB
/
message-queue.zod.ts
File metadata and controls
500 lines (437 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
import {
ConnectorSchema,
} from '../connector.zod';
/**
* Message Queue Connector Protocol Template
*
* Specialized connector for message queue systems (RabbitMQ, Kafka, SQS, etc.)
* Extends the base connector with message queue-specific features like topics,
* consumer groups, and message acknowledgment patterns.
*/
/**
* Message Queue Provider Types
*/
export const MessageQueueProviderSchema = z.enum([
'rabbitmq', // RabbitMQ
'kafka', // Apache Kafka
'redis_pubsub', // Redis Pub/Sub
'redis_streams', // Redis Streams
'aws_sqs', // Amazon SQS
'aws_sns', // Amazon SNS
'google_pubsub', // Google Cloud Pub/Sub
'azure_service_bus', // Azure Service Bus
'azure_event_hubs', // Azure Event Hubs
'nats', // NATS
'pulsar', // Apache Pulsar
'activemq', // Apache ActiveMQ
'custom', // Custom message queue
]).describe('Message queue provider type');
export type MessageQueueProvider = z.infer<typeof MessageQueueProviderSchema>;
/**
* Message Format
*/
export const MessageFormatSchema = z.enum([
'json',
'xml',
'protobuf',
'avro',
'text',
'binary',
]).describe('Message format/serialization');
export type MessageFormat = z.infer<typeof MessageFormatSchema>;
/**
* Message Acknowledgment Mode
*/
export const AckModeSchema = z.enum([
'auto', // Auto-acknowledge
'manual', // Manual acknowledge after processing
'client', // Client-controlled acknowledge
]).describe('Message acknowledgment mode');
export type AckMode = z.infer<typeof AckModeSchema>;
/**
* Delivery Guarantee
*/
export const DeliveryGuaranteeSchema = z.enum([
'at_most_once', // Fire and forget
'at_least_once', // May deliver duplicates
'exactly_once', // Guaranteed exactly once delivery
]).describe('Message delivery guarantee');
export type DeliveryGuarantee = z.infer<typeof DeliveryGuaranteeSchema>;
/**
* Consumer Configuration
*/
export const ConsumerConfigSchema = z.object({
enabled: z.boolean().optional().default(true).describe('Enable consumer'),
consumerGroup: z.string().optional().describe('Consumer group ID'),
concurrency: z.number().min(1).max(100).optional().default(1).describe('Number of concurrent consumers'),
prefetchCount: z.number().min(1).max(1000).optional().default(10).describe('Prefetch count'),
ackMode: AckModeSchema.optional().default('manual'),
autoCommit: z.boolean().optional().default(false).describe('Auto-commit offsets'),
autoCommitIntervalMs: z.number().min(100).optional().default(5000).describe('Auto-commit interval in ms'),
sessionTimeoutMs: z.number().min(1000).optional().default(30000).describe('Session timeout in ms'),
rebalanceTimeoutMs: z.number().min(1000).optional().describe('Rebalance timeout in ms'),
});
export type ConsumerConfig = z.infer<typeof ConsumerConfigSchema>;
/**
* Producer Configuration
*/
export const ProducerConfigSchema = z.object({
enabled: z.boolean().optional().default(true).describe('Enable producer'),
acks: z.enum(['0', '1', 'all']).optional().default('all').describe('Acknowledgment level'),
compressionType: z.enum(['none', 'gzip', 'snappy', 'lz4', 'zstd']).optional().default('none').describe('Compression type'),
batchSize: z.number().min(1).optional().default(16384).describe('Batch size in bytes'),
lingerMs: z.number().min(0).optional().default(0).describe('Linger time in ms'),
maxInFlightRequests: z.number().min(1).optional().default(5).describe('Max in-flight requests'),
idempotence: z.boolean().optional().default(true).describe('Enable idempotent producer'),
transactional: z.boolean().optional().default(false).describe('Enable transactional producer'),
transactionTimeoutMs: z.number().min(1000).optional().describe('Transaction timeout in ms'),
});
export type ProducerConfig = z.infer<typeof ProducerConfigSchema>;
/**
* Dead Letter Queue Configuration
*/
export const DlqConfigSchema = z.object({
enabled: z.boolean().optional().default(false).describe('Enable DLQ'),
queueName: z.string().describe('Dead letter queue/topic name'),
maxRetries: z.number().min(0).max(100).optional().default(3).describe('Max retries before DLQ'),
retryDelayMs: z.number().min(0).optional().default(60000).describe('Retry delay in ms'),
});
export type DlqConfig = z.infer<typeof DlqConfigSchema>;
/**
* Topic/Queue Configuration
*/
export const TopicQueueSchema = z.object({
name: z.string().regex(/^[a-z_][a-z0-9_]*$/).describe('Topic/queue identifier in ObjectStack (snake_case)'),
label: z.string().describe('Display label'),
topicName: z.string().describe('Actual topic/queue name in message queue system'),
enabled: z.boolean().optional().default(true).describe('Enable sync for this topic/queue'),
/**
* Consumer or Producer
*/
mode: z.enum(['consumer', 'producer', 'both']).optional().default('both').describe('Consumer, producer, or both'),
/**
* Message format
*/
messageFormat: MessageFormatSchema.optional().default('json'),
/**
* Partition/shard configuration
*/
partitions: z.number().min(1).optional().describe('Number of partitions (for Kafka)'),
/**
* Replication factor
*/
replicationFactor: z.number().min(1).optional().describe('Replication factor (for Kafka)'),
/**
* Consumer configuration
*/
consumerConfig: ConsumerConfigSchema.optional().describe('Consumer-specific configuration'),
/**
* Producer configuration
*/
producerConfig: ProducerConfigSchema.optional().describe('Producer-specific configuration'),
/**
* Dead letter queue configuration
*/
dlqConfig: DlqConfigSchema.optional().describe('Dead letter queue configuration'),
/**
* Message routing key (for RabbitMQ)
*/
routingKey: z.string().optional().describe('Routing key pattern'),
/**
* Message filter
*/
messageFilter: z.object({
headers: z.record(z.string(), z.string()).optional().describe('Filter by message headers'),
attributes: z.record(z.string(), z.unknown()).optional().describe('Filter by message attributes'),
}).optional().describe('Message filter criteria'),
});
export type TopicQueue = z.infer<typeof TopicQueueSchema>;
/**
* Message Queue Connector Configuration Schema
*/
export const MessageQueueConnectorSchema = ConnectorSchema.extend({
type: z.literal('message_queue'),
/**
* Message queue provider
*/
provider: MessageQueueProviderSchema.describe('Message queue provider type'),
/**
* Broker configuration
*/
brokerConfig: z.object({
brokers: z.array(z.string()).describe('Broker addresses (host:port)'),
clientId: z.string().optional().describe('Client ID'),
connectionTimeoutMs: z.number().min(1000).optional().default(30000).describe('Connection timeout in ms'),
requestTimeoutMs: z.number().min(1000).optional().default(30000).describe('Request timeout in ms'),
}).describe('Broker connection configuration'),
/**
* Topics/queues to sync
*/
topics: z.array(TopicQueueSchema).describe('Topics/queues to sync'),
/**
* Delivery guarantee
*/
deliveryGuarantee: DeliveryGuaranteeSchema.optional().default('at_least_once'),
/**
* SSL/TLS configuration
*/
sslConfig: z.object({
enabled: z.boolean().optional().default(false).describe('Enable SSL/TLS'),
rejectUnauthorized: z.boolean().optional().default(true).describe('Reject unauthorized certificates'),
ca: z.string().optional().describe('CA certificate'),
cert: z.string().optional().describe('Client certificate'),
key: z.string().optional().describe('Client private key'),
}).optional().describe('SSL/TLS configuration'),
/**
* SASL authentication (for Kafka)
*/
saslConfig: z.object({
mechanism: z.enum(['plain', 'scram-sha-256', 'scram-sha-512', 'aws']).describe('SASL mechanism'),
username: z.string().optional().describe('SASL username'),
password: z.string().optional().describe('SASL password'),
}).optional().describe('SASL authentication configuration'),
/**
* Schema registry configuration (for Kafka/Avro)
*/
schemaRegistry: z.object({
url: z.string().url().describe('Schema registry URL'),
auth: z.object({
username: z.string().optional(),
password: z.string().optional(),
}).optional(),
}).optional().describe('Schema registry configuration'),
/**
* Message ordering
*/
preserveOrder: z.boolean().optional().default(true).describe('Preserve message ordering'),
/**
* Enable metrics
*/
enableMetrics: z.boolean().optional().default(true).describe('Enable message queue metrics'),
/**
* Enable distributed tracing
*/
enableTracing: z.boolean().optional().default(false).describe('Enable distributed tracing'),
});
export type MessageQueueConnector = z.infer<typeof MessageQueueConnectorSchema>;
// ============================================================================
// Helper Functions & Examples
// ============================================================================
/**
* Example: Apache Kafka Connector Configuration
*/
export const kafkaConnectorExample = {
name: 'kafka_production',
label: 'Production Kafka Cluster',
type: 'message_queue',
provider: 'kafka',
authentication: {
type: 'none',
},
brokerConfig: {
brokers: ['kafka-1.example.com:9092', 'kafka-2.example.com:9092', 'kafka-3.example.com:9092'],
clientId: 'objectstack-client',
connectionTimeoutMs: 30000,
requestTimeoutMs: 30000,
},
topics: [
{
name: 'order_events',
label: 'Order Events',
topicName: 'orders',
enabled: true,
mode: 'consumer',
messageFormat: 'json',
partitions: 10,
replicationFactor: 3,
consumerConfig: {
enabled: true,
consumerGroup: 'objectstack-consumer-group',
concurrency: 5,
prefetchCount: 100,
ackMode: 'manual',
autoCommit: false,
sessionTimeoutMs: 30000,
},
dlqConfig: {
enabled: true,
queueName: 'orders-dlq',
maxRetries: 3,
retryDelayMs: 60000,
},
},
{
name: 'user_activity',
label: 'User Activity',
topicName: 'user-activity',
enabled: true,
mode: 'producer',
messageFormat: 'json',
partitions: 5,
replicationFactor: 3,
producerConfig: {
enabled: true,
acks: 'all',
compressionType: 'snappy',
batchSize: 16384,
lingerMs: 10,
maxInFlightRequests: 5,
idempotence: true,
},
},
],
deliveryGuarantee: 'at_least_once',
saslConfig: {
mechanism: 'scram-sha-256',
username: '${KAFKA_USERNAME}',
password: '${KAFKA_PASSWORD}',
},
sslConfig: {
enabled: true,
rejectUnauthorized: true,
},
preserveOrder: true,
enableMetrics: true,
enableTracing: true,
status: 'active',
enabled: true,
};
/**
* Example: RabbitMQ Connector Configuration
*/
export const rabbitmqConnectorExample = {
name: 'rabbitmq_events',
label: 'RabbitMQ Event Bus',
type: 'message_queue',
provider: 'rabbitmq',
authentication: {
type: 'basic',
username: '${RABBITMQ_USERNAME}',
password: '${RABBITMQ_PASSWORD}',
},
brokerConfig: {
brokers: ['amqp://rabbitmq.example.com:5672'],
clientId: 'objectstack-rabbitmq-client',
},
topics: [
{
name: 'notifications',
label: 'Notifications',
topicName: 'notifications',
enabled: true,
mode: 'both',
messageFormat: 'json',
routingKey: 'notification.*',
consumerConfig: {
enabled: true,
prefetchCount: 10,
ackMode: 'manual',
},
producerConfig: {
enabled: true,
},
dlqConfig: {
enabled: true,
queueName: 'notifications-dlq',
maxRetries: 3,
retryDelayMs: 30000,
},
},
],
deliveryGuarantee: 'at_least_once',
status: 'active',
enabled: true,
};
/**
* Example: AWS SQS Connector Configuration
*/
export const sqsConnectorExample = {
name: 'aws_sqs_queue',
label: 'AWS SQS Queue',
type: 'message_queue',
provider: 'aws_sqs',
authentication: {
type: 'api_key',
apiKey: '${AWS_ACCESS_KEY_ID}:${AWS_SECRET_ACCESS_KEY}',
headerName: 'Authorization',
},
brokerConfig: {
brokers: ['https://sqs.us-east-1.amazonaws.com'],
},
topics: [
{
name: 'task_queue',
label: 'Task Queue',
topicName: 'task-queue',
enabled: true,
mode: 'consumer',
messageFormat: 'json',
consumerConfig: {
enabled: true,
concurrency: 10,
prefetchCount: 10,
ackMode: 'manual',
},
dlqConfig: {
enabled: true,
queueName: 'task-queue-dlq',
maxRetries: 3,
retryDelayMs: 120000,
},
},
],
deliveryGuarantee: 'at_least_once',
retryConfig: {
strategy: 'exponential_backoff',
maxAttempts: 3,
initialDelayMs: 1000,
maxDelayMs: 60000,
backoffMultiplier: 2,
},
status: 'active',
enabled: true,
};
/**
* Example: Google Cloud Pub/Sub Connector Configuration
*/
export const pubsubConnectorExample = {
name: 'gcp_pubsub',
label: 'Google Cloud Pub/Sub',
type: 'message_queue',
provider: 'google_pubsub',
authentication: {
type: 'oauth2',
clientId: '${GCP_CLIENT_ID}',
clientSecret: '${GCP_CLIENT_SECRET}',
authorizationUrl: 'https://accounts.google.com/o/oauth2/v2/auth',
tokenUrl: 'https://oauth2.googleapis.com/token',
grantType: 'client_credentials',
scopes: ['https://www.googleapis.com/auth/pubsub'],
},
brokerConfig: {
brokers: ['pubsub.googleapis.com'],
},
topics: [
{
name: 'analytics_events',
label: 'Analytics Events',
topicName: 'projects/my-project/topics/analytics-events',
enabled: true,
mode: 'both',
messageFormat: 'json',
consumerConfig: {
enabled: true,
consumerGroup: 'objectstack-subscription',
concurrency: 5,
prefetchCount: 100,
ackMode: 'manual',
},
},
],
deliveryGuarantee: 'at_least_once',
enableMetrics: true,
status: 'active',
enabled: true,
};