@lunora/queue 1.0.0-alpha.3 → 1.0.0-alpha.31
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 +7 -1
- package/dist/index.d.mts +219 -161
- package/dist/index.d.ts +219 -161
- package/dist/index.mjs +1 -5
- package/dist/packem_shared/createQueueCaptureSink-D8pUrrb_.mjs +1 -0
- package/dist/packem_shared/createQueueContext-B9PgIzBM.mjs +1 -0
- package/dist/packem_shared/createQueueRunContext-Bxw8EGDA.mjs +1 -0
- package/dist/packem_shared/createQueues-FpKDoLFX.mjs +1 -0
- package/dist/packem_shared/defineQueue-TY4-i3nG.mjs +1 -0
- package/dist/packem_shared/dispatchQueueBatch-dejEVSC6.mjs +1 -0
- package/dist/packem_shared/run-context-C97v6VyP.mjs +1 -0
- package/package.json +3 -2
- package/dist/packem_shared/createQueueContext-D0XCdCsd.mjs +0 -14
- package/dist/packem_shared/createQueueRunContext-_2hD-TK7.mjs +0 -73
- package/dist/packem_shared/createQueues-14-vSICK.mjs +0 -33
- package/dist/packem_shared/defineQueue-D40gREfg.mjs +0 -18
- package/dist/packem_shared/dispatchQueueBatch-DR15pYS7.mjs +0 -19
package/LICENSE.md
CHANGED
|
@@ -103,3 +103,9 @@ Unless required by applicable law or agreed to in writing, software distributed
|
|
|
103
103
|
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
104
|
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
105
|
specific language governing permissions and limitations under the License.
|
|
106
|
+
|
|
107
|
+
<!-- DEPENDENCIES -->
|
|
108
|
+
<!-- /DEPENDENCIES -->
|
|
109
|
+
|
|
110
|
+
<!-- TYPE_DEPENDENCIES -->
|
|
111
|
+
<!-- /TYPE_DEPENDENCIES -->
|
package/README.md
CHANGED
|
@@ -26,13 +26,19 @@ import { api } from "./_generated/api";
|
|
|
26
26
|
export const emailQueue = defineQueue<{ to: string }>({
|
|
27
27
|
handler: async (ctx, batch) => {
|
|
28
28
|
for (const message of batch.messages) {
|
|
29
|
-
await
|
|
29
|
+
await message.run(api.email.send, { to: message.body.to });
|
|
30
30
|
message.ack();
|
|
31
31
|
}
|
|
32
32
|
},
|
|
33
33
|
});
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
+
`message.run(...)` is `ctx.run(...)` pinned to that message. Call it inside the
|
|
37
|
+
batch loop: when the dispatched function fails deterministically (`400`, `403`,
|
|
38
|
+
`404`, `422`), the consumer acks just that message and retries the rest instead
|
|
39
|
+
of letting one poison message retry — and eventually dead-letter — the whole
|
|
40
|
+
batch.
|
|
41
|
+
|
|
36
42
|
```ts
|
|
37
43
|
// inside a mutation/action
|
|
38
44
|
await ctx.queues.emailQueue.send({ to: user.email });
|
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,19 @@ 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
|
+
* Correlate this call with a caller-defined message/item id (e.g. a queue
|
|
19
|
+
* message's `id`). Purely local bookkeeping — never sent to the dispatch
|
|
20
|
+
* endpoint — carried onto the `LunoraError` a deterministic dispatch
|
|
21
|
+
* failure throws, so a batching consumer (`@lunora/queue`'s push handler)
|
|
22
|
+
* can read it back and attribute the failure to the one item that caused
|
|
23
|
+
* it instead of the whole batch. Optional and inert when omitted.
|
|
24
|
+
*/
|
|
25
|
+
messageId?: string;
|
|
15
26
|
/** Route the call to a specific shard (defaults to the worker's root shard). */
|
|
16
27
|
shardKey?: string;
|
|
28
|
+
/** Abort the dispatch after this many ms; the abort is retryable. Overrides the runner's default. */
|
|
29
|
+
timeoutMs?: number;
|
|
17
30
|
}
|
|
18
31
|
/** Invoke a Lunora function (query/mutation/action) by reference. The shape of `ctx.run`. */
|
|
19
32
|
type DispatchRunFunction = <F extends FunctionReference>(function_: F, args?: ArgsOf<F>, options?: RunFunctionOptions) => Promise<unknown>;
|
|
@@ -24,66 +37,11 @@ interface DispatchLogger {
|
|
|
24
37
|
info: (message: unknown, ...rest: unknown[]) => void;
|
|
25
38
|
warn: (message: unknown, ...rest: unknown[]) => void;
|
|
26
39
|
}
|
|
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
40
|
/**
|
|
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
|
-
*/
|
|
41
|
+
* The typed producer bound to `ctx.queues.<name>`. Sending is a side effect, so
|
|
42
|
+
* the generated context exposes this only on `MutationCtx` / `ActionCtx` (never
|
|
43
|
+
* the deterministic `QueryCtx`), mirroring `ctx.scheduler` / `ctx.workflows`.
|
|
44
|
+
*/
|
|
87
45
|
interface QueueProducer<Body = unknown> {
|
|
88
46
|
/** Enqueue one message. */
|
|
89
47
|
send: (body: Body, options?: QueueSendOptions) => Promise<void>;
|
|
@@ -91,10 +49,10 @@ interface QueueProducer<Body = unknown> {
|
|
|
91
49
|
sendBatch: (messages: Iterable<MessageSendRequestLike<Body>>, options?: QueueSendBatchOptions) => Promise<void>;
|
|
92
50
|
}
|
|
93
51
|
/**
|
|
94
|
-
* `ctx.queues` — the map of declared queue export names → typed producers.
|
|
95
|
-
* Codegen narrows this to the exact export names; the package keeps it open so
|
|
96
|
-
* `createQueues` stays schema-agnostic.
|
|
97
|
-
*/
|
|
52
|
+
* `ctx.queues` — the map of declared queue export names → typed producers.
|
|
53
|
+
* Codegen narrows this to the exact export names; the package keeps it open so
|
|
54
|
+
* `createQueues` stays schema-agnostic.
|
|
55
|
+
*/
|
|
98
56
|
interface Queues {
|
|
99
57
|
[exportName: string]: QueueProducer;
|
|
100
58
|
}
|
|
@@ -104,23 +62,49 @@ interface LunoraQueuesOptions {
|
|
|
104
62
|
bindings: Record<string, QueueBindingLike>;
|
|
105
63
|
}
|
|
106
64
|
/**
|
|
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
|
-
*/
|
|
65
|
+
* The context handed to a `defineQueue` handler. Decoupled from `@lunora/server`
|
|
66
|
+
* (like the workflow run context): to touch data, call a Lunora mutation/action
|
|
67
|
+
* via `ctx.run(api.x.y, args)` — the dispatch goes through the same
|
|
68
|
+
* `/_lunora/scheduler/dispatch` path the SchedulerDO and workflows use.
|
|
69
|
+
*/
|
|
112
70
|
interface QueueRunContext {
|
|
113
71
|
/** The worker `env` (bindings + vars). */
|
|
114
72
|
readonly env: Record<string, unknown>;
|
|
115
73
|
/** Queue-name-prefixed logger. */
|
|
116
74
|
readonly log: DispatchLogger;
|
|
117
|
-
/**
|
|
75
|
+
/**
|
|
76
|
+
* Invoke a Lunora function (query/mutation/action) by reference.
|
|
77
|
+
*
|
|
78
|
+
* Batch-unaware: a failure it throws is attributed to nothing, so a
|
|
79
|
+
* deterministic failure retries the whole batch. Inside the `batch.messages`
|
|
80
|
+
* loop, prefer {@link QueueMessage.run}, which pins the call to its message.
|
|
81
|
+
*/
|
|
118
82
|
readonly run: DispatchRunFunction;
|
|
119
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* One delivered message as the push handler sees it: the Cloudflare `Message`
|
|
86
|
+
* plus `run` — a {@link QueueRunContext.run} pinned to THIS message.
|
|
87
|
+
*
|
|
88
|
+
* Prefer `message.run(api.x.y, args)` over `ctx.run(...)` inside the batch
|
|
89
|
+
* loop. The pin is what lets the dispatcher attribute a deterministic dispatch
|
|
90
|
+
* failure (400/403/404/422) to the one message that caused it: that message is
|
|
91
|
+
* acked and every other one is retried, instead of the whole batch being
|
|
92
|
+
* re-delivered because of a single poison message. A plain `ctx.run` call
|
|
93
|
+
* carries no message id, so its failure stays unattributed and the whole batch
|
|
94
|
+
* retries.
|
|
95
|
+
*/
|
|
96
|
+
interface QueueMessage<Body = unknown> extends MessageLike<Body> {
|
|
97
|
+
/** {@link QueueRunContext.run}, pinned to this message for failure attribution. */
|
|
98
|
+
readonly run: DispatchRunFunction;
|
|
99
|
+
}
|
|
100
|
+
/** The delivered batch as the push handler sees it — {@link QueueMessage}s rather than bare `Message`s. */
|
|
101
|
+
interface QueueMessageBatch<Body = unknown> extends Omit<MessageBatchLike<Body>, "messages"> {
|
|
102
|
+
readonly messages: ReadonlyArray<QueueMessage<Body>>;
|
|
103
|
+
}
|
|
120
104
|
/** Whether a declared queue is consumed by this worker (push) or polled externally (pull). */
|
|
121
105
|
type QueueConsumerMode = "pull" | "push";
|
|
122
106
|
/** The handler body run for each delivered batch (push consumers only). */
|
|
123
|
-
type QueueHandler<Body = unknown> = (context: QueueRunContext, batch:
|
|
107
|
+
type QueueHandler<Body = unknown> = (context: QueueRunContext, batch: QueueMessageBatch<Body>) => Promise<void> | void;
|
|
124
108
|
/** Push-consumer batch/retry tuning, mirrored onto the wrangler `queues.consumers[]` entry. */
|
|
125
109
|
interface QueueConsumerTuning {
|
|
126
110
|
/** Name of the dead-letter queue messages land in after `maxRetries`. */
|
|
@@ -137,25 +121,25 @@ interface QueueConsumerTuning {
|
|
|
137
121
|
/** The config object passed to `defineQueue`. */
|
|
138
122
|
interface QueueConfig<Body = unknown> extends QueueConsumerTuning {
|
|
139
123
|
/**
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
124
|
+
* The push-consumer body. Required for `mode: "push"` (the default); omit it
|
|
125
|
+
* for `mode: "pull"`, where an external worker polls the queue over HTTP.
|
|
126
|
+
*/
|
|
143
127
|
handler?: QueueHandler<Body>;
|
|
144
128
|
/** How this queue is consumed. Defaults to `"push"`. */
|
|
145
129
|
mode?: QueueConsumerMode;
|
|
146
130
|
/**
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
131
|
+
* Stable wrangler queue name (`queues.producers[].queue`). Defaults to the
|
|
132
|
+
* kebab-cased export name (`emailQueue` → `email-queue`).
|
|
133
|
+
*/
|
|
150
134
|
name?: string;
|
|
151
135
|
}
|
|
152
136
|
/** The branded result of `defineQueue`, discovered by codegen + config. */
|
|
153
137
|
interface QueueDefinition<Body = unknown> extends QueueConfig<Body> {
|
|
154
138
|
/**
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
139
|
+
* Phantom carrier for the message body type, so codegen can type the
|
|
140
|
+
* generated `ctx.queues.<name>` producer as `QueueProducer<Body>` from
|
|
141
|
+
* `typeof <export>`. Never assigned at runtime (type-only).
|
|
142
|
+
*/
|
|
159
143
|
readonly __lunoraBody?: Body;
|
|
160
144
|
/** Runtime brand identifying a `defineQueue` result. */
|
|
161
145
|
isLunoraQueue: true;
|
|
@@ -169,98 +153,172 @@ interface QueueBindingSpec {
|
|
|
169
153
|
/** The stable wrangler queue name, e.g. `email-queue`. */
|
|
170
154
|
name: string;
|
|
171
155
|
}
|
|
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
156
|
/** One declared queue, keyed for batch routing by its stable wrangler name. */
|
|
236
157
|
interface QueueRegistryEntry {
|
|
237
158
|
/**
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
159
|
+
* The `defineQueue` result (carries the push handler). The body type is
|
|
160
|
+
* erased to `any` here because the registry is heterogeneous — different
|
|
161
|
+
* queues carry different message bodies, and the handler param is
|
|
162
|
+
* contravariant, so a precise `QueueDefinition<Body>` would not be assignable
|
|
163
|
+
* to a shared `unknown`-bodied slot. Runtime dispatch passes the delivered
|
|
164
|
+
* batch straight through, so the erasure is type-only.
|
|
165
|
+
*/
|
|
245
166
|
definition: QueueDefinition<any>;
|
|
246
167
|
/** The `lunora/queues.ts` export name, for log correlation. */
|
|
247
168
|
exportName: string;
|
|
248
169
|
}
|
|
249
170
|
/** Map of stable wrangler queue name → registry entry, built by codegen. */
|
|
250
171
|
type QueueRegistry = Record<string, QueueRegistryEntry>;
|
|
172
|
+
/** The disposition a consumer left one message in for a single delivery attempt. */
|
|
173
|
+
type QueueMessageOutcome = "ack" | "error" | "retry";
|
|
174
|
+
/**
|
|
175
|
+
* One consumed message as captured by {@link dispatchQueueBatch} and handed to an
|
|
176
|
+
* {@link QueueCaptureSink}. Structurally matches `@lunora/do`'s
|
|
177
|
+
* `RecordQueueMessageInput` (the reserved `recordQueueMessage` admin RPC payload);
|
|
178
|
+
* the two packages share only this contract, so keep them in sync by hand.
|
|
179
|
+
*/
|
|
180
|
+
interface CapturedQueueMessage {
|
|
181
|
+
/** Delivery attempt number for this message (`message.attempts`). */
|
|
182
|
+
attempts: number;
|
|
183
|
+
/** The message body (JSON-encoded + capped by the catcher). */
|
|
184
|
+
body: unknown;
|
|
185
|
+
/** `true` when this failed delivery was the message's last (its retries are exhausted — the broker dead-letters it). */
|
|
186
|
+
deadLettered: boolean;
|
|
187
|
+
/** Handler error message when `outcome` is `error`; absent otherwise. */
|
|
188
|
+
error?: string;
|
|
189
|
+
/** The `lunora/queues.ts` export name that consumed it. */
|
|
190
|
+
exportName: string;
|
|
191
|
+
/** The delivered message id (`message.id`). */
|
|
192
|
+
messageId: string;
|
|
193
|
+
/** How the handler disposed of the message this attempt. */
|
|
194
|
+
outcome: QueueMessageOutcome;
|
|
195
|
+
/** The stable wrangler queue name the batch was delivered from (`batch.queue`). */
|
|
196
|
+
queue: string;
|
|
197
|
+
/** Original message timestamp in epoch-ms (`message.timestamp`). */
|
|
198
|
+
timestamp: number;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Persists a batch of consumed messages. The codegen worker wires this to POST the
|
|
202
|
+
* batch to the root shard's `recordQueueMessage` admin RPC (the dev queue catcher).
|
|
203
|
+
* Best-effort by contract: {@link dispatchQueueBatch} swallows a rejection so a
|
|
204
|
+
* capture failure never changes delivery semantics.
|
|
205
|
+
*/
|
|
206
|
+
type QueueCaptureSink = (messages: CapturedQueueMessage[]) => Promise<void> | void;
|
|
251
207
|
interface DispatchOptions {
|
|
208
|
+
/**
|
|
209
|
+
* Optional capture sink. When set, every message's final disposition is
|
|
210
|
+
* turned into a record and handed to this sink after the handler runs.
|
|
211
|
+
* Omitted in production unless queue capture is enabled, so a consumer pays
|
|
212
|
+
* no record-building or sink cost by default. Delivery semantics — including
|
|
213
|
+
* poison-message isolation — do not depend on it.
|
|
214
|
+
*/
|
|
215
|
+
capture?: QueueCaptureSink;
|
|
252
216
|
/** Worker `env`, forwarded to the queue run context. */
|
|
253
217
|
env: Record<string, unknown>;
|
|
254
218
|
/** Injectable fetch for the `ctx.run` dispatcher (tests). */
|
|
255
219
|
fetchImpl?: typeof fetch;
|
|
256
220
|
}
|
|
257
221
|
/**
|
|
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
|
-
*/
|
|
222
|
+
* Look up the handler for `batch.queue` and invoke it with a fresh
|
|
223
|
+
* `QueueRunContext`. Throws a directed error when no push handler is registered
|
|
224
|
+
* for the delivered queue (a misconfiguration — the consumer was declared
|
|
225
|
+
* `pull`, or the queue name drifted from the `defineQueue` export).
|
|
226
|
+
*/
|
|
263
227
|
declare const dispatchQueueBatch: (batch: MessageBatchLike, registry: QueueRegistry, options: DispatchOptions) => Promise<void>;
|
|
228
|
+
/** A Worker `env` projected as a plain record (vars, secrets, and bindings are `unknown`-valued). */
|
|
229
|
+
type QueueEnv = Record<string, unknown>;
|
|
230
|
+
/** Options for {@link createQueueCaptureSink}. */
|
|
231
|
+
interface QueueCaptureOptions {
|
|
232
|
+
/** Pin the consumed-message log to a Cloudflare data-residency jurisdiction (match the worker's `jurisdiction`). */
|
|
233
|
+
jurisdiction?: string;
|
|
234
|
+
/** Shard the consumed-message log lives on; override if the worker sets a custom `defaultShardKey`. */
|
|
235
|
+
rootShard?: string;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Whether consumed queue messages should be captured into the studio's log.
|
|
239
|
+
* Explicit `LUNORA_QUEUE_CAPTURE` (`"1"`/`"true"` vs `"0"`/`"false"`) always wins;
|
|
240
|
+
* unset, capture is on only in a development environment. Mirrors
|
|
241
|
+
* `@lunora/mail`'s `shouldCaptureMail` so mail and queue dev capture toggle the
|
|
242
|
+
* same way.
|
|
243
|
+
*/
|
|
244
|
+
declare const shouldCaptureQueue: (env: QueueEnv) => boolean;
|
|
245
|
+
/**
|
|
246
|
+
* Build the {@link QueueCaptureSink} that records a processed batch into the
|
|
247
|
+
* studio's root-shard consumed-message log via the reserved `recordQueueMessage`
|
|
248
|
+
* admin RPC. Best-effort by contract: without the `SHARD` binding or
|
|
249
|
+
* `LUNORA_ADMIN_TOKEN` it no-ops, and `dispatchQueueBatch` swallows a
|
|
250
|
+
* rejection, so capture never changes delivery semantics.
|
|
251
|
+
*/
|
|
252
|
+
declare const createQueueCaptureSink: (env: QueueEnv, options?: QueueCaptureOptions) => QueueCaptureSink;
|
|
253
|
+
/**
|
|
254
|
+
* Build the `ctx.queues` map for a request: resolve every spec's `env[binding]`
|
|
255
|
+
* into the `exportName → Queue binding` map and wrap it in {@link createQueues}.
|
|
256
|
+
* A spec whose binding is absent from `env` is skipped here — the helpful "no
|
|
257
|
+
* queue named …" error is raised lazily by `ctx.queues.<name>.send(...)` when
|
|
258
|
+
* the missing queue is actually used.
|
|
259
|
+
*/
|
|
260
|
+
declare const createQueueContext: (env: Record<string, unknown>, specs: ReadonlyArray<QueueBindingSpec>) => Queues;
|
|
261
|
+
/**
|
|
262
|
+
* Build the `ctx.queues` map from `lunora/queues.ts` export name → Cloudflare
|
|
263
|
+
* `Queue` binding. Each property is a typed {@link QueueProducer}; accessing an
|
|
264
|
+
* export whose binding is absent throws a directed error naming the declared
|
|
265
|
+
* queues (raised lazily on first use).
|
|
266
|
+
*/
|
|
267
|
+
declare const createQueues: (options: LunoraQueuesOptions) => Queues;
|
|
268
|
+
/**
|
|
269
|
+
* The wrangler producer binding name for a queue export: `emailQueue` →
|
|
270
|
+
* `QUEUE_EMAIL_QUEUE`, `email` → `QUEUE_EMAIL`. The `QUEUE_` prefix namespaces
|
|
271
|
+
* these away from `SHARD`/`SESSION`/`SCHEDULER`/`WORKFLOW_*`/`CONTAINER_*` so a
|
|
272
|
+
* queue export can never collide with the built-in bindings.
|
|
273
|
+
*/
|
|
274
|
+
declare const queueBindingName: (exportName: string) => string;
|
|
275
|
+
/**
|
|
276
|
+
* The stable queue name wrangler registers (`queues.producers[].queue` and
|
|
277
|
+
* `queues.consumers[].queue`): `emailQueue` → `email-queue`. Used as the
|
|
278
|
+
* deployed queue's identifier when no explicit `name` override is given.
|
|
279
|
+
*/
|
|
280
|
+
declare const queueDefaultName: (exportName: string) => string;
|
|
281
|
+
/**
|
|
282
|
+
* Declare a Cloudflare Queue deployed alongside the app. Pure validation +
|
|
283
|
+
* branding: codegen discovers the export, emits the typed `ctx.queues.<name>`
|
|
284
|
+
* producer and (for push consumers) the worker `queue()` dispatch; the config
|
|
285
|
+
* layer reconciles the wrangler `queues.producers[]` / `queues.consumers[]`
|
|
286
|
+
* entries from the same definition.
|
|
287
|
+
*
|
|
288
|
+
* ```ts
|
|
289
|
+
* // lunora/queues.ts
|
|
290
|
+
* import { defineQueue } from "@lunora/queue";
|
|
291
|
+
* import { api } from "./_generated/api";
|
|
292
|
+
*
|
|
293
|
+
* export const emailQueue = defineQueue<{ to: string }>({
|
|
294
|
+
* handler: async (ctx, batch) => {
|
|
295
|
+
* for (const message of batch.messages) {
|
|
296
|
+
* await message.run(api.email.send, { to: message.body.to });
|
|
297
|
+
* message.ack();
|
|
298
|
+
* }
|
|
299
|
+
* },
|
|
300
|
+
* });
|
|
301
|
+
* ```
|
|
302
|
+
*
|
|
303
|
+
* Enqueue from a mutation or action: `await ctx.queues.emailQueue.send({ to })`.
|
|
304
|
+
*
|
|
305
|
+
* `message.run(...)` is `ctx.run(...)` pinned to that message — prefer it inside
|
|
306
|
+
* the batch loop. A deterministic failure (400/403/404/422) from a pinned call
|
|
307
|
+
* is attributed to its message: that one is acked and the rest are retried,
|
|
308
|
+
* instead of one poison message re-delivering (and eventually dead-lettering)
|
|
309
|
+
* the whole batch.
|
|
310
|
+
*
|
|
311
|
+
* ⚠️ **Privileged dispatch.** A push handler's `ctx.run(...)` calls back into
|
|
312
|
+
* Lunora functions over the admin-authenticated dispatch endpoint (the same
|
|
313
|
+
* trusted path the scheduler and workflows use), so those calls run with the
|
|
314
|
+
* system identity — **end-user RLS is not applied**. Treat a queue handler as
|
|
315
|
+
* trusted server code: validate `message.body` (it may be attacker-influenced if
|
|
316
|
+
* anything user-facing can enqueue) before acting on it, and don't forward an
|
|
317
|
+
* unchecked body straight into a privileged mutation.
|
|
318
|
+
*/
|
|
319
|
+
declare const defineQueue: <Body = unknown>(config: QueueConfig<Body>) => QueueDefinition<Body>;
|
|
320
|
+
/** True when a value is a `defineQueue` result (the runtime brand check). */
|
|
321
|
+
declare const isQueueDefinition: (value: unknown) => value is QueueDefinition;
|
|
264
322
|
interface RunContextOptions {
|
|
265
323
|
env: Record<string, unknown>;
|
|
266
324
|
exportName: string;
|
|
@@ -268,4 +326,4 @@ interface RunContextOptions {
|
|
|
268
326
|
}
|
|
269
327
|
/** Assemble the {@link QueueRunContext} passed to a `defineQueue` handler. */
|
|
270
328
|
declare const createQueueRunContext: (options: RunContextOptions) => QueueRunContext;
|
|
271
|
-
export { type ArgsOf, type
|
|
329
|
+
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, createQueueCaptureSink, createQueueContext, createQueueRunContext, createQueues, defineQueue, dispatchQueueBatch, isQueueDefinition, queueBindingName, queueDefaultName, shouldCaptureQueue };
|
package/dist/index.d.ts
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,19 @@ 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
|
+
* Correlate this call with a caller-defined message/item id (e.g. a queue
|
|
19
|
+
* message's `id`). Purely local bookkeeping — never sent to the dispatch
|
|
20
|
+
* endpoint — carried onto the `LunoraError` a deterministic dispatch
|
|
21
|
+
* failure throws, so a batching consumer (`@lunora/queue`'s push handler)
|
|
22
|
+
* can read it back and attribute the failure to the one item that caused
|
|
23
|
+
* it instead of the whole batch. Optional and inert when omitted.
|
|
24
|
+
*/
|
|
25
|
+
messageId?: string;
|
|
15
26
|
/** Route the call to a specific shard (defaults to the worker's root shard). */
|
|
16
27
|
shardKey?: string;
|
|
28
|
+
/** Abort the dispatch after this many ms; the abort is retryable. Overrides the runner's default. */
|
|
29
|
+
timeoutMs?: number;
|
|
17
30
|
}
|
|
18
31
|
/** Invoke a Lunora function (query/mutation/action) by reference. The shape of `ctx.run`. */
|
|
19
32
|
type DispatchRunFunction = <F extends FunctionReference>(function_: F, args?: ArgsOf<F>, options?: RunFunctionOptions) => Promise<unknown>;
|
|
@@ -24,66 +37,11 @@ interface DispatchLogger {
|
|
|
24
37
|
info: (message: unknown, ...rest: unknown[]) => void;
|
|
25
38
|
warn: (message: unknown, ...rest: unknown[]) => void;
|
|
26
39
|
}
|
|
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
40
|
/**
|
|
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
|
-
*/
|
|
41
|
+
* The typed producer bound to `ctx.queues.<name>`. Sending is a side effect, so
|
|
42
|
+
* the generated context exposes this only on `MutationCtx` / `ActionCtx` (never
|
|
43
|
+
* the deterministic `QueryCtx`), mirroring `ctx.scheduler` / `ctx.workflows`.
|
|
44
|
+
*/
|
|
87
45
|
interface QueueProducer<Body = unknown> {
|
|
88
46
|
/** Enqueue one message. */
|
|
89
47
|
send: (body: Body, options?: QueueSendOptions) => Promise<void>;
|
|
@@ -91,10 +49,10 @@ interface QueueProducer<Body = unknown> {
|
|
|
91
49
|
sendBatch: (messages: Iterable<MessageSendRequestLike<Body>>, options?: QueueSendBatchOptions) => Promise<void>;
|
|
92
50
|
}
|
|
93
51
|
/**
|
|
94
|
-
* `ctx.queues` — the map of declared queue export names → typed producers.
|
|
95
|
-
* Codegen narrows this to the exact export names; the package keeps it open so
|
|
96
|
-
* `createQueues` stays schema-agnostic.
|
|
97
|
-
*/
|
|
52
|
+
* `ctx.queues` — the map of declared queue export names → typed producers.
|
|
53
|
+
* Codegen narrows this to the exact export names; the package keeps it open so
|
|
54
|
+
* `createQueues` stays schema-agnostic.
|
|
55
|
+
*/
|
|
98
56
|
interface Queues {
|
|
99
57
|
[exportName: string]: QueueProducer;
|
|
100
58
|
}
|
|
@@ -104,23 +62,49 @@ interface LunoraQueuesOptions {
|
|
|
104
62
|
bindings: Record<string, QueueBindingLike>;
|
|
105
63
|
}
|
|
106
64
|
/**
|
|
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
|
-
*/
|
|
65
|
+
* The context handed to a `defineQueue` handler. Decoupled from `@lunora/server`
|
|
66
|
+
* (like the workflow run context): to touch data, call a Lunora mutation/action
|
|
67
|
+
* via `ctx.run(api.x.y, args)` — the dispatch goes through the same
|
|
68
|
+
* `/_lunora/scheduler/dispatch` path the SchedulerDO and workflows use.
|
|
69
|
+
*/
|
|
112
70
|
interface QueueRunContext {
|
|
113
71
|
/** The worker `env` (bindings + vars). */
|
|
114
72
|
readonly env: Record<string, unknown>;
|
|
115
73
|
/** Queue-name-prefixed logger. */
|
|
116
74
|
readonly log: DispatchLogger;
|
|
117
|
-
/**
|
|
75
|
+
/**
|
|
76
|
+
* Invoke a Lunora function (query/mutation/action) by reference.
|
|
77
|
+
*
|
|
78
|
+
* Batch-unaware: a failure it throws is attributed to nothing, so a
|
|
79
|
+
* deterministic failure retries the whole batch. Inside the `batch.messages`
|
|
80
|
+
* loop, prefer {@link QueueMessage.run}, which pins the call to its message.
|
|
81
|
+
*/
|
|
118
82
|
readonly run: DispatchRunFunction;
|
|
119
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* One delivered message as the push handler sees it: the Cloudflare `Message`
|
|
86
|
+
* plus `run` — a {@link QueueRunContext.run} pinned to THIS message.
|
|
87
|
+
*
|
|
88
|
+
* Prefer `message.run(api.x.y, args)` over `ctx.run(...)` inside the batch
|
|
89
|
+
* loop. The pin is what lets the dispatcher attribute a deterministic dispatch
|
|
90
|
+
* failure (400/403/404/422) to the one message that caused it: that message is
|
|
91
|
+
* acked and every other one is retried, instead of the whole batch being
|
|
92
|
+
* re-delivered because of a single poison message. A plain `ctx.run` call
|
|
93
|
+
* carries no message id, so its failure stays unattributed and the whole batch
|
|
94
|
+
* retries.
|
|
95
|
+
*/
|
|
96
|
+
interface QueueMessage<Body = unknown> extends MessageLike<Body> {
|
|
97
|
+
/** {@link QueueRunContext.run}, pinned to this message for failure attribution. */
|
|
98
|
+
readonly run: DispatchRunFunction;
|
|
99
|
+
}
|
|
100
|
+
/** The delivered batch as the push handler sees it — {@link QueueMessage}s rather than bare `Message`s. */
|
|
101
|
+
interface QueueMessageBatch<Body = unknown> extends Omit<MessageBatchLike<Body>, "messages"> {
|
|
102
|
+
readonly messages: ReadonlyArray<QueueMessage<Body>>;
|
|
103
|
+
}
|
|
120
104
|
/** Whether a declared queue is consumed by this worker (push) or polled externally (pull). */
|
|
121
105
|
type QueueConsumerMode = "pull" | "push";
|
|
122
106
|
/** The handler body run for each delivered batch (push consumers only). */
|
|
123
|
-
type QueueHandler<Body = unknown> = (context: QueueRunContext, batch:
|
|
107
|
+
type QueueHandler<Body = unknown> = (context: QueueRunContext, batch: QueueMessageBatch<Body>) => Promise<void> | void;
|
|
124
108
|
/** Push-consumer batch/retry tuning, mirrored onto the wrangler `queues.consumers[]` entry. */
|
|
125
109
|
interface QueueConsumerTuning {
|
|
126
110
|
/** Name of the dead-letter queue messages land in after `maxRetries`. */
|
|
@@ -137,25 +121,25 @@ interface QueueConsumerTuning {
|
|
|
137
121
|
/** The config object passed to `defineQueue`. */
|
|
138
122
|
interface QueueConfig<Body = unknown> extends QueueConsumerTuning {
|
|
139
123
|
/**
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
124
|
+
* The push-consumer body. Required for `mode: "push"` (the default); omit it
|
|
125
|
+
* for `mode: "pull"`, where an external worker polls the queue over HTTP.
|
|
126
|
+
*/
|
|
143
127
|
handler?: QueueHandler<Body>;
|
|
144
128
|
/** How this queue is consumed. Defaults to `"push"`. */
|
|
145
129
|
mode?: QueueConsumerMode;
|
|
146
130
|
/**
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
131
|
+
* Stable wrangler queue name (`queues.producers[].queue`). Defaults to the
|
|
132
|
+
* kebab-cased export name (`emailQueue` → `email-queue`).
|
|
133
|
+
*/
|
|
150
134
|
name?: string;
|
|
151
135
|
}
|
|
152
136
|
/** The branded result of `defineQueue`, discovered by codegen + config. */
|
|
153
137
|
interface QueueDefinition<Body = unknown> extends QueueConfig<Body> {
|
|
154
138
|
/**
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
139
|
+
* Phantom carrier for the message body type, so codegen can type the
|
|
140
|
+
* generated `ctx.queues.<name>` producer as `QueueProducer<Body>` from
|
|
141
|
+
* `typeof <export>`. Never assigned at runtime (type-only).
|
|
142
|
+
*/
|
|
159
143
|
readonly __lunoraBody?: Body;
|
|
160
144
|
/** Runtime brand identifying a `defineQueue` result. */
|
|
161
145
|
isLunoraQueue: true;
|
|
@@ -169,98 +153,172 @@ interface QueueBindingSpec {
|
|
|
169
153
|
/** The stable wrangler queue name, e.g. `email-queue`. */
|
|
170
154
|
name: string;
|
|
171
155
|
}
|
|
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
156
|
/** One declared queue, keyed for batch routing by its stable wrangler name. */
|
|
236
157
|
interface QueueRegistryEntry {
|
|
237
158
|
/**
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
159
|
+
* The `defineQueue` result (carries the push handler). The body type is
|
|
160
|
+
* erased to `any` here because the registry is heterogeneous — different
|
|
161
|
+
* queues carry different message bodies, and the handler param is
|
|
162
|
+
* contravariant, so a precise `QueueDefinition<Body>` would not be assignable
|
|
163
|
+
* to a shared `unknown`-bodied slot. Runtime dispatch passes the delivered
|
|
164
|
+
* batch straight through, so the erasure is type-only.
|
|
165
|
+
*/
|
|
245
166
|
definition: QueueDefinition<any>;
|
|
246
167
|
/** The `lunora/queues.ts` export name, for log correlation. */
|
|
247
168
|
exportName: string;
|
|
248
169
|
}
|
|
249
170
|
/** Map of stable wrangler queue name → registry entry, built by codegen. */
|
|
250
171
|
type QueueRegistry = Record<string, QueueRegistryEntry>;
|
|
172
|
+
/** The disposition a consumer left one message in for a single delivery attempt. */
|
|
173
|
+
type QueueMessageOutcome = "ack" | "error" | "retry";
|
|
174
|
+
/**
|
|
175
|
+
* One consumed message as captured by {@link dispatchQueueBatch} and handed to an
|
|
176
|
+
* {@link QueueCaptureSink}. Structurally matches `@lunora/do`'s
|
|
177
|
+
* `RecordQueueMessageInput` (the reserved `recordQueueMessage` admin RPC payload);
|
|
178
|
+
* the two packages share only this contract, so keep them in sync by hand.
|
|
179
|
+
*/
|
|
180
|
+
interface CapturedQueueMessage {
|
|
181
|
+
/** Delivery attempt number for this message (`message.attempts`). */
|
|
182
|
+
attempts: number;
|
|
183
|
+
/** The message body (JSON-encoded + capped by the catcher). */
|
|
184
|
+
body: unknown;
|
|
185
|
+
/** `true` when this failed delivery was the message's last (its retries are exhausted — the broker dead-letters it). */
|
|
186
|
+
deadLettered: boolean;
|
|
187
|
+
/** Handler error message when `outcome` is `error`; absent otherwise. */
|
|
188
|
+
error?: string;
|
|
189
|
+
/** The `lunora/queues.ts` export name that consumed it. */
|
|
190
|
+
exportName: string;
|
|
191
|
+
/** The delivered message id (`message.id`). */
|
|
192
|
+
messageId: string;
|
|
193
|
+
/** How the handler disposed of the message this attempt. */
|
|
194
|
+
outcome: QueueMessageOutcome;
|
|
195
|
+
/** The stable wrangler queue name the batch was delivered from (`batch.queue`). */
|
|
196
|
+
queue: string;
|
|
197
|
+
/** Original message timestamp in epoch-ms (`message.timestamp`). */
|
|
198
|
+
timestamp: number;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Persists a batch of consumed messages. The codegen worker wires this to POST the
|
|
202
|
+
* batch to the root shard's `recordQueueMessage` admin RPC (the dev queue catcher).
|
|
203
|
+
* Best-effort by contract: {@link dispatchQueueBatch} swallows a rejection so a
|
|
204
|
+
* capture failure never changes delivery semantics.
|
|
205
|
+
*/
|
|
206
|
+
type QueueCaptureSink = (messages: CapturedQueueMessage[]) => Promise<void> | void;
|
|
251
207
|
interface DispatchOptions {
|
|
208
|
+
/**
|
|
209
|
+
* Optional capture sink. When set, every message's final disposition is
|
|
210
|
+
* turned into a record and handed to this sink after the handler runs.
|
|
211
|
+
* Omitted in production unless queue capture is enabled, so a consumer pays
|
|
212
|
+
* no record-building or sink cost by default. Delivery semantics — including
|
|
213
|
+
* poison-message isolation — do not depend on it.
|
|
214
|
+
*/
|
|
215
|
+
capture?: QueueCaptureSink;
|
|
252
216
|
/** Worker `env`, forwarded to the queue run context. */
|
|
253
217
|
env: Record<string, unknown>;
|
|
254
218
|
/** Injectable fetch for the `ctx.run` dispatcher (tests). */
|
|
255
219
|
fetchImpl?: typeof fetch;
|
|
256
220
|
}
|
|
257
221
|
/**
|
|
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
|
-
*/
|
|
222
|
+
* Look up the handler for `batch.queue` and invoke it with a fresh
|
|
223
|
+
* `QueueRunContext`. Throws a directed error when no push handler is registered
|
|
224
|
+
* for the delivered queue (a misconfiguration — the consumer was declared
|
|
225
|
+
* `pull`, or the queue name drifted from the `defineQueue` export).
|
|
226
|
+
*/
|
|
263
227
|
declare const dispatchQueueBatch: (batch: MessageBatchLike, registry: QueueRegistry, options: DispatchOptions) => Promise<void>;
|
|
228
|
+
/** A Worker `env` projected as a plain record (vars, secrets, and bindings are `unknown`-valued). */
|
|
229
|
+
type QueueEnv = Record<string, unknown>;
|
|
230
|
+
/** Options for {@link createQueueCaptureSink}. */
|
|
231
|
+
interface QueueCaptureOptions {
|
|
232
|
+
/** Pin the consumed-message log to a Cloudflare data-residency jurisdiction (match the worker's `jurisdiction`). */
|
|
233
|
+
jurisdiction?: string;
|
|
234
|
+
/** Shard the consumed-message log lives on; override if the worker sets a custom `defaultShardKey`. */
|
|
235
|
+
rootShard?: string;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Whether consumed queue messages should be captured into the studio's log.
|
|
239
|
+
* Explicit `LUNORA_QUEUE_CAPTURE` (`"1"`/`"true"` vs `"0"`/`"false"`) always wins;
|
|
240
|
+
* unset, capture is on only in a development environment. Mirrors
|
|
241
|
+
* `@lunora/mail`'s `shouldCaptureMail` so mail and queue dev capture toggle the
|
|
242
|
+
* same way.
|
|
243
|
+
*/
|
|
244
|
+
declare const shouldCaptureQueue: (env: QueueEnv) => boolean;
|
|
245
|
+
/**
|
|
246
|
+
* Build the {@link QueueCaptureSink} that records a processed batch into the
|
|
247
|
+
* studio's root-shard consumed-message log via the reserved `recordQueueMessage`
|
|
248
|
+
* admin RPC. Best-effort by contract: without the `SHARD` binding or
|
|
249
|
+
* `LUNORA_ADMIN_TOKEN` it no-ops, and `dispatchQueueBatch` swallows a
|
|
250
|
+
* rejection, so capture never changes delivery semantics.
|
|
251
|
+
*/
|
|
252
|
+
declare const createQueueCaptureSink: (env: QueueEnv, options?: QueueCaptureOptions) => QueueCaptureSink;
|
|
253
|
+
/**
|
|
254
|
+
* Build the `ctx.queues` map for a request: resolve every spec's `env[binding]`
|
|
255
|
+
* into the `exportName → Queue binding` map and wrap it in {@link createQueues}.
|
|
256
|
+
* A spec whose binding is absent from `env` is skipped here — the helpful "no
|
|
257
|
+
* queue named …" error is raised lazily by `ctx.queues.<name>.send(...)` when
|
|
258
|
+
* the missing queue is actually used.
|
|
259
|
+
*/
|
|
260
|
+
declare const createQueueContext: (env: Record<string, unknown>, specs: ReadonlyArray<QueueBindingSpec>) => Queues;
|
|
261
|
+
/**
|
|
262
|
+
* Build the `ctx.queues` map from `lunora/queues.ts` export name → Cloudflare
|
|
263
|
+
* `Queue` binding. Each property is a typed {@link QueueProducer}; accessing an
|
|
264
|
+
* export whose binding is absent throws a directed error naming the declared
|
|
265
|
+
* queues (raised lazily on first use).
|
|
266
|
+
*/
|
|
267
|
+
declare const createQueues: (options: LunoraQueuesOptions) => Queues;
|
|
268
|
+
/**
|
|
269
|
+
* The wrangler producer binding name for a queue export: `emailQueue` →
|
|
270
|
+
* `QUEUE_EMAIL_QUEUE`, `email` → `QUEUE_EMAIL`. The `QUEUE_` prefix namespaces
|
|
271
|
+
* these away from `SHARD`/`SESSION`/`SCHEDULER`/`WORKFLOW_*`/`CONTAINER_*` so a
|
|
272
|
+
* queue export can never collide with the built-in bindings.
|
|
273
|
+
*/
|
|
274
|
+
declare const queueBindingName: (exportName: string) => string;
|
|
275
|
+
/**
|
|
276
|
+
* The stable queue name wrangler registers (`queues.producers[].queue` and
|
|
277
|
+
* `queues.consumers[].queue`): `emailQueue` → `email-queue`. Used as the
|
|
278
|
+
* deployed queue's identifier when no explicit `name` override is given.
|
|
279
|
+
*/
|
|
280
|
+
declare const queueDefaultName: (exportName: string) => string;
|
|
281
|
+
/**
|
|
282
|
+
* Declare a Cloudflare Queue deployed alongside the app. Pure validation +
|
|
283
|
+
* branding: codegen discovers the export, emits the typed `ctx.queues.<name>`
|
|
284
|
+
* producer and (for push consumers) the worker `queue()` dispatch; the config
|
|
285
|
+
* layer reconciles the wrangler `queues.producers[]` / `queues.consumers[]`
|
|
286
|
+
* entries from the same definition.
|
|
287
|
+
*
|
|
288
|
+
* ```ts
|
|
289
|
+
* // lunora/queues.ts
|
|
290
|
+
* import { defineQueue } from "@lunora/queue";
|
|
291
|
+
* import { api } from "./_generated/api";
|
|
292
|
+
*
|
|
293
|
+
* export const emailQueue = defineQueue<{ to: string }>({
|
|
294
|
+
* handler: async (ctx, batch) => {
|
|
295
|
+
* for (const message of batch.messages) {
|
|
296
|
+
* await message.run(api.email.send, { to: message.body.to });
|
|
297
|
+
* message.ack();
|
|
298
|
+
* }
|
|
299
|
+
* },
|
|
300
|
+
* });
|
|
301
|
+
* ```
|
|
302
|
+
*
|
|
303
|
+
* Enqueue from a mutation or action: `await ctx.queues.emailQueue.send({ to })`.
|
|
304
|
+
*
|
|
305
|
+
* `message.run(...)` is `ctx.run(...)` pinned to that message — prefer it inside
|
|
306
|
+
* the batch loop. A deterministic failure (400/403/404/422) from a pinned call
|
|
307
|
+
* is attributed to its message: that one is acked and the rest are retried,
|
|
308
|
+
* instead of one poison message re-delivering (and eventually dead-lettering)
|
|
309
|
+
* the whole batch.
|
|
310
|
+
*
|
|
311
|
+
* ⚠️ **Privileged dispatch.** A push handler's `ctx.run(...)` calls back into
|
|
312
|
+
* Lunora functions over the admin-authenticated dispatch endpoint (the same
|
|
313
|
+
* trusted path the scheduler and workflows use), so those calls run with the
|
|
314
|
+
* system identity — **end-user RLS is not applied**. Treat a queue handler as
|
|
315
|
+
* trusted server code: validate `message.body` (it may be attacker-influenced if
|
|
316
|
+
* anything user-facing can enqueue) before acting on it, and don't forward an
|
|
317
|
+
* unchecked body straight into a privileged mutation.
|
|
318
|
+
*/
|
|
319
|
+
declare const defineQueue: <Body = unknown>(config: QueueConfig<Body>) => QueueDefinition<Body>;
|
|
320
|
+
/** True when a value is a `defineQueue` result (the runtime brand check). */
|
|
321
|
+
declare const isQueueDefinition: (value: unknown) => value is QueueDefinition;
|
|
264
322
|
interface RunContextOptions {
|
|
265
323
|
env: Record<string, unknown>;
|
|
266
324
|
exportName: string;
|
|
@@ -268,4 +326,4 @@ interface RunContextOptions {
|
|
|
268
326
|
}
|
|
269
327
|
/** Assemble the {@link QueueRunContext} passed to a `defineQueue` handler. */
|
|
270
328
|
declare const createQueueRunContext: (options: RunContextOptions) => QueueRunContext;
|
|
271
|
-
export { type ArgsOf, type
|
|
329
|
+
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, createQueueCaptureSink, createQueueContext, createQueueRunContext, createQueues, defineQueue, dispatchQueueBatch, isQueueDefinition, queueBindingName, queueDefaultName, shouldCaptureQueue };
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { default as createQueues } from './packem_shared/createQueues-14-vSICK.mjs';
|
|
3
|
-
export { defineQueue, isQueueDefinition, queueBindingName, queueDefaultName } from './packem_shared/defineQueue-D40gREfg.mjs';
|
|
4
|
-
export { dispatchQueueBatch } from './packem_shared/dispatchQueueBatch-DR15pYS7.mjs';
|
|
5
|
-
export { createQueueRunContext } from './packem_shared/createQueueRunContext-_2hD-TK7.mjs';
|
|
1
|
+
import{createQueueCaptureSink as t,shouldCaptureQueue as r}from"./packem_shared/createQueueCaptureSink-D8pUrrb_.mjs";import{createQueueContext as a}from"./packem_shared/createQueueContext-B9PgIzBM.mjs";import{default as i}from"./packem_shared/createQueues-FpKDoLFX.mjs";import{defineQueue as p,isQueueDefinition as m,queueBindingName as x,queueDefaultName as Q}from"./packem_shared/defineQueue-TY4-i3nG.mjs";import{dispatchQueueBatch as s}from"./packem_shared/dispatchQueueBatch-dejEVSC6.mjs";import{c as C}from"./packem_shared/run-context-C97v6VyP.mjs";export{t as createQueueCaptureSink,a as createQueueContext,C as createQueueRunContext,i as createQueues,p as defineQueue,s as dispatchQueueBatch,m as isQueueDefinition,x as queueBindingName,Q as queueDefaultName,r as shouldCaptureQueue};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{LunoraError as _}from"@lunora/errors";const l="__lunora_admin__:recordQueueMessage",N="__root__",p=/^(?:dev(?:elopment)?|local(?:host)?|test)$/iu,T=["CF_ENV","ENVIRONMENT","NODE_ENV","WORKER_ENV"],f=5e3,O=o=>{const t=o.LUNORA_QUEUE_CAPTURE;return typeof t=="string"?t==="1"||t.toLowerCase()==="true":T.some(i=>{const e=o[i];return typeof e=="string"&&p.test(e)})},R=(o,t={})=>{const i=t.rootShard??N;return async e=>{if(e.length===0)return;const n=o.SHARD,s=typeof o.LUNORA_ADMIN_TOKEN=="string"?o.LUNORA_ADMIN_TOKEN:void 0;if(n===void 0||s===void 0)return;let a=n;if(t.jurisdiction!==void 0){if(typeof n.jurisdiction!="function")throw new TypeError(`@lunora/queue: Durable Object namespace does not support jurisdiction("${t.jurisdiction}") — update @cloudflare/workers-types or remove the jurisdiction option`);a=n.jurisdiction(t.jurisdiction)}const d=a.get(a.idFromName(i)),u=new AbortController,E=setTimeout(()=>{u.abort()},f);try{const r=await d.fetch("https://shard.internal/rpc",{body:JSON.stringify({args:{messages:e},functionPath:l}),headers:{authorization:`Bearer ${s}`,"content-type":"application/json"},method:"POST",signal:u.signal});if(!r.ok){const c=await r.text().catch(()=>"");throw new _("INTERNAL",`@lunora/queue: capture write to the root shard failed (${String(r.status)} ${r.statusText})${c===""?"":`: ${c}`}`)}await r.body?.cancel()}finally{clearTimeout(E)}}};export{R as createQueueCaptureSink,O as shouldCaptureQueue};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import i from"./createQueues-FpKDoLFX.mjs";const s=(o,c)=>{const n={};for(const t of c){const e=o[t.binding];e&&typeof e.send=="function"&&typeof e.sendBatch=="function"&&(n[t.exportName]=e)}return i({bindings:n})};export{s as createQueueContext};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{c as o}from"./run-context-C97v6VyP.mjs";export{o as createQueueRunContext};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{LunoraError as i}from"@lunora/errors";const u=100,d=o=>({send:async(r,n)=>{await o.send(r,n)},sendBatch:async(r,n)=>{const t=[...r];if(t.length>u)throw new i("VALIDATION_ERROR",`@lunora/queue: sendBatch exceeds ${String(u)} (got ${String(t.length)}) — split across calls`);await o.sendBatch(t,n)}}),g=o=>{const r=o.bindings??{},n=Object.create(null);for(const[s,e]of Object.entries(r))n[s]=d(e);const t=Object.keys(n),a=s=>{const e=t.length===0?"no queues are declared":`known queues: ${t.join(", ")}`,c=()=>Promise.reject(new Error(`@lunora/queue: no queue named "${s}" (${e})`));return{send:c,sendBatch:c}};return new Proxy(n,{get(s,e){if(typeof e=="string")return Object.hasOwn(s,e)?s[e]:a(e)}})};export{g as default};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const n=e=>`QUEUE_${e.replaceAll(/(?<=[a-z0-9])(?=[A-Z])/g,"_").toUpperCase()}`,t=e=>e.replaceAll(/(?<=[a-z0-9])(?=[A-Z])/g,"-").toLowerCase(),o=e=>{const u=e.mode??"push";if(u!=="push"&&u!=="pull")throw new TypeError(`defineQueue: \`mode\` must be "push" or "pull" (got ${JSON.stringify(e.mode)})`);if(u==="push"&&typeof e.handler!="function")throw new TypeError('defineQueue: `handler` must be a function for a push consumer (omit it only when `mode: "pull"`)');if(e.name!==void 0&&(typeof e.name!="string"||e.name.length===0))throw new TypeError("defineQueue: `name` must be a non-empty string when provided");return{...e,isLunoraQueue:!0,mode:u}},r=e=>typeof e=="object"&&e!==null&&e.isLunoraQueue===!0;export{o as defineQueue,r as isQueueDefinition,n as queueBindingName,t as queueDefaultName};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{c as l,k as w,M as h}from"./run-context-C97v6VyP.mjs";import{LunoraError as y}from"@lunora/errors";const v=3,k=e=>{if(e instanceof Date)return e.getTime();const n=typeof e=="number"?e:Number(e);return Number.isFinite(n)?n:0},x=(e,n)=>(t,r,o)=>e(t,r,{...o,messageId:n}),b=(e,n,t)=>{const r=x(t,e.id);return new Proxy(e,{get:(o,i)=>i==="ack"?()=>{n.set(o,"ack"),o.ack()}:i==="retry"?u=>{n.set(o,"retry"),o.retry(u)}:i==="run"?r:Reflect.get(o,i,o)})},q=(e,n,t)=>new Proxy(e,{get:(r,o)=>o==="ackAll"?()=>{t("ack"),r.ackAll()}:o==="retryAll"?i=>{t("retry"),r.retryAll(i)}:o==="messages"?n:Reflect.get(r,o,r)}),R=(e,n)=>{const t=new Map,r=e.messages,o=r.map(c=>b(c,t,n)),u=q(e,o,c=>{for(const a of r)t.has(a)||t.set(a,c)});return{dispositions:t,originals:r,wrappedBatch:u}},N=e=>{if(e instanceof Error)return e.message;if(typeof e=="string")return e;if(e!==null&&typeof e=="object")try{return JSON.stringify(e)}catch{return"[unserializable thrown value]"}return String(e)},A=(e,n,t,r,o,i)=>{const u=r?N(o):void 0,c=typeof n.definition.maxRetries=="number"?n.definition.maxRetries:v,a=r?"error":"ack";return e.originals.map(s=>{const p=s===i,d=e.dispositions.get(s),f=p?"error":d??a,m=typeof s.attempts=="number"?s.attempts:1;return{attempts:m,body:s.body,deadLettered:!p&&f!=="ack"&&m>c,error:f==="error"?u:void 0,exportName:n.exportName,messageId:s.id,outcome:f,queue:t,timestamp:k(s.timestamp)}})},g=(e,n,t)=>{if(!n||!w(t))return;const r=h(t);if(r===void 0)return;const o=e.originals.find(i=>i.id===r);if(!(o===void 0||e.dispositions.has(o)))return o},M=(e,n)=>{n.ack();for(const t of e.originals)t!==n&&!e.dispositions.has(t)&&(e.dispositions.set(t,"retry"),t.retry())},I=async(e,n,t)=>{const r=Object.hasOwn(n,e.queue)?n[e.queue]:void 0;if(r===void 0){const d=Object.keys(n),f=d.length===0?"no push queues are declared":`known push queues: ${d.join(", ")}`;throw new y("INTERNAL",`@lunora/queue: received a batch for queue "${e.queue}" but no push handler is registered (${f})`)}const{handler:o}=r.definition;if(typeof o!="function")throw new TypeError(`@lunora/queue: queue "${e.queue}" (${r.exportName}) has no push handler — it is declared as a pull consumer`);const i=l({env:t.env,exportName:r.exportName,fetchImpl:t.fetchImpl}),u=R(e,i.run);let c=!1,a;try{await o(i,u.wrappedBatch)}catch(d){c=!0,a=d}const s=g(u,c,a);s!==void 0&&M(u,s);const p=c&&s===void 0;if(t.capture!==void 0)try{await t.capture(A(u,r,e.queue,c,a,s))}catch(d){console.warn("@lunora/queue: capture sink failed (delivery unaffected):",d)}if(p)throw a};export{I as dispatchQueueBatch};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{LunoraError as c,isLunoraError as N}from"@lunora/errors";const A=t=>({debug:(e,...n)=>{console.debug(t,e,...n)},error:(e,...n)=>{console.error(t,e,...n)},info:(e,...n)=>{console.info(t,e,...n)},warn:(e,...n)=>{console.warn(t,e,...n)}}),L=t=>{let e="";for(let n=0;n<t.length;n+=32768)e+=String.fromCharCode(...t.subarray(n,n+32768));return btoa(e)},b=t=>L(t).replaceAll("+","-").replaceAll("/","_").replace(/=+$/,"");new TextDecoder;const w=new TextEncoder,g="=",S=t=>{for(let e=0;e<t.length;e+=1)if(t.charCodeAt(e)>255)return!1;return!0},T=t=>b(w.encode(JSON.stringify(t))),_=t=>!t.startsWith(g)&&S(t)?t:`${g}${b(w.encode(t))}`,E="/_lunora/scheduler/dispatch",x=3e4,U=t=>{let e=t.length;for(;e>0&&t[e-1]==="/";)e-=1;return t.slice(0,e)},I=Symbol("lunoraDispatchFailure"),$=Symbol("lunoraDispatchMessageId"),m=(t,e)=>(Object.defineProperty(t,I,{value:!0}),e!==void 0&&Object.defineProperty(t,$,{value:e}),t),j=t=>N(t)?t[$]:void 0,D=(t,e,n,i)=>{try{const o=JSON.parse(n)?.error;if(typeof o=="object"&&o!==null&&typeof o.code=="string"){const{code:h,data:u,message:s}=o;return m(new c(h,typeof s=="string"?s:void 0,{data:u,status:e}),i)}}catch{}return m(new c("INTERNAL",`${t}: function dispatch failed (${String(e)}): ${n}`,{status:e}),i)},k=new Set([400,403,404,422]),K=t=>N(t)&&t[I]===!0&&k.has(t.status),C=(t,e,n)=>new c("INTERNAL",`${t}: function dispatch to "${e}" timed out after ${String(n)}ms`,{status:503}),J=t=>{const{label:e}=t,n=globalThis.fetch,i=t.fetchImpl??(typeof n=="function"?n.bind(globalThis):void 0);return async(o,h,u={})=>{if(typeof i!="function")throw new TypeError(`${e}: no fetch implementation available — pass fetchImpl or run on a platform with global fetch`);const s=t.env.LUNORA_ORIGIN_URL;if(typeof s!="string"||s.length===0)throw new c("INTERNAL",`${e}: \`LUNORA_ORIGIN_URL\` must be set on the Worker env so a handler can call back into Lunora functions`);const f=t.env.LUNORA_ADMIN_TOKEN;if(typeof f!="string"||f.length===0)throw new c("INTERNAL",`${e}: \`LUNORA_ADMIN_TOKEN\` must be set on the Worker env to authenticate function dispatch`);const v=`${U(s)}${E}`,d={authorization:`Bearer ${f}`,"content-type":"application/json"};t.identity?.userId!==void 0&&(d["x-lunora-userid"]=_(t.identity.userId)),t.identity?.claims!==void 0&&(d["x-lunora-identity"]=T(t.identity.claims));const y=u.timeoutMs??x,O=AbortSignal.timeout(y),p=r=>{throw r instanceof Error&&r.name==="TimeoutError"?C(e,o.__lunoraRef,y):r};let a;try{a=await i(v,{body:JSON.stringify({args:h??{},functionPath:o.__lunoraRef,shardKey:u.shardKey}),headers:d,method:"POST",signal:O})}catch(r){return p(r)}if(!a.ok){let r;try{r=await a.text()}catch(R){return p(R)}throw D(e,a.status,r,u.messageId)}let l;try{l=await a.text()}catch(r){return p(r)}if(l.length!==0)try{return JSON.parse(l)}catch{throw new c("INTERNAL",`${e}: function dispatch returned a non-JSON body (${String(a.status)}): ${l}`,{status:a.status})}}},P=t=>({env:t.env,log:A(`[queue:${t.exportName}]`),run:J({env:t.env,fetchImpl:t.fetchImpl,label:"@lunora/queue"})});export{j as M,P as c,K as k};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunora/queue",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.31",
|
|
4
4
|
"description": "Cloudflare Queues for Lunora: defineQueue producers + consumers, the ctx.queues surface, and the generated queue() worker handler",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"background-jobs",
|
|
@@ -44,7 +44,8 @@
|
|
|
44
44
|
"access": "public"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@lunora/errors": "1.0.0-alpha.
|
|
47
|
+
"@lunora/errors": "1.0.0-alpha.22",
|
|
48
|
+
"@lunora/platform": "1.0.0-alpha.15"
|
|
48
49
|
},
|
|
49
50
|
"engines": {
|
|
50
51
|
"node": "^22.15.0 || >=24.11.0"
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import createQueues from './createQueues-14-vSICK.mjs';
|
|
2
|
-
|
|
3
|
-
const createQueueContext = (env, specs) => {
|
|
4
|
-
const bindings = {};
|
|
5
|
-
for (const spec of specs) {
|
|
6
|
-
const binding = env[spec.binding];
|
|
7
|
-
if (binding && typeof binding.send === "function" && typeof binding.sendBatch === "function") {
|
|
8
|
-
bindings[spec.exportName] = binding;
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
return createQueues({ bindings });
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export { createQueueContext };
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { LunoraError } from '@lunora/errors';
|
|
2
|
-
|
|
3
|
-
const createDispatchLogger = (prefix) => {
|
|
4
|
-
return {
|
|
5
|
-
debug: (message, ...rest) => {
|
|
6
|
-
console.debug(prefix, message, ...rest);
|
|
7
|
-
},
|
|
8
|
-
error: (message, ...rest) => {
|
|
9
|
-
console.error(prefix, message, ...rest);
|
|
10
|
-
},
|
|
11
|
-
info: (message, ...rest) => {
|
|
12
|
-
console.info(prefix, message, ...rest);
|
|
13
|
-
},
|
|
14
|
-
warn: (message, ...rest) => {
|
|
15
|
-
console.warn(prefix, message, ...rest);
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const SCHEDULER_DISPATCH_PATH = "/_lunora/scheduler/dispatch";
|
|
21
|
-
const trimTrailingSlashes = (value) => {
|
|
22
|
-
let end = value.length;
|
|
23
|
-
while (end > 0 && value[end - 1] === "/") {
|
|
24
|
-
end -= 1;
|
|
25
|
-
}
|
|
26
|
-
return value.slice(0, end);
|
|
27
|
-
};
|
|
28
|
-
const createDispatchRunner = (options) => {
|
|
29
|
-
const { label } = options;
|
|
30
|
-
const globalFetch = globalThis.fetch;
|
|
31
|
-
const fetchImpl = options.fetchImpl ?? (typeof globalFetch === "function" ? globalFetch.bind(globalThis) : void 0);
|
|
32
|
-
return async (function_, args, runOptions = {}) => {
|
|
33
|
-
if (typeof fetchImpl !== "function") {
|
|
34
|
-
throw new TypeError(`${label}: no fetch implementation available — pass fetchImpl or run on a platform with global fetch`);
|
|
35
|
-
}
|
|
36
|
-
const origin = options.env.LUNORA_ORIGIN_URL;
|
|
37
|
-
if (typeof origin !== "string" || origin.length === 0) {
|
|
38
|
-
throw new LunoraError("INTERNAL", `${label}: \`LUNORA_ORIGIN_URL\` must be set on the Worker env so a handler can call back into Lunora functions`);
|
|
39
|
-
}
|
|
40
|
-
const token = options.env.LUNORA_ADMIN_TOKEN;
|
|
41
|
-
if (typeof token !== "string" || token.length === 0) {
|
|
42
|
-
throw new LunoraError("INTERNAL", `${label}: \`LUNORA_ADMIN_TOKEN\` must be set on the Worker env to authenticate function dispatch`);
|
|
43
|
-
}
|
|
44
|
-
const url = `${trimTrailingSlashes(origin)}${SCHEDULER_DISPATCH_PATH}`;
|
|
45
|
-
const response = await fetchImpl(url, {
|
|
46
|
-
body: JSON.stringify({ args: args ?? {}, functionPath: function_.__lunoraRef, shardKey: runOptions.shardKey }),
|
|
47
|
-
headers: { authorization: `Bearer ${token}`, "content-type": "application/json" },
|
|
48
|
-
method: "POST"
|
|
49
|
-
});
|
|
50
|
-
if (!response.ok) {
|
|
51
|
-
throw new LunoraError("INTERNAL", `${label}: function dispatch failed (${String(response.status)}): ${await response.text()}`);
|
|
52
|
-
}
|
|
53
|
-
const text = await response.text();
|
|
54
|
-
if (text.length === 0) {
|
|
55
|
-
return void 0;
|
|
56
|
-
}
|
|
57
|
-
try {
|
|
58
|
-
return JSON.parse(text);
|
|
59
|
-
} catch {
|
|
60
|
-
return text;
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
const createQueueRunContext = (options) => {
|
|
66
|
-
return {
|
|
67
|
-
env: options.env,
|
|
68
|
-
log: createDispatchLogger(`[queue:${options.exportName}]`),
|
|
69
|
-
run: createDispatchRunner({ env: options.env, fetchImpl: options.fetchImpl, label: "@lunora/queue" })
|
|
70
|
-
};
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
export { createQueueRunContext };
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
const producerFor = (binding) => {
|
|
2
|
-
return {
|
|
3
|
-
send: async (body, options) => {
|
|
4
|
-
await binding.send(body, options);
|
|
5
|
-
},
|
|
6
|
-
sendBatch: async (messages, options) => {
|
|
7
|
-
await binding.sendBatch(messages, options);
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
};
|
|
11
|
-
const createQueues = (options) => {
|
|
12
|
-
const bindings = options.bindings ?? {};
|
|
13
|
-
const producers = /* @__PURE__ */ Object.create(null);
|
|
14
|
-
for (const [exportName, binding] of Object.entries(bindings)) {
|
|
15
|
-
producers[exportName] = producerFor(binding);
|
|
16
|
-
}
|
|
17
|
-
const known = Object.keys(producers);
|
|
18
|
-
const missing = (name) => {
|
|
19
|
-
const suffix = known.length === 0 ? "no queues are declared" : `known queues: ${known.join(", ")}`;
|
|
20
|
-
const error = () => Promise.reject(new Error(`@lunora/queue: no queue named "${name}" (${suffix})`));
|
|
21
|
-
return { send: error, sendBatch: error };
|
|
22
|
-
};
|
|
23
|
-
return /* @__PURE__ */ new Proxy(producers, {
|
|
24
|
-
get(target, property) {
|
|
25
|
-
if (typeof property !== "string") {
|
|
26
|
-
return void 0;
|
|
27
|
-
}
|
|
28
|
-
return Object.hasOwn(target, property) ? target[property] : missing(property);
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export { createQueues as default };
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
const queueBindingName = (exportName) => `QUEUE_${exportName.replaceAll(/(?<=[a-z0-9])(?=[A-Z])/g, "_").toUpperCase()}`;
|
|
2
|
-
const queueDefaultName = (exportName) => exportName.replaceAll(/(?<=[a-z0-9])(?=[A-Z])/g, "-").toLowerCase();
|
|
3
|
-
const defineQueue = (config) => {
|
|
4
|
-
const mode = config.mode ?? "push";
|
|
5
|
-
if (mode !== "push" && mode !== "pull") {
|
|
6
|
-
throw new TypeError(`defineQueue: \`mode\` must be "push" or "pull" (got ${JSON.stringify(config.mode)})`);
|
|
7
|
-
}
|
|
8
|
-
if (mode === "push" && typeof config.handler !== "function") {
|
|
9
|
-
throw new TypeError('defineQueue: `handler` must be a function for a push consumer (omit it only when `mode: "pull"`)');
|
|
10
|
-
}
|
|
11
|
-
if (config.name !== void 0 && (typeof config.name !== "string" || config.name.length === 0)) {
|
|
12
|
-
throw new TypeError("defineQueue: `name` must be a non-empty string when provided");
|
|
13
|
-
}
|
|
14
|
-
return { ...config, isLunoraQueue: true, mode };
|
|
15
|
-
};
|
|
16
|
-
const isQueueDefinition = (value) => typeof value === "object" && value !== null && value.isLunoraQueue === true;
|
|
17
|
-
|
|
18
|
-
export { defineQueue, isQueueDefinition, queueBindingName, queueDefaultName };
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { LunoraError } from '@lunora/errors';
|
|
2
|
-
import { createQueueRunContext } from './createQueueRunContext-_2hD-TK7.mjs';
|
|
3
|
-
|
|
4
|
-
const dispatchQueueBatch = async (batch, registry, options) => {
|
|
5
|
-
const entry = registry[batch.queue];
|
|
6
|
-
if (entry === void 0) {
|
|
7
|
-
const known = Object.keys(registry);
|
|
8
|
-
const suffix = known.length === 0 ? "no push queues are declared" : `known push queues: ${known.join(", ")}`;
|
|
9
|
-
throw new LunoraError("INTERNAL", `@lunora/queue: received a batch for queue "${batch.queue}" but no push handler is registered (${suffix})`);
|
|
10
|
-
}
|
|
11
|
-
const { handler } = entry.definition;
|
|
12
|
-
if (typeof handler !== "function") {
|
|
13
|
-
throw new TypeError(`@lunora/queue: queue "${batch.queue}" (${entry.exportName}) has no push handler — it is declared as a pull consumer`);
|
|
14
|
-
}
|
|
15
|
-
const context = createQueueRunContext({ env: options.env, exportName: entry.exportName, fetchImpl: options.fetchImpl });
|
|
16
|
-
await handler(context, batch);
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export { dispatchQueueBatch };
|