@gravito/stream 1.0.0-alpha.5 → 1.0.0-beta.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.
Files changed (150) hide show
  1. package/README.md +8 -3
  2. package/dist/index.cjs +1016 -31676
  3. package/dist/index.d.cts +1132 -0
  4. package/dist/index.d.ts +1123 -36
  5. package/dist/index.js +1412 -0
  6. package/package.json +3 -3
  7. package/dist/Consumer.d.ts +0 -67
  8. package/dist/Consumer.d.ts.map +0 -1
  9. package/dist/Job.d.ts +0 -76
  10. package/dist/Job.d.ts.map +0 -1
  11. package/dist/OrbitQueue.d.ts +0 -74
  12. package/dist/OrbitQueue.d.ts.map +0 -1
  13. package/dist/QueueManager.d.ts +0 -86
  14. package/dist/QueueManager.d.ts.map +0 -1
  15. package/dist/Queueable.d.ts +0 -63
  16. package/dist/Queueable.d.ts.map +0 -1
  17. package/dist/Worker.d.ts +0 -48
  18. package/dist/Worker.d.ts.map +0 -1
  19. package/dist/core/src/ConfigManager.d.ts +0 -26
  20. package/dist/core/src/ConfigManager.d.ts.map +0 -1
  21. package/dist/core/src/Container.d.ts +0 -39
  22. package/dist/core/src/Container.d.ts.map +0 -1
  23. package/dist/core/src/Event.d.ts +0 -6
  24. package/dist/core/src/Event.d.ts.map +0 -1
  25. package/dist/core/src/EventManager.d.ts +0 -124
  26. package/dist/core/src/EventManager.d.ts.map +0 -1
  27. package/dist/core/src/GlobalErrorHandlers.d.ts +0 -32
  28. package/dist/core/src/GlobalErrorHandlers.d.ts.map +0 -1
  29. package/dist/core/src/HookManager.d.ts +0 -29
  30. package/dist/core/src/HookManager.d.ts.map +0 -1
  31. package/dist/core/src/Listener.d.ts +0 -5
  32. package/dist/core/src/Listener.d.ts.map +0 -1
  33. package/dist/core/src/Logger.d.ts +0 -21
  34. package/dist/core/src/Logger.d.ts.map +0 -1
  35. package/dist/core/src/PlanetCore.d.ts +0 -116
  36. package/dist/core/src/PlanetCore.d.ts.map +0 -1
  37. package/dist/core/src/Route.d.ts +0 -13
  38. package/dist/core/src/Route.d.ts.map +0 -1
  39. package/dist/core/src/Router.d.ts +0 -168
  40. package/dist/core/src/Router.d.ts.map +0 -1
  41. package/dist/core/src/ServiceProvider.d.ts +0 -17
  42. package/dist/core/src/ServiceProvider.d.ts.map +0 -1
  43. package/dist/core/src/exceptions/AuthenticationException.d.ts +0 -5
  44. package/dist/core/src/exceptions/AuthenticationException.d.ts.map +0 -1
  45. package/dist/core/src/exceptions/AuthorizationException.d.ts +0 -5
  46. package/dist/core/src/exceptions/AuthorizationException.d.ts.map +0 -1
  47. package/dist/core/src/exceptions/GravitoException.d.ts +0 -15
  48. package/dist/core/src/exceptions/GravitoException.d.ts.map +0 -1
  49. package/dist/core/src/exceptions/ModelNotFoundException.d.ts +0 -7
  50. package/dist/core/src/exceptions/ModelNotFoundException.d.ts.map +0 -1
  51. package/dist/core/src/exceptions/ValidationException.d.ts +0 -15
  52. package/dist/core/src/exceptions/ValidationException.d.ts.map +0 -1
  53. package/dist/core/src/exceptions/index.d.ts +0 -6
  54. package/dist/core/src/exceptions/index.d.ts.map +0 -1
  55. package/dist/core/src/helpers/Arr.d.ts +0 -15
  56. package/dist/core/src/helpers/Arr.d.ts.map +0 -1
  57. package/dist/core/src/helpers/Str.d.ts +0 -19
  58. package/dist/core/src/helpers/Str.d.ts.map +0 -1
  59. package/dist/core/src/helpers/data.d.ts +0 -6
  60. package/dist/core/src/helpers/data.d.ts.map +0 -1
  61. package/dist/core/src/helpers/errors.d.ts +0 -13
  62. package/dist/core/src/helpers/errors.d.ts.map +0 -1
  63. package/dist/core/src/helpers/response.d.ts +0 -19
  64. package/dist/core/src/helpers/response.d.ts.map +0 -1
  65. package/dist/core/src/helpers.d.ts +0 -39
  66. package/dist/core/src/helpers.d.ts.map +0 -1
  67. package/dist/core/src/http/CookieJar.d.ts +0 -34
  68. package/dist/core/src/http/CookieJar.d.ts.map +0 -1
  69. package/dist/core/src/http/middleware/ThrottleRequests.d.ts +0 -13
  70. package/dist/core/src/http/middleware/ThrottleRequests.d.ts.map +0 -1
  71. package/dist/core/src/index.d.ts +0 -32
  72. package/dist/core/src/index.d.ts.map +0 -1
  73. package/dist/core/src/security/Encrypter.d.ts +0 -25
  74. package/dist/core/src/security/Encrypter.d.ts.map +0 -1
  75. package/dist/core/src/security/Hasher.d.ts +0 -30
  76. package/dist/core/src/security/Hasher.d.ts.map +0 -1
  77. package/dist/core/src/types/events.d.ts +0 -95
  78. package/dist/core/src/types/events.d.ts.map +0 -1
  79. package/dist/drivers/DatabaseDriver.d.ts +0 -60
  80. package/dist/drivers/DatabaseDriver.d.ts.map +0 -1
  81. package/dist/drivers/KafkaDriver.d.ts +0 -134
  82. package/dist/drivers/KafkaDriver.d.ts.map +0 -1
  83. package/dist/drivers/MemoryDriver.d.ts +0 -45
  84. package/dist/drivers/MemoryDriver.d.ts.map +0 -1
  85. package/dist/drivers/QueueDriver.d.ts +0 -89
  86. package/dist/drivers/QueueDriver.d.ts.map +0 -1
  87. package/dist/drivers/RedisDriver.d.ts +0 -79
  88. package/dist/drivers/RedisDriver.d.ts.map +0 -1
  89. package/dist/drivers/SQSDriver.d.ts +0 -100
  90. package/dist/drivers/SQSDriver.d.ts.map +0 -1
  91. package/dist/index.cjs.map +0 -422
  92. package/dist/index.d.ts.map +0 -1
  93. package/dist/index.mjs +0 -32096
  94. package/dist/index.mjs.map +0 -422
  95. package/dist/orbit-db/src/DBService.d.ts +0 -270
  96. package/dist/orbit-db/src/DBService.d.ts.map +0 -1
  97. package/dist/orbit-db/src/EventBus.d.ts +0 -53
  98. package/dist/orbit-db/src/EventBus.d.ts.map +0 -1
  99. package/dist/orbit-db/src/MigrationDriver.d.ts +0 -55
  100. package/dist/orbit-db/src/MigrationDriver.d.ts.map +0 -1
  101. package/dist/orbit-db/src/Model.d.ts +0 -564
  102. package/dist/orbit-db/src/Model.d.ts.map +0 -1
  103. package/dist/orbit-db/src/ModelCollection.d.ts +0 -35
  104. package/dist/orbit-db/src/ModelCollection.d.ts.map +0 -1
  105. package/dist/orbit-db/src/index.d.ts +0 -34
  106. package/dist/orbit-db/src/index.d.ts.map +0 -1
  107. package/dist/orbit-db/src/types.d.ts +0 -146
  108. package/dist/orbit-db/src/types.d.ts.map +0 -1
  109. package/dist/orbit-queue/src/Consumer.d.ts +0 -67
  110. package/dist/orbit-queue/src/Consumer.d.ts.map +0 -1
  111. package/dist/orbit-queue/src/Job.d.ts +0 -76
  112. package/dist/orbit-queue/src/Job.d.ts.map +0 -1
  113. package/dist/orbit-queue/src/OrbitQueue.d.ts +0 -74
  114. package/dist/orbit-queue/src/OrbitQueue.d.ts.map +0 -1
  115. package/dist/orbit-queue/src/QueueManager.d.ts +0 -86
  116. package/dist/orbit-queue/src/QueueManager.d.ts.map +0 -1
  117. package/dist/orbit-queue/src/Queueable.d.ts +0 -63
  118. package/dist/orbit-queue/src/Queueable.d.ts.map +0 -1
  119. package/dist/orbit-queue/src/Worker.d.ts +0 -48
  120. package/dist/orbit-queue/src/Worker.d.ts.map +0 -1
  121. package/dist/orbit-queue/src/drivers/DatabaseDriver.d.ts +0 -60
  122. package/dist/orbit-queue/src/drivers/DatabaseDriver.d.ts.map +0 -1
  123. package/dist/orbit-queue/src/drivers/KafkaDriver.d.ts +0 -134
  124. package/dist/orbit-queue/src/drivers/KafkaDriver.d.ts.map +0 -1
  125. package/dist/orbit-queue/src/drivers/MemoryDriver.d.ts +0 -45
  126. package/dist/orbit-queue/src/drivers/MemoryDriver.d.ts.map +0 -1
  127. package/dist/orbit-queue/src/drivers/QueueDriver.d.ts +0 -89
  128. package/dist/orbit-queue/src/drivers/QueueDriver.d.ts.map +0 -1
  129. package/dist/orbit-queue/src/drivers/RedisDriver.d.ts +0 -79
  130. package/dist/orbit-queue/src/drivers/RedisDriver.d.ts.map +0 -1
  131. package/dist/orbit-queue/src/drivers/SQSDriver.d.ts +0 -100
  132. package/dist/orbit-queue/src/drivers/SQSDriver.d.ts.map +0 -1
  133. package/dist/orbit-queue/src/index.d.ts +0 -45
  134. package/dist/orbit-queue/src/index.d.ts.map +0 -1
  135. package/dist/orbit-queue/src/serializers/ClassNameSerializer.d.ts +0 -46
  136. package/dist/orbit-queue/src/serializers/ClassNameSerializer.d.ts.map +0 -1
  137. package/dist/orbit-queue/src/serializers/JobSerializer.d.ts +0 -36
  138. package/dist/orbit-queue/src/serializers/JobSerializer.d.ts.map +0 -1
  139. package/dist/orbit-queue/src/serializers/JsonSerializer.d.ts +0 -32
  140. package/dist/orbit-queue/src/serializers/JsonSerializer.d.ts.map +0 -1
  141. package/dist/orbit-queue/src/types.d.ts +0 -85
  142. package/dist/orbit-queue/src/types.d.ts.map +0 -1
  143. package/dist/serializers/ClassNameSerializer.d.ts +0 -46
  144. package/dist/serializers/ClassNameSerializer.d.ts.map +0 -1
  145. package/dist/serializers/JobSerializer.d.ts +0 -36
  146. package/dist/serializers/JobSerializer.d.ts.map +0 -1
  147. package/dist/serializers/JsonSerializer.d.ts +0 -32
  148. package/dist/serializers/JsonSerializer.d.ts.map +0 -1
  149. package/dist/types.d.ts +0 -85
  150. package/dist/types.d.ts.map +0 -1
@@ -0,0 +1,1132 @@
1
+ import { GravitoOrbit, PlanetCore } from 'gravito-core';
2
+
3
+ /**
4
+ * Serialized job payload.
5
+ */
6
+ interface SerializedJob {
7
+ /**
8
+ * Unique job identifier.
9
+ */
10
+ id: string;
11
+ /**
12
+ * Serializer type: `'json'` or `'class'`.
13
+ */
14
+ type: 'json' | 'class';
15
+ /**
16
+ * Serialized data.
17
+ */
18
+ data: string;
19
+ /**
20
+ * Class name (only for `type === 'class'`).
21
+ */
22
+ className?: string;
23
+ /**
24
+ * Created timestamp.
25
+ */
26
+ createdAt: number;
27
+ /**
28
+ * Delay before execution (seconds).
29
+ */
30
+ delaySeconds?: number;
31
+ /**
32
+ * Current attempt number.
33
+ */
34
+ attempts?: number;
35
+ /**
36
+ * Maximum attempts.
37
+ */
38
+ maxAttempts?: number;
39
+ }
40
+ /**
41
+ * Topic options (for Kafka, etc.).
42
+ */
43
+ interface TopicOptions {
44
+ /**
45
+ * Number of partitions.
46
+ */
47
+ partitions?: number;
48
+ /**
49
+ * Replication factor.
50
+ */
51
+ replicationFactor?: number;
52
+ /**
53
+ * Additional config.
54
+ */
55
+ config?: Record<string, string>;
56
+ }
57
+ /**
58
+ * Queue connection config.
59
+ */
60
+ interface QueueConnectionConfig {
61
+ /**
62
+ * Driver type.
63
+ */
64
+ driver: 'memory' | 'database' | 'redis' | 'kafka' | 'sqs' | 'rabbitmq' | 'nats';
65
+ /**
66
+ * Driver-specific config.
67
+ */
68
+ [key: string]: unknown;
69
+ }
70
+ /**
71
+ * Queue manager config.
72
+ */
73
+ interface QueueConfig {
74
+ /**
75
+ * Default connection name.
76
+ */
77
+ default?: string;
78
+ /**
79
+ * Connection configs.
80
+ */
81
+ connections?: Record<string, QueueConnectionConfig>;
82
+ /**
83
+ * Default serializer type.
84
+ */
85
+ defaultSerializer?: 'json' | 'class';
86
+ }
87
+
88
+ /**
89
+ * Queue driver interface.
90
+ *
91
+ * All queue drivers must implement this interface.
92
+ * Defines basic queue operations plus optional enterprise-grade capabilities.
93
+ *
94
+ * @example
95
+ * ```typescript
96
+ * class MyDriver implements QueueDriver {
97
+ * async push(queue: string, job: SerializedJob): Promise<void> {
98
+ * // push a job
99
+ * }
100
+ *
101
+ * async pop(queue: string): Promise<SerializedJob | null> {
102
+ * // pop a job
103
+ * }
104
+ *
105
+ * async size(queue: string): Promise<number> {
106
+ * // queue size
107
+ * }
108
+ *
109
+ * async clear(queue: string): Promise<void> {
110
+ * // clear queue
111
+ * }
112
+ * }
113
+ * ```
114
+ */
115
+ interface QueueDriver {
116
+ /**
117
+ * Push a job to a queue.
118
+ * @param queue - Queue name
119
+ * @param job - Serialized job
120
+ */
121
+ push(queue: string, job: SerializedJob): Promise<void>;
122
+ /**
123
+ * Pop a job from a queue (non-blocking).
124
+ * @param queue - Queue name
125
+ * @returns Serialized job, or `null` if the queue is empty
126
+ */
127
+ pop(queue: string): Promise<SerializedJob | null>;
128
+ /**
129
+ * Get queue size.
130
+ * @param queue - Queue name
131
+ * @returns Number of jobs in the queue
132
+ */
133
+ size(queue: string): Promise<number>;
134
+ /**
135
+ * Clear a queue.
136
+ * @param queue - Queue name
137
+ */
138
+ clear(queue: string): Promise<void>;
139
+ /**
140
+ * Push multiple jobs (optional, higher throughput).
141
+ * @param queue - Queue name
142
+ * @param jobs - Serialized job array
143
+ */
144
+ pushMany?(queue: string, jobs: SerializedJob[]): Promise<void>;
145
+ /**
146
+ * Pop multiple jobs (optional, higher throughput).
147
+ * @param queue - Queue name
148
+ * @param count - Max number of jobs to pop
149
+ * @returns Serialized job array
150
+ */
151
+ popMany?(queue: string, count: number): Promise<SerializedJob[]>;
152
+ /**
153
+ * Acknowledge a message (enterprise capability, e.g. Kafka/SQS).
154
+ * @param messageId - Message ID
155
+ */
156
+ acknowledge?(messageId: string): Promise<void>;
157
+ /**
158
+ * Subscribe to a queue (push-based model, e.g. Kafka/SQS).
159
+ * @param queue - Queue name
160
+ * @param callback - Callback to process jobs
161
+ */
162
+ subscribe?(queue: string, callback: (job: SerializedJob) => Promise<void>): Promise<void>;
163
+ /**
164
+ * Create a topic (Kafka, etc.).
165
+ * @param topic - Topic name
166
+ * @param options - Topic options
167
+ */
168
+ createTopic?(topic: string, options?: TopicOptions): Promise<void>;
169
+ /**
170
+ * Delete a topic (Kafka, etc.).
171
+ * @param topic - Topic name
172
+ */
173
+ deleteTopic?(topic: string): Promise<void>;
174
+ }
175
+
176
+ /**
177
+ * Queueable interface.
178
+ *
179
+ * Classes implementing this interface can be pushed to a queue for execution.
180
+ * Provides a fluent API for queue/connection/delay configuration.
181
+ *
182
+ * @example
183
+ * ```typescript
184
+ * class MyJob implements Queueable {
185
+ * queueName?: string
186
+ * connectionName?: string
187
+ * delaySeconds?: number
188
+ *
189
+ * onQueue(queue: string): this {
190
+ * this.queueName = queue
191
+ * return this
192
+ * }
193
+ *
194
+ * onConnection(connection: string): this {
195
+ * this.connectionName = connection
196
+ * return this
197
+ * }
198
+ *
199
+ * delay(seconds: number): this {
200
+ * this.delaySeconds = seconds
201
+ * return this
202
+ * }
203
+ * }
204
+ * ```
205
+ */
206
+ interface Queueable {
207
+ /**
208
+ * Queue name where the job should be pushed.
209
+ */
210
+ queueName?: string;
211
+ /**
212
+ * Connection name the job should use.
213
+ */
214
+ connectionName?: string;
215
+ /**
216
+ * Delay before execution (seconds).
217
+ */
218
+ delaySeconds?: number;
219
+ /**
220
+ * Set target queue.
221
+ * @param queue - Queue name
222
+ * @returns Self for fluent chaining
223
+ */
224
+ onQueue(queue: string): this;
225
+ /**
226
+ * Set target connection.
227
+ * @param connection - Connection name
228
+ * @returns Self for fluent chaining
229
+ */
230
+ onConnection(connection: string): this;
231
+ /**
232
+ * Set delay (seconds).
233
+ * @param delay - Delay seconds
234
+ * @returns Self for fluent chaining
235
+ */
236
+ delay(delay: number): this;
237
+ }
238
+
239
+ /**
240
+ * Base Job.
241
+ *
242
+ * All tasks that should be pushed to a queue should extend this class.
243
+ * Implements the `Queueable` interface, providing a fluent API for queue/connection/delay.
244
+ *
245
+ * @example
246
+ * ```typescript
247
+ * class SendWelcomeEmail extends Job {
248
+ * constructor(private userId: string) {
249
+ * super()
250
+ * }
251
+ *
252
+ * async handle(): Promise<void> {
253
+ * const user = await User.find(this.userId)
254
+ * await mail.send(new WelcomeEmail(user))
255
+ * }
256
+ * }
257
+ *
258
+ * // Usage
259
+ * await queue.push(new SendWelcomeEmail('123'))
260
+ * .onQueue('emails')
261
+ * .delay(60)
262
+ * ```
263
+ */
264
+ declare abstract class Job implements Queueable {
265
+ /**
266
+ * Queue name.
267
+ */
268
+ queueName?: string;
269
+ /**
270
+ * Connection name.
271
+ */
272
+ connectionName?: string;
273
+ /**
274
+ * Delay before execution (seconds).
275
+ */
276
+ delaySeconds?: number;
277
+ /**
278
+ * Current attempt number.
279
+ */
280
+ attempts?: number;
281
+ /**
282
+ * Maximum attempts.
283
+ */
284
+ maxAttempts?: number;
285
+ /**
286
+ * Set target queue.
287
+ */
288
+ onQueue(queue: string): this;
289
+ /**
290
+ * Set target connection.
291
+ */
292
+ onConnection(connection: string): this;
293
+ /**
294
+ * Set delay (seconds).
295
+ */
296
+ delay(delay: number): this;
297
+ /**
298
+ * Job handler logic.
299
+ *
300
+ * Subclasses must implement this method.
301
+ */
302
+ abstract handle(): Promise<void>;
303
+ /**
304
+ * Failure handler (optional).
305
+ *
306
+ * Called when the job fails and reaches the maximum number of attempts.
307
+ * Subclasses can override to implement custom failure handling.
308
+ *
309
+ * @param error - Error instance
310
+ */
311
+ failed(_error: Error): Promise<void>;
312
+ }
313
+
314
+ /**
315
+ * Job serializer interface.
316
+ *
317
+ * Responsible for serializing and deserializing jobs.
318
+ * Supports multiple strategies (JSON, class-name, etc.).
319
+ *
320
+ * @example
321
+ * ```typescript
322
+ * class MySerializer implements JobSerializer {
323
+ * serialize(job: Job): SerializedJob {
324
+ * // serialization logic
325
+ * }
326
+ *
327
+ * deserialize(serialized: SerializedJob): Job {
328
+ * // deserialization logic
329
+ * }
330
+ * }
331
+ * ```
332
+ */
333
+ interface JobSerializer {
334
+ /**
335
+ * Serialize a job.
336
+ * @param job - Job instance
337
+ * @returns Serialized job payload
338
+ */
339
+ serialize(job: Job): SerializedJob;
340
+ /**
341
+ * Deserialize a job.
342
+ * @param serialized - Serialized job payload
343
+ * @returns Job instance
344
+ */
345
+ deserialize(serialized: SerializedJob): Job;
346
+ }
347
+
348
+ /**
349
+ * Queue Manager
350
+ *
351
+ * Manages multiple queue connections and drivers, exposing a unified API for pushing and consuming jobs.
352
+ * Supports lazy-loading drivers to keep the core lightweight.
353
+ *
354
+ * @example
355
+ * ```typescript
356
+ * const manager = new QueueManager({
357
+ * default: 'database',
358
+ * connections: {
359
+ * database: { driver: 'database', table: 'jobs' },
360
+ * redis: { driver: 'redis', url: 'redis://...' }
361
+ * }
362
+ * })
363
+ *
364
+ * await manager.push(new SendEmail('user@example.com'))
365
+ * ```
366
+ */
367
+ declare class QueueManager {
368
+ private drivers;
369
+ private serializers;
370
+ private defaultConnection;
371
+ private defaultSerializer;
372
+ constructor(config?: QueueConfig);
373
+ /**
374
+ * Register a connection.
375
+ * @param name - Connection name
376
+ * @param config - Connection config
377
+ */
378
+ registerConnection(name: string, config: unknown): void;
379
+ /**
380
+ * Get a driver for a connection.
381
+ * @param connection - Connection name
382
+ * @returns Driver instance
383
+ */
384
+ getDriver(connection: string): QueueDriver;
385
+ /**
386
+ * Get a serializer.
387
+ * @param type - Serializer type
388
+ * @returns Serializer instance
389
+ */
390
+ getSerializer(type?: string): JobSerializer;
391
+ /**
392
+ * Register Job classes (used by ClassNameSerializer).
393
+ * @param jobClasses - Job class array
394
+ */
395
+ registerJobClasses(jobClasses: Array<new (...args: unknown[]) => Job>): void;
396
+ /**
397
+ * Push a Job to the queue.
398
+ *
399
+ * @template T - The type of the job.
400
+ * @param job - Job instance to push.
401
+ * @returns The same job instance (for fluent chaining).
402
+ *
403
+ * @example
404
+ * ```typescript
405
+ * await manager.push(new SendEmailJob('user@example.com'));
406
+ * ```
407
+ */
408
+ push<T extends Job & Queueable>(job: T): Promise<T>;
409
+ /**
410
+ * Push multiple jobs to the queue.
411
+ *
412
+ * @template T - The type of the jobs.
413
+ * @param jobs - Array of job instances.
414
+ *
415
+ * @example
416
+ * ```typescript
417
+ * await manager.pushMany([new JobA(), new JobB()]);
418
+ * ```
419
+ */
420
+ pushMany<T extends Job & Queueable>(jobs: T[]): Promise<void>;
421
+ /**
422
+ * Pop a job from the queue.
423
+ *
424
+ * @param queue - Queue name (default: 'default').
425
+ * @param connection - Connection name (optional).
426
+ * @returns Job instance or null if queue is empty.
427
+ *
428
+ * @example
429
+ * ```typescript
430
+ * const job = await manager.pop('emails');
431
+ * if (job) await job.handle();
432
+ * ```
433
+ */
434
+ pop(queue?: string, connection?: string): Promise<Job | null>;
435
+ /**
436
+ * Get queue size.
437
+ *
438
+ * @param queue - Queue name (default: 'default').
439
+ * @param connection - Connection name (optional).
440
+ * @returns Number of jobs in the queue.
441
+ */
442
+ size(queue?: string, connection?: string): Promise<number>;
443
+ /**
444
+ * Clear all jobs from a queue.
445
+ *
446
+ * @param queue - Queue name (default: 'default').
447
+ * @param connection - Connection name (optional).
448
+ */
449
+ clear(queue?: string, connection?: string): Promise<void>;
450
+ }
451
+
452
+ /**
453
+ * Worker options.
454
+ */
455
+ interface WorkerOptions {
456
+ /**
457
+ * Maximum retry attempts.
458
+ */
459
+ maxAttempts?: number;
460
+ /**
461
+ * Job timeout (seconds).
462
+ */
463
+ timeout?: number;
464
+ /**
465
+ * Failure callback.
466
+ */
467
+ onFailed?: (job: Job, error: Error) => Promise<void>;
468
+ }
469
+ /**
470
+ * Base Worker.
471
+ *
472
+ * Responsible for executing `Job` instances.
473
+ * Provides error handling, retry logic, and timeout support.
474
+ *
475
+ * @example
476
+ * ```typescript
477
+ * const worker = new Worker({
478
+ * maxAttempts: 3,
479
+ * timeout: 60
480
+ * })
481
+ *
482
+ * await worker.process(job)
483
+ * ```
484
+ */
485
+ declare class Worker {
486
+ private options;
487
+ constructor(options?: WorkerOptions);
488
+ /**
489
+ * Process a Job.
490
+ * @param job - Job instance
491
+ */
492
+ process(job: Job): Promise<void>;
493
+ /**
494
+ * Handle failure.
495
+ */
496
+ private handleFailure;
497
+ }
498
+
499
+ /**
500
+ * Consumer options.
501
+ */
502
+ interface ConsumerOptions {
503
+ /**
504
+ * Queues to listen on.
505
+ */
506
+ queues: string[];
507
+ /**
508
+ * Connection name.
509
+ */
510
+ connection?: string;
511
+ /**
512
+ * Worker options.
513
+ */
514
+ workerOptions?: WorkerOptions;
515
+ /**
516
+ * Polling interval (milliseconds).
517
+ */
518
+ pollInterval?: number;
519
+ /**
520
+ * Whether to keep polling when queues are empty.
521
+ */
522
+ keepAlive?: boolean;
523
+ }
524
+ /**
525
+ * Consumer
526
+ *
527
+ * Consumes and executes jobs from queues.
528
+ * Supports embedded mode (inside the main app) and standalone mode (as a worker service).
529
+ *
530
+ * @example
531
+ * ```typescript
532
+ * // Embedded mode
533
+ * const consumer = new Consumer(queueManager, {
534
+ * queues: ['default', 'emails'],
535
+ * pollInterval: 1000
536
+ * })
537
+ *
538
+ * consumer.start()
539
+ *
540
+ * // Standalone mode (CLI)
541
+ * // Start via CLI tooling with graceful shutdown
542
+ * ```
543
+ */
544
+ declare class Consumer {
545
+ private queueManager;
546
+ private options;
547
+ private running;
548
+ private stopRequested;
549
+ constructor(queueManager: QueueManager, options: ConsumerOptions);
550
+ /**
551
+ * Start the consumer loop.
552
+ */
553
+ start(): Promise<void>;
554
+ /**
555
+ * Stop the consumer loop (graceful shutdown).
556
+ */
557
+ stop(): Promise<void>;
558
+ /**
559
+ * Check whether the consumer is running.
560
+ */
561
+ isRunning(): boolean;
562
+ }
563
+
564
+ /**
565
+ * Generic database service interface.
566
+ * Users should implement this interface with their preferred ORM/database client.
567
+ */
568
+ interface DatabaseService {
569
+ /**
570
+ * Execute a raw SQL query.
571
+ * @param sql - The SQL query string with placeholders ($1, $2, etc.)
572
+ * @param bindings - The values to bind to placeholders
573
+ */
574
+ execute<T = unknown>(sql: string, bindings?: unknown[]): Promise<T[] | T>;
575
+ /**
576
+ * Execute multiple queries within a transaction.
577
+ * @param callback - The callback to execute within the transaction
578
+ */
579
+ transaction<T>(callback: (tx: DatabaseService) => Promise<T>): Promise<T>;
580
+ }
581
+ /**
582
+ * Database driver configuration.
583
+ */
584
+ interface DatabaseDriverConfig {
585
+ /**
586
+ * Table name (default: `jobs`).
587
+ */
588
+ table?: string;
589
+ /**
590
+ * Database service instance that implements DatabaseService interface.
591
+ */
592
+ dbService?: DatabaseService;
593
+ }
594
+ /**
595
+ * Database Driver
596
+ *
597
+ * Uses a database as the queue backend.
598
+ * Works with any database service that implements the DatabaseService interface.
599
+ *
600
+ * @example
601
+ * ```typescript
602
+ * // Create a database service adapter
603
+ * const dbService = {
604
+ * execute: async (sql, bindings) => yourDbClient.query(sql, bindings),
605
+ * transaction: async (callback) => yourDbClient.transaction(callback),
606
+ * }
607
+ *
608
+ * const driver = new DatabaseDriver({ dbService, table: 'jobs' })
609
+ * await driver.push('default', serializedJob)
610
+ * ```
611
+ */
612
+ declare class DatabaseDriver implements QueueDriver {
613
+ private tableName;
614
+ private dbService;
615
+ constructor(config: DatabaseDriverConfig);
616
+ /**
617
+ * Push a job to a queue.
618
+ */
619
+ push(queue: string, job: SerializedJob): Promise<void>;
620
+ /**
621
+ * Pop a job from the queue (FIFO, with delay support).
622
+ */
623
+ pop(queue: string): Promise<SerializedJob | null>;
624
+ /**
625
+ * Get queue size.
626
+ */
627
+ size(queue: string): Promise<number>;
628
+ /**
629
+ * Clear a queue.
630
+ */
631
+ clear(queue: string): Promise<void>;
632
+ /**
633
+ * Push multiple jobs.
634
+ */
635
+ pushMany(queue: string, jobs: SerializedJob[]): Promise<void>;
636
+ }
637
+
638
+ /**
639
+ * Kafka driver configuration.
640
+ */
641
+ interface KafkaDriverConfig {
642
+ /**
643
+ * Kafka client instance (kafkajs).
644
+ */
645
+ client: {
646
+ producer: () => {
647
+ connect: () => Promise<void>;
648
+ send: (args: {
649
+ topic: string;
650
+ messages: Array<{
651
+ key?: string;
652
+ value: string;
653
+ }>;
654
+ }) => Promise<void>;
655
+ disconnect: () => Promise<void>;
656
+ };
657
+ admin: () => {
658
+ connect: () => Promise<void>;
659
+ createTopics: (args: {
660
+ topics: Array<{
661
+ topic: string;
662
+ numPartitions?: number;
663
+ replicationFactor?: number;
664
+ }>;
665
+ }) => Promise<void>;
666
+ deleteTopics: (args: {
667
+ topics: string[];
668
+ }) => Promise<void>;
669
+ disconnect: () => Promise<void>;
670
+ };
671
+ consumer: (args: {
672
+ groupId: string;
673
+ }) => {
674
+ connect: () => Promise<void>;
675
+ subscribe: (args: {
676
+ topics: string[];
677
+ }) => Promise<void>;
678
+ run: (args: {
679
+ eachMessage: (args: {
680
+ topic: string;
681
+ partition: number;
682
+ message: {
683
+ key?: Buffer;
684
+ value: Buffer;
685
+ offset: string;
686
+ };
687
+ }) => Promise<void>;
688
+ }) => Promise<void>;
689
+ disconnect: () => Promise<void>;
690
+ };
691
+ };
692
+ /**
693
+ * Consumer group ID (for consuming messages).
694
+ */
695
+ consumerGroupId?: string;
696
+ }
697
+ /**
698
+ * Kafka Driver
699
+ *
700
+ * Uses Apache Kafka as the queue backend.
701
+ * Supports topic management, consumer groups, and batch operations.
702
+ *
703
+ * Requires `kafkajs`.
704
+ *
705
+ * @example
706
+ * ```typescript
707
+ * import { Kafka } from 'kafkajs'
708
+ *
709
+ * const kafka = new Kafka({
710
+ * brokers: ['localhost:9092'],
711
+ * clientId: 'gravito-app'
712
+ * })
713
+ *
714
+ * const driver = new KafkaDriver({ client: kafka, consumerGroupId: 'workers' })
715
+ * await driver.push('default', serializedJob)
716
+ * ```
717
+ */
718
+ declare class KafkaDriver implements QueueDriver {
719
+ private client;
720
+ private consumerGroupId;
721
+ private producer?;
722
+ private admin?;
723
+ constructor(config: KafkaDriverConfig);
724
+ /**
725
+ * Ensure the producer is connected.
726
+ */
727
+ private ensureProducer;
728
+ /**
729
+ * Ensure the admin client is connected.
730
+ */
731
+ private ensureAdmin;
732
+ /**
733
+ * Push a job to a topic.
734
+ */
735
+ push(queue: string, job: SerializedJob): Promise<void>;
736
+ /**
737
+ * Pop is not supported for Kafka.
738
+ *
739
+ * Note: Kafka uses a push-based model, so you should use `subscribe()`.
740
+ */
741
+ pop(_queue: string): Promise<SerializedJob | null>;
742
+ /**
743
+ * Kafka does not provide a direct queue size.
744
+ *
745
+ * Returns 0; use Kafka tooling/metrics for lag/size insights.
746
+ */
747
+ size(_queue: string): Promise<number>;
748
+ /**
749
+ * Clear a queue by deleting the topic.
750
+ */
751
+ clear(queue: string): Promise<void>;
752
+ /**
753
+ * Push multiple jobs.
754
+ */
755
+ pushMany(queue: string, jobs: SerializedJob[]): Promise<void>;
756
+ /**
757
+ * Create a topic.
758
+ */
759
+ createTopic(topic: string, options?: TopicOptions): Promise<void>;
760
+ /**
761
+ * Delete a topic.
762
+ */
763
+ deleteTopic(topic: string): Promise<void>;
764
+ /**
765
+ * Subscribe to a topic (push-based model).
766
+ */
767
+ subscribe(queue: string, callback: (job: SerializedJob) => Promise<void>): Promise<void>;
768
+ }
769
+
770
+ /**
771
+ * Memory Driver
772
+ *
773
+ * In-memory driver for development and testing.
774
+ * All data is stored in memory and will be lost when the process restarts.
775
+ *
776
+ * Zero-config: works out of the box.
777
+ *
778
+ * @example
779
+ * ```typescript
780
+ * const driver = new MemoryDriver()
781
+ * await driver.push('default', serializedJob)
782
+ * const job = await driver.pop('default')
783
+ * ```
784
+ */
785
+ declare class MemoryDriver implements QueueDriver {
786
+ private queues;
787
+ /**
788
+ * Push a job to a queue.
789
+ */
790
+ push(queue: string, job: SerializedJob): Promise<void>;
791
+ /**
792
+ * Pop a job from a queue (FIFO).
793
+ */
794
+ pop(queue: string): Promise<SerializedJob | null>;
795
+ /**
796
+ * Get queue size.
797
+ */
798
+ size(queue: string): Promise<number>;
799
+ /**
800
+ * Clear a queue.
801
+ */
802
+ clear(queue: string): Promise<void>;
803
+ /**
804
+ * Push multiple jobs.
805
+ */
806
+ pushMany(queue: string, jobs: SerializedJob[]): Promise<void>;
807
+ /**
808
+ * Pop multiple jobs.
809
+ */
810
+ popMany(queue: string, count: number): Promise<SerializedJob[]>;
811
+ }
812
+
813
+ /**
814
+ * Redis driver configuration.
815
+ */
816
+ interface RedisDriverConfig {
817
+ /**
818
+ * Redis client instance (ioredis or node-redis).
819
+ */
820
+ client: {
821
+ lpush: (key: string, ...values: string[]) => Promise<number>;
822
+ rpop: (key: string) => Promise<string | null>;
823
+ llen: (key: string) => Promise<number>;
824
+ del: (key: string) => Promise<number>;
825
+ lpushx?: (key: string, ...values: string[]) => Promise<number>;
826
+ rpoplpush?: (src: string, dst: string) => Promise<string | null>;
827
+ [key: string]: unknown;
828
+ };
829
+ /**
830
+ * Key prefix (default: `queue:`).
831
+ */
832
+ prefix?: string;
833
+ }
834
+ /**
835
+ * Redis Driver
836
+ *
837
+ * Uses Redis as the queue backend.
838
+ * Implements FIFO via Redis Lists (LPUSH/RPOP).
839
+ *
840
+ * Requires `ioredis` or `redis`.
841
+ *
842
+ * @example
843
+ * ```typescript
844
+ * import Redis from 'ioredis'
845
+ *
846
+ * const redis = new Redis('redis://localhost:6379')
847
+ * const driver = new RedisDriver({ client: redis })
848
+ *
849
+ * await driver.push('default', serializedJob)
850
+ * ```
851
+ */
852
+ declare class RedisDriver implements QueueDriver {
853
+ private prefix;
854
+ private client;
855
+ constructor(config: RedisDriverConfig);
856
+ /**
857
+ * Get full Redis key for a queue.
858
+ */
859
+ private getKey;
860
+ /**
861
+ * Push a job (LPUSH).
862
+ */
863
+ push(queue: string, job: SerializedJob): Promise<void>;
864
+ /**
865
+ * Pop a job (RPOP, FIFO).
866
+ */
867
+ pop(queue: string): Promise<SerializedJob | null>;
868
+ /**
869
+ * Parse Redis payload.
870
+ */
871
+ private parsePayload;
872
+ /**
873
+ * Get queue size.
874
+ */
875
+ size(queue: string): Promise<number>;
876
+ /**
877
+ * Clear a queue.
878
+ */
879
+ clear(queue: string): Promise<void>;
880
+ /**
881
+ * Push multiple jobs.
882
+ */
883
+ pushMany(queue: string, jobs: SerializedJob[]): Promise<void>;
884
+ /**
885
+ * Pop multiple jobs.
886
+ */
887
+ popMany(queue: string, count: number): Promise<SerializedJob[]>;
888
+ }
889
+
890
+ /**
891
+ * SQS driver configuration.
892
+ */
893
+ interface SQSDriverConfig {
894
+ /**
895
+ * SQS client instance (`@aws-sdk/client-sqs`).
896
+ */
897
+ client: {
898
+ send: (command: unknown) => Promise<{
899
+ MessageId?: string;
900
+ Messages?: Array<{
901
+ MessageId?: string;
902
+ ReceiptHandle?: string;
903
+ Body?: string;
904
+ }>;
905
+ }>;
906
+ };
907
+ /**
908
+ * Queue URL prefix (used to build full queue URLs).
909
+ */
910
+ queueUrlPrefix?: string;
911
+ /**
912
+ * Visibility timeout (seconds, default: 30).
913
+ */
914
+ visibilityTimeout?: number;
915
+ /**
916
+ * Long-polling duration (seconds, default: 20).
917
+ */
918
+ waitTimeSeconds?: number;
919
+ }
920
+ /**
921
+ * SQS Driver
922
+ *
923
+ * Uses AWS SQS as the queue backend.
924
+ * Supports standard/FIFO queues, long polling, DLQ setups, etc.
925
+ *
926
+ * Requires `@aws-sdk/client-sqs`.
927
+ *
928
+ * @example
929
+ * ```typescript
930
+ * import { SQSClient } from '@aws-sdk/client-sqs'
931
+ *
932
+ * const sqs = new SQSClient({
933
+ * region: 'us-east-1',
934
+ * credentials: {
935
+ * accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
936
+ * secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
937
+ * }
938
+ * })
939
+ *
940
+ * const driver = new SQSDriver({ client: sqs })
941
+ * await driver.push('default', serializedJob)
942
+ * ```
943
+ */
944
+ declare class SQSDriver implements QueueDriver {
945
+ private client;
946
+ private queueUrlPrefix;
947
+ private visibilityTimeout;
948
+ private waitTimeSeconds;
949
+ private queueUrls;
950
+ constructor(config: SQSDriverConfig);
951
+ /**
952
+ * Resolve the full queue URL.
953
+ */
954
+ private getQueueUrl;
955
+ /**
956
+ * Push a job to SQS.
957
+ */
958
+ push(queue: string, job: SerializedJob): Promise<void>;
959
+ /**
960
+ * Pop a job (long polling).
961
+ */
962
+ pop(queue: string): Promise<SerializedJob | null>;
963
+ /**
964
+ * Get queue size (approximate).
965
+ */
966
+ size(queue: string): Promise<number>;
967
+ /**
968
+ * Clear a queue by receiving and deleting messages.
969
+ *
970
+ * Note: SQS does not provide a direct "purge" API via this wrapper. This method will
971
+ * keep receiving and deleting messages until the queue is empty.
972
+ */
973
+ clear(queue: string): Promise<void>;
974
+ /**
975
+ * Push multiple jobs.
976
+ */
977
+ pushMany(queue: string, jobs: SerializedJob[]): Promise<void>;
978
+ /**
979
+ * Acknowledge is not supported via messageId.
980
+ */
981
+ acknowledge(_messageId: string): Promise<void>;
982
+ /**
983
+ * Delete a message (acknowledge processing completion).
984
+ */
985
+ deleteMessage(queue: string, receiptHandle: string): Promise<void>;
986
+ }
987
+
988
+ /**
989
+ * Orbit Queue configuration options.
990
+ */
991
+ interface OrbitStreamOptions extends QueueConfig {
992
+ /**
993
+ * Whether to auto-start an embedded worker in development.
994
+ */
995
+ autoStartWorker?: boolean;
996
+ /**
997
+ * Embedded worker options.
998
+ */
999
+ workerOptions?: ConsumerOptions;
1000
+ }
1001
+ /**
1002
+ * Orbit Queue
1003
+ *
1004
+ * Gravito Orbit implementation providing queue functionality.
1005
+ * Integrates with PlanetCore and injects a `queue` service into the Photon Context.
1006
+ *
1007
+ * @example
1008
+ * ```typescript
1009
+ * const core = await PlanetCore.boot({
1010
+ * orbits: [
1011
+ * OrbitStream.configure({
1012
+ * default: 'database',
1013
+ * connections: {
1014
+ * database: { driver: 'database', table: 'jobs' }
1015
+ * }
1016
+ * })
1017
+ * ]
1018
+ * })
1019
+ *
1020
+ * // Use in a controller/handler
1021
+ * const queue = c.get('queue')
1022
+ * await queue.push(new SendEmail('user@example.com'))
1023
+ * ```
1024
+ */
1025
+ declare class OrbitStream implements GravitoOrbit {
1026
+ private options;
1027
+ private queueManager?;
1028
+ private consumer?;
1029
+ constructor(options?: OrbitStreamOptions);
1030
+ /**
1031
+ * Static configuration helper.
1032
+ */
1033
+ static configure(options: OrbitStreamOptions): OrbitStream;
1034
+ /**
1035
+ * Install into PlanetCore.
1036
+ */
1037
+ install(core: PlanetCore): void;
1038
+ /**
1039
+ * Start embedded worker.
1040
+ */
1041
+ startWorker(options: ConsumerOptions): void;
1042
+ /**
1043
+ * Stop embedded worker.
1044
+ */
1045
+ stopWorker(): Promise<void>;
1046
+ /**
1047
+ * Get QueueManager instance.
1048
+ */
1049
+ getQueueManager(): QueueManager | undefined;
1050
+ }
1051
+ declare module 'gravito-core' {
1052
+ interface GravitoVariables {
1053
+ /** Queue manager for job processing */
1054
+ queue?: QueueManager;
1055
+ /** Database service (from orbit-db) */
1056
+ db?: unknown;
1057
+ }
1058
+ }
1059
+
1060
+ /**
1061
+ * Class name serializer (Laravel-style).
1062
+ *
1063
+ * Stores the class name and properties, then recreates an instance at runtime.
1064
+ * This is the recommended serializer because it can restore class instances correctly.
1065
+ *
1066
+ * Requirement: Job classes must be dynamically loadable (by class name).
1067
+ *
1068
+ * @example
1069
+ * ```typescript
1070
+ * const serializer = new ClassNameSerializer()
1071
+ * const serialized = serializer.serialize(new SendEmail('user@example.com'))
1072
+ * // serialized.data contains class name and properties
1073
+ *
1074
+ * const job = serializer.deserialize(serialized)
1075
+ * // job is an instance of SendEmail
1076
+ * ```
1077
+ */
1078
+ declare class ClassNameSerializer implements JobSerializer {
1079
+ /**
1080
+ * Job class registry (for resolving classes by name).
1081
+ */
1082
+ private jobClasses;
1083
+ /**
1084
+ * Register a Job class.
1085
+ * @param jobClass - Job class
1086
+ */
1087
+ register(jobClass: new (...args: unknown[]) => Job): void;
1088
+ /**
1089
+ * Register multiple Job classes.
1090
+ * @param jobClasses - Job class array
1091
+ */
1092
+ registerMany(jobClasses: Array<new (...args: unknown[]) => Job>): void;
1093
+ /**
1094
+ * Serialize a Job.
1095
+ */
1096
+ serialize(job: Job): SerializedJob;
1097
+ /**
1098
+ * Deserialize a Job.
1099
+ */
1100
+ deserialize(serialized: SerializedJob): Job;
1101
+ }
1102
+
1103
+ /**
1104
+ * JSON Serializer
1105
+ *
1106
+ * Serializes jobs using JSON.
1107
+ * Suitable for simple scenarios where you only need to persist plain properties.
1108
+ *
1109
+ * Limitation: cannot restore class instances, functions, or complex objects.
1110
+ *
1111
+ * @example
1112
+ * ```typescript
1113
+ * const serializer = new JsonSerializer()
1114
+ * const serialized = serializer.serialize(job)
1115
+ * const job = serializer.deserialize(serialized)
1116
+ * ```
1117
+ */
1118
+ declare class JsonSerializer implements JobSerializer {
1119
+ /**
1120
+ * Serialize a job.
1121
+ */
1122
+ serialize(job: Job): SerializedJob;
1123
+ /**
1124
+ * Deserialize a job.
1125
+ *
1126
+ * Note: this implementation only restores properties and does not recreate class instances.
1127
+ * For class instances, use `ClassNameSerializer`.
1128
+ */
1129
+ deserialize(serialized: SerializedJob): Job;
1130
+ }
1131
+
1132
+ export { ClassNameSerializer, Consumer, type ConsumerOptions, DatabaseDriver, type DatabaseDriverConfig, Job, type JobSerializer, JsonSerializer, KafkaDriver, type KafkaDriverConfig, MemoryDriver, OrbitStream, type OrbitStreamOptions, type QueueConfig, type QueueConnectionConfig, type QueueDriver, QueueManager, type Queueable, RedisDriver, type RedisDriverConfig, SQSDriver, type SQSDriverConfig, type SerializedJob, type TopicOptions, Worker, type WorkerOptions };