bunqueue-client 0.1.2 → 0.1.3

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 (60) hide show
  1. package/README.md +45 -3
  2. package/dist/bunqueue/aging.d.ts +17 -0
  3. package/dist/bunqueue/aging.js +53 -0
  4. package/dist/bunqueue/batch.d.ts +17 -0
  5. package/dist/bunqueue/batch.js +60 -0
  6. package/dist/bunqueue/bunqueue-api.d.ts +87 -0
  7. package/dist/bunqueue/bunqueue-api.js +116 -0
  8. package/dist/bunqueue/bunqueue.d.ts +57 -0
  9. package/dist/bunqueue/bunqueue.js +182 -0
  10. package/dist/bunqueue/cancellation.d.ts +20 -0
  11. package/dist/bunqueue/cancellation.js +48 -0
  12. package/dist/bunqueue/circuit-breaker.d.ts +21 -0
  13. package/dist/bunqueue/circuit-breaker.js +69 -0
  14. package/dist/bunqueue/dedup-debounce.d.ts +14 -0
  15. package/dist/bunqueue/dedup-debounce.js +37 -0
  16. package/dist/bunqueue/dlq-rate-limit.d.ts +33 -0
  17. package/dist/bunqueue/dlq-rate-limit.js +65 -0
  18. package/dist/bunqueue/rate-gate.d.ts +21 -0
  19. package/dist/bunqueue/rate-gate.js +48 -0
  20. package/dist/bunqueue/retry.d.ts +9 -0
  21. package/dist/bunqueue/retry.js +56 -0
  22. package/dist/bunqueue/triggers.d.ts +18 -0
  23. package/dist/bunqueue/triggers.js +42 -0
  24. package/dist/bunqueue/ttl.d.ts +18 -0
  25. package/dist/bunqueue/ttl.js +31 -0
  26. package/dist/bunqueue/types.d.ts +151 -0
  27. package/dist/bunqueue/types.js +6 -0
  28. package/dist/connection.d.ts +9 -1
  29. package/dist/connection.js +32 -47
  30. package/dist/index.d.ts +4 -1
  31. package/dist/index.js +3 -1
  32. package/dist/job.d.ts +2 -0
  33. package/dist/job.js +4 -0
  34. package/dist/queue-admin.js +2 -1
  35. package/dist/socket-factory.d.ts +4 -0
  36. package/dist/socket-factory.js +47 -0
  37. package/dist/worker-base.d.ts +13 -2
  38. package/dist/worker-base.js +29 -4
  39. package/dist/worker.js +8 -0
  40. package/package.json +1 -1
  41. package/src/bunqueue/aging.ts +62 -0
  42. package/src/bunqueue/batch.ts +79 -0
  43. package/src/bunqueue/bunqueue-api.ts +212 -0
  44. package/src/bunqueue/bunqueue.ts +210 -0
  45. package/src/bunqueue/cancellation.ts +54 -0
  46. package/src/bunqueue/circuit-breaker.ts +81 -0
  47. package/src/bunqueue/dedup-debounce.ts +43 -0
  48. package/src/bunqueue/dlq-rate-limit.ts +89 -0
  49. package/src/bunqueue/rate-gate.ts +53 -0
  50. package/src/bunqueue/retry.ts +72 -0
  51. package/src/bunqueue/triggers.ts +51 -0
  52. package/src/bunqueue/ttl.ts +38 -0
  53. package/src/bunqueue/types.ts +171 -0
  54. package/src/connection.ts +38 -46
  55. package/src/index.ts +22 -1
  56. package/src/job.ts +5 -0
  57. package/src/queue-admin.ts +2 -1
  58. package/src/socket-factory.ts +51 -0
  59. package/src/worker-base.ts +30 -4
  60. package/src/worker.ts +9 -0
package/README.md CHANGED
@@ -8,9 +8,9 @@ The bunqueue server runs on Bun, distributed as a binary or a Docker image. This
8
8
 
9
9
  | Runtime | Status | Notes |
10
10
  |---|---|---|
11
- | Node.js 20 or later | Supported, 58/58 e2e and 8/8 integration tests | ESM. TypeScript files run directly on Node 22 or later via `--experimental-strip-types` |
12
- | Bun | Supported, 58/58 e2e and 8/8 integration tests | No additional configuration required |
13
- | Deno 2 or later | Supported, 58/58 e2e and 8/8 integration tests | Uses `node:` builtins and the npm `msgpackr` package |
11
+ | Node.js 20 or later | Supported, 72/72 e2e and 8/8 integration tests | ESM. TypeScript files run directly on Node 22 or later via `--experimental-strip-types` |
12
+ | Bun | Supported, 72/72 e2e and 8/8 integration tests | No additional configuration required |
13
+ | Deno 2 or later | Supported, 72/72 e2e and 8/8 integration tests | Uses `node:` builtins and the npm `msgpackr` package |
14
14
  | tsx, ts-node, vitest, jest | Supported | These environments execute on Node.js |
15
15
  | Cloudflare Workers | Supported, 11/11 e2e tests inside workerd | Requires the `nodejs_compat` compatibility flag. The runtime is request scoped, so long lived worker loops are not available: consume in batches from Cron Triggers or Durable Object alarms, a pattern covered by the test suite. TLS connections require a publicly trusted certificate |
16
16
  | Browser | Not supported | Raw TCP sockets are unavailable. Use the server HTTP API instead |
@@ -142,6 +142,47 @@ const node = await flow.add({
142
142
  });
143
143
  ```
144
144
 
145
+ ## Simple Mode
146
+
147
+ `Bunqueue` bundles a Queue and a Worker in one object, with routes, onion middleware, in process retry strategies, a circuit breaker, batch accumulation, event triggers, job TTL, priority aging, cooperative cancellation, and dedup or debounce defaults. It is a 1:1 port of the official client's Simple Mode, TCP only (the `embedded` option raises).
148
+
149
+ ```typescript
150
+ import { Bunqueue, type Job } from 'bunqueue-client';
151
+
152
+ const app = new Bunqueue('notifications', {
153
+ connection: { host: 'localhost', port: 6789 },
154
+ concurrency: 10,
155
+ routes: {
156
+ 'send-email': async (job: Job<{ to: string }>) => ({ channel: 'email' }),
157
+ 'send-sms': async (job: Job<{ to: string }>) => ({ channel: 'sms' }),
158
+ },
159
+ retry: { maxAttempts: 5, delay: 1000, strategy: 'jitter' },
160
+ circuitBreaker: { threshold: 5, resetTimeout: 30_000 },
161
+ ttl: { perName: { 'verify-otp': 60_000 } },
162
+ deduplication: { ttl: 5000 },
163
+ });
164
+
165
+ app.use(async (job, next) => {
166
+ const start = Date.now();
167
+ const result = await next();
168
+ console.log(`${job.name}: ${Date.now() - start}ms`);
169
+ return result;
170
+ });
171
+
172
+ app.trigger({
173
+ on: 'send-email',
174
+ create: 'send-sms',
175
+ data: (result, job) => job.data,
176
+ condition: (result) => (result as { channel: string }).channel === 'email',
177
+ });
178
+
179
+ await app.cron('daily-digest', '0 9 * * *', { to: 'all' });
180
+ await app.add('send-email', { to: 'alice@example.com' });
181
+ // later: await app.close();
182
+ ```
183
+
184
+ Use `processor` for a single handler, `routes` to dispatch by job name, or `batch` to accumulate N jobs into one call. Exactly one of the three is required.
185
+
145
186
  ## Scheduling
146
187
 
147
188
  ```typescript
@@ -173,6 +214,7 @@ Authentication uses server side tokens (`AUTH_TOKENS`). Transport security uses
173
214
  | Dead letter queue | `getDlq`, `retryDlq`, `purgeDlq`, DLQ configuration |
174
215
  | Administration | rate limiting, global concurrency, stall configuration, webhooks, stats, metrics, `listQueues`, `getWorkers` |
175
216
  | Worker events | `ready`, `active`, `completed`, `failed`, `progress`, `drained`, `error`, `closed`, with automatic lock heartbeats so that jobs longer than the lock TTL survive |
217
+ | Simple Mode | `Bunqueue`: routes, middleware, in process retry (fixed, exponential, jitter, fibonacci, custom), circuit breaker, batch accumulation, triggers, TTL, priority aging, cancellation via `getSignal`, dedup and debounce defaults, cron shorthands |
176
218
 
177
219
  The following features require the in process Bun runtime and are intentionally out of scope for this client: embedded mode, sandboxed workers, and `QueueEvents`. Use webhooks or the HTTP SSE and WebSocket endpoints for event streaming.
178
220
 
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Bunqueue Simple Mode — priority aging.
3
+ * Port of src/client/bunqueue/aging.ts: automatically boosts the priority of
4
+ * old waiting jobs. Same semantics; the job scan goes over TCP (GetJobs on
5
+ * the waiting and prioritized states) instead of the embedded manager.
6
+ */
7
+ import type { Queue } from '../queue.js';
8
+ import type { PriorityAgingConfig } from './types.js';
9
+ export declare class PriorityAger<T = unknown> {
10
+ private timer;
11
+ private readonly config;
12
+ private readonly queue;
13
+ constructor(config: PriorityAgingConfig, queue: Queue<T>);
14
+ start(): void;
15
+ private tick;
16
+ destroy(): void;
17
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Bunqueue Simple Mode — priority aging.
3
+ * Port of src/client/bunqueue/aging.ts: automatically boosts the priority of
4
+ * old waiting jobs. Same semantics; the job scan goes over TCP (GetJobs on
5
+ * the waiting and prioritized states) instead of the embedded manager.
6
+ */
7
+ export class PriorityAger {
8
+ timer = null;
9
+ config;
10
+ queue;
11
+ constructor(config, queue) {
12
+ this.config = config;
13
+ this.queue = queue;
14
+ }
15
+ start() {
16
+ const interval = this.config.interval ?? 60000;
17
+ this.timer = setInterval(() => {
18
+ void this.tick();
19
+ }, interval);
20
+ this.timer.unref?.();
21
+ }
22
+ async tick() {
23
+ const minAge = this.config.minAge ?? 60000;
24
+ const boost = this.config.boost ?? 1;
25
+ const maxPriority = this.config.maxPriority ?? 100;
26
+ const maxScan = this.config.maxScan ?? 100;
27
+ // Both waiting and prioritized jobs (priority > 0 means "prioritized")
28
+ const [waiting, prioritized] = await Promise.all([
29
+ this.queue.getJobs({ state: 'waiting', start: 0, end: maxScan }),
30
+ this.queue.getJobs({ state: 'prioritized', start: 0, end: maxScan }),
31
+ ]);
32
+ const jobs = [...waiting, ...prioritized];
33
+ const now = Date.now();
34
+ for (const job of jobs) {
35
+ const age = now - job.timestamp;
36
+ if (age >= minAge && job.priority < maxPriority) {
37
+ const newPriority = Math.min(job.priority + boost, maxPriority);
38
+ try {
39
+ await this.queue.changeJobPriority(job.id, { priority: newPriority });
40
+ }
41
+ catch {
42
+ // Best-effort — the job may have been processed meanwhile
43
+ }
44
+ }
45
+ }
46
+ }
47
+ destroy() {
48
+ if (this.timer) {
49
+ clearInterval(this.timer);
50
+ this.timer = null;
51
+ }
52
+ }
53
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Bunqueue Simple Mode — batch processing.
3
+ * 1:1 port of src/client/bunqueue/batch.ts: accumulates jobs and processes
4
+ * them in groups; flushes on size, on timeout, and on destroy (close).
5
+ */
6
+ import type { Processor } from '../worker-types.js';
7
+ import type { BatchConfig } from './types.js';
8
+ export declare class BatchAccumulator<T = unknown, R = unknown> {
9
+ private readonly buffer;
10
+ private timer;
11
+ private readonly config;
12
+ constructor(config: BatchConfig<T, R>);
13
+ /** Build a Processor that buffers jobs into batches. */
14
+ buildProcessor(): Processor<T, R>;
15
+ flush(): void;
16
+ destroy(): void;
17
+ }
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Bunqueue Simple Mode — batch processing.
3
+ * 1:1 port of src/client/bunqueue/batch.ts: accumulates jobs and processes
4
+ * them in groups; flushes on size, on timeout, and on destroy (close).
5
+ */
6
+ export class BatchAccumulator {
7
+ buffer = [];
8
+ timer = null;
9
+ config;
10
+ constructor(config) {
11
+ this.config = config;
12
+ }
13
+ /** Build a Processor that buffers jobs into batches. */
14
+ buildProcessor() {
15
+ return (job) => {
16
+ return new Promise((resolve, reject) => {
17
+ this.buffer.push({ job, resolve, reject });
18
+ if (this.buffer.length >= this.config.size) {
19
+ this.flush();
20
+ }
21
+ else if (!this.timer) {
22
+ const timeout = this.config.timeout ?? 5000;
23
+ this.timer = setTimeout(() => {
24
+ this.flush();
25
+ }, timeout);
26
+ }
27
+ });
28
+ };
29
+ }
30
+ flush() {
31
+ if (this.timer) {
32
+ clearTimeout(this.timer);
33
+ this.timer = null;
34
+ }
35
+ const batch = this.buffer.splice(0);
36
+ if (batch.length === 0)
37
+ return;
38
+ const jobs = batch.map((b) => b.job);
39
+ this.config.processor(jobs).then((results) => {
40
+ for (let i = 0; i < batch.length; i++) {
41
+ batch[i].resolve(results[i] ?? undefined);
42
+ }
43
+ }, (err) => {
44
+ const error = err instanceof Error ? err : new Error(String(err));
45
+ for (const b of batch) {
46
+ b.reject(error);
47
+ }
48
+ });
49
+ }
50
+ destroy() {
51
+ if (this.timer) {
52
+ clearTimeout(this.timer);
53
+ this.timer = null;
54
+ }
55
+ // Flush remaining
56
+ if (this.buffer.length > 0) {
57
+ this.flush();
58
+ }
59
+ }
60
+ }
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Bunqueue Simple Mode — delegation API surface (cron, cancellation, circuit
3
+ * breaker, TTL, DLQ, rate limit, events, control). Merged onto the Bunqueue
4
+ * prototype by bunqueue.ts; split out to keep files small.
5
+ */
6
+ import type { Job } from '../job.js';
7
+ import type { JobOptions } from '../types.js';
8
+ import type { Bunqueue } from './bunqueue.js';
9
+ import type { DlqFilter, DlqStats } from './dlq-rate-limit.js';
10
+ import type { CircuitState, TriggerRule } from './types.js';
11
+ type Raw = Record<string, unknown>;
12
+ type Ctx = Bunqueue<any, any>;
13
+ export declare const bunqueueApi: {
14
+ cron(this: Ctx, id: string, pattern: string, data?: unknown, opts?: {
15
+ timezone?: string;
16
+ jobOpts?: JobOptions;
17
+ }): Promise<Raw | null>;
18
+ every(this: Ctx, id: string, intervalMs: number, data?: unknown, opts?: {
19
+ jobOpts?: JobOptions;
20
+ }): Promise<Raw | null>;
21
+ removeCron(this: Ctx, id: string): Promise<void>;
22
+ listCrons(this: Ctx): Promise<Raw[]>;
23
+ cancel(this: Ctx, jobId: string, gracePeriodMs?: number): void;
24
+ isCancelled(this: Ctx, jobId: string): boolean;
25
+ getSignal(this: Ctx, jobId: string): AbortSignal | null;
26
+ getCircuitState(this: Ctx): CircuitState;
27
+ resetCircuit(this: Ctx): void;
28
+ trigger(this: Ctx, rule: TriggerRule): Ctx;
29
+ setDefaultTtl(this: Ctx, ttlMs: number): void;
30
+ setNameTtl(this: Ctx, name: string, ttlMs: number): void;
31
+ setDlqConfig(this: Ctx, config: Raw): Promise<void>;
32
+ getDlqConfig(this: Ctx): Promise<Raw>;
33
+ getDlq(this: Ctx, filter?: DlqFilter): Promise<Raw[]>;
34
+ getDlqStats(this: Ctx): Promise<DlqStats>;
35
+ retryDlq(this: Ctx, id?: string): Promise<number>;
36
+ purgeDlq(this: Ctx): Promise<number>;
37
+ setGlobalRateLimit(this: Ctx, max: number, duration?: number): Promise<void>;
38
+ removeGlobalRateLimit(this: Ctx): Promise<void>;
39
+ on(this: Ctx, event: string, listener: (...args: never[]) => void): Ctx;
40
+ once(this: Ctx, event: string, listener: (...args: never[]) => void): Ctx;
41
+ off(this: Ctx, event: string, listener: (...args: never[]) => void): Ctx;
42
+ pause(this: Ctx): void;
43
+ resume(this: Ctx): void;
44
+ close(this: Ctx, force?: boolean): Promise<void>;
45
+ isRunning(this: Ctx): boolean;
46
+ isPaused(this: Ctx): boolean;
47
+ isClosed(this: Ctx): boolean;
48
+ };
49
+ /** Declaration-merged API installed on the Bunqueue prototype. */
50
+ export interface BunqueueApi<T = unknown, R = unknown> {
51
+ cron(id: string, pattern: string, data?: T, opts?: {
52
+ timezone?: string;
53
+ jobOpts?: JobOptions;
54
+ }): Promise<Raw | null>;
55
+ every(id: string, intervalMs: number, data?: T, opts?: {
56
+ jobOpts?: JobOptions;
57
+ }): Promise<Raw | null>;
58
+ removeCron(id: string): Promise<void>;
59
+ listCrons(): Promise<Raw[]>;
60
+ cancel(jobId: string, gracePeriodMs?: number): void;
61
+ isCancelled(jobId: string): boolean;
62
+ getSignal(jobId: string): AbortSignal | null;
63
+ getCircuitState(): CircuitState;
64
+ resetCircuit(): void;
65
+ trigger(rule: TriggerRule<T>): Bunqueue<T, R>;
66
+ setDefaultTtl(ttlMs: number): void;
67
+ setNameTtl(name: string, ttlMs: number): void;
68
+ setDlqConfig(config: Raw): Promise<void>;
69
+ getDlqConfig(): Promise<Raw>;
70
+ getDlq(filter?: DlqFilter): Promise<Raw[]>;
71
+ getDlqStats(): Promise<DlqStats>;
72
+ retryDlq(id?: string): Promise<number>;
73
+ purgeDlq(): Promise<number>;
74
+ setGlobalRateLimit(max: number, duration?: number): Promise<void>;
75
+ removeGlobalRateLimit(): Promise<void>;
76
+ on(event: string, listener: (...args: never[]) => void): Bunqueue<T, R>;
77
+ once(event: string, listener: (...args: never[]) => void): Bunqueue<T, R>;
78
+ off(event: string, listener: (...args: never[]) => void): Bunqueue<T, R>;
79
+ pause(): void;
80
+ resume(): void;
81
+ close(force?: boolean): Promise<void>;
82
+ isRunning(): boolean;
83
+ isPaused(): boolean;
84
+ isClosed(): boolean;
85
+ getJob(id: string): Promise<Job<T> | null>;
86
+ }
87
+ export {};
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Bunqueue Simple Mode — delegation API surface (cron, cancellation, circuit
3
+ * breaker, TTL, DLQ, rate limit, events, control). Merged onto the Bunqueue
4
+ * prototype by bunqueue.ts; split out to keep files small.
5
+ */
6
+ export const bunqueueApi = {
7
+ // --------------------------------------------------------------------- cron
8
+ async cron(id, pattern, data, opts) {
9
+ await this.queue.upsertJobScheduler(id, { pattern, tz: opts?.timezone }, { name: id, data, opts: opts?.jobOpts });
10
+ return this.queue.getJobScheduler(id);
11
+ },
12
+ async every(id, intervalMs, data, opts) {
13
+ await this.queue.upsertJobScheduler(id, { every: intervalMs }, { name: id, data, opts: opts?.jobOpts });
14
+ return this.queue.getJobScheduler(id);
15
+ },
16
+ removeCron(id) {
17
+ return this.queue.removeJobScheduler(id);
18
+ },
19
+ listCrons() {
20
+ return this.queue.getJobSchedulers();
21
+ },
22
+ // ------------------------------------------------------------- cancellation
23
+ cancel(jobId, gracePeriodMs = 0) {
24
+ this.cancellation.cancel(jobId, gracePeriodMs);
25
+ },
26
+ isCancelled(jobId) {
27
+ return this.cancellation.isCancelled(jobId);
28
+ },
29
+ getSignal(jobId) {
30
+ return this.cancellation.getSignal(jobId);
31
+ },
32
+ // ---------------------------------------------------------- circuit breaker
33
+ getCircuitState() {
34
+ return this.cb?.currentState ?? 'closed';
35
+ },
36
+ resetCircuit() {
37
+ this.cb?.reset();
38
+ },
39
+ // ----------------------------------------------------------------- triggers
40
+ trigger(rule) {
41
+ this.triggerMgr.add(rule);
42
+ return this;
43
+ },
44
+ // ---------------------------------------------------------------------- ttl
45
+ setDefaultTtl(ttlMs) {
46
+ this.ttlChecker?.setDefaultTtl(ttlMs);
47
+ },
48
+ setNameTtl(name, ttlMs) {
49
+ this.ttlChecker?.setNameTtl(name, ttlMs);
50
+ },
51
+ // ---------------------------------------------------------------------- dlq
52
+ setDlqConfig(config) {
53
+ return this.dlqrl.setDlqConfig(config);
54
+ },
55
+ getDlqConfig() {
56
+ return this.dlqrl.getDlqConfig();
57
+ },
58
+ getDlq(filter) {
59
+ return this.dlqrl.getDlq(filter);
60
+ },
61
+ getDlqStats() {
62
+ return this.dlqrl.getDlqStats();
63
+ },
64
+ retryDlq(id) {
65
+ return this.dlqrl.retryDlq(id);
66
+ },
67
+ purgeDlq() {
68
+ return this.dlqrl.purgeDlq();
69
+ },
70
+ // ------------------------------------------------------------ rate limiting
71
+ setGlobalRateLimit(max, duration) {
72
+ return this.dlqrl.setGlobalRateLimit(max, duration);
73
+ },
74
+ removeGlobalRateLimit() {
75
+ return this.dlqrl.removeGlobalRateLimit();
76
+ },
77
+ // ------------------------------------------------------------------- events
78
+ on(event, listener) {
79
+ this.worker.on(event, listener);
80
+ return this;
81
+ },
82
+ once(event, listener) {
83
+ this.worker.once(event, listener);
84
+ return this;
85
+ },
86
+ off(event, listener) {
87
+ this.worker.off(event, listener);
88
+ return this;
89
+ },
90
+ // ------------------------------------------------------------------ control
91
+ pause() {
92
+ void this.queue.pause().catch(() => { });
93
+ this.worker.pause();
94
+ },
95
+ resume() {
96
+ void this.queue.resume().catch(() => { });
97
+ this.worker.resume();
98
+ },
99
+ async close(force = false) {
100
+ this.ager?.destroy();
101
+ this.cb?.destroy();
102
+ this.batchAcc?.destroy();
103
+ this.cancellation.destroyAll();
104
+ await this.worker.close(force);
105
+ this.queue.close();
106
+ },
107
+ isRunning() {
108
+ return this.worker.isRunning();
109
+ },
110
+ isPaused() {
111
+ return this.worker.isPaused();
112
+ },
113
+ isClosed() {
114
+ return this.worker.isClosed();
115
+ },
116
+ };
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Bunqueue — simplified all-in-one Queue + Worker (Simple Mode).
3
+ * 1:1 port of src/client/bunqueue.ts from the official client, TCP mode only
4
+ * (the embedded mode requires the in-process Bun runtime). The delegation
5
+ * API (cron, DLQ, events, control, …) lives in bunqueue-api.ts and is merged
6
+ * onto the prototype below.
7
+ */
8
+ import type { Job } from '../job.js';
9
+ import { Queue } from '../queue.js';
10
+ import type { JobOptions } from '../types.js';
11
+ import { Worker } from '../worker.js';
12
+ import { PriorityAger } from './aging.js';
13
+ import { BatchAccumulator } from './batch.js';
14
+ import { type BunqueueApi } from './bunqueue-api.js';
15
+ import { CancellationManager } from './cancellation.js';
16
+ import { WorkerCircuitBreaker } from './circuit-breaker.js';
17
+ import { DlqRateLimitManager } from './dlq-rate-limit.js';
18
+ import { TriggerManager } from './triggers.js';
19
+ import { TtlChecker } from './ttl.js';
20
+ import type { BunqueueMiddleware, BunqueueOptions } from './types.js';
21
+ export declare class Bunqueue<T = unknown, R = unknown> {
22
+ readonly name: string;
23
+ readonly queue: Queue<T>;
24
+ readonly worker: Worker<T, R>;
25
+ /** @internal */ readonly cancellation: CancellationManager;
26
+ /** @internal */ readonly cb: WorkerCircuitBreaker | null;
27
+ /** @internal */ readonly triggerMgr: TriggerManager<T, R>;
28
+ /** @internal */ readonly ager: PriorityAger<T> | null;
29
+ /** @internal */ readonly ttlChecker: TtlChecker | null;
30
+ /** @internal */ readonly batchAcc: BatchAccumulator<T, R> | null;
31
+ /** @internal */ readonly dlqrl: DlqRateLimitManager<T>;
32
+ private readonly middlewares;
33
+ private readonly baseProcessor;
34
+ private readonly retryConfig;
35
+ private readonly merger;
36
+ private readonly rateGate;
37
+ private readonly defaultJobOptions;
38
+ constructor(name: string, opts: BunqueueOptions<T, R>);
39
+ private buildRouteProcessor;
40
+ private buildDefaultJobOptions;
41
+ private processJob;
42
+ private runMiddlewareChain;
43
+ use(middleware: BunqueueMiddleware<T, R>): this;
44
+ add(name: string, data: T, opts?: JobOptions): Promise<Job<T>>;
45
+ addBulk(jobs: Array<{
46
+ name: string;
47
+ data: T;
48
+ opts?: JobOptions;
49
+ }>): Promise<Job<T>[]>;
50
+ getJob(id: string): Promise<Job<T> | null>;
51
+ getJobCounts(): Promise<import("../types.js").JobCounts>;
52
+ getJobCountsAsync(): Promise<import("../types.js").JobCounts>;
53
+ count(): Promise<number>;
54
+ countAsync(): Promise<number>;
55
+ }
56
+ export interface Bunqueue<T = unknown, R = unknown> extends BunqueueApi<T, R> {
57
+ }
@@ -0,0 +1,182 @@
1
+ /**
2
+ * Bunqueue — simplified all-in-one Queue + Worker (Simple Mode).
3
+ * 1:1 port of src/client/bunqueue.ts from the official client, TCP mode only
4
+ * (the embedded mode requires the in-process Bun runtime). The delegation
5
+ * API (cron, DLQ, events, control, …) lives in bunqueue-api.ts and is merged
6
+ * onto the prototype below.
7
+ */
8
+ import { Queue } from '../queue.js';
9
+ import { Worker } from '../worker.js';
10
+ import { PriorityAger } from './aging.js';
11
+ import { BatchAccumulator } from './batch.js';
12
+ import { bunqueueApi } from './bunqueue-api.js';
13
+ import { CancellationManager } from './cancellation.js';
14
+ import { WorkerCircuitBreaker } from './circuit-breaker.js';
15
+ import { DedupDebounceMerger } from './dedup-debounce.js';
16
+ import { DlqRateLimitManager } from './dlq-rate-limit.js';
17
+ import { RateGate } from './rate-gate.js';
18
+ import { executeWithRetry } from './retry.js';
19
+ import { TriggerManager } from './triggers.js';
20
+ import { TtlChecker } from './ttl.js';
21
+ // biome-ignore lint/suspicious/noUnsafeDeclarationMerging: prototype-mixin composition — Object.assign below installs exactly the methods BunqueueApi declares
22
+ export class Bunqueue {
23
+ name;
24
+ queue;
25
+ worker;
26
+ /** @internal */ cancellation = new CancellationManager();
27
+ /** @internal */ cb;
28
+ /** @internal */ triggerMgr;
29
+ /** @internal */ ager;
30
+ /** @internal */ ttlChecker;
31
+ /** @internal */ batchAcc;
32
+ /** @internal */ dlqrl;
33
+ middlewares = [];
34
+ baseProcessor;
35
+ retryConfig;
36
+ merger;
37
+ rateGate;
38
+ defaultJobOptions;
39
+ constructor(name, opts) {
40
+ if (opts.embedded || opts.dataPath) {
41
+ throw new Error('Bunqueue (bunqueue-client) is TCP-only: embedded mode requires the Bun runtime — use the official "bunqueue" package on Bun instead');
42
+ }
43
+ const modes = [opts.processor, opts.routes, opts.batch].filter(Boolean).length;
44
+ if (modes === 0)
45
+ throw new Error('Bunqueue requires "processor", "routes", or "batch"');
46
+ if (modes > 1)
47
+ throw new Error('Bunqueue: use only one of "processor", "routes", or "batch"');
48
+ this.name = (opts.prefixKey ?? '') + name;
49
+ this.retryConfig = opts.retry ?? null;
50
+ this.ttlChecker = opts.ttl ? new TtlChecker(opts.ttl) : null;
51
+ this.merger = new DedupDebounceMerger(opts.deduplication ?? null, opts.debounce ?? null);
52
+ const limiter = opts.rateLimit ?? opts.limiter;
53
+ this.rateGate = limiter ? new RateGate(limiter) : null;
54
+ this.defaultJobOptions = this.buildDefaultJobOptions(opts);
55
+ // Build base processor
56
+ if (opts.batch) {
57
+ this.batchAcc = new BatchAccumulator(opts.batch);
58
+ this.baseProcessor = this.batchAcc.buildProcessor();
59
+ }
60
+ else {
61
+ this.batchAcc = null;
62
+ this.baseProcessor = opts.routes
63
+ ? this.buildRouteProcessor(opts.routes)
64
+ : opts.processor;
65
+ }
66
+ const wrappedProcessor = (job) => this.processJob(job);
67
+ const conn = opts.connection ?? {};
68
+ this.queue = new Queue(this.name, conn);
69
+ this.worker = new Worker(this.name, wrappedProcessor, {
70
+ ...conn,
71
+ concurrency: opts.concurrency,
72
+ autorun: opts.autorun,
73
+ heartbeatIntervalS: opts.heartbeatInterval !== undefined ? opts.heartbeatInterval / 1000 : undefined,
74
+ batchSize: opts.batchSize,
75
+ pollTimeoutMs: opts.pollTimeout,
76
+ });
77
+ // DLQ & rate limit manager
78
+ this.dlqrl = new DlqRateLimitManager(this.queue);
79
+ if (opts.dlq)
80
+ void this.dlqrl.setDlqConfig(opts.dlq);
81
+ // Subsystems
82
+ this.cb = opts.circuitBreaker
83
+ ? new WorkerCircuitBreaker(opts.circuitBreaker, this.worker)
84
+ : null;
85
+ this.triggerMgr = new TriggerManager(this.queue, this.worker);
86
+ this.ager = opts.priorityAging ? new PriorityAger(opts.priorityAging, this.queue) : null;
87
+ this.ager?.start();
88
+ }
89
+ buildRouteProcessor(routes) {
90
+ const routeMap = routes;
91
+ return (job) => {
92
+ const handler = routeMap[job.name ?? ''];
93
+ if (!handler)
94
+ throw new Error(`No route for job "${job.name}" in queue "${this.name}"`);
95
+ return handler(job);
96
+ };
97
+ }
98
+ buildDefaultJobOptions(opts) {
99
+ const base = { ...opts.defaultJobOptions };
100
+ if (opts.removeOnComplete !== undefined)
101
+ base.removeOnComplete ??= opts.removeOnComplete;
102
+ if (opts.removeOnFail !== undefined)
103
+ base.removeOnFail ??= opts.removeOnFail;
104
+ return Object.keys(base).length > 0 ? base : undefined;
105
+ }
106
+ // ------------------------------------------------- core processing pipeline
107
+ async processJob(job) {
108
+ if (this.rateGate)
109
+ await this.rateGate.acquire(this.rateGate.groupFor(job.data));
110
+ // Circuit breaker check
111
+ if (this.cb?.isOpen()) {
112
+ throw new Error('Circuit breaker is open');
113
+ }
114
+ // TTL check
115
+ if (this.ttlChecker?.isExpired(job.name ?? '', job.timestamp)) {
116
+ throw new Error(`Job expired (age: ${Date.now() - job.timestamp}ms)`);
117
+ }
118
+ // Register cancellation
119
+ const ac = this.cancellation.register(job.id);
120
+ const runChain = () => this.runMiddlewareChain(job, ac);
121
+ const execute = this.retryConfig ? executeWithRetry(runChain, this.retryConfig) : runChain();
122
+ return execute.then((result) => {
123
+ this.cb?.onSuccess();
124
+ this.cancellation.unregister(job.id);
125
+ return result;
126
+ }, (err) => {
127
+ this.cb?.onFailure();
128
+ this.cancellation.unregister(job.id);
129
+ throw err;
130
+ });
131
+ }
132
+ runMiddlewareChain(job, ac) {
133
+ if (this.middlewares.length === 0) {
134
+ const result = this.baseProcessor(job);
135
+ return result instanceof Promise ? result : Promise.resolve(result);
136
+ }
137
+ let index = 0;
138
+ const mws = this.middlewares;
139
+ const base = this.baseProcessor;
140
+ const next = () => {
141
+ if (ac.signal.aborted)
142
+ return Promise.reject(new Error('Job cancelled'));
143
+ if (index < mws.length)
144
+ return mws[index++](job, next);
145
+ const result = base(job);
146
+ return result instanceof Promise ? result : Promise.resolve(result);
147
+ };
148
+ return next();
149
+ }
150
+ // --------------------------------------------------------------- middleware
151
+ use(middleware) {
152
+ this.middlewares.push(middleware);
153
+ return this;
154
+ }
155
+ // --------------------------------------------------------- queue operations
156
+ add(name, data, opts) {
157
+ const merged = { ...this.defaultJobOptions, ...opts };
158
+ return this.queue.add(name, data, this.merger.merge(name, merged, data));
159
+ }
160
+ addBulk(jobs) {
161
+ return this.queue.addBulk(jobs.map((j) => ({
162
+ ...j,
163
+ opts: this.merger.merge(j.name, { ...this.defaultJobOptions, ...j.opts }, j.data),
164
+ })));
165
+ }
166
+ getJob(id) {
167
+ return this.queue.getJob(id);
168
+ }
169
+ getJobCounts() {
170
+ return this.queue.getJobCounts();
171
+ }
172
+ getJobCountsAsync() {
173
+ return this.queue.getJobCounts();
174
+ }
175
+ count() {
176
+ return this.queue.count();
177
+ }
178
+ countAsync() {
179
+ return this.queue.count();
180
+ }
181
+ }
182
+ Object.assign(Bunqueue.prototype, bunqueueApi);
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Bunqueue Simple Mode — graceful job cancellation.
3
+ * 1:1 port of src/client/bunqueue/cancellation.ts: AbortController-based
4
+ * cancellation with optional grace period.
5
+ */
6
+ export declare class CancellationManager {
7
+ private readonly controllers;
8
+ /** Register a new AbortController for a job. */
9
+ register(jobId: string): AbortController;
10
+ /** Remove a job's controller (on completion). */
11
+ unregister(jobId: string): void;
12
+ /** Cancel a job with optional grace period. */
13
+ cancel(jobId: string, gracePeriodMs?: number): void;
14
+ /** Check if a job is cancelled. */
15
+ isCancelled(jobId: string): boolean;
16
+ /** Get the AbortSignal for a job. */
17
+ getSignal(jobId: string): AbortSignal | null;
18
+ /** Cancel all and clear. */
19
+ destroyAll(): void;
20
+ }