@vite-hub/queue 0.1.0 → 0.4.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 +152 -29
- package/dist/client-B53_G4ky.js +182 -0
- package/dist/{cloudflare-client-COzQ8fHk.js → cloudflare-client-CoISDpIx.js} +28 -21
- package/dist/{cloudflare-resource-name-CCuLDvSP.js → cloudflare-resource-name-BIfBglC2.js} +1 -1
- package/dist/config-B2eaXaT5.js +105 -0
- package/dist/durability.d.ts +13 -0
- package/dist/durability.js +47 -0
- package/dist/duration-tujyhAKt.js +111 -0
- package/dist/error-diagnostics-CQ4BUqRe.js +270 -0
- package/dist/error-diagnostics-mJXBjmQx.js +181 -0
- package/dist/{handler-response-CBwIkmTS.js → handler-response-B6lz9nCn.js} +9 -4
- package/dist/index.d.ts +20 -32
- package/dist/index.js +39 -17
- package/dist/internal/runtime/cloudflare-client.d.ts +2 -2
- 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 +570 -0
- package/dist/internal/runtime/state.d.ts +11 -4
- package/dist/internal/runtime/state.js +2 -2
- package/dist/internal/runtime/vercel-client.d.ts +2 -2
- package/dist/internal/runtime/vercel-client.js +66 -28
- 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-CUaWoRUx.d.ts +23 -0
- package/dist/local-Di0FrVUw.js +553 -0
- package/dist/object-D8pRdprI.js +15 -0
- package/dist/{operations-tNUdj3G4.d.ts → operation-definition-nmRu_O3V.d.ts} +42 -15
- package/dist/operations-BVV-9Q0N.js +129 -0
- package/dist/operations.d.ts +47 -9
- package/dist/operations.js +186 -69
- package/dist/runtime/hosted.d.ts +3 -5
- package/dist/runtime/hosted.js +21 -45
- package/dist/{state-G9s378-e.js → state-DLqTqF6V.js} +59 -47
- package/dist/types-CiuSIPR0.d.ts +440 -0
- package/dist/types-Dsbb_Pvp.d.ts +10 -0
- package/dist/vercel-region-B95MAMXN.js +26 -0
- package/dist/virtual.d.ts +4 -0
- package/dist/vite.d.ts +366 -49
- package/dist/vite.js +2071 -2
- package/package.json +39 -46
- package/dist/client-CIc3jsj2.js +0 -114
- package/dist/cloudflare-vite-BCuJOXcT.js +0 -111
- package/dist/cloudflare-vite-CjdP5yBN.d.ts +0 -29
- package/dist/config-BINujLv4.js +0 -74
- package/dist/enqueue-CnQ0QR3O.js +0 -22
- package/dist/error-diagnostics-A_8Kp-48.js +0 -116
- package/dist/error-diagnostics-qzRF6IVB.js +0 -109
- package/dist/errors-DIFaM9tW.js +0 -42
- package/dist/nuxt.d.ts +0 -39
- package/dist/nuxt.js +0 -34
- package/dist/object-B1NW521B.js +0 -7
- package/dist/operations-Cnjbz8kk.js +0 -63
- package/dist/types-1GRMYES1.d.ts +0 -180
- package/dist/vercel-region-cjskrOKl.js +0 -122
- package/dist/vite-ChWLzOVu.js +0 -1512
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
import { Duration, Result } from "@vite-hub/runtime";
|
|
2
|
+
//#region src/types.d.ts
|
|
3
|
+
type QueueDriver = "cloudflare" | "local" | "postgres" | "vercel";
|
|
4
|
+
/** The Queue drivers that run jobs in a Node server process. */
|
|
5
|
+
type NodeQueueDriver = "local" | "postgres";
|
|
6
|
+
type CloudflareQueueContentType = "bytes" | "json" | "text" | "v8";
|
|
7
|
+
interface CloudflareQueueSendOptions {
|
|
8
|
+
contentType?: CloudflareQueueContentType;
|
|
9
|
+
delaySeconds?: number;
|
|
10
|
+
}
|
|
11
|
+
interface CloudflareQueueSendBatchOptions {
|
|
12
|
+
delaySeconds?: number;
|
|
13
|
+
}
|
|
14
|
+
interface CloudflareQueueSendBatchMessage<T = unknown> extends CloudflareQueueSendOptions {
|
|
15
|
+
body: T;
|
|
16
|
+
}
|
|
17
|
+
/** One job in `sendBatch()` of a Cloudflare QueueClient. */
|
|
18
|
+
interface CloudflareQueueSendBatchItem<T = unknown> {
|
|
19
|
+
body: T;
|
|
20
|
+
contentType?: CloudflareQueueContentType;
|
|
21
|
+
/** Time before delivery, such as `"1m"`. */
|
|
22
|
+
delay?: Duration;
|
|
23
|
+
}
|
|
24
|
+
interface CloudflareQueueRetryOptions {
|
|
25
|
+
delaySeconds?: number;
|
|
26
|
+
}
|
|
27
|
+
interface CloudflareQueueBinding {
|
|
28
|
+
send: <T = unknown>(body: T, options?: CloudflareQueueSendOptions) => Promise<unknown>;
|
|
29
|
+
sendBatch: <T = unknown>(messages: Iterable<CloudflareQueueSendBatchMessage<T>>, options?: CloudflareQueueSendBatchOptions) => Promise<unknown>;
|
|
30
|
+
}
|
|
31
|
+
interface CloudflareQueueMessage<T = unknown> {
|
|
32
|
+
ack: () => void;
|
|
33
|
+
attempts: number;
|
|
34
|
+
body: T;
|
|
35
|
+
id: string;
|
|
36
|
+
retry: (options?: CloudflareQueueRetryOptions) => void;
|
|
37
|
+
timestamp?: Date;
|
|
38
|
+
}
|
|
39
|
+
interface CloudflareQueueMessageBatch<T = unknown> {
|
|
40
|
+
ackAll: () => void;
|
|
41
|
+
messages: CloudflareQueueMessage<T>[];
|
|
42
|
+
queue: string;
|
|
43
|
+
retryAll: (options?: CloudflareQueueRetryOptions) => void;
|
|
44
|
+
}
|
|
45
|
+
/** What `onError` returns: `"ack"`, `"retry"`, or `{ retry: { delay: "30s" } }` to retry after a time. */
|
|
46
|
+
type CloudflareQueueBatchErrorAction = "ack" | "retry" | {
|
|
47
|
+
retry: {
|
|
48
|
+
delay?: Duration;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
interface CloudflareQueueBatchHandlerOptions<T = unknown> {
|
|
52
|
+
concurrency?: number;
|
|
53
|
+
onError?: (error: unknown, message: CloudflareQueueMessage<T>, batch: CloudflareQueueMessageBatch<T>) => CloudflareQueueBatchErrorAction | void | Promise<CloudflareQueueBatchErrorAction | void>;
|
|
54
|
+
onMessage: (message: CloudflareQueueMessage<T>, batch: CloudflareQueueMessageBatch<T>) => unknown | Promise<unknown>;
|
|
55
|
+
}
|
|
56
|
+
type VercelQueueRetryDirective = {
|
|
57
|
+
acknowledge: true;
|
|
58
|
+
} | {
|
|
59
|
+
afterSeconds: number;
|
|
60
|
+
};
|
|
61
|
+
type VercelQueueRetryHandler = (error: unknown, metadata: unknown) => VercelQueueRetryDirective | void;
|
|
62
|
+
interface VercelQueueCallbackOptions {
|
|
63
|
+
retry?: VercelQueueRetryHandler;
|
|
64
|
+
visibilityTimeoutSeconds?: number;
|
|
65
|
+
}
|
|
66
|
+
interface VercelQueueSendOptions {
|
|
67
|
+
delaySeconds?: number;
|
|
68
|
+
headers?: Record<string, string>;
|
|
69
|
+
idempotencyKey?: string;
|
|
70
|
+
region?: string;
|
|
71
|
+
retentionSeconds?: number;
|
|
72
|
+
}
|
|
73
|
+
interface VercelQueueSendResult {
|
|
74
|
+
messageId?: string | null;
|
|
75
|
+
}
|
|
76
|
+
interface VercelQueueBatchSendMessage extends Omit<VercelQueueSendOptions, "headers" | "region"> {
|
|
77
|
+
payload: unknown;
|
|
78
|
+
}
|
|
79
|
+
/** Result of one message in a `@vercel/queue` `sendBatch()` request, in input order. */
|
|
80
|
+
type VercelQueueBatchSendResult = {
|
|
81
|
+
messageId: string;
|
|
82
|
+
status: "sent";
|
|
83
|
+
} | {
|
|
84
|
+
messageId: null;
|
|
85
|
+
status: "deferred";
|
|
86
|
+
} | {
|
|
87
|
+
error: string;
|
|
88
|
+
retryable: boolean;
|
|
89
|
+
status: "failed";
|
|
90
|
+
statusCode: number;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* One job in `sendBatch()` of a Vercel QueueClient. Omitted `idempotencyKey` values are generated anew on each call.
|
|
94
|
+
* Supply a stable key from the first attempt to deduplicate retries.
|
|
95
|
+
*/
|
|
96
|
+
interface VercelQueueSendBatchMessage<T = unknown> {
|
|
97
|
+
body: T;
|
|
98
|
+
/** Time before delivery, such as `"1m"`. */
|
|
99
|
+
delay?: Duration;
|
|
100
|
+
idempotencyKey?: string;
|
|
101
|
+
/** How long Vercel keeps the job: a duration such as `"1h"`, or a number of seconds. */
|
|
102
|
+
retention?: Duration | number;
|
|
103
|
+
}
|
|
104
|
+
/** Options that apply to every job of a Vercel `sendBatch()` unless the job sets its own. */
|
|
105
|
+
interface VercelQueueSendBatchOptions {
|
|
106
|
+
delay?: Duration;
|
|
107
|
+
retention?: Duration | number;
|
|
108
|
+
}
|
|
109
|
+
type VercelQueueMessageHandler<T = unknown> = (payload: T, metadata?: unknown) => unknown | Promise<unknown>;
|
|
110
|
+
type VercelQueueCallbackReturn = (request: Request) => Promise<unknown> | unknown;
|
|
111
|
+
interface VercelQueueSDK {
|
|
112
|
+
handleCallback: <TPayload = unknown>(handler: VercelQueueMessageHandler<TPayload>, options?: VercelQueueCallbackOptions) => VercelQueueCallbackReturn;
|
|
113
|
+
send: (topic: string, payload: unknown, options?: VercelQueueSendOptions) => Promise<VercelQueueSendResult>;
|
|
114
|
+
/** `@vercel/queue` 0.10 and later. A custom client without it makes `sendBatch()` fail with `QUEUE_PROVIDER_FAILED`. */
|
|
115
|
+
sendBatch?: (topic: string, messages: readonly VercelQueueBatchSendMessage[]) => Promise<readonly VercelQueueBatchSendResult[]>;
|
|
116
|
+
}
|
|
117
|
+
interface QueueSharedOptions {
|
|
118
|
+
cache?: boolean;
|
|
119
|
+
}
|
|
120
|
+
interface CloudflareQueueDriverOptions extends QueueSharedOptions {
|
|
121
|
+
binding?: string | CloudflareQueueBinding;
|
|
122
|
+
driver: "cloudflare";
|
|
123
|
+
}
|
|
124
|
+
interface CloudflareQueueModuleDriverOptions extends QueueSharedOptions {
|
|
125
|
+
binding?: string;
|
|
126
|
+
namePrefix?: string;
|
|
127
|
+
driver: "cloudflare";
|
|
128
|
+
}
|
|
129
|
+
interface VercelQueueDriverOptions extends QueueSharedOptions {
|
|
130
|
+
client?: VercelQueueSDK;
|
|
131
|
+
driver: "vercel";
|
|
132
|
+
region?: string;
|
|
133
|
+
topic?: string;
|
|
134
|
+
}
|
|
135
|
+
interface VercelQueueModuleDriverOptions extends QueueSharedOptions {
|
|
136
|
+
driver: "vercel";
|
|
137
|
+
region?: string;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* The local driver keeps jobs in files under `base` and runs the Queue Definitions in this process.
|
|
141
|
+
* The jobs survive a restart on the same host. They do not move to another host or container.
|
|
142
|
+
*/
|
|
143
|
+
interface LocalQueueDriverOptions extends QueueSharedOptions {
|
|
144
|
+
/** Directory of the stored jobs, relative to the working directory. Defaults to `.vitehub/data/queue`. */
|
|
145
|
+
base?: string;
|
|
146
|
+
driver: "local";
|
|
147
|
+
/** Queue Definition that receives the jobs. `useQueue()` sets it. */
|
|
148
|
+
queue?: string;
|
|
149
|
+
/** The driver whose delivery rules the jobs follow. Defaults to `local`. `vite dev` sets the driver of the build. */
|
|
150
|
+
rules?: LocalQueueRules;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* The driver whose documented delivery rules the local driver follows: attempts, retry delay, the largest delay,
|
|
154
|
+
* payload, and batch, and the enqueue options it accepts. In `vite dev`, a Cloudflare or Vercel app runs its jobs
|
|
155
|
+
* with the local driver and the rules of its hosted driver.
|
|
156
|
+
*/
|
|
157
|
+
type LocalQueueRules = QueueDriver;
|
|
158
|
+
interface LocalQueueModuleDriverOptions extends QueueSharedOptions {
|
|
159
|
+
/** Directory of the stored jobs, relative to the working directory. Defaults to `.vitehub/data/queue`. */
|
|
160
|
+
base?: string;
|
|
161
|
+
driver: "local";
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The postgres driver keeps jobs in the `vitehub_queue_jobs` table of a Postgres database. Every server process
|
|
165
|
+
* that runs the app takes jobs from the table, so the jobs survive a restart and spread over all instances.
|
|
166
|
+
* It uses the connection of the Default Database. `QUEUE_POSTGRES_URL` overrides it at runtime.
|
|
167
|
+
*/
|
|
168
|
+
interface PostgresQueueDriverOptions extends QueueSharedOptions {
|
|
169
|
+
/** The Database whose Postgres connection Queue uses. The build sets `default` when the Default Database is on Postgres. */
|
|
170
|
+
database?: string;
|
|
171
|
+
driver: "postgres";
|
|
172
|
+
/** Queue Definition that receives the jobs. `getQueue()` sets it. */
|
|
173
|
+
queue?: string;
|
|
174
|
+
}
|
|
175
|
+
interface PostgresQueueModuleDriverOptions extends QueueSharedOptions {
|
|
176
|
+
driver: "postgres";
|
|
177
|
+
}
|
|
178
|
+
/** Postgres options after resolution. The build adds `database` when the Default Database is on Postgres. */
|
|
179
|
+
interface ResolvedPostgresQueueOptions extends PostgresQueueModuleDriverOptions {
|
|
180
|
+
database?: string;
|
|
181
|
+
}
|
|
182
|
+
type QueueDriverOptions = CloudflareQueueDriverOptions | LocalQueueDriverOptions | PostgresQueueDriverOptions | VercelQueueDriverOptions;
|
|
183
|
+
type QueueModuleDriverOptions = CloudflareQueueModuleDriverOptions | LocalQueueModuleDriverOptions | PostgresQueueModuleDriverOptions | VercelQueueModuleDriverOptions;
|
|
184
|
+
type QueueOptions = false | (QueueSharedOptions & {
|
|
185
|
+
base?: string;
|
|
186
|
+
namePrefix?: string;
|
|
187
|
+
driver?: undefined;
|
|
188
|
+
}) | QueueModuleDriverOptions;
|
|
189
|
+
/** Local options after resolution. `vite dev` adds `rules` for a Cloudflare or Vercel app. */
|
|
190
|
+
interface ResolvedLocalQueueOptions extends LocalQueueModuleDriverOptions {
|
|
191
|
+
rules?: LocalQueueRules;
|
|
192
|
+
}
|
|
193
|
+
type ResolvedQueueOptions = CloudflareQueueModuleDriverOptions | ResolvedLocalQueueOptions | ResolvedPostgresQueueOptions | VercelQueueModuleDriverOptions;
|
|
194
|
+
/** The one object that a Queue handler receives: the `payload` that was sent, and the delivery details. */
|
|
195
|
+
interface QueueJob<TPayload = unknown> {
|
|
196
|
+
/** Delivery count of this job, from 1. */
|
|
197
|
+
attempts: number;
|
|
198
|
+
/** Message ID of this job. */
|
|
199
|
+
id: string;
|
|
200
|
+
/** Delivery metadata of the provider, when it gives some. */
|
|
201
|
+
metadata?: unknown;
|
|
202
|
+
payload: TPayload;
|
|
203
|
+
}
|
|
204
|
+
type QueueHandler<TPayload = unknown, TResult = unknown> = (job: QueueJob<TPayload>) => TResult;
|
|
205
|
+
/** Options of `defineQueue()`, next to `handler`. */
|
|
206
|
+
interface QueueDefinitionOptions {
|
|
207
|
+
cache?: boolean;
|
|
208
|
+
callbackOptions?: VercelQueueCallbackOptions;
|
|
209
|
+
concurrency?: number;
|
|
210
|
+
onDispatchError?: (error: unknown, context: {
|
|
211
|
+
name: string;
|
|
212
|
+
}) => unknown | Promise<unknown>;
|
|
213
|
+
onError?: CloudflareQueueBatchHandlerOptions["onError"];
|
|
214
|
+
}
|
|
215
|
+
/** A Queue Definition: `defineQueue({ handler, ...options })`. */
|
|
216
|
+
interface QueueDefinition<TPayload = unknown, TResult = unknown> extends QueueDefinitionOptions {
|
|
217
|
+
handler: QueueHandler<TPayload, TResult>;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Options of `sendQueue()` and `send()`. ViteHub generates the message ID. To deduplicate sends, pass `idempotencyKey`
|
|
221
|
+
* on a provider that supports it.
|
|
222
|
+
*/
|
|
223
|
+
interface QueueEnqueueOptions {
|
|
224
|
+
contentType?: CloudflareQueueContentType;
|
|
225
|
+
/** Time before delivery, such as `"30s"`, `"5m"`, or `"1 day"`. A number fails with `RUNTIME_DURATION_INVALID`. */
|
|
226
|
+
delay?: Duration;
|
|
227
|
+
idempotencyKey?: string;
|
|
228
|
+
region?: string;
|
|
229
|
+
/** How long the provider keeps the job: a duration such as `"1h"`, or a number of seconds. */
|
|
230
|
+
retention?: Duration | number;
|
|
231
|
+
}
|
|
232
|
+
type QueueSendResult = {
|
|
233
|
+
messageId?: string;
|
|
234
|
+
status: "queued";
|
|
235
|
+
};
|
|
236
|
+
/** Codes that Queue operations return in the `[error, data]` tuple. Config errors throw instead. */
|
|
237
|
+
type QueueErrorCode = "QUEUE_PROVIDER_FAILED" | "QUEUE_PROVIDER_INVALID_RESPONSE" | "QUEUE_UNSUPPORTED_OPTION" | "RUNTIME_DURATION_INVALID";
|
|
238
|
+
/** Codes that `send()` and `sendBatch()` of a Cloudflare QueueClient return. */
|
|
239
|
+
type CloudflareQueueErrorCode = "QUEUE_PROVIDER_FAILED" | "QUEUE_UNSUPPORTED_OPTION" | "RUNTIME_DURATION_INVALID";
|
|
240
|
+
/** Codes that `send()` and `sendBatch()` of a local QueueClient return. */
|
|
241
|
+
type LocalQueueErrorCode = "QUEUE_PROVIDER_FAILED" | "QUEUE_UNSUPPORTED_OPTION" | "RUNTIME_DURATION_INVALID";
|
|
242
|
+
/** Codes that `send()` and `sendBatch()` of a postgres QueueClient return. */
|
|
243
|
+
type PostgresQueueErrorCode = "QUEUE_PROVIDER_FAILED" | "QUEUE_UNSUPPORTED_OPTION" | "RUNTIME_DURATION_INVALID";
|
|
244
|
+
/** One job in `sendBatch()` of a local or postgres QueueClient. */
|
|
245
|
+
interface LocalQueueSendBatchMessage<T = unknown> {
|
|
246
|
+
body: T;
|
|
247
|
+
/** Time before delivery, such as `"1m"`. */
|
|
248
|
+
delay?: Duration;
|
|
249
|
+
idempotencyKey?: string;
|
|
250
|
+
}
|
|
251
|
+
/** Codes that `send()` and `sendBatch()` of a Vercel QueueClient return. */
|
|
252
|
+
type VercelQueueErrorCode = QueueErrorCode;
|
|
253
|
+
interface QueueClientBase<P extends QueueDriver = QueueDriver> {
|
|
254
|
+
readonly native: unknown;
|
|
255
|
+
readonly provider: P;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* `send()` and `sendBatch()` return `[error, data]`. `error.code` is one of the codes in the method type.
|
|
259
|
+
*/
|
|
260
|
+
interface CloudflareQueueClient<TPayload = unknown> extends QueueClientBase<"cloudflare"> {
|
|
261
|
+
readonly binding: CloudflareQueueBinding;
|
|
262
|
+
createBatchHandler: <TMessage = unknown>(options: CloudflareQueueBatchHandlerOptions<TMessage>) => (batch: CloudflareQueueMessageBatch<TMessage>) => Promise<void>;
|
|
263
|
+
send: (payload: TPayload, options?: QueueEnqueueOptions) => Promise<Result<QueueSendResult, CloudflareQueueErrorCode>>;
|
|
264
|
+
sendBatch: (items: CloudflareQueueSendBatchItem<TPayload>[], options?: QueueEnqueueOptions) => Promise<Result<QueueSendResult[], CloudflareQueueErrorCode>>;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* `send()` and `sendBatch()` return `[error, data]`. `error.code` is one of the codes in the method type.
|
|
268
|
+
*/
|
|
269
|
+
interface VercelQueueClient<TPayload = unknown> extends QueueClientBase<"vercel"> {
|
|
270
|
+
readonly topic: string;
|
|
271
|
+
send: (payload: TPayload, options?: QueueEnqueueOptions) => Promise<Result<QueueSendResult, VercelQueueErrorCode>>;
|
|
272
|
+
/**
|
|
273
|
+
* Sends 1 to 100 jobs in one `@vercel/queue` request. When any job fails, it returns `QUEUE_PROVIDER_FAILED`
|
|
274
|
+
* with the per-job results in `error.cause.results`. Request-level failures can occur after jobs are accepted.
|
|
275
|
+
* To retry safely, supply the same caller-provided `idempotencyKey` on every job from the first attempt.
|
|
276
|
+
* Generated keys change on each call and do not deduplicate retries.
|
|
277
|
+
*/
|
|
278
|
+
sendBatch: (items: VercelQueueSendBatchMessage<TPayload>[], options?: VercelQueueSendBatchOptions) => Promise<Result<QueueSendResult[], VercelQueueErrorCode>>;
|
|
279
|
+
callback: <TMessage = unknown>(handler: VercelQueueMessageHandler<TMessage>, options?: VercelQueueCallbackOptions) => VercelQueueCallbackReturn;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* `send()` stores the job under `base` and returns when the file is written. This process then runs the
|
|
283
|
+
* Queue Definition. `send()` and `sendBatch()` return `[error, data]`.
|
|
284
|
+
*/
|
|
285
|
+
interface LocalQueueClient<TPayload = unknown> extends QueueClientBase<"local"> {
|
|
286
|
+
readonly queue: string;
|
|
287
|
+
send: (payload: TPayload, options?: QueueEnqueueOptions) => Promise<Result<QueueSendResult, LocalQueueErrorCode>>;
|
|
288
|
+
sendBatch: (items: LocalQueueSendBatchMessage<TPayload>[], options?: {
|
|
289
|
+
delay?: Duration;
|
|
290
|
+
}) => Promise<Result<QueueSendResult[], LocalQueueErrorCode>>;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* `send()` inserts the job in the Postgres table and returns when the insert commits. Any server process of the app
|
|
294
|
+
* then runs the Queue Definition. `send()` and `sendBatch()` return `[error, data]`.
|
|
295
|
+
*/
|
|
296
|
+
interface PostgresQueueClient<TPayload = unknown> extends QueueClientBase<"postgres"> {
|
|
297
|
+
readonly queue: string;
|
|
298
|
+
send: (payload: TPayload, options?: QueueEnqueueOptions) => Promise<Result<QueueSendResult, PostgresQueueErrorCode>>;
|
|
299
|
+
/** Inserts all jobs in one transaction. When one insert fails, no job of the batch is stored. */
|
|
300
|
+
sendBatch: (items: LocalQueueSendBatchMessage<TPayload>[], options?: {
|
|
301
|
+
delay?: Duration;
|
|
302
|
+
}) => Promise<Result<QueueSendResult[], PostgresQueueErrorCode>>;
|
|
303
|
+
}
|
|
304
|
+
interface QueueClientMap<TPayload> {
|
|
305
|
+
cloudflare: CloudflareQueueClient<TPayload>;
|
|
306
|
+
local: LocalQueueClient<TPayload>;
|
|
307
|
+
postgres: PostgresQueueClient<TPayload>;
|
|
308
|
+
vercel: VercelQueueClient<TPayload>;
|
|
309
|
+
}
|
|
310
|
+
type QueueClient<P extends QueueDriver = QueueDriver, TPayload = unknown> = QueueClientMap<TPayload>[P];
|
|
311
|
+
/** Discovered Queue Definitions. Generated declarations extend this interface. */
|
|
312
|
+
interface QueueRegistry {}
|
|
313
|
+
type QueueName = Extract<keyof QueueRegistry, string>;
|
|
314
|
+
type QueuePayload<TName extends QueueName> = QueueRegistry[TName] extends {
|
|
315
|
+
handler: (job: infer TJob) => unknown;
|
|
316
|
+
} ? TJob extends QueueJob<infer TPayload> ? TPayload : never : never;
|
|
317
|
+
interface QueueDefinitionRegistry {
|
|
318
|
+
[name: string]: () => Promise<{
|
|
319
|
+
default?: QueueDefinition;
|
|
320
|
+
} | QueueDefinition>;
|
|
321
|
+
}
|
|
322
|
+
interface DiscoveredQueueDefinition {
|
|
323
|
+
/** Plugin that contributed this Definition through `vitehub.queues`, when it was not found in the app. */
|
|
324
|
+
contributor?: string;
|
|
325
|
+
handler: string;
|
|
326
|
+
name: string;
|
|
327
|
+
/** Why the contributing plugin adds this Definition. */
|
|
328
|
+
reason?: string;
|
|
329
|
+
source?: "server-queues" | "vite-plugin-contribution" | "vite-suffix";
|
|
330
|
+
}
|
|
331
|
+
/** The last delivery error of a stored job, as `vitehub queue jobs` shows it. */
|
|
332
|
+
interface QueueDeliveryErrorDetails {
|
|
333
|
+
code?: string;
|
|
334
|
+
details?: Readonly<Record<string, unknown>>;
|
|
335
|
+
docs?: string;
|
|
336
|
+
fix?: string;
|
|
337
|
+
message: string;
|
|
338
|
+
name: string;
|
|
339
|
+
stack?: string;
|
|
340
|
+
}
|
|
341
|
+
/** The delivery rules that the local engine applies. */
|
|
342
|
+
interface LocalQueueDeliveryRules {
|
|
343
|
+
/** Deliveries before a failing job becomes a dead job. `Infinity` retries until the job expires. */
|
|
344
|
+
maxAttempts: number;
|
|
345
|
+
/** Largest `delay`, in seconds, that a send accepts. */
|
|
346
|
+
maxDelaySeconds?: number;
|
|
347
|
+
/** Largest number of jobs in one `sendBatch()`. */
|
|
348
|
+
maxBatchJobs?: number;
|
|
349
|
+
/** Largest JSON size in bytes of all payloads of one `sendBatch()`. */
|
|
350
|
+
maxBatchBytes?: number;
|
|
351
|
+
/** Largest JSON size in bytes of one payload. */
|
|
352
|
+
maxPayloadBytes?: number;
|
|
353
|
+
/** Accepted `retention`, in seconds. A job expires at the end of its retention, whether it ran or not. */
|
|
354
|
+
retention?: {
|
|
355
|
+
defaultSeconds: number;
|
|
356
|
+
maxSeconds: number;
|
|
357
|
+
minSeconds: number;
|
|
358
|
+
};
|
|
359
|
+
/** Milliseconds before the next delivery after `attempts` failed deliveries. */
|
|
360
|
+
retryDelayMs: (attempts: number) => number;
|
|
361
|
+
/** The provider whose documented rules these are. */
|
|
362
|
+
rules: LocalQueueRules;
|
|
363
|
+
/** Enqueue options that fail with `QUEUE_UNSUPPORTED_OPTION`. */
|
|
364
|
+
unsupportedOptions: readonly ("contentType" | "idempotencyKey" | "region" | "retention")[];
|
|
365
|
+
}
|
|
366
|
+
interface LocalQueueStatus {
|
|
367
|
+
/** Absolute directory of the local provider, or the Postgres table and database, without the password. */
|
|
368
|
+
base: string;
|
|
369
|
+
/** Dead jobs kept per Queue Definition. Older dead jobs are deleted. */
|
|
370
|
+
deadLimit: number;
|
|
371
|
+
/**
|
|
372
|
+
* `single-host`: the jobs survive a restart of this host. Another host or a new container does not see them.
|
|
373
|
+
* `shared`: the postgres provider keeps the jobs in Postgres, and every instance of the app takes jobs from it.
|
|
374
|
+
*/
|
|
375
|
+
durability: "shared" | "single-host";
|
|
376
|
+
queues: Record<string, {
|
|
377
|
+
dead: number;
|
|
378
|
+
pending: number;
|
|
379
|
+
running: number;
|
|
380
|
+
}>;
|
|
381
|
+
}
|
|
382
|
+
/** One stored job, as `vitehub queue jobs list` shows it. Times are ISO strings. */
|
|
383
|
+
interface LocalQueueJob {
|
|
384
|
+
attempts: number;
|
|
385
|
+
/** When a pending job runs next. */
|
|
386
|
+
availableAt: string;
|
|
387
|
+
enqueuedAt: string;
|
|
388
|
+
/** The last delivery error. A pending job has one after a failed attempt. */
|
|
389
|
+
error?: QueueDeliveryErrorDetails;
|
|
390
|
+
/** When the job expires, for Vercel rules. */
|
|
391
|
+
expiresAt?: string;
|
|
392
|
+
/** When the job became dead. */
|
|
393
|
+
failedAt?: string;
|
|
394
|
+
/** The message ID that `runQueue()` returned. */
|
|
395
|
+
id: string;
|
|
396
|
+
queue: string;
|
|
397
|
+
state: "dead" | "pending" | "running";
|
|
398
|
+
}
|
|
399
|
+
/** One stored job with its payload, as `vitehub queue jobs show` shows it. */
|
|
400
|
+
interface LocalQueueJobDetail extends LocalQueueJob {
|
|
401
|
+
/** The local provider only: the file of the job. */
|
|
402
|
+
file?: string;
|
|
403
|
+
idempotencyKey?: string;
|
|
404
|
+
payload?: unknown;
|
|
405
|
+
}
|
|
406
|
+
/** Which jobs `jobs()` lists. */
|
|
407
|
+
interface QueueJobsFilter {
|
|
408
|
+
queue?: string;
|
|
409
|
+
state?: LocalQueueJob["state"];
|
|
410
|
+
}
|
|
411
|
+
/** Which jobs `clear()` removes. Running jobs always stay. */
|
|
412
|
+
interface LocalQueueClearFilter {
|
|
413
|
+
queue?: string;
|
|
414
|
+
state?: "dead" | "pending";
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* The stored jobs of a provider that ViteHub can read: the local and the postgres provider. The Queue operations,
|
|
418
|
+
* so `vitehub queue jobs` and the Console, read jobs through it.
|
|
419
|
+
*/
|
|
420
|
+
interface QueueJobStore {
|
|
421
|
+
/** The directory of the local provider, or the Postgres table and database, without the password. */
|
|
422
|
+
readonly base: string;
|
|
423
|
+
/** Removes the stored jobs that match, except running jobs. Returns how many it removed. */
|
|
424
|
+
clear: (filter?: LocalQueueClearFilter) => Promise<number>;
|
|
425
|
+
/** The newest stored job with this message ID, with its payload. */
|
|
426
|
+
job: (id: string) => Promise<LocalQueueJobDetail | undefined>;
|
|
427
|
+
/** The stored jobs, newest first. The postgres provider applies `filter` and returns at most 1,000 jobs. */
|
|
428
|
+
jobs: (filter?: QueueJobsFilter) => Promise<LocalQueueJob[]>;
|
|
429
|
+
readonly provider: "local" | "postgres";
|
|
430
|
+
/**
|
|
431
|
+
* Runs the newest stored job with this message ID now. A dead job gets a new set of attempts. A pending job keeps
|
|
432
|
+
* its attempts and does not wait for its delay or retry delay. A running job is not changed. `duplicate`: a newer
|
|
433
|
+
* live job holds the `idempotencyKey` of the dead job.
|
|
434
|
+
*/
|
|
435
|
+
retry: (id: string) => Promise<LocalQueueJob | "duplicate" | "missing" | "running">;
|
|
436
|
+
readonly rules: LocalQueueDeliveryRules;
|
|
437
|
+
status: () => Promise<LocalQueueStatus>;
|
|
438
|
+
}
|
|
439
|
+
//#endregion
|
|
440
|
+
export { QueueJobStore as A, VercelQueueCallbackOptions as B, QueueDefinitionRegistry as C, QueueErrorCode as D, QueueEnqueueOptions as E, QueueRegistry as F, VercelQueueSendBatchMessage as G, VercelQueueErrorCode as H, QueueSendResult as I, VercelQueueSendBatchOptions as K, ResolvedLocalQueueOptions as L, QueueName as M, QueueOptions as N, QueueHandler as O, QueuePayload as P, ResolvedQueueOptions as R, QueueDefinitionOptions as S, QueueDriverOptions as T, VercelQueueRetryDirective as U, VercelQueueClient as V, VercelQueueRetryHandler as W, PostgresQueueClient as _, CloudflareQueueMessageBatch as a, QueueClient as b, LocalQueueClient as c, LocalQueueJob as d, LocalQueueJobDetail as f, NodeQueueDriver as g, LocalQueueStatus as h, CloudflareQueueMessage as i, QueueModuleDriverOptions as j, QueueJob as k, LocalQueueDriverOptions as l, LocalQueueSendBatchMessage as m, CloudflareQueueClient as n, CloudflareQueueSendBatchItem as o, LocalQueueRules as p, VercelQueueSendResult as q, CloudflareQueueErrorCode as r, DiscoveredQueueDefinition as s, CloudflareQueueBatchHandlerOptions as t, LocalQueueErrorCode as u, PostgresQueueDriverOptions as v, QueueDriver as w, QueueDefinition as x, PostgresQueueErrorCode as y, VercelQueueBatchSendResult as z };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { R as ResolvedQueueOptions, T as QueueDriverOptions } from "./types-CiuSIPR0.js";
|
|
2
|
+
//#region src/internal/runtime/types.d.ts
|
|
3
|
+
/** Only the generated development integration enables handler error details. */
|
|
4
|
+
interface QueueDevelopmentOptions {
|
|
5
|
+
development?: boolean;
|
|
6
|
+
}
|
|
7
|
+
type QueueRuntimeOptions = ResolvedQueueOptions & QueueDevelopmentOptions;
|
|
8
|
+
type QueueRuntimeDriverOptions = QueueDriverOptions & QueueDevelopmentOptions;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { QueueRuntimeOptions as n, QueueRuntimeDriverOptions as t };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { a as getQueueRuntimeEvent } from "./state-DLqTqF6V.js";
|
|
2
|
+
//#region src/internal/vercel-region.ts
|
|
3
|
+
function readHeader(headers, name) {
|
|
4
|
+
const get = headers?.get;
|
|
5
|
+
const value = typeof get === "function" ? get.call(headers, name) : headers?.[name];
|
|
6
|
+
if (typeof value === "string") return value || void 0;
|
|
7
|
+
if (Array.isArray(value) && typeof value[0] === "string") return value[0];
|
|
8
|
+
}
|
|
9
|
+
function parseRegionFromVercelId(value) {
|
|
10
|
+
return value?.match(/^([a-z0-9]+)::/i)?.[1]?.toLowerCase();
|
|
11
|
+
}
|
|
12
|
+
function resolveVercelQueueRegion(explicitRegion) {
|
|
13
|
+
const region = explicitRegion || process.env.QUEUE_REGION || process.env.VERCEL_REGION;
|
|
14
|
+
if (region) return {
|
|
15
|
+
region,
|
|
16
|
+
requestScoped: false
|
|
17
|
+
};
|
|
18
|
+
const event = getQueueRuntimeEvent();
|
|
19
|
+
const headers = event?.request?.headers ?? event?.req?.headers;
|
|
20
|
+
return {
|
|
21
|
+
region: readHeader(headers, "ce-vqsregion") || parseRegionFromVercelId(readHeader(headers, "x-vercel-id")),
|
|
22
|
+
requestScoped: event !== void 0
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
export { resolveVercelQueueRegion as t };
|
package/dist/virtual.d.ts
CHANGED
|
@@ -2,3 +2,7 @@ declare module "#vitehub/queue/registry" {
|
|
|
2
2
|
const registry: import("./index.js").QueueDefinitionRegistry
|
|
3
3
|
export default registry
|
|
4
4
|
}
|
|
5
|
+
|
|
6
|
+
declare module "#vitehub/queue/node-client" {
|
|
7
|
+
export const createPostgresQueueRuntimeClient: typeof import("./internal/runtime/postgres-client.js").createPostgresQueueRuntimeClient
|
|
8
|
+
}
|