@nwire/bullmq 0.10.0 → 0.11.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.
@@ -0,0 +1,29 @@
1
+ /**
2
+ * `bullmqOutboundAdapter` — outbound endpoint adapter that drains the App's
3
+ * sink into BullMQ queues backed by Redis.
4
+ *
5
+ * At endpoint boot the adapter calls `installSinkStage` with a terminal
6
+ * stage of kind `"bullmq"`. Every time a handler calls `ctx.publish` for
7
+ * a public event, the sink chain reaches this terminal and pushes a
8
+ * BullMQ job onto a per-event-name queue. Downstream workers (the same
9
+ * `BullMQQueueTransport.subscribe`, or any independent BullMQ consumer)
10
+ * receive the event.
11
+ *
12
+ * await endpoint("orders", { exitOnShutdown: false })
13
+ * .use(bullmqOutboundAdapter({ connection: { host, port } }))
14
+ * .mount(app)
15
+ * .run();
16
+ */
17
+ import type { ConnectionOptions } from "bullmq";
18
+ import type { Adapter } from "@nwire/endpoint";
19
+ export interface BullmqOutboundOptions {
20
+ readonly connection: ConnectionOptions;
21
+ /** Prefix applied to every queue. Default: `"nwire"`. */
22
+ readonly prefix?: string;
23
+ /**
24
+ * Map an event name to a BullMQ queue name. Default: pass-through —
25
+ * each event lands on a queue with the same name.
26
+ */
27
+ readonly queueFor?: (eventName: string) => string;
28
+ }
29
+ export declare function bullmqOutboundAdapter(opts: BullmqOutboundOptions): Adapter;
@@ -0,0 +1,67 @@
1
+ /**
2
+ * `bullmqOutboundAdapter` — outbound endpoint adapter that drains the App's
3
+ * sink into BullMQ queues backed by Redis.
4
+ *
5
+ * At endpoint boot the adapter calls `installSinkStage` with a terminal
6
+ * stage of kind `"bullmq"`. Every time a handler calls `ctx.publish` for
7
+ * a public event, the sink chain reaches this terminal and pushes a
8
+ * BullMQ job onto a per-event-name queue. Downstream workers (the same
9
+ * `BullMQQueueTransport.subscribe`, or any independent BullMQ consumer)
10
+ * receive the event.
11
+ *
12
+ * await endpoint("orders", { exitOnShutdown: false })
13
+ * .use(bullmqOutboundAdapter({ connection: { host, port } }))
14
+ * .mount(app)
15
+ * .run();
16
+ */
17
+ import { Queue } from "bullmq";
18
+ export function bullmqOutboundAdapter(opts) {
19
+ const prefix = opts.prefix ?? "nwire";
20
+ const queueFor = opts.queueFor ?? ((eventName) => eventName);
21
+ const queues = new Map();
22
+ let stopped = false;
23
+ function getQueue(name) {
24
+ let q = queues.get(name);
25
+ if (!q) {
26
+ q = new Queue(name, { connection: opts.connection, prefix });
27
+ queues.set(name, q);
28
+ }
29
+ return q;
30
+ }
31
+ return {
32
+ $kind: "adapter",
33
+ kind: "bullmq",
34
+ direction: "outbound",
35
+ async boot(ctx) {
36
+ if (!ctx.installSinkStage) {
37
+ ctx.logger.warn("bullmqOutboundAdapter: endpoint did not provide installSinkStage; no outbound delivery will run.");
38
+ return;
39
+ }
40
+ const stage = {
41
+ name: "bullmq.terminal",
42
+ position: "terminal",
43
+ kind: "bullmq",
44
+ async run({ event, payload, envelope }) {
45
+ if (stopped)
46
+ return;
47
+ const queueName = queueFor(event.name);
48
+ const q = getQueue(queueName);
49
+ await q.add(queueName, { eventName: event.name, payload, envelope }, { jobId: envelope.messageId });
50
+ },
51
+ };
52
+ ctx.installSinkStage(stage);
53
+ },
54
+ async shutdown() {
55
+ stopped = true;
56
+ for (const q of queues.values()) {
57
+ try {
58
+ await q.close();
59
+ }
60
+ catch {
61
+ // best-effort
62
+ }
63
+ }
64
+ queues.clear();
65
+ },
66
+ };
67
+ }
@@ -0,0 +1,2 @@
1
+ export { BullMQQueueTransport, type BullMQQueueOptions } from "./queue-bullmq.js";
2
+ export { bullmqOutboundAdapter, type BullmqOutboundOptions } from "./bullmq-outbound.js";
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { BullMQQueueTransport } from "./queue-bullmq.js";
2
+ export { bullmqOutboundAdapter } from "./bullmq-outbound.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nwire/bullmq",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "Nwire — BullMQ-backed QueueTransport for production. Redis-backed durable queues + delayed jobs (timer scheduler) + failed-jobs board.",
5
5
  "keywords": [
6
6
  "adapter",
@@ -16,12 +16,12 @@
16
16
  "LICENSE"
17
17
  ],
18
18
  "type": "module",
19
- "main": "./dist/queue-bullmq.js",
20
- "types": "./dist/queue-bullmq.d.ts",
19
+ "main": "./dist/index.js",
20
+ "types": "./dist/index.d.ts",
21
21
  "exports": {
22
22
  ".": {
23
- "import": "./dist/queue-bullmq.js",
24
- "types": "./dist/queue-bullmq.d.ts"
23
+ "import": "./dist/index.js",
24
+ "types": "./dist/index.d.ts"
25
25
  }
26
26
  },
27
27
  "publishConfig": {
@@ -29,12 +29,17 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "bullmq": "^5.67.3",
32
- "@nwire/queue": "0.10.0"
32
+ "@nwire/endpoint": "0.11.0",
33
+ "@nwire/runtime": "0.11.0",
34
+ "@nwire/queue": "0.11.0"
33
35
  },
34
36
  "devDependencies": {
35
37
  "@types/node": "^22.19.9",
36
38
  "typescript": "^5.9.3",
37
- "@nwire/test-kit": "0.10.0"
39
+ "zod": "^4.1.12",
40
+ "@nwire/test-kit": "0.11.0",
41
+ "@nwire/app": "0.11.0",
42
+ "@nwire/messages": "0.11.0"
38
43
  },
39
44
  "scripts": {
40
45
  "build": "tsc && node ../../scripts/fix-dist-extensions.mjs dist",