@eventferry/core 3.2.1 → 3.3.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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,290 @@
1
+ # @eventferry/core
2
+
3
+ ## 3.3.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 3c33f71: **chore: ship `CHANGELOG.md` inside the npm tarball**
8
+
9
+ Previously, each package's `files` allowlist contained only `"dist"` (and `"sql"` for `@eventferry/postgres`), so the auto-generated `CHANGELOG.md` was never published. Users browsing the package on npmjs.com or unpacking the tarball couldn't see release notes — they had to navigate to the GitHub repo.
10
+
11
+ This release adds `"CHANGELOG.md"` to the `files` array of every publishable package. Starting with this version, the per-version release notes are accessible:
12
+
13
+ - Directly in `node_modules/@eventferry/<pkg>/CHANGELOG.md` after `npm install`
14
+ - In the file listing on npmjs.com (under the "Code" / "Files" tab, depending on the npm UI)
15
+ - Inside the tarball downloaded from `https://registry.npmjs.org/...`
16
+
17
+ No code or API surface changes.
18
+
19
+ ## 3.3.0
20
+
21
+ ### Minor Changes
22
+
23
+ - cdc20cf: **feat: DLQ enrichment + backpressure runtime + quota multiplier — Tier 1 of the reliability gap closed**
24
+
25
+ ### DLQ enrichment
26
+
27
+ Records routed to the dead-letter queue now carry the full context an operator needs to triage:
28
+
29
+ | Header | Set by | Note |
30
+ | --------------------------- | --------- | ------------------------------------------------------------------------------------------------ |
31
+ | `original-topic` | relay | already existed |
32
+ | `dlq-reason` | publisher | already existed (`error.message`) |
33
+ | `dlq-failed-at` | publisher | already existed (ISO timestamp) |
34
+ | `dlq-error-class` | publisher | **new** — `error.name` / constructor name |
35
+ | `dlq-attempts` | relay | **new** — string-encoded `attempts` count |
36
+ | `dlq-original-aggregate-id` | relay | **new** — for joining with business state |
37
+ | `dlq-original-message-id` | relay | **new** — for dedup / idempotency lookups |
38
+ | `dlq-error-stack` | relay | **new** — opt-in via `DlqConfig.includeStackTraces`, truncated to `maxStackBytes` (default 4 KB) |
39
+
40
+ ```ts
41
+ new Relay({
42
+ store,
43
+ publisher,
44
+ dlq: { topic: "orders.dlq", includeStackTraces: true, maxStackBytes: 4096 },
45
+ });
46
+ ```
47
+
48
+ ### Backpressure runtime behavior
49
+
50
+ When the driver classifies a failure as `errorKind: "backpressure"` (client-side producer queue full), the relay no longer treats it like a regular retriable failure. Instead:
51
+
52
+ - The record is re-queued via the new `OutboxStore.requeue(id, retryAt)` method,
53
+ - `attempts` is **not incremented** — the buffer being full is a "slow down" signal, not the record's fault,
54
+ - The retry is scheduled `RetryConfig.backpressureDelayMs` ms ahead (default 1000 ms).
55
+
56
+ Stores that don't implement `requeue` fall back to `markFailed` (with attempts++); both `@eventferry/postgres` and `@eventferry/mysql` ship a real implementation.
57
+
58
+ ### Quota multiplier
59
+
60
+ When the driver classifies a failure as `errorKind: "quota"` (broker `THROTTLING_QUOTA_EXCEEDED`), the scheduled retry delay is multiplied by `RetryConfig.quotaMultiplier` (default 5) so the producer gives the broker breathing room. Quota failures DO count as attempts — after the budget is exhausted the record routes to DLQ + `dead`.
61
+
62
+ ### New / changed types
63
+
64
+ - `RetryConfig` gains `backpressureDelayMs?` and `quotaMultiplier?`.
65
+ - `DlqConfig` gains `includeStackTraces?` and `maxStackBytes?`.
66
+ - `OutboxStore.requeue?(recordId, retryAt)` is a new **optional** method. Stores without it fall through to `markFailed`.
67
+
68
+ ### Backward compatibility
69
+
70
+ Pure-additive everywhere. Default behavior matches the prior release:
71
+
72
+ - A `RetryConfig` without `backpressureDelayMs` uses 1000 ms (sensible default).
73
+ - A `DlqConfig` without `includeStackTraces` keeps DLQ messages small (default off).
74
+ - An `OutboxStore` without `requeue` falls back to `markFailed` — same as before, just with a documented quirk.
75
+
76
+ This closes the last three Tier 1 items in `docs/kafka-gap-analysis/reliability.md`. Phase A reliability surface is now ~100% complete.
77
+
78
+ ## 3.2.1
79
+
80
+ ### Patch Changes
81
+
82
+ - 9beb3e2: **chore: migrate to independent versioning (Astro pattern)**
83
+
84
+ Fixes the major-version inflation that produced four consecutive surprise majors (`1.0.4 → 2.0.0`, `2.0.0 → 3.0.0`, `3.0.0 → 4.0.0 corrected to 3.1.0`, `3.1.0 → 4.0.0 corrected to 3.2.0`) from changesets whose frontmatter only asked for `minor`.
85
+
86
+ **Root cause** (cited in [changesets/changesets#1759](https://github.com/changesets/changesets/issues/1759) and [docs/decisions.md](https://github.com/changesets/changesets/blob/main/docs/decisions.md)): the adapters listed `@eventferry/core` as a `peerDependency` with `workspace:*`. Changesets' documented rule is that an internal bump of a peer forces a major bump on the dependent — and the `fixed: [["@eventferry/*"]]` group reconciler then propagated that major across every package in the group.
87
+
88
+ **Fix** (exactly the [Astro config](https://github.com/withastro/astro/blob/main/.changeset/config.json)):
89
+
90
+ 1. `.changeset/config.json` — drop `fixed`, set `linked: []`, enable
91
+ `___experimentalUnsafeOptions_WILL_CHANGE_IN_PATCH.onlyUpdatePeerDependentsWhenOutOfRange: true`.
92
+ 2. Move `@eventferry/core` from `peerDependencies` to `dependencies` in
93
+ `@eventferry/postgres`, `@eventferry/mysql`, `@eventferry/kafka`, and
94
+ `@eventferry/schema-registry`. External user-facing peers (`pg`,
95
+ `mysql2`, `kafkajs`, `@confluentinc/kafka-javascript`,
96
+ `@kafkajs/confluent-schema-registry`) stay unchanged.
97
+
98
+ **Effect on releases.** Packages now evolve at independent semver tempos: a `core: minor` changeset produces `core@3.3.0` alongside `postgres@3.2.1` (patch, from "Updated dependencies"). No more major surprises. No more manual force-push corrections.
99
+
100
+ **Effect on consumers.** Pure-additive at the install boundary: `npm i @eventferry/kafka` now resolves `@eventferry/core` automatically (it's a regular dep). Previously consumers had to install it themselves as a peer; the typical flow already did this. No source-code changes required.
101
+
102
+ ## 3.2.0
103
+
104
+ ## 3.1.0
105
+
106
+ ### Minor Changes
107
+
108
+ - da39b08: **feat: producer tuning passthrough + per-message partition override + kafkajs partitioner choice**
109
+
110
+ ### Producer tuning
111
+
112
+ `KafkaPublisher` now accepts the full set of producer tuning knobs every serious Kafka deployment eventually needs:
113
+
114
+ ```ts
115
+ new KafkaPublisher({
116
+ driver: "confluent",
117
+ brokers,
118
+ lingerMs: 25, // ⚠ confluent only
119
+ batchSize: 131_072, // ⚠ confluent only
120
+ maxInFlightRequests: 5,
121
+ requestTimeoutMs: 30_000,
122
+ deliveryTimeoutMs: 120_000, // ⚠ confluent only
123
+ maxRequestSize: 2_000_000, // ⚠ confluent only
124
+ transactionTimeoutMs: 90_000,
125
+ });
126
+ ```
127
+
128
+ **Driver asymmetry:** `kafkajs` has no producer-level config for `lingerMs`, `batchSize`, `deliveryTimeoutMs`, or `maxRequestSize` — its batching is sticky-partitioner + hardcoded internals. The typed API stays uniform; on the kafkajs driver, those four knobs log a **one-time** warning (deduped process-wide) and are otherwise ignored. For fine-grained tuning, switch to the confluent driver.
129
+
130
+ ### Per-message partition override
131
+
132
+ `PublishableMessage` gains an optional `partition?: number` field. When set, the publisher routes that record to the exact partition, bypassing the configured partitioner. Use cases: compacted topics with application-managed sharding, tenant-affinity routing, geo-pinning. Both drivers honor it.
133
+
134
+ ### kafkajs partitioner choice
135
+
136
+ Silences the noisy `KafkaJSPartitionerNotSpecified` warning kafkajs v2 emits on every producer instance, by letting you pick a partitioner explicitly:
137
+
138
+ ```ts
139
+ new KafkaPublisher({
140
+ driver: "kafkajs",
141
+ brokers,
142
+ partitioner: "java-compatible", // (default) | "legacy" | "default"
143
+ });
144
+ ```
145
+
146
+ - `"java-compatible"` is the new greenfield default (matches the Java client's murmur2).
147
+ - `"legacy"` preserves pre-v2 hash continuity for existing topics.
148
+ - `"default"` follows kafkajs's current default.
149
+
150
+ ### Backward compatibility
151
+
152
+ Pure-additive. Existing call sites continue to work unchanged; the partitioner-choice default (`"java-compatible"`) is what kafkajs v2's migration guide recommends for new producers.
153
+
154
+ ## 3.0.0
155
+
156
+ ### Minor Changes
157
+
158
+ - f0c7483: **feat: error classification for smarter retry, DLQ, and pause behavior**
159
+
160
+ Publisher implementations can now tag each failed `PublishResult` with an `errorKind` so the relay knows whether the error is worth retrying.
161
+
162
+ **New in `@eventferry/core`:**
163
+
164
+ - `PublishErrorKind = "retriable" | "fatal" | "poison" | "backpressure" | "quota"` — opt-in classification surface on `PublishResult.errorKind`.
165
+ - The `Relay` now reads `errorKind`:
166
+ - `"fatal"` (auth denied, fenced epoch, transactional id rejected) and `"poison"` (oversized record, corrupt payload, schema rejected) **short-circuit retries** straight to the DLQ + `dead` status. No more burning the retry budget on errors that cannot succeed.
167
+ - `"retriable"`, `"backpressure"`, `"quota"`, and absent (`undefined`) continue to use the existing backoff schedule, preserving backward compatibility. Smarter `backpressure` / `quota` handling (pause polling, longer backoff) is planned for a follow-up release.
168
+
169
+ **New in `@eventferry/kafka`:**
170
+
171
+ - `classifyKafkajsError(err): PublishErrorKind` — maps the most-common `KafkaJSProtocolError` types/codes and the `KafkaJSConnectionError` / `KafkaJSRequestTimeoutError` / `KafkaJSNonRetriableError` subclasses to a category. Verified against `kafkajs/src/errors.js`.
172
+ - `classifyConfluentError(err): PublishErrorKind` — maps the librdkafka `RD_KAFKA_RESP_ERR_*` codes (both negative internal codes and Kafka wire-protocol codes) to a category. Verified against `librdkafka/src/rdkafka.h`. Includes the dedicated `"backpressure"` mapping for `ERR__QUEUE_FULL` (-184) and `"quota"` for `ERR_THROTTLING_QUOTA_EXCEEDED` (89).
173
+ - Both drivers (`KafkaJsDriver`, `ConfluentDriver`) now call their respective classifier in the catch path and emit the `errorKind` on every failed `PublishResult`.
174
+
175
+ **Backward compatibility:** `errorKind` is optional everywhere. Existing publisher implementations that don't set it continue to work unchanged — the relay treats absent `errorKind` as `"retriable"`, which is what the relay did before this change.
176
+
177
+ **Migration:** none required.
178
+
179
+ ## 2.0.0
180
+
181
+ ## 1.0.4
182
+
183
+ ### Patch Changes
184
+
185
+ - 64d115d: docs / metadata: expand `keywords` on all packages for better npm and LLM discoverability (outbox-pattern, dual-write, cdc, event-driven, microservices, etc.). No code changes.
186
+
187
+ ## 1.0.3
188
+
189
+ ### Patch Changes
190
+
191
+ - aaca9a2: docs: use a non-expiring `2026-present` copyright year in LICENSE and a static MIT license badge in the README
192
+
193
+ ## 1.0.2
194
+
195
+ ### Patch Changes
196
+
197
+ - 89f1867: Declare `engines.node` (>=18) so npm shows the supported Node version and tooling can warn on unsupported runtimes.
198
+
199
+ ## 1.0.1
200
+
201
+ ### Patch Changes
202
+
203
+ - docs: polish per-package READMEs (npm page content). No code changes.
204
+
205
+ ## 1.0.0
206
+
207
+ ### Minor Changes
208
+
209
+ - b06f8ec: Add a low-latency notify-driven relay (Postgres `LISTEN`/`NOTIFY`).
210
+
211
+ - **core:** new `Waker` interface and an optional `Relay({ waker })`. The relay's
212
+ idle wait is now interruptible — when the waker signals, it claims immediately
213
+ instead of sleeping out `pollIntervalMs`. With no waker, behavior is unchanged.
214
+ - **postgres:** `PostgresNotifyWaker` holds a dedicated `LISTEN` connection and
215
+ wakes the relay on each notification, reconnecting with backoff if it drops.
216
+ `createNotifyTriggerSql(table, channel)` emits an `AFTER INSERT FOR EACH STATEMENT`
217
+ trigger that `pg_notify`s on commit (empty payload — the relay re-claims).
218
+ - Polling remains the safety net: a missed notification is caught by the next poll,
219
+ so no event is lost. All ordering/retry/DLQ/crash-recovery guarantees are unchanged.
220
+ - No new dependencies (`LISTEN`/`NOTIFY` is native to `pg`).
221
+
222
+ - b06f8ec: Add a streaming relay that publishes straight from the Postgres WAL (logical replication).
223
+
224
+ - **postgres:** `PostgresStreamingRelay` consumes INSERTs on the outbox table via
225
+ `pg-logical-replication` + `pgoutput` (built-in, no DB extension) and publishes them
226
+ with no claim query on the happy path — lower DB load than the notify waker. A failed
227
+ publish is demoted to `failed`; an internal claim-based retry loop drains it with the
228
+ existing backoff / DLQ / dead handling. `pg-logical-replication` is a new **optional**
229
+ peer dependency, loaded only in streaming mode.
230
+ - **postgres:** `PostgresStore` gains `claimFailedOnly` (claims only `failed`/timed-out
231
+ `processing` rows, never `pending`) so the stream owns pending rows with no duplication.
232
+ `createPublicationSql(table, publication)` emits an idempotent insert-only publication.
233
+ - **core:** the record→message builder is extracted as `buildPublishable(record,
234
+ serializer)` and shared by `Relay` and the streaming relay (no behavior change).
235
+ - **At-least-once:** the slot's LSN is acknowledged only after a batch's side effects
236
+ commit; a crash re-streams and re-publishes (idempotent consumers absorb the duplicate).
237
+ - **Ordering:** streaming is best-effort per-aggregate (a retried failure may land after
238
+ later same-aggregate rows). Use the polling relay for the strict head-of-aggregate
239
+ guarantee. Requires `wal_level = logical`.
240
+
241
+ - b06f8ec: Strict per-aggregate ordering, crash recovery, and driver/packaging fixes.
242
+
243
+ - **postgres:** the claim query now enforces strict per-aggregate ordering by
244
+ only taking the _head_ of each aggregate (no earlier unfinished row for the
245
+ same `aggregateId`). At most one in-flight message per aggregate; failed
246
+ messages block their successors until resolved.
247
+ - **postgres:** added a `claimed_at` column and a visibility-timeout reaper
248
+ (`claimTimeoutMs`, default 60s) so rows orphaned by a crashed relay are
249
+ reclaimed instead of stuck in `processing` forever. Migration is upgrade-safe
250
+ (`ADD COLUMN IF NOT EXISTS`); the partial indexes were retuned for the new
251
+ ordered, reaper-aware claim.
252
+ - **core:** dead-lettered messages now carry the real `original-topic` header
253
+ (previously always empty); `ConsoleLogger` routes warn/error to the matching
254
+ `console` methods.
255
+ - **kafka:** the confluent driver now honors `acks` and `compression` (it
256
+ silently ignored them before), matching the kafkajs driver.
257
+ - **packaging:** the `@eventferry/postgres/migrations` subpath export now
258
+ advertises its types; `pnpm-workspace.yaml` dropped an invalid placeholder
259
+ block.
260
+
261
+ Note: `claimTimeoutMs` should exceed your worst-case publish latency. This is
262
+ an at-least-once system — pair it with idempotent producers/consumers.
263
+
264
+ - b06f8ec: Add a type-safe event registry: `defineOutbox`.
265
+
266
+ Declare each topic once (`{ aggregateType, schema }`) and get a typed, runtime-
267
+ validated `enqueue` plus a `decode` helper consumers can reuse from the same
268
+ registry. Payloads are validated before the row is inserted, so a malformed event
269
+ rolls back with the rest of your transaction instead of reaching the outbox.
270
+
271
+ - **Validator-agnostic:** any [Standard Schema](https://standardschema.dev) works
272
+ (Zod 3.24+, Valibot, ArkType, …). The spec interface is inlined, so `@eventferry/core`
273
+ gains no runtime dependency.
274
+ - **Producer + consumer:** `defineOutbox(registry, { store })` exposes typed
275
+ `enqueue`; `defineOutbox(registry)` (no store) exposes `decode`/`validate` for
276
+ consuming services.
277
+ - New `OutboxValidationError` carries the failing topic and the validator's issues.
278
+ - Purely additive — `PostgresStore`, `Relay`, and untyped `store.enqueue` are unchanged.
279
+
280
+ - b06f8ec: Add W3C trace propagation (OpenTelemetry-compatible), dependency-free.
281
+
282
+ - **core:** new `Tracing` interface (`inject(carrier)`), the shape of an OpenTelemetry
283
+ `TextMapPropagator` — the library depends on no tracing package.
284
+ - **postgres:** `PostgresStore({ tracing })` captures the active W3C
285
+ `traceparent`/`tracestate` into the row's headers at `enqueue`, so it rides along to
286
+ the published message (on every path — polling, notify, streaming — since headers
287
+ already pass through) and the consumer can continue the trace.
288
+ - The caller's `headers` object is never mutated. With no `tracing` configured,
289
+ behavior is unchanged. The existing `trace-id` header stays for simple correlation.
290
+ - OpenTelemetry/Datadog/custom integrate via a ~5-line adapter (documented, not bundled).
package/dist/index.cjs CHANGED
@@ -258,9 +258,26 @@ var Relay = class {
258
258
  return batch.length;
259
259
  }
260
260
  async handleFailure(record, error, errorKind) {
261
+ if (errorKind === "backpressure") {
262
+ const delay = this.retry.backpressureDelayMs ?? 1e3;
263
+ const retryAt2 = new Date(Date.now() + delay);
264
+ this.hooks.onFailed?.(record, error, true);
265
+ this.log.warn("publish backpressure \u2014 requeueing without bumping attempts", {
266
+ recordId: record.id,
267
+ retryAt: retryAt2.toISOString(),
268
+ errorKind,
269
+ error: error.message
270
+ });
271
+ if (this.store.requeue) {
272
+ await this.store.requeue(record.id, retryAt2);
273
+ } else {
274
+ await this.store.markFailed(record.id, retryAt2, "failed");
275
+ }
276
+ return;
277
+ }
261
278
  const attempts = record.attempts + 1;
262
279
  const isTerminalKind = errorKind === "fatal" || errorKind === "poison";
263
- const retryAt = isTerminalKind ? null : nextRetryAt(this.retry, attempts);
280
+ const retryAt = isTerminalKind ? null : this.nextRetryAtForKind(attempts, errorKind);
264
281
  const willRetry = retryAt !== null;
265
282
  this.hooks.onFailed?.(record, error, willRetry);
266
283
  this.log.warn("publish failed", {
@@ -282,9 +299,10 @@ var Relay = class {
282
299
  {
283
300
  ...msg,
284
301
  topic: this.dlq.topic,
285
- // Preserve the original destination so the publisher can record
286
- // it as a header; otherwise it is lost when we overwrite `topic`.
287
- headers: { ...msg.headers, "original-topic": record.topic }
302
+ // Per-record enrichment so the DLQ consumer has everything
303
+ // needed for triage: original destination, aggregate / message
304
+ // identity, the attempts count, and (opt-in) a truncated stack.
305
+ headers: this.buildDlqHeaders(record, error, msg.headers)
288
306
  },
289
307
  error
290
308
  );
@@ -300,6 +318,39 @@ var Relay = class {
300
318
  await this.store.markFailed(record.id, null, "dead");
301
319
  this.hooks.onDead?.(record, error);
302
320
  }
321
+ /**
322
+ * Compute the next retry instant, applying the quota multiplier when the
323
+ * driver classified the failure as a server throttle. Quota failures DO
324
+ * count as attempts (unlike backpressure) — the multiplier only stretches
325
+ * the delay, not the budget.
326
+ */
327
+ nextRetryAtForKind(attempts, errorKind) {
328
+ if (attempts >= this.retry.maxAttempts) return null;
329
+ const baseDelay = computeBackoff(this.retry, attempts);
330
+ const multiplier = errorKind === "quota" ? this.retry.quotaMultiplier ?? 5 : 1;
331
+ const delay = Math.min(baseDelay * multiplier, this.retry.maxMs * 10);
332
+ return new Date(Date.now() + delay);
333
+ }
334
+ /**
335
+ * Build the per-record DLQ header bag. Operators triaging the DLQ get the
336
+ * original destination, the aggregate / message identity, attempts count,
337
+ * and optionally a truncated error stack — everything you need to decide
338
+ * whether to re-enqueue, escalate, or drop.
339
+ */
340
+ buildDlqHeaders(record, error, existing) {
341
+ const out = {
342
+ ...existing,
343
+ "original-topic": record.topic,
344
+ "dlq-attempts": String(record.attempts + 1),
345
+ "dlq-original-aggregate-id": record.aggregateId,
346
+ "dlq-original-message-id": record.messageId
347
+ };
348
+ if (this.dlq.includeStackTraces && error.stack) {
349
+ const maxBytes = this.dlq.maxStackBytes ?? 4096;
350
+ out["dlq-error-stack"] = truncateUtf8(error.stack, maxBytes);
351
+ }
352
+ return out;
353
+ }
303
354
  async toPublishable(records) {
304
355
  const out = [];
305
356
  for (const record of records) {
@@ -336,6 +387,22 @@ var Relay = class {
336
387
  });
337
388
  }
338
389
  };
390
+ function truncateUtf8(input, maxBytes) {
391
+ const marker = "\u2026[truncated]";
392
+ const markerBytes = Buffer.byteLength(marker, "utf8");
393
+ const buf = Buffer.from(input, "utf8");
394
+ if (buf.byteLength <= maxBytes) return input;
395
+ const budget = Math.max(0, maxBytes - markerBytes);
396
+ let end = budget;
397
+ while (end > 0) {
398
+ const slice = buf.subarray(0, end).toString("utf8");
399
+ if (!slice.endsWith("\uFFFD")) {
400
+ return slice + marker;
401
+ }
402
+ end--;
403
+ }
404
+ return marker;
405
+ }
339
406
 
340
407
  // src/errors.ts
341
408
  var OutboxValidationError = class extends Error {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/types.ts","../src/backoff.ts","../src/serializer.ts","../src/publishable.ts","../src/relay.ts","../src/errors.ts","../src/registry.ts"],"sourcesContent":["export * from \"./types.js\";\nexport * from \"./backoff.js\";\nexport * from \"./serializer.js\";\nexport * from \"./publishable.js\";\nexport * from \"./relay.js\";\nexport * from \"./standard-schema.js\";\nexport * from \"./errors.js\";\nexport * from \"./registry.js\";\n","/**\n * Status lifecycle of an outbox record.\n *\n * pending -> freshly enqueued, awaiting first publish\n * processing -> claimed by a relay instance, publish in flight\n * done -> successfully published to the broker\n * failed -> publish failed, awaiting retry (next_retry_at)\n * dead -> exhausted retries, routed to DLQ (or parked)\n */\nexport type OutboxStatus = \"pending\" | \"processing\" | \"done\" | \"failed\" | \"dead\";\n\nexport const OUTBOX_STATUS_CODE: Record<OutboxStatus, number> = {\n pending: 0,\n processing: 1,\n done: 2,\n failed: 3,\n dead: 4,\n};\n\nexport const OUTBOX_STATUS_FROM_CODE: Record<number, OutboxStatus> = {\n 0: \"pending\",\n 1: \"processing\",\n 2: \"done\",\n 3: \"failed\",\n 4: \"dead\",\n};\n\n/**\n * A message as enqueued by the application inside its own DB transaction.\n * This is the write-side input — no infra concerns leak in here.\n */\nexport interface OutboxMessageInput {\n /** Logical topic the message will be published to. */\n topic: string;\n /** Type of the aggregate that produced this event (e.g. \"order\"). */\n aggregateType: string;\n /**\n * Identifier of the aggregate instance (e.g. order id).\n * Used as the default partition key to preserve per-aggregate ordering.\n */\n aggregateId: string;\n /** Event payload. Serialized by the configured serializer. */\n payload: unknown;\n /** Optional explicit partition key. Falls back to aggregateId. */\n key?: string;\n /** Optional message headers. */\n headers?: Record<string, string>;\n /**\n * Optional client-supplied message id for idempotency / dedup.\n * If omitted, the store generates one.\n */\n messageId?: string;\n}\n\n/**\n * A persisted outbox record as read back by the relay.\n */\nexport interface OutboxRecord {\n id: string;\n messageId: string;\n topic: string;\n aggregateType: string;\n aggregateId: string;\n key: string | null;\n payload: unknown;\n headers: Record<string, string>;\n traceId: string | null;\n status: OutboxStatus;\n attempts: number;\n nextRetryAt: Date | null;\n createdAt: Date;\n processedAt: Date | null;\n}\n\n/**\n * The message handed to a Publisher after serialization.\n */\nexport interface PublishableMessage {\n topic: string;\n key: string | null;\n /** Serialized payload bytes. */\n value: Buffer;\n headers: Record<string, string>;\n /** Original record id, for correlation in publish results. */\n recordId: string;\n messageId: string;\n /**\n * Explicit partition override. When set, the publisher MUST route the\n * record to this exact partition, bypassing the configured partitioner.\n *\n * Use cases:\n * - Compacted topics with application-managed sharding.\n * - Tenant-affinity routing where you compute the partition yourself.\n * - Geo-pinning records to a specific broker.\n *\n * When omitted (the default), the underlying client's partitioner\n * decides — usually a hash of `key`, falling back to sticky round-robin\n * when `key` is null.\n */\n partition?: number;\n}\n\n/**\n * Why a publish failed, in terms the relay can act on. Drivers classify their\n * native errors into one of these buckets; the relay reads `errorKind` to\n * decide whether to retry, short-circuit to the DLQ, or pause polling. The\n * field is optional for backward compatibility — when absent, the relay\n * treats the error as `\"retriable\"`.\n *\n * - `retriable` — transient (broker unreachable, leader election, request\n * timeout); retry per the configured backoff policy. The default for any\n * unclassified error.\n * - `fatal` — the producer or the credentials are broken (fenced epoch,\n * authentication failed, ACL denied). Retrying cannot help; the relay\n * short-circuits straight to the DLQ + `dead` status.\n * - `poison` — the message itself is rejectable by every broker\n * (oversized record, corrupt payload, schema-registry refused encoding).\n * Same handling as `fatal`: DLQ + dead, no retries.\n * - `backpressure` — the *producer's own* outbound buffer is full\n * (librdkafka `__QUEUE_FULL`). The right response is to slow the relay\n * down, not to burn retries. v2.1 treats this as `retriable` for\n * compatibility; smarter handling (pause polling) is planned.\n * - `quota` — the broker is throttling us (`THROTTLING_QUOTA_EXCEEDED`).\n * Back off with longer delays. v2.1 treats as `retriable`; smarter\n * handling (longer backoff) is planned.\n */\nexport type PublishErrorKind =\n | \"retriable\"\n | \"fatal\"\n | \"poison\"\n | \"backpressure\"\n | \"quota\";\n\n/**\n * Result of attempting to publish a single message.\n */\nexport interface PublishResult {\n recordId: string;\n ok: boolean;\n error?: Error;\n /**\n * Optional classification of `error` for relay-level decision-making. Set\n * by publisher implementations that know how to inspect their native error\n * shapes. Absent value is treated as `\"retriable\"` by the relay (the safe\n * default — at worst we retry an error we should have skipped).\n */\n errorKind?: PublishErrorKind;\n}\n\n/**\n * Pluggable serializer. Default is JSON; users can swap in\n * Avro / Protobuf / Schema-Registry-backed serializers.\n */\nexport interface Serializer {\n serialize(message: OutboxRecord): Buffer | Promise<Buffer>;\n /** Content-type header value advertised for this serializer. */\n readonly contentType: string;\n}\n\n/**\n * Storage abstraction. Implemented per-database (postgres, mysql, ...).\n * The relay only talks to the store through this interface.\n */\nexport interface OutboxStore {\n /**\n * Atomically claim up to `batchSize` due messages and mark them\n * as `processing`. Implementations MUST be safe under concurrent\n * relay instances (e.g. SELECT ... FOR UPDATE SKIP LOCKED).\n */\n claimBatch(batchSize: number): Promise<OutboxRecord[]>;\n\n /** Mark records as successfully published. */\n markDone(recordIds: string[]): Promise<void>;\n\n /**\n * Mark a record as failed and schedule its next retry.\n * `nextRetryAt` of null + status \"dead\" means terminal.\n */\n markFailed(\n recordId: string,\n nextRetryAt: Date | null,\n status: \"failed\" | \"dead\",\n ): Promise<void>;\n\n /** Best-effort lifecycle hooks; no-op allowed. */\n init?(): Promise<void>;\n close?(): Promise<void>;\n}\n\n/**\n * Broker abstraction. Implemented per-driver (kafkajs, confluent, ...).\n */\nexport interface Publisher {\n connect(): Promise<void>;\n disconnect(): Promise<void>;\n /**\n * Publish a batch. Returns a per-message result so the relay can\n * mark partial success. Implementations may use a transactional\n * producer to make the batch atomic.\n */\n publish(messages: PublishableMessage[]): Promise<PublishResult[]>;\n /** Route a permanently-failed message to a dead-letter destination. */\n publishToDlq?(message: PublishableMessage, error: Error): Promise<void>;\n}\n\n/**\n * Backoff strategy for retrying failed publishes.\n */\nexport type BackoffStrategy = \"fixed\" | \"linear\" | \"exponential\";\n\nexport interface RetryConfig {\n maxAttempts: number;\n strategy: BackoffStrategy;\n baseMs: number;\n maxMs: number;\n /** Add random jitter (0..baseMs) to avoid thundering herd. Default true. */\n jitter?: boolean;\n}\n\nexport interface DlqConfig {\n /** Topic to route dead messages to. If absent, dead messages are parked. */\n topic?: string;\n}\n\n/**\n * Optional trace-context propagator. `inject` writes the active trace context\n * into the carrier as W3C `traceparent`/`tracestate` (the shape of an\n * OpenTelemetry TextMapPropagator), so it is persisted with the outbox row and\n * carried to the published message. The library depends on no tracing package;\n * provide a thin adapter over yours (OpenTelemetry, Datadog, …).\n */\nexport interface Tracing {\n inject(carrier: Record<string, string>): void;\n}\n\n/**\n * Minimal structured logger. Console-backed default provided.\n */\nexport interface Logger {\n debug(msg: string, meta?: Record<string, unknown>): void;\n info(msg: string, meta?: Record<string, unknown>): void;\n warn(msg: string, meta?: Record<string, unknown>): void;\n error(msg: string, meta?: Record<string, unknown>): void;\n}\n\n/**\n * A low-latency wake source for the relay. Instead of only waking on the poll\n * interval, the relay claims immediately whenever `onWake` fires. The signal is\n * advisory: it may fire spuriously or be missed entirely, and the relay's\n * polling remains the safety net, so implementations need not deduplicate or\n * guarantee delivery. (e.g. a Postgres LISTEN/NOTIFY waker.)\n */\nexport interface Waker {\n start(onWake: () => void): Promise<void>;\n stop(): Promise<void>;\n}\n\n/**\n * Lifecycle / observability hooks emitted by the relay.\n */\nexport interface RelayHooks {\n onBatchClaimed?(count: number): void;\n onPublished?(result: PublishResult): void;\n onFailed?(record: OutboxRecord, error: Error, willRetry: boolean): void;\n onDead?(record: OutboxRecord, error: Error): void;\n onError?(error: Error): void;\n}\n","import type { RetryConfig } from \"./types.js\";\n\n/**\n * Compute the delay (ms) before the next retry attempt.\n *\n * @param attempt 1-based attempt number that just failed.\n */\nexport function computeBackoff(config: RetryConfig, attempt: number): number {\n const { strategy, baseMs, maxMs } = config;\n const jitter = config.jitter ?? true;\n\n let delay: number;\n switch (strategy) {\n case \"fixed\":\n delay = baseMs;\n break;\n case \"linear\":\n delay = baseMs * attempt;\n break;\n case \"exponential\":\n // base * 2^(attempt-1), capped to avoid overflow before clamping\n delay = baseMs * 2 ** Math.min(attempt - 1, 30);\n break;\n }\n\n delay = Math.min(delay, maxMs);\n\n if (jitter) {\n // Full jitter: random in [0, delay]. Decorrelates concurrent relays.\n delay = Math.random() * delay;\n }\n\n return Math.floor(delay);\n}\n\n/**\n * Resolve when (Date) the next retry should occur, or null if the\n * record has exhausted its attempts and should go dead.\n */\nexport function nextRetryAt(\n config: RetryConfig,\n attempts: number,\n now: Date = new Date(),\n): Date | null {\n if (attempts >= config.maxAttempts) return null;\n const delay = computeBackoff(config, attempts);\n return new Date(now.getTime() + delay);\n}\n","import type { Logger, OutboxRecord, Serializer } from \"./types.js\";\n\n/**\n * Default serializer: JSON-encodes the record payload to UTF-8 bytes.\n */\nexport class JsonSerializer implements Serializer {\n readonly contentType = \"application/json\";\n\n serialize(record: OutboxRecord): Buffer {\n return Buffer.from(JSON.stringify(record.payload), \"utf8\");\n }\n}\n\n/**\n * Minimal console-backed logger. Swap in pino/winston by implementing Logger.\n */\nexport class ConsoleLogger implements Logger {\n constructor(private readonly prefix = \"[outbox]\") {}\n\n private fmt(\n write: (line: string, meta?: Record<string, unknown>) => void,\n level: string,\n msg: string,\n meta?: Record<string, unknown>,\n ): void {\n const line = `${this.prefix} ${level} ${msg}`;\n if (meta) {\n write(line, meta);\n } else {\n write(line);\n }\n }\n\n debug(msg: string, meta?: Record<string, unknown>): void {\n // eslint-disable-next-line no-console\n this.fmt(console.debug, \"DEBUG\", msg, meta);\n }\n info(msg: string, meta?: Record<string, unknown>): void {\n // eslint-disable-next-line no-console\n this.fmt(console.info, \"INFO\", msg, meta);\n }\n warn(msg: string, meta?: Record<string, unknown>): void {\n // eslint-disable-next-line no-console\n this.fmt(console.warn, \"WARN\", msg, meta);\n }\n error(msg: string, meta?: Record<string, unknown>): void {\n // eslint-disable-next-line no-console\n this.fmt(console.error, \"ERROR\", msg, meta);\n }\n}\n\n/** Logger that discards everything. Useful for tests. */\nexport class NoopLogger implements Logger {\n debug(): void {}\n info(): void {}\n warn(): void {}\n error(): void {}\n}\n","import type { OutboxRecord, PublishableMessage, Serializer } from \"./types.js\";\n\n/**\n * Turn a persisted outbox record into a broker-ready message: serialize the\n * payload and attach the standard correlation headers. Shared by the polling\n * `Relay` and the streaming relay so both produce byte-identical messages.\n */\nexport async function buildPublishable(\n record: OutboxRecord,\n serializer: Serializer,\n): Promise<PublishableMessage> {\n const value = await serializer.serialize(record);\n const headers: Record<string, string> = {\n ...record.headers,\n \"content-type\": serializer.contentType,\n \"message-id\": record.messageId,\n \"aggregate-type\": record.aggregateType,\n \"aggregate-id\": record.aggregateId,\n };\n if (record.traceId) headers[\"trace-id\"] = record.traceId;\n\n return {\n topic: record.topic,\n key: record.key ?? record.aggregateId,\n value,\n headers,\n recordId: record.id,\n messageId: record.messageId,\n };\n}\n","import { nextRetryAt } from \"./backoff.js\";\nimport { buildPublishable } from \"./publishable.js\";\nimport { ConsoleLogger, JsonSerializer } from \"./serializer.js\";\nimport type {\n DlqConfig,\n Logger,\n OutboxRecord,\n OutboxStore,\n PublishableMessage,\n PublishErrorKind,\n Publisher,\n RelayHooks,\n RetryConfig,\n Serializer,\n Waker,\n} from \"./types.js\";\n\nexport interface RelayOptions {\n store: OutboxStore;\n publisher: Publisher;\n /** Messages claimed per poll iteration. Default 100. */\n batchSize?: number;\n /** Idle wait (ms) when a poll returns no work. Default 200. */\n pollIntervalMs?: number;\n retry?: Partial<RetryConfig>;\n dlq?: DlqConfig;\n serializer?: Serializer;\n logger?: Logger;\n hooks?: RelayHooks;\n /**\n * Optional low-latency wake source. When provided, the relay claims as soon as\n * the waker signals new work, instead of waiting out `pollIntervalMs`. Polling\n * stays on as a safety net, so set `pollIntervalMs` longer when using a waker.\n */\n waker?: Waker;\n}\n\nconst DEFAULT_RETRY: RetryConfig = {\n maxAttempts: 5,\n strategy: \"exponential\",\n baseMs: 200,\n maxMs: 30_000,\n jitter: true,\n};\n\n/**\n * The Relay drains the outbox store and publishes messages to the broker.\n *\n * It is safe to run multiple Relay instances concurrently against the same\n * store as long as the store's claimBatch uses a lock-free claim strategy\n * (e.g. SELECT ... FOR UPDATE SKIP LOCKED).\n */\nexport class Relay {\n private readonly store: OutboxStore;\n private readonly publisher: Publisher;\n private readonly batchSize: number;\n private readonly pollIntervalMs: number;\n private readonly retry: RetryConfig;\n private readonly dlq: DlqConfig;\n private readonly serializer: Serializer;\n private readonly log: Logger;\n private readonly hooks: RelayHooks;\n private readonly waker: Waker | null;\n\n private running = false;\n private stopping = false;\n private loopPromise: Promise<void> | null = null;\n\n // Interruptible idle wait: `signal()` wakes a pending wait (or marks one\n // pending if none is in flight, so a wake can't be lost between cycles).\n private wakePending = false;\n private wakeResolver: (() => void) | null = null;\n\n constructor(opts: RelayOptions) {\n this.store = opts.store;\n this.publisher = opts.publisher;\n this.batchSize = opts.batchSize ?? 100;\n this.pollIntervalMs = opts.pollIntervalMs ?? 200;\n this.retry = { ...DEFAULT_RETRY, ...opts.retry };\n this.dlq = opts.dlq ?? {};\n this.serializer = opts.serializer ?? new JsonSerializer();\n this.log = opts.logger ?? new ConsoleLogger();\n this.hooks = opts.hooks ?? {};\n this.waker = opts.waker ?? null;\n }\n\n async start(): Promise<void> {\n if (this.running) return;\n this.running = true;\n this.stopping = false;\n\n await this.store.init?.();\n await this.publisher.connect();\n await this.waker?.start(() => this.signal());\n this.log.info(\"relay started\", {\n batchSize: this.batchSize,\n pollIntervalMs: this.pollIntervalMs,\n waker: this.waker !== null,\n });\n\n this.loopPromise = this.loop();\n }\n\n /**\n * Stop accepting new work, finish the in-flight batch, then disconnect.\n */\n async stop(): Promise<void> {\n if (!this.running) return;\n this.stopping = true;\n this.signal(); // break any in-flight idle wait so shutdown is immediate\n this.log.info(\"relay stopping, draining in-flight batch\");\n await this.loopPromise;\n await this.waker?.stop();\n await this.publisher.disconnect();\n await this.store.close?.();\n this.running = false;\n this.log.info(\"relay stopped\");\n }\n\n private async loop(): Promise<void> {\n while (!this.stopping) {\n try {\n const processed = await this.tick();\n if (processed === 0 && !this.stopping) {\n await this.waitForWork(this.pollIntervalMs);\n }\n } catch (err) {\n const error = err instanceof Error ? err : new Error(String(err));\n this.log.error(\"relay loop error\", { error: error.message });\n this.hooks.onError?.(error);\n await this.waitForWork(this.pollIntervalMs);\n }\n }\n }\n\n /**\n * Run a single claim+publish cycle. Returns number of records processed.\n * Exposed for tests / manual single-shot draining.\n */\n async tick(): Promise<number> {\n const batch = await this.store.claimBatch(this.batchSize);\n if (batch.length === 0) return 0;\n\n this.hooks.onBatchClaimed?.(batch.length);\n this.log.debug(\"batch claimed\", { count: batch.length });\n\n const messages = await this.toPublishable(batch);\n const recordsById = new Map(batch.map((r) => [r.id, r]));\n\n const results = await this.publisher.publish(messages);\n\n const succeeded: string[] = [];\n for (const result of results) {\n const record = recordsById.get(result.recordId);\n if (!record) continue;\n\n if (result.ok) {\n succeeded.push(record.id);\n this.hooks.onPublished?.(result);\n } else {\n await this.handleFailure(\n record,\n result.error ?? new Error(\"unknown publish error\"),\n result.errorKind,\n );\n }\n }\n\n if (succeeded.length > 0) {\n await this.store.markDone(succeeded);\n }\n\n return batch.length;\n }\n\n private async handleFailure(\n record: OutboxRecord,\n error: Error,\n errorKind?: PublishErrorKind,\n ): Promise<void> {\n const attempts = record.attempts + 1;\n // fatal/poison short-circuit: retrying cannot help (auth denied, fenced\n // epoch, oversized record, schema rejected). Skip the backoff schedule\n // entirely and go straight to DLQ + dead.\n const isTerminalKind = errorKind === \"fatal\" || errorKind === \"poison\";\n const retryAt = isTerminalKind ? null : nextRetryAt(this.retry, attempts);\n const willRetry = retryAt !== null;\n\n this.hooks.onFailed?.(record, error, willRetry);\n this.log.warn(\"publish failed\", {\n recordId: record.id,\n attempts,\n willRetry,\n errorKind: errorKind ?? \"retriable\",\n error: error.message,\n });\n\n if (willRetry) {\n await this.store.markFailed(record.id, retryAt, \"failed\");\n return;\n }\n\n // Terminal: route to DLQ if configured, then mark dead.\n if (this.dlq.topic && this.publisher.publishToDlq) {\n try {\n const msg = (await this.toPublishable([record]))[0];\n if (msg) {\n await this.publisher.publishToDlq(\n {\n ...msg,\n topic: this.dlq.topic,\n // Preserve the original destination so the publisher can record\n // it as a header; otherwise it is lost when we overwrite `topic`.\n headers: { ...msg.headers, \"original-topic\": record.topic },\n },\n error,\n );\n }\n } catch (dlqErr) {\n const e = dlqErr instanceof Error ? dlqErr : new Error(String(dlqErr));\n this.log.error(\"DLQ publish failed\", {\n recordId: record.id,\n error: e.message,\n });\n }\n }\n\n await this.store.markFailed(record.id, null, \"dead\");\n this.hooks.onDead?.(record, error);\n }\n\n private async toPublishable(\n records: OutboxRecord[],\n ): Promise<PublishableMessage[]> {\n const out: PublishableMessage[] = [];\n for (const record of records) {\n out.push(await buildPublishable(record, this.serializer));\n }\n return out;\n }\n\n /** Wake a pending idle wait, or mark one pending so the next wait is skipped. */\n private signal(): void {\n this.wakePending = true;\n this.wakeResolver?.();\n }\n\n /**\n * Idle wait that resolves after `ms`, or early when `signal()` fires. A signal\n * raised while no wait is in flight is remembered (wakePending), so a wake that\n * races a claim cycle is never lost.\n */\n private waitForWork(ms: number): Promise<void> {\n if (this.wakePending) {\n this.wakePending = false;\n return Promise.resolve();\n }\n return new Promise((resolve) => {\n const timer = setTimeout(() => {\n this.wakeResolver = null;\n resolve();\n }, ms);\n this.wakeResolver = () => {\n clearTimeout(timer);\n this.wakeResolver = null;\n this.wakePending = false;\n resolve();\n };\n });\n }\n}\n","import type { StandardSchemaV1 } from \"./standard-schema.js\";\n\n/**\n * Thrown when an outbox payload fails its topic's schema — at `enqueue`\n * (before the DB insert) or at `decode` (malformed bytes / schema mismatch).\n * Carries the topic and the validator's structured issues for diagnostics.\n */\nexport class OutboxValidationError extends Error {\n readonly topic: string;\n readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;\n\n constructor(\n topic: string,\n issues: ReadonlyArray<StandardSchemaV1.Issue>,\n options?: { cause?: unknown },\n ) {\n const first = issues[0]?.message ?? \"unknown validation error\";\n super(`Outbox payload for \"${topic}\" failed validation: ${first}`, options);\n this.name = \"OutboxValidationError\";\n this.topic = topic;\n this.issues = issues;\n }\n}\n","import { OutboxValidationError } from \"./errors.js\";\nimport type { StandardSchemaV1 } from \"./standard-schema.js\";\nimport type { OutboxMessageInput } from \"./types.js\";\n\n/** One topic's contract: the aggregate it belongs to and its payload schema. */\nexport interface TopicDefinition {\n /** Aggregate type stamped on every event of this topic (e.g. \"order\"). */\n readonly aggregateType: string;\n /** Standard Schema for the payload (Zod 3.24+/Valibot/ArkType/…). */\n readonly schema: StandardSchemaV1;\n}\n\n/** A map of topic name -> its definition. The single source of truth. */\nexport type OutboxRegistry = Record<string, TopicDefinition>;\n\n/**\n * Minimal store surface the producer facade needs. `PostgresStore` satisfies\n * this structurally, so the facade stays DB-agnostic and `tx` flows from the\n * concrete store (e.g. a pg client).\n */\nexport interface EnqueueableStore<Tx = unknown> {\n enqueue(tx: Tx, msg: OutboxMessageInput & { traceId?: string }): Promise<string>;\n}\n\ntype TxOf<S> = S extends EnqueueableStore<infer Tx> ? Tx : never;\n\ntype PayloadInput<R extends OutboxRegistry, K extends keyof R> =\n StandardSchemaV1.InferInput<R[K][\"schema\"]>;\ntype PayloadOutput<R extends OutboxRegistry, K extends keyof R> =\n StandardSchemaV1.InferOutput<R[K][\"schema\"]>;\n\n/** The write-side input for a typed enqueue (payload is the schema's input type). */\nexport interface EnqueueInput<R extends OutboxRegistry, K extends keyof R> {\n aggregateId: string;\n payload: PayloadInput<R, K>;\n key?: string;\n headers?: Record<string, string>;\n messageId?: string;\n traceId?: string;\n}\n\n/** Consumer-side facade: validate/decode without a store. */\nexport interface OutboxConsumer<R extends OutboxRegistry> {\n /** Validate an already-parsed value against the topic's schema. */\n validate<K extends keyof R & string>(\n topic: K,\n value: unknown,\n ): Promise<PayloadOutput<R, K>>;\n /** JSON-parse `bytes`, validate against the topic's schema, return the payload. */\n decode<K extends keyof R & string>(\n topic: K,\n bytes: Buffer | Uint8Array | string,\n ): Promise<PayloadOutput<R, K>>;\n}\n\n/** Producer-side facade: adds typed, validated enqueue. */\nexport interface OutboxProducer<R extends OutboxRegistry, Tx>\n extends OutboxConsumer<R> {\n /** Validate `payload`, then enqueue inside the caller's transaction `tx`. */\n enqueue<K extends keyof R & string>(\n tx: Tx,\n topic: K,\n msg: EnqueueInput<R, K>,\n ): Promise<string>;\n}\n\n// Loose shape used inside the implementation; the public types come from overloads.\ninterface LooseEnqueueInput {\n aggregateId: string;\n payload: unknown;\n key?: string;\n headers?: Record<string, string>;\n messageId?: string;\n traceId?: string;\n}\n\nexport function defineOutbox<R extends OutboxRegistry>(\n registry: R,\n): OutboxConsumer<R>;\nexport function defineOutbox<R extends OutboxRegistry, S extends EnqueueableStore>(\n registry: R,\n opts: { store: S },\n): OutboxProducer<R, TxOf<S>>;\nexport function defineOutbox<R extends OutboxRegistry>(\n registry: R,\n opts?: { store: EnqueueableStore },\n): OutboxConsumer<R> | OutboxProducer<R, unknown> {\n const validate = async (topic: string, value: unknown): Promise<unknown> => {\n const def = registry[topic];\n if (!def) {\n throw new OutboxValidationError(topic, [\n { message: `unknown topic \"${topic}\"` },\n ]);\n }\n const result = await def.schema[\"~standard\"].validate(value);\n if (result.issues) throw new OutboxValidationError(topic, result.issues);\n return result.value;\n };\n\n const decode = async (\n topic: string,\n bytes: Buffer | Uint8Array | string,\n ): Promise<unknown> => {\n const text =\n typeof bytes === \"string\" ? bytes : Buffer.from(bytes).toString(\"utf8\");\n let parsed: unknown;\n try {\n parsed = JSON.parse(text);\n } catch (err) {\n throw new OutboxValidationError(\n topic,\n [{ message: `invalid JSON: ${(err as Error).message}` }],\n { cause: err },\n );\n }\n return validate(topic, parsed);\n };\n\n const consumer = { validate, decode } as unknown as OutboxConsumer<R>;\n if (!opts?.store) return consumer;\n\n const store = opts.store;\n const enqueue = async (\n tx: unknown,\n topic: string,\n msg: LooseEnqueueInput,\n ): Promise<string> => {\n const def = registry[topic];\n if (!def) {\n throw new OutboxValidationError(topic, [\n { message: `unknown topic \"${topic}\"` },\n ]);\n }\n const payload = await validate(topic, msg.payload);\n return store.enqueue(tx, {\n topic,\n aggregateType: def.aggregateType,\n aggregateId: msg.aggregateId,\n payload,\n key: msg.key,\n headers: msg.headers,\n messageId: msg.messageId,\n traceId: msg.traceId,\n });\n };\n\n return { validate, decode, enqueue } as unknown as OutboxProducer<R, unknown>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACWO,IAAM,qBAAmD;AAAA,EAC9D,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,MAAM;AACR;AAEO,IAAM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J,OAAO,UAAU,WAAW,QAAQ,OAAO,KAAK,KAAK,EAAE,SAAS,MAAM;AACxE,QAAI;AACJ,QAAI;AACF,eAAS,KAAK,MAAM,IAAI;AAAA,IAC1B,SAAS,KAAK;AACZ,YAAM,IAAI;AAAA,QACR;AAAA,QACA,CAAC,EAAE,SAAS,iBAAkB,IAAc,OAAO,GAAG,CAAC;AAAA,QACvD,EAAE,OAAO,IAAI;AAAA,MACf;AAAA,IACF;AACA,WAAO,SAAS,OAAO,MAAM;AAAA,EAC/B;AAEA,QAAM,WAAW,EAAE,UAAU,OAAO;AACpC,MAAI,CAAC,MAAM,MAAO,QAAO;AAEzB,QAAM,QAAQ,KAAK;AACnB,QAAM,UAAU,OACd,IACA,OACA,QACoB;AACpB,UAAM,MAAM,SAAS,KAAK;AAC1B,QAAI,CAAC,KAAK;AACR,YAAM,IAAI,sBAAsB,OAAO;AAAA,QACrC,EAAE,SAAS,kBAAkB,KAAK,IAAI;AAAA,MACxC,CAAC;AAAA,IACH;AACA,UAAM,UAAU,MAAM,SAAS,OAAO,IAAI,OAAO;AACjD,WAAO,MAAM,QAAQ,IAAI;AAAA,MACvB;AAAA,MACA,eAAe,IAAI;AAAA,MACnB,aAAa,IAAI;AAAA,MACjB;AAAA,MACA,KAAK,IAAI;AAAA,MACT,SAAS,IAAI;AAAA,MACb,WAAW,IAAI;AAAA,MACf,SAAS,IAAI;AAAA,IACf,CAAC;AAAA,EACH;AAEA,SAAO,EAAE,UAAU,QAAQ,QAAQ;AACrC;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/types.ts","../src/backoff.ts","../src/serializer.ts","../src/publishable.ts","../src/relay.ts","../src/errors.ts","../src/registry.ts"],"sourcesContent":["export * from \"./types.js\";\nexport * from \"./backoff.js\";\nexport * from \"./serializer.js\";\nexport * from \"./publishable.js\";\nexport * from \"./relay.js\";\nexport * from \"./standard-schema.js\";\nexport * from \"./errors.js\";\nexport * from \"./registry.js\";\n","/**\n * Status lifecycle of an outbox record.\n *\n * pending -> freshly enqueued, awaiting first publish\n * processing -> claimed by a relay instance, publish in flight\n * done -> successfully published to the broker\n * failed -> publish failed, awaiting retry (next_retry_at)\n * dead -> exhausted retries, routed to DLQ (or parked)\n */\nexport type OutboxStatus = \"pending\" | \"processing\" | \"done\" | \"failed\" | \"dead\";\n\nexport const OUTBOX_STATUS_CODE: Record<OutboxStatus, number> = {\n pending: 0,\n processing: 1,\n done: 2,\n failed: 3,\n dead: 4,\n};\n\nexport const OUTBOX_STATUS_FROM_CODE: Record<number, OutboxStatus> = {\n 0: \"pending\",\n 1: \"processing\",\n 2: \"done\",\n 3: \"failed\",\n 4: \"dead\",\n};\n\n/**\n * A message as enqueued by the application inside its own DB transaction.\n * This is the write-side input — no infra concerns leak in here.\n */\nexport interface OutboxMessageInput {\n /** Logical topic the message will be published to. */\n topic: string;\n /** Type of the aggregate that produced this event (e.g. \"order\"). */\n aggregateType: string;\n /**\n * Identifier of the aggregate instance (e.g. order id).\n * Used as the default partition key to preserve per-aggregate ordering.\n */\n aggregateId: string;\n /** Event payload. Serialized by the configured serializer. */\n payload: unknown;\n /** Optional explicit partition key. Falls back to aggregateId. */\n key?: string;\n /** Optional message headers. */\n headers?: Record<string, string>;\n /**\n * Optional client-supplied message id for idempotency / dedup.\n * If omitted, the store generates one.\n */\n messageId?: string;\n}\n\n/**\n * A persisted outbox record as read back by the relay.\n */\nexport interface OutboxRecord {\n id: string;\n messageId: string;\n topic: string;\n aggregateType: string;\n aggregateId: string;\n key: string | null;\n payload: unknown;\n headers: Record<string, string>;\n traceId: string | null;\n status: OutboxStatus;\n attempts: number;\n nextRetryAt: Date | null;\n createdAt: Date;\n processedAt: Date | null;\n}\n\n/**\n * The message handed to a Publisher after serialization.\n */\nexport interface PublishableMessage {\n topic: string;\n key: string | null;\n /** Serialized payload bytes. */\n value: Buffer;\n headers: Record<string, string>;\n /** Original record id, for correlation in publish results. */\n recordId: string;\n messageId: string;\n /**\n * Explicit partition override. When set, the publisher MUST route the\n * record to this exact partition, bypassing the configured partitioner.\n *\n * Use cases:\n * - Compacted topics with application-managed sharding.\n * - Tenant-affinity routing where you compute the partition yourself.\n * - Geo-pinning records to a specific broker.\n *\n * When omitted (the default), the underlying client's partitioner\n * decides — usually a hash of `key`, falling back to sticky round-robin\n * when `key` is null.\n */\n partition?: number;\n}\n\n/**\n * Why a publish failed, in terms the relay can act on. Drivers classify their\n * native errors into one of these buckets; the relay reads `errorKind` to\n * decide whether to retry, short-circuit to the DLQ, or pause polling. The\n * field is optional for backward compatibility — when absent, the relay\n * treats the error as `\"retriable\"`.\n *\n * - `retriable` — transient (broker unreachable, leader election, request\n * timeout); retry per the configured backoff policy. The default for any\n * unclassified error.\n * - `fatal` — the producer or the credentials are broken (fenced epoch,\n * authentication failed, ACL denied). Retrying cannot help; the relay\n * short-circuits straight to the DLQ + `dead` status.\n * - `poison` — the message itself is rejectable by every broker\n * (oversized record, corrupt payload, schema-registry refused encoding).\n * Same handling as `fatal`: DLQ + dead, no retries.\n * - `backpressure` — the *producer's own* outbound buffer is full\n * (librdkafka `__QUEUE_FULL`). The right response is to slow the relay\n * down, not to burn retries. v2.1 treats this as `retriable` for\n * compatibility; smarter handling (pause polling) is planned.\n * - `quota` — the broker is throttling us (`THROTTLING_QUOTA_EXCEEDED`).\n * Back off with longer delays. v2.1 treats as `retriable`; smarter\n * handling (longer backoff) is planned.\n */\nexport type PublishErrorKind =\n | \"retriable\"\n | \"fatal\"\n | \"poison\"\n | \"backpressure\"\n | \"quota\";\n\n/**\n * Result of attempting to publish a single message.\n */\nexport interface PublishResult {\n recordId: string;\n ok: boolean;\n error?: Error;\n /**\n * Optional classification of `error` for relay-level decision-making. Set\n * by publisher implementations that know how to inspect their native error\n * shapes. Absent value is treated as `\"retriable\"` by the relay (the safe\n * default — at worst we retry an error we should have skipped).\n */\n errorKind?: PublishErrorKind;\n}\n\n/**\n * Pluggable serializer. Default is JSON; users can swap in\n * Avro / Protobuf / Schema-Registry-backed serializers.\n */\nexport interface Serializer {\n serialize(message: OutboxRecord): Buffer | Promise<Buffer>;\n /** Content-type header value advertised for this serializer. */\n readonly contentType: string;\n}\n\n/**\n * Storage abstraction. Implemented per-database (postgres, mysql, ...).\n * The relay only talks to the store through this interface.\n */\nexport interface OutboxStore {\n /**\n * Atomically claim up to `batchSize` due messages and mark them\n * as `processing`. Implementations MUST be safe under concurrent\n * relay instances (e.g. SELECT ... FOR UPDATE SKIP LOCKED).\n */\n claimBatch(batchSize: number): Promise<OutboxRecord[]>;\n\n /** Mark records as successfully published. */\n markDone(recordIds: string[]): Promise<void>;\n\n /**\n * Mark a record as failed and schedule its next retry.\n * `nextRetryAt` of null + status \"dead\" means terminal.\n * Implementations MUST increment `attempts` so the retry budget is honored.\n */\n markFailed(\n recordId: string,\n nextRetryAt: Date | null,\n status: \"failed\" | \"dead\",\n ): Promise<void>;\n\n /**\n * Re-queue a record back to `failed` with the given `retryAt` **WITHOUT\n * incrementing attempts**. Used by the relay for backpressure handling —\n * a client-side queue-full failure is a \"slow down\" signal, not a\n * record-specific failure, and counting it as a retry would burn the\n * attempt budget unfairly.\n *\n * Optional: stores that don't implement this fall back to `markFailed`\n * (which increments attempts, with the caveat documented above).\n */\n requeue?(recordId: string, retryAt: Date): Promise<void>;\n\n /** Best-effort lifecycle hooks; no-op allowed. */\n init?(): Promise<void>;\n close?(): Promise<void>;\n}\n\n/**\n * Broker abstraction. Implemented per-driver (kafkajs, confluent, ...).\n */\nexport interface Publisher {\n connect(): Promise<void>;\n disconnect(): Promise<void>;\n /**\n * Publish a batch. Returns a per-message result so the relay can\n * mark partial success. Implementations may use a transactional\n * producer to make the batch atomic.\n */\n publish(messages: PublishableMessage[]): Promise<PublishResult[]>;\n /** Route a permanently-failed message to a dead-letter destination. */\n publishToDlq?(message: PublishableMessage, error: Error): Promise<void>;\n}\n\n/**\n * Backoff strategy for retrying failed publishes.\n */\nexport type BackoffStrategy = \"fixed\" | \"linear\" | \"exponential\";\n\nexport interface RetryConfig {\n maxAttempts: number;\n strategy: BackoffStrategy;\n baseMs: number;\n maxMs: number;\n /** Add random jitter (0..baseMs) to avoid thundering herd. Default true. */\n jitter?: boolean;\n /**\n * Delay (ms) before re-queueing a record whose publish was rejected with\n * `errorKind: \"backpressure\"` (client-side producer buffer full).\n *\n * Backpressure failures do NOT count as a failed attempt — the buffer\n * being full is a \"slow down\" signal, not a record-specific failure.\n * The record is requeued at the next interval, not promoted to dead.\n *\n * Default 1000 ms. Requires the {@link OutboxStore} to implement\n * `requeue` — stores without it fall back to {@link OutboxStore.markFailed}\n * which DOES increment attempts.\n */\n backpressureDelayMs?: number;\n /**\n * Multiplier applied to the computed backoff for records rejected with\n * `errorKind: \"quota\"` (broker is throttling the producer). Default 5 —\n * a quota signal asks for a longer breath than a generic transient error.\n * Quota failures DO count as attempts (unlike backpressure).\n */\n quotaMultiplier?: number;\n}\n\nexport interface DlqConfig {\n /** Topic to route dead messages to. If absent, dead messages are parked. */\n topic?: string;\n /**\n * Include a truncated stack trace as the `dlq-error-stack` header when\n * routing a record to the DLQ. Default false — keep DLQ messages small\n * by default; opt in if your triage workflow needs the stack.\n */\n includeStackTraces?: boolean;\n /**\n * Maximum bytes of the truncated stack trace included when\n * `includeStackTraces` is on. Default 4096.\n */\n maxStackBytes?: number;\n}\n\n/**\n * Optional trace-context propagator. `inject` writes the active trace context\n * into the carrier as W3C `traceparent`/`tracestate` (the shape of an\n * OpenTelemetry TextMapPropagator), so it is persisted with the outbox row and\n * carried to the published message. The library depends on no tracing package;\n * provide a thin adapter over yours (OpenTelemetry, Datadog, …).\n */\nexport interface Tracing {\n inject(carrier: Record<string, string>): void;\n}\n\n/**\n * Minimal structured logger. Console-backed default provided.\n */\nexport interface Logger {\n debug(msg: string, meta?: Record<string, unknown>): void;\n info(msg: string, meta?: Record<string, unknown>): void;\n warn(msg: string, meta?: Record<string, unknown>): void;\n error(msg: string, meta?: Record<string, unknown>): void;\n}\n\n/**\n * A low-latency wake source for the relay. Instead of only waking on the poll\n * interval, the relay claims immediately whenever `onWake` fires. The signal is\n * advisory: it may fire spuriously or be missed entirely, and the relay's\n * polling remains the safety net, so implementations need not deduplicate or\n * guarantee delivery. (e.g. a Postgres LISTEN/NOTIFY waker.)\n */\nexport interface Waker {\n start(onWake: () => void): Promise<void>;\n stop(): Promise<void>;\n}\n\n/**\n * Lifecycle / observability hooks emitted by the relay.\n */\nexport interface RelayHooks {\n onBatchClaimed?(count: number): void;\n onPublished?(result: PublishResult): void;\n onFailed?(record: OutboxRecord, error: Error, willRetry: boolean): void;\n onDead?(record: OutboxRecord, error: Error): void;\n onError?(error: Error): void;\n}\n","import type { RetryConfig } from \"./types.js\";\n\n/**\n * Compute the delay (ms) before the next retry attempt.\n *\n * @param attempt 1-based attempt number that just failed.\n */\nexport function computeBackoff(config: RetryConfig, attempt: number): number {\n const { strategy, baseMs, maxMs } = config;\n const jitter = config.jitter ?? true;\n\n let delay: number;\n switch (strategy) {\n case \"fixed\":\n delay = baseMs;\n break;\n case \"linear\":\n delay = baseMs * attempt;\n break;\n case \"exponential\":\n // base * 2^(attempt-1), capped to avoid overflow before clamping\n delay = baseMs * 2 ** Math.min(attempt - 1, 30);\n break;\n }\n\n delay = Math.min(delay, maxMs);\n\n if (jitter) {\n // Full jitter: random in [0, delay]. Decorrelates concurrent relays.\n delay = Math.random() * delay;\n }\n\n return Math.floor(delay);\n}\n\n/**\n * Resolve when (Date) the next retry should occur, or null if the\n * record has exhausted its attempts and should go dead.\n */\nexport function nextRetryAt(\n config: RetryConfig,\n attempts: number,\n now: Date = new Date(),\n): Date | null {\n if (attempts >= config.maxAttempts) return null;\n const delay = computeBackoff(config, attempts);\n return new Date(now.getTime() + delay);\n}\n","import type { Logger, OutboxRecord, Serializer } from \"./types.js\";\n\n/**\n * Default serializer: JSON-encodes the record payload to UTF-8 bytes.\n */\nexport class JsonSerializer implements Serializer {\n readonly contentType = \"application/json\";\n\n serialize(record: OutboxRecord): Buffer {\n return Buffer.from(JSON.stringify(record.payload), \"utf8\");\n }\n}\n\n/**\n * Minimal console-backed logger. Swap in pino/winston by implementing Logger.\n */\nexport class ConsoleLogger implements Logger {\n constructor(private readonly prefix = \"[outbox]\") {}\n\n private fmt(\n write: (line: string, meta?: Record<string, unknown>) => void,\n level: string,\n msg: string,\n meta?: Record<string, unknown>,\n ): void {\n const line = `${this.prefix} ${level} ${msg}`;\n if (meta) {\n write(line, meta);\n } else {\n write(line);\n }\n }\n\n debug(msg: string, meta?: Record<string, unknown>): void {\n // eslint-disable-next-line no-console\n this.fmt(console.debug, \"DEBUG\", msg, meta);\n }\n info(msg: string, meta?: Record<string, unknown>): void {\n // eslint-disable-next-line no-console\n this.fmt(console.info, \"INFO\", msg, meta);\n }\n warn(msg: string, meta?: Record<string, unknown>): void {\n // eslint-disable-next-line no-console\n this.fmt(console.warn, \"WARN\", msg, meta);\n }\n error(msg: string, meta?: Record<string, unknown>): void {\n // eslint-disable-next-line no-console\n this.fmt(console.error, \"ERROR\", msg, meta);\n }\n}\n\n/** Logger that discards everything. Useful for tests. */\nexport class NoopLogger implements Logger {\n debug(): void {}\n info(): void {}\n warn(): void {}\n error(): void {}\n}\n","import type { OutboxRecord, PublishableMessage, Serializer } from \"./types.js\";\n\n/**\n * Turn a persisted outbox record into a broker-ready message: serialize the\n * payload and attach the standard correlation headers. Shared by the polling\n * `Relay` and the streaming relay so both produce byte-identical messages.\n */\nexport async function buildPublishable(\n record: OutboxRecord,\n serializer: Serializer,\n): Promise<PublishableMessage> {\n const value = await serializer.serialize(record);\n const headers: Record<string, string> = {\n ...record.headers,\n \"content-type\": serializer.contentType,\n \"message-id\": record.messageId,\n \"aggregate-type\": record.aggregateType,\n \"aggregate-id\": record.aggregateId,\n };\n if (record.traceId) headers[\"trace-id\"] = record.traceId;\n\n return {\n topic: record.topic,\n key: record.key ?? record.aggregateId,\n value,\n headers,\n recordId: record.id,\n messageId: record.messageId,\n };\n}\n","import { computeBackoff } from \"./backoff.js\";\nimport { buildPublishable } from \"./publishable.js\";\nimport { ConsoleLogger, JsonSerializer } from \"./serializer.js\";\nimport type {\n DlqConfig,\n Logger,\n OutboxRecord,\n OutboxStore,\n PublishableMessage,\n PublishErrorKind,\n Publisher,\n RelayHooks,\n RetryConfig,\n Serializer,\n Waker,\n} from \"./types.js\";\n\nexport interface RelayOptions {\n store: OutboxStore;\n publisher: Publisher;\n /** Messages claimed per poll iteration. Default 100. */\n batchSize?: number;\n /** Idle wait (ms) when a poll returns no work. Default 200. */\n pollIntervalMs?: number;\n retry?: Partial<RetryConfig>;\n dlq?: DlqConfig;\n serializer?: Serializer;\n logger?: Logger;\n hooks?: RelayHooks;\n /**\n * Optional low-latency wake source. When provided, the relay claims as soon as\n * the waker signals new work, instead of waiting out `pollIntervalMs`. Polling\n * stays on as a safety net, so set `pollIntervalMs` longer when using a waker.\n */\n waker?: Waker;\n}\n\nconst DEFAULT_RETRY: RetryConfig = {\n maxAttempts: 5,\n strategy: \"exponential\",\n baseMs: 200,\n maxMs: 30_000,\n jitter: true,\n};\n\n/**\n * The Relay drains the outbox store and publishes messages to the broker.\n *\n * It is safe to run multiple Relay instances concurrently against the same\n * store as long as the store's claimBatch uses a lock-free claim strategy\n * (e.g. SELECT ... FOR UPDATE SKIP LOCKED).\n */\nexport class Relay {\n private readonly store: OutboxStore;\n private readonly publisher: Publisher;\n private readonly batchSize: number;\n private readonly pollIntervalMs: number;\n private readonly retry: RetryConfig;\n private readonly dlq: DlqConfig;\n private readonly serializer: Serializer;\n private readonly log: Logger;\n private readonly hooks: RelayHooks;\n private readonly waker: Waker | null;\n\n private running = false;\n private stopping = false;\n private loopPromise: Promise<void> | null = null;\n\n // Interruptible idle wait: `signal()` wakes a pending wait (or marks one\n // pending if none is in flight, so a wake can't be lost between cycles).\n private wakePending = false;\n private wakeResolver: (() => void) | null = null;\n\n constructor(opts: RelayOptions) {\n this.store = opts.store;\n this.publisher = opts.publisher;\n this.batchSize = opts.batchSize ?? 100;\n this.pollIntervalMs = opts.pollIntervalMs ?? 200;\n this.retry = { ...DEFAULT_RETRY, ...opts.retry };\n this.dlq = opts.dlq ?? {};\n this.serializer = opts.serializer ?? new JsonSerializer();\n this.log = opts.logger ?? new ConsoleLogger();\n this.hooks = opts.hooks ?? {};\n this.waker = opts.waker ?? null;\n }\n\n async start(): Promise<void> {\n if (this.running) return;\n this.running = true;\n this.stopping = false;\n\n await this.store.init?.();\n await this.publisher.connect();\n await this.waker?.start(() => this.signal());\n this.log.info(\"relay started\", {\n batchSize: this.batchSize,\n pollIntervalMs: this.pollIntervalMs,\n waker: this.waker !== null,\n });\n\n this.loopPromise = this.loop();\n }\n\n /**\n * Stop accepting new work, finish the in-flight batch, then disconnect.\n */\n async stop(): Promise<void> {\n if (!this.running) return;\n this.stopping = true;\n this.signal(); // break any in-flight idle wait so shutdown is immediate\n this.log.info(\"relay stopping, draining in-flight batch\");\n await this.loopPromise;\n await this.waker?.stop();\n await this.publisher.disconnect();\n await this.store.close?.();\n this.running = false;\n this.log.info(\"relay stopped\");\n }\n\n private async loop(): Promise<void> {\n while (!this.stopping) {\n try {\n const processed = await this.tick();\n if (processed === 0 && !this.stopping) {\n await this.waitForWork(this.pollIntervalMs);\n }\n } catch (err) {\n const error = err instanceof Error ? err : new Error(String(err));\n this.log.error(\"relay loop error\", { error: error.message });\n this.hooks.onError?.(error);\n await this.waitForWork(this.pollIntervalMs);\n }\n }\n }\n\n /**\n * Run a single claim+publish cycle. Returns number of records processed.\n * Exposed for tests / manual single-shot draining.\n */\n async tick(): Promise<number> {\n const batch = await this.store.claimBatch(this.batchSize);\n if (batch.length === 0) return 0;\n\n this.hooks.onBatchClaimed?.(batch.length);\n this.log.debug(\"batch claimed\", { count: batch.length });\n\n const messages = await this.toPublishable(batch);\n const recordsById = new Map(batch.map((r) => [r.id, r]));\n\n const results = await this.publisher.publish(messages);\n\n const succeeded: string[] = [];\n for (const result of results) {\n const record = recordsById.get(result.recordId);\n if (!record) continue;\n\n if (result.ok) {\n succeeded.push(record.id);\n this.hooks.onPublished?.(result);\n } else {\n await this.handleFailure(\n record,\n result.error ?? new Error(\"unknown publish error\"),\n result.errorKind,\n );\n }\n }\n\n if (succeeded.length > 0) {\n await this.store.markDone(succeeded);\n }\n\n return batch.length;\n }\n\n private async handleFailure(\n record: OutboxRecord,\n error: Error,\n errorKind?: PublishErrorKind,\n ): Promise<void> {\n // Backpressure: client-side producer queue is full. Re-queue the record\n // with a brief delay; do NOT increment attempts (this isn't the record's\n // fault, the producer is just full). Stores without `requeue` fall back\n // to markFailed, accepting the attempt-counter hit.\n if (errorKind === \"backpressure\") {\n const delay = this.retry.backpressureDelayMs ?? 1000;\n const retryAt = new Date(Date.now() + delay);\n this.hooks.onFailed?.(record, error, true);\n this.log.warn(\"publish backpressure — requeueing without bumping attempts\", {\n recordId: record.id,\n retryAt: retryAt.toISOString(),\n errorKind,\n error: error.message,\n });\n if (this.store.requeue) {\n await this.store.requeue(record.id, retryAt);\n } else {\n // Fallback: markFailed (will increment attempts). Documented in the\n // OutboxStore.requeue JSDoc.\n await this.store.markFailed(record.id, retryAt, \"failed\");\n }\n return;\n }\n\n const attempts = record.attempts + 1;\n // fatal/poison short-circuit: retrying cannot help (auth denied, fenced\n // epoch, oversized record, schema rejected). Skip the backoff schedule\n // entirely and go straight to DLQ + dead.\n const isTerminalKind = errorKind === \"fatal\" || errorKind === \"poison\";\n const retryAt = isTerminalKind\n ? null\n : this.nextRetryAtForKind(attempts, errorKind);\n const willRetry = retryAt !== null;\n\n this.hooks.onFailed?.(record, error, willRetry);\n this.log.warn(\"publish failed\", {\n recordId: record.id,\n attempts,\n willRetry,\n errorKind: errorKind ?? \"retriable\",\n error: error.message,\n });\n\n if (willRetry) {\n await this.store.markFailed(record.id, retryAt, \"failed\");\n return;\n }\n\n // Terminal: route to DLQ if configured, then mark dead.\n if (this.dlq.topic && this.publisher.publishToDlq) {\n try {\n const msg = (await this.toPublishable([record]))[0];\n if (msg) {\n await this.publisher.publishToDlq(\n {\n ...msg,\n topic: this.dlq.topic,\n // Per-record enrichment so the DLQ consumer has everything\n // needed for triage: original destination, aggregate / message\n // identity, the attempts count, and (opt-in) a truncated stack.\n headers: this.buildDlqHeaders(record, error, msg.headers),\n },\n error,\n );\n }\n } catch (dlqErr) {\n const e = dlqErr instanceof Error ? dlqErr : new Error(String(dlqErr));\n this.log.error(\"DLQ publish failed\", {\n recordId: record.id,\n error: e.message,\n });\n }\n }\n\n await this.store.markFailed(record.id, null, \"dead\");\n this.hooks.onDead?.(record, error);\n }\n\n /**\n * Compute the next retry instant, applying the quota multiplier when the\n * driver classified the failure as a server throttle. Quota failures DO\n * count as attempts (unlike backpressure) — the multiplier only stretches\n * the delay, not the budget.\n */\n private nextRetryAtForKind(\n attempts: number,\n errorKind: PublishErrorKind | undefined,\n ): Date | null {\n if (attempts >= this.retry.maxAttempts) return null;\n const baseDelay = computeBackoff(this.retry, attempts);\n const multiplier =\n errorKind === \"quota\" ? this.retry.quotaMultiplier ?? 5 : 1;\n const delay = Math.min(baseDelay * multiplier, this.retry.maxMs * 10);\n return new Date(Date.now() + delay);\n }\n\n /**\n * Build the per-record DLQ header bag. Operators triaging the DLQ get the\n * original destination, the aggregate / message identity, attempts count,\n * and optionally a truncated error stack — everything you need to decide\n * whether to re-enqueue, escalate, or drop.\n */\n private buildDlqHeaders(\n record: OutboxRecord,\n error: Error,\n existing: Record<string, string>,\n ): Record<string, string> {\n const out: Record<string, string> = {\n ...existing,\n \"original-topic\": record.topic,\n \"dlq-attempts\": String(record.attempts + 1),\n \"dlq-original-aggregate-id\": record.aggregateId,\n \"dlq-original-message-id\": record.messageId,\n };\n if (this.dlq.includeStackTraces && error.stack) {\n const maxBytes = this.dlq.maxStackBytes ?? 4096;\n out[\"dlq-error-stack\"] = truncateUtf8(error.stack, maxBytes);\n }\n return out;\n }\n\n private async toPublishable(\n records: OutboxRecord[],\n ): Promise<PublishableMessage[]> {\n const out: PublishableMessage[] = [];\n for (const record of records) {\n out.push(await buildPublishable(record, this.serializer));\n }\n return out;\n }\n\n /** Wake a pending idle wait, or mark one pending so the next wait is skipped. */\n private signal(): void {\n this.wakePending = true;\n this.wakeResolver?.();\n }\n\n /**\n * Idle wait that resolves after `ms`, or early when `signal()` fires. A signal\n * raised while no wait is in flight is remembered (wakePending), so a wake that\n * races a claim cycle is never lost.\n */\n private waitForWork(ms: number): Promise<void> {\n if (this.wakePending) {\n this.wakePending = false;\n return Promise.resolve();\n }\n return new Promise((resolve) => {\n const timer = setTimeout(() => {\n this.wakeResolver = null;\n resolve();\n }, ms);\n this.wakeResolver = () => {\n clearTimeout(timer);\n this.wakeResolver = null;\n this.wakePending = false;\n resolve();\n };\n });\n }\n}\n\n/**\n * Truncate a string to at most `maxBytes` of its UTF-8 encoding, appending a\n * \"…[truncated]\" marker when bytes are removed. Used to keep the DLQ stack\n * header bounded — headers in Kafka are byte-sized, not character-sized, so\n * a naive `slice` on multibyte text can blow the limit. Always returns a\n * valid (no-broken-codepoint) UTF-8 string.\n */\nfunction truncateUtf8(input: string, maxBytes: number): string {\n const marker = \"…[truncated]\";\n const markerBytes = Buffer.byteLength(marker, \"utf8\");\n const buf = Buffer.from(input, \"utf8\");\n if (buf.byteLength <= maxBytes) return input;\n const budget = Math.max(0, maxBytes - markerBytes);\n // Slice and decode; if the slice lands mid-codepoint, drop trailing bytes\n // until the decode is clean.\n let end = budget;\n while (end > 0) {\n const slice = buf.subarray(0, end).toString(\"utf8\");\n if (!slice.endsWith(\"�\")) {\n return slice + marker;\n }\n end--;\n }\n return marker;\n}\n","import type { StandardSchemaV1 } from \"./standard-schema.js\";\n\n/**\n * Thrown when an outbox payload fails its topic's schema — at `enqueue`\n * (before the DB insert) or at `decode` (malformed bytes / schema mismatch).\n * Carries the topic and the validator's structured issues for diagnostics.\n */\nexport class OutboxValidationError extends Error {\n readonly topic: string;\n readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;\n\n constructor(\n topic: string,\n issues: ReadonlyArray<StandardSchemaV1.Issue>,\n options?: { cause?: unknown },\n ) {\n const first = issues[0]?.message ?? \"unknown validation error\";\n super(`Outbox payload for \"${topic}\" failed validation: ${first}`, options);\n this.name = \"OutboxValidationError\";\n this.topic = topic;\n this.issues = issues;\n }\n}\n","import { OutboxValidationError } from \"./errors.js\";\nimport type { StandardSchemaV1 } from \"./standard-schema.js\";\nimport type { OutboxMessageInput } from \"./types.js\";\n\n/** One topic's contract: the aggregate it belongs to and its payload schema. */\nexport interface TopicDefinition {\n /** Aggregate type stamped on every event of this topic (e.g. \"order\"). */\n readonly aggregateType: string;\n /** Standard Schema for the payload (Zod 3.24+/Valibot/ArkType/…). */\n readonly schema: StandardSchemaV1;\n}\n\n/** A map of topic name -> its definition. The single source of truth. */\nexport type OutboxRegistry = Record<string, TopicDefinition>;\n\n/**\n * Minimal store surface the producer facade needs. `PostgresStore` satisfies\n * this structurally, so the facade stays DB-agnostic and `tx` flows from the\n * concrete store (e.g. a pg client).\n */\nexport interface EnqueueableStore<Tx = unknown> {\n enqueue(tx: Tx, msg: OutboxMessageInput & { traceId?: string }): Promise<string>;\n}\n\ntype TxOf<S> = S extends EnqueueableStore<infer Tx> ? Tx : never;\n\ntype PayloadInput<R extends OutboxRegistry, K extends keyof R> =\n StandardSchemaV1.InferInput<R[K][\"schema\"]>;\ntype PayloadOutput<R extends OutboxRegistry, K extends keyof R> =\n StandardSchemaV1.InferOutput<R[K][\"schema\"]>;\n\n/** The write-side input for a typed enqueue (payload is the schema's input type). */\nexport interface EnqueueInput<R extends OutboxRegistry, K extends keyof R> {\n aggregateId: string;\n payload: PayloadInput<R, K>;\n key?: string;\n headers?: Record<string, string>;\n messageId?: string;\n traceId?: string;\n}\n\n/** Consumer-side facade: validate/decode without a store. */\nexport interface OutboxConsumer<R extends OutboxRegistry> {\n /** Validate an already-parsed value against the topic's schema. */\n validate<K extends keyof R & string>(\n topic: K,\n value: unknown,\n ): Promise<PayloadOutput<R, K>>;\n /** JSON-parse `bytes`, validate against the topic's schema, return the payload. */\n decode<K extends keyof R & string>(\n topic: K,\n bytes: Buffer | Uint8Array | string,\n ): Promise<PayloadOutput<R, K>>;\n}\n\n/** Producer-side facade: adds typed, validated enqueue. */\nexport interface OutboxProducer<R extends OutboxRegistry, Tx>\n extends OutboxConsumer<R> {\n /** Validate `payload`, then enqueue inside the caller's transaction `tx`. */\n enqueue<K extends keyof R & string>(\n tx: Tx,\n topic: K,\n msg: EnqueueInput<R, K>,\n ): Promise<string>;\n}\n\n// Loose shape used inside the implementation; the public types come from overloads.\ninterface LooseEnqueueInput {\n aggregateId: string;\n payload: unknown;\n key?: string;\n headers?: Record<string, string>;\n messageId?: string;\n traceId?: string;\n}\n\nexport function defineOutbox<R extends OutboxRegistry>(\n registry: R,\n): OutboxConsumer<R>;\nexport function defineOutbox<R extends OutboxRegistry, S extends EnqueueableStore>(\n registry: R,\n opts: { store: S },\n): OutboxProducer<R, TxOf<S>>;\nexport function defineOutbox<R extends OutboxRegistry>(\n registry: R,\n opts?: { store: EnqueueableStore },\n): OutboxConsumer<R> | OutboxProducer<R, unknown> {\n const validate = async (topic: string, value: unknown): Promise<unknown> => {\n const def = registry[topic];\n if (!def) {\n throw new OutboxValidationError(topic, [\n { message: `unknown topic \"${topic}\"` },\n ]);\n }\n const result = await def.schema[\"~standard\"].validate(value);\n if (result.issues) throw new OutboxValidationError(topic, result.issues);\n return result.value;\n };\n\n const decode = async (\n topic: string,\n bytes: Buffer | Uint8Array | string,\n ): Promise<unknown> => {\n const text =\n typeof bytes === \"string\" ? bytes : Buffer.from(bytes).toString(\"utf8\");\n let parsed: unknown;\n try {\n parsed = JSON.parse(text);\n } catch (err) {\n throw new OutboxValidationError(\n topic,\n [{ message: `invalid JSON: ${(err as Error).message}` }],\n { cause: err },\n );\n }\n return validate(topic, parsed);\n };\n\n const consumer = { validate, decode } as unknown as OutboxConsumer<R>;\n if (!opts?.store) return consumer;\n\n const store = opts.store;\n const enqueue = async (\n tx: unknown,\n topic: string,\n msg: LooseEnqueueInput,\n ): Promise<string> => {\n const def = registry[topic];\n if (!def) {\n throw new OutboxValidationError(topic, [\n { message: `unknown topic \"${topic}\"` },\n ]);\n }\n const payload = await validate(topic, msg.payload);\n return store.enqueue(tx, {\n topic,\n aggregateType: def.aggregateType,\n aggregateId: msg.aggregateId,\n payload,\n key: msg.key,\n headers: msg.headers,\n messageId: msg.messageId,\n traceId: msg.traceId,\n });\n };\n\n return { validate, decode, enqueue } as unknown as OutboxProducer<R, unknown>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACWO,IAAM,qBAAmD;AAAA,EAC9D,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,MAAM;AACR;AAEO,IAAM,0BAAwD;AAAA,EACnE,GAAG;AAAA,EACH,GAAG;AAAA,EACH,GAAG;AAAA,EACH,GAAG;AAAA,EACH,GAAG;AACL;;;AClBO,SAAS,eAAe,QAAqB,SAAyB;AAC3E,QAAM,EAAE,UAAU,QAAQ,MAAM,IAAI;AACpC,QAAM,SAAS,OAAO,UAAU;AAEhC,MAAI;AACJ,UAAQ,UAAU;AAAA,IAChB,KAAK;AACH,cAAQ;AACR;AAAA,IACF,KAAK;AACH,cAAQ,SAAS;AACjB;AAAA,IACF,KAAK;AAEH,cAAQ,SAAS,KAAK,KAAK,IAAI,UAAU,GAAG,EAAE;AAC9C;AAAA,EACJ;AAEA,UAAQ,KAAK,IAAI,OAAO,KAAK;AAE7B,MAAI,QAAQ;AAEV,YAAQ,KAAK,OAAO,IAAI;AAAA,EAC1B;AAEA,SAAO,KAAK,MAAM,KAAK;AACzB;AAMO,SAAS,YACd,QACA,UACA,MAAY,oBAAI,KAAK,GACR;AACb,MAAI,YAAY,OAAO,YAAa,QAAO;AAC3C,QAAM,QAAQ,eAAe,QAAQ,QAAQ;AAC7C,SAAO,IAAI,KAAK,IAAI,QAAQ,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;AAAA,IACF;AACA,WAAO,SAAS,OAAO,MAAM;AAAA,EAC/B;AAEA,QAAM,WAAW,EAAE,UAAU,OAAO;AACpC,MAAI,CAAC,MAAM,MAAO,QAAO;AAEzB,QAAM,QAAQ,KAAK;AACnB,QAAM,UAAU,OACd,IACA,OACA,QACoB;AACpB,UAAM,MAAM,SAAS,KAAK;AAC1B,QAAI,CAAC,KAAK;AACR,YAAM,IAAI,sBAAsB,OAAO;AAAA,QACrC,EAAE,SAAS,kBAAkB,KAAK,IAAI;AAAA,MACxC,CAAC;AAAA,IACH;AACA,UAAM,UAAU,MAAM,SAAS,OAAO,IAAI,OAAO;AACjD,WAAO,MAAM,QAAQ,IAAI;AAAA,MACvB;AAAA,MACA,eAAe,IAAI;AAAA,MACnB,aAAa,IAAI;AAAA,MACjB;AAAA,MACA,KAAK,IAAI;AAAA,MACT,SAAS,IAAI;AAAA,MACb,WAAW,IAAI;AAAA,MACf,SAAS,IAAI;AAAA,IACf,CAAC;AAAA,EACH;AAEA,SAAO,EAAE,UAAU,QAAQ,QAAQ;AACrC;","names":["retryAt"]}
package/dist/index.d.cts CHANGED
@@ -147,8 +147,20 @@ interface OutboxStore {
147
147
  /**
148
148
  * Mark a record as failed and schedule its next retry.
149
149
  * `nextRetryAt` of null + status "dead" means terminal.
150
+ * Implementations MUST increment `attempts` so the retry budget is honored.
150
151
  */
151
152
  markFailed(recordId: string, nextRetryAt: Date | null, status: "failed" | "dead"): Promise<void>;
153
+ /**
154
+ * Re-queue a record back to `failed` with the given `retryAt` **WITHOUT
155
+ * incrementing attempts**. Used by the relay for backpressure handling —
156
+ * a client-side queue-full failure is a "slow down" signal, not a
157
+ * record-specific failure, and counting it as a retry would burn the
158
+ * attempt budget unfairly.
159
+ *
160
+ * Optional: stores that don't implement this fall back to `markFailed`
161
+ * (which increments attempts, with the caveat documented above).
162
+ */
163
+ requeue?(recordId: string, retryAt: Date): Promise<void>;
152
164
  /** Best-effort lifecycle hooks; no-op allowed. */
153
165
  init?(): Promise<void>;
154
166
  close?(): Promise<void>;
@@ -179,10 +191,41 @@ interface RetryConfig {
179
191
  maxMs: number;
180
192
  /** Add random jitter (0..baseMs) to avoid thundering herd. Default true. */
181
193
  jitter?: boolean;
194
+ /**
195
+ * Delay (ms) before re-queueing a record whose publish was rejected with
196
+ * `errorKind: "backpressure"` (client-side producer buffer full).
197
+ *
198
+ * Backpressure failures do NOT count as a failed attempt — the buffer
199
+ * being full is a "slow down" signal, not a record-specific failure.
200
+ * The record is requeued at the next interval, not promoted to dead.
201
+ *
202
+ * Default 1000 ms. Requires the {@link OutboxStore} to implement
203
+ * `requeue` — stores without it fall back to {@link OutboxStore.markFailed}
204
+ * which DOES increment attempts.
205
+ */
206
+ backpressureDelayMs?: number;
207
+ /**
208
+ * Multiplier applied to the computed backoff for records rejected with
209
+ * `errorKind: "quota"` (broker is throttling the producer). Default 5 —
210
+ * a quota signal asks for a longer breath than a generic transient error.
211
+ * Quota failures DO count as attempts (unlike backpressure).
212
+ */
213
+ quotaMultiplier?: number;
182
214
  }
183
215
  interface DlqConfig {
184
216
  /** Topic to route dead messages to. If absent, dead messages are parked. */
185
217
  topic?: string;
218
+ /**
219
+ * Include a truncated stack trace as the `dlq-error-stack` header when
220
+ * routing a record to the DLQ. Default false — keep DLQ messages small
221
+ * by default; opt in if your triage workflow needs the stack.
222
+ */
223
+ includeStackTraces?: boolean;
224
+ /**
225
+ * Maximum bytes of the truncated stack trace included when
226
+ * `includeStackTraces` is on. Default 4096.
227
+ */
228
+ maxStackBytes?: number;
186
229
  }
187
230
  /**
188
231
  * Optional trace-context propagator. `inject` writes the active trace context
@@ -326,6 +369,20 @@ declare class Relay {
326
369
  */
327
370
  tick(): Promise<number>;
328
371
  private handleFailure;
372
+ /**
373
+ * Compute the next retry instant, applying the quota multiplier when the
374
+ * driver classified the failure as a server throttle. Quota failures DO
375
+ * count as attempts (unlike backpressure) — the multiplier only stretches
376
+ * the delay, not the budget.
377
+ */
378
+ private nextRetryAtForKind;
379
+ /**
380
+ * Build the per-record DLQ header bag. Operators triaging the DLQ get the
381
+ * original destination, the aggregate / message identity, attempts count,
382
+ * and optionally a truncated error stack — everything you need to decide
383
+ * whether to re-enqueue, escalate, or drop.
384
+ */
385
+ private buildDlqHeaders;
329
386
  private toPublishable;
330
387
  /** Wake a pending idle wait, or mark one pending so the next wait is skipped. */
331
388
  private signal;
package/dist/index.d.ts CHANGED
@@ -147,8 +147,20 @@ interface OutboxStore {
147
147
  /**
148
148
  * Mark a record as failed and schedule its next retry.
149
149
  * `nextRetryAt` of null + status "dead" means terminal.
150
+ * Implementations MUST increment `attempts` so the retry budget is honored.
150
151
  */
151
152
  markFailed(recordId: string, nextRetryAt: Date | null, status: "failed" | "dead"): Promise<void>;
153
+ /**
154
+ * Re-queue a record back to `failed` with the given `retryAt` **WITHOUT
155
+ * incrementing attempts**. Used by the relay for backpressure handling —
156
+ * a client-side queue-full failure is a "slow down" signal, not a
157
+ * record-specific failure, and counting it as a retry would burn the
158
+ * attempt budget unfairly.
159
+ *
160
+ * Optional: stores that don't implement this fall back to `markFailed`
161
+ * (which increments attempts, with the caveat documented above).
162
+ */
163
+ requeue?(recordId: string, retryAt: Date): Promise<void>;
152
164
  /** Best-effort lifecycle hooks; no-op allowed. */
153
165
  init?(): Promise<void>;
154
166
  close?(): Promise<void>;
@@ -179,10 +191,41 @@ interface RetryConfig {
179
191
  maxMs: number;
180
192
  /** Add random jitter (0..baseMs) to avoid thundering herd. Default true. */
181
193
  jitter?: boolean;
194
+ /**
195
+ * Delay (ms) before re-queueing a record whose publish was rejected with
196
+ * `errorKind: "backpressure"` (client-side producer buffer full).
197
+ *
198
+ * Backpressure failures do NOT count as a failed attempt — the buffer
199
+ * being full is a "slow down" signal, not a record-specific failure.
200
+ * The record is requeued at the next interval, not promoted to dead.
201
+ *
202
+ * Default 1000 ms. Requires the {@link OutboxStore} to implement
203
+ * `requeue` — stores without it fall back to {@link OutboxStore.markFailed}
204
+ * which DOES increment attempts.
205
+ */
206
+ backpressureDelayMs?: number;
207
+ /**
208
+ * Multiplier applied to the computed backoff for records rejected with
209
+ * `errorKind: "quota"` (broker is throttling the producer). Default 5 —
210
+ * a quota signal asks for a longer breath than a generic transient error.
211
+ * Quota failures DO count as attempts (unlike backpressure).
212
+ */
213
+ quotaMultiplier?: number;
182
214
  }
183
215
  interface DlqConfig {
184
216
  /** Topic to route dead messages to. If absent, dead messages are parked. */
185
217
  topic?: string;
218
+ /**
219
+ * Include a truncated stack trace as the `dlq-error-stack` header when
220
+ * routing a record to the DLQ. Default false — keep DLQ messages small
221
+ * by default; opt in if your triage workflow needs the stack.
222
+ */
223
+ includeStackTraces?: boolean;
224
+ /**
225
+ * Maximum bytes of the truncated stack trace included when
226
+ * `includeStackTraces` is on. Default 4096.
227
+ */
228
+ maxStackBytes?: number;
186
229
  }
187
230
  /**
188
231
  * Optional trace-context propagator. `inject` writes the active trace context
@@ -326,6 +369,20 @@ declare class Relay {
326
369
  */
327
370
  tick(): Promise<number>;
328
371
  private handleFailure;
372
+ /**
373
+ * Compute the next retry instant, applying the quota multiplier when the
374
+ * driver classified the failure as a server throttle. Quota failures DO
375
+ * count as attempts (unlike backpressure) — the multiplier only stretches
376
+ * the delay, not the budget.
377
+ */
378
+ private nextRetryAtForKind;
379
+ /**
380
+ * Build the per-record DLQ header bag. Operators triaging the DLQ get the
381
+ * original destination, the aggregate / message identity, attempts count,
382
+ * and optionally a truncated error stack — everything you need to decide
383
+ * whether to re-enqueue, escalate, or drop.
384
+ */
385
+ private buildDlqHeaders;
329
386
  private toPublishable;
330
387
  /** Wake a pending idle wait, or mark one pending so the next wait is skipped. */
331
388
  private signal;
package/dist/index.js CHANGED
@@ -222,9 +222,26 @@ var Relay = class {
222
222
  return batch.length;
223
223
  }
224
224
  async handleFailure(record, error, errorKind) {
225
+ if (errorKind === "backpressure") {
226
+ const delay = this.retry.backpressureDelayMs ?? 1e3;
227
+ const retryAt2 = new Date(Date.now() + delay);
228
+ this.hooks.onFailed?.(record, error, true);
229
+ this.log.warn("publish backpressure \u2014 requeueing without bumping attempts", {
230
+ recordId: record.id,
231
+ retryAt: retryAt2.toISOString(),
232
+ errorKind,
233
+ error: error.message
234
+ });
235
+ if (this.store.requeue) {
236
+ await this.store.requeue(record.id, retryAt2);
237
+ } else {
238
+ await this.store.markFailed(record.id, retryAt2, "failed");
239
+ }
240
+ return;
241
+ }
225
242
  const attempts = record.attempts + 1;
226
243
  const isTerminalKind = errorKind === "fatal" || errorKind === "poison";
227
- const retryAt = isTerminalKind ? null : nextRetryAt(this.retry, attempts);
244
+ const retryAt = isTerminalKind ? null : this.nextRetryAtForKind(attempts, errorKind);
228
245
  const willRetry = retryAt !== null;
229
246
  this.hooks.onFailed?.(record, error, willRetry);
230
247
  this.log.warn("publish failed", {
@@ -246,9 +263,10 @@ var Relay = class {
246
263
  {
247
264
  ...msg,
248
265
  topic: this.dlq.topic,
249
- // Preserve the original destination so the publisher can record
250
- // it as a header; otherwise it is lost when we overwrite `topic`.
251
- headers: { ...msg.headers, "original-topic": record.topic }
266
+ // Per-record enrichment so the DLQ consumer has everything
267
+ // needed for triage: original destination, aggregate / message
268
+ // identity, the attempts count, and (opt-in) a truncated stack.
269
+ headers: this.buildDlqHeaders(record, error, msg.headers)
252
270
  },
253
271
  error
254
272
  );
@@ -264,6 +282,39 @@ var Relay = class {
264
282
  await this.store.markFailed(record.id, null, "dead");
265
283
  this.hooks.onDead?.(record, error);
266
284
  }
285
+ /**
286
+ * Compute the next retry instant, applying the quota multiplier when the
287
+ * driver classified the failure as a server throttle. Quota failures DO
288
+ * count as attempts (unlike backpressure) — the multiplier only stretches
289
+ * the delay, not the budget.
290
+ */
291
+ nextRetryAtForKind(attempts, errorKind) {
292
+ if (attempts >= this.retry.maxAttempts) return null;
293
+ const baseDelay = computeBackoff(this.retry, attempts);
294
+ const multiplier = errorKind === "quota" ? this.retry.quotaMultiplier ?? 5 : 1;
295
+ const delay = Math.min(baseDelay * multiplier, this.retry.maxMs * 10);
296
+ return new Date(Date.now() + delay);
297
+ }
298
+ /**
299
+ * Build the per-record DLQ header bag. Operators triaging the DLQ get the
300
+ * original destination, the aggregate / message identity, attempts count,
301
+ * and optionally a truncated error stack — everything you need to decide
302
+ * whether to re-enqueue, escalate, or drop.
303
+ */
304
+ buildDlqHeaders(record, error, existing) {
305
+ const out = {
306
+ ...existing,
307
+ "original-topic": record.topic,
308
+ "dlq-attempts": String(record.attempts + 1),
309
+ "dlq-original-aggregate-id": record.aggregateId,
310
+ "dlq-original-message-id": record.messageId
311
+ };
312
+ if (this.dlq.includeStackTraces && error.stack) {
313
+ const maxBytes = this.dlq.maxStackBytes ?? 4096;
314
+ out["dlq-error-stack"] = truncateUtf8(error.stack, maxBytes);
315
+ }
316
+ return out;
317
+ }
267
318
  async toPublishable(records) {
268
319
  const out = [];
269
320
  for (const record of records) {
@@ -300,6 +351,22 @@ var Relay = class {
300
351
  });
301
352
  }
302
353
  };
354
+ function truncateUtf8(input, maxBytes) {
355
+ const marker = "\u2026[truncated]";
356
+ const markerBytes = Buffer.byteLength(marker, "utf8");
357
+ const buf = Buffer.from(input, "utf8");
358
+ if (buf.byteLength <= maxBytes) return input;
359
+ const budget = Math.max(0, maxBytes - markerBytes);
360
+ let end = budget;
361
+ while (end > 0) {
362
+ const slice = buf.subarray(0, end).toString("utf8");
363
+ if (!slice.endsWith("\uFFFD")) {
364
+ return slice + marker;
365
+ }
366
+ end--;
367
+ }
368
+ return marker;
369
+ }
303
370
 
304
371
  // src/errors.ts
305
372
  var OutboxValidationError = class extends Error {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/types.ts","../src/backoff.ts","../src/serializer.ts","../src/publishable.ts","../src/relay.ts","../src/errors.ts","../src/registry.ts"],"sourcesContent":["/**\n * Status lifecycle of an outbox record.\n *\n * pending -> freshly enqueued, awaiting first publish\n * processing -> claimed by a relay instance, publish in flight\n * done -> successfully published to the broker\n * failed -> publish failed, awaiting retry (next_retry_at)\n * dead -> exhausted retries, routed to DLQ (or parked)\n */\nexport type OutboxStatus = \"pending\" | \"processing\" | \"done\" | \"failed\" | \"dead\";\n\nexport const OUTBOX_STATUS_CODE: Record<OutboxStatus, number> = {\n pending: 0,\n processing: 1,\n done: 2,\n failed: 3,\n dead: 4,\n};\n\nexport const OUTBOX_STATUS_FROM_CODE: Record<number, OutboxStatus> = {\n 0: \"pending\",\n 1: \"processing\",\n 2: \"done\",\n 3: \"failed\",\n 4: \"dead\",\n};\n\n/**\n * A message as enqueued by the application inside its own DB transaction.\n * This is the write-side input — no infra concerns leak in here.\n */\nexport interface OutboxMessageInput {\n /** Logical topic the message will be published to. */\n topic: string;\n /** Type of the aggregate that produced this event (e.g. \"order\"). */\n aggregateType: string;\n /**\n * Identifier of the aggregate instance (e.g. order id).\n * Used as the default partition key to preserve per-aggregate ordering.\n */\n aggregateId: string;\n /** Event payload. Serialized by the configured serializer. */\n payload: unknown;\n /** Optional explicit partition key. Falls back to aggregateId. */\n key?: string;\n /** Optional message headers. */\n headers?: Record<string, string>;\n /**\n * Optional client-supplied message id for idempotency / dedup.\n * If omitted, the store generates one.\n */\n messageId?: string;\n}\n\n/**\n * A persisted outbox record as read back by the relay.\n */\nexport interface OutboxRecord {\n id: string;\n messageId: string;\n topic: string;\n aggregateType: string;\n aggregateId: string;\n key: string | null;\n payload: unknown;\n headers: Record<string, string>;\n traceId: string | null;\n status: OutboxStatus;\n attempts: number;\n nextRetryAt: Date | null;\n createdAt: Date;\n processedAt: Date | null;\n}\n\n/**\n * The message handed to a Publisher after serialization.\n */\nexport interface PublishableMessage {\n topic: string;\n key: string | null;\n /** Serialized payload bytes. */\n value: Buffer;\n headers: Record<string, string>;\n /** Original record id, for correlation in publish results. */\n recordId: string;\n messageId: string;\n /**\n * Explicit partition override. When set, the publisher MUST route the\n * record to this exact partition, bypassing the configured partitioner.\n *\n * Use cases:\n * - Compacted topics with application-managed sharding.\n * - Tenant-affinity routing where you compute the partition yourself.\n * - Geo-pinning records to a specific broker.\n *\n * When omitted (the default), the underlying client's partitioner\n * decides — usually a hash of `key`, falling back to sticky round-robin\n * when `key` is null.\n */\n partition?: number;\n}\n\n/**\n * Why a publish failed, in terms the relay can act on. Drivers classify their\n * native errors into one of these buckets; the relay reads `errorKind` to\n * decide whether to retry, short-circuit to the DLQ, or pause polling. The\n * field is optional for backward compatibility — when absent, the relay\n * treats the error as `\"retriable\"`.\n *\n * - `retriable` — transient (broker unreachable, leader election, request\n * timeout); retry per the configured backoff policy. The default for any\n * unclassified error.\n * - `fatal` — the producer or the credentials are broken (fenced epoch,\n * authentication failed, ACL denied). Retrying cannot help; the relay\n * short-circuits straight to the DLQ + `dead` status.\n * - `poison` — the message itself is rejectable by every broker\n * (oversized record, corrupt payload, schema-registry refused encoding).\n * Same handling as `fatal`: DLQ + dead, no retries.\n * - `backpressure` — the *producer's own* outbound buffer is full\n * (librdkafka `__QUEUE_FULL`). The right response is to slow the relay\n * down, not to burn retries. v2.1 treats this as `retriable` for\n * compatibility; smarter handling (pause polling) is planned.\n * - `quota` — the broker is throttling us (`THROTTLING_QUOTA_EXCEEDED`).\n * Back off with longer delays. v2.1 treats as `retriable`; smarter\n * handling (longer backoff) is planned.\n */\nexport type PublishErrorKind =\n | \"retriable\"\n | \"fatal\"\n | \"poison\"\n | \"backpressure\"\n | \"quota\";\n\n/**\n * Result of attempting to publish a single message.\n */\nexport interface PublishResult {\n recordId: string;\n ok: boolean;\n error?: Error;\n /**\n * Optional classification of `error` for relay-level decision-making. Set\n * by publisher implementations that know how to inspect their native error\n * shapes. Absent value is treated as `\"retriable\"` by the relay (the safe\n * default — at worst we retry an error we should have skipped).\n */\n errorKind?: PublishErrorKind;\n}\n\n/**\n * Pluggable serializer. Default is JSON; users can swap in\n * Avro / Protobuf / Schema-Registry-backed serializers.\n */\nexport interface Serializer {\n serialize(message: OutboxRecord): Buffer | Promise<Buffer>;\n /** Content-type header value advertised for this serializer. */\n readonly contentType: string;\n}\n\n/**\n * Storage abstraction. Implemented per-database (postgres, mysql, ...).\n * The relay only talks to the store through this interface.\n */\nexport interface OutboxStore {\n /**\n * Atomically claim up to `batchSize` due messages and mark them\n * as `processing`. Implementations MUST be safe under concurrent\n * relay instances (e.g. SELECT ... FOR UPDATE SKIP LOCKED).\n */\n claimBatch(batchSize: number): Promise<OutboxRecord[]>;\n\n /** Mark records as successfully published. */\n markDone(recordIds: string[]): Promise<void>;\n\n /**\n * Mark a record as failed and schedule its next retry.\n * `nextRetryAt` of null + status \"dead\" means terminal.\n */\n markFailed(\n recordId: string,\n nextRetryAt: Date | null,\n status: \"failed\" | \"dead\",\n ): Promise<void>;\n\n /** Best-effort lifecycle hooks; no-op allowed. */\n init?(): Promise<void>;\n close?(): Promise<void>;\n}\n\n/**\n * Broker abstraction. Implemented per-driver (kafkajs, confluent, ...).\n */\nexport interface Publisher {\n connect(): Promise<void>;\n disconnect(): Promise<void>;\n /**\n * Publish a batch. Returns a per-message result so the relay can\n * mark partial success. Implementations may use a transactional\n * producer to make the batch atomic.\n */\n publish(messages: PublishableMessage[]): Promise<PublishResult[]>;\n /** Route a permanently-failed message to a dead-letter destination. */\n publishToDlq?(message: PublishableMessage, error: Error): Promise<void>;\n}\n\n/**\n * Backoff strategy for retrying failed publishes.\n */\nexport type BackoffStrategy = \"fixed\" | \"linear\" | \"exponential\";\n\nexport interface RetryConfig {\n maxAttempts: number;\n strategy: BackoffStrategy;\n baseMs: number;\n maxMs: number;\n /** Add random jitter (0..baseMs) to avoid thundering herd. Default true. */\n jitter?: boolean;\n}\n\nexport interface DlqConfig {\n /** Topic to route dead messages to. If absent, dead messages are parked. */\n topic?: string;\n}\n\n/**\n * Optional trace-context propagator. `inject` writes the active trace context\n * into the carrier as W3C `traceparent`/`tracestate` (the shape of an\n * OpenTelemetry TextMapPropagator), so it is persisted with the outbox row and\n * carried to the published message. The library depends on no tracing package;\n * provide a thin adapter over yours (OpenTelemetry, Datadog, …).\n */\nexport interface Tracing {\n inject(carrier: Record<string, string>): void;\n}\n\n/**\n * Minimal structured logger. Console-backed default provided.\n */\nexport interface Logger {\n debug(msg: string, meta?: Record<string, unknown>): void;\n info(msg: string, meta?: Record<string, unknown>): void;\n warn(msg: string, meta?: Record<string, unknown>): void;\n error(msg: string, meta?: Record<string, unknown>): void;\n}\n\n/**\n * A low-latency wake source for the relay. Instead of only waking on the poll\n * interval, the relay claims immediately whenever `onWake` fires. The signal is\n * advisory: it may fire spuriously or be missed entirely, and the relay's\n * polling remains the safety net, so implementations need not deduplicate or\n * guarantee delivery. (e.g. a Postgres LISTEN/NOTIFY waker.)\n */\nexport interface Waker {\n start(onWake: () => void): Promise<void>;\n stop(): Promise<void>;\n}\n\n/**\n * Lifecycle / observability hooks emitted by the relay.\n */\nexport interface RelayHooks {\n onBatchClaimed?(count: number): void;\n onPublished?(result: PublishResult): void;\n onFailed?(record: OutboxRecord, error: Error, willRetry: boolean): void;\n onDead?(record: OutboxRecord, error: Error): void;\n onError?(error: Error): void;\n}\n","import type { RetryConfig } from \"./types.js\";\n\n/**\n * Compute the delay (ms) before the next retry attempt.\n *\n * @param attempt 1-based attempt number that just failed.\n */\nexport function computeBackoff(config: RetryConfig, attempt: number): number {\n const { strategy, baseMs, maxMs } = config;\n const jitter = config.jitter ?? true;\n\n let delay: number;\n switch (strategy) {\n case \"fixed\":\n delay = baseMs;\n break;\n case \"linear\":\n delay = baseMs * attempt;\n break;\n case \"exponential\":\n // base * 2^(attempt-1), capped to avoid overflow before clamping\n delay = baseMs * 2 ** Math.min(attempt - 1, 30);\n break;\n }\n\n delay = Math.min(delay, maxMs);\n\n if (jitter) {\n // Full jitter: random in [0, delay]. Decorrelates concurrent relays.\n delay = Math.random() * delay;\n }\n\n return Math.floor(delay);\n}\n\n/**\n * Resolve when (Date) the next retry should occur, or null if the\n * record has exhausted its attempts and should go dead.\n */\nexport function nextRetryAt(\n config: RetryConfig,\n attempts: number,\n now: Date = new Date(),\n): Date | null {\n if (attempts >= config.maxAttempts) return null;\n const delay = computeBackoff(config, attempts);\n return new Date(now.getTime() + delay);\n}\n","import type { Logger, OutboxRecord, Serializer } from \"./types.js\";\n\n/**\n * Default serializer: JSON-encodes the record payload to UTF-8 bytes.\n */\nexport class JsonSerializer implements Serializer {\n readonly contentType = \"application/json\";\n\n serialize(record: OutboxRecord): Buffer {\n return Buffer.from(JSON.stringify(record.payload), \"utf8\");\n }\n}\n\n/**\n * Minimal console-backed logger. Swap in pino/winston by implementing Logger.\n */\nexport class ConsoleLogger implements Logger {\n constructor(private readonly prefix = \"[outbox]\") {}\n\n private fmt(\n write: (line: string, meta?: Record<string, unknown>) => void,\n level: string,\n msg: string,\n meta?: Record<string, unknown>,\n ): void {\n const line = `${this.prefix} ${level} ${msg}`;\n if (meta) {\n write(line, meta);\n } else {\n write(line);\n }\n }\n\n debug(msg: string, meta?: Record<string, unknown>): void {\n // eslint-disable-next-line no-console\n this.fmt(console.debug, \"DEBUG\", msg, meta);\n }\n info(msg: string, meta?: Record<string, unknown>): void {\n // eslint-disable-next-line no-console\n this.fmt(console.info, \"INFO\", msg, meta);\n }\n warn(msg: string, meta?: Record<string, unknown>): void {\n // eslint-disable-next-line no-console\n this.fmt(console.warn, \"WARN\", msg, meta);\n }\n error(msg: string, meta?: Record<string, unknown>): void {\n // eslint-disable-next-line no-console\n this.fmt(console.error, \"ERROR\", msg, meta);\n }\n}\n\n/** Logger that discards everything. Useful for tests. */\nexport class NoopLogger implements Logger {\n debug(): void {}\n info(): void {}\n warn(): void {}\n error(): void {}\n}\n","import type { OutboxRecord, PublishableMessage, Serializer } from \"./types.js\";\n\n/**\n * Turn a persisted outbox record into a broker-ready message: serialize the\n * payload and attach the standard correlation headers. Shared by the polling\n * `Relay` and the streaming relay so both produce byte-identical messages.\n */\nexport async function buildPublishable(\n record: OutboxRecord,\n serializer: Serializer,\n): Promise<PublishableMessage> {\n const value = await serializer.serialize(record);\n const headers: Record<string, string> = {\n ...record.headers,\n \"content-type\": serializer.contentType,\n \"message-id\": record.messageId,\n \"aggregate-type\": record.aggregateType,\n \"aggregate-id\": record.aggregateId,\n };\n if (record.traceId) headers[\"trace-id\"] = record.traceId;\n\n return {\n topic: record.topic,\n key: record.key ?? record.aggregateId,\n value,\n headers,\n recordId: record.id,\n messageId: record.messageId,\n };\n}\n","import { nextRetryAt } from \"./backoff.js\";\nimport { buildPublishable } from \"./publishable.js\";\nimport { ConsoleLogger, JsonSerializer } from \"./serializer.js\";\nimport type {\n DlqConfig,\n Logger,\n OutboxRecord,\n OutboxStore,\n PublishableMessage,\n PublishErrorKind,\n Publisher,\n RelayHooks,\n RetryConfig,\n Serializer,\n Waker,\n} from \"./types.js\";\n\nexport interface RelayOptions {\n store: OutboxStore;\n publisher: Publisher;\n /** Messages claimed per poll iteration. Default 100. */\n batchSize?: number;\n /** Idle wait (ms) when a poll returns no work. Default 200. */\n pollIntervalMs?: number;\n retry?: Partial<RetryConfig>;\n dlq?: DlqConfig;\n serializer?: Serializer;\n logger?: Logger;\n hooks?: RelayHooks;\n /**\n * Optional low-latency wake source. When provided, the relay claims as soon as\n * the waker signals new work, instead of waiting out `pollIntervalMs`. Polling\n * stays on as a safety net, so set `pollIntervalMs` longer when using a waker.\n */\n waker?: Waker;\n}\n\nconst DEFAULT_RETRY: RetryConfig = {\n maxAttempts: 5,\n strategy: \"exponential\",\n baseMs: 200,\n maxMs: 30_000,\n jitter: true,\n};\n\n/**\n * The Relay drains the outbox store and publishes messages to the broker.\n *\n * It is safe to run multiple Relay instances concurrently against the same\n * store as long as the store's claimBatch uses a lock-free claim strategy\n * (e.g. SELECT ... FOR UPDATE SKIP LOCKED).\n */\nexport class Relay {\n private readonly store: OutboxStore;\n private readonly publisher: Publisher;\n private readonly batchSize: number;\n private readonly pollIntervalMs: number;\n private readonly retry: RetryConfig;\n private readonly dlq: DlqConfig;\n private readonly serializer: Serializer;\n private readonly log: Logger;\n private readonly hooks: RelayHooks;\n private readonly waker: Waker | null;\n\n private running = false;\n private stopping = false;\n private loopPromise: Promise<void> | null = null;\n\n // Interruptible idle wait: `signal()` wakes a pending wait (or marks one\n // pending if none is in flight, so a wake can't be lost between cycles).\n private wakePending = false;\n private wakeResolver: (() => void) | null = null;\n\n constructor(opts: RelayOptions) {\n this.store = opts.store;\n this.publisher = opts.publisher;\n this.batchSize = opts.batchSize ?? 100;\n this.pollIntervalMs = opts.pollIntervalMs ?? 200;\n this.retry = { ...DEFAULT_RETRY, ...opts.retry };\n this.dlq = opts.dlq ?? {};\n this.serializer = opts.serializer ?? new JsonSerializer();\n this.log = opts.logger ?? new ConsoleLogger();\n this.hooks = opts.hooks ?? {};\n this.waker = opts.waker ?? null;\n }\n\n async start(): Promise<void> {\n if (this.running) return;\n this.running = true;\n this.stopping = false;\n\n await this.store.init?.();\n await this.publisher.connect();\n await this.waker?.start(() => this.signal());\n this.log.info(\"relay started\", {\n batchSize: this.batchSize,\n pollIntervalMs: this.pollIntervalMs,\n waker: this.waker !== null,\n });\n\n this.loopPromise = this.loop();\n }\n\n /**\n * Stop accepting new work, finish the in-flight batch, then disconnect.\n */\n async stop(): Promise<void> {\n if (!this.running) return;\n this.stopping = true;\n this.signal(); // break any in-flight idle wait so shutdown is immediate\n this.log.info(\"relay stopping, draining in-flight batch\");\n await this.loopPromise;\n await this.waker?.stop();\n await this.publisher.disconnect();\n await this.store.close?.();\n this.running = false;\n this.log.info(\"relay stopped\");\n }\n\n private async loop(): Promise<void> {\n while (!this.stopping) {\n try {\n const processed = await this.tick();\n if (processed === 0 && !this.stopping) {\n await this.waitForWork(this.pollIntervalMs);\n }\n } catch (err) {\n const error = err instanceof Error ? err : new Error(String(err));\n this.log.error(\"relay loop error\", { error: error.message });\n this.hooks.onError?.(error);\n await this.waitForWork(this.pollIntervalMs);\n }\n }\n }\n\n /**\n * Run a single claim+publish cycle. Returns number of records processed.\n * Exposed for tests / manual single-shot draining.\n */\n async tick(): Promise<number> {\n const batch = await this.store.claimBatch(this.batchSize);\n if (batch.length === 0) return 0;\n\n this.hooks.onBatchClaimed?.(batch.length);\n this.log.debug(\"batch claimed\", { count: batch.length });\n\n const messages = await this.toPublishable(batch);\n const recordsById = new Map(batch.map((r) => [r.id, r]));\n\n const results = await this.publisher.publish(messages);\n\n const succeeded: string[] = [];\n for (const result of results) {\n const record = recordsById.get(result.recordId);\n if (!record) continue;\n\n if (result.ok) {\n succeeded.push(record.id);\n this.hooks.onPublished?.(result);\n } else {\n await this.handleFailure(\n record,\n result.error ?? new Error(\"unknown publish error\"),\n result.errorKind,\n );\n }\n }\n\n if (succeeded.length > 0) {\n await this.store.markDone(succeeded);\n }\n\n return batch.length;\n }\n\n private async handleFailure(\n record: OutboxRecord,\n error: Error,\n errorKind?: PublishErrorKind,\n ): Promise<void> {\n const attempts = record.attempts + 1;\n // fatal/poison short-circuit: retrying cannot help (auth denied, fenced\n // epoch, oversized record, schema rejected). Skip the backoff schedule\n // entirely and go straight to DLQ + dead.\n const isTerminalKind = errorKind === \"fatal\" || errorKind === \"poison\";\n const retryAt = isTerminalKind ? null : nextRetryAt(this.retry, attempts);\n const willRetry = retryAt !== null;\n\n this.hooks.onFailed?.(record, error, willRetry);\n this.log.warn(\"publish failed\", {\n recordId: record.id,\n attempts,\n willRetry,\n errorKind: errorKind ?? \"retriable\",\n error: error.message,\n });\n\n if (willRetry) {\n await this.store.markFailed(record.id, retryAt, \"failed\");\n return;\n }\n\n // Terminal: route to DLQ if configured, then mark dead.\n if (this.dlq.topic && this.publisher.publishToDlq) {\n try {\n const msg = (await this.toPublishable([record]))[0];\n if (msg) {\n await this.publisher.publishToDlq(\n {\n ...msg,\n topic: this.dlq.topic,\n // Preserve the original destination so the publisher can record\n // it as a header; otherwise it is lost when we overwrite `topic`.\n headers: { ...msg.headers, \"original-topic\": record.topic },\n },\n error,\n );\n }\n } catch (dlqErr) {\n const e = dlqErr instanceof Error ? dlqErr : new Error(String(dlqErr));\n this.log.error(\"DLQ publish failed\", {\n recordId: record.id,\n error: e.message,\n });\n }\n }\n\n await this.store.markFailed(record.id, null, \"dead\");\n this.hooks.onDead?.(record, error);\n }\n\n private async toPublishable(\n records: OutboxRecord[],\n ): Promise<PublishableMessage[]> {\n const out: PublishableMessage[] = [];\n for (const record of records) {\n out.push(await buildPublishable(record, this.serializer));\n }\n return out;\n }\n\n /** Wake a pending idle wait, or mark one pending so the next wait is skipped. */\n private signal(): void {\n this.wakePending = true;\n this.wakeResolver?.();\n }\n\n /**\n * Idle wait that resolves after `ms`, or early when `signal()` fires. A signal\n * raised while no wait is in flight is remembered (wakePending), so a wake that\n * races a claim cycle is never lost.\n */\n private waitForWork(ms: number): Promise<void> {\n if (this.wakePending) {\n this.wakePending = false;\n return Promise.resolve();\n }\n return new Promise((resolve) => {\n const timer = setTimeout(() => {\n this.wakeResolver = null;\n resolve();\n }, ms);\n this.wakeResolver = () => {\n clearTimeout(timer);\n this.wakeResolver = null;\n this.wakePending = false;\n resolve();\n };\n });\n }\n}\n","import type { StandardSchemaV1 } from \"./standard-schema.js\";\n\n/**\n * Thrown when an outbox payload fails its topic's schema — at `enqueue`\n * (before the DB insert) or at `decode` (malformed bytes / schema mismatch).\n * Carries the topic and the validator's structured issues for diagnostics.\n */\nexport class OutboxValidationError extends Error {\n readonly topic: string;\n readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;\n\n constructor(\n topic: string,\n issues: ReadonlyArray<StandardSchemaV1.Issue>,\n options?: { cause?: unknown },\n ) {\n const first = issues[0]?.message ?? \"unknown validation error\";\n super(`Outbox payload for \"${topic}\" failed validation: ${first}`, options);\n this.name = \"OutboxValidationError\";\n this.topic = topic;\n this.issues = issues;\n }\n}\n","import { OutboxValidationError } from \"./errors.js\";\nimport type { StandardSchemaV1 } from \"./standard-schema.js\";\nimport type { OutboxMessageInput } from \"./types.js\";\n\n/** One topic's contract: the aggregate it belongs to and its payload schema. */\nexport interface TopicDefinition {\n /** Aggregate type stamped on every event of this topic (e.g. \"order\"). */\n readonly aggregateType: string;\n /** Standard Schema for the payload (Zod 3.24+/Valibot/ArkType/…). */\n readonly schema: StandardSchemaV1;\n}\n\n/** A map of topic name -> its definition. The single source of truth. */\nexport type OutboxRegistry = Record<string, TopicDefinition>;\n\n/**\n * Minimal store surface the producer facade needs. `PostgresStore` satisfies\n * this structurally, so the facade stays DB-agnostic and `tx` flows from the\n * concrete store (e.g. a pg client).\n */\nexport interface EnqueueableStore<Tx = unknown> {\n enqueue(tx: Tx, msg: OutboxMessageInput & { traceId?: string }): Promise<string>;\n}\n\ntype TxOf<S> = S extends EnqueueableStore<infer Tx> ? Tx : never;\n\ntype PayloadInput<R extends OutboxRegistry, K extends keyof R> =\n StandardSchemaV1.InferInput<R[K][\"schema\"]>;\ntype PayloadOutput<R extends OutboxRegistry, K extends keyof R> =\n StandardSchemaV1.InferOutput<R[K][\"schema\"]>;\n\n/** The write-side input for a typed enqueue (payload is the schema's input type). */\nexport interface EnqueueInput<R extends OutboxRegistry, K extends keyof R> {\n aggregateId: string;\n payload: PayloadInput<R, K>;\n key?: string;\n headers?: Record<string, string>;\n messageId?: string;\n traceId?: string;\n}\n\n/** Consumer-side facade: validate/decode without a store. */\nexport interface OutboxConsumer<R extends OutboxRegistry> {\n /** Validate an already-parsed value against the topic's schema. */\n validate<K extends keyof R & string>(\n topic: K,\n value: unknown,\n ): Promise<PayloadOutput<R, K>>;\n /** JSON-parse `bytes`, validate against the topic's schema, return the payload. */\n decode<K extends keyof R & string>(\n topic: K,\n bytes: Buffer | Uint8Array | string,\n ): Promise<PayloadOutput<R, K>>;\n}\n\n/** Producer-side facade: adds typed, validated enqueue. */\nexport interface OutboxProducer<R extends OutboxRegistry, Tx>\n extends OutboxConsumer<R> {\n /** Validate `payload`, then enqueue inside the caller's transaction `tx`. */\n enqueue<K extends keyof R & string>(\n tx: Tx,\n topic: K,\n msg: EnqueueInput<R, K>,\n ): Promise<string>;\n}\n\n// Loose shape used inside the implementation; the public types come from overloads.\ninterface LooseEnqueueInput {\n aggregateId: string;\n payload: unknown;\n key?: string;\n headers?: Record<string, string>;\n messageId?: string;\n traceId?: string;\n}\n\nexport function defineOutbox<R extends OutboxRegistry>(\n registry: R,\n): OutboxConsumer<R>;\nexport function defineOutbox<R extends OutboxRegistry, S extends EnqueueableStore>(\n registry: R,\n opts: { store: S },\n): OutboxProducer<R, TxOf<S>>;\nexport function defineOutbox<R extends OutboxRegistry>(\n registry: R,\n opts?: { store: EnqueueableStore },\n): OutboxConsumer<R> | OutboxProducer<R, unknown> {\n const validate = async (topic: string, value: unknown): Promise<unknown> => {\n const def = registry[topic];\n if (!def) {\n throw new OutboxValidationError(topic, [\n { message: `unknown topic \"${topic}\"` },\n ]);\n }\n const result = await def.schema[\"~standard\"].validate(value);\n if (result.issues) throw new OutboxValidationError(topic, result.issues);\n return result.value;\n };\n\n const decode = async (\n topic: string,\n bytes: Buffer | Uint8Array | string,\n ): Promise<unknown> => {\n const text =\n typeof bytes === \"string\" ? bytes : Buffer.from(bytes).toString(\"utf8\");\n let parsed: unknown;\n try {\n parsed = JSON.parse(text);\n } catch (err) {\n throw new OutboxValidationError(\n topic,\n [{ message: `invalid JSON: ${(err as Error).message}` }],\n { cause: err },\n );\n }\n return validate(topic, parsed);\n };\n\n const consumer = { validate, decode } as unknown as OutboxConsumer<R>;\n if (!opts?.store) return consumer;\n\n const store = opts.store;\n const enqueue = async (\n tx: unknown,\n topic: string,\n msg: LooseEnqueueInput,\n ): Promise<string> => {\n const def = registry[topic];\n if (!def) {\n throw new OutboxValidationError(topic, [\n { message: `unknown topic \"${topic}\"` },\n ]);\n }\n const payload = await validate(topic, msg.payload);\n return store.enqueue(tx, {\n topic,\n aggregateType: def.aggregateType,\n aggregateId: msg.aggregateId,\n payload,\n key: msg.key,\n headers: msg.headers,\n messageId: msg.messageId,\n traceId: msg.traceId,\n });\n };\n\n return { validate, decode, enqueue } as unknown as OutboxProducer<R, unknown>;\n}\n"],"mappings":";AAWO,IAAM,qBAAmD;AAAA,EAC9D,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,M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UACqB;AACrB,UAAM,OACJ,OAAO,UAAU,WAAW,QAAQ,OAAO,KAAK,KAAK,EAAE,SAAS,MAAM;AACxE,QAAI;AACJ,QAAI;AACF,eAAS,KAAK,MAAM,IAAI;AAAA,IAC1B,SAAS,KAAK;AACZ,YAAM,IAAI;AAAA,QACR;AAAA,QACA,CAAC,EAAE,SAAS,iBAAkB,IAAc,OAAO,GAAG,CAAC;AAAA,QACvD,EAAE,OAAO,IAAI;AAAA,MACf;AAAA,IACF;AACA,WAAO,SAAS,OAAO,MAAM;AAAA,EAC/B;AAEA,QAAM,WAAW,EAAE,UAAU,OAAO;AACpC,MAAI,CAAC,MAAM,MAAO,QAAO;AAEzB,QAAM,QAAQ,KAAK;AACnB,QAAM,UAAU,OACd,IACA,OACA,QACoB;AACpB,UAAM,MAAM,SAAS,KAAK;AAC1B,QAAI,CAAC,KAAK;AACR,YAAM,IAAI,sBAAsB,OAAO;AAAA,QACrC,EAAE,SAAS,kBAAkB,KAAK,IAAI;AAAA,MACxC,CAAC;AAAA,IACH;AACA,UAAM,UAAU,MAAM,SAAS,OAAO,IAAI,OAAO;AACjD,WAAO,MAAM,QAAQ,IAAI;AAAA,MACvB;AAAA,MACA,eAAe,IAAI;AAAA,MACnB,aAAa,IAAI;AAAA,MACjB;AAAA,MACA,KAAK,IAAI;AAAA,MACT,SAAS,IAAI;AAAA,MACb,WAAW,IAAI;AAAA,MACf,SAAS,IAAI;AAAA,IACf,CAAC;AAAA,EACH;AAEA,SAAO,EAAE,UAAU,QAAQ,QAAQ;AACrC;","names":[]}
1
+ {"version":3,"sources":["../src/types.ts","../src/backoff.ts","../src/serializer.ts","../src/publishable.ts","../src/relay.ts","../src/errors.ts","../src/registry.ts"],"sourcesContent":["/**\n * Status lifecycle of an outbox record.\n *\n * pending -> freshly enqueued, awaiting first publish\n * processing -> claimed by a relay instance, publish in flight\n * done -> successfully published to the broker\n * failed -> publish failed, awaiting retry (next_retry_at)\n * dead -> exhausted retries, routed to DLQ (or parked)\n */\nexport type OutboxStatus = \"pending\" | \"processing\" | \"done\" | \"failed\" | \"dead\";\n\nexport const OUTBOX_STATUS_CODE: Record<OutboxStatus, number> = {\n pending: 0,\n processing: 1,\n done: 2,\n failed: 3,\n dead: 4,\n};\n\nexport const OUTBOX_STATUS_FROM_CODE: Record<number, OutboxStatus> = {\n 0: \"pending\",\n 1: \"processing\",\n 2: \"done\",\n 3: \"failed\",\n 4: \"dead\",\n};\n\n/**\n * A message as enqueued by the application inside its own DB transaction.\n * This is the write-side input — no infra concerns leak in here.\n */\nexport interface OutboxMessageInput {\n /** Logical topic the message will be published to. */\n topic: string;\n /** Type of the aggregate that produced this event (e.g. \"order\"). */\n aggregateType: string;\n /**\n * Identifier of the aggregate instance (e.g. order id).\n * Used as the default partition key to preserve per-aggregate ordering.\n */\n aggregateId: string;\n /** Event payload. Serialized by the configured serializer. */\n payload: unknown;\n /** Optional explicit partition key. Falls back to aggregateId. */\n key?: string;\n /** Optional message headers. */\n headers?: Record<string, string>;\n /**\n * Optional client-supplied message id for idempotency / dedup.\n * If omitted, the store generates one.\n */\n messageId?: string;\n}\n\n/**\n * A persisted outbox record as read back by the relay.\n */\nexport interface OutboxRecord {\n id: string;\n messageId: string;\n topic: string;\n aggregateType: string;\n aggregateId: string;\n key: string | null;\n payload: unknown;\n headers: Record<string, string>;\n traceId: string | null;\n status: OutboxStatus;\n attempts: number;\n nextRetryAt: Date | null;\n createdAt: Date;\n processedAt: Date | null;\n}\n\n/**\n * The message handed to a Publisher after serialization.\n */\nexport interface PublishableMessage {\n topic: string;\n key: string | null;\n /** Serialized payload bytes. */\n value: Buffer;\n headers: Record<string, string>;\n /** Original record id, for correlation in publish results. */\n recordId: string;\n messageId: string;\n /**\n * Explicit partition override. When set, the publisher MUST route the\n * record to this exact partition, bypassing the configured partitioner.\n *\n * Use cases:\n * - Compacted topics with application-managed sharding.\n * - Tenant-affinity routing where you compute the partition yourself.\n * - Geo-pinning records to a specific broker.\n *\n * When omitted (the default), the underlying client's partitioner\n * decides — usually a hash of `key`, falling back to sticky round-robin\n * when `key` is null.\n */\n partition?: number;\n}\n\n/**\n * Why a publish failed, in terms the relay can act on. Drivers classify their\n * native errors into one of these buckets; the relay reads `errorKind` to\n * decide whether to retry, short-circuit to the DLQ, or pause polling. The\n * field is optional for backward compatibility — when absent, the relay\n * treats the error as `\"retriable\"`.\n *\n * - `retriable` — transient (broker unreachable, leader election, request\n * timeout); retry per the configured backoff policy. The default for any\n * unclassified error.\n * - `fatal` — the producer or the credentials are broken (fenced epoch,\n * authentication failed, ACL denied). Retrying cannot help; the relay\n * short-circuits straight to the DLQ + `dead` status.\n * - `poison` — the message itself is rejectable by every broker\n * (oversized record, corrupt payload, schema-registry refused encoding).\n * Same handling as `fatal`: DLQ + dead, no retries.\n * - `backpressure` — the *producer's own* outbound buffer is full\n * (librdkafka `__QUEUE_FULL`). The right response is to slow the relay\n * down, not to burn retries. v2.1 treats this as `retriable` for\n * compatibility; smarter handling (pause polling) is planned.\n * - `quota` — the broker is throttling us (`THROTTLING_QUOTA_EXCEEDED`).\n * Back off with longer delays. v2.1 treats as `retriable`; smarter\n * handling (longer backoff) is planned.\n */\nexport type PublishErrorKind =\n | \"retriable\"\n | \"fatal\"\n | \"poison\"\n | \"backpressure\"\n | \"quota\";\n\n/**\n * Result of attempting to publish a single message.\n */\nexport interface PublishResult {\n recordId: string;\n ok: boolean;\n error?: Error;\n /**\n * Optional classification of `error` for relay-level decision-making. Set\n * by publisher implementations that know how to inspect their native error\n * shapes. Absent value is treated as `\"retriable\"` by the relay (the safe\n * default — at worst we retry an error we should have skipped).\n */\n errorKind?: PublishErrorKind;\n}\n\n/**\n * Pluggable serializer. Default is JSON; users can swap in\n * Avro / Protobuf / Schema-Registry-backed serializers.\n */\nexport interface Serializer {\n serialize(message: OutboxRecord): Buffer | Promise<Buffer>;\n /** Content-type header value advertised for this serializer. */\n readonly contentType: string;\n}\n\n/**\n * Storage abstraction. Implemented per-database (postgres, mysql, ...).\n * The relay only talks to the store through this interface.\n */\nexport interface OutboxStore {\n /**\n * Atomically claim up to `batchSize` due messages and mark them\n * as `processing`. Implementations MUST be safe under concurrent\n * relay instances (e.g. SELECT ... FOR UPDATE SKIP LOCKED).\n */\n claimBatch(batchSize: number): Promise<OutboxRecord[]>;\n\n /** Mark records as successfully published. */\n markDone(recordIds: string[]): Promise<void>;\n\n /**\n * Mark a record as failed and schedule its next retry.\n * `nextRetryAt` of null + status \"dead\" means terminal.\n * Implementations MUST increment `attempts` so the retry budget is honored.\n */\n markFailed(\n recordId: string,\n nextRetryAt: Date | null,\n status: \"failed\" | \"dead\",\n ): Promise<void>;\n\n /**\n * Re-queue a record back to `failed` with the given `retryAt` **WITHOUT\n * incrementing attempts**. Used by the relay for backpressure handling —\n * a client-side queue-full failure is a \"slow down\" signal, not a\n * record-specific failure, and counting it as a retry would burn the\n * attempt budget unfairly.\n *\n * Optional: stores that don't implement this fall back to `markFailed`\n * (which increments attempts, with the caveat documented above).\n */\n requeue?(recordId: string, retryAt: Date): Promise<void>;\n\n /** Best-effort lifecycle hooks; no-op allowed. */\n init?(): Promise<void>;\n close?(): Promise<void>;\n}\n\n/**\n * Broker abstraction. Implemented per-driver (kafkajs, confluent, ...).\n */\nexport interface Publisher {\n connect(): Promise<void>;\n disconnect(): Promise<void>;\n /**\n * Publish a batch. Returns a per-message result so the relay can\n * mark partial success. Implementations may use a transactional\n * producer to make the batch atomic.\n */\n publish(messages: PublishableMessage[]): Promise<PublishResult[]>;\n /** Route a permanently-failed message to a dead-letter destination. */\n publishToDlq?(message: PublishableMessage, error: Error): Promise<void>;\n}\n\n/**\n * Backoff strategy for retrying failed publishes.\n */\nexport type BackoffStrategy = \"fixed\" | \"linear\" | \"exponential\";\n\nexport interface RetryConfig {\n maxAttempts: number;\n strategy: BackoffStrategy;\n baseMs: number;\n maxMs: number;\n /** Add random jitter (0..baseMs) to avoid thundering herd. Default true. */\n jitter?: boolean;\n /**\n * Delay (ms) before re-queueing a record whose publish was rejected with\n * `errorKind: \"backpressure\"` (client-side producer buffer full).\n *\n * Backpressure failures do NOT count as a failed attempt — the buffer\n * being full is a \"slow down\" signal, not a record-specific failure.\n * The record is requeued at the next interval, not promoted to dead.\n *\n * Default 1000 ms. Requires the {@link OutboxStore} to implement\n * `requeue` — stores without it fall back to {@link OutboxStore.markFailed}\n * which DOES increment attempts.\n */\n backpressureDelayMs?: number;\n /**\n * Multiplier applied to the computed backoff for records rejected with\n * `errorKind: \"quota\"` (broker is throttling the producer). Default 5 —\n * a quota signal asks for a longer breath than a generic transient error.\n * Quota failures DO count as attempts (unlike backpressure).\n */\n quotaMultiplier?: number;\n}\n\nexport interface DlqConfig {\n /** Topic to route dead messages to. If absent, dead messages are parked. */\n topic?: string;\n /**\n * Include a truncated stack trace as the `dlq-error-stack` header when\n * routing a record to the DLQ. Default false — keep DLQ messages small\n * by default; opt in if your triage workflow needs the stack.\n */\n includeStackTraces?: boolean;\n /**\n * Maximum bytes of the truncated stack trace included when\n * `includeStackTraces` is on. Default 4096.\n */\n maxStackBytes?: number;\n}\n\n/**\n * Optional trace-context propagator. `inject` writes the active trace context\n * into the carrier as W3C `traceparent`/`tracestate` (the shape of an\n * OpenTelemetry TextMapPropagator), so it is persisted with the outbox row and\n * carried to the published message. The library depends on no tracing package;\n * provide a thin adapter over yours (OpenTelemetry, Datadog, …).\n */\nexport interface Tracing {\n inject(carrier: Record<string, string>): void;\n}\n\n/**\n * Minimal structured logger. Console-backed default provided.\n */\nexport interface Logger {\n debug(msg: string, meta?: Record<string, unknown>): void;\n info(msg: string, meta?: Record<string, unknown>): void;\n warn(msg: string, meta?: Record<string, unknown>): void;\n error(msg: string, meta?: Record<string, unknown>): void;\n}\n\n/**\n * A low-latency wake source for the relay. Instead of only waking on the poll\n * interval, the relay claims immediately whenever `onWake` fires. The signal is\n * advisory: it may fire spuriously or be missed entirely, and the relay's\n * polling remains the safety net, so implementations need not deduplicate or\n * guarantee delivery. (e.g. a Postgres LISTEN/NOTIFY waker.)\n */\nexport interface Waker {\n start(onWake: () => void): Promise<void>;\n stop(): Promise<void>;\n}\n\n/**\n * Lifecycle / observability hooks emitted by the relay.\n */\nexport interface RelayHooks {\n onBatchClaimed?(count: number): void;\n onPublished?(result: PublishResult): void;\n onFailed?(record: OutboxRecord, error: Error, willRetry: boolean): void;\n onDead?(record: OutboxRecord, error: Error): void;\n onError?(error: Error): void;\n}\n","import type { RetryConfig } from \"./types.js\";\n\n/**\n * Compute the delay (ms) before the next retry attempt.\n *\n * @param attempt 1-based attempt number that just failed.\n */\nexport function computeBackoff(config: RetryConfig, attempt: number): number {\n const { strategy, baseMs, maxMs } = config;\n const jitter = config.jitter ?? true;\n\n let delay: number;\n switch (strategy) {\n case \"fixed\":\n delay = baseMs;\n break;\n case \"linear\":\n delay = baseMs * attempt;\n break;\n case \"exponential\":\n // base * 2^(attempt-1), capped to avoid overflow before clamping\n delay = baseMs * 2 ** Math.min(attempt - 1, 30);\n break;\n }\n\n delay = Math.min(delay, maxMs);\n\n if (jitter) {\n // Full jitter: random in [0, delay]. Decorrelates concurrent relays.\n delay = Math.random() * delay;\n }\n\n return Math.floor(delay);\n}\n\n/**\n * Resolve when (Date) the next retry should occur, or null if the\n * record has exhausted its attempts and should go dead.\n */\nexport function nextRetryAt(\n config: RetryConfig,\n attempts: number,\n now: Date = new Date(),\n): Date | null {\n if (attempts >= config.maxAttempts) return null;\n const delay = computeBackoff(config, attempts);\n return new Date(now.getTime() + delay);\n}\n","import type { Logger, OutboxRecord, Serializer } from \"./types.js\";\n\n/**\n * Default serializer: JSON-encodes the record payload to UTF-8 bytes.\n */\nexport class JsonSerializer implements Serializer {\n readonly contentType = \"application/json\";\n\n serialize(record: OutboxRecord): Buffer {\n return Buffer.from(JSON.stringify(record.payload), \"utf8\");\n }\n}\n\n/**\n * Minimal console-backed logger. Swap in pino/winston by implementing Logger.\n */\nexport class ConsoleLogger implements Logger {\n constructor(private readonly prefix = \"[outbox]\") {}\n\n private fmt(\n write: (line: string, meta?: Record<string, unknown>) => void,\n level: string,\n msg: string,\n meta?: Record<string, unknown>,\n ): void {\n const line = `${this.prefix} ${level} ${msg}`;\n if (meta) {\n write(line, meta);\n } else {\n write(line);\n }\n }\n\n debug(msg: string, meta?: Record<string, unknown>): void {\n // eslint-disable-next-line no-console\n this.fmt(console.debug, \"DEBUG\", msg, meta);\n }\n info(msg: string, meta?: Record<string, unknown>): void {\n // eslint-disable-next-line no-console\n this.fmt(console.info, \"INFO\", msg, meta);\n }\n warn(msg: string, meta?: Record<string, unknown>): void {\n // eslint-disable-next-line no-console\n this.fmt(console.warn, \"WARN\", msg, meta);\n }\n error(msg: string, meta?: Record<string, unknown>): void {\n // eslint-disable-next-line no-console\n this.fmt(console.error, \"ERROR\", msg, meta);\n }\n}\n\n/** Logger that discards everything. Useful for tests. */\nexport class NoopLogger implements Logger {\n debug(): void {}\n info(): void {}\n warn(): void {}\n error(): void {}\n}\n","import type { OutboxRecord, PublishableMessage, Serializer } from \"./types.js\";\n\n/**\n * Turn a persisted outbox record into a broker-ready message: serialize the\n * payload and attach the standard correlation headers. Shared by the polling\n * `Relay` and the streaming relay so both produce byte-identical messages.\n */\nexport async function buildPublishable(\n record: OutboxRecord,\n serializer: Serializer,\n): Promise<PublishableMessage> {\n const value = await serializer.serialize(record);\n const headers: Record<string, string> = {\n ...record.headers,\n \"content-type\": serializer.contentType,\n \"message-id\": record.messageId,\n \"aggregate-type\": record.aggregateType,\n \"aggregate-id\": record.aggregateId,\n };\n if (record.traceId) headers[\"trace-id\"] = record.traceId;\n\n return {\n topic: record.topic,\n key: record.key ?? record.aggregateId,\n value,\n headers,\n recordId: record.id,\n messageId: record.messageId,\n };\n}\n","import { computeBackoff } from \"./backoff.js\";\nimport { buildPublishable } from \"./publishable.js\";\nimport { ConsoleLogger, JsonSerializer } from \"./serializer.js\";\nimport type {\n DlqConfig,\n Logger,\n OutboxRecord,\n OutboxStore,\n PublishableMessage,\n PublishErrorKind,\n Publisher,\n RelayHooks,\n RetryConfig,\n Serializer,\n Waker,\n} from \"./types.js\";\n\nexport interface RelayOptions {\n store: OutboxStore;\n publisher: Publisher;\n /** Messages claimed per poll iteration. Default 100. */\n batchSize?: number;\n /** Idle wait (ms) when a poll returns no work. Default 200. */\n pollIntervalMs?: number;\n retry?: Partial<RetryConfig>;\n dlq?: DlqConfig;\n serializer?: Serializer;\n logger?: Logger;\n hooks?: RelayHooks;\n /**\n * Optional low-latency wake source. When provided, the relay claims as soon as\n * the waker signals new work, instead of waiting out `pollIntervalMs`. Polling\n * stays on as a safety net, so set `pollIntervalMs` longer when using a waker.\n */\n waker?: Waker;\n}\n\nconst DEFAULT_RETRY: RetryConfig = {\n maxAttempts: 5,\n strategy: \"exponential\",\n baseMs: 200,\n maxMs: 30_000,\n jitter: true,\n};\n\n/**\n * The Relay drains the outbox store and publishes messages to the broker.\n *\n * It is safe to run multiple Relay instances concurrently against the same\n * store as long as the store's claimBatch uses a lock-free claim strategy\n * (e.g. SELECT ... FOR UPDATE SKIP LOCKED).\n */\nexport class Relay {\n private readonly store: OutboxStore;\n private readonly publisher: Publisher;\n private readonly batchSize: number;\n private readonly pollIntervalMs: number;\n private readonly retry: RetryConfig;\n private readonly dlq: DlqConfig;\n private readonly serializer: Serializer;\n private readonly log: Logger;\n private readonly hooks: RelayHooks;\n private readonly waker: Waker | null;\n\n private running = false;\n private stopping = false;\n private loopPromise: Promise<void> | null = null;\n\n // Interruptible idle wait: `signal()` wakes a pending wait (or marks one\n // pending if none is in flight, so a wake can't be lost between cycles).\n private wakePending = false;\n private wakeResolver: (() => void) | null = null;\n\n constructor(opts: RelayOptions) {\n this.store = opts.store;\n this.publisher = opts.publisher;\n this.batchSize = opts.batchSize ?? 100;\n this.pollIntervalMs = opts.pollIntervalMs ?? 200;\n this.retry = { ...DEFAULT_RETRY, ...opts.retry };\n this.dlq = opts.dlq ?? {};\n this.serializer = opts.serializer ?? new JsonSerializer();\n this.log = opts.logger ?? new ConsoleLogger();\n this.hooks = opts.hooks ?? {};\n this.waker = opts.waker ?? null;\n }\n\n async start(): Promise<void> {\n if (this.running) return;\n this.running = true;\n this.stopping = false;\n\n await this.store.init?.();\n await this.publisher.connect();\n await this.waker?.start(() => this.signal());\n this.log.info(\"relay started\", {\n batchSize: this.batchSize,\n pollIntervalMs: this.pollIntervalMs,\n waker: this.waker !== null,\n });\n\n this.loopPromise = this.loop();\n }\n\n /**\n * Stop accepting new work, finish the in-flight batch, then disconnect.\n */\n async stop(): Promise<void> {\n if (!this.running) return;\n this.stopping = true;\n this.signal(); // break any in-flight idle wait so shutdown is immediate\n this.log.info(\"relay stopping, draining in-flight batch\");\n await this.loopPromise;\n await this.waker?.stop();\n await this.publisher.disconnect();\n await this.store.close?.();\n this.running = false;\n this.log.info(\"relay stopped\");\n }\n\n private async loop(): Promise<void> {\n while (!this.stopping) {\n try {\n const processed = await this.tick();\n if (processed === 0 && !this.stopping) {\n await this.waitForWork(this.pollIntervalMs);\n }\n } catch (err) {\n const error = err instanceof Error ? err : new Error(String(err));\n this.log.error(\"relay loop error\", { error: error.message });\n this.hooks.onError?.(error);\n await this.waitForWork(this.pollIntervalMs);\n }\n }\n }\n\n /**\n * Run a single claim+publish cycle. Returns number of records processed.\n * Exposed for tests / manual single-shot draining.\n */\n async tick(): Promise<number> {\n const batch = await this.store.claimBatch(this.batchSize);\n if (batch.length === 0) return 0;\n\n this.hooks.onBatchClaimed?.(batch.length);\n this.log.debug(\"batch claimed\", { count: batch.length });\n\n const messages = await this.toPublishable(batch);\n const recordsById = new Map(batch.map((r) => [r.id, r]));\n\n const results = await this.publisher.publish(messages);\n\n const succeeded: string[] = [];\n for (const result of results) {\n const record = recordsById.get(result.recordId);\n if (!record) continue;\n\n if (result.ok) {\n succeeded.push(record.id);\n this.hooks.onPublished?.(result);\n } else {\n await this.handleFailure(\n record,\n result.error ?? new Error(\"unknown publish error\"),\n result.errorKind,\n );\n }\n }\n\n if (succeeded.length > 0) {\n await this.store.markDone(succeeded);\n }\n\n return batch.length;\n }\n\n private async handleFailure(\n record: OutboxRecord,\n error: Error,\n errorKind?: PublishErrorKind,\n ): Promise<void> {\n // Backpressure: client-side producer queue is full. Re-queue the record\n // with a brief delay; do NOT increment attempts (this isn't the record's\n // fault, the producer is just full). Stores without `requeue` fall back\n // to markFailed, accepting the attempt-counter hit.\n if (errorKind === \"backpressure\") {\n const delay = this.retry.backpressureDelayMs ?? 1000;\n const retryAt = new Date(Date.now() + delay);\n this.hooks.onFailed?.(record, error, true);\n this.log.warn(\"publish backpressure — requeueing without bumping attempts\", {\n recordId: record.id,\n retryAt: retryAt.toISOString(),\n errorKind,\n error: error.message,\n });\n if (this.store.requeue) {\n await this.store.requeue(record.id, retryAt);\n } else {\n // Fallback: markFailed (will increment attempts). Documented in the\n // OutboxStore.requeue JSDoc.\n await this.store.markFailed(record.id, retryAt, \"failed\");\n }\n return;\n }\n\n const attempts = record.attempts + 1;\n // fatal/poison short-circuit: retrying cannot help (auth denied, fenced\n // epoch, oversized record, schema rejected). Skip the backoff schedule\n // entirely and go straight to DLQ + dead.\n const isTerminalKind = errorKind === \"fatal\" || errorKind === \"poison\";\n const retryAt = isTerminalKind\n ? null\n : this.nextRetryAtForKind(attempts, errorKind);\n const willRetry = retryAt !== null;\n\n this.hooks.onFailed?.(record, error, willRetry);\n this.log.warn(\"publish failed\", {\n recordId: record.id,\n attempts,\n willRetry,\n errorKind: errorKind ?? \"retriable\",\n error: error.message,\n });\n\n if (willRetry) {\n await this.store.markFailed(record.id, retryAt, \"failed\");\n return;\n }\n\n // Terminal: route to DLQ if configured, then mark dead.\n if (this.dlq.topic && this.publisher.publishToDlq) {\n try {\n const msg = (await this.toPublishable([record]))[0];\n if (msg) {\n await this.publisher.publishToDlq(\n {\n ...msg,\n topic: this.dlq.topic,\n // Per-record enrichment so the DLQ consumer has everything\n // needed for triage: original destination, aggregate / message\n // identity, the attempts count, and (opt-in) a truncated stack.\n headers: this.buildDlqHeaders(record, error, msg.headers),\n },\n error,\n );\n }\n } catch (dlqErr) {\n const e = dlqErr instanceof Error ? dlqErr : new Error(String(dlqErr));\n this.log.error(\"DLQ publish failed\", {\n recordId: record.id,\n error: e.message,\n });\n }\n }\n\n await this.store.markFailed(record.id, null, \"dead\");\n this.hooks.onDead?.(record, error);\n }\n\n /**\n * Compute the next retry instant, applying the quota multiplier when the\n * driver classified the failure as a server throttle. Quota failures DO\n * count as attempts (unlike backpressure) — the multiplier only stretches\n * the delay, not the budget.\n */\n private nextRetryAtForKind(\n attempts: number,\n errorKind: PublishErrorKind | undefined,\n ): Date | null {\n if (attempts >= this.retry.maxAttempts) return null;\n const baseDelay = computeBackoff(this.retry, attempts);\n const multiplier =\n errorKind === \"quota\" ? this.retry.quotaMultiplier ?? 5 : 1;\n const delay = Math.min(baseDelay * multiplier, this.retry.maxMs * 10);\n return new Date(Date.now() + delay);\n }\n\n /**\n * Build the per-record DLQ header bag. Operators triaging the DLQ get the\n * original destination, the aggregate / message identity, attempts count,\n * and optionally a truncated error stack — everything you need to decide\n * whether to re-enqueue, escalate, or drop.\n */\n private buildDlqHeaders(\n record: OutboxRecord,\n error: Error,\n existing: Record<string, string>,\n ): Record<string, string> {\n const out: Record<string, string> = {\n ...existing,\n \"original-topic\": record.topic,\n \"dlq-attempts\": String(record.attempts + 1),\n \"dlq-original-aggregate-id\": record.aggregateId,\n \"dlq-original-message-id\": record.messageId,\n };\n if (this.dlq.includeStackTraces && error.stack) {\n const maxBytes = this.dlq.maxStackBytes ?? 4096;\n out[\"dlq-error-stack\"] = truncateUtf8(error.stack, maxBytes);\n }\n return out;\n }\n\n private async toPublishable(\n records: OutboxRecord[],\n ): Promise<PublishableMessage[]> {\n const out: PublishableMessage[] = [];\n for (const record of records) {\n out.push(await buildPublishable(record, this.serializer));\n }\n return out;\n }\n\n /** Wake a pending idle wait, or mark one pending so the next wait is skipped. */\n private signal(): void {\n this.wakePending = true;\n this.wakeResolver?.();\n }\n\n /**\n * Idle wait that resolves after `ms`, or early when `signal()` fires. A signal\n * raised while no wait is in flight is remembered (wakePending), so a wake that\n * races a claim cycle is never lost.\n */\n private waitForWork(ms: number): Promise<void> {\n if (this.wakePending) {\n this.wakePending = false;\n return Promise.resolve();\n }\n return new Promise((resolve) => {\n const timer = setTimeout(() => {\n this.wakeResolver = null;\n resolve();\n }, ms);\n this.wakeResolver = () => {\n clearTimeout(timer);\n this.wakeResolver = null;\n this.wakePending = false;\n resolve();\n };\n });\n }\n}\n\n/**\n * Truncate a string to at most `maxBytes` of its UTF-8 encoding, appending a\n * \"…[truncated]\" marker when bytes are removed. Used to keep the DLQ stack\n * header bounded — headers in Kafka are byte-sized, not character-sized, so\n * a naive `slice` on multibyte text can blow the limit. Always returns a\n * valid (no-broken-codepoint) UTF-8 string.\n */\nfunction truncateUtf8(input: string, maxBytes: number): string {\n const marker = \"…[truncated]\";\n const markerBytes = Buffer.byteLength(marker, \"utf8\");\n const buf = Buffer.from(input, \"utf8\");\n if (buf.byteLength <= maxBytes) return input;\n const budget = Math.max(0, maxBytes - markerBytes);\n // Slice and decode; if the slice lands mid-codepoint, drop trailing bytes\n // until the decode is clean.\n let end = budget;\n while (end > 0) {\n const slice = buf.subarray(0, end).toString(\"utf8\");\n if (!slice.endsWith(\"�\")) {\n return slice + marker;\n }\n end--;\n }\n return marker;\n}\n","import type { StandardSchemaV1 } from \"./standard-schema.js\";\n\n/**\n * Thrown when an outbox payload fails its topic's schema — at `enqueue`\n * (before the DB insert) or at `decode` (malformed bytes / schema mismatch).\n * Carries the topic and the validator's structured issues for diagnostics.\n */\nexport class OutboxValidationError extends Error {\n readonly topic: string;\n readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;\n\n constructor(\n topic: string,\n issues: ReadonlyArray<StandardSchemaV1.Issue>,\n options?: { cause?: unknown },\n ) {\n const first = issues[0]?.message ?? \"unknown validation error\";\n super(`Outbox payload for \"${topic}\" failed validation: ${first}`, options);\n this.name = \"OutboxValidationError\";\n this.topic = topic;\n this.issues = issues;\n }\n}\n","import { OutboxValidationError } from \"./errors.js\";\nimport type { StandardSchemaV1 } from \"./standard-schema.js\";\nimport type { OutboxMessageInput } from \"./types.js\";\n\n/** One topic's contract: the aggregate it belongs to and its payload schema. */\nexport interface TopicDefinition {\n /** Aggregate type stamped on every event of this topic (e.g. \"order\"). */\n readonly aggregateType: string;\n /** Standard Schema for the payload (Zod 3.24+/Valibot/ArkType/…). */\n readonly schema: StandardSchemaV1;\n}\n\n/** A map of topic name -> its definition. The single source of truth. */\nexport type OutboxRegistry = Record<string, TopicDefinition>;\n\n/**\n * Minimal store surface the producer facade needs. `PostgresStore` satisfies\n * this structurally, so the facade stays DB-agnostic and `tx` flows from the\n * concrete store (e.g. a pg client).\n */\nexport interface EnqueueableStore<Tx = unknown> {\n enqueue(tx: Tx, msg: OutboxMessageInput & { traceId?: string }): Promise<string>;\n}\n\ntype TxOf<S> = S extends EnqueueableStore<infer Tx> ? Tx : never;\n\ntype PayloadInput<R extends OutboxRegistry, K extends keyof R> =\n StandardSchemaV1.InferInput<R[K][\"schema\"]>;\ntype PayloadOutput<R extends OutboxRegistry, K extends keyof R> =\n StandardSchemaV1.InferOutput<R[K][\"schema\"]>;\n\n/** The write-side input for a typed enqueue (payload is the schema's input type). */\nexport interface EnqueueInput<R extends OutboxRegistry, K extends keyof R> {\n aggregateId: string;\n payload: PayloadInput<R, K>;\n key?: string;\n headers?: Record<string, string>;\n messageId?: string;\n traceId?: string;\n}\n\n/** Consumer-side facade: validate/decode without a store. */\nexport interface OutboxConsumer<R extends OutboxRegistry> {\n /** Validate an already-parsed value against the topic's schema. */\n validate<K extends keyof R & string>(\n topic: K,\n value: unknown,\n ): Promise<PayloadOutput<R, K>>;\n /** JSON-parse `bytes`, validate against the topic's schema, return the payload. */\n decode<K extends keyof R & string>(\n topic: K,\n bytes: Buffer | Uint8Array | string,\n ): Promise<PayloadOutput<R, K>>;\n}\n\n/** Producer-side facade: adds typed, validated enqueue. */\nexport interface OutboxProducer<R extends OutboxRegistry, Tx>\n extends OutboxConsumer<R> {\n /** Validate `payload`, then enqueue inside the caller's transaction `tx`. */\n enqueue<K extends keyof R & string>(\n tx: Tx,\n topic: K,\n msg: EnqueueInput<R, K>,\n ): Promise<string>;\n}\n\n// Loose shape used inside the implementation; the public types come from overloads.\ninterface LooseEnqueueInput {\n aggregateId: string;\n payload: unknown;\n key?: string;\n headers?: Record<string, string>;\n messageId?: string;\n traceId?: string;\n}\n\nexport function defineOutbox<R extends OutboxRegistry>(\n registry: R,\n): OutboxConsumer<R>;\nexport function defineOutbox<R extends OutboxRegistry, S extends EnqueueableStore>(\n registry: R,\n opts: { store: S },\n): OutboxProducer<R, TxOf<S>>;\nexport function defineOutbox<R extends OutboxRegistry>(\n registry: R,\n opts?: { store: EnqueueableStore },\n): OutboxConsumer<R> | OutboxProducer<R, unknown> {\n const validate = async (topic: string, value: unknown): Promise<unknown> => {\n const def = registry[topic];\n if (!def) {\n throw new OutboxValidationError(topic, [\n { message: `unknown topic \"${topic}\"` },\n ]);\n }\n const result = await def.schema[\"~standard\"].validate(value);\n if (result.issues) throw new OutboxValidationError(topic, result.issues);\n return result.value;\n };\n\n const decode = async (\n topic: string,\n bytes: Buffer | Uint8Array | string,\n ): Promise<unknown> => {\n const text =\n typeof bytes === \"string\" ? bytes : Buffer.from(bytes).toString(\"utf8\");\n let parsed: unknown;\n try {\n parsed = JSON.parse(text);\n } catch (err) {\n throw new OutboxValidationError(\n topic,\n [{ message: `invalid JSON: ${(err as Error).message}` }],\n { cause: err },\n );\n }\n return validate(topic, parsed);\n };\n\n const consumer = { validate, decode } as unknown as OutboxConsumer<R>;\n if (!opts?.store) return consumer;\n\n const store = opts.store;\n const enqueue = async (\n tx: unknown,\n topic: string,\n msg: LooseEnqueueInput,\n ): Promise<string> => {\n const def = registry[topic];\n if (!def) {\n throw new OutboxValidationError(topic, [\n { message: `unknown topic \"${topic}\"` },\n ]);\n }\n const payload = await validate(topic, msg.payload);\n return store.enqueue(tx, {\n topic,\n aggregateType: def.aggregateType,\n aggregateId: msg.aggregateId,\n payload,\n key: msg.key,\n headers: msg.headers,\n messageId: msg.messageId,\n traceId: msg.traceId,\n });\n };\n\n return { validate, decode, enqueue } as unknown as OutboxProducer<R, unknown>;\n}\n"],"mappings":";AAWO,IAAM,qBAAmD;AAAA,EAC9D,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,MAAM;AACR;AAEO,IAAM,0BAAwD;AAAA,EACnE,GAAG;AAAA,EACH,GAAG;AAAA,EACH,GAAG;AAAA,EACH,GAAG;AAAA,EACH,GAAG;AACL;;;AClBO,SAAS,eAAe,QAAqB,SAAyB;AAC3E,QAAM,EAAE,UAAU,QAAQ,MAAM,IAAI;AACpC,QAAM,SAAS,OAAO,UAAU;AAEhC,MAAI;AACJ,UAAQ,UAAU;AAAA,IAChB,KAAK;AACH,cAAQ;AACR;AAAA,IACF,KAAK;AACH,cAAQ,SAAS;AACjB;AAAA,IACF,KAAK;AAEH,cAAQ,SAAS,KAAK,KAAK,IAAI,UAAU,GAAG,EAAE;AAC9C;AAAA,EACJ;AAEA,UAAQ,KAAK,IAAI,OAAO,KAAK;AAE7B,MAAI,QAAQ;AAEV,YAAQ,KAAK,OAAO,IAAI;AAAA,EAC1B;AAEA,SAAO,KAAK,MAAM,KAAK;AACzB;AAMO,SAAS,YACd,QACA,UACA,MAAY,oBAAI,KAAK,GACR;AACb,MAAI,YAAY,OAAO,YAAa,QAAO;AAC3C,QAAM,QAAQ,eAAe,QAAQ,QAAQ;AAC7C,SAAO,IAAI,KAAK,IAAI,QAAQ,IAAI,KAAK;AACvC;;;AC1CO,IAAM,iBAAN,MAA2C;AAAA,EACvC,cAAc;AAAA,EAEvB,UAAU,QAA8B;AACtC,WAAO,OAAO,KAAK,KAAK,UAAU,OAAO,OAAO,GAAG,MAAM;AAAA,EAC3D;AACF;AAKO,IAAM,gBAAN,MAAsC;AAAA,EAC3C,YAA6B,SAAS,YAAY;AAArB;AAAA,EAAsB;AAAA,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M,IAAI,sBAAsB,OAAO;AAAA,QACrC,EAAE,SAAS,kBAAkB,KAAK,IAAI;AAAA,MACxC,CAAC;AAAA,IACH;AACA,UAAM,UAAU,MAAM,SAAS,OAAO,IAAI,OAAO;AACjD,WAAO,MAAM,QAAQ,IAAI;AAAA,MACvB;AAAA,MACA,eAAe,IAAI;AAAA,MACnB,aAAa,IAAI;AAAA,MACjB;AAAA,MACA,KAAK,IAAI;AAAA,MACT,SAAS,IAAI;AAAA,MACb,WAAW,IAAI;AAAA,MACf,SAAS,IAAI;AAAA,IACf,CAAC;AAAA,EACH;AAEA,SAAO,EAAE,UAAU,QAAQ,QAAQ;AACrC;","names":["retryAt"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eventferry/core",
3
- "version": "3.2.1",
3
+ "version": "3.3.1",
4
4
  "description": "DB- and broker-agnostic core for the transactional outbox pattern",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -14,7 +14,8 @@
14
14
  }
15
15
  },
16
16
  "files": [
17
- "dist"
17
+ "dist",
18
+ "CHANGELOG.md"
18
19
  ],
19
20
  "keywords": [
20
21
  "outbox",