@babelqueue/pulsar 1.1.0 → 1.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/README.md +103 -10
- package/dist/index.cjs +253 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +139 -7
- package/dist/index.d.ts +139 -7
- package/dist/index.js +253 -13
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -42,13 +42,19 @@ const consumer = new PulsarConsumer(
|
|
|
42
42
|
console.log(env.data.order_id, env.trace_id, env.attempts);
|
|
43
43
|
},
|
|
44
44
|
},
|
|
45
|
-
{
|
|
45
|
+
{
|
|
46
|
+
// Declare the subscription type (or pass `client` together with `topic`, the topic you subscribed to): an undeclared type counts as Exclusive and
|
|
47
|
+
// the constructor throws, see "Exclusive / Failover subscriptions" below.
|
|
48
|
+
subscriptionType: "Shared",
|
|
49
|
+
onError: (err) => console.error(err),
|
|
50
|
+
},
|
|
46
51
|
);
|
|
47
52
|
await consumer.run();
|
|
48
53
|
```
|
|
49
54
|
|
|
50
55
|
Delayed delivery: `publish(urn, data, { delayMs: 300000 })` → native `deliverAfter`. The
|
|
51
|
-
consumer routes
|
|
56
|
+
consumer routes on the `bq-job` property (§5.2); when it is absent or blank it falls back to the
|
|
57
|
+
body's URN. The body is always decoded and the handler always receives the body's envelope.
|
|
52
58
|
|
|
53
59
|
## Contract mapping (§5)
|
|
54
60
|
|
|
@@ -61,17 +67,104 @@ consumer routes purely on the `bq-job` property.
|
|
|
61
67
|
| `meta.schema_version` | property `bq-schema-version` |
|
|
62
68
|
| `meta.lang` | property `bq-source-lang` |
|
|
63
69
|
| `meta.created_at` | publish time (mirror; body authoritative) |
|
|
64
|
-
| `attempts` | property `bq-attempts` (authoritative),
|
|
70
|
+
| `attempts` | property `bq-attempts` (authoritative; body is the fallback), floored by `getRedeliveryCount()` |
|
|
65
71
|
| reserve / ack / retry | `acknowledge` / `negativeAcknowledge` |
|
|
66
72
|
|
|
67
73
|
Pulsar properties are string→string, so `bq-attempts` carries the contract `attempts` and is
|
|
68
|
-
**authoritative**.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
the
|
|
73
|
-
|
|
74
|
-
|
|
74
|
+
**authoritative**. It is valid only when it is plain decimal digits (`^[0-9]+$`, at most
|
|
75
|
+
2147483647) — a sign, whitespace, an empty string or an out-of-range value is invalid and the
|
|
76
|
+
consumer falls back to the body's `attempts`. The result is reconciled to
|
|
77
|
+
`max(.., getRedeliveryCount())`: `getRedeliveryCount()` is 0-based (0 on first delivery) so it
|
|
78
|
+
maps directly with **no −1** (the body is only rewritten when it holds an integer `attempts` that really changes; a missing or non-integer `attempts` is still rejected) — so a republish-driven retry and a native redelivery both
|
|
79
|
+
converge on the same number.
|
|
80
|
+
|
|
81
|
+
**Version gate (§5.7 step 3).** Before the body is decoded the consumer reads
|
|
82
|
+
`bq-schema-version`. Missing or blank: nothing changes ("blank" is the same set in every SDK: the empty string or only ASCII whitespace — space, `\t`, `\n`, `\v`, `\f`, `\r`; NBSP, U+0085, U+FEFF, U+3000 and the like are not blank). Any value other than the core's supported
|
|
83
|
+
`SCHEMA_VERSION` (`"2"`, `"x"`, `"01"`, ...) rejects the message **undecoded** through the same
|
|
84
|
+
path as a non-conformant envelope — `onError` (an error naming the version, with an empty
|
|
85
|
+
envelope) and a `negativeAcknowledge`; the handler, `onUnknownUrn` and `onRoutingMismatch` are
|
|
86
|
+
not called.
|
|
87
|
+
|
|
88
|
+
If the `bq-job` property and the body's URN differ, routing follows the property and the
|
|
89
|
+
optional `onRoutingMismatch(propertyUrn, bodyUrn, envelope)` hook is called (default: no-op).
|
|
90
|
+
It is separate from `onError`, its failures (including a rejected promise) are swallowed, and
|
|
91
|
+
it never changes the ack flow. The hook runs only for an accepted envelope — after `accepts()` and
|
|
92
|
+
the `attempts` reconcile — so it sees the same envelope the handler gets. A body without a `job` is still rejected by the codec even when
|
|
93
|
+
`bq-job` is set. A throwing handler `negativeAcknowledge`s, so
|
|
94
|
+
the broker redelivers (at-least-once) — up to `maxAttempts` deliveries (see below). The poll loop
|
|
95
|
+
never stops on a bad message — observe via `onError` / `onUnknownUrn`. The envelope is unchanged
|
|
96
|
+
(`schema_version` stays `1`); Pulsar is purely additive.
|
|
97
|
+
|
|
98
|
+
### Max attempts, dead-letter and poison (ADR-0036)
|
|
99
|
+
|
|
100
|
+
Redelivery is bounded, so every message terminates. `maxAttempts` (default **3**, an integer of at
|
|
101
|
+
least 1) is the total number of deliveries a message gets, the first included; `1` means no retry.
|
|
102
|
+
The SDK counts `counted = reconciled attempts + 1` (the reconciled value above plus the current
|
|
103
|
+
delivery) and decides for a retryable outcome — a handler that throws, an unmapped URN without an
|
|
104
|
+
`onUnknownUrn` hook, or a poison body with dead-lettering off:
|
|
105
|
+
|
|
106
|
+
- `counted < maxAttempts` → **retry**: with a `retryProducer`, re-publish to the same topic with
|
|
107
|
+
`bq-attempts` (and the body's `attempts`) set to `counted`, then acknowledge (the Go model);
|
|
108
|
+
without a producer or client, `negativeAcknowledge` (the native redelivery count advances on
|
|
109
|
+
`Shared` / `Key_Shared` only; see Exclusive / Failover below).
|
|
110
|
+
- otherwise → **dead-letter** when a `deadLetterProducer` is given, else **drop** (acknowledge, and
|
|
111
|
+
a warning through `onWarn`, default `console.warn`).
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
const dlq = await client.createProducer({ topic: "orders.dlq" });
|
|
115
|
+
const consumer = new PulsarConsumer(sub, handlers, {
|
|
116
|
+
maxAttempts: 3, // default
|
|
117
|
+
deadLetterProducer: dlq, // dead-lettering is on exactly when this is set
|
|
118
|
+
client, // lets the consumer build its own retry producer; see Exclusive / Failover below
|
|
119
|
+
topic: "orders", // ... on the topic you subscribed to (pulsar-client's Consumer has no getTopic())
|
|
120
|
+
queue: "orders", // dead_letter.original_queue of a poison record (default: the DLQ topic without ".dlq")
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
The dead-letter record is the canonical envelope with the `dead_letter` block (`reason` `failed` or
|
|
125
|
+
`unknown_urn`; `dead_letter.attempts` and the record's `attempts` are the counted value, `3` with the
|
|
126
|
+
default). The original is acknowledged **only after** the record was sent; a failed send leaves it
|
|
127
|
+
negatively acknowledged, so it is redelivered and the write is tried again. The SDK does not use
|
|
128
|
+
Pulsar's native `DeadLetterPolicy`; adding one yourself creates a second, uncoordinated limit.
|
|
129
|
+
|
|
130
|
+
**Poison.** A body that does not decode into an envelope — not JSON, not an object, or an object with
|
|
131
|
+
neither a valid `job` nor a valid `urn`, or any other body the codec does not accept as a conformant envelope (for example a missing `trace_id` or a non-integer `attempts`) — never reaches a handler or `onUnknownUrn`. With
|
|
132
|
+
`deadLetterProducer` it is written to the dead-letter topic at once, without retry, in the ADR-0035
|
|
133
|
+
shape (a strict JSON object keeps every original byte and gains a `dead_letter` block with
|
|
134
|
+
`reason: "poison"`; anything else is wrapped as `{"raw", "raw_encoding"?, "dead_letter"}`). Without
|
|
135
|
+
one it takes the bounded retry above and is then dropped.
|
|
136
|
+
|
|
137
|
+
**Exclusive / Failover subscriptions.** A negative acknowledgement advances the native redelivery
|
|
138
|
+
count only on `Shared` / `Key_Shared`; on `Exclusive` / `Failover` (pulsar-client's default is
|
|
139
|
+
`Exclusive`) the count stays 0, so a nack-driven retry would never reach `maxAttempts`. The retry is
|
|
140
|
+
therefore a **re-publish** to the consumer's own topic with `bq-attempts` + 1 and an acknowledge, which
|
|
141
|
+
carries the count in the message itself (reconciled as `max(bq-attempts, redeliveryCount)`) and bounds
|
|
142
|
+
every subscription type. Give the consumer the means to publish:
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
const consumer = new PulsarConsumer(sub, handlers, {
|
|
146
|
+
client, // the pulsar-client Client: the consumer builds the retry producer itself,
|
|
147
|
+
// lazily on the first retry
|
|
148
|
+
topic: "orders", // the topic you subscribed to (pulsar-client's Consumer has no getTopic())
|
|
149
|
+
subscriptionType: "Exclusive", // or "Shared" / "Key_Shared" (not declared = Exclusive, see below)
|
|
150
|
+
});
|
|
151
|
+
// or pass your own producer on the topic you read: { retryProducer: await client.createProducer({ topic }) }
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
A given `retryProducer` wins over `client`. The constructor **fails closed**: with `maxAttempts > 1`
|
|
155
|
+
and neither a `retryProducer` nor a `client` (with its `topic`), `subscriptionType` `Exclusive`,
|
|
156
|
+
`Failover` **or not declared** (it then counts as `Exclusive`, pulsar-client's default) throws a
|
|
157
|
+
`BabelQueueError`. Declare `subscriptionType: "Shared"` or `"Key_Shared"` to keep the
|
|
158
|
+
`negativeAcknowledge` retry without a producer. A poison body is re-published byte for byte, only
|
|
159
|
+
`bq-attempts` moves; out-of-band headers (e.g. `traceparent`) ride along, and the incoming `bq-job`
|
|
160
|
+
route is kept on the retry and on the DLQ record so every delivery reaches the same handler. A failed
|
|
161
|
+
re-publish or producer creation leaves the original negatively acknowledged and is reported through
|
|
162
|
+
`onWarn`.
|
|
163
|
+
|
|
164
|
+
**Behaviour change (MINOR).** Redelivery used to be unbounded; with the defaults a message that keeps
|
|
165
|
+
failing is now dropped (or dead-lettered) at its third delivery. Set `maxAttempts` higher to keep
|
|
166
|
+
more retries. A message with an unsupported `bq-schema-version` still takes the version gate above
|
|
167
|
+
(`onError` and a `negativeAcknowledge`).
|
|
75
168
|
|
|
76
169
|
The producer/consumer are injected, so the unit tests use fakes — no Pulsar, no broker. Dual
|
|
77
170
|
ESM + CJS.
|
package/dist/index.cjs
CHANGED
|
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
DEFAULT_MAX_ATTEMPTS: () => DEFAULT_MAX_ATTEMPTS,
|
|
23
24
|
PulsarConsumer: () => PulsarConsumer,
|
|
24
25
|
PulsarPublisher: () => PulsarPublisher,
|
|
25
26
|
headersOf: () => headersOf,
|
|
@@ -105,6 +106,25 @@ var PulsarPublisher = class {
|
|
|
105
106
|
return envelope.meta.id;
|
|
106
107
|
}
|
|
107
108
|
};
|
|
109
|
+
var DEFAULT_MAX_ATTEMPTS = 3;
|
|
110
|
+
var MAX_ATTEMPTS = 2 ** 31 - 1;
|
|
111
|
+
function parseAttempts(raw) {
|
|
112
|
+
if (typeof raw !== "string" || !/^[0-9]+$/.test(raw)) return void 0;
|
|
113
|
+
const value = Number(raw);
|
|
114
|
+
return value <= MAX_ATTEMPTS ? value : void 0;
|
|
115
|
+
}
|
|
116
|
+
function reconcileAttempts(properties, bodyAttempts, redeliveryCount) {
|
|
117
|
+
let attempts = bodyAttempts;
|
|
118
|
+
const fromProperty = parseAttempts(properties["bq-attempts"]);
|
|
119
|
+
if (fromProperty !== void 0) attempts = fromProperty;
|
|
120
|
+
return redeliveryCount > attempts ? redeliveryCount : attempts;
|
|
121
|
+
}
|
|
122
|
+
var BLANK_ASCII = /^[ \t\n\v\f\r]*$/;
|
|
123
|
+
function unsupportedSchemaVersion(properties) {
|
|
124
|
+
const raw = properties["bq-schema-version"];
|
|
125
|
+
if (typeof raw !== "string" || BLANK_ASCII.test(raw)) return void 0;
|
|
126
|
+
return raw === String(import_core.SCHEMA_VERSION) ? void 0 : raw;
|
|
127
|
+
}
|
|
108
128
|
function isReceiveTimeout(error) {
|
|
109
129
|
const message = error instanceof Error ? error.message : String(error);
|
|
110
130
|
return /timeout/i.test(message);
|
|
@@ -117,10 +137,62 @@ var PulsarConsumer = class {
|
|
|
117
137
|
this.consumer = consumer;
|
|
118
138
|
this.handlers = handlers;
|
|
119
139
|
this.options = options;
|
|
140
|
+
const maxAttempts = options.maxAttempts ?? DEFAULT_MAX_ATTEMPTS;
|
|
141
|
+
if (!Number.isInteger(maxAttempts) || maxAttempts < 1) {
|
|
142
|
+
throw new import_core.BabelQueueError(`maxAttempts must be an integer of at least 1, got ${String(maxAttempts)}.`);
|
|
143
|
+
}
|
|
144
|
+
this.maxAttempts = maxAttempts;
|
|
145
|
+
this.checkRetryBound(maxAttempts);
|
|
146
|
+
this.warn = options.onWarn ?? ((message) => console.warn(message));
|
|
120
147
|
}
|
|
121
148
|
consumer;
|
|
122
149
|
handlers;
|
|
123
150
|
options;
|
|
151
|
+
maxAttempts;
|
|
152
|
+
builtRetryProducer;
|
|
153
|
+
warn;
|
|
154
|
+
/**
|
|
155
|
+
* Setup check: on `Exclusive` / `Failover` (and an undeclared type) a negative acknowledgement does not advance the native
|
|
156
|
+
* redelivery count, so a retry needs a re-publish. Fail loudly here instead of looping at run time.
|
|
157
|
+
*/
|
|
158
|
+
checkRetryBound(maxAttempts) {
|
|
159
|
+
const declared = this.options.subscriptionType;
|
|
160
|
+
if (declared !== void 0 && !["Exclusive", "Shared", "Failover", "KeyShared"].includes(declared.replace("_", ""))) {
|
|
161
|
+
throw new import_core.BabelQueueError(`Unknown subscriptionType "${String(declared)}".`);
|
|
162
|
+
}
|
|
163
|
+
const type = declared === void 0 ? "Exclusive" : declared.replace("_", "");
|
|
164
|
+
if (type !== "Exclusive" && type !== "Failover") return;
|
|
165
|
+
if (maxAttempts === 1 || this.options.retryProducer !== void 0) return;
|
|
166
|
+
if (this.options.client !== void 0 && this.retryTopic() !== void 0) return;
|
|
167
|
+
const shown = declared === void 0 ? "An undeclared subscriptionType (treated as Exclusive)" : `A ${type} subscription`;
|
|
168
|
+
if (this.options.client !== void 0) {
|
|
169
|
+
throw new import_core.BabelQueueError(
|
|
170
|
+
`${shown} with a client needs the \`topic\` option (the topic this consumer reads) to build the retry producer: pulsar-client's Consumer cannot report its own topic.`
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
throw new import_core.BabelQueueError(
|
|
174
|
+
`${shown} needs a retryProducer (or a client plus a topic) to bound retries: a negative acknowledgement does not advance the redelivery count there, so a retry would never reach maxAttempts. Pass a client plus topic (the topic you subscribed to), or a retryProducer, or declare subscriptionType: "Shared" / "Key_Shared".`
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
retryTopic() {
|
|
178
|
+
const configured = this.options.topic;
|
|
179
|
+
if (configured !== void 0 && configured !== "") return configured;
|
|
180
|
+
const own = typeof this.consumer.getTopic === "function" ? this.consumer.getTopic() : void 0;
|
|
181
|
+
return own !== void 0 && own !== "" ? own : void 0;
|
|
182
|
+
}
|
|
183
|
+
/** The retry producer: the one given, else one built (once) from the client; undefined when neither exists. */
|
|
184
|
+
async resolveRetryProducer() {
|
|
185
|
+
if (this.options.retryProducer !== void 0) return this.options.retryProducer;
|
|
186
|
+
const client = this.options.client;
|
|
187
|
+
const topic = this.retryTopic();
|
|
188
|
+
if (client === void 0 || topic === void 0) return void 0;
|
|
189
|
+
this.builtRetryProducer ??= client.createProducer({ topic }).catch((error) => {
|
|
190
|
+
this.builtRetryProducer = void 0;
|
|
191
|
+
this.warn(`babelqueue: creating the retry producer on ${topic} failed (${errorText(error)}).`);
|
|
192
|
+
return void 0;
|
|
193
|
+
});
|
|
194
|
+
return this.builtRetryProducer;
|
|
195
|
+
}
|
|
124
196
|
/** Receive one message (up to the receive timeout), route + settle it. Returns 1, or 0 on timeout. */
|
|
125
197
|
async poll() {
|
|
126
198
|
let message;
|
|
@@ -140,30 +212,48 @@ var PulsarConsumer = class {
|
|
|
140
212
|
}
|
|
141
213
|
}
|
|
142
214
|
async handle(message) {
|
|
143
|
-
const
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
if (redeliveryCount > current) {
|
|
147
|
-
envelope.attempts = redeliveryCount;
|
|
148
|
-
}
|
|
149
|
-
if (!import_core.EnvelopeCodec.accepts(envelope)) {
|
|
215
|
+
const properties = safeProperties(message);
|
|
216
|
+
const rejectedVersion = unsupportedSchemaVersion(properties);
|
|
217
|
+
if (rejectedVersion !== void 0) {
|
|
150
218
|
this.options.onError?.(
|
|
151
|
-
new import_core.BabelQueueError(
|
|
152
|
-
|
|
219
|
+
new import_core.BabelQueueError(
|
|
220
|
+
`Rejected a message from Apache Pulsar with unsupported bq-schema-version "${rejectedVersion}" (supported: ${import_core.SCHEMA_VERSION}).`
|
|
221
|
+
),
|
|
222
|
+
{},
|
|
153
223
|
message
|
|
154
224
|
);
|
|
155
225
|
await this.consumer.negativeAcknowledge(message);
|
|
156
226
|
return;
|
|
157
227
|
}
|
|
158
|
-
const
|
|
228
|
+
const redeliveryCount = typeof message.getRedeliveryCount === "function" ? message.getRedeliveryCount() : 0;
|
|
229
|
+
const envelope = import_core.EnvelopeCodec.decode(decodePayload(message.getData()));
|
|
230
|
+
const bodyUrn = import_core.EnvelopeCodec.urn(envelope);
|
|
231
|
+
const rawJob = properties["bq-job"];
|
|
232
|
+
const propertyUrn = typeof rawJob === "string" ? rawJob.trim() : "";
|
|
233
|
+
const urn = propertyUrn !== "" ? propertyUrn : bodyUrn;
|
|
234
|
+
if (!import_core.EnvelopeCodec.accepts(envelope)) {
|
|
235
|
+
const error = new import_core.BabelQueueError("Rejected a non-conformant BabelQueue envelope from Apache Pulsar.");
|
|
236
|
+
this.options.onError?.(error, envelope, message);
|
|
237
|
+
await this.settle("poison", reconcileAttempts(properties, 0, redeliveryCount) + 1, null, message, error);
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
const bodyAttempts = envelope.attempts;
|
|
241
|
+
const attempts = reconcileAttempts(properties, bodyAttempts, redeliveryCount);
|
|
242
|
+
if (bodyAttempts !== attempts) {
|
|
243
|
+
envelope.attempts = attempts;
|
|
244
|
+
}
|
|
245
|
+
if (propertyUrn !== "" && bodyUrn !== "" && propertyUrn !== bodyUrn) {
|
|
246
|
+
this.notifyRoutingMismatch(propertyUrn, bodyUrn, envelope);
|
|
247
|
+
}
|
|
159
248
|
const handler = this.handlers[urn];
|
|
160
249
|
if (!handler) {
|
|
161
250
|
if (this.options.onUnknownUrn) {
|
|
162
251
|
await this.options.onUnknownUrn(envelope, message);
|
|
163
252
|
await this.consumer.acknowledge(message);
|
|
164
253
|
} else {
|
|
165
|
-
|
|
166
|
-
|
|
254
|
+
const error = new import_core.UnknownUrnError(urn);
|
|
255
|
+
this.options.onError?.(error, envelope, message);
|
|
256
|
+
await this.settle("unknown_urn", attempts + 1, envelope, message, error);
|
|
167
257
|
}
|
|
168
258
|
return;
|
|
169
259
|
}
|
|
@@ -172,12 +262,163 @@ var PulsarConsumer = class {
|
|
|
172
262
|
await this.consumer.acknowledge(message);
|
|
173
263
|
} catch (error) {
|
|
174
264
|
this.options.onError?.(error, envelope, message);
|
|
265
|
+
await this.settle("failed", attempts + 1, envelope, message, error);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* The settle decision of a retryable outcome (ADR-0036 decisions 1 and 2). `counted` is the reconciled
|
|
270
|
+
* attempts plus one for this delivery. A poison body with dead-lettering on is recorded without retry;
|
|
271
|
+
* every other outcome retries (`negativeAcknowledge`: the native redelivery count advances, so the
|
|
272
|
+
* next delivery counts one more) while `counted < maxAttempts`, then dead-letters or drops.
|
|
273
|
+
*/
|
|
274
|
+
async settle(kind, counted, envelope, message, error) {
|
|
275
|
+
const producer = this.options.deadLetterProducer;
|
|
276
|
+
if (kind === "poison" && producer !== void 0) {
|
|
277
|
+
await this.deadLetter(producer, kind, counted, null, message, error);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (counted < this.maxAttempts) {
|
|
281
|
+
await this.retry(counted, envelope, message);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (producer !== void 0) {
|
|
285
|
+
await this.deadLetter(producer, kind, counted, envelope, message, error);
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
this.warn(
|
|
289
|
+
`babelqueue: dropped a ${kind} Apache Pulsar message after ${counted} of ${this.maxAttempts} deliveries (dead-lettering is off).`
|
|
290
|
+
);
|
|
291
|
+
await this.consumer.acknowledge(message);
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Retry one delivery. With a {@link PulsarConsumerOptions.retryProducer}: re-publish to the consumer's
|
|
295
|
+
* own topic carrying `bq-attempts = counted` (reconcile = max(bq-attempts, RedeliveryCount) on the next
|
|
296
|
+
* delivery), then acknowledge the original — only after the send succeeded. Otherwise `negativeAcknowledge`.
|
|
297
|
+
*/
|
|
298
|
+
async retry(counted, envelope, message) {
|
|
299
|
+
const producer = await this.resolveRetryProducer();
|
|
300
|
+
if (producer === void 0) {
|
|
301
|
+
await this.consumer.negativeAcknowledge(message);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
try {
|
|
305
|
+
await producer.send(this.retryMessage(counted, envelope, message));
|
|
306
|
+
} catch (writeError) {
|
|
307
|
+
this.warn(
|
|
308
|
+
`babelqueue: retry re-publish to ${producer.getTopic()} failed (${errorText(writeError)}); the message is left for redelivery.`
|
|
309
|
+
);
|
|
310
|
+
await this.consumer.negativeAcknowledge(message);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
await this.consumer.acknowledge(message);
|
|
314
|
+
}
|
|
315
|
+
retryMessage(counted, envelope, message) {
|
|
316
|
+
const carried = headersOf(message);
|
|
317
|
+
delete carried["bq-delay"];
|
|
318
|
+
if (envelope !== null) {
|
|
319
|
+
return keepRoute(toPulsarMessage({ ...envelope, attempts: counted }, carried), message);
|
|
320
|
+
}
|
|
321
|
+
const data = message.getData();
|
|
322
|
+
return keepRoute(
|
|
323
|
+
{
|
|
324
|
+
data: typeof data === "string" ? Buffer.from(data, "utf8") : Buffer.from(data),
|
|
325
|
+
properties: { ...carried, "bq-attempts": String(counted) }
|
|
326
|
+
},
|
|
327
|
+
message
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Write the DLQ record, then acknowledge the original — only after the send succeeded. A failed send
|
|
332
|
+
* leaves the original negatively acknowledged, so it is redelivered and the write is tried again.
|
|
333
|
+
*/
|
|
334
|
+
async deadLetter(producer, kind, counted, envelope, message, error) {
|
|
335
|
+
try {
|
|
336
|
+
await producer.send(this.deadLetterMessage(producer, kind, counted, envelope, message, error));
|
|
337
|
+
} catch (writeError) {
|
|
338
|
+
this.warn(
|
|
339
|
+
`babelqueue: dead-letter write to ${producer.getTopic()} failed (${errorText(writeError)}); the message is left for redelivery.`
|
|
340
|
+
);
|
|
175
341
|
await this.consumer.negativeAcknowledge(message);
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
await this.consumer.acknowledge(message);
|
|
345
|
+
}
|
|
346
|
+
deadLetterMessage(producer, kind, counted, envelope, message, error) {
|
|
347
|
+
const details = {
|
|
348
|
+
attempts: counted,
|
|
349
|
+
error: error instanceof Error ? error.message : null,
|
|
350
|
+
exception: error instanceof Error ? error.name : null
|
|
351
|
+
};
|
|
352
|
+
if (envelope === null) {
|
|
353
|
+
const original2 = this.options.queue ?? stripDlqSuffix(topicName(producer.getTopic()));
|
|
354
|
+
const delivered = counted - 1;
|
|
355
|
+
const deadLetter = (0, import_core.annotate)({ attempts: delivered }, kind, original2, {
|
|
356
|
+
...details,
|
|
357
|
+
attempts: delivered
|
|
358
|
+
}).dead_letter;
|
|
359
|
+
const data = message.getData();
|
|
360
|
+
const body = typeof data === "string" ? Buffer.from(data, "utf8") : Buffer.from(data);
|
|
361
|
+
const record = (0, import_core.buildPoisonRecord)(body, JSON.stringify(deadLetter));
|
|
362
|
+
return keepRoute({ data: Buffer.from(record, "utf8"), properties: poisonProperties(record) }, message);
|
|
363
|
+
}
|
|
364
|
+
const original = envelope.meta?.queue || this.options.queue || stripDlqSuffix(topicName(producer.getTopic()));
|
|
365
|
+
const annotated = (0, import_core.annotate)({ ...envelope, attempts: counted }, kind, original, details);
|
|
366
|
+
return keepRoute(toPulsarMessage(annotated), message);
|
|
367
|
+
}
|
|
368
|
+
notifyRoutingMismatch(propertyUrn, bodyUrn, envelope) {
|
|
369
|
+
const hook = this.options.onRoutingMismatch;
|
|
370
|
+
if (hook === void 0) return;
|
|
371
|
+
try {
|
|
372
|
+
const result = hook(propertyUrn, bodyUrn, envelope);
|
|
373
|
+
Promise.resolve(result).catch(() => void 0);
|
|
374
|
+
} catch {
|
|
176
375
|
}
|
|
177
376
|
}
|
|
178
377
|
};
|
|
378
|
+
function safeProperties(message) {
|
|
379
|
+
try {
|
|
380
|
+
const properties = typeof message.getProperties === "function" ? message.getProperties() : void 0;
|
|
381
|
+
return properties ?? {};
|
|
382
|
+
} catch {
|
|
383
|
+
return {};
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
function keepRoute(outgoing, incoming) {
|
|
387
|
+
const route = safeProperties(incoming)["bq-job"];
|
|
388
|
+
if (typeof route === "string" && route.trim() !== "") {
|
|
389
|
+
outgoing.properties = { ...outgoing.properties, "bq-job": route };
|
|
390
|
+
}
|
|
391
|
+
return outgoing;
|
|
392
|
+
}
|
|
393
|
+
function errorText(error) {
|
|
394
|
+
return error instanceof Error ? error.message : String(error);
|
|
395
|
+
}
|
|
396
|
+
function stripDlqSuffix(topic) {
|
|
397
|
+
return topic.endsWith(".dlq") && topic.length > 4 ? topic.slice(0, -4) : topic;
|
|
398
|
+
}
|
|
399
|
+
function poisonProperties(recordJson) {
|
|
400
|
+
const properties = {};
|
|
401
|
+
let record;
|
|
402
|
+
try {
|
|
403
|
+
record = JSON.parse(recordJson);
|
|
404
|
+
} catch {
|
|
405
|
+
return properties;
|
|
406
|
+
}
|
|
407
|
+
if (record === null || typeof record !== "object") return properties;
|
|
408
|
+
const top = record;
|
|
409
|
+
const str = (value) => typeof value === "string" && value !== "" ? value : void 0;
|
|
410
|
+
const job = str(top["job"]) ?? str(top["urn"]);
|
|
411
|
+
if (job) properties["bq-job"] = job;
|
|
412
|
+
const trace = str(top["trace_id"]);
|
|
413
|
+
if (trace) properties["bq-trace-id"] = trace;
|
|
414
|
+
const meta = top["meta"] !== null && typeof top["meta"] === "object" ? top["meta"] : {};
|
|
415
|
+
const id = str(meta["id"]);
|
|
416
|
+
if (id) properties["bq-message-id"] = id;
|
|
417
|
+
return properties;
|
|
418
|
+
}
|
|
179
419
|
// Annotate the CommonJS export names for ESM import in node:
|
|
180
420
|
0 && (module.exports = {
|
|
421
|
+
DEFAULT_MAX_ATTEMPTS,
|
|
181
422
|
PulsarConsumer,
|
|
182
423
|
PulsarPublisher,
|
|
183
424
|
headersOf,
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/headers.ts"],"sourcesContent":["/**\n * Apache Pulsar adapter for BabelQueue.\n *\n * A canonical-envelope **publisher** and a URN-routed **consumer** over Apache Pulsar, so a\n * Pulsar-based Node service speaks the same contract (envelope shape, URN identity, trace\n * propagation) as the .NET, Java, Python and Go SDKs.\n *\n * import Pulsar from \"pulsar-client\";\n * import { PulsarPublisher, PulsarConsumer } from \"@babelqueue/pulsar\";\n *\n * const client = new Pulsar.Client({ serviceUrl: \"pulsar://localhost:6650\" });\n *\n * const producer = await client.createProducer({ topic: \"orders\" });\n * await new PulsarPublisher(producer).publish(\"urn:babel:orders:created\", { order_id: 1042 });\n *\n * const sub = await client.subscribe({\n * topic: \"orders\", subscription: \"babelqueue\", subscriptionType: \"Shared\",\n * });\n * const consumer = new PulsarConsumer(sub, {\n * \"urn:babel:orders:created\": async (env) => { ... },\n * });\n * await consumer.run();\n *\n * This implements §5 of the broker-bindings contract: the canonical envelope is the message\n * payload, projected onto native Pulsar message properties (string→string) — `bq-job` = URN,\n * `bq-trace-id` = trace_id, `bq-message-id` = meta.id, plus `bq-schema-version` /\n * `bq-source-lang` / `bq-attempts`. The envelope is unchanged (`schema_version` stays 1);\n * Pulsar is purely additive. A failed handler `negativeAcknowledge`s the message, so it is\n * redelivered (at-least-once) and `getRedeliveryCount()` is incremented; the authoritative\n * attempt count is the body's `bq-attempts`, reconciled to `max(bq-attempts,\n * getRedeliveryCount())` — no −1, because Pulsar's redelivery count is 0-based.\n *\n * **Out-of-band headers (ADR-0028).** A `headers` carrier (e.g. a W3C `traceparent` for cross-hop\n * span linkage) rides as additional native Pulsar **message properties** beside the contract `bq-*`\n * properties — the contract properties win a key collision (merge-not-clobber), and blanks are\n * dropped. On consume the inbound `getProperties()` are surfaced as a `Record<string,string>` so\n * the core's `otel` extract sees the `traceparent` and links the consumer span as a child. A\n * header-less publish is byte-identical. GR-1: the wire envelope (the payload) is never touched.\n */\n\nimport { BabelQueueError, EnvelopeCodec, UnknownUrnError } from \"@babelqueue/core\";\nimport type { Envelope, HeaderCarrier, IncomingEnvelope } from \"@babelqueue/core\";\n\nimport { mergeInto } from \"./headers.js\";\n\n// --- Minimal Pulsar shapes (a structural subset of pulsar-client) --------------\n\n/** A message to send (structural subset of pulsar-client `ProducerMessage`). */\nexport interface PulsarProducerMessage {\n data: Buffer;\n properties?: { [key: string]: string };\n /** Relative delay in milliseconds before the message becomes available (native `deliverAfter`). */\n deliverAfter?: number;\n}\n\n/**\n * The subset of the Pulsar producer this adapter calls. A `Producer` from `pulsar-client`\n * satisfies it structurally; a fake satisfies it in tests.\n */\nexport interface PulsarProducer {\n getTopic(): string;\n send(message: PulsarProducerMessage): Promise<unknown>;\n}\n\n/** A received message (structural subset of pulsar-client `Message`). */\nexport interface PulsarReceivedMessage {\n getData(): Buffer | string;\n getProperties(): { [key: string]: string };\n getRedeliveryCount(): number;\n}\n\n/**\n * The subset of the Pulsar consumer this adapter calls. A `Consumer` from `pulsar-client`\n * satisfies it structurally; a fake satisfies it in tests.\n */\nexport interface PulsarConsumerClient {\n receive(timeoutMs?: number): Promise<PulsarReceivedMessage>;\n acknowledge(message: PulsarReceivedMessage): Promise<void> | void;\n negativeAcknowledge(message: PulsarReceivedMessage): Promise<void> | void;\n}\n\n// --- Property projection (contract §5.2) ---------------------------------------\n\n/** Bare topic name (the part after the last `/`), used as `meta.queue`. */\nfunction topicName(topic: string): string {\n const slash = topic.lastIndexOf(\"/\");\n return slash >= 0 ? topic.slice(slash + 1) : topic;\n}\n\n/**\n * Project the envelope's contract fields onto native Pulsar message properties\n * (string→string): `bq-job` = URN, `bq-trace-id` = trace_id, `bq-message-id` = meta.id, plus\n * `bq-schema-version` / `bq-source-lang` / `bq-attempts`. The body stays authoritative.\n */\nexport function pulsarProperties(envelope: Envelope): { [key: string]: string } {\n const properties: { [key: string]: string } = {};\n if (envelope.job) properties[\"bq-job\"] = envelope.job;\n if (envelope.trace_id) properties[\"bq-trace-id\"] = envelope.trace_id;\n if (envelope.meta.id) properties[\"bq-message-id\"] = envelope.meta.id;\n if (envelope.meta.schema_version != null) {\n properties[\"bq-schema-version\"] = String(envelope.meta.schema_version);\n }\n if (envelope.meta.lang) properties[\"bq-source-lang\"] = envelope.meta.lang;\n properties[\"bq-attempts\"] = String(envelope.attempts ?? 0);\n return properties;\n}\n\n/**\n * Project the envelope onto a native Pulsar producer message (payload + properties). When `extra`\n * is given, those out-of-band headers (e.g. a `traceparent`, ADR-0028) are merged beside the\n * contract `bq-*` properties without clobbering them (the contract wins a key collision).\n */\nexport function toPulsarMessage(envelope: Envelope, extra?: HeaderCarrier | null): PulsarProducerMessage {\n return {\n data: Buffer.from(EnvelopeCodec.encode(envelope), \"utf8\"),\n properties: mergeInto(pulsarProperties(envelope), extra),\n };\n}\n\n/**\n * Read a received message's native properties back into a flat {@link HeaderCarrier} via\n * `getProperties()`. Returns an empty object when there are none. Both the contract `bq-*`\n * properties and any out-of-band rider (e.g. `traceparent`) surface — the core's `otel` extract\n * reads only the keys it knows.\n */\nexport function headersOf(message: PulsarReceivedMessage): HeaderCarrier {\n const props = typeof message.getProperties === \"function\" ? message.getProperties() : undefined;\n const out: HeaderCarrier = {};\n if (!props) return out;\n for (const key of Object.keys(props)) {\n const value = props[key];\n if (value != null && value !== \"\") out[key] = value;\n }\n return out;\n}\n\n// --- Publisher -----------------------------------------------------------------\n\n/** Options for {@link PulsarPublisher.publish}. */\nexport interface PublishOptions {\n /** Reuse an existing trace id (trace continuation). */\n traceId?: string;\n /** Schedule native delayed delivery this many milliseconds from now (`deliverAfter`). */\n delayMs?: number;\n /**\n * Out-of-band transport headers carried as native Pulsar message properties beside the contract\n * `bq-*` properties (ADR-0028) — e.g. a W3C `traceparent` written by `@babelqueue/core/otel`'s\n * `publish`. The contract properties win a key collision; an empty/omitted carrier leaves the\n * message unchanged.\n */\n headers?: HeaderCarrier;\n}\n\n/** Sends canonical-envelope messages to one Pulsar topic with the §5 property projection. */\nexport class PulsarPublisher {\n constructor(private readonly producer: PulsarProducer) {}\n\n /**\n * Build the canonical envelope for `(urn, data)`, send it with the §5 property projection,\n * and return the message id (`meta.id`). A positive `delayMs` schedules native delayed\n * delivery via `deliverAfter` and mirrors `bq-delay`.\n */\n async publish(\n urn: string,\n data: Record<string, unknown>,\n options: PublishOptions = {},\n ): Promise<string> {\n const envelope = EnvelopeCodec.make(urn, data, {\n queue: topicName(this.producer.getTopic()),\n traceId: options.traceId,\n });\n const message = toPulsarMessage(envelope, options.headers);\n\n if (options.delayMs != null && options.delayMs > 0) {\n message.properties = { ...message.properties, \"bq-delay\": String(options.delayMs) };\n message.deliverAfter = options.delayMs;\n }\n\n await this.producer.send(message);\n return envelope.meta.id;\n }\n}\n\n// --- Consumer ------------------------------------------------------------------\n\n/**\n * A URN handler. Receives the validated envelope, the raw Pulsar message, and the out-of-band\n * {@link HeaderCarrier} read from the message's properties (empty when there are none). Pass\n * `headers` to `@babelqueue/core/otel`'s `wrapHandler` to link the consumer span as a child of the\n * producer span (ADR-0028).\n */\nexport type BabelHandler = (envelope: Envelope, message: PulsarReceivedMessage, headers: HeaderCarrier) => unknown | Promise<unknown>;\n\n/** A map of URN → handler. */\nexport type BabelHandlers = Record<string, BabelHandler>;\n\n/** Options for {@link PulsarConsumer}. */\nexport interface PulsarConsumerOptions {\n /** Called instead of erroring when a message's URN has no handler (then the message is acknowledged). */\n onUnknownUrn?: (envelope: IncomingEnvelope, message: PulsarReceivedMessage) => unknown | Promise<unknown>;\n /** Called for a non-conformant envelope, an unmapped URN (no `onUnknownUrn`), or a throwing handler. The loop never stops. */\n onError?: (error: unknown, envelope: IncomingEnvelope, message: PulsarReceivedMessage) => void;\n /** Per-receive timeout in ms (default 1000). A timeout yields an empty poll, not an error. */\n receiveTimeoutMs?: number;\n}\n\nfunction isReceiveTimeout(error: unknown): boolean {\n const message = error instanceof Error ? error.message : String(error);\n return /timeout/i.test(message);\n}\n\nfunction decodePayload(data: Buffer | string): string {\n return typeof data === \"string\" ? data : data.toString(\"utf8\");\n}\n\n/**\n * Receives from a Pulsar subscription, decodes + validates each message, routes it to the\n * handler for its URN, and acknowledges it on success. A throwing handler\n * `negativeAcknowledge`s the message — the broker redelivers it and increments\n * `getRedeliveryCount()` (at-least-once); `attempts` is reconciled to\n * `max(bq-attempts, getRedeliveryCount())` for the handler.\n */\nexport class PulsarConsumer {\n constructor(\n private readonly consumer: PulsarConsumerClient,\n private readonly handlers: BabelHandlers,\n private readonly options: PulsarConsumerOptions = {},\n ) {}\n\n /** Receive one message (up to the receive timeout), route + settle it. Returns 1, or 0 on timeout. */\n async poll(): Promise<number> {\n let message: PulsarReceivedMessage;\n try {\n message = await this.consumer.receive(this.options.receiveTimeoutMs ?? 1000);\n } catch (error) {\n if (isReceiveTimeout(error)) return 0;\n throw error;\n }\n await this.handle(message);\n return 1;\n }\n\n /** Poll until `signal` aborts. */\n async run(signal?: AbortSignal): Promise<void> {\n while (signal?.aborted !== true) {\n await this.poll();\n }\n }\n\n private async handle(message: PulsarReceivedMessage): Promise<void> {\n const envelope = EnvelopeCodec.decode(decodePayload(message.getData()));\n\n // attempts = max(current, getRedeliveryCount()): the redelivery count is 0-based, so it\n // maps directly with no −1; the max never lowers a higher body count republished from\n // another SDK (the Go/Python transports retry by re-sending with attempts+1).\n const redeliveryCount = typeof message.getRedeliveryCount === \"function\" ? message.getRedeliveryCount() : 0;\n const current = typeof envelope.attempts === \"number\" ? envelope.attempts : 0;\n if (redeliveryCount > current) {\n envelope.attempts = redeliveryCount;\n }\n\n if (!EnvelopeCodec.accepts(envelope)) {\n this.options.onError?.(\n new BabelQueueError(\"Rejected a non-conformant BabelQueue envelope from Apache Pulsar.\"),\n envelope,\n message,\n );\n await this.consumer.negativeAcknowledge(message);\n return;\n }\n\n const urn = EnvelopeCodec.urn(envelope);\n const handler = this.handlers[urn];\n if (!handler) {\n if (this.options.onUnknownUrn) {\n await this.options.onUnknownUrn(envelope, message);\n await this.consumer.acknowledge(message);\n } else {\n this.options.onError?.(new UnknownUrnError(urn), envelope, message);\n await this.consumer.negativeAcknowledge(message);\n }\n return;\n }\n\n try {\n await handler(envelope as Envelope, message, headersOf(message));\n await this.consumer.acknowledge(message);\n } catch (error) {\n // Negative-ack releases the message — the broker redelivers and increments the count.\n this.options.onError?.(error, envelope, message);\n await this.consumer.negativeAcknowledge(message);\n }\n }\n}\n","/**\n * Pure, dependency-free helpers for the out-of-band transport-header seam (ADR-0028) — the Node\n * adapter mirror of Go's `sanitizeHeaders` and PHP's `Support\\Headers`. They fold an injected\n * {@link HeaderCarrier} (e.g. a W3C `traceparent`) onto a transport's metadata channel **without\n * clobbering** the contract headers it already carries.\n *\n * Headers ride beside the frozen wire envelope (GR-1), never inside it.\n */\n\nimport type { HeaderCarrier } from \"@babelqueue/core\";\n\n/**\n * Copy `headers`, dropping blank keys and blank values. Returns an empty object when nothing\n * survives. A nullish input yields an empty object.\n */\nexport function sanitizeHeaders(headers: HeaderCarrier | null | undefined): HeaderCarrier {\n const out: HeaderCarrier = {};\n if (!headers) return out;\n for (const key of Object.keys(headers)) {\n const value = headers[key];\n if (key === \"\" || value == null || value === \"\") continue;\n out[key] = value;\n }\n return out;\n}\n\n/**\n * Merge out-of-band `extra` headers onto an already-built `base` map **without clobbering** the\n * contract headers in `base` (they win a key collision) and dropping blanks. Returns `base`,\n * mutated in place.\n */\nexport function mergeInto(base: { [key: string]: string }, extra: HeaderCarrier | null | undefined): { [key: string]: string } {\n for (const [key, value] of Object.entries(sanitizeHeaders(extra))) {\n if (key in base) continue; // the contract header already there wins\n base[key] = value;\n }\n return base;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAA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F,YAAM,QAAQ,UAAsB,SAAS,UAAU,OAAO,CAAC;AAC/D,YAAM,KAAK,SAAS,YAAY,OAAO;AAAA,IACzC,SAAS,OAAO;AAEd,WAAK,QAAQ,UAAU,OAAO,UAAU,OAAO;AAC/C,YAAM,KAAK,SAAS,oBAAoB,OAAO;AAAA,IACjD;AAAA,EACF;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/headers.ts"],"sourcesContent":["/**\n * Apache Pulsar adapter for BabelQueue.\n *\n * A canonical-envelope **publisher** and a URN-routed **consumer** over Apache Pulsar, so a\n * Pulsar-based Node service speaks the same contract (envelope shape, URN identity, trace\n * propagation) as the .NET, Java, Python and Go SDKs.\n *\n * import Pulsar from \"pulsar-client\";\n * import { PulsarPublisher, PulsarConsumer } from \"@babelqueue/pulsar\";\n *\n * const client = new Pulsar.Client({ serviceUrl: \"pulsar://localhost:6650\" });\n *\n * const producer = await client.createProducer({ topic: \"orders\" });\n * await new PulsarPublisher(producer).publish(\"urn:babel:orders:created\", { order_id: 1042 });\n *\n * const sub = await client.subscribe({\n * topic: \"orders\", subscription: \"babelqueue\", subscriptionType: \"Shared\",\n * });\n * const consumer = new PulsarConsumer(sub, {\n * \"urn:babel:orders:created\": async (env) => { ... },\n * }, { subscriptionType: \"Shared\" }); // declare it (or pass `client` together with `topic`): see PulsarConsumerOptions\n * await consumer.run();\n *\n * This implements §5 of the broker-bindings contract: the canonical envelope is the message\n * payload, projected onto native Pulsar message properties (string→string) — `bq-job` = URN,\n * `bq-trace-id` = trace_id, `bq-message-id` = meta.id, plus `bq-schema-version` /\n * `bq-source-lang` / `bq-attempts`. The envelope is unchanged (`schema_version` stays 1);\n * Pulsar is purely additive. A failed handler `negativeAcknowledge`s the message, so it is\n * redelivered (at-least-once) and `getRedeliveryCount()` is incremented — up to `maxAttempts`\n * deliveries (default 3, ADR-0036): the message that exhausts them is written to `<queue>.dlq`\n * (when a `deadLetterProducer` is given) or acknowledged and dropped. A body that does not decode\n * into an envelope (including an object with no valid identity) is poison: it never reaches a\n * handler, and with dead-lettering on it is recorded in `<queue>.dlq` in the ADR-0035 shape without\n * retry. Per §5.2 / §5.7 the\n * consumer routes on the `bq-job` property (the body's URN is used only when the property is\n * absent or blank) and takes `attempts` from the authoritative `bq-attempts` property (the\n * body's value is used only when the property is absent or not a plain ASCII-decimal integer\n * that fits a 32-bit int), reconciled to `max(bq-attempts, getRedeliveryCount())` — no −1,\n * because Pulsar's redelivery count is 0-based. The handler still receives the body's envelope.\n *\n * **Version gate (§5.7 step 3).** Before the body is decoded the consumer reads the\n * `bq-schema-version` property. A missing or blank value (empty or only ASCII whitespace) changes nothing; any value other than\n * the core's supported `SCHEMA_VERSION` (`\"2\"`, `\"x\"`, `\"01\"`, ...) rejects the message undecoded\n * through the same path as a non-conformant envelope — `onError` (with an error naming the\n * version, and an empty envelope since nothing was decoded) and a `negativeAcknowledge`.\n *\n * **Out-of-band headers (ADR-0028).** A `headers` carrier (e.g. a W3C `traceparent` for cross-hop\n * span linkage) rides as additional native Pulsar **message properties** beside the contract `bq-*`\n * properties — the contract properties win a key collision (merge-not-clobber), and blanks are\n * dropped. On consume the inbound `getProperties()` are surfaced as a `Record<string,string>` so\n * the core's `otel` extract sees the `traceparent` and links the consumer span as a child. A\n * header-less publish is byte-identical. GR-1: the wire envelope (the payload) is never touched.\n */\n\nimport { BabelQueueError, EnvelopeCodec, SCHEMA_VERSION, UnknownUrnError, annotate, buildPoisonRecord } from \"@babelqueue/core\";\nimport type { Envelope, HeaderCarrier, IncomingEnvelope } from \"@babelqueue/core\";\n\nimport { mergeInto } from \"./headers.js\";\n\n// --- Minimal Pulsar shapes (a structural subset of pulsar-client) --------------\n\n/** A message to send (structural subset of pulsar-client `ProducerMessage`). */\nexport interface PulsarProducerMessage {\n data: Buffer;\n properties?: { [key: string]: string };\n /** Relative delay in milliseconds before the message becomes available (native `deliverAfter`). */\n deliverAfter?: number;\n}\n\n/**\n * The subset of the Pulsar producer this adapter calls. A `Producer` from `pulsar-client`\n * satisfies it structurally; a fake satisfies it in tests.\n */\nexport interface PulsarProducer {\n getTopic(): string;\n send(message: PulsarProducerMessage): Promise<unknown>;\n}\n\n/** A received message (structural subset of pulsar-client `Message`). */\nexport interface PulsarReceivedMessage {\n getData(): Buffer | string;\n getProperties(): { [key: string]: string };\n getRedeliveryCount(): number;\n}\n\n/**\n * The subset of the Pulsar consumer this adapter calls. A `Consumer` from `pulsar-client`\n * satisfies it structurally; a fake satisfies it in tests.\n */\nexport interface PulsarConsumerClient {\n /**\n * The topic the subscription reads. pulsar-client's `Consumer` has **no** such method (only its `Producer`\n * does), so with it the topic comes from the {@link PulsarConsumerOptions.topic} option; a custom\n * subscription wrapper may provide this to make `topic` unnecessary.\n */\n getTopic?(): string;\n receive(timeoutMs?: number): Promise<PulsarReceivedMessage>;\n acknowledge(message: PulsarReceivedMessage): Promise<void> | void;\n negativeAcknowledge(message: PulsarReceivedMessage): Promise<void> | void;\n}\n\n/**\n * The subset of the Pulsar client this adapter calls to build its own retry producer. A `Client` from\n * `pulsar-client` satisfies it structurally.\n */\nexport interface PulsarClientLike {\n createProducer(config: { topic: string }): Promise<PulsarProducer>;\n}\n\n/** The subscription type, as pulsar-client names it (`Key_Shared` is accepted as an alias of `KeyShared`). */\nexport type PulsarSubscriptionType = \"Exclusive\" | \"Shared\" | \"Failover\" | \"KeyShared\" | \"Key_Shared\";\n\n// --- Property projection (contract §5.2) ---------------------------------------\n\n/** Bare topic name (the part after the last `/`), used as `meta.queue`. */\nfunction topicName(topic: string): string {\n const slash = topic.lastIndexOf(\"/\");\n return slash >= 0 ? topic.slice(slash + 1) : topic;\n}\n\n/**\n * Project the envelope's contract fields onto native Pulsar message properties\n * (string→string): `bq-job` = URN, `bq-trace-id` = trace_id, `bq-message-id` = meta.id, plus\n * `bq-schema-version` / `bq-source-lang` / `bq-attempts`. The body stays authoritative.\n */\nexport function pulsarProperties(envelope: Envelope): { [key: string]: string } {\n const properties: { [key: string]: string } = {};\n if (envelope.job) properties[\"bq-job\"] = envelope.job;\n if (envelope.trace_id) properties[\"bq-trace-id\"] = envelope.trace_id;\n if (envelope.meta.id) properties[\"bq-message-id\"] = envelope.meta.id;\n if (envelope.meta.schema_version != null) {\n properties[\"bq-schema-version\"] = String(envelope.meta.schema_version);\n }\n if (envelope.meta.lang) properties[\"bq-source-lang\"] = envelope.meta.lang;\n properties[\"bq-attempts\"] = String(envelope.attempts ?? 0);\n return properties;\n}\n\n/**\n * Project the envelope onto a native Pulsar producer message (payload + properties). When `extra`\n * is given, those out-of-band headers (e.g. a `traceparent`, ADR-0028) are merged beside the\n * contract `bq-*` properties without clobbering them (the contract wins a key collision).\n */\nexport function toPulsarMessage(envelope: Envelope, extra?: HeaderCarrier | null): PulsarProducerMessage {\n return {\n data: Buffer.from(EnvelopeCodec.encode(envelope), \"utf8\"),\n properties: mergeInto(pulsarProperties(envelope), extra),\n };\n}\n\n/**\n * Read a received message's native properties back into a flat {@link HeaderCarrier} via\n * `getProperties()`. Returns an empty object when there are none. Both the contract `bq-*`\n * properties and any out-of-band rider (e.g. `traceparent`) surface — the core's `otel` extract\n * reads only the keys it knows.\n */\nexport function headersOf(message: PulsarReceivedMessage): HeaderCarrier {\n const props = typeof message.getProperties === \"function\" ? message.getProperties() : undefined;\n const out: HeaderCarrier = {};\n if (!props) return out;\n for (const key of Object.keys(props)) {\n const value = props[key];\n if (value != null && value !== \"\") out[key] = value;\n }\n return out;\n}\n\n// --- Publisher -----------------------------------------------------------------\n\n/** Options for {@link PulsarPublisher.publish}. */\nexport interface PublishOptions {\n /** Reuse an existing trace id (trace continuation). */\n traceId?: string;\n /** Schedule native delayed delivery this many milliseconds from now (`deliverAfter`). */\n delayMs?: number;\n /**\n * Out-of-band transport headers carried as native Pulsar message properties beside the contract\n * `bq-*` properties (ADR-0028) — e.g. a W3C `traceparent` written by `@babelqueue/core/otel`'s\n * `publish`. The contract properties win a key collision; an empty/omitted carrier leaves the\n * message unchanged.\n */\n headers?: HeaderCarrier;\n}\n\n/** Sends canonical-envelope messages to one Pulsar topic with the §5 property projection. */\nexport class PulsarPublisher {\n constructor(private readonly producer: PulsarProducer) {}\n\n /**\n * Build the canonical envelope for `(urn, data)`, send it with the §5 property projection,\n * and return the message id (`meta.id`). A positive `delayMs` schedules native delayed\n * delivery via `deliverAfter` and mirrors `bq-delay`.\n */\n async publish(\n urn: string,\n data: Record<string, unknown>,\n options: PublishOptions = {},\n ): Promise<string> {\n const envelope = EnvelopeCodec.make(urn, data, {\n queue: topicName(this.producer.getTopic()),\n traceId: options.traceId,\n });\n const message = toPulsarMessage(envelope, options.headers);\n\n if (options.delayMs != null && options.delayMs > 0) {\n message.properties = { ...message.properties, \"bq-delay\": String(options.delayMs) };\n message.deliverAfter = options.delayMs;\n }\n\n await this.producer.send(message);\n return envelope.meta.id;\n }\n}\n\n// --- Consumer ------------------------------------------------------------------\n\n/**\n * A URN handler. Receives the validated envelope, the raw Pulsar message, and the out-of-band\n * {@link HeaderCarrier} read from the message's properties (empty when there are none). Pass\n * `headers` to `@babelqueue/core/otel`'s `wrapHandler` to link the consumer span as a child of the\n * producer span (ADR-0028).\n */\nexport type BabelHandler = (envelope: Envelope, message: PulsarReceivedMessage, headers: HeaderCarrier) => unknown | Promise<unknown>;\n\n/** A map of URN → handler. */\nexport type BabelHandlers = Record<string, BabelHandler>;\n\n/** Options for {@link PulsarConsumer}. */\nexport interface PulsarConsumerOptions {\n /** Called instead of erroring when a message's URN has no handler (then the message is acknowledged). */\n onUnknownUrn?: (envelope: IncomingEnvelope, message: PulsarReceivedMessage) => unknown | Promise<unknown>;\n /** Called for a non-conformant envelope, an unmapped URN (no `onUnknownUrn`), or a throwing handler. The loop never stops. */\n onError?: (error: unknown, envelope: IncomingEnvelope, message: PulsarReceivedMessage) => void;\n /** Per-receive timeout in ms (default 1000). A timeout yields an empty poll, not an error. */\n receiveTimeoutMs?: number;\n /**\n * Optional diagnostic hook, called when the `bq-job` property and the body's URN differ (the\n * property wins and the message is routed on it). `envelope` is the body's envelope. Separate\n * from {@link PulsarConsumerOptions.onError}: it is not an error, and anything it throws (or\n * rejects with) is swallowed — it never changes how the message is handled or acknowledged.\n */\n onRoutingMismatch?: (propertyUrn: string, bodyUrn: string, envelope: IncomingEnvelope) => unknown;\n /**\n * The total number of deliveries a message gets, the first included (default 3; `1` = no retry;\n * ADR-0036 decision 1). A failing handler, an unmapped URN without {@link onUnknownUrn} and a poison\n * body with dead-lettering off are retried (`negativeAcknowledge`) while `reconciled attempts + 1`\n * is below it; the delivery that reaches it is dead-lettered (when {@link deadLetterProducer} is\n * set) or acknowledged and dropped. Must be an integer of at least 1.\n */\n maxAttempts?: number;\n /**\n * A producer on the dead-letter topic (by convention `<queue>.dlq`). Dead-lettering is on exactly\n * when it is set; the SDK does not use Pulsar's native `DeadLetterPolicy`. The original message is\n * acknowledged only after the record was sent; a failed send leaves it negatively acknowledged so\n * it is redelivered and the write is tried again.\n */\n deadLetterProducer?: PulsarProducer;\n /**\n * A producer on the consumer's **own** topic. A retry re-publishes the message to that topic with\n * `bq-attempts` (and the body's `attempts`) raised to the counted value and then acknowledges the\n * original — the Go model. The count then lives in the message itself, so the bound holds on every\n * subscription type. Optional when {@link client} is given (the consumer builds the producer itself);\n * a failed re-publish leaves the original negatively acknowledged (never lost).\n *\n * Without a producer or a client a retry is a `negativeAcknowledge`, which advances the native\n * redelivery count only on `Shared` / `Key_Shared`: on `Exclusive` / `Failover` it stays 0 and the retry\n * would never reach `maxAttempts`, so that combination is rejected at construction (see\n * {@link subscriptionType}).\n */\n retryProducer?: PulsarProducer;\n /**\n * The Pulsar client the consumer builds its retry producer from, lazily on the first retry, on\n * {@link topic}. Needs `topic` (pulsar-client's `Consumer` cannot tell its topic). Ignored when\n * {@link retryProducer} is given.\n */\n client?: PulsarClientLike;\n /**\n * The topic the retry producer is created on — the topic the consumer reads. Required together with\n * {@link client} (a pulsar-client `Consumer` has no `getTopic()`); a subscription wrapper that provides\n * `getTopic()` makes it optional.\n */\n topic?: string;\n /**\n * The subscription type the consumer was created with. `Exclusive` and `Failover` cannot bound a\n * nack-driven retry (a negative acknowledgement does not advance the redelivery count there), so with\n * `maxAttempts > 1` they require a {@link retryProducer} or a {@link client}. **Not declaring it counts as\n * `Exclusive`** (pulsar-client's own default, and the fail-closed choice): without a `retryProducer` or a\n * `client` the constructor throws `BabelQueueError`. Declare `\"Shared\"` or `\"Key_Shared\"` to keep the\n * `negativeAcknowledge` retry without a producer.\n */\n subscriptionType?: PulsarSubscriptionType;\n /**\n * The queue name written to `dead_letter.original_queue` of a poison record (a decodable envelope\n * uses its own `meta.queue`). Defaults to the {@link deadLetterProducer} topic without its `.dlq`\n * suffix.\n */\n queue?: string;\n /** Receives the \"message dropped\" and \"dead-letter write failed\" warnings (default `console.warn`). */\n onWarn?: (message: string) => void;\n}\n\n/** What a retryable outcome is about: the reason written to `dead_letter.reason`. */\ntype SettleKind = \"failed\" | \"unknown_urn\" | \"poison\";\n\n/** Default total number of deliveries (ADR-0036 decision 1). */\nexport const DEFAULT_MAX_ATTEMPTS = 3;\n\n/** `bq-attempts` is valid only as ASCII digits that fit a signed 32-bit int (matches Java/.NET, GR-5). */\nconst MAX_ATTEMPTS = 2 ** 31 - 1;\n\nfunction parseAttempts(raw: string | undefined): number | undefined {\n if (typeof raw !== \"string\" || !/^[0-9]+$/.test(raw)) return undefined;\n const value = Number(raw);\n return value <= MAX_ATTEMPTS ? value : undefined;\n}\n\n/**\n * The reconciled attempt count of a delivery (broker-bindings §5.5): the `bq-attempts` property when\n * valid, else `bodyAttempts` (0 when the body has none), floored by the 0-based native redelivery count.\n */\nfunction reconcileAttempts(\n properties: { [key: string]: string },\n bodyAttempts: number,\n redeliveryCount: number,\n): number {\n let attempts = bodyAttempts;\n const fromProperty = parseAttempts(properties[\"bq-attempts\"]);\n if (fromProperty !== undefined) attempts = fromProperty;\n return redeliveryCount > attempts ? redeliveryCount : attempts;\n}\n\n/** Blank = empty or only ASCII whitespace (space, \\t, \\n, \\v, \\f, \\r) — not `String.prototype.trim()`'s wider set. */\nconst BLANK_ASCII = /^[ \\t\\n\\v\\f\\r]*$/;\n\n/**\n * The `bq-schema-version` value when it is present, non-blank and not the supported version\n * (compared against the core's `SCHEMA_VERSION`, exact string match: `\"01\"`, `\" 1\"`, `\"1.0\"` are\n * rejected). Blank means the empty string or only ASCII whitespace (the same set in every SDK, GR-5);\n * NBSP, U+0085, U+FEFF, U+3000 and the like are not blank. `undefined` when the message may be decoded.\n */\nfunction unsupportedSchemaVersion(properties: { [key: string]: string }): string | undefined {\n const raw: unknown = properties[\"bq-schema-version\"];\n if (typeof raw !== \"string\" || BLANK_ASCII.test(raw)) return undefined;\n return raw === String(SCHEMA_VERSION) ? undefined : raw;\n}\n\nfunction isReceiveTimeout(error: unknown): boolean {\n const message = error instanceof Error ? error.message : String(error);\n return /timeout/i.test(message);\n}\n\nfunction decodePayload(data: Buffer | string): string {\n return typeof data === \"string\" ? data : data.toString(\"utf8\");\n}\n\n/**\n * Receives from a Pulsar subscription, decodes + validates each message, routes it to the\n * handler for its URN, and acknowledges it on success. A throwing handler\n * `negativeAcknowledge`s the message — the broker redelivers it and increments\n * `getRedeliveryCount()` (at-least-once; with a `retryProducer` the message is instead re-published with\n * `bq-attempts` + 1, which also bounds `Exclusive` / `Failover` subscriptions) — until `maxAttempts` deliveries are used up; the last one is\n * dead-lettered to `<queue>.dlq` or acknowledged and dropped (ADR-0036). Routing uses the `bq-job`\n * property (body URN as the fallback); `attempts` is the `bq-attempts` property (body value as the\n * fallback) reconciled to `max(.., getRedeliveryCount())` for the handler.\n */\nexport class PulsarConsumer {\n private readonly maxAttempts: number;\n private builtRetryProducer: Promise<PulsarProducer | undefined> | undefined;\n private readonly warn: (message: string) => void;\n\n constructor(\n private readonly consumer: PulsarConsumerClient,\n private readonly handlers: BabelHandlers,\n private readonly options: PulsarConsumerOptions = {},\n ) {\n const maxAttempts = options.maxAttempts ?? DEFAULT_MAX_ATTEMPTS;\n if (!Number.isInteger(maxAttempts) || maxAttempts < 1) {\n throw new BabelQueueError(`maxAttempts must be an integer of at least 1, got ${String(maxAttempts)}.`);\n }\n this.maxAttempts = maxAttempts;\n this.checkRetryBound(maxAttempts);\n this.warn = options.onWarn ?? ((message) => console.warn(message));\n }\n\n /**\n * Setup check: on `Exclusive` / `Failover` (and an undeclared type) a negative acknowledgement does not advance the native\n * redelivery count, so a retry needs a re-publish. Fail loudly here instead of looping at run time.\n */\n private checkRetryBound(maxAttempts: number): void {\n const declared = this.options.subscriptionType;\n if (declared !== undefined && ![\"Exclusive\", \"Shared\", \"Failover\", \"KeyShared\"].includes(declared.replace(\"_\", \"\"))) {\n throw new BabelQueueError(`Unknown subscriptionType \"${String(declared)}\".`);\n }\n // An undeclared type is treated as Exclusive (fail closed).\n const type = declared === undefined ? \"Exclusive\" : declared.replace(\"_\", \"\");\n if (type !== \"Exclusive\" && type !== \"Failover\") return;\n if (maxAttempts === 1 || this.options.retryProducer !== undefined) return;\n if (this.options.client !== undefined && this.retryTopic() !== undefined) return;\n const shown = declared === undefined ? \"An undeclared subscriptionType (treated as Exclusive)\" : `A ${type} subscription`;\n if (this.options.client !== undefined) {\n // A client alone cannot name the topic: pulsar-client's Consumer has no getTopic().\n throw new BabelQueueError(\n `${shown} with a client needs the \\`topic\\` option (the topic this consumer reads) to build the retry producer: ` +\n \"pulsar-client's Consumer cannot report its own topic.\",\n );\n }\n throw new BabelQueueError(\n `${shown} needs a retryProducer (or a client plus a topic) to bound retries: ` +\n \"a negative acknowledgement does not advance the redelivery count there, so a retry would never reach maxAttempts. \" +\n 'Pass a client plus topic (the topic you subscribed to), or a retryProducer, or declare subscriptionType: \"Shared\" / \"Key_Shared\".',\n );\n }\n\n private retryTopic(): string | undefined {\n const configured = this.options.topic;\n if (configured !== undefined && configured !== \"\") return configured;\n const own = typeof this.consumer.getTopic === \"function\" ? this.consumer.getTopic() : undefined;\n return own !== undefined && own !== \"\" ? own : undefined;\n }\n\n /** The retry producer: the one given, else one built (once) from the client; undefined when neither exists. */\n private async resolveRetryProducer(): Promise<PulsarProducer | undefined> {\n if (this.options.retryProducer !== undefined) return this.options.retryProducer;\n const client = this.options.client;\n const topic = this.retryTopic();\n if (client === undefined || topic === undefined) return undefined;\n this.builtRetryProducer ??= client.createProducer({ topic }).catch((error: unknown) => {\n this.builtRetryProducer = undefined; // try again on the next retry\n this.warn(`babelqueue: creating the retry producer on ${topic} failed (${errorText(error)}).`);\n return undefined;\n });\n return this.builtRetryProducer;\n }\n\n /** Receive one message (up to the receive timeout), route + settle it. Returns 1, or 0 on timeout. */\n async poll(): Promise<number> {\n let message: PulsarReceivedMessage;\n try {\n message = await this.consumer.receive(this.options.receiveTimeoutMs ?? 1000);\n } catch (error) {\n if (isReceiveTimeout(error)) return 0;\n throw error;\n }\n await this.handle(message);\n return 1;\n }\n\n /** Poll until `signal` aborts. */\n async run(signal?: AbortSignal): Promise<void> {\n while (signal?.aborted !== true) {\n await this.poll();\n }\n }\n\n private async handle(message: PulsarReceivedMessage): Promise<void> {\n const properties = safeProperties(message);\n\n // §5.7 step 3: gate on bq-schema-version before the body is decoded.\n const rejectedVersion = unsupportedSchemaVersion(properties);\n if (rejectedVersion !== undefined) {\n this.options.onError?.(\n new BabelQueueError(\n `Rejected a message from Apache Pulsar with unsupported bq-schema-version \"${rejectedVersion}\" (supported: ${SCHEMA_VERSION}).`,\n ),\n {},\n message,\n );\n await this.consumer.negativeAcknowledge(message);\n return;\n }\n\n const redeliveryCount = typeof message.getRedeliveryCount === \"function\" ? message.getRedeliveryCount() : 0;\n const envelope = EnvelopeCodec.decode(decodePayload(message.getData()));\n\n // routing: the bq-job property wins; the body's URN is the fallback. A body with no URN is\n // still rejected below, whatever bq-job says.\n const bodyUrn = EnvelopeCodec.urn(envelope);\n const rawJob: unknown = properties[\"bq-job\"];\n const propertyUrn = typeof rawJob === \"string\" ? rawJob.trim() : \"\";\n const urn = propertyUrn !== \"\" ? propertyUrn : bodyUrn;\n\n if (!EnvelopeCodec.accepts(envelope)) {\n // Poison (ADR-0036 decisions 2 and 4): a body that does not decode into an envelope, including an\n // object with no valid identity, never reaches a handler. Its attempts come from the transport\n // alone (bq-attempts, the redelivery count): there is no decodable body to read them from.\n const error = new BabelQueueError(\"Rejected a non-conformant BabelQueue envelope from Apache Pulsar.\");\n this.options.onError?.(error, envelope, message);\n await this.settle(\"poison\", reconcileAttempts(properties, 0, redeliveryCount) + 1, null, message, error);\n return;\n }\n\n // attempts: the authoritative bq-attempts property (the body's value when it is absent or\n // invalid), then max(.., getRedeliveryCount()): the redelivery count is 0-based, so it maps\n // directly with no −1; the max never lowers a higher count republished from another SDK\n // (the Go/Python transports retry by re-sending with attempts+1).\n // accepts() above guarantees an integer body attempts; the value is written only on a change.\n const bodyAttempts = envelope.attempts as number;\n const attempts = reconcileAttempts(properties, bodyAttempts, redeliveryCount);\n if (bodyAttempts !== attempts) {\n envelope.attempts = attempts;\n }\n\n // Diagnostics only, and only for an accepted envelope (07a Java/.NET order): after accepts()\n // and the attempts reconcile, so the hook sees the same envelope the handler gets.\n if (propertyUrn !== \"\" && bodyUrn !== \"\" && propertyUrn !== bodyUrn) {\n this.notifyRoutingMismatch(propertyUrn, bodyUrn, envelope);\n }\n\n const handler = this.handlers[urn];\n if (!handler) {\n if (this.options.onUnknownUrn) {\n await this.options.onUnknownUrn(envelope, message);\n await this.consumer.acknowledge(message);\n } else {\n const error = new UnknownUrnError(urn);\n this.options.onError?.(error, envelope, message);\n await this.settle(\"unknown_urn\", attempts + 1, envelope as Envelope, message, error);\n }\n return;\n }\n\n try {\n await handler(envelope as Envelope, message, headersOf(message));\n await this.consumer.acknowledge(message);\n } catch (error) {\n this.options.onError?.(error, envelope, message);\n await this.settle(\"failed\", attempts + 1, envelope as Envelope, message, error);\n }\n }\n\n /**\n * The settle decision of a retryable outcome (ADR-0036 decisions 1 and 2). `counted` is the reconciled\n * attempts plus one for this delivery. A poison body with dead-lettering on is recorded without retry;\n * every other outcome retries (`negativeAcknowledge`: the native redelivery count advances, so the\n * next delivery counts one more) while `counted < maxAttempts`, then dead-letters or drops.\n */\n private async settle(\n kind: SettleKind,\n counted: number,\n envelope: Envelope | null,\n message: PulsarReceivedMessage,\n error: unknown,\n ): Promise<void> {\n const producer = this.options.deadLetterProducer;\n if (kind === \"poison\" && producer !== undefined) {\n await this.deadLetter(producer, kind, counted, null, message, error);\n return;\n }\n if (counted < this.maxAttempts) {\n await this.retry(counted, envelope, message);\n return;\n }\n if (producer !== undefined) {\n await this.deadLetter(producer, kind, counted, envelope, message, error);\n return;\n }\n this.warn(\n `babelqueue: dropped a ${kind} Apache Pulsar message after ${counted} of ${this.maxAttempts} deliveries (dead-lettering is off).`,\n );\n await this.consumer.acknowledge(message);\n }\n\n /**\n * Retry one delivery. With a {@link PulsarConsumerOptions.retryProducer}: re-publish to the consumer's\n * own topic carrying `bq-attempts = counted` (reconcile = max(bq-attempts, RedeliveryCount) on the next\n * delivery), then acknowledge the original — only after the send succeeded. Otherwise `negativeAcknowledge`.\n */\n private async retry(counted: number, envelope: Envelope | null, message: PulsarReceivedMessage): Promise<void> {\n const producer = await this.resolveRetryProducer();\n if (producer === undefined) {\n await this.consumer.negativeAcknowledge(message);\n return;\n }\n try {\n await producer.send(this.retryMessage(counted, envelope, message));\n } catch (writeError) {\n this.warn(\n `babelqueue: retry re-publish to ${producer.getTopic()} failed (${errorText(writeError)}); the message is left for redelivery.`,\n );\n await this.consumer.negativeAcknowledge(message);\n return;\n }\n await this.consumer.acknowledge(message);\n }\n\n private retryMessage(counted: number, envelope: Envelope | null, message: PulsarReceivedMessage): PulsarProducerMessage {\n // Out-of-band headers (e.g. a traceparent) ride along; the contract bq-* properties are rebuilt and win,\n // and the one-shot bq-delay of the first publish is not repeated.\n const carried = headersOf(message);\n delete carried[\"bq-delay\"];\n if (envelope !== null) {\n return keepRoute(toPulsarMessage({ ...envelope, attempts: counted }, carried), message);\n }\n // A poison body is retried byte for byte (there is no envelope to re-encode); only the counter moves.\n const data = message.getData();\n return keepRoute(\n {\n data: typeof data === \"string\" ? Buffer.from(data, \"utf8\") : Buffer.from(data),\n properties: { ...carried, \"bq-attempts\": String(counted) },\n },\n message,\n );\n }\n\n /**\n * Write the DLQ record, then acknowledge the original — only after the send succeeded. A failed send\n * leaves the original negatively acknowledged, so it is redelivered and the write is tried again.\n */\n private async deadLetter(\n producer: PulsarProducer,\n kind: SettleKind,\n counted: number,\n envelope: Envelope | null,\n message: PulsarReceivedMessage,\n error: unknown,\n ): Promise<void> {\n try {\n await producer.send(this.deadLetterMessage(producer, kind, counted, envelope, message, error));\n } catch (writeError) {\n this.warn(\n `babelqueue: dead-letter write to ${producer.getTopic()} failed (${errorText(writeError)}); the message is left for redelivery.`,\n );\n await this.consumer.negativeAcknowledge(message);\n return;\n }\n await this.consumer.acknowledge(message);\n }\n\n private deadLetterMessage(\n producer: PulsarProducer,\n kind: SettleKind,\n counted: number,\n envelope: Envelope | null,\n message: PulsarReceivedMessage,\n error: unknown,\n ): PulsarProducerMessage {\n const details = {\n attempts: counted,\n error: error instanceof Error ? error.message : null,\n exception: error instanceof Error ? error.name : null,\n };\n if (envelope === null) {\n // ADR-0035: the original bytes are kept; only the dead_letter block is added.\n const original = this.options.queue ?? stripDlqSuffix(topicName(producer.getTopic()));\n // No retry decision was taken, so the value is informational: the delivered count.\n const delivered = counted - 1;\n const deadLetter = annotate({ attempts: delivered } as unknown as Envelope, kind, original, {\n ...details,\n attempts: delivered,\n }).dead_letter;\n const data = message.getData();\n const body = typeof data === \"string\" ? Buffer.from(data, \"utf8\") : Buffer.from(data);\n const record = buildPoisonRecord(body, JSON.stringify(deadLetter));\n return keepRoute({ data: Buffer.from(record, \"utf8\"), properties: poisonProperties(record) }, message);\n }\n // The envelope as the next hop will see it: attempts is the counted value, the same one that was\n // compared against maxAttempts and that dead_letter.attempts records.\n const original = envelope.meta?.queue || this.options.queue || stripDlqSuffix(topicName(producer.getTopic()));\n const annotated = annotate({ ...envelope, attempts: counted }, kind, original, details);\n return keepRoute(toPulsarMessage(annotated), message);\n }\n\n private notifyRoutingMismatch(propertyUrn: string, bodyUrn: string, envelope: IncomingEnvelope): void {\n const hook = this.options.onRoutingMismatch;\n if (hook === undefined) return;\n try {\n const result = hook(propertyUrn, bodyUrn, envelope);\n Promise.resolve(result).catch(() => undefined);\n } catch {\n // A diagnostic hook must never disturb consumption.\n }\n }\n}\n\nfunction safeProperties(message: PulsarReceivedMessage): { [key: string]: string } {\n try {\n const properties = typeof message.getProperties === \"function\" ? message.getProperties() : undefined;\n return properties ?? {};\n } catch {\n return {};\n }\n}\n\n/**\n * Keep the route a delivery arrived on: when the incoming message carries a non-blank `bq-job` property\n * (the consumer routes on it, §5.2/§5.7) the re-published retry or DLQ message carries the same value, even\n * if the body names another URN. Otherwise the second delivery could reach a different handler (Go's\n * `publishKeepingRoute`).\n */\nfunction keepRoute(outgoing: PulsarProducerMessage, incoming: PulsarReceivedMessage): PulsarProducerMessage {\n const route: unknown = safeProperties(incoming)[\"bq-job\"];\n if (typeof route === \"string\" && route.trim() !== \"\") {\n outgoing.properties = { ...outgoing.properties, \"bq-job\": route };\n }\n return outgoing;\n}\n\nfunction errorText(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n\n/** `orders.dlq` -> `orders`: the queue a DLQ topic belongs to (a name without the suffix is kept). */\nfunction stripDlqSuffix(topic: string): string {\n return topic.endsWith(\".dlq\") && topic.length > 4 ? topic.slice(0, -4) : topic;\n}\n\n/**\n * Contract `bq-*` properties for a poison DLQ record, projected from whatever the body carries (a body\n * that is not an object, or carries none of them, yields none).\n */\nfunction poisonProperties(recordJson: string): { [key: string]: string } {\n const properties: { [key: string]: string } = {};\n let record: unknown;\n try {\n record = JSON.parse(recordJson);\n } catch {\n return properties; // nested deeper than JSON.parse can recurse\n }\n if (record === null || typeof record !== \"object\") return properties;\n const top = record as Record<string, unknown>;\n const str = (value: unknown): string | undefined => (typeof value === \"string\" && value !== \"\" ? value : undefined);\n const job = str(top[\"job\"]) ?? str(top[\"urn\"]);\n if (job) properties[\"bq-job\"] = job;\n const trace = str(top[\"trace_id\"]);\n if (trace) properties[\"bq-trace-id\"] = trace;\n const meta = top[\"meta\"] !== null && typeof top[\"meta\"] === \"object\" ? (top[\"meta\"] as Record<string, unknown>) : {};\n const id = str(meta[\"id\"]);\n if (id) properties[\"bq-message-id\"] = id;\n return properties;\n}\n","/**\n * Pure, dependency-free helpers for the out-of-band transport-header seam (ADR-0028) — the Node\n * adapter mirror of Go's `sanitizeHeaders` and PHP's `Support\\Headers`. They fold an injected\n * {@link HeaderCarrier} (e.g. a W3C `traceparent`) onto a transport's metadata channel **without\n * clobbering** the contract headers it already carries.\n *\n * Headers ride beside the frozen wire envelope (GR-1), never inside it.\n */\n\nimport type { HeaderCarrier } from \"@babelqueue/core\";\n\n/**\n * Copy `headers`, dropping blank keys and blank values. Returns an empty object when nothing\n * survives. A nullish input yields an empty object.\n */\nexport function sanitizeHeaders(headers: HeaderCarrier | null | undefined): HeaderCarrier {\n const out: HeaderCarrier = {};\n if (!headers) return out;\n for (const key of Object.keys(headers)) {\n const value = headers[key];\n if (key === \"\" || value == null || value === \"\") continue;\n out[key] = value;\n }\n return out;\n}\n\n/**\n * Merge out-of-band `extra` headers onto an already-built `base` map **without clobbering** the\n * contract headers in `base` (they win a key collision) and dropping blanks. Returns `base`,\n * mutated in place.\n */\nexport function mergeInto(base: { [key: string]: string }, extra: HeaderCarrier | null | undefined): { [key: string]: string } {\n for (const [key, value] of Object.entries(sanitizeHeaders(extra))) {\n if (key in base) continue; // the contract header already there wins\n base[key] = value;\n }\n return base;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAsDA,kBAA6G;;;ACvCtG,SAAS,gBAAgB,SAA0D;AACxF,QAAM,MAAqB,CAAC;AAC5B,MAAI,CAAC,QAAS,QAAO;AACrB,aAAW,OAAO,OAAO,KAAK,OAAO,GAAG;AACtC,U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