@eventferry/core 3.2.1 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,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;AAC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AAU,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,eA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,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,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,O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AA,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,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,OAAO;AAAA,EAChB;AAEA,QAAM,SAAS,OACb,OACA,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":["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.0",
4
4
  "description": "DB- and broker-agnostic core for the transactional outbox pattern",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",