@vite-hub/queue 0.0.4 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +170 -31
- package/dist/client-Cj50hQYp.js +180 -0
- package/dist/cloudflare-client-DYsAjM8B.js +140 -0
- package/dist/{cloudflare-resource-name-R8zYQiZb.js → cloudflare-resource-name-BJHYRCMQ.js} +3 -2
- package/dist/config-BcTDeQIP.js +105 -0
- package/dist/durability.d.ts +13 -0
- package/dist/durability.js +47 -0
- package/dist/error-diagnostics-BRartC1z.js +181 -0
- package/dist/error-diagnostics-DSkCJC_L.js +266 -0
- package/dist/errors-DL3pcNO4.js +77 -0
- package/dist/handler-response-By0Fpspv.js +61 -0
- package/dist/index.d.ts +29 -83
- package/dist/index.js +24 -11
- package/dist/inspect-PeuckaC3.js +30 -0
- package/dist/internal/runtime/cloudflare-client.d.ts +3 -5
- package/dist/internal/runtime/cloudflare-client.js +1 -1
- package/dist/internal/runtime/cloudflare-vite.d.ts +30 -2
- package/dist/internal/runtime/cloudflare-vite.js +111 -1
- package/dist/internal/runtime/local-client.d.ts +17 -0
- package/dist/internal/runtime/local-client.js +71 -0
- package/dist/internal/runtime/postgres-client.d.ts +46 -0
- package/dist/internal/runtime/postgres-client.js +560 -0
- package/dist/internal/runtime/state.d.ts +23 -15
- package/dist/internal/runtime/state.js +2 -2
- package/dist/internal/runtime/vercel-client.d.ts +3 -5
- package/dist/internal/runtime/vercel-client.js +120 -63
- package/dist/internal/runtime/worker-client.d.ts +6 -0
- package/dist/internal/runtime/worker-client.js +11 -0
- package/dist/invariant-D8_xBqmZ.js +22 -0
- package/dist/local-CGckW5nr.js +540 -0
- package/dist/local-CVVRVWeR.d.ts +22 -0
- package/dist/object-D8pRdprI.js +15 -0
- package/dist/operation-definition-8gOe-ubD.d.ts +234 -0
- package/dist/operations-Crwjca91.js +124 -0
- package/dist/operations.d.ts +69 -0
- package/dist/operations.js +1344 -0
- package/dist/runtime/hosted.d.ts +5 -9
- package/dist/runtime/hosted.js +21 -23
- package/dist/state-mVA2X_wa.js +196 -0
- package/dist/types-BD-8U9zS.d.ts +420 -0
- package/dist/types-DS_rqu9l.d.ts +10 -0
- package/dist/vercel-region-D1CivgdA.js +26 -0
- package/dist/virtual.d.ts +4 -0
- package/dist/vite.d.ts +655 -40
- package/dist/vite.js +2071 -2
- package/package.json +66 -30
- package/dist/_app-D29AwUdD.d.ts +0 -11
- package/dist/app-BZpq2cwC.js +0 -10
- package/dist/client-DG9fweET.js +0 -93
- package/dist/cloudflare-client-w5vphoNn.js +0 -99
- package/dist/cloudflare-vite-BlQfDVZD.js +0 -97
- package/dist/cloudflare-vite-nFPor46-.d.ts +0 -24
- package/dist/config-CNgAeIND.js +0 -53
- package/dist/delivery-error-glgYWC9g.js +0 -45
- package/dist/errors-CjuG_qr-.js +0 -108
- package/dist/internal/runtime/vercel-vite.d.ts +0 -20
- package/dist/internal/runtime/vercel-vite.js +0 -49
- package/dist/nuxt.d.ts +0 -19
- package/dist/nuxt.js +0 -30
- package/dist/state-Cno9O7YS.js +0 -110
- package/dist/types-CwvoMATy.d.ts +0 -169
- package/dist/vercel-runtime-package-copy-CEToJtCu.js +0 -116
- package/dist/vite-DOuoen0w.js +0 -4414
package/README.md
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
# @vite-hub/queue
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://npmx.dev/package/@vite-hub/queue)
|
|
4
|
+
[](https://npmx.dev/package/@vite-hub/queue)
|
|
5
|
+
[](https://github.com/vite-hub/vitehub/blob/main/LICENSE)
|
|
6
|
+
[](https://vitehub.dev/docs)
|
|
4
7
|
|
|
5
|
-
|
|
8
|
+
`@vite-hub/queue` discovers background job handlers during a Vite build and connects `sendQueue()` to Cloudflare Queues, Vercel Queues, or a Node server that runs the jobs from Postgres or from local files. In `vite dev`, every app runs its jobs in the dev server, so you can test a handler without an account. A successful enqueue means the provider accepted the job. It does not mean the handler finished.
|
|
9
|
+
|
|
10
|
+
Applications that already use the `vite-hub` framework distribution should enable Queue in `vitehub()` and import from `vite-hub/queue`. Install this owner package directly when you are building a custom composition, another framework integration, or package-level tooling. The [Queue guide](https://vitehub.dev/docs/queue) covers the framework-distribution setup.
|
|
6
11
|
|
|
7
12
|
## Install the owner package
|
|
8
13
|
|
|
@@ -11,18 +16,18 @@ pnpm add @vite-hub/queue
|
|
|
11
16
|
pnpm add -D @vite-hub/cli vite
|
|
12
17
|
```
|
|
13
18
|
|
|
14
|
-
The package requires Node
|
|
19
|
+
The package requires Node.js 22.19 or newer. Vite is an optional peer dependency and is required for definition discovery and provider output. The ViteHub CLI provides the `vitehub provision` command used by the host guides.
|
|
15
20
|
|
|
16
21
|
For Vercel Queues, also install the provider package and the ambient types used by generated functions:
|
|
17
22
|
|
|
18
23
|
```sh
|
|
19
|
-
pnpm add @vercel/queue
|
|
24
|
+
pnpm add @vercel/queue @vercel/functions
|
|
20
25
|
pnpm add -D @types/node @types/ws
|
|
21
26
|
```
|
|
22
27
|
|
|
23
28
|
## Build the first Queue Definition
|
|
24
29
|
|
|
25
|
-
Queue
|
|
30
|
+
The local provider runs Queue Definitions in a Node server without credentials. See [Run jobs on a Node server](#run-jobs-on-a-node-server). The example below selects Cloudflare explicitly, so its first result is a build that discovers a Queue Definition and emits provider configuration.
|
|
26
31
|
|
|
27
32
|
Define the handler in a discoverable file:
|
|
28
33
|
|
|
@@ -39,10 +44,12 @@ Enqueue it by its discovered name:
|
|
|
39
44
|
|
|
40
45
|
```ts
|
|
41
46
|
// src/server.ts
|
|
42
|
-
import {
|
|
47
|
+
import { sendQueue } from "@vite-hub/queue";
|
|
43
48
|
|
|
44
49
|
export async function enqueueWelcomeEmail(email: string) {
|
|
45
|
-
|
|
50
|
+
const [error, result] = await sendQueue("welcome-email", { email });
|
|
51
|
+
if (error) throw error;
|
|
52
|
+
return result;
|
|
46
53
|
}
|
|
47
54
|
|
|
48
55
|
export default function handleRequest() {
|
|
@@ -50,23 +57,17 @@ export default function handleRequest() {
|
|
|
50
57
|
}
|
|
51
58
|
```
|
|
52
59
|
|
|
53
|
-
Register the
|
|
60
|
+
Register the Vite integration with the Nitro Vite plugin. Nitro is the only host output:
|
|
54
61
|
|
|
55
62
|
```ts
|
|
56
63
|
// vite.config.ts
|
|
57
|
-
import { resolve } from "node:path";
|
|
58
|
-
|
|
59
64
|
import { hubQueue } from "@vite-hub/queue/vite";
|
|
65
|
+
import { nitro } from "nitro/vite";
|
|
60
66
|
import { defineConfig } from "vite";
|
|
61
67
|
|
|
62
68
|
export default defineConfig({
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
input: resolve(import.meta.dirname, "src/server.ts"),
|
|
66
|
-
},
|
|
67
|
-
ssr: true,
|
|
68
|
-
},
|
|
69
|
-
plugins: [hubQueue({ provider: "cloudflare" })],
|
|
69
|
+
nitro: { preset: "cloudflare-module", serverEntry: "./src/server.ts" },
|
|
70
|
+
plugins: [hubQueue({ driver: "cloudflare" }), nitro()],
|
|
70
71
|
});
|
|
71
72
|
```
|
|
72
73
|
|
|
@@ -76,26 +77,120 @@ Run a production-shaped build:
|
|
|
76
77
|
pnpm vite build
|
|
77
78
|
```
|
|
78
79
|
|
|
79
|
-
A successful build writes `.vitehub/queue/registry.mjs` with the `welcome-email` definition.
|
|
80
|
+
A successful build writes `.vitehub/queue/registry.mjs` with the `welcome-email` definition. Nitro writes the Worker and `.output/server/wrangler.json`. The Wrangler configuration contains one generated producer binding and one consumer for the Queue Definition.
|
|
80
81
|
|
|
81
|
-
That build proves discovery and generated output only. `
|
|
82
|
+
That build proves discovery and generated output only. In the built Worker, `sendQueue()` needs a deployed Cloudflare binding and an existing queue before it can return `[undefined, { status: "queued" }]`. In `vite dev`, it needs neither: see [Run jobs in vite dev](#run-jobs-in-vite-dev). Use the [Cloudflare host guide](https://vitehub.dev/docs/frameworks-hosts/cloudflare) for provisioning and deployment checks. Do not import `.vitehub` or `dist` files from application code.
|
|
82
83
|
|
|
83
|
-
## Choose
|
|
84
|
+
## Choose a driver
|
|
84
85
|
|
|
85
86
|
| Provider | Choose it when | Generated output | Enqueue options |
|
|
86
87
|
| ---------- | ----------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------- | -------------------------------------------------------------- |
|
|
87
88
|
| Cloudflare | The deployed runtime supplies request-scoped Cloudflare Queue bindings | Worker bundle and `wrangler.json` producer and consumer entries | `contentType`, `delaySeconds` |
|
|
88
|
-
| Vercel | The deployment runs Vercel Queue callbacks and has a Queue region when the installed SDK requires one |
|
|
89
|
+
| Vercel | The deployment runs Vercel Queue callbacks and has a Queue region when the installed SDK requires one | One Nitro route per Queue Definition, with a Vercel queue trigger | `delaySeconds`, `idempotencyKey`, `region`, `retentionSeconds` |
|
|
90
|
+
| Local | One Node server runs the jobs: `node .output/server/index.mjs`, Docker, or a WebContainer | A Nitro plugin that runs stored jobs in the server process | `delaySeconds`, `idempotencyKey` |
|
|
91
|
+
| Postgres | One or more Node servers share a Postgres database | A Nitro plugin that starts workers in each server process | `delaySeconds`, `idempotencyKey` |
|
|
89
92
|
|
|
90
|
-
The ViteHub framework
|
|
93
|
+
The ViteHub framework host selects Cloudflare for `host: "cloudflare"` and Vercel for `host: "vercel"`. For `host: "node"` and a Nitro `node-server` preset, it selects the postgres driver when the app has a Postgres Default Database, else the local driver. `vitehub({ queue: { driver: "postgres" } })` or `{ driver: "local" }` picks one on Node. Set `driver` explicitly when host inference would be ambiguous. Netlify cannot infer one. ViteHub provides no Queue driver for Deno. Check the [runtime and host support matrix](https://vitehub.dev/docs/frameworks-hosts/support-matrix) before choosing a deployment target.
|
|
91
94
|
|
|
92
95
|
Cloudflare needs a concrete request-scoped binding at runtime. Its generated queue resources can be created through the ViteHub Cloudflare provision step when the required account credentials are present. Cloudflare rejects Vercel-only options such as `idempotencyKey`, `region`, and `retentionSeconds`.
|
|
93
96
|
|
|
94
97
|
Vercel needs a generated topic and the `@vercel/queue` runtime. It rejects Cloudflare's `contentType` option. The [Vercel host guide](https://vitehub.dev/docs/frameworks-hosts/vercel) explains the generated Build Output boundary.
|
|
95
98
|
|
|
99
|
+
## Run jobs on a Node server
|
|
100
|
+
|
|
101
|
+
The local provider stores each accepted job as a file in `.vitehub/data/queue`, next to the KV `fs-lite` store, and runs the Queue Definition in the same server process. `sendQueue()` returns after the file is written.
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
// vite.config.ts
|
|
105
|
+
import { nitro } from "nitro/vite"
|
|
106
|
+
import { hubQueue } from "@vite-hub/queue/vite"
|
|
107
|
+
import { defineConfig } from "vite"
|
|
108
|
+
|
|
109
|
+
export default defineConfig({
|
|
110
|
+
nitro: { preset: "node-server", serverEntry: "./src/server.ts" },
|
|
111
|
+
plugins: [hubQueue(), nitro()],
|
|
112
|
+
})
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
`hubQueue({ driver: "local", base: "data/queue" })` selects it explicitly and changes the directory.
|
|
116
|
+
|
|
117
|
+
Direct clients from `createQueueClient()` always redact plain handler errors. They cannot share a directory with the generated dev engine, which reports error details: that fails with `QUEUE_LOCAL_MODE_MISMATCH`. Use `useQueue()` in the dev server, or give the direct client a separate `base` directory.
|
|
118
|
+
|
|
119
|
+
When a production server reopens a dev store, it redacts stored errors without a code before it exposes the jobs. Queue errors and `ViteHubError` keep their code and message.
|
|
120
|
+
|
|
121
|
+
- A failed job runs again after 1, 2, and 4 seconds. After 4 attempts, or after a Queue error that a retry cannot fix, it becomes a dead job. A dead job stays in its file with the last error. The provider keeps the 100 newest dead jobs of each Queue Definition.
|
|
122
|
+
- The server writes the attempt count before the handler runs. A job whose handler was running when the process stopped runs again after the restart. If that was the fourth attempt, the job becomes a dead job instead.
|
|
123
|
+
- Delivery is at least once. A successful handler can run again after a restart if the process stops before completion is saved, or if both deleting the job and saving its completed state fail. Make handler side effects idempotent.
|
|
124
|
+
- `delaySeconds` survives a restart. `idempotencyKey` returns the message ID of a pending or running job with the same key. A dead job does not hold its key. `sendBatch()` stores all jobs or none.
|
|
125
|
+
- The jobs survive a restart on the same disk. Another host, a new container, or an ephemeral filesystem does not have them. Run one server process per directory: there is no lock between processes, so two processes can run the same job.
|
|
126
|
+
- `onError` and `callbackOptions` are Cloudflare and Vercel options. The local provider does not call them.
|
|
127
|
+
|
|
128
|
+
The server logs the directory and this durability once when it starts. `vitehub queue list` shows the provider, the directory, `durability: "single-host"`, the dead-job limit, and the pending, running, and dead jobs of each Queue Definition. In `vite dev`, it also shows `rules`, the provider of the build. `vitehub queue jobs list` shows each job.
|
|
129
|
+
|
|
130
|
+
## Run jobs on several Node servers
|
|
131
|
+
|
|
132
|
+
The postgres driver keeps the jobs in the `vitehub_queue_jobs` table, and every server process of the app takes due jobs from it. Install `postgres` (postgres.js). The jobs use the postgres.js client of the Default Database, so they connect as the Database does, whatever variable or URL its Definition reads. `QUEUE_POSTGRES_URL` on the server overrides it. An explicit `driver: "postgres"` without a Postgres Default Database needs `QUEUE_POSTGRES_URL`.
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
// vite.config.ts
|
|
136
|
+
import { hubQueue } from "@vite-hub/queue/vite"
|
|
137
|
+
import { nitro } from "nitro/vite"
|
|
138
|
+
import { defineConfig } from "vite"
|
|
139
|
+
|
|
140
|
+
export default defineConfig({
|
|
141
|
+
nitro: { preset: "node-server", serverEntry: "./src/server.ts" },
|
|
142
|
+
plugins: [hubQueue({ driver: "postgres" }), nitro()],
|
|
143
|
+
})
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
- The first server creates the table and its indexes under `pg_advisory_xact_lock`, so servers that start at the same time do not race.
|
|
147
|
+
- Workers claim jobs with `UPDATE ... WHERE id IN (SELECT ... FOR UPDATE SKIP LOCKED)`. A send wakes the workers with `NOTIFY`. Without it, each worker checks every second.
|
|
148
|
+
- A claim holds a job for 30 seconds, and a running handler renews it. When a process stops during a handler, another instance runs the job again with the next attempt number after the claim ends.
|
|
149
|
+
- Retries, dead jobs, `delaySeconds`, `idempotencyKey`, `sendBatch()`, and `concurrency` follow the local rules above. `concurrency` applies per process.
|
|
150
|
+
- When Nitro closes, the workers stop taking jobs and wait up to 10 seconds for the running jobs.
|
|
151
|
+
- `vitehub queue list` shows the table and its database without the password, and `durability: "shared"`. `vitehub queue jobs` and the Console read the table, so any instance shows the jobs of all instances.
|
|
152
|
+
- `vite dev` runs the jobs of a postgres app with the local provider and the same rules, so development needs no Postgres.
|
|
153
|
+
|
|
154
|
+
## Run jobs in vite dev
|
|
155
|
+
|
|
156
|
+
In `vite dev` and `nuxt dev`, `sendQueue()` stores the job in `.vitehub/data/queue` of the project and the dev server runs the Queue Definition, whatever the provider of the build. A Cloudflare or Vercel app needs no binding, credentials, or `wrangler dev`. The build output does not change: `vite build` still writes the Cloudflare or Vercel runtime.
|
|
157
|
+
|
|
158
|
+
The dev server follows the documented defaults of the provider of the build, so a handler fails and retries in dev as it does on the host:
|
|
159
|
+
|
|
160
|
+
| Rule | Cloudflare | Vercel | Local |
|
|
161
|
+
| --- | --- | --- | --- |
|
|
162
|
+
| Attempts | 4 (`max_retries` 3) | No limit until the job expires | 4 |
|
|
163
|
+
| Retry delay | None | 60 seconds (`retryAfterSeconds`) | 1, 2, then 4 seconds |
|
|
164
|
+
| Largest `delaySeconds` | 86400 (24 hours) | 604800 (7 days) | No limit |
|
|
165
|
+
| Retention | None | `retentionSeconds` from 60 to 604800, default 86400 | None |
|
|
166
|
+
| Largest payload | 128 KB of JSON | 100 MB of JSON | No limit |
|
|
167
|
+
| Largest `sendBatch()` | 100 jobs, 256 KB | 100 jobs | No limit |
|
|
168
|
+
| Rejected options | `idempotencyKey`, `region`, `retentionSeconds` | `contentType` | `contentType`, `region`, `retentionSeconds` |
|
|
169
|
+
|
|
170
|
+
The sources are the [Cloudflare Queues limits](https://developers.cloudflare.com/queues/platform/limits/), [Cloudflare batching and retries](https://developers.cloudflare.com/queues/configuration/batching-retries/), the [Vercel Queues concepts](https://vercel.com/docs/queues/concepts), and the [Vercel Queues limits](https://vercel.com/docs/queues/pricing#limits). A broken limit fails `sendQueue()` with `QUEUE_PROVIDER_FAILED`, and `error.cause` names the limit. A rejected option fails with `QUEUE_UNSUPPORTED_OPTION` and names the provider of the build.
|
|
171
|
+
|
|
172
|
+
Dev does not emulate these parts of the host:
|
|
173
|
+
|
|
174
|
+
- It delivers one job at a time per Queue Definition, up to `concurrency`. It has no Cloudflare batches, `max_batch_size`, or `max_batch_timeout`.
|
|
175
|
+
- It does not call `onError` or `callbackOptions`. A failed handler retries by the rules above, and a Queue error that a retry cannot fix makes the job dead at once.
|
|
176
|
+
- It keeps a dead job for inspection. Cloudflare deletes it, or moves it to a dead-letter queue, and Vercel deletes an expired job.
|
|
177
|
+
- The payload is a JSON copy. Cloudflare `contentType: "v8"` can send values that JSON cannot, such as a `Map`. The payload size is the JSON size, not the provider encoding.
|
|
178
|
+
- `idempotencyKey` deduplicates only while a job is pending or running. Vercel deduplicates for the retention, up to 24 hours. `useQueue()` returns the local client.
|
|
179
|
+
|
|
180
|
+
List, inspect, run again, and remove the stored jobs with the CLI. The commands call the `queue.jobs-list`, `queue.jobs-show`, `queue.jobs-retry`, and `queue.jobs-clear` operations of the running dev server, or of a deployed app with `--url`. The Console and agents run the same operations.
|
|
181
|
+
|
|
182
|
+
```sh
|
|
183
|
+
pnpm vitehub queue jobs list --state dead
|
|
184
|
+
pnpm vitehub queue jobs show <message-id>
|
|
185
|
+
pnpm vitehub queue jobs retry <message-id>
|
|
186
|
+
pnpm vitehub queue jobs clear --state dead --yes
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
`--json` prints JSON. `jobs list` takes `--queue` and `--state`. `jobs retry` runs the job now: a dead job gets a new set of attempts, and a delayed job does not wait. `jobs clear` removes pending and dead jobs, not running ones. It asks for confirmation on a terminal and needs `--yes` elsewhere. The handler of a successful job deletes it, so the list shows pending, running, and dead jobs. In `vite dev`, a plain `Error` from a handler shows its message and stack. A production server shows `Queue Delivery failed.` instead, because a message can hold private data; throw a `ViteHubError` to keep a code and message. The same commands work on a Node server with the local or postgres provider. Cloudflare and Vercel do not let the app read their queue, so the commands fail there with `QUEUE_JOBS_UNAVAILABLE`.
|
|
190
|
+
|
|
96
191
|
## Treat acceptance and delivery separately
|
|
97
192
|
|
|
98
|
-
`
|
|
193
|
+
`sendQueue()` waits for provider enqueue and returns `[error, result]`. `result` is this portable value:
|
|
99
194
|
|
|
100
195
|
```ts
|
|
101
196
|
type QueueSendResult = {
|
|
@@ -104,30 +199,74 @@ type QueueSendResult = {
|
|
|
104
199
|
};
|
|
105
200
|
```
|
|
106
201
|
|
|
107
|
-
|
|
202
|
+
ViteHub generates the message ID; an `id` option returns `QUEUE_UNSUPPORTED_OPTION`. Pass `idempotencyKey` to deduplicate sends on the local, postgres, and Vercel providers. When `send()` returns a `messageId`, the handler receives the same `id`. The local and postgres providers return the ID of the stored job, and Vercel returns the ID from its enqueue response. Cloudflare returns no `messageId`, because its binding does not report the ID that the delivery gets. In every case, `status: "queued"` is the portable acceptance signal. Queue handlers run later and return no value to the producer. Use a [Workflow](https://vitehub.dev/docs/workflows) when the caller needs a tracked run, durable steps, waits, or progress.
|
|
108
203
|
|
|
109
204
|
`deferQueue()` does not wait for provider acceptance. It schedules enqueue work through the current request's `waitUntil`, logs dispatch failures, and calls the Queue Definition's `onDispatchError` hook when present.
|
|
110
205
|
|
|
111
|
-
Providers can retry failed delivery, so handlers must tolerate another invocation after a partial side effect. ViteHub does not expose an exactly-once delivery guarantee. Vercel sends an idempotency key, using the generated message ID by default; pass a stable application key when repeated enqueue attempts should share one key. Cloudflare does not accept `idempotencyKey`, so protect non-repeatable side effects inside the handler.
|
|
206
|
+
Providers, the local one included, can retry failed delivery, so handlers must tolerate another invocation after a partial side effect. ViteHub does not expose an exactly-once delivery guarantee. Vercel sends an idempotency key, using the generated message ID by default; pass a stable application key when repeated enqueue attempts should share one key. Cloudflare does not accept `idempotencyKey`, so protect non-repeatable side effects inside the handler.
|
|
207
|
+
|
|
208
|
+
On Cloudflare, successful handlers acknowledge the message. Failed handlers retry by default unless the error is a Queue error that a retry cannot fix or `onError` returns an explicit acknowledge or retry action. On Vercel, `callbackOptions.retry` can return an explicit directive; returning nothing preserves provider behavior. Application error codes do not choose retry policy.
|
|
209
|
+
|
|
210
|
+
`sendQueue()`, `dynamicQueue.send()`, `send()`, and `sendBatch()` return `[error, data]`. `error.code` is `QUEUE_PROVIDER_FAILED`, `QUEUE_PROVIDER_INVALID_RESPONSE`, or `QUEUE_UNSUPPORTED_OPTION`, and each error has `why`, `fix`, `docs`, and `cause`. Configuration errors, for example an unknown Queue name or a missing Cloudflare binding, throw. `JSON.stringify(error)` and Queue delivery reports omit `cause`, so keep credentials and provider responses there. Throw `ViteHubError` from a handler for an application code. See the [Queue error reference](https://vitehub.dev/docs/queue/limits-and-errors) for each code and the retry policy.
|
|
112
211
|
|
|
113
|
-
|
|
212
|
+
## Add a Queue from another plugin
|
|
114
213
|
|
|
115
|
-
|
|
214
|
+
A package can add a Queue Definition from its Vite plugin, so the app needs no file in `server/queues/`. Put the entry in `vitehub.queues`:
|
|
215
|
+
|
|
216
|
+
```ts
|
|
217
|
+
import { fileURLToPath } from "node:url"
|
|
218
|
+
|
|
219
|
+
import type { Plugin } from "vite"
|
|
220
|
+
import type { QueueContributingPlugin } from "@vite-hub/queue/vite"
|
|
221
|
+
|
|
222
|
+
export function webhooks(): Plugin & QueueContributingPlugin {
|
|
223
|
+
return {
|
|
224
|
+
name: "@acme/webhooks",
|
|
225
|
+
vitehub: {
|
|
226
|
+
queues: [{
|
|
227
|
+
name: "acme-webhooks",
|
|
228
|
+
definition: { module: fileURLToPath(new URL("./deliver.js", import.meta.url)) },
|
|
229
|
+
reason: "Delivers webhooks after their delay.",
|
|
230
|
+
}],
|
|
231
|
+
},
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
`definition.module` is an absolute path or `file:` URL of a module with `export default defineQueue(...)`. `vitehub.queues` can also be a function that gets `{ root, projectRoot, serverDirs }` and returns the list. Queue treats the entry like a discovered file: it goes in the registry and the generated types, and gets its Cloudflare binding, Vercel consumer route, provisioning, and inspection. `vitehub inspect` shows the plugin and the reason. A name that a file or another plugin also uses fails with `QUEUE_DEFINITION_DUPLICATE`, and an invalid entry fails with `QUEUE_CONTRIBUTION_INVALID`.
|
|
116
237
|
|
|
117
238
|
## Public imports
|
|
118
239
|
|
|
119
240
|
| Import | Purpose |
|
|
120
241
|
| -------------------------------- | ----------------------------------------------------------------------------------- |
|
|
121
242
|
| `@vite-hub/queue` | Queue Definitions, enqueue helpers, direct clients, host adapters, and public types |
|
|
122
|
-
| `@vite-hub/queue/vite` | Vite discovery and Cloudflare or
|
|
123
|
-
| `@vite-hub/queue/nuxt` | Nuxt module that composes Queue into Nitro |
|
|
243
|
+
| `@vite-hub/queue/vite` | Vite discovery and Cloudflare, Vercel, local, or postgres Nitro configuration |
|
|
124
244
|
| `@vite-hub/queue/runtime/hosted` | Advanced hosted Vercel callback adapter |
|
|
125
245
|
|
|
126
246
|
`@vite-hub/queue/internal/*` exists for generated framework integration. Application code should not import it.
|
|
127
247
|
|
|
128
248
|
## Go deeper
|
|
129
249
|
|
|
130
|
-
- [Queue guide](https://vitehub.dev/docs/
|
|
250
|
+
- [Queue guide](https://vitehub.dev/docs/queue)
|
|
131
251
|
- [Runtime and host support](https://vitehub.dev/docs/frameworks-hosts/support-matrix)
|
|
132
|
-
- [Definitions and discovery](https://vitehub.dev/docs/
|
|
252
|
+
- [Definitions and discovery](https://vitehub.dev/docs/development/definition-discovery)
|
|
133
253
|
- [Public import paths](https://vitehub.dev/docs/reference/import-paths)
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
## Definition-owned dispatch types
|
|
257
|
+
|
|
258
|
+
The Vite Integration writes `.vitehub/queue.d.ts`. Include that file in your TypeScript project. The Nuxt Integration adds it to the generated type context. Names and payloads then come from the discovered Queue Definitions:
|
|
259
|
+
|
|
260
|
+
```ts
|
|
261
|
+
import { sendQueue, useQueue } from "@vite-hub/queue"
|
|
262
|
+
|
|
263
|
+
const [error] = await sendQueue("welcome-email", { email: "ada@example.com" }, { delaySeconds: 60 })
|
|
264
|
+
const queue = await useQueue("welcome-email")
|
|
265
|
+
const [sendError] = await queue.send({ email: "ada@example.com" })
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
`QueueRegistry` is the generated definition map. `QueuePayload<"welcome-email">` extracts a payload type. A missing name or wrong payload is a type error. Run Vite configuration or the Nuxt prepare step after you add a definition. For a standalone TypeScript host, you can extend `QueueRegistry` with `typeof import("./welcome.queue").default`.
|
|
269
|
+
|
|
270
|
+
Use `dynamicQueue.send(name, payload, options?)`, `dynamicQueue.defer()`, or `dynamicQueue.use()` for names read from external input. These methods check that the definition exists at runtime. They do not validate payload shape. Validate external data in your application. Direct provider clients from `createQueueClient()` also accept unknown payloads because they have no Queue Definition.
|
|
271
|
+
|
|
272
|
+
This is a breaking change: replace `sendQueue(name, { payload, ...options })` with `sendQueue(name, payload, options)`, and `client.send({ payload, ...options })` with `client.send(payload, options)`. There is no envelope detection. Existing envelope objects are delivered whole as payloads.
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { t as queueErrorDiagnostics } from "./error-diagnostics-BRartC1z.js";
|
|
2
|
+
import { i as normalizePublicQueueIdentifier, l as normalizeQueueEnqueueInput, n as isQueueError, s as queueUnsupportedOptionsError } from "./errors-DL3pcNO4.js";
|
|
3
|
+
import { a as getQueueRuntimeEvent, c as loadQueueDefinition, g as resolveWaitUntil, i as getQueueRuntimeConfig, n as getOrCreateQueueClient, r as getQueueRuntimeClientFactory, u as runWithQueueRuntimeEvent, v as getCloudflareEnv } from "./state-mVA2X_wa.js";
|
|
4
|
+
import { n as normalizeQueueOptions } from "./config-BcTDeQIP.js";
|
|
5
|
+
import { t as resolveVercelQueueRegion } from "./vercel-region-D1CivgdA.js";
|
|
6
|
+
import { t as getCloudflareQueueBindingName } from "./cloudflare-GosJefIK.js";
|
|
7
|
+
import { t as getVercelQueueTopicName } from "./vercel-C_4vqxcl.js";
|
|
8
|
+
import { getViteHubTestMode } from "@vite-hub/runtime/internal/test-mode";
|
|
9
|
+
//#region src/runtime/test-client.ts
|
|
10
|
+
/** The sent jobs of the current test. `vite-hub/test` reads the same list. */
|
|
11
|
+
function queueTestMessages(test) {
|
|
12
|
+
return test.backend("queue", () => []);
|
|
13
|
+
}
|
|
14
|
+
/** Records the job and does not run it, so a test asserts what the app sent. */
|
|
15
|
+
function recordQueueTestMessage(test, queue, input) {
|
|
16
|
+
const messages = queueTestMessages(test);
|
|
17
|
+
const duplicate = input.options.idempotencyKey === void 0 ? void 0 : messages.find((message) => message.queue === queue && message.options.idempotencyKey === input.options.idempotencyKey);
|
|
18
|
+
if (duplicate) return {
|
|
19
|
+
messageId: duplicate.id,
|
|
20
|
+
status: "queued"
|
|
21
|
+
};
|
|
22
|
+
messages.push({
|
|
23
|
+
id: input.id,
|
|
24
|
+
options: input.options,
|
|
25
|
+
payload: structuredClone(input.payload),
|
|
26
|
+
queue
|
|
27
|
+
});
|
|
28
|
+
return {
|
|
29
|
+
messageId: input.id,
|
|
30
|
+
status: "queued"
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/** A Queue client that records every job in the test's list. */
|
|
34
|
+
function createTestQueueClient(test, queue, provider) {
|
|
35
|
+
return {
|
|
36
|
+
native: void 0,
|
|
37
|
+
provider: "local",
|
|
38
|
+
queue,
|
|
39
|
+
async send(payload, options) {
|
|
40
|
+
const input = normalizeQueueEnqueueInput(payload, options);
|
|
41
|
+
const error = queueUnsupportedOptionsError(input.options, provider);
|
|
42
|
+
if (error) return [error, void 0];
|
|
43
|
+
return [void 0, recordQueueTestMessage(test, queue, input)];
|
|
44
|
+
},
|
|
45
|
+
async sendBatch(items, options = {}) {
|
|
46
|
+
const inputs = items.map(({ body, ...itemOptions }) => normalizeQueueEnqueueInput(body, {
|
|
47
|
+
...options,
|
|
48
|
+
...Object.fromEntries(Object.entries(itemOptions).filter(([, value]) => value !== void 0))
|
|
49
|
+
}));
|
|
50
|
+
for (const input of inputs) {
|
|
51
|
+
const error = queueUnsupportedOptionsError(input.options, provider);
|
|
52
|
+
if (error) return [error, void 0];
|
|
53
|
+
}
|
|
54
|
+
return [void 0, inputs.map((input) => recordQueueTestMessage(test, queue, input))];
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/runtime/client.ts
|
|
60
|
+
async function loadNamedQueueDefinition(name) {
|
|
61
|
+
try {
|
|
62
|
+
return await loadQueueDefinition(name);
|
|
63
|
+
} catch (cause) {
|
|
64
|
+
throw queueErrorDiagnostics.QUEUE_DEFINITION_LOAD_FAILED({
|
|
65
|
+
cause,
|
|
66
|
+
queue: normalizePublicQueueIdentifier(name)
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function resolveCloudflareBinding(binding, name) {
|
|
71
|
+
if (binding && typeof binding !== "string") return binding;
|
|
72
|
+
const bindingName = binding || getCloudflareQueueBindingName(name);
|
|
73
|
+
return getCloudflareEnv(getQueueRuntimeEvent())?.[bindingName] || bindingName;
|
|
74
|
+
}
|
|
75
|
+
function toDriverOptions(name, config) {
|
|
76
|
+
if (config.driver === "cloudflare") return {
|
|
77
|
+
...config,
|
|
78
|
+
binding: resolveCloudflareBinding(config.binding, name)
|
|
79
|
+
};
|
|
80
|
+
if (config.driver === "local") return {
|
|
81
|
+
...config,
|
|
82
|
+
queue: name
|
|
83
|
+
};
|
|
84
|
+
if (config.driver === "postgres") return {
|
|
85
|
+
...config,
|
|
86
|
+
queue: name
|
|
87
|
+
};
|
|
88
|
+
return {
|
|
89
|
+
...config,
|
|
90
|
+
topic: getVercelQueueTopicName(name)
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function getActiveQueueConfig() {
|
|
94
|
+
const config = getQueueRuntimeConfig();
|
|
95
|
+
if (config === false) return false;
|
|
96
|
+
return config || normalizeQueueOptions(void 0, { hosting: "vercel" });
|
|
97
|
+
}
|
|
98
|
+
async function createNamedQueueClient(name) {
|
|
99
|
+
const config = getActiveQueueConfig();
|
|
100
|
+
if (config === false) throw queueErrorDiagnostics.QUEUE_DISABLED();
|
|
101
|
+
const driverOptions = toDriverOptions(name, config);
|
|
102
|
+
const createClient = getQueueRuntimeClientFactory();
|
|
103
|
+
if (!createClient) throw queueErrorDiagnostics.QUEUE_RUNTIME_NOT_INSTALLED();
|
|
104
|
+
try {
|
|
105
|
+
return await createClient(driverOptions);
|
|
106
|
+
} catch (cause) {
|
|
107
|
+
if (isQueueError(cause)) throw cause;
|
|
108
|
+
throw queueErrorDiagnostics.QUEUE_PROVIDER_FAILED({
|
|
109
|
+
cause,
|
|
110
|
+
operation: "create-client",
|
|
111
|
+
provider: driverOptions.driver
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async function useDynamicQueue(name) {
|
|
116
|
+
const config = getActiveQueueConfig();
|
|
117
|
+
if (config === false) throw queueErrorDiagnostics.QUEUE_DISABLED();
|
|
118
|
+
const definition = await loadNamedQueueDefinition(name);
|
|
119
|
+
if (!definition) throw queueErrorDiagnostics.QUEUE_DEFINITION_NOT_FOUND({ queue: normalizePublicQueueIdentifier(name) });
|
|
120
|
+
const test = getViteHubTestMode();
|
|
121
|
+
if (test) return createTestQueueClient(test, name, config.driver === "local" ? config.rules ?? "local" : config.driver);
|
|
122
|
+
if (definition.options?.cache === false || config.cache === false || config.driver === "cloudflare" || config.driver === "vercel" && resolveVercelQueueRegion(config.region).requestScoped) return await createNamedQueueClient(name);
|
|
123
|
+
return await getOrCreateQueueClient(name, () => createNamedQueueClient(name));
|
|
124
|
+
}
|
|
125
|
+
async function sendDynamicQueue(name, payload, options) {
|
|
126
|
+
let queue;
|
|
127
|
+
try {
|
|
128
|
+
queue = await useDynamicQueue(name);
|
|
129
|
+
} catch (error) {
|
|
130
|
+
if (isQueueError(error, ["QUEUE_PROVIDER_FAILED"])) return [error, void 0];
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
return await queue.send(payload, options);
|
|
134
|
+
}
|
|
135
|
+
function deferDynamicQueue(name, payload, options) {
|
|
136
|
+
const request = getQueueRuntimeEvent();
|
|
137
|
+
const task = async () => {
|
|
138
|
+
const [error] = await runWithQueueRuntimeEvent(request, () => sendDynamicQueue(name, payload, options));
|
|
139
|
+
if (error) throw error;
|
|
140
|
+
};
|
|
141
|
+
const handleError = async (error) => {
|
|
142
|
+
console.error(`[vitehub] Deferred queue dispatch failed for "${name}"`, error);
|
|
143
|
+
try {
|
|
144
|
+
await (await loadNamedQueueDefinition(name))?.options?.onDispatchError?.(error, { name });
|
|
145
|
+
} catch (hookError) {
|
|
146
|
+
console.error(`[vitehub] onDispatchError hook failed for "${name}"`, hookError);
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
const promise = task().catch(handleError);
|
|
150
|
+
const waitUntil = resolveWaitUntil(request);
|
|
151
|
+
if (typeof waitUntil === "function") waitUntil(promise);
|
|
152
|
+
}
|
|
153
|
+
/** Dispatch externally supplied names and payloads after application validation. */
|
|
154
|
+
const dynamicQueue = {
|
|
155
|
+
defer: deferDynamicQueue,
|
|
156
|
+
send: sendDynamicQueue,
|
|
157
|
+
use: useDynamicQueue
|
|
158
|
+
};
|
|
159
|
+
/**
|
|
160
|
+
* Returns the QueueClient of a discovered Queue Definition, for `sendBatch()` and provider methods. Config errors, for example an unknown name, throw.
|
|
161
|
+
*/
|
|
162
|
+
async function useQueue(name) {
|
|
163
|
+
return await useDynamicQueue(name);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Sends one job to the Queue and returns `[error, result]`. `error.code` is a `QueueErrorCode`.
|
|
167
|
+
* Config errors, for example an unknown Queue name, throw.
|
|
168
|
+
*/
|
|
169
|
+
function sendQueue(name, payload, options) {
|
|
170
|
+
return sendDynamicQueue(name, payload, options);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Sends one job after the response, through `waitUntil`. It returns nothing: a failure goes to the log and to the
|
|
174
|
+
* `onDispatchError` option of the Queue Definition. Call `sendQueue()` when the caller needs the message ID.
|
|
175
|
+
*/
|
|
176
|
+
function deferQueue(name, payload, options) {
|
|
177
|
+
deferDynamicQueue(name, payload, options);
|
|
178
|
+
}
|
|
179
|
+
//#endregion
|
|
180
|
+
export { useQueue as i, dynamicQueue as n, sendQueue as r, deferQueue as t };
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { t as queueErrorDiagnostics } from "./error-diagnostics-BRartC1z.js";
|
|
2
|
+
import { l as normalizeQueueEnqueueInput, o as queueProviderResult, s as queueUnsupportedOptionsError } from "./errors-DL3pcNO4.js";
|
|
3
|
+
import { n as getCloudflareQueueDefinitionName } from "./cloudflare-GosJefIK.js";
|
|
4
|
+
import { i as reportQueueDeliveryError, r as isNonRetryableQueueError, t as checkQueueHandlerResponse } from "./handler-response-By0Fpspv.js";
|
|
5
|
+
//#region src/providers/cloudflare.ts
|
|
6
|
+
const cloudflareBindingName = /^[A-Z_]\w{0,127}$/i;
|
|
7
|
+
function isCloudflareQueueBinding(binding) {
|
|
8
|
+
if (binding === null || typeof binding !== "object" || Array.isArray(binding)) return false;
|
|
9
|
+
const hasCallableMethod = (name) => {
|
|
10
|
+
let owner = binding;
|
|
11
|
+
while (owner && owner !== Object.prototype) {
|
|
12
|
+
let descriptor;
|
|
13
|
+
try {
|
|
14
|
+
descriptor = Object.getOwnPropertyDescriptor(owner, name);
|
|
15
|
+
} catch {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
if (descriptor) {
|
|
19
|
+
if (owner !== binding) {
|
|
20
|
+
const constructor = Object.getOwnPropertyDescriptor(owner, "constructor");
|
|
21
|
+
if (!constructor || !("value" in constructor) || typeof constructor.value !== "function" || constructor.value.prototype !== owner) return false;
|
|
22
|
+
}
|
|
23
|
+
if ("value" in descriptor) return typeof descriptor.value === "function";
|
|
24
|
+
try {
|
|
25
|
+
return typeof Reflect.get(binding, name) === "function";
|
|
26
|
+
} catch {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
owner = Object.getPrototypeOf(owner);
|
|
32
|
+
} catch {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
};
|
|
38
|
+
return hasCallableMethod("send") && hasCallableMethod("sendBatch");
|
|
39
|
+
}
|
|
40
|
+
function toSendOptions(options = {}) {
|
|
41
|
+
const error = queueUnsupportedOptionsError(options, "cloudflare");
|
|
42
|
+
if (error) return [error, void 0];
|
|
43
|
+
return [void 0, {
|
|
44
|
+
contentType: options.contentType,
|
|
45
|
+
delaySeconds: options.delaySeconds
|
|
46
|
+
}];
|
|
47
|
+
}
|
|
48
|
+
function resolveAction(action, message, fallback) {
|
|
49
|
+
if (action && typeof action === "object" && !Array.isArray(action) && Object.hasOwn(action, "retry")) {
|
|
50
|
+
message.retry(action.retry);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if ((action === "ack" || action === "retry" ? action : fallback) === "ack") message.ack();
|
|
54
|
+
else message.retry();
|
|
55
|
+
}
|
|
56
|
+
function createCloudflareQueueBatchHandler(options) {
|
|
57
|
+
const requested = Number(options.concurrency ?? 1);
|
|
58
|
+
const concurrency = Number.isFinite(requested) ? Math.max(1, Math.floor(requested)) : 1;
|
|
59
|
+
return async (batch) => {
|
|
60
|
+
const messages = Array.isArray(batch?.messages) ? batch.messages : [];
|
|
61
|
+
if (!messages.length) return;
|
|
62
|
+
let index = 0;
|
|
63
|
+
const worker = async () => {
|
|
64
|
+
while (index < messages.length) {
|
|
65
|
+
const message = messages[index++];
|
|
66
|
+
try {
|
|
67
|
+
checkQueueHandlerResponse(await options.onMessage(message, batch));
|
|
68
|
+
message.ack();
|
|
69
|
+
} catch (error) {
|
|
70
|
+
const reportContext = {
|
|
71
|
+
attempts: message.attempts,
|
|
72
|
+
id: message.id,
|
|
73
|
+
provider: "cloudflare",
|
|
74
|
+
queue: getCloudflareQueueDefinitionName(batch.queue)
|
|
75
|
+
};
|
|
76
|
+
reportQueueDeliveryError(error, reportContext);
|
|
77
|
+
let action = void 0;
|
|
78
|
+
if (options.onError) try {
|
|
79
|
+
action = await options.onError(error, message, batch);
|
|
80
|
+
} catch (hookError) {
|
|
81
|
+
reportQueueDeliveryError(hookError, reportContext);
|
|
82
|
+
}
|
|
83
|
+
resolveAction(action, message, isNonRetryableQueueError(error) ? "ack" : "retry");
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, messages.length) }, () => worker()));
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function createCloudflareQueueClient(provider) {
|
|
91
|
+
if (typeof provider.binding === "undefined" || typeof provider.binding === "string") {
|
|
92
|
+
const binding = typeof provider.binding === "string" && cloudflareBindingName.test(provider.binding) ? provider.binding : void 0;
|
|
93
|
+
throw queueErrorDiagnostics.QUEUE_BINDING_NOT_FOUND({ binding });
|
|
94
|
+
}
|
|
95
|
+
if (!isCloudflareQueueBinding(provider.binding)) throw queueErrorDiagnostics.QUEUE_INVALID_BINDING();
|
|
96
|
+
const binding = provider.binding;
|
|
97
|
+
return {
|
|
98
|
+
provider: "cloudflare",
|
|
99
|
+
native: binding,
|
|
100
|
+
binding,
|
|
101
|
+
async send(payload, options) {
|
|
102
|
+
const normalized = normalizeQueueEnqueueInput(payload, options);
|
|
103
|
+
const [optionsError, sendOptions] = toSendOptions(normalized.options);
|
|
104
|
+
if (optionsError) return [optionsError, void 0];
|
|
105
|
+
const [error] = await queueProviderResult("cloudflare", "send", () => binding.send(normalized.payload, sendOptions));
|
|
106
|
+
if (error) return [error, void 0];
|
|
107
|
+
return [void 0, { status: "queued" }];
|
|
108
|
+
},
|
|
109
|
+
async sendBatch(items, options) {
|
|
110
|
+
const messages = [];
|
|
111
|
+
for (const item of items) {
|
|
112
|
+
const [optionsError, sendOptions] = toSendOptions({
|
|
113
|
+
...options,
|
|
114
|
+
contentType: item.contentType || options?.contentType,
|
|
115
|
+
delaySeconds: item.delaySeconds ?? options?.delaySeconds
|
|
116
|
+
});
|
|
117
|
+
if (optionsError) return [optionsError, void 0];
|
|
118
|
+
messages.push({
|
|
119
|
+
...item,
|
|
120
|
+
...sendOptions
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
const [error] = await queueProviderResult("cloudflare", "send-batch", () => binding.sendBatch(messages));
|
|
124
|
+
if (error) return [error, void 0];
|
|
125
|
+
return [void 0, messages.map(() => ({ status: "queued" }))];
|
|
126
|
+
},
|
|
127
|
+
createBatchHandler: createCloudflareQueueBatchHandler
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
//#endregion
|
|
131
|
+
//#region src/internal/runtime/cloudflare-client.ts
|
|
132
|
+
async function createCloudflareQueueRuntimeClient(options) {
|
|
133
|
+
if (options.driver !== "cloudflare") throw queueErrorDiagnostics.QUEUE_DRIVER_MISMATCH({
|
|
134
|
+
driver: options.driver,
|
|
135
|
+
expected: "cloudflare"
|
|
136
|
+
});
|
|
137
|
+
return createCloudflareQueueClient(options);
|
|
138
|
+
}
|
|
139
|
+
//#endregion
|
|
140
|
+
export { createCloudflareQueueBatchHandler as n, createCloudflareQueueRuntimeClient as t };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { t as queueErrorDiagnostics } from "./error-diagnostics-BRartC1z.js";
|
|
1
2
|
import { r as encodeNameHex } from "./cloudflare-GosJefIK.js";
|
|
2
3
|
//#region src/internal/cloudflare-resource-name.ts
|
|
3
4
|
const cloudflareQueueNamePrefix = "queue--";
|
|
@@ -8,7 +9,7 @@ const maxCloudflareQueueNameLength = 63;
|
|
|
8
9
|
const digestLength = 32;
|
|
9
10
|
function digestCloudflareQueueName(name, namePrefix) {
|
|
10
11
|
const crypto = globalThis.process?.getBuiltinModule("node:crypto");
|
|
11
|
-
if (!crypto) throw
|
|
12
|
+
if (!crypto) throw queueErrorDiagnostics.QUEUE_CRYPTO_UNAVAILABLE();
|
|
12
13
|
return crypto.hash("sha256", JSON.stringify([
|
|
13
14
|
"vitehub",
|
|
14
15
|
"cloudflare",
|
|
@@ -27,7 +28,7 @@ function createBoundedCloudflareQueueName(name, namePrefix) {
|
|
|
27
28
|
}
|
|
28
29
|
function getCloudflareQueueName(name, namePrefix = "") {
|
|
29
30
|
const queueName = `${namePrefix}${cloudflareQueueNamePrefix}${encodeNameHex(name)}`;
|
|
30
|
-
if (!cloudflareQueueNamePattern.test(queueName)) throw
|
|
31
|
+
if (!cloudflareQueueNamePattern.test(queueName)) throw queueErrorDiagnostics.QUEUE_INVALID_NAME({ name: queueName });
|
|
31
32
|
return queueName.length <= maxCloudflareQueueNameLength ? queueName : createBoundedCloudflareQueueName(name, namePrefix);
|
|
32
33
|
}
|
|
33
34
|
//#endregion
|