@vercel/queue 0.0.0-alpha.9 → 0.0.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 +468 -604
- package/dist/index.d.mts +476 -188
- package/dist/index.d.ts +476 -188
- package/dist/index.js +1418 -765
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1394 -757
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -5
package/dist/index.d.mts
CHANGED
|
@@ -1,37 +1,83 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Serializer/Deserializer interface for message payloads
|
|
2
|
+
* Serializer/Deserializer interface for message payloads.
|
|
3
|
+
*
|
|
4
|
+
* Built-in implementations:
|
|
5
|
+
* - `JsonTransport` - JSON serialization (default, buffers entire payload)
|
|
6
|
+
* - `BufferTransport` - Binary data (buffers entire payload)
|
|
7
|
+
* - `StreamTransport` - Pass-through streaming (no buffering, ideal for large payloads)
|
|
3
8
|
*/
|
|
4
9
|
interface Transport<T = unknown> {
|
|
5
10
|
/**
|
|
6
|
-
* Serialize a value to a buffer or stream for transmission
|
|
11
|
+
* Serialize a value to a buffer or stream for transmission.
|
|
12
|
+
* The result is sent as the request body with the `contentType` header.
|
|
7
13
|
*/
|
|
8
14
|
serialize(value: T): Buffer | ReadableStream<Uint8Array>;
|
|
9
15
|
/**
|
|
10
|
-
* Deserialize a readable stream back to the original value
|
|
16
|
+
* Deserialize a readable stream back to the original value.
|
|
17
|
+
* Called when receiving messages from the queue.
|
|
11
18
|
*/
|
|
12
19
|
deserialize(stream: ReadableStream<Uint8Array>): Promise<T>;
|
|
13
20
|
/**
|
|
14
|
-
* Optional cleanup method for deserialized payloads that may contain resources
|
|
15
|
-
*
|
|
21
|
+
* Optional cleanup method for deserialized payloads that may contain resources.
|
|
22
|
+
* Called automatically by ConsumerGroup on error; manual cleanup required for direct client usage.
|
|
16
23
|
* @param payload The deserialized payload to clean up
|
|
17
24
|
*/
|
|
18
25
|
finalize?(payload: T): Promise<void>;
|
|
19
26
|
/**
|
|
20
|
-
* MIME type for this serialization format
|
|
27
|
+
* MIME type for this serialization format.
|
|
28
|
+
* Sent as the Content-Type header when publishing messages.
|
|
21
29
|
*/
|
|
22
30
|
contentType: string;
|
|
23
31
|
}
|
|
24
32
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
33
|
+
* JSON serializer/deserializer (default transport).
|
|
34
|
+
*
|
|
35
|
+
* Ideal for structured data. Buffers the entire payload in memory for parsing.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* // Default (JsonTransport is used automatically)
|
|
40
|
+
* const queue = new QueueClient({ region: process.env.QUEUE_REGION! });
|
|
41
|
+
* await queue.send("topic", { data: "example" });
|
|
42
|
+
*
|
|
43
|
+
* // With custom serialization
|
|
44
|
+
* const queue = new QueueClient({
|
|
45
|
+
* region: process.env.QUEUE_REGION!,
|
|
46
|
+
* transport: new JsonTransport({
|
|
47
|
+
* replacer: (key, value) => key === "password" ? undefined : value,
|
|
48
|
+
* reviver: (key, value) => key === "date" ? new Date(value) : value,
|
|
49
|
+
* }),
|
|
50
|
+
* });
|
|
51
|
+
* await queue.send("topic", { data: "example" });
|
|
52
|
+
* ```
|
|
27
53
|
*/
|
|
28
54
|
declare class JsonTransport<T = unknown> implements Transport<T> {
|
|
29
55
|
readonly contentType = "application/json";
|
|
56
|
+
readonly replacer?: Parameters<typeof JSON.stringify>[1];
|
|
57
|
+
readonly reviver?: Parameters<typeof JSON.parse>[1];
|
|
58
|
+
/**
|
|
59
|
+
* Create a new JsonTransport.
|
|
60
|
+
* @param options - Optional JSON serialization options
|
|
61
|
+
* @param options.replacer - Custom replacer for JSON.stringify
|
|
62
|
+
* @param options.reviver - Custom reviver for JSON.parse
|
|
63
|
+
*/
|
|
64
|
+
constructor(options?: {
|
|
65
|
+
replacer?: Parameters<typeof JSON.stringify>[1];
|
|
66
|
+
reviver?: Parameters<typeof JSON.parse>[1];
|
|
67
|
+
});
|
|
30
68
|
serialize(value: T): Buffer;
|
|
31
69
|
deserialize(stream: ReadableStream<Uint8Array>): Promise<T>;
|
|
32
70
|
}
|
|
33
71
|
/**
|
|
34
|
-
*
|
|
72
|
+
* Binary data serializer/deserializer.
|
|
73
|
+
*
|
|
74
|
+
* Ideal for binary data that fits in memory. Buffers the entire payload.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const queue = new QueueClient({ region: "iad1", transport: new BufferTransport() });
|
|
79
|
+
* await queue.send("binary-topic", myBuffer);
|
|
80
|
+
* ```
|
|
35
81
|
*/
|
|
36
82
|
declare class BufferTransport implements Transport<Buffer> {
|
|
37
83
|
readonly contentType = "application/octet-stream";
|
|
@@ -39,31 +85,36 @@ declare class BufferTransport implements Transport<Buffer> {
|
|
|
39
85
|
deserialize(stream: ReadableStream<Uint8Array>): Promise<Buffer>;
|
|
40
86
|
}
|
|
41
87
|
/**
|
|
42
|
-
*
|
|
43
|
-
*
|
|
88
|
+
* Pass-through stream serializer/deserializer.
|
|
89
|
+
*
|
|
90
|
+
* Ideal for large files and real-time streams. Does not buffer data in memory.
|
|
44
91
|
*
|
|
45
|
-
*
|
|
46
|
-
* processing
|
|
47
|
-
*
|
|
92
|
+
* **Important:** When using StreamTransport, you must call `finalize(payload)` when done
|
|
93
|
+
* processing to prevent resource leaks. ConsumerGroup handles this automatically;
|
|
94
|
+
* direct client usage requires manual cleanup.
|
|
48
95
|
*
|
|
49
|
-
*
|
|
96
|
+
* @example
|
|
50
97
|
* ```typescript
|
|
51
|
-
* const
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* //
|
|
60
|
-
*
|
|
61
|
-
*
|
|
98
|
+
* const queue = new QueueClient({ region: "iad1", transport: new StreamTransport() });
|
|
99
|
+
*
|
|
100
|
+
* // Sending a stream
|
|
101
|
+
* await queue.send("large-file", myReadableStream);
|
|
102
|
+
*
|
|
103
|
+
* // Receiving - cleanup handled automatically
|
|
104
|
+
* await queue.receive("large-file", "processor", async (stream, meta) => {
|
|
105
|
+
* const reader = stream.getReader();
|
|
106
|
+
* // Process chunks...
|
|
107
|
+
* });
|
|
108
|
+
* ```
|
|
62
109
|
*/
|
|
63
110
|
declare class StreamTransport implements Transport<ReadableStream<Uint8Array>> {
|
|
64
111
|
readonly contentType = "application/octet-stream";
|
|
65
112
|
serialize(value: ReadableStream<Uint8Array>): ReadableStream<Uint8Array>;
|
|
66
113
|
deserialize(stream: ReadableStream<Uint8Array>): Promise<ReadableStream<Uint8Array>>;
|
|
114
|
+
/**
|
|
115
|
+
* Consume any remaining stream data to prevent resource leaks.
|
|
116
|
+
* Called automatically by ConsumerGroup; manual call required for direct client usage.
|
|
117
|
+
*/
|
|
67
118
|
finalize(payload: ReadableStream<Uint8Array>): Promise<void>;
|
|
68
119
|
}
|
|
69
120
|
|
|
@@ -72,120 +123,271 @@ declare class StreamTransport implements Transport<ReadableStream<Uint8Array>> {
|
|
|
72
123
|
*/
|
|
73
124
|
|
|
74
125
|
/**
|
|
75
|
-
*
|
|
126
|
+
* Vercel region code. The 20 known codes match
|
|
127
|
+
* {@link https://dcs.vercel-infra.com/ | dcs.vercel-infra.com}.
|
|
128
|
+
* Arbitrary strings are also accepted for forward compatibility
|
|
129
|
+
* with regions added after this SDK version.
|
|
76
130
|
*/
|
|
77
|
-
|
|
131
|
+
type VercelRegion = "arn1" | "bom1" | "cdg1" | "cle1" | "cpt1" | "dub1" | "dxb1" | "fra1" | "gru1" | "hkg1" | "hnd1" | "iad1" | "icn1" | "kix1" | "lhr1" | "pdx1" | "sfo1" | "sin1" | "syd1" | "yul1" | (string & {});
|
|
132
|
+
/**
|
|
133
|
+
* Resolves a region code to a base URL for the Vercel Queue Service API.
|
|
134
|
+
*
|
|
135
|
+
* Default: `` (region) => `https://${region}.vercel-queue.com` ``
|
|
136
|
+
*/
|
|
137
|
+
type BaseUrlResolver = (region: string) => string;
|
|
138
|
+
interface QueueClientOptions {
|
|
139
|
+
/**
|
|
140
|
+
* Vercel region code for API routing.
|
|
141
|
+
*
|
|
142
|
+
* Requests are sent to the regional endpoint resolved by
|
|
143
|
+
* {@link BaseUrlResolver} (default: `https://${region}.vercel-queue.com`).
|
|
144
|
+
*
|
|
145
|
+
* Use a `QUEUE_REGION` env var set to `${VERCEL_REGION}` in production
|
|
146
|
+
* and a fixed region (e.g. `"iad1"`) in development.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```ts
|
|
150
|
+
* new QueueClient({ region: process.env.QUEUE_REGION! })
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
region: VercelRegion;
|
|
154
|
+
/**
|
|
155
|
+
* Custom resolver that maps a region code to a base URL.
|
|
156
|
+
* @default (region) => `https://${region}.vercel-queue.com`
|
|
157
|
+
*/
|
|
158
|
+
resolveBaseUrl?: BaseUrlResolver;
|
|
78
159
|
/**
|
|
79
|
-
*
|
|
80
|
-
*
|
|
160
|
+
* Authentication token for the Vercel Queue Service API.
|
|
161
|
+
* If not provided, the client will attempt to get a token via OIDC
|
|
162
|
+
* when running in a Vercel Function environment.
|
|
163
|
+
*/
|
|
164
|
+
token?: string;
|
|
165
|
+
/**
|
|
166
|
+
* Custom headers to include in all requests.
|
|
167
|
+
*/
|
|
168
|
+
headers?: Record<string, string>;
|
|
169
|
+
/**
|
|
170
|
+
* Deployment ID for message routing and lease validation.
|
|
171
|
+
*
|
|
172
|
+
* - `undefined` (default): auto-detect from `VERCEL_DEPLOYMENT_ID` env var;
|
|
173
|
+
* sent messages are pinned to this deployment.
|
|
174
|
+
* - `null`: explicitly unpinned — no deployment ID is sent on any request.
|
|
175
|
+
* - `"dpl_xxx"`: explicit value used for both send pinning and consume identification.
|
|
176
|
+
*
|
|
177
|
+
* Ignored in development mode (deployment ID is never sent locally).
|
|
178
|
+
*/
|
|
179
|
+
deploymentId?: string | null;
|
|
180
|
+
/**
|
|
181
|
+
* Serializer/deserializer for message payloads.
|
|
182
|
+
* Used for all send and receive operations.
|
|
183
|
+
* @default JsonTransport
|
|
184
|
+
*/
|
|
185
|
+
transport?: Transport;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Options for sending messages.
|
|
189
|
+
*/
|
|
190
|
+
interface SendOptions {
|
|
191
|
+
/**
|
|
192
|
+
* Unique key to prevent duplicate message submissions.
|
|
193
|
+
* Deduplication window: `min(message retention, 24 hours)`.
|
|
81
194
|
*/
|
|
82
195
|
idempotencyKey?: string;
|
|
83
196
|
/**
|
|
84
|
-
* Message retention time in seconds
|
|
197
|
+
* Message retention time in seconds. After this period, the message expires.
|
|
85
198
|
* @default 86400 (24 hours)
|
|
86
|
-
* @
|
|
87
|
-
* @
|
|
199
|
+
* @minimum 60 (1 minute)
|
|
200
|
+
* @maximum 86400 (24 hours)
|
|
88
201
|
*/
|
|
89
202
|
retentionSeconds?: number;
|
|
90
|
-
}
|
|
91
|
-
interface SendMessageOptions<T = unknown> extends PublishOptions {
|
|
92
203
|
/**
|
|
93
|
-
*
|
|
204
|
+
* Delay delivery of the message by this many seconds.
|
|
205
|
+
* The message will not be visible to consumers until the delay has passed.
|
|
206
|
+
* @default 0
|
|
207
|
+
* @minimum 0
|
|
208
|
+
* @maximum Value of retentionSeconds (delay cannot exceed retention)
|
|
94
209
|
*/
|
|
95
|
-
|
|
210
|
+
delaySeconds?: number;
|
|
96
211
|
/**
|
|
97
|
-
*
|
|
212
|
+
* Custom headers to include with this message.
|
|
213
|
+
* These headers are passed through to the VQS server.
|
|
98
214
|
*/
|
|
99
|
-
|
|
215
|
+
headers?: Record<string, string>;
|
|
100
216
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
217
|
+
/**
|
|
218
|
+
* Result returned by `send()`.
|
|
219
|
+
*
|
|
220
|
+
* `messageId` is `null` when the server accepted the message for deferred
|
|
221
|
+
* processing (e.g. during a server-side outage) and no ID is available yet.
|
|
222
|
+
*/
|
|
223
|
+
interface SendResult {
|
|
224
|
+
messageId: string | null;
|
|
106
225
|
}
|
|
107
226
|
interface Message<T = unknown> {
|
|
108
227
|
/**
|
|
109
|
-
*
|
|
228
|
+
* Unique message identifier. Used for receive-by-ID operations.
|
|
110
229
|
*/
|
|
111
230
|
messageId: string;
|
|
112
231
|
/**
|
|
113
|
-
* The deserialized message payload
|
|
114
|
-
* Note: If using
|
|
115
|
-
* when done processing, especially in error cases
|
|
232
|
+
* The deserialized message payload.
|
|
233
|
+
* Note: If using StreamTransport, ensure proper cleanup by calling
|
|
234
|
+
* `transport.finalize(payload)` when done processing, especially in error cases.
|
|
116
235
|
*/
|
|
117
236
|
payload: T;
|
|
118
237
|
/**
|
|
119
|
-
* Number of times this message has been delivered
|
|
238
|
+
* Number of times this message has been delivered.
|
|
239
|
+
* Starts at 1 for the first delivery, increments on each retry.
|
|
120
240
|
*/
|
|
121
241
|
deliveryCount: number;
|
|
122
242
|
/**
|
|
123
|
-
* Timestamp when the message was created
|
|
243
|
+
* Timestamp when the message was created/published.
|
|
124
244
|
*/
|
|
125
|
-
|
|
245
|
+
createdAt: Date;
|
|
126
246
|
/**
|
|
127
|
-
*
|
|
247
|
+
* Timestamp when the message expires.
|
|
248
|
+
* Only present for messages delivered via the v2beta binary callback path.
|
|
128
249
|
*/
|
|
129
|
-
|
|
250
|
+
expiresAt?: Date;
|
|
130
251
|
/**
|
|
131
|
-
*
|
|
252
|
+
* MIME type of the message content.
|
|
253
|
+
* Set by the transport's `contentType` property during publish.
|
|
132
254
|
*/
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Result indicating the message should be timed out for retry later
|
|
137
|
-
*/
|
|
138
|
-
interface MessageTimeoutResult {
|
|
255
|
+
contentType: string;
|
|
139
256
|
/**
|
|
140
|
-
*
|
|
257
|
+
* Receipt handle (lease token) for this message delivery.
|
|
258
|
+
* Required for acknowledge and visibility extension operations.
|
|
259
|
+
* Valid only until the visibility timeout expires.
|
|
141
260
|
*/
|
|
142
|
-
|
|
261
|
+
receiptHandle: string;
|
|
143
262
|
}
|
|
144
|
-
/**
|
|
145
|
-
* Result returned by message handlers
|
|
146
|
-
*/
|
|
147
|
-
type MessageHandlerResult = void | MessageTimeoutResult;
|
|
148
263
|
/**
|
|
149
264
|
* Message metadata provided to handlers
|
|
150
265
|
*/
|
|
151
266
|
interface MessageMetadata {
|
|
152
267
|
messageId: string;
|
|
153
268
|
deliveryCount: number;
|
|
154
|
-
|
|
269
|
+
createdAt: Date;
|
|
270
|
+
expiresAt?: Date;
|
|
271
|
+
topicName: string;
|
|
272
|
+
consumerGroup: string;
|
|
273
|
+
/** Vercel region the client is targeting. */
|
|
274
|
+
region: string;
|
|
155
275
|
}
|
|
156
276
|
/**
|
|
157
|
-
*
|
|
277
|
+
* Instruction returned by a {@link RetryHandler} to control what happens
|
|
278
|
+
* to a message when the handler throws.
|
|
279
|
+
*
|
|
280
|
+
* - `{ afterSeconds: N }` — reschedule the message for redelivery after N seconds
|
|
281
|
+
* - `{ acknowledge: true }` — acknowledge the message so it is never retried
|
|
282
|
+
*/
|
|
283
|
+
type RetryDirective = {
|
|
284
|
+
afterSeconds: number;
|
|
285
|
+
} | {
|
|
286
|
+
acknowledge: true;
|
|
287
|
+
};
|
|
288
|
+
/**
|
|
289
|
+
* Called when the message handler throws an error.
|
|
290
|
+
* Return a {@link RetryDirective} to reschedule or acknowledge the message,
|
|
291
|
+
* or return `undefined` to let the error propagate normally.
|
|
292
|
+
*/
|
|
293
|
+
type RetryHandler = (error: unknown, metadata: MessageMetadata) => RetryDirective | void | undefined;
|
|
294
|
+
/**
|
|
295
|
+
* Message handler function type.
|
|
296
|
+
*
|
|
297
|
+
* Called once per message with the deserialized payload and metadata.
|
|
298
|
+
* Not called when the queue is empty — check the return value of `receive()` instead.
|
|
299
|
+
*/
|
|
300
|
+
type MessageHandler<T = unknown> = (message: T, metadata: MessageMetadata) => Promise<void> | void;
|
|
301
|
+
/**
|
|
302
|
+
* Result returned by `receive()`. Use `ok` to check if messages were processed.
|
|
303
|
+
*
|
|
304
|
+
* @example
|
|
305
|
+
* ```typescript
|
|
306
|
+
* const result = await receive("topic", "group", handler);
|
|
307
|
+
* if (result.ok) {
|
|
308
|
+
* // Messages were processed successfully
|
|
309
|
+
* } else if (result.reason === "empty") {
|
|
310
|
+
* // Queue had no messages available
|
|
311
|
+
* }
|
|
312
|
+
* ```
|
|
313
|
+
*/
|
|
314
|
+
type ReceiveResult = {
|
|
315
|
+
ok: true;
|
|
316
|
+
} | {
|
|
317
|
+
ok: false;
|
|
318
|
+
reason: "empty";
|
|
319
|
+
} | {
|
|
320
|
+
ok: false;
|
|
321
|
+
reason: "not_found";
|
|
322
|
+
messageId: string;
|
|
323
|
+
} | {
|
|
324
|
+
ok: false;
|
|
325
|
+
reason: "not_available";
|
|
326
|
+
messageId: string;
|
|
327
|
+
} | {
|
|
328
|
+
ok: false;
|
|
329
|
+
reason: "already_processed";
|
|
330
|
+
messageId: string;
|
|
331
|
+
};
|
|
332
|
+
/**
|
|
333
|
+
* Options for receiving a specific message by ID.
|
|
158
334
|
*/
|
|
159
|
-
|
|
335
|
+
interface ReceiveByIdOptions {
|
|
336
|
+
/** The specific message ID to consume */
|
|
337
|
+
messageId: string;
|
|
338
|
+
/**
|
|
339
|
+
* Message lock duration in seconds.
|
|
340
|
+
* @default 300 (5 minutes)
|
|
341
|
+
* @minimum 30
|
|
342
|
+
* @maximum 3600 (1 hour)
|
|
343
|
+
*/
|
|
344
|
+
visibilityTimeoutSeconds?: number;
|
|
345
|
+
/**
|
|
346
|
+
* Called when the handler throws. Return `{ afterSeconds: N }` to reschedule
|
|
347
|
+
* the message for redelivery after N seconds, or return `undefined`
|
|
348
|
+
* to let the error propagate normally.
|
|
349
|
+
*/
|
|
350
|
+
retry?: RetryHandler;
|
|
351
|
+
}
|
|
160
352
|
/**
|
|
161
|
-
* Options for
|
|
353
|
+
* Options for receiving messages from the queue with an optional batch limit.
|
|
162
354
|
*/
|
|
163
|
-
interface
|
|
355
|
+
interface ReceiveBatchOptions {
|
|
164
356
|
/**
|
|
165
|
-
*
|
|
166
|
-
* @default
|
|
357
|
+
* Maximum number of messages to retrieve in a single request.
|
|
358
|
+
* @default 1
|
|
359
|
+
* @minimum 1
|
|
360
|
+
* @maximum 10
|
|
167
361
|
*/
|
|
168
|
-
|
|
362
|
+
limit?: number;
|
|
169
363
|
/**
|
|
170
|
-
*
|
|
171
|
-
* @default
|
|
364
|
+
* Message lock duration in seconds.
|
|
365
|
+
* @default 300 (5 minutes)
|
|
366
|
+
* @minimum 30
|
|
367
|
+
* @maximum 3600 (1 hour)
|
|
172
368
|
*/
|
|
173
369
|
visibilityTimeoutSeconds?: number;
|
|
174
370
|
/**
|
|
175
|
-
*
|
|
176
|
-
*
|
|
371
|
+
* Called when the handler throws. Return `{ afterSeconds: N }` to reschedule
|
|
372
|
+
* the message for redelivery after N seconds, or return `undefined`
|
|
373
|
+
* to let the error propagate normally.
|
|
177
374
|
*/
|
|
178
|
-
|
|
375
|
+
retry?: RetryHandler;
|
|
179
376
|
}
|
|
180
377
|
/**
|
|
181
|
-
*
|
|
378
|
+
* Options for the receive method. Either receive by message ID or receive
|
|
379
|
+
* from the queue with an optional limit. These options are mutually exclusive.
|
|
380
|
+
*/
|
|
381
|
+
type ReceiveOptions = ReceiveByIdOptions | ReceiveBatchOptions;
|
|
382
|
+
/**
|
|
383
|
+
* Error thrown when a message does not exist or has expired.
|
|
182
384
|
*/
|
|
183
385
|
declare class MessageNotFoundError extends Error {
|
|
184
386
|
constructor(messageId: string);
|
|
185
387
|
}
|
|
186
388
|
/**
|
|
187
|
-
* Error thrown when a message
|
|
188
|
-
* This can happen when the message is in the wrong state
|
|
389
|
+
* Error thrown when a message exists but cannot be processed.
|
|
390
|
+
* This can happen when the message is in the wrong state or already claimed by another consumer.
|
|
189
391
|
*/
|
|
190
392
|
declare class MessageNotAvailableError extends Error {
|
|
191
393
|
constructor(messageId: string, reason?: string);
|
|
@@ -197,166 +399,252 @@ declare class MessageCorruptedError extends Error {
|
|
|
197
399
|
constructor(messageId: string, reason: string);
|
|
198
400
|
}
|
|
199
401
|
/**
|
|
200
|
-
* Error thrown when there are no messages available in the queue
|
|
402
|
+
* Error thrown when there are no messages available in the queue.
|
|
201
403
|
*/
|
|
202
404
|
declare class QueueEmptyError extends Error {
|
|
203
405
|
constructor(queueName: string, consumerGroup: string);
|
|
204
406
|
}
|
|
205
407
|
/**
|
|
206
|
-
* Error thrown when a message is temporarily locked
|
|
408
|
+
* Error thrown when a message is temporarily locked by another consumer.
|
|
409
|
+
* The message is currently being processed and cannot be claimed.
|
|
207
410
|
*/
|
|
208
411
|
declare class MessageLockedError extends Error {
|
|
412
|
+
/** Suggested retry delay in seconds, if provided by the server. */
|
|
209
413
|
readonly retryAfter?: number;
|
|
210
414
|
constructor(messageId: string, retryAfter?: number);
|
|
211
415
|
}
|
|
212
416
|
/**
|
|
213
|
-
* Error thrown when authentication fails
|
|
417
|
+
* Error thrown when authentication fails.
|
|
418
|
+
* This typically means the token is missing, invalid, or expired.
|
|
214
419
|
*/
|
|
215
420
|
declare class UnauthorizedError extends Error {
|
|
216
421
|
constructor(message?: string);
|
|
217
422
|
}
|
|
218
423
|
/**
|
|
219
|
-
* Error thrown when access is forbidden
|
|
424
|
+
* Error thrown when access is forbidden.
|
|
425
|
+
* This typically means the queue belongs to a different environment or project.
|
|
220
426
|
*/
|
|
221
427
|
declare class ForbiddenError extends Error {
|
|
222
428
|
constructor(message?: string);
|
|
223
429
|
}
|
|
224
430
|
/**
|
|
225
|
-
* Error thrown
|
|
431
|
+
* Error thrown when request parameters are invalid.
|
|
226
432
|
*/
|
|
227
433
|
declare class BadRequestError extends Error {
|
|
228
434
|
constructor(message: string);
|
|
229
435
|
}
|
|
230
436
|
/**
|
|
231
|
-
* Error thrown
|
|
437
|
+
* Error thrown when the server encounters an unexpected error.
|
|
232
438
|
*/
|
|
233
439
|
declare class InternalServerError extends Error {
|
|
234
440
|
constructor(message?: string);
|
|
235
441
|
}
|
|
236
442
|
/**
|
|
237
|
-
* Error thrown when batch limit parameter is
|
|
443
|
+
* Error thrown when the batch limit parameter is outside the valid range.
|
|
444
|
+
* The limit must be between 1 and 10 (inclusive).
|
|
238
445
|
*/
|
|
239
446
|
declare class InvalidLimitError extends Error {
|
|
240
447
|
constructor(limit: number, min?: number, max?: number);
|
|
241
448
|
}
|
|
242
|
-
|
|
243
449
|
/**
|
|
244
|
-
*
|
|
450
|
+
* Error thrown when attempting to process a message that has already been successfully processed.
|
|
245
451
|
*/
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
messageId?: string;
|
|
249
|
-
/** Whether to skip downloading the payload (only allowed when messageId is provided) */
|
|
250
|
-
skipPayload?: boolean;
|
|
452
|
+
declare class MessageAlreadyProcessedError extends Error {
|
|
453
|
+
constructor(messageId: string);
|
|
251
454
|
}
|
|
252
|
-
|
|
253
455
|
/**
|
|
254
|
-
*
|
|
456
|
+
* Error thrown when a duplicate idempotency key is detected.
|
|
457
|
+
* The message was already sent within the deduplication window.
|
|
255
458
|
*/
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
* @default JsonTransport instance
|
|
260
|
-
*/
|
|
261
|
-
transport?: Transport<T>;
|
|
459
|
+
declare class DuplicateMessageError extends Error {
|
|
460
|
+
readonly idempotencyKey?: string;
|
|
461
|
+
constructor(message: string, idempotencyKey?: string);
|
|
262
462
|
}
|
|
263
463
|
/**
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
* @throws {UnauthorizedError} When authentication fails
|
|
272
|
-
* @throws {ForbiddenError} When access is denied (environment mismatch)
|
|
273
|
-
* @throws {InternalServerError} When server encounters an error
|
|
274
|
-
*/
|
|
275
|
-
declare function send<T = unknown>(topicName: string, payload: T, options?: SendOptions<T>): Promise<{
|
|
276
|
-
messageId: string;
|
|
277
|
-
}>;
|
|
464
|
+
* Error thrown when consumer discovery fails.
|
|
465
|
+
* This typically means the deployment could not be reached or is not configured correctly.
|
|
466
|
+
*/
|
|
467
|
+
declare class ConsumerDiscoveryError extends Error {
|
|
468
|
+
readonly deploymentId?: string;
|
|
469
|
+
constructor(message: string, deploymentId?: string);
|
|
470
|
+
}
|
|
278
471
|
/**
|
|
279
|
-
*
|
|
472
|
+
* Error thrown when the consumer registry is not configured for the project.
|
|
280
473
|
*/
|
|
281
|
-
|
|
474
|
+
declare class ConsumerRegistryNotConfiguredError extends Error {
|
|
475
|
+
constructor(message?: string);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
declare class QueueClient {
|
|
479
|
+
constructor(options: QueueClientOptions);
|
|
480
|
+
/**
|
|
481
|
+
* Send a message to a topic.
|
|
482
|
+
*
|
|
483
|
+
* This is an arrow function property so it can be destructured:
|
|
484
|
+
* ```typescript
|
|
485
|
+
* const { send } = new QueueClient({ region: process.env.QUEUE_REGION! });
|
|
486
|
+
* await send("my-topic", payload);
|
|
487
|
+
* ```
|
|
488
|
+
*
|
|
489
|
+
* @param topicName - Name of the topic (pattern: `[A-Za-z0-9_-]+`)
|
|
490
|
+
* @param payload - The data to send (serialized via the configured transport)
|
|
491
|
+
* @param options - Optional send options (idempotencyKey, retentionSeconds, delaySeconds, headers)
|
|
492
|
+
* @returns `{ messageId }` — `messageId` is `null` when the server accepted
|
|
493
|
+
* the message for deferred processing (no ID available yet)
|
|
494
|
+
*/
|
|
495
|
+
send: <T = unknown>(topicName: string, payload: T, options?: SendOptions) => Promise<SendResult>;
|
|
496
|
+
/**
|
|
497
|
+
* Receive and process messages from a topic.
|
|
498
|
+
*
|
|
499
|
+
* Each message is automatically locked, kept alive via periodic visibility
|
|
500
|
+
* extensions during processing, and acknowledged upon successful handler completion.
|
|
501
|
+
* The handler is not called when the queue is empty — check `result.ok` instead.
|
|
502
|
+
*
|
|
503
|
+
* This is an arrow function property so it can be destructured:
|
|
504
|
+
* ```typescript
|
|
505
|
+
* const { receive } = new QueueClient({ region: process.env.QUEUE_REGION! });
|
|
506
|
+
* const result = await receive("my-topic", "my-group", handler);
|
|
507
|
+
* if (!result.ok) console.log(result.reason);
|
|
508
|
+
* ```
|
|
509
|
+
*
|
|
510
|
+
* @param topicName - Name of the topic (pattern: `[A-Za-z0-9_-]+`)
|
|
511
|
+
* @param consumerGroup - Name of the consumer group (pattern: `[A-Za-z0-9_-]+`)
|
|
512
|
+
* @param handler - Function to process each message payload and metadata.
|
|
513
|
+
* Not called when the queue is empty.
|
|
514
|
+
* @param options - Optional receive options (visibilityTimeoutSeconds, limit, or messageId)
|
|
515
|
+
* @returns Discriminated result: `{ ok: true }` on success, `{ ok: false, reason }` otherwise
|
|
516
|
+
*/
|
|
517
|
+
receive: <T = unknown>(topicName: string, consumerGroup: string, handler: MessageHandler<T>, options?: ReceiveOptions) => Promise<ReceiveResult>;
|
|
518
|
+
/**
|
|
519
|
+
* Create a Web API route handler for processing queue callback messages.
|
|
520
|
+
*
|
|
521
|
+
* Parses incoming `Request` as a CloudEvent and invokes the handler.
|
|
522
|
+
* For use on Vercel — Vercel invokes this route when messages are available.
|
|
523
|
+
*
|
|
524
|
+
* This is an arrow function property so it can be destructured:
|
|
525
|
+
* ```typescript
|
|
526
|
+
* const { handleCallback } = new QueueClient({ region: process.env.QUEUE_REGION! });
|
|
527
|
+
* export const POST = handleCallback(handler);
|
|
528
|
+
* ```
|
|
529
|
+
*
|
|
530
|
+
* @param handler - Function to process the message payload and metadata
|
|
531
|
+
* @param options - Optional configuration
|
|
532
|
+
* @param options.visibilityTimeoutSeconds - Message lock duration (default: 300, max: 3600)
|
|
533
|
+
* @param options.retry - Called when the handler throws. Return `{ afterSeconds: N }` to
|
|
534
|
+
* reschedule the message for redelivery after N seconds.
|
|
535
|
+
* @returns A `(request: Request) => Promise<Response>` route handler
|
|
536
|
+
*/
|
|
537
|
+
handleCallback: <T = unknown>(handler: MessageHandler<T>, options?: {
|
|
538
|
+
visibilityTimeoutSeconds?: number;
|
|
539
|
+
retry?: RetryHandler;
|
|
540
|
+
}) => ((request: Request) => Promise<Response>);
|
|
541
|
+
/**
|
|
542
|
+
* Create a Connect-style route handler for processing queue callback messages.
|
|
543
|
+
* For use on Vercel — Vercel invokes this route when messages are available.
|
|
544
|
+
*
|
|
545
|
+
* For frameworks using the `(req, res)` middleware pattern where `req.body`
|
|
546
|
+
* is pre-parsed (Next.js Pages Router, etc.).
|
|
547
|
+
*
|
|
548
|
+
* This is an arrow function property so it can be destructured:
|
|
549
|
+
* ```typescript
|
|
550
|
+
* const { handleNodeCallback } = new QueueClient({ region: process.env.QUEUE_REGION! });
|
|
551
|
+
* app.post("/api/queue", handleNodeCallback(handler));
|
|
552
|
+
* ```
|
|
553
|
+
*
|
|
554
|
+
* @param handler - Function to process the message payload and metadata
|
|
555
|
+
* @param options - Optional configuration
|
|
556
|
+
* @param options.visibilityTimeoutSeconds - Message lock duration (default: 300, max: 3600)
|
|
557
|
+
* @param options.retry - Called when the handler throws. Return `{ afterSeconds: N }` to
|
|
558
|
+
* reschedule the message for redelivery after N seconds.
|
|
559
|
+
* @returns A `(req, res) => Promise<void>` route handler
|
|
560
|
+
*/
|
|
561
|
+
handleNodeCallback: <T = unknown>(handler: MessageHandler<T>, options?: {
|
|
562
|
+
visibilityTimeoutSeconds?: number;
|
|
563
|
+
retry?: RetryHandler;
|
|
564
|
+
}) => ((req: {
|
|
565
|
+
method?: string;
|
|
566
|
+
headers: Record<string, string | string[] | undefined>;
|
|
567
|
+
body?: unknown;
|
|
568
|
+
}, res: {
|
|
569
|
+
status(code: number): {
|
|
570
|
+
json(data: unknown): void;
|
|
571
|
+
end(): void;
|
|
572
|
+
};
|
|
573
|
+
end(): void;
|
|
574
|
+
}) => Promise<void>);
|
|
282
575
|
}
|
|
576
|
+
|
|
283
577
|
/**
|
|
284
|
-
*
|
|
285
|
-
*
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
*
|
|
289
|
-
|
|
290
|
-
* @throws All the same errors as the underlying client methods
|
|
291
|
-
*/
|
|
292
|
-
declare function receive<T = unknown>(topicName: string, consumerGroup: string, handler: MessageHandler<T>, options?: ReceiveOptions<T>): Promise<void>;
|
|
293
|
-
/**
|
|
294
|
-
* Receive a specific message by its ID with full payload
|
|
295
|
-
* @param topicName Name of the topic to receive from
|
|
296
|
-
* @param consumerGroup Name of the consumer group
|
|
297
|
-
* @param handler Function to process the message
|
|
298
|
-
* @param options Receive options with messageId specified
|
|
299
|
-
* @returns Promise that resolves when the message is processed
|
|
300
|
-
* @throws All the same errors as the underlying client methods
|
|
301
|
-
*/
|
|
302
|
-
declare function receive<T = unknown>(topicName: string, consumerGroup: string, handler: MessageHandler<T>, options: ReceiveOptions<T> & {
|
|
303
|
-
messageId: string;
|
|
304
|
-
skipPayload?: false | undefined;
|
|
305
|
-
}): Promise<void>;
|
|
306
|
-
/**
|
|
307
|
-
* Receive a specific message by its ID without downloading the payload (metadata only)
|
|
308
|
-
* @param topicName Name of the topic to receive from
|
|
309
|
-
* @param consumerGroup Name of the consumer group
|
|
310
|
-
* @param handler Function to process the message metadata (payload will be void)
|
|
311
|
-
* @param options Receive options with messageId and skipPayload specified
|
|
312
|
-
* @returns Promise that resolves when the message is processed
|
|
313
|
-
* @throws All the same errors as the underlying client methods
|
|
314
|
-
*/
|
|
315
|
-
declare function receive<T = unknown>(topicName: string, consumerGroup: string, handler: MessageHandler<void>, options: ReceiveOptions<T> & {
|
|
316
|
-
messageId: string;
|
|
317
|
-
skipPayload: true;
|
|
318
|
-
}): Promise<void>;
|
|
578
|
+
* Core queue callback utilities for handling incoming webhook payloads
|
|
579
|
+
* from Vercel triggers using the CloudEvent specification.
|
|
580
|
+
*
|
|
581
|
+
* This module provides the framework-agnostic core. For framework-specific
|
|
582
|
+
* wrappers, see `@vercel/queue/web` and `@vercel/queue/nextjs/pages`.
|
|
583
|
+
*/
|
|
319
584
|
|
|
585
|
+
declare const CLOUD_EVENT_TYPE_V1BETA = "com.vercel.queue.v1beta";
|
|
586
|
+
declare const CLOUD_EVENT_TYPE_V2BETA = "com.vercel.queue.v2beta";
|
|
320
587
|
/**
|
|
321
|
-
*
|
|
588
|
+
* Routing-only callback: the SDK must fetch the message by ID.
|
|
589
|
+
* Produced by v1beta structured mode and v2beta large-body mode.
|
|
322
590
|
*/
|
|
323
|
-
type
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
591
|
+
type ParsedCallbackV1 = {
|
|
592
|
+
queueName: string;
|
|
593
|
+
consumerGroup: string;
|
|
594
|
+
messageId: string;
|
|
595
|
+
region?: string;
|
|
596
|
+
};
|
|
597
|
+
/**
|
|
598
|
+
* Full-message callback: payload and receipt handle are inlined,
|
|
599
|
+
* so the SDK can process directly without an extra fetch.
|
|
600
|
+
* Produced by v2beta small-body mode.
|
|
601
|
+
*/
|
|
602
|
+
type ParsedCallbackV2 = {
|
|
603
|
+
queueName: string;
|
|
604
|
+
consumerGroup: string;
|
|
605
|
+
messageId: string;
|
|
606
|
+
region?: string;
|
|
607
|
+
receiptHandle: string;
|
|
608
|
+
deliveryCount?: number;
|
|
609
|
+
createdAt?: string;
|
|
610
|
+
expiresAt?: string;
|
|
611
|
+
contentType?: string;
|
|
612
|
+
visibilityDeadline?: string;
|
|
613
|
+
rawBody?: ReadableStream<Uint8Array>;
|
|
614
|
+
parsedPayload?: unknown;
|
|
327
615
|
};
|
|
616
|
+
type ParsedCallbackRequest = ParsedCallbackV1 | ParsedCallbackV2;
|
|
328
617
|
/**
|
|
329
|
-
*
|
|
618
|
+
* Parse a callback from a pre-parsed body and headers.
|
|
330
619
|
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
620
|
+
* For frameworks like Next.js Pages Router where the body has already been
|
|
621
|
+
* parsed, use this instead of {@link parseCallback}.
|
|
333
622
|
*
|
|
334
|
-
*
|
|
335
|
-
*
|
|
623
|
+
* Detects the CloudEvent version from the `ce-type` header:
|
|
624
|
+
* - `com.vercel.queue.v2beta`: binary content mode (metadata in headers,
|
|
625
|
+
* payload in body). For small messages, the body is attached as `parsedPayload`.
|
|
626
|
+
* - Otherwise: structured content mode (v1beta, entire CloudEvent in JSON body)
|
|
336
627
|
*
|
|
337
|
-
* @
|
|
338
|
-
*
|
|
339
|
-
*
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
628
|
+
* @param body - The framework-parsed request body. Type depends on Content-Type
|
|
629
|
+
* and framework configuration:
|
|
630
|
+
* - v1beta: parsed CloudEvent JSON object
|
|
631
|
+
* - v2beta: the message payload as parsed by the framework (object, Buffer, or string)
|
|
632
|
+
* @param headers - HTTP headers
|
|
633
|
+
* @returns Parsed callback request with routing metadata and optional payload
|
|
634
|
+
*/
|
|
635
|
+
declare function parseRawCallback(body: unknown, headers: Record<string, string | string[] | undefined>): ParsedCallbackRequest;
|
|
636
|
+
/**
|
|
637
|
+
* Parse and validate a CloudEvent callback from a Web API `Request` object.
|
|
346
638
|
*
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
*
|
|
351
|
-
*
|
|
352
|
-
*
|
|
353
|
-
*
|
|
354
|
-
*
|
|
355
|
-
* "notifications": (order, metadata) => console.log("Notifying order", order),
|
|
356
|
-
* }
|
|
357
|
-
* });
|
|
358
|
-
* ```
|
|
639
|
+
* Detects the CloudEvent version from the `ce-type` header:
|
|
640
|
+
* - `com.vercel.queue.v2beta`: binary content mode (metadata in headers,
|
|
641
|
+
* payload in body). For v2beta, the body is attached as `rawBody` (a
|
|
642
|
+
* ReadableStream) rather than being parsed.
|
|
643
|
+
* - Otherwise: structured content mode (v1beta, entire CloudEvent in JSON body)
|
|
644
|
+
*
|
|
645
|
+
* For frameworks that pre-parse the body (e.g. Next.js Pages Router),
|
|
646
|
+
* use {@link parseRawCallback} instead.
|
|
359
647
|
*/
|
|
360
|
-
declare function
|
|
648
|
+
declare function parseCallback(request: Request): Promise<ParsedCallbackRequest>;
|
|
361
649
|
|
|
362
|
-
export { BadRequestError, BufferTransport, ForbiddenError, InternalServerError, InvalidLimitError, JsonTransport, type Message, MessageCorruptedError, type MessageHandler,
|
|
650
|
+
export { BadRequestError, type BaseUrlResolver, BufferTransport, CLOUD_EVENT_TYPE_V1BETA, CLOUD_EVENT_TYPE_V2BETA, ConsumerDiscoveryError, ConsumerRegistryNotConfiguredError, DuplicateMessageError, ForbiddenError, InternalServerError, InvalidLimitError, JsonTransport, type Message, MessageAlreadyProcessedError, MessageCorruptedError, type MessageHandler, MessageLockedError, type MessageMetadata, MessageNotAvailableError, MessageNotFoundError, type ParsedCallbackRequest, type ParsedCallbackV1, type ParsedCallbackV2, QueueClient, type QueueClientOptions, QueueEmptyError, type ReceiveBatchOptions, type ReceiveByIdOptions, type ReceiveOptions, type ReceiveResult, type RetryDirective, type RetryHandler, type SendOptions, type SendResult, StreamTransport, type Transport, UnauthorizedError, type VercelRegion, parseCallback, parseRawCallback };
|