@lunora/queue 1.0.0-alpha.1 → 1.0.0-alpha.100
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/LICENSE.md +6 -0
- package/README.md +19 -1
- package/dist/index.d.mts +371 -169
- package/dist/index.d.ts +371 -169
- package/dist/index.mjs +1 -5
- package/dist/packem_shared/createQueueCaptureSink-CSOPftvj.mjs +1 -0
- package/dist/packem_shared/createQueueContext-BelEY_4M.mjs +1 -0
- package/dist/packem_shared/createQueueRunContext-C0hN52Mg.mjs +1 -0
- package/dist/packem_shared/createQueues-AS-deVhl.mjs +1 -0
- package/dist/packem_shared/createTopicContext-jtRf138F.mjs +1 -0
- package/dist/packem_shared/defineQueue-TY4-i3nG.mjs +1 -0
- package/dist/packem_shared/dispatchQueueBatch-D030OrmN.mjs +1 -0
- package/dist/packem_shared/requeue-envelope-Ylx5QErd.mjs +4 -0
- package/dist/packem_shared/run-context-CoNKhXT_.mjs +1 -0
- package/package.json +5 -1
- package/dist/packem_shared/createQueueContext-D0XCdCsd.mjs +0 -14
- package/dist/packem_shared/createQueueRunContext-DVqG7Oyk.mjs +0 -71
- package/dist/packem_shared/createQueues-14-vSICK.mjs +0 -33
- package/dist/packem_shared/defineQueue-D40gREfg.mjs +0 -18
- package/dist/packem_shared/dispatchQueueBatch-DVMmBISH.mjs +0 -18
package/dist/index.d.mts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import { QueueBindingLike, MessageBatchLike, MessageLike, QueueSendOptions, MessageSendRequestLike, QueueSendBatchOptions } from '@lunora/platform';
|
|
2
|
+
export type { MessageBatchLike, MessageLike, MessageSendRequestLike, QueueBindingLike, QueueContentType, QueueRetryOptions, QueueSendBatchOptions, QueueSendOptions } from '@lunora/platform';
|
|
1
3
|
/**
|
|
2
|
-
* Shared types for dispatching a Lunora function back into the worker from a
|
|
3
|
-
* server-initiated context (a workflow body, a queue handler, a scheduled job).
|
|
4
|
-
* Node-safe — no Cloudflare runtime imports — so the consumers stay unit-testable
|
|
5
|
-
* with plain-object doubles.
|
|
6
|
-
*/
|
|
4
|
+
* Shared types for dispatching a Lunora function back into the worker from a
|
|
5
|
+
* server-initiated context (a workflow body, a queue handler, a scheduled job).
|
|
6
|
+
* Node-safe — no Cloudflare runtime imports — so the consumers stay unit-testable
|
|
7
|
+
* with plain-object doubles.
|
|
8
|
+
*/
|
|
7
9
|
/** Opaque generated function reference (`api.foo.bar`), carrying its dispatch id. */
|
|
8
10
|
interface FunctionReference {
|
|
9
11
|
__lunoraRef: string;
|
|
@@ -12,8 +14,45 @@ interface FunctionReference {
|
|
|
12
14
|
type ArgsOf<F> = F extends FunctionReference ? Record<string, unknown> : never;
|
|
13
15
|
/** Options for a function call made via a dispatch runner. */
|
|
14
16
|
interface RunFunctionOptions {
|
|
17
|
+
/**
|
|
18
|
+
* The at-least-once dedup key for THIS ONE CALL, sent to the dispatch
|
|
19
|
+
* endpoint as the body's `id` and forwarded to the shard as the
|
|
20
|
+
* replay-dedup `mutationId` — so a redelivery that re-runs the same call
|
|
21
|
+
* applies it exactly once instead of twice.
|
|
22
|
+
*
|
|
23
|
+
* Must be unique per call and stable across redeliveries. It is NOT
|
|
24
|
+
* {@link RunFunctionOptions.messageId}: the shard's dedup table is keyed
|
|
25
|
+
* `(identity, mutationId)` with no function path in it, and every
|
|
26
|
+
* server-initiated dispatch shares the `"system:"` identity, so reusing
|
|
27
|
+
* one id across two calls makes the second return the FIRST call's cached
|
|
28
|
+
* result without ever executing. A per-message id is 1:N with the calls a
|
|
29
|
+
* handler makes; this is 1:1.
|
|
30
|
+
*
|
|
31
|
+
* `@lunora/queue`'s `message.run` derives one automatically as
|
|
32
|
+
* `<messageId>#<n>`, `n` counting that message's calls in order. That is
|
|
33
|
+
* stable across redeliveries because the handler replays from the start —
|
|
34
|
+
* it relies on the handler issuing its `run` calls in a DETERMINISTIC
|
|
35
|
+
* order, which at-least-once replay already assumes. A handler whose call
|
|
36
|
+
* order varies per attempt (branching on `Date.now()`, `Math.random()`,
|
|
37
|
+
* or unordered concurrent settles) must pass its own stable ids instead.
|
|
38
|
+
*
|
|
39
|
+
* Optional; when omitted the dispatch is at-least-once.
|
|
40
|
+
*/
|
|
41
|
+
dedupId?: string;
|
|
42
|
+
/**
|
|
43
|
+
* Correlate this call with a caller-defined message/item id (e.g. a queue
|
|
44
|
+
* message's `id`), for failure attribution only — never sent to the
|
|
45
|
+
* dispatch endpoint. Carried onto the `LunoraError` a deterministic
|
|
46
|
+
* dispatch failure throws, so a batching consumer (`@lunora/queue`'s push
|
|
47
|
+
* handler) can read it back and attribute the failure to the one item that
|
|
48
|
+
* caused it instead of the whole batch. Deliberately NOT the dedup key —
|
|
49
|
+
* see {@link RunFunctionOptions.dedupId}. Optional and inert when omitted.
|
|
50
|
+
*/
|
|
51
|
+
messageId?: string;
|
|
15
52
|
/** Route the call to a specific shard (defaults to the worker's root shard). */
|
|
16
53
|
shardKey?: string;
|
|
54
|
+
/** Abort the dispatch after this many ms; the abort is retryable. Overrides the runner's default. */
|
|
55
|
+
timeoutMs?: number;
|
|
17
56
|
}
|
|
18
57
|
/** Invoke a Lunora function (query/mutation/action) by reference. The shape of `ctx.run`. */
|
|
19
58
|
type DispatchRunFunction = <F extends FunctionReference>(function_: F, args?: ArgsOf<F>, options?: RunFunctionOptions) => Promise<unknown>;
|
|
@@ -24,66 +63,11 @@ interface DispatchLogger {
|
|
|
24
63
|
info: (message: unknown, ...rest: unknown[]) => void;
|
|
25
64
|
warn: (message: unknown, ...rest: unknown[]) => void;
|
|
26
65
|
}
|
|
27
|
-
/** Build a {@link DispatchLogger} that prefixes every line with `prefix` (e.g. `[queue:email]`). */
|
|
28
|
-
/** How a queue message body is serialized on the wire (Cloudflare default `"json"`). */
|
|
29
|
-
type QueueContentType = "bytes" | "json" | "text" | "v8";
|
|
30
|
-
/** Options for a single `producer.send(body, options?)`. */
|
|
31
|
-
interface QueueSendOptions {
|
|
32
|
-
/** Wire serialization for this message (defaults to the queue's content type). */
|
|
33
|
-
contentType?: QueueContentType;
|
|
34
|
-
/** Per-message delivery delay in seconds (0–43200, i.e. up to 12 hours). */
|
|
35
|
-
delaySeconds?: number;
|
|
36
|
-
}
|
|
37
|
-
/** Options for a `producer.sendBatch(messages, options?)`. */
|
|
38
|
-
interface QueueSendBatchOptions {
|
|
39
|
-
/** Delivery delay applied to the whole batch, in seconds. */
|
|
40
|
-
delaySeconds?: number;
|
|
41
|
-
}
|
|
42
|
-
/** One entry in a `sendBatch` call — a body plus optional per-message overrides. */
|
|
43
|
-
interface MessageSendRequestLike<Body = unknown> {
|
|
44
|
-
body: Body;
|
|
45
|
-
contentType?: QueueContentType;
|
|
46
|
-
delaySeconds?: number;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Minimal structural projection of workers-types' `Queue<Body>` (the producer
|
|
50
|
-
* binding). The real binding's `send`/`sendBatch` resolve to a metadata object;
|
|
51
|
-
* we widen the return to `Promise<unknown>` so a plain-object fake satisfies it.
|
|
52
|
-
*/
|
|
53
|
-
interface QueueBindingLike<Body = unknown> {
|
|
54
|
-
send: (message: Body, options?: QueueSendOptions) => Promise<unknown>;
|
|
55
|
-
sendBatch: (messages: Iterable<MessageSendRequestLike<Body>>, options?: QueueSendBatchOptions) => Promise<unknown>;
|
|
56
|
-
}
|
|
57
|
-
/** Options for retrying a message / batch (`message.retry({ delaySeconds })`). */
|
|
58
|
-
interface QueueRetryOptions {
|
|
59
|
-
delaySeconds?: number;
|
|
60
|
-
}
|
|
61
|
-
/** Structural mirror of workers-types' `Message<Body>` (one delivered message). */
|
|
62
|
-
interface MessageLike<Body = unknown> {
|
|
63
|
-
/** Acknowledge this message so it is not redelivered. */
|
|
64
|
-
ack: () => void;
|
|
65
|
-
readonly attempts: number;
|
|
66
|
-
readonly body: Body;
|
|
67
|
-
readonly id: string;
|
|
68
|
-
/** Explicitly retry this message (optionally after a delay). */
|
|
69
|
-
retry: (options?: QueueRetryOptions) => void;
|
|
70
|
-
readonly timestamp: Date;
|
|
71
|
-
}
|
|
72
|
-
/** Structural mirror of workers-types' `MessageBatch<Body>` handed to a consumer. */
|
|
73
|
-
interface MessageBatchLike<Body = unknown> {
|
|
74
|
-
/** Acknowledge every message in the batch. */
|
|
75
|
-
ackAll: () => void;
|
|
76
|
-
readonly messages: ReadonlyArray<MessageLike<Body>>;
|
|
77
|
-
/** The queue name this batch was delivered from (`batch.queue`), used to route. */
|
|
78
|
-
readonly queue: string;
|
|
79
|
-
/** Retry every message in the batch (optionally after a delay). */
|
|
80
|
-
retryAll: (options?: QueueRetryOptions) => void;
|
|
81
|
-
}
|
|
82
66
|
/**
|
|
83
|
-
* The typed producer bound to `ctx.queues
|
|
84
|
-
* the generated context exposes this only on `MutationCtx` / `ActionCtx` (never
|
|
85
|
-
* the deterministic `QueryCtx`), mirroring `ctx.scheduler` / `ctx.workflows`.
|
|
86
|
-
*/
|
|
67
|
+
* The typed producer bound to `ctx.queues.<name>`. Sending is a side effect, so
|
|
68
|
+
* the generated context exposes this only on `MutationCtx` / `ActionCtx` (never
|
|
69
|
+
* the deterministic `QueryCtx`), mirroring `ctx.scheduler` / `ctx.workflows`.
|
|
70
|
+
*/
|
|
87
71
|
interface QueueProducer<Body = unknown> {
|
|
88
72
|
/** Enqueue one message. */
|
|
89
73
|
send: (body: Body, options?: QueueSendOptions) => Promise<void>;
|
|
@@ -91,36 +75,62 @@ interface QueueProducer<Body = unknown> {
|
|
|
91
75
|
sendBatch: (messages: Iterable<MessageSendRequestLike<Body>>, options?: QueueSendBatchOptions) => Promise<void>;
|
|
92
76
|
}
|
|
93
77
|
/**
|
|
94
|
-
* `ctx.queues` — the map of declared queue export names → typed producers.
|
|
95
|
-
*
|
|
96
|
-
* `
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
78
|
+
* `ctx.queues` — the map of declared queue export names → typed producers.
|
|
79
|
+
* Keyed by the exact export names `createQueueContext` / `createQueues` are
|
|
80
|
+
* handed, so `queues.jobs` is a `QueueProducer` rather than `QueueProducer |
|
|
81
|
+
* undefined` under `noUncheckedIndexedAccess`. `Name` defaults to `string`
|
|
82
|
+
* (an open map) for schema-agnostic callers.
|
|
83
|
+
*/
|
|
84
|
+
type Queues<Name extends string = string> = Readonly<Record<Name, QueueProducer>>;
|
|
101
85
|
/** Options the package-level `createQueues` factory takes. */
|
|
102
|
-
interface LunoraQueuesOptions {
|
|
86
|
+
interface LunoraQueuesOptions<Name extends string = string> {
|
|
103
87
|
/** Map of `lunora/queues.ts` export name → Cloudflare `Queue` producer binding. */
|
|
104
|
-
bindings: Record<
|
|
88
|
+
bindings: Record<Name, QueueBindingLike>;
|
|
105
89
|
}
|
|
106
90
|
/**
|
|
107
|
-
* The context handed to a `defineQueue` handler. Decoupled from `@lunora/server`
|
|
108
|
-
* (like the workflow run context): to touch data, call a Lunora mutation/action
|
|
109
|
-
* via `ctx.run(api.x.y, args)` — the dispatch goes through the same
|
|
110
|
-
* `/_lunora/scheduler/dispatch` path the SchedulerDO and workflows use.
|
|
111
|
-
*/
|
|
91
|
+
* The context handed to a `defineQueue` handler. Decoupled from `@lunora/server`
|
|
92
|
+
* (like the workflow run context): to touch data, call a Lunora mutation/action
|
|
93
|
+
* via `ctx.run(api.x.y, args)` — the dispatch goes through the same
|
|
94
|
+
* `/_lunora/scheduler/dispatch` path the SchedulerDO and workflows use.
|
|
95
|
+
*/
|
|
112
96
|
interface QueueRunContext {
|
|
113
97
|
/** The worker `env` (bindings + vars). */
|
|
114
98
|
readonly env: Record<string, unknown>;
|
|
115
99
|
/** Queue-name-prefixed logger. */
|
|
116
100
|
readonly log: DispatchLogger;
|
|
117
|
-
/**
|
|
101
|
+
/**
|
|
102
|
+
* Invoke a Lunora function (query/mutation/action) by reference.
|
|
103
|
+
*
|
|
104
|
+
* Batch-unaware: a failure it throws is attributed to nothing, so a
|
|
105
|
+
* deterministic failure retries the whole batch. Inside the `batch.messages`
|
|
106
|
+
* loop, prefer {@link QueueMessage.run}, which pins the call to its message.
|
|
107
|
+
*/
|
|
118
108
|
readonly run: DispatchRunFunction;
|
|
119
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* One delivered message as the push handler sees it: the Cloudflare `Message`
|
|
112
|
+
* plus `run` — a {@link QueueRunContext.run} pinned to THIS message.
|
|
113
|
+
*
|
|
114
|
+
* Prefer `message.run(api.x.y, args)` over `ctx.run(...)` inside the batch
|
|
115
|
+
* loop. The pin is what lets the dispatcher attribute a deterministic dispatch
|
|
116
|
+
* failure (400/403/404/422) to the one message that caused it: that message is
|
|
117
|
+
* acked and every other one is retried, instead of the whole batch being
|
|
118
|
+
* re-delivered because of a single poison message. A plain `ctx.run` call
|
|
119
|
+
* carries no message id, so its failure stays unattributed and the whole batch
|
|
120
|
+
* retries.
|
|
121
|
+
*/
|
|
122
|
+
interface QueueMessage<Body = unknown> extends MessageLike<Body> {
|
|
123
|
+
/** {@link QueueRunContext.run}, pinned to this message for failure attribution. */
|
|
124
|
+
readonly run: DispatchRunFunction;
|
|
125
|
+
}
|
|
126
|
+
/** The delivered batch as the push handler sees it — {@link QueueMessage}s rather than bare `Message`s. */
|
|
127
|
+
interface QueueMessageBatch<Body = unknown> extends Omit<MessageBatchLike<Body>, "messages"> {
|
|
128
|
+
readonly messages: ReadonlyArray<QueueMessage<Body>>;
|
|
129
|
+
}
|
|
120
130
|
/** Whether a declared queue is consumed by this worker (push) or polled externally (pull). */
|
|
121
131
|
type QueueConsumerMode = "pull" | "push";
|
|
122
132
|
/** The handler body run for each delivered batch (push consumers only). */
|
|
123
|
-
type QueueHandler<Body = unknown> = (context: QueueRunContext, batch:
|
|
133
|
+
type QueueHandler<Body = unknown> = (context: QueueRunContext, batch: QueueMessageBatch<Body>) => Promise<void> | void;
|
|
124
134
|
/** Push-consumer batch/retry tuning, mirrored onto the wrangler `queues.consumers[]` entry. */
|
|
125
135
|
interface QueueConsumerTuning {
|
|
126
136
|
/** Name of the dead-letter queue messages land in after `maxRetries`. */
|
|
@@ -129,7 +139,9 @@ interface QueueConsumerTuning {
|
|
|
129
139
|
maxBatchSize?: number;
|
|
130
140
|
/** Max seconds to wait before delivering a partial batch (0–60, default 5). */
|
|
131
141
|
maxBatchTimeout?: number;
|
|
132
|
-
/** Max
|
|
142
|
+
/** Max concurrent consumer invocations (1–250). Unset lets Cloudflare autoscale. Not emulated by Miniflare in dev. */
|
|
143
|
+
maxConcurrency?: number;
|
|
144
|
+
/** Retries **after** the initial delivery, before a message is dropped / dead-lettered. Default 3, so up to 4 deliveries in total. */
|
|
133
145
|
maxRetries?: number;
|
|
134
146
|
/** Delay in seconds before a failed batch is retried. */
|
|
135
147
|
retryDelay?: number;
|
|
@@ -137,135 +149,325 @@ interface QueueConsumerTuning {
|
|
|
137
149
|
/** The config object passed to `defineQueue`. */
|
|
138
150
|
interface QueueConfig<Body = unknown> extends QueueConsumerTuning {
|
|
139
151
|
/**
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
152
|
+
* The push-consumer body. Required for `mode: "push"` (the default); omit it
|
|
153
|
+
* for `mode: "pull"`, where an external worker polls the queue over HTTP.
|
|
154
|
+
*/
|
|
143
155
|
handler?: QueueHandler<Body>;
|
|
144
156
|
/** How this queue is consumed. Defaults to `"push"`. */
|
|
145
157
|
mode?: QueueConsumerMode;
|
|
146
158
|
/**
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
159
|
+
* Stable wrangler queue name (`queues.producers[].queue`). Defaults to the
|
|
160
|
+
* kebab-cased export name (`emailQueue` → `email-queue`).
|
|
161
|
+
*/
|
|
150
162
|
name?: string;
|
|
151
163
|
}
|
|
152
164
|
/** The branded result of `defineQueue`, discovered by codegen + config. */
|
|
153
165
|
interface QueueDefinition<Body = unknown> extends QueueConfig<Body> {
|
|
154
166
|
/**
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
167
|
+
* Phantom carrier for the message body type, so codegen can type the
|
|
168
|
+
* generated `ctx.queues.<name>` producer as `QueueProducer<Body>` from
|
|
169
|
+
* `typeof <export>`. Never assigned at runtime (type-only).
|
|
170
|
+
*/
|
|
159
171
|
readonly __lunoraBody?: Body;
|
|
160
172
|
/** Runtime brand identifying a `defineQueue` result. */
|
|
161
173
|
isLunoraQueue: true;
|
|
162
174
|
}
|
|
175
|
+
/**
|
|
176
|
+
* The typed publisher bound to `ctx.topics.<name>`. A publish is one send to
|
|
177
|
+
* **each** subscription's queue, in parallel, so it is not atomic: when one send
|
|
178
|
+
* fails the call rejects, and a retry re-delivers to the subscriptions whose send
|
|
179
|
+
* already succeeded. Delivery is at-least-once per subscription — make handlers
|
|
180
|
+
* idempotent. A topic with no subscriptions publishes to nobody.
|
|
181
|
+
*/
|
|
182
|
+
interface TopicPublisher<Payload = unknown> {
|
|
183
|
+
/** Publish one message to every subscription. */
|
|
184
|
+
publish: (payload: Payload, options?: QueueSendOptions) => Promise<void>;
|
|
185
|
+
/** Publish a batch (at most 100) to every subscription in one send per subscription. */
|
|
186
|
+
publishBatch: (messages: Iterable<MessageSendRequestLike<Payload>>, options?: QueueSendBatchOptions) => Promise<void>;
|
|
187
|
+
}
|
|
188
|
+
/** `ctx.topics` — declared topic export names → typed publishers, keyed like {@link Queues}. */
|
|
189
|
+
type Topics<Name extends string = string> = Readonly<Record<Name, TopicPublisher>>;
|
|
190
|
+
/** The branded result of `defineTopic`, discovered by codegen. Carries no deploy config of its own. */
|
|
191
|
+
interface TopicDefinition<Payload = unknown> {
|
|
192
|
+
/**
|
|
193
|
+
* Phantom carrier for the payload type, so codegen can type `ctx.topics.<name>`.
|
|
194
|
+
* Named like {@link QueueDefinition}'s so one emitted helper reads both. Never
|
|
195
|
+
* assigned at runtime.
|
|
196
|
+
*/
|
|
197
|
+
readonly __lunoraBody?: Payload;
|
|
198
|
+
/** Runtime brand identifying a `defineTopic` result. */
|
|
199
|
+
isLunoraTopic: true;
|
|
200
|
+
}
|
|
201
|
+
/** The config passed to `defineSubscription`: a push consumer's handler plus its own retry/DLQ tuning. */
|
|
202
|
+
interface SubscriptionConfig<Payload = unknown> extends QueueConsumerTuning {
|
|
203
|
+
handler: QueueHandler<Payload>;
|
|
204
|
+
/**
|
|
205
|
+
* Stable wrangler queue name for this subscription. Defaults to the
|
|
206
|
+
* kebab-cased export name, exactly like a `defineQueue` export.
|
|
207
|
+
*/
|
|
208
|
+
name?: string;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* The branded result of `defineSubscription`: a push {@link QueueDefinition}
|
|
212
|
+
* (so dispatch, retries, the DLQ, wrangler reconcile and the studio treat it as
|
|
213
|
+
* the queue it is) tagged with the topic it consumes.
|
|
214
|
+
*/
|
|
215
|
+
interface SubscriptionDefinition<Payload = unknown> extends QueueDefinition<Payload> {
|
|
216
|
+
handler: QueueHandler<Payload>;
|
|
217
|
+
mode: "push";
|
|
218
|
+
/** The topic this subscription consumes. */
|
|
219
|
+
topic: TopicDefinition<Payload>;
|
|
220
|
+
}
|
|
221
|
+
/** Wiring info for one declared topic, emitted by codegen into the generated shard. */
|
|
222
|
+
interface TopicBindingSpec<Name extends string = string> {
|
|
223
|
+
/** The `lunora/queues.ts` topic export name, e.g. `signups`. */
|
|
224
|
+
exportName: Name;
|
|
225
|
+
/** One entry per subscription: its export name and its `Queue` producer binding. */
|
|
226
|
+
subscriptions: ReadonlyArray<{
|
|
227
|
+
binding: string;
|
|
228
|
+
exportName: string;
|
|
229
|
+
}>;
|
|
230
|
+
}
|
|
163
231
|
/** Wiring info for one declared queue, emitted by codegen into the generated shard/handler. */
|
|
164
|
-
interface QueueBindingSpec {
|
|
232
|
+
interface QueueBindingSpec<Name extends string = string> {
|
|
165
233
|
/** The Cloudflare `Queue` producer binding name, e.g. `QUEUE_EMAIL`. */
|
|
166
234
|
binding: string;
|
|
167
235
|
/** The `lunora/queues.ts` export name, e.g. `emailQueue`. */
|
|
168
|
-
exportName:
|
|
236
|
+
exportName: Name;
|
|
169
237
|
/** The stable wrangler queue name, e.g. `email-queue`. */
|
|
170
238
|
name: string;
|
|
171
239
|
}
|
|
172
|
-
/**
|
|
173
|
-
* Build the `ctx.queues` map for a request: resolve every spec's `env[binding]`
|
|
174
|
-
* into the `exportName → Queue binding` map and wrap it in {@link createQueues}.
|
|
175
|
-
* A spec whose binding is absent from `env` is skipped here — the helpful "no
|
|
176
|
-
* queue named …" error is raised lazily by `ctx.queues.<name>.send(...)` when
|
|
177
|
-
* the missing queue is actually used.
|
|
178
|
-
*/
|
|
179
|
-
declare const createQueueContext: (env: Record<string, unknown>, specs: ReadonlyArray<QueueBindingSpec>) => Queues;
|
|
180
|
-
/**
|
|
181
|
-
* Build the `ctx.queues` map from `lunora/queues.ts` export name → Cloudflare
|
|
182
|
-
* `Queue` binding. Each property is a typed {@link QueueProducer}; accessing an
|
|
183
|
-
* export whose binding is absent throws a directed error naming the declared
|
|
184
|
-
* queues (raised lazily on first use).
|
|
185
|
-
*/
|
|
186
|
-
declare const createQueues: (options: LunoraQueuesOptions) => Queues;
|
|
187
|
-
/**
|
|
188
|
-
* The wrangler producer binding name for a queue export: `emailQueue` →
|
|
189
|
-
* `QUEUE_EMAIL_QUEUE`, `email` → `QUEUE_EMAIL`. The `QUEUE_` prefix namespaces
|
|
190
|
-
* these away from `SHARD`/`SESSION`/`SCHEDULER`/`WORKFLOW_*`/`CONTAINER_*` so a
|
|
191
|
-
* queue export can never collide with the built-in bindings.
|
|
192
|
-
*/
|
|
193
|
-
declare const queueBindingName: (exportName: string) => string;
|
|
194
|
-
/**
|
|
195
|
-
* The stable queue name wrangler registers (`queues.producers[].queue` and
|
|
196
|
-
* `queues.consumers[].queue`): `emailQueue` → `email-queue`. Used as the
|
|
197
|
-
* deployed queue's identifier when no explicit `name` override is given.
|
|
198
|
-
*/
|
|
199
|
-
declare const queueDefaultName: (exportName: string) => string;
|
|
200
|
-
/**
|
|
201
|
-
* Declare a Cloudflare Queue deployed alongside the app. Pure validation +
|
|
202
|
-
* branding: codegen discovers the export, emits the typed `ctx.queues.<name>`
|
|
203
|
-
* producer and (for push consumers) the worker `queue()` dispatch; the config
|
|
204
|
-
* layer reconciles the wrangler `queues.producers[]` / `queues.consumers[]`
|
|
205
|
-
* entries from the same definition.
|
|
206
|
-
*
|
|
207
|
-
* ```ts
|
|
208
|
-
* // lunora/queues.ts
|
|
209
|
-
* import { defineQueue } from "@lunora/queue";
|
|
210
|
-
* import { api } from "./_generated/api";
|
|
211
|
-
*
|
|
212
|
-
* export const emailQueue = defineQueue<{ to: string }>({
|
|
213
|
-
* handler: async (ctx, batch) => {
|
|
214
|
-
* for (const message of batch.messages) {
|
|
215
|
-
* await ctx.run(api.email.send, { to: message.body.to });
|
|
216
|
-
* message.ack();
|
|
217
|
-
* }
|
|
218
|
-
* },
|
|
219
|
-
* });
|
|
220
|
-
* ```
|
|
221
|
-
*
|
|
222
|
-
* Enqueue from a mutation or action: `await ctx.queues.emailQueue.send({ to })`.
|
|
223
|
-
*
|
|
224
|
-
* ⚠️ **Privileged dispatch.** A push handler's `ctx.run(...)` calls back into
|
|
225
|
-
* Lunora functions over the admin-authenticated dispatch endpoint (the same
|
|
226
|
-
* trusted path the scheduler and workflows use), so those calls run with the
|
|
227
|
-
* system identity — **end-user RLS is not applied**. Treat a queue handler as
|
|
228
|
-
* trusted server code: validate `message.body` (it may be attacker-influenced if
|
|
229
|
-
* anything user-facing can enqueue) before acting on it, and don't forward an
|
|
230
|
-
* unchecked body straight into a privileged mutation.
|
|
231
|
-
*/
|
|
232
|
-
declare const defineQueue: <Body = unknown>(config: QueueConfig<Body>) => QueueDefinition<Body>;
|
|
233
|
-
/** True when a value is a `defineQueue` result (the runtime brand check). */
|
|
234
|
-
declare const isQueueDefinition: (value: unknown) => value is QueueDefinition;
|
|
235
240
|
/** One declared queue, keyed for batch routing by its stable wrangler name. */
|
|
236
241
|
interface QueueRegistryEntry {
|
|
237
242
|
/**
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
243
|
+
* The queue's own producer binding on `env` (`QUEUE_*`). A message whose
|
|
244
|
+
* dispatch is declined on its last delivery is re-enqueued through it (see
|
|
245
|
+
* {@link resolveDeclinedBatch}); without it that message is dead-lettered or
|
|
246
|
+
* dropped, and logged. Codegen always emits it.
|
|
247
|
+
*/
|
|
248
|
+
binding?: string;
|
|
249
|
+
/**
|
|
250
|
+
* The `defineQueue` result (carries the push handler). The body type is
|
|
251
|
+
* erased to `any` here because the registry is heterogeneous — different
|
|
252
|
+
* queues carry different message bodies, and the handler param is
|
|
253
|
+
* contravariant, so a precise `QueueDefinition<Body>` would not be assignable
|
|
254
|
+
* to a shared `unknown`-bodied slot. Runtime dispatch passes the delivered
|
|
255
|
+
* batch straight through, so the erasure is type-only.
|
|
256
|
+
*/
|
|
245
257
|
definition: QueueDefinition<any>;
|
|
246
258
|
/** The `lunora/queues.ts` export name, for log correlation. */
|
|
247
259
|
exportName: string;
|
|
248
260
|
}
|
|
249
261
|
/** Map of stable wrangler queue name → registry entry, built by codegen. */
|
|
250
262
|
type QueueRegistry = Record<string, QueueRegistryEntry>;
|
|
263
|
+
/** The disposition a consumer left one message in for a single delivery attempt. */
|
|
264
|
+
type QueueMessageOutcome = "ack" | "error" | "retry";
|
|
265
|
+
/**
|
|
266
|
+
* One consumed message as captured by {@link dispatchQueueBatch} and handed to an
|
|
267
|
+
* {@link QueueCaptureSink}. Structurally matches `@lunora/do`'s
|
|
268
|
+
* `RecordQueueMessageInput` (the reserved `recordQueueMessage` admin RPC payload);
|
|
269
|
+
* the two packages share only this contract, so keep them in sync by hand.
|
|
270
|
+
*/
|
|
271
|
+
interface CapturedQueueMessage {
|
|
272
|
+
/** Delivery attempt number for this message (`message.attempts`). */
|
|
273
|
+
attempts: number;
|
|
274
|
+
/** The message body (JSON-encoded + capped by the catcher). */
|
|
275
|
+
body: unknown;
|
|
276
|
+
/**
|
|
277
|
+
* `true` when this failed delivery was the message's last (its retries are
|
|
278
|
+
* exhausted) AND the queue declares a `deadLetterQueue` for it to land in.
|
|
279
|
+
* Stays `false` for a queue with no DLQ, where the broker drops the
|
|
280
|
+
* exhausted message instead — `attempts > maxRetries` with
|
|
281
|
+
* `outcome !== "ack"` is what identifies that case.
|
|
282
|
+
*
|
|
283
|
+
* Read from the `defineQueue` DECLARATION, because a consumer has no
|
|
284
|
+
* runtime API that exposes its deployed `queues.consumers[]` settings. It is
|
|
285
|
+
* true of the deployed broker only as far as Lunora's binding reconcile (run
|
|
286
|
+
* by `lunora dev`, `deploy` and `prepare`) keeps the two in step: it writes
|
|
287
|
+
* every declared tuning field onto the consumer, including onto one that
|
|
288
|
+
* already exists, and takes a field back out once `defineQueue` drops it
|
|
289
|
+
* (it records what `defineQueue` declared in `package.json`
|
|
290
|
+
* `lunora.queueTuning`). The gaps that remain: a field changed by hand to
|
|
291
|
+
* another value than the declared one is
|
|
292
|
+
* kept when the declaration drops it, so a hand-set DLQ still reads
|
|
293
|
+
* `false` here; a `--env` deploy retunes the `env.<name>` consumers but
|
|
294
|
+
* never writes their `dead_letter_queue`, which that block names itself;
|
|
295
|
+
* and a bare `wrangler deploy` over a hand-edited consumer can disagree
|
|
296
|
+
* too. Nothing on this side can see any of it.
|
|
297
|
+
*/
|
|
298
|
+
deadLettered: boolean;
|
|
299
|
+
/** Handler error message when `outcome` is `error`; absent otherwise. */
|
|
300
|
+
error?: string;
|
|
301
|
+
/** The `lunora/queues.ts` export name that consumed it. */
|
|
302
|
+
exportName: string;
|
|
303
|
+
/** The delivered message id (`message.id`). */
|
|
304
|
+
messageId: string;
|
|
305
|
+
/** How the handler disposed of the message this attempt. */
|
|
306
|
+
outcome: QueueMessageOutcome;
|
|
307
|
+
/** The stable wrangler queue name the batch was delivered from (`batch.queue`). */
|
|
308
|
+
queue: string;
|
|
309
|
+
/** Original message timestamp in epoch-ms (`message.timestamp`). */
|
|
310
|
+
timestamp: number;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Persists a batch of consumed messages. The codegen worker wires this to POST the
|
|
314
|
+
* batch to the root shard's `recordQueueMessage` admin RPC (the dev queue catcher).
|
|
315
|
+
* Best-effort by contract: {@link dispatchQueueBatch} swallows a rejection so a
|
|
316
|
+
* capture failure never changes delivery semantics.
|
|
317
|
+
*/
|
|
318
|
+
type QueueCaptureSink = (messages: CapturedQueueMessage[]) => Promise<void> | void;
|
|
251
319
|
interface DispatchOptions {
|
|
320
|
+
/**
|
|
321
|
+
* Optional capture sink. When set, every message's final disposition is
|
|
322
|
+
* turned into a record and handed to this sink after the handler runs.
|
|
323
|
+
* Omitted in production unless queue capture is enabled, so a consumer pays
|
|
324
|
+
* no record-building or sink cost by default. Delivery semantics — including
|
|
325
|
+
* poison-message isolation — do not depend on it.
|
|
326
|
+
*/
|
|
327
|
+
capture?: QueueCaptureSink;
|
|
252
328
|
/** Worker `env`, forwarded to the queue run context. */
|
|
253
329
|
env: Record<string, unknown>;
|
|
254
330
|
/** Injectable fetch for the `ctx.run` dispatcher (tests). */
|
|
255
331
|
fetchImpl?: typeof fetch;
|
|
332
|
+
/**
|
|
333
|
+
* W3C `traceparent` of the consumer invocation's own trace, supplied by the
|
|
334
|
+
* runtime's `queue()` entry. Forwarded on every `ctx.run` dispatch so the
|
|
335
|
+
* functions a handler calls are CHILDREN of the queue span instead of a set of
|
|
336
|
+
* unrelated root traces. Absent (a hand-built dispatch, a unit test) keeps the
|
|
337
|
+
* prior behaviour: the callee mints its own trace.
|
|
338
|
+
*/
|
|
339
|
+
traceparent?: string;
|
|
256
340
|
}
|
|
257
341
|
/**
|
|
258
|
-
* Look up the handler for `batch.queue` and invoke it with a fresh
|
|
259
|
-
* `QueueRunContext`. Throws a directed error when no push handler is registered
|
|
260
|
-
* for the delivered queue (a misconfiguration — the consumer was declared
|
|
261
|
-
* `pull`, or the queue name drifted from the `defineQueue` export).
|
|
262
|
-
|
|
342
|
+
* Look up the handler for `batch.queue` and invoke it with a fresh
|
|
343
|
+
* `QueueRunContext`. Throws a directed error when no push handler is registered
|
|
344
|
+
* for the delivered queue (a misconfiguration — the consumer was declared
|
|
345
|
+
* `pull`, or the queue name drifted from the `defineQueue` export). The
|
|
346
|
+
* registry already carries every per-environment name wrangler.jsonc maps to a
|
|
347
|
+
* declared producer binding (see codegen's `emitQueues`), so the lookup stays exact.
|
|
348
|
+
*/
|
|
263
349
|
declare const dispatchQueueBatch: (batch: MessageBatchLike, registry: QueueRegistry, options: DispatchOptions) => Promise<void>;
|
|
350
|
+
/** A Worker `env` projected as a plain record (vars, secrets, and bindings are `unknown`-valued). */
|
|
351
|
+
type QueueEnv = Record<string, unknown>;
|
|
352
|
+
/** Options for {@link createQueueCaptureSink}. */
|
|
353
|
+
interface QueueCaptureOptions {
|
|
354
|
+
/** Pin the consumed-message log to a Cloudflare data-residency jurisdiction (match the worker's `jurisdiction`). */
|
|
355
|
+
jurisdiction?: string;
|
|
356
|
+
/** Shard the consumed-message log lives on; override if the worker sets a custom `defaultShardKey`. */
|
|
357
|
+
rootShard?: string;
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Whether consumed queue messages should be captured into the studio's log.
|
|
361
|
+
* Explicit `LUNORA_QUEUE_CAPTURE` (`"1"`/`"true"` vs `"0"`/`"false"`) always wins;
|
|
362
|
+
* unset, capture is on only in a development environment. Mirrors
|
|
363
|
+
* `@lunora/mail`'s `shouldCaptureMail` so mail and queue dev capture toggle the
|
|
364
|
+
* same way.
|
|
365
|
+
*/
|
|
366
|
+
declare const shouldCaptureQueue: (env: QueueEnv) => boolean;
|
|
367
|
+
/**
|
|
368
|
+
* Build the {@link QueueCaptureSink} that records a processed batch into the
|
|
369
|
+
* studio's root-shard consumed-message log via the reserved `recordQueueMessage`
|
|
370
|
+
* admin RPC. Best-effort by contract: without the `SHARD` binding or
|
|
371
|
+
* `LUNORA_ADMIN_TOKEN` it no-ops, and `dispatchQueueBatch` swallows a
|
|
372
|
+
* rejection, so capture never changes delivery semantics.
|
|
373
|
+
*/
|
|
374
|
+
declare const createQueueCaptureSink: (env: QueueEnv, options?: QueueCaptureOptions) => QueueCaptureSink;
|
|
375
|
+
/**
|
|
376
|
+
* Build the `ctx.queues` map for a request: resolve every spec's `env[binding]`
|
|
377
|
+
* into the `exportName → Queue binding` map and wrap it in {@link createQueues}.
|
|
378
|
+
* A spec whose binding is absent from `env` is skipped here — the helpful "no
|
|
379
|
+
* queue named …" error is raised lazily by `ctx.queues.<name>.send(...)` when
|
|
380
|
+
* the missing queue is actually used.
|
|
381
|
+
*/
|
|
382
|
+
declare const createQueueContext: <Name extends string>(env: Record<string, unknown>, specs: ReadonlyArray<QueueBindingSpec<Name>>) => Queues<Name>;
|
|
383
|
+
/**
|
|
384
|
+
* Build the `ctx.queues` map from `lunora/queues.ts` export name → Cloudflare
|
|
385
|
+
* `Queue` binding. Each property is a typed {@link QueueProducer}; accessing an
|
|
386
|
+
* export whose binding is absent throws a directed error naming the declared
|
|
387
|
+
* queues (raised lazily on first use).
|
|
388
|
+
*/
|
|
389
|
+
declare const createQueues: <Name extends string>(options: LunoraQueuesOptions<Name>) => Queues<Name>;
|
|
390
|
+
/**
|
|
391
|
+
* The wrangler producer binding name for a queue export: `emailQueue` →
|
|
392
|
+
* `QUEUE_EMAIL_QUEUE`, `email` → `QUEUE_EMAIL`. The `QUEUE_` prefix namespaces
|
|
393
|
+
* these away from `SHARD`/`SESSION`/`SCHEDULER`/`CONTAINER_*` so a
|
|
394
|
+
* queue export can never collide with the built-in bindings.
|
|
395
|
+
*/
|
|
396
|
+
declare const queueBindingName: (exportName: string) => string;
|
|
397
|
+
/**
|
|
398
|
+
* The stable queue name wrangler registers (`queues.producers[].queue` and
|
|
399
|
+
* `queues.consumers[].queue`): `emailQueue` → `email-queue`. Used as the
|
|
400
|
+
* deployed queue's identifier when no explicit `name` override is given.
|
|
401
|
+
*/
|
|
402
|
+
declare const queueDefaultName: (exportName: string) => string;
|
|
403
|
+
/**
|
|
404
|
+
* Declare a Cloudflare Queue deployed alongside the app. Pure validation +
|
|
405
|
+
* branding: codegen discovers the export, emits the typed `ctx.queues.<name>`
|
|
406
|
+
* producer and (for push consumers) the worker `queue()` dispatch; the config
|
|
407
|
+
* layer reconciles the wrangler `queues.producers[]` / `queues.consumers[]`
|
|
408
|
+
* entries from the same definition.
|
|
409
|
+
*
|
|
410
|
+
* ```ts
|
|
411
|
+
* // lunora/queues.ts
|
|
412
|
+
* import { defineQueue } from "@lunora/queue";
|
|
413
|
+
* import { api } from "./_generated/api";
|
|
414
|
+
*
|
|
415
|
+
* export const emailQueue = defineQueue<{ to: string }>({
|
|
416
|
+
* handler: async (ctx, batch) => {
|
|
417
|
+
* for (const message of batch.messages) {
|
|
418
|
+
* await message.run(api.email.send, { to: message.body.to });
|
|
419
|
+
* message.ack();
|
|
420
|
+
* }
|
|
421
|
+
* },
|
|
422
|
+
* });
|
|
423
|
+
* ```
|
|
424
|
+
*
|
|
425
|
+
* Enqueue from a mutation or action: `await ctx.queues.emailQueue.send({ to })`.
|
|
426
|
+
*
|
|
427
|
+
* `message.run(...)` is `ctx.run(...)` pinned to that message — prefer it inside
|
|
428
|
+
* the batch loop. A deterministic failure (400/403/404/422) from a pinned call
|
|
429
|
+
* is attributed to its message: that one is acked and the rest are retried,
|
|
430
|
+
* instead of one poison message re-delivering (and eventually dead-lettering)
|
|
431
|
+
* the whole batch.
|
|
432
|
+
*
|
|
433
|
+
* ⚠️ **Privileged dispatch.** A push handler's `ctx.run(...)` calls back into
|
|
434
|
+
* Lunora functions over the admin-authenticated dispatch endpoint (the same
|
|
435
|
+
* trusted path the scheduler and workflows use), so those calls run with the
|
|
436
|
+
* system identity — **end-user RLS is not applied**. Treat a queue handler as
|
|
437
|
+
* trusted server code: validate `message.body` (it may be attacker-influenced if
|
|
438
|
+
* anything user-facing can enqueue) before acting on it, and don't forward an
|
|
439
|
+
* unchecked body straight into a privileged mutation.
|
|
440
|
+
*/
|
|
441
|
+
declare const defineQueue: <Body = unknown>(config: QueueConfig<Body>) => QueueDefinition<Body>;
|
|
442
|
+
/** True when a value is a `defineQueue` result (the runtime brand check). */
|
|
443
|
+
declare const isQueueDefinition: (value: unknown) => value is QueueDefinition;
|
|
264
444
|
interface RunContextOptions {
|
|
265
445
|
env: Record<string, unknown>;
|
|
266
446
|
exportName: string;
|
|
267
447
|
fetchImpl?: typeof fetch;
|
|
448
|
+
/** The consumer invocation's `traceparent`, so `ctx.run` joins the queue's trace. */
|
|
449
|
+
traceparent?: string;
|
|
268
450
|
}
|
|
269
451
|
/** Assemble the {@link QueueRunContext} passed to a `defineQueue` handler. */
|
|
270
452
|
declare const createQueueRunContext: (options: RunContextOptions) => QueueRunContext;
|
|
271
|
-
|
|
453
|
+
/**
|
|
454
|
+
* Declare a topic in `lunora/queues.ts`. The payload type is the only thing a
|
|
455
|
+
* topic carries; delivery config lives on each subscription.
|
|
456
|
+
*/
|
|
457
|
+
declare const defineTopic: <Payload = unknown>() => TopicDefinition<Payload>;
|
|
458
|
+
/**
|
|
459
|
+
* Subscribe to a topic. The result is a push queue definition — export it from
|
|
460
|
+
* `lunora/queues.ts` and it deploys, retries and dead-letters exactly like a
|
|
461
|
+
* `defineQueue` export of the same name.
|
|
462
|
+
*
|
|
463
|
+
* ⚠️ Same trust model as a queue handler: `message.run(...)` dispatches with the
|
|
464
|
+
* system identity, so validate `message.body` before acting on it.
|
|
465
|
+
*/
|
|
466
|
+
declare const defineSubscription: <Payload>(topic: TopicDefinition<Payload>, config: SubscriptionConfig<Payload>) => SubscriptionDefinition<Payload>;
|
|
467
|
+
/**
|
|
468
|
+
* Build `ctx.topics` for a request from the Worker `env` and the codegen-emitted
|
|
469
|
+
* specs. A topic with no subscriptions publishes to nobody; a topic with an
|
|
470
|
+
* unbound subscription rejects every publish (see {@link unboundPublisher}).
|
|
471
|
+
*/
|
|
472
|
+
declare const createTopicContext: <Name extends string>(env: Record<string, unknown>, specs: ReadonlyArray<TopicBindingSpec<Name>>) => Topics<Name>;
|
|
473
|
+
export { type ArgsOf, type CapturedQueueMessage, type FunctionReference, type LunoraQueuesOptions, type QueueBindingSpec, type QueueCaptureOptions, type QueueCaptureSink, type QueueConfig, type QueueConsumerMode, type QueueConsumerTuning, type QueueDefinition, type QueueEnv, type QueueHandler, type DispatchLogger as QueueLogger, type QueueMessage, type QueueMessageBatch, type QueueProducer, type QueueRegistry, type QueueRegistryEntry, type QueueRunContext, type DispatchRunFunction as QueueRunFunction, type Queues, type RunFunctionOptions, type SubscriptionConfig, type SubscriptionDefinition, type TopicBindingSpec, type TopicDefinition, type TopicPublisher, type Topics, createQueueCaptureSink, createQueueContext, createQueueRunContext, createQueues, createTopicContext, defineQueue, defineSubscription, defineTopic, dispatchQueueBatch, isQueueDefinition, queueBindingName, queueDefaultName, shouldCaptureQueue };
|