@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.
Files changed (60) hide show
  1. package/README.md +152 -29
  2. package/dist/client-B53_G4ky.js +182 -0
  3. package/dist/{cloudflare-client-COzQ8fHk.js → cloudflare-client-CoISDpIx.js} +28 -21
  4. package/dist/{cloudflare-resource-name-CCuLDvSP.js → cloudflare-resource-name-BIfBglC2.js} +1 -1
  5. package/dist/config-B2eaXaT5.js +105 -0
  6. package/dist/durability.d.ts +13 -0
  7. package/dist/durability.js +47 -0
  8. package/dist/duration-tujyhAKt.js +111 -0
  9. package/dist/error-diagnostics-CQ4BUqRe.js +270 -0
  10. package/dist/error-diagnostics-mJXBjmQx.js +181 -0
  11. package/dist/{handler-response-CBwIkmTS.js → handler-response-B6lz9nCn.js} +9 -4
  12. package/dist/index.d.ts +20 -32
  13. package/dist/index.js +39 -17
  14. package/dist/internal/runtime/cloudflare-client.d.ts +2 -2
  15. package/dist/internal/runtime/cloudflare-client.js +1 -1
  16. package/dist/internal/runtime/cloudflare-vite.d.ts +30 -2
  17. package/dist/internal/runtime/cloudflare-vite.js +111 -1
  18. package/dist/internal/runtime/local-client.d.ts +17 -0
  19. package/dist/internal/runtime/local-client.js +71 -0
  20. package/dist/internal/runtime/postgres-client.d.ts +46 -0
  21. package/dist/internal/runtime/postgres-client.js +570 -0
  22. package/dist/internal/runtime/state.d.ts +11 -4
  23. package/dist/internal/runtime/state.js +2 -2
  24. package/dist/internal/runtime/vercel-client.d.ts +2 -2
  25. package/dist/internal/runtime/vercel-client.js +66 -28
  26. package/dist/internal/runtime/worker-client.d.ts +6 -0
  27. package/dist/internal/runtime/worker-client.js +11 -0
  28. package/dist/invariant-D8_xBqmZ.js +22 -0
  29. package/dist/local-CUaWoRUx.d.ts +23 -0
  30. package/dist/local-Di0FrVUw.js +553 -0
  31. package/dist/object-D8pRdprI.js +15 -0
  32. package/dist/{operations-tNUdj3G4.d.ts → operation-definition-nmRu_O3V.d.ts} +42 -15
  33. package/dist/operations-BVV-9Q0N.js +129 -0
  34. package/dist/operations.d.ts +47 -9
  35. package/dist/operations.js +186 -69
  36. package/dist/runtime/hosted.d.ts +3 -5
  37. package/dist/runtime/hosted.js +21 -45
  38. package/dist/{state-G9s378-e.js → state-DLqTqF6V.js} +59 -47
  39. package/dist/types-CiuSIPR0.d.ts +440 -0
  40. package/dist/types-Dsbb_Pvp.d.ts +10 -0
  41. package/dist/vercel-region-B95MAMXN.js +26 -0
  42. package/dist/virtual.d.ts +4 -0
  43. package/dist/vite.d.ts +366 -49
  44. package/dist/vite.js +2071 -2
  45. package/package.json +39 -46
  46. package/dist/client-CIc3jsj2.js +0 -114
  47. package/dist/cloudflare-vite-BCuJOXcT.js +0 -111
  48. package/dist/cloudflare-vite-CjdP5yBN.d.ts +0 -29
  49. package/dist/config-BINujLv4.js +0 -74
  50. package/dist/enqueue-CnQ0QR3O.js +0 -22
  51. package/dist/error-diagnostics-A_8Kp-48.js +0 -116
  52. package/dist/error-diagnostics-qzRF6IVB.js +0 -109
  53. package/dist/errors-DIFaM9tW.js +0 -42
  54. package/dist/nuxt.d.ts +0 -39
  55. package/dist/nuxt.js +0 -34
  56. package/dist/object-B1NW521B.js +0 -7
  57. package/dist/operations-Cnjbz8kk.js +0 -63
  58. package/dist/types-1GRMYES1.d.ts +0 -180
  59. package/dist/vercel-region-cjskrOKl.js +0 -122
  60. package/dist/vite-ChWLzOVu.js +0 -1512
@@ -1,109 +0,0 @@
1
- import { defineErrors } from "@vite-hub/runtime";
2
- //#region src/error-diagnostics.ts
3
- const queueErrorDiagnostics = /*#__PURE__*/ defineErrors({
4
- docs: "https://vitehub.dev/docs/queue/limits-and-errors",
5
- codes: {
6
- QUEUE_PROVIDER_FAILED: {
7
- why: ({ operation, provider }) => `The ${provider} queue provider failed during ${operation}.`,
8
- fix: "Read `error.cause` for the provider error. Retry when the provider is available again. Pass `idempotencyKey` on Vercel so that a retry does not enqueue the job two times."
9
- },
10
- QUEUE_PROVIDER_INVALID_RESPONSE: {
11
- why: "The Vercel queue provider accepted the job, but returned an invalid `messageId`.",
12
- fix: "Do not send the job again without an `idempotencyKey`, because the provider can have it. Check the installed `@vercel/queue` version."
13
- },
14
- QUEUE_UNSUPPORTED_OPTION: {
15
- why: ({ options, provider }) => `The ${provider} queue provider does not support the enqueue option ${options}.`,
16
- fix: "Remove the option for this provider. The enqueue options table on the Queue Server API page shows which provider supports each option."
17
- },
18
- QUEUE_BINDING_NOT_FOUND: {
19
- why: ({ binding }) => binding ? `The Cloudflare queue binding "${binding}" is not in the Worker env.` : "The Cloudflare queue client has no binding.",
20
- fix: "The dev server has no local Queue delivery. Run the built Worker with `wrangler dev`, or deploy it. In a deployed Worker, add the binding to the Worker configuration."
21
- },
22
- QUEUE_DEFINITION_LOAD_FAILED: {
23
- why: ({ queue }) => queue ? `The Queue Definition "${queue}" failed to load.` : "A Queue Definition failed to load.",
24
- fix: "Read `error.cause` for the import error, and fix the Queue Definition module."
25
- },
26
- QUEUE_DEFINITION_NOT_FOUND: {
27
- why: ({ queue }) => queue ? `No Queue Definition has the name "${queue}".` : "No Queue Definition has the requested name.",
28
- fix: "Add `server/queues/<name>.ts` with a default `defineQueue()` export, and build again. The generated server output loads the Queue Definitions, so a script that runs outside of it finds none."
29
- },
30
- QUEUE_DISABLED: {
31
- why: "Queue is disabled in this runtime.",
32
- fix: "Add `hubQueue()` to the Vite config and do not set `queue: false`."
33
- },
34
- QUEUE_INVALID_BINDING: {
35
- why: "The Cloudflare queue binding does not have `send()` and `sendBatch()`.",
36
- fix: "Pass a Cloudflare Queue producer binding, for example `env.WELCOME_QUEUE`."
37
- },
38
- QUEUE_MISSING_REGION: {
39
- why: "The installed `@vercel/queue` needs a region, and ViteHub cannot find one.",
40
- fix: "Set `queue.region` in the Vite config, or set the `QUEUE_REGION` environment variable."
41
- },
42
- QUEUE_MISSING_TOPIC: {
43
- why: "A direct Vercel queue client needs a `topic`.",
44
- fix: "Pass `topic` to `createQueueClient()`, or use `getQueue()` for a discovered Queue Definition."
45
- },
46
- QUEUE_PROVIDER_MISMATCH: {
47
- why: ({ expected, provider }) => `The ${expected} Queue runtime received the ${provider} provider.`,
48
- fix: "Set `queue.provider` to the provider of the deployment preset, and build again."
49
- },
50
- QUEUE_RUNTIME_NOT_INSTALLED: {
51
- why: "The Queue runtime has no client factory. The generated server output installs it.",
52
- fix: "Add `hubQueue()` to the Vite config, build the app, and run the built server."
53
- },
54
- QUEUE_VERCEL_SDK_INVALID: {
55
- why: "`@vercel/queue` does not have the client API that ViteHub uses.",
56
- fix: "Install a supported `@vercel/queue` version, or pass a `client` that has `send()` and `handleCallback()`."
57
- },
58
- QUEUE_VERCEL_SDK_UNAVAILABLE: {
59
- why: "`@vercel/queue` cannot be loaded.",
60
- fix: "Add `@vercel/queue` to the app dependencies. Read `error.cause` for the import error."
61
- },
62
- QUEUE_BINDING_CONFLICT: {
63
- why: "A custom `queue.binding` works with one Queue Definition only, and this app has more.",
64
- fix: "Remove `queue.binding`, so that each Queue Definition gets its own generated binding."
65
- },
66
- QUEUE_CRYPTO_UNAVAILABLE: {
67
- why: "Cloudflare queue names for long Queue Definition names need the `node:crypto` module, and this runtime does not have it.",
68
- fix: "Enable the `nodejs_compat` compatibility flag, or use a shorter Queue Definition name."
69
- },
70
- QUEUE_DEFINITIONS_CHANGED: {
71
- why: ({ context }) => `The ${context} Queue Definitions changed after Vite resolved the config.`,
72
- fix: "Generate Queue Definition files before the Vite config resolves, then build again."
73
- },
74
- QUEUE_INVALID_CONFIG: {
75
- why: "`queue` must be a plain object or `false`.",
76
- fix: "Pass an object, for example `queue: { provider: 'cloudflare' }`."
77
- },
78
- QUEUE_INVALID_DEFINITION: {
79
- why: "`defineQueue()` needs a handler function.",
80
- fix: "Pass the handler as the first argument, for example `defineQueue(async (job) => { ... })`."
81
- },
82
- QUEUE_INVALID_NAME: {
83
- why: ({ name }) => `The Cloudflare queue name "${name}" can contain only letters, numbers, and dashes, and must start and end with a letter or number.`,
84
- fix: "Change `queue.namePrefix` so that it contains only letters, numbers, and dashes."
85
- },
86
- QUEUE_INVALID_PROVIDER: {
87
- why: ({ provider }) => `\`queue.provider\` "${provider}" is not a Queue provider.`,
88
- fix: "Set `queue.provider` to \"cloudflare\" or \"vercel\"."
89
- },
90
- QUEUE_MISSING_PROVIDER: {
91
- why: ({ hosting }) => `ViteHub cannot select a Queue provider for ${hosting} hosting.`,
92
- fix: "Set `queue.provider`, select the Cloudflare or Vercel deployment preset, or set `queue: false`."
93
- },
94
- QUEUE_NAME_COLLISION: {
95
- why: ({ first, resolved, second }) => `The Queue Definitions "${first}" and "${second}" have the same provider name "${resolved}".`,
96
- fix: "Rename one of the two Queue Definition files."
97
- },
98
- QUEUE_PLUGIN_INCOMPATIBLE: {
99
- why: "The `hubQueue()` plugin in the Vite config does not expose the Queue Nitro config.",
100
- fix: "Install one version of `@vite-hub/queue`, and use `hubQueue()` from it."
101
- },
102
- QUEUE_PROVISION_FAILED: {
103
- why: "Cloudflare returned an invalid queue list during provisioning.",
104
- fix: "Run `vitehub provision run --provider cloudflare --dry-run` again, and check the Cloudflare token permissions."
105
- }
106
- }
107
- });
108
- //#endregion
109
- export { queueErrorDiagnostics as t };
@@ -1,42 +0,0 @@
1
- import { t as queueErrorDiagnostics } from "./error-diagnostics-qzRF6IVB.js";
2
- import { isRuntimeDiagnostic } from "@vite-hub/runtime";
3
- //#region src/errors.ts
4
- const cloudflareUnsupportedEnqueueOptions = [
5
- "idempotencyKey",
6
- "region",
7
- "retentionSeconds"
8
- ];
9
- /** Returns `true` for an error from the Queue catalog. Pass `codes` to accept only some codes. */
10
- function isQueueError(error, codes) {
11
- if (!isRuntimeDiagnostic(error) || !Object.hasOwn(queueErrorDiagnostics, error.code)) return false;
12
- return !codes || codes.some((code) => code === error.code);
13
- }
14
- /** Formats option names for an error `why`, for example `"region" and "retentionSeconds"`. */
15
- function formatQueueOptionNames(names) {
16
- const quoted = names.map((name) => `\`${name}\``);
17
- return quoted.length > 1 ? `${quoted.slice(0, -1).join(", ")} and ${quoted.at(-1)}` : quoted.join("");
18
- }
19
- function normalizePublicQueueIdentifier(value) {
20
- if (typeof value !== "string" || !value || value.length > 128 || value.trim() !== value) return;
21
- if (!/^[a-z\d][a-z\d._/-]*$/i.test(value)) return;
22
- if (/^[a-z][a-z\d+.-]*:\/\//i.test(value) || /^[a-z]:\//i.test(value)) return;
23
- if (value.split("/").some((part) => part === "." || part === "..")) return;
24
- return value;
25
- }
26
- /**
27
- * Runs one provider call and returns `[error, data]`.
28
- * Any failure becomes `QUEUE_PROVIDER_FAILED` with the failure in `cause`.
29
- */
30
- async function queueProviderResult(provider, operation, run) {
31
- try {
32
- return [void 0, await run()];
33
- } catch (cause) {
34
- return [queueErrorDiagnostics.QUEUE_PROVIDER_FAILED({
35
- cause,
36
- operation,
37
- provider
38
- }), void 0];
39
- }
40
- }
41
- //#endregion
42
- export { queueProviderResult as a, normalizePublicQueueIdentifier as i, formatQueueOptionNames as n, isQueueError as r, cloudflareUnsupportedEnqueueOptions as t };
package/dist/nuxt.d.ts DELETED
@@ -1,39 +0,0 @@
1
- import { h as QueueModuleOptions } from "./types-1GRMYES1.js";
2
- //#region ../internal/src/build/vite.d.ts
3
- interface NuxtLayerDirectoryConfig {
4
- rootDir?: string;
5
- serverDir?: string;
6
- srcDir?: string;
7
- }
8
- /** The Nuxt options that locate the server directory of each layer. */
9
- interface NuxtServerDirOptions extends NuxtLayerDirectoryConfig {
10
- _layers?: ReadonlyArray<{
11
- config?: NuxtLayerDirectoryConfig;
12
- cwd?: string;
13
- }>;
14
- }
15
- //#endregion
16
- //#region src/nuxt.d.ts
17
- export type QueueNuxtModuleOptions = QueueModuleOptions;
18
- type NuxtLike = {
19
- hook?: {
20
- (name: "nitro:config", handler: (nitroConfig: Record<string, unknown>) => void | Promise<void>): void;
21
- (name: "prepare:types", handler: (context: {
22
- references: {
23
- path: string;
24
- }[];
25
- }) => void): void;
26
- };
27
- options: {
28
- _layers?: NuxtServerDirOptions["_layers"];
29
- dev?: boolean;
30
- rootDir?: string;
31
- serverDir?: string;
32
- srcDir?: string;
33
- vite?: {
34
- plugins?: unknown[];
35
- };
36
- };
37
- };
38
- export default function viteHubQueueNuxtModule(options?: QueueNuxtModuleOptions, nuxt?: NuxtLike): void;
39
- //#endregion
package/dist/nuxt.js DELETED
@@ -1,34 +0,0 @@
1
- import { n as hubQueue, o as resolveNuxtServerDirs, t as createQueueNitroConfig } from "./vite-ChWLzOVu.js";
2
- import { resolve } from "node:path";
3
- //#region src/nuxt.ts
4
- function isQueueVitePlugin(value) {
5
- return Boolean(value && typeof value === "object" && value.name === "@vite-hub/queue/vite");
6
- }
7
- function findQueueVitePlugin(plugins) {
8
- return plugins.flat(Infinity).find(isQueueVitePlugin);
9
- }
10
- function viteHubQueueNuxtModule(options = {}, nuxt) {
11
- if (!nuxt) return;
12
- nuxt.hook?.("prepare:types", (context) => {
13
- context.references.push({ path: resolve(nuxt.options.rootDir || process.cwd(), ".vitehub/queue.d.ts") });
14
- });
15
- nuxt.options.vite ??= {};
16
- const plugins = Array.isArray(nuxt.options.vite.plugins) ? nuxt.options.vite.plugins : [];
17
- const existingPlugin = findQueueVitePlugin(plugins);
18
- const plugin = existingPlugin || hubQueue(options);
19
- if (!existingPlugin) plugins.push(plugin);
20
- nuxt.options.vite.plugins = plugins;
21
- nuxt.hook?.("nitro:config", async (nitroConfig) => {
22
- const projectRoot = nuxt.options.rootDir || process.cwd();
23
- const nitro = await createQueueNitroConfig(plugin, {
24
- development: nuxt.options.dev,
25
- nitro: nitroConfig,
26
- projectRoot,
27
- root: nuxt.options.srcDir || projectRoot,
28
- serverDirs: resolveNuxtServerDirs(nuxt.options)
29
- });
30
- Object.assign(nitroConfig, nitro);
31
- });
32
- }
33
- //#endregion
34
- export { viteHubQueueNuxtModule as default };
@@ -1,7 +0,0 @@
1
- //#region ../internal/src/object.ts
2
- function isPlainObject(value) {
3
- if (value === null || Object(value) !== value || Array.isArray(value)) return false;
4
- return typeof value === "object";
5
- }
6
- //#endregion
7
- export { isPlainObject as t };
@@ -1,63 +0,0 @@
1
- import { i as normalizePublicQueueIdentifier, r as isQueueError } from "./errors-DIFaM9tW.js";
2
- import { i as getQueueRuntimeConfig, o as listQueueDefinitionNames } from "./state-G9s378-e.js";
3
- import { n as dynamicQueue } from "./client-CIc3jsj2.js";
4
- import { t as redactInspectionText } from "./inspect-PeuckaC3.js";
5
- //#region src/runtime/operations.ts
6
- /** A failure with the fields that every surface reports. */
7
- var QueueOperationError = class extends Error {
8
- status;
9
- code;
10
- fix;
11
- constructor(message, status, code, fix) {
12
- super(redactInspectionText(message));
13
- this.status = status;
14
- this.code = code;
15
- this.fix = fix;
16
- }
17
- };
18
- const statuses = {
19
- QUEUE_DEFINITION_NOT_FOUND: 404,
20
- QUEUE_DISABLED: 409,
21
- QUEUE_PROVIDER_FAILED: 502,
22
- QUEUE_PROVIDER_INVALID_RESPONSE: 502,
23
- QUEUE_UNSUPPORTED_OPTION: 400
24
- };
25
- /** Gives a Queue catalog error the status that the Console RPC reports. */
26
- function failure(error) {
27
- if (!isQueueError(error)) return error;
28
- return new QueueOperationError(error.message, statuses[error.code] ?? 500, error.code, error.fix);
29
- }
30
- function listQueues() {
31
- const config = getQueueRuntimeConfig();
32
- if (config === false) throw new QueueOperationError("Queue is disabled in this runtime.", 409, "QUEUE_DISABLED", "Add `hubQueue()` to the Vite config and do not set `queue: false`.");
33
- return {
34
- ...config ? { provider: config.provider } : {},
35
- queues: listQueueDefinitionNames().map((name) => ({ name }))
36
- };
37
- }
38
- async function sendToQueue(input) {
39
- const queue = normalizePublicQueueIdentifier(input.queue);
40
- if (!queue) throw new QueueOperationError("The queue name is not a valid Queue Definition name.", 400, "QUEUE_INVALID_NAME", "Pass the name of a Queue Definition. `vitehub queue list` shows them.");
41
- const options = {
42
- ...input.delaySeconds === void 0 ? {} : { delaySeconds: input.delaySeconds },
43
- ...input.id === void 0 ? {} : { id: input.id },
44
- ...input.idempotencyKey === void 0 ? {} : { idempotencyKey: input.idempotencyKey }
45
- };
46
- let result;
47
- try {
48
- result = await dynamicQueue.run(queue, input.payload, options);
49
- } catch (error) {
50
- throw failure(error);
51
- }
52
- const [error, sent] = result;
53
- if (error) throw failure(error);
54
- const provider = getQueueRuntimeConfig();
55
- return {
56
- ...sent?.messageId ? { messageId: sent.messageId } : {},
57
- ...provider ? { provider: provider.provider } : {},
58
- queue,
59
- status: "queued"
60
- };
61
- }
62
- //#endregion
63
- export { listQueues, sendToQueue };
@@ -1,180 +0,0 @@
1
- import { Result } from "@vite-hub/runtime";
2
- //#region src/types.d.ts
3
- type QueueProvider = "cloudflare" | "vercel";
4
- type CloudflareQueueContentType = "bytes" | "json" | "text" | "v8";
5
- interface CloudflareQueueSendOptions {
6
- contentType?: CloudflareQueueContentType;
7
- delaySeconds?: number;
8
- }
9
- interface CloudflareQueueSendBatchOptions {
10
- delaySeconds?: number;
11
- }
12
- interface CloudflareQueueSendBatchMessage<T = unknown> extends CloudflareQueueSendOptions {
13
- body: T;
14
- }
15
- interface CloudflareQueueRetryOptions {
16
- delaySeconds?: number;
17
- }
18
- interface CloudflareQueueBinding {
19
- send: <T = unknown>(body: T, options?: CloudflareQueueSendOptions) => Promise<unknown>;
20
- sendBatch: <T = unknown>(messages: Iterable<CloudflareQueueSendBatchMessage<T>>, options?: CloudflareQueueSendBatchOptions) => Promise<unknown>;
21
- }
22
- interface CloudflareQueueMessage<T = unknown> {
23
- ack: () => void;
24
- attempts: number;
25
- body: T;
26
- id: string;
27
- retry: (options?: CloudflareQueueRetryOptions) => void;
28
- timestamp?: Date;
29
- }
30
- interface CloudflareQueueMessageBatch<T = unknown> {
31
- ackAll: () => void;
32
- messages: CloudflareQueueMessage<T>[];
33
- queue: string;
34
- retryAll: (options?: CloudflareQueueRetryOptions) => void;
35
- }
36
- type CloudflareQueueBatchErrorAction = "ack" | "retry" | {
37
- retry: CloudflareQueueRetryOptions;
38
- };
39
- interface CloudflareQueueBatchHandlerOptions<T = unknown> {
40
- concurrency?: number;
41
- onError?: (error: unknown, message: CloudflareQueueMessage<T>, batch: CloudflareQueueMessageBatch<T>) => CloudflareQueueBatchErrorAction | void | Promise<CloudflareQueueBatchErrorAction | void>;
42
- onMessage: (message: CloudflareQueueMessage<T>, batch: CloudflareQueueMessageBatch<T>) => unknown | Promise<unknown>;
43
- }
44
- type VercelQueueRetryDirective = {
45
- acknowledge: true;
46
- } | {
47
- afterSeconds: number;
48
- };
49
- type VercelQueueRetryHandler = (error: unknown, metadata: unknown) => VercelQueueRetryDirective | void;
50
- interface VercelQueueCallbackOptions {
51
- retry?: VercelQueueRetryHandler;
52
- visibilityTimeoutSeconds?: number;
53
- }
54
- interface VercelQueueSendOptions {
55
- delaySeconds?: number;
56
- headers?: Record<string, string>;
57
- idempotencyKey?: string;
58
- region?: string;
59
- retentionSeconds?: number;
60
- }
61
- interface VercelQueueSendResult {
62
- messageId?: string | null;
63
- }
64
- type VercelQueueMessageHandler<T = unknown> = (payload: T, metadata?: unknown) => unknown | Promise<unknown>;
65
- type VercelQueueCallbackReturn = (request: Request) => Promise<unknown> | unknown;
66
- interface VercelQueueSDK {
67
- handleCallback: <TPayload = unknown>(handler: VercelQueueMessageHandler<TPayload>, options?: VercelQueueCallbackOptions) => VercelQueueCallbackReturn;
68
- send: (topic: string, payload: unknown, options?: VercelQueueSendOptions) => Promise<VercelQueueSendResult>;
69
- }
70
- interface QueueSharedOptions {
71
- cache?: boolean;
72
- }
73
- interface CloudflareQueueProviderOptions extends QueueSharedOptions {
74
- binding?: string | CloudflareQueueBinding;
75
- provider: "cloudflare";
76
- }
77
- interface CloudflareQueueModuleProviderOptions extends QueueSharedOptions {
78
- binding?: string;
79
- namePrefix?: string;
80
- provider: "cloudflare";
81
- }
82
- interface VercelQueueProviderOptions extends QueueSharedOptions {
83
- client?: VercelQueueSDK;
84
- provider: "vercel";
85
- region?: string;
86
- topic?: string;
87
- }
88
- interface VercelQueueModuleProviderOptions extends QueueSharedOptions {
89
- provider: "vercel";
90
- region?: string;
91
- }
92
- type QueueProviderOptions = CloudflareQueueProviderOptions | VercelQueueProviderOptions;
93
- type QueueModuleProviderOptions = CloudflareQueueModuleProviderOptions | VercelQueueModuleProviderOptions;
94
- type QueueModuleOptions = false | (QueueSharedOptions & {
95
- namePrefix?: string;
96
- provider?: undefined;
97
- }) | QueueModuleProviderOptions;
98
- type ResolvedQueueOptions = QueueModuleProviderOptions;
99
- interface QueueJob<TPayload = unknown> {
100
- attempts: number;
101
- id: string;
102
- metadata?: unknown;
103
- payload: TPayload;
104
- }
105
- type QueueHandler<TPayload = unknown, TResult = unknown> = (job: QueueJob<TPayload>) => TResult;
106
- interface QueueDefinitionOptions {
107
- cache?: boolean;
108
- callbackOptions?: VercelQueueCallbackOptions;
109
- concurrency?: number;
110
- onDispatchError?: (error: unknown, context: {
111
- name: string;
112
- }) => unknown | Promise<unknown>;
113
- onError?: CloudflareQueueBatchHandlerOptions["onError"];
114
- }
115
- interface QueueDefinition<TPayload = unknown, TResult = unknown> {
116
- handler: QueueHandler<TPayload, TResult>;
117
- options?: QueueDefinitionOptions;
118
- }
119
- interface QueueEnqueueOptions {
120
- id?: string;
121
- contentType?: CloudflareQueueContentType;
122
- delaySeconds?: number;
123
- idempotencyKey?: string;
124
- region?: string;
125
- retentionSeconds?: number;
126
- }
127
- type QueueSendResult = {
128
- messageId?: string;
129
- status: "queued";
130
- };
131
- /** Codes that Queue operations return in the `[error, data]` tuple. Config errors throw instead. */
132
- type QueueErrorCode = "QUEUE_PROVIDER_FAILED" | "QUEUE_PROVIDER_INVALID_RESPONSE" | "QUEUE_UNSUPPORTED_OPTION";
133
- /** Codes that `send()` and `sendBatch()` of a Cloudflare QueueClient return. */
134
- type CloudflareQueueErrorCode = "QUEUE_PROVIDER_FAILED" | "QUEUE_UNSUPPORTED_OPTION";
135
- /** Codes that `send()` of a Vercel QueueClient returns. */
136
- type VercelQueueErrorCode = QueueErrorCode;
137
- interface QueueClientBase<P extends QueueProvider = QueueProvider> {
138
- readonly native: unknown;
139
- readonly provider: P;
140
- }
141
- /**
142
- * `send()` and `sendBatch()` return `[error, data]`. `error.code` is one of the codes in the method type.
143
- */
144
- interface CloudflareQueueClient<TPayload = unknown> extends QueueClientBase<"cloudflare"> {
145
- readonly binding: CloudflareQueueBinding;
146
- createBatchHandler: <TMessage = unknown>(options: CloudflareQueueBatchHandlerOptions<TMessage>) => (batch: CloudflareQueueMessageBatch<TMessage>) => Promise<void>;
147
- send: (payload: TPayload, options?: QueueEnqueueOptions) => Promise<Result<QueueSendResult, CloudflareQueueErrorCode>>;
148
- sendBatch: (items: CloudflareQueueSendBatchMessage<TPayload>[], options?: Omit<QueueEnqueueOptions, "id">) => Promise<Result<QueueSendResult[], CloudflareQueueErrorCode>>;
149
- }
150
- /**
151
- * `send()` returns `[error, data]`. `error.code` is one of the codes in the method type.
152
- */
153
- interface VercelQueueClient<TPayload = unknown> extends QueueClientBase<"vercel"> {
154
- readonly topic: string;
155
- send: (payload: TPayload, options?: QueueEnqueueOptions) => Promise<Result<QueueSendResult, VercelQueueErrorCode>>;
156
- callback: <TMessage = unknown>(handler: VercelQueueMessageHandler<TMessage>, options?: VercelQueueCallbackOptions) => VercelQueueCallbackReturn;
157
- }
158
- interface QueueClientMap<TPayload> {
159
- cloudflare: CloudflareQueueClient<TPayload>;
160
- vercel: VercelQueueClient<TPayload>;
161
- }
162
- type QueueClient<P extends QueueProvider = QueueProvider, TPayload = unknown> = QueueClientMap<TPayload>[P];
163
- /** Discovered Queue Definitions. Generated declarations extend this interface. */
164
- interface QueueRegistry {}
165
- type QueueName = Extract<keyof QueueRegistry, string>;
166
- type QueuePayload<TName extends QueueName> = QueueRegistry[TName] extends {
167
- handler: (job: infer TJob) => unknown;
168
- } ? TJob extends QueueJob<infer TPayload> ? TPayload : never : never;
169
- interface QueueDefinitionRegistry {
170
- [name: string]: () => Promise<{
171
- default?: QueueDefinition;
172
- } | QueueDefinition>;
173
- }
174
- interface DiscoveredQueueDefinition {
175
- handler: string;
176
- name: string;
177
- source?: "server-queues" | "vite-suffix";
178
- }
179
- //#endregion
180
- export { ResolvedQueueOptions as C, VercelQueueRetryDirective as D, VercelQueueErrorCode as E, VercelQueueRetryHandler as O, QueueSendResult as S, VercelQueueClient as T, QueueName as _, CloudflareQueueMessageBatch as a, QueueProviderOptions as b, QueueDefinition as c, QueueEnqueueOptions as d, QueueErrorCode as f, QueueModuleProviderOptions as g, QueueModuleOptions as h, CloudflareQueueMessage as i, VercelQueueSendResult as k, QueueDefinitionOptions as l, QueueJob as m, CloudflareQueueClient as n, DiscoveredQueueDefinition as o, QueueHandler as p, CloudflareQueueErrorCode as r, QueueClient as s, CloudflareQueueBatchHandlerOptions as t, QueueDefinitionRegistry as u, QueuePayload as v, VercelQueueCallbackOptions as w, QueueRegistry as x, QueueProvider as y };
@@ -1,122 +0,0 @@
1
- import { a as getQueueRuntimeEvent } from "./state-G9s378-e.js";
2
- //#region src/internal/vercel-region.ts
3
- function readHeader(headers, name) {
4
- if (!headers) return;
5
- if (isHeadersLike(headers)) try {
6
- const descriptor = getPrototypeDescriptor(headers, "get");
7
- if (!descriptor) return;
8
- if (!isBrandedMethod(descriptor.value, headers, descriptor.owner, void 0, "probe").ok) return;
9
- const value = descriptor.value.call(headers, name);
10
- if (value !== descriptor.value.call(headers, name)) return;
11
- return typeof value === "string" && value ? value : void 0;
12
- } catch {
13
- return;
14
- }
15
- let value;
16
- try {
17
- value = headers[name] ?? headers[name.toLowerCase()];
18
- } catch {
19
- return;
20
- }
21
- if (typeof value === "string") return value;
22
- if (Array.isArray(value)) return typeof value[0] === "string" ? value[0] : void 0;
23
- }
24
- function isHeadersLike(value) {
25
- if (!value || typeof value !== "object" && typeof value !== "function") return false;
26
- try {
27
- if (Object.prototype.hasOwnProperty.call(value, "get")) return false;
28
- const descriptor = getPrototypeDescriptor(value, "get");
29
- if (!descriptor) return false;
30
- return isBrandedMethod(descriptor.value, value, descriptor.owner, void 0, "probe").ok;
31
- } catch {
32
- return false;
33
- }
34
- }
35
- function isBrandedMethod(value, receiver, owner, targetName, mode = "accessor") {
36
- if (typeof value !== "function") return { ok: false };
37
- try {
38
- const source = Function.prototype.toString.call(value);
39
- if (!source.includes("[native code]") && !source.includes("webidl.brandCheck")) return { ok: false };
40
- if (!owner || Object.getPrototypeOf(owner) === null) return { ok: false };
41
- const constructor = Object.getOwnPropertyDescriptor(owner, "constructor")?.value;
42
- if (typeof constructor !== "function" || constructor.prototype !== owner || constructor.name !== "Headers" && constructor.name !== "Request") return { ok: false };
43
- if (!(receiver instanceof constructor)) return { ok: false };
44
- if (mode === "accessor") {
45
- value.call(receiver);
46
- return { ok: true };
47
- }
48
- const probeName = targetName ?? "x-vitehub-brand-probe";
49
- const probe = value.call(receiver, probeName);
50
- if (probe !== null && typeof probe !== "string") return { ok: false };
51
- return {
52
- ok: true,
53
- value: typeof probe === "string" && probe ? probe : void 0
54
- };
55
- } catch {
56
- return { ok: false };
57
- }
58
- }
59
- function getRequestHeaders(value) {
60
- if (!value || typeof value !== "object" && typeof value !== "function") return void 0;
61
- try {
62
- const descriptor = getPrototypeDescriptor(value, "headers");
63
- if (!descriptor?.get || !isBrandedMethod(descriptor.get, value, descriptor.owner, void 0, "accessor").ok) return void 0;
64
- const headers = descriptor.get.call(value);
65
- return isHeadersLike(headers) ? headers : void 0;
66
- } catch {
67
- return;
68
- }
69
- }
70
- function getPrototypeDescriptor(value, property) {
71
- let prototype = value;
72
- const visited = /* @__PURE__ */ new WeakSet();
73
- const maxDepth = 1024;
74
- let depth = 0;
75
- while (prototype && depth++ < maxDepth) {
76
- if (visited.has(prototype)) return void 0;
77
- visited.add(prototype);
78
- const descriptor = Object.getOwnPropertyDescriptor(prototype, property);
79
- if (descriptor) return {
80
- ...descriptor,
81
- owner: prototype
82
- };
83
- prototype = Object.getPrototypeOf(prototype);
84
- }
85
- }
86
- function parseRegionFromVercelId(value) {
87
- if (!value) return;
88
- return value.match(/^([a-z0-9]+)::/i)?.[1]?.toLowerCase();
89
- }
90
- function resolveVercelQueueRegion(explicitRegion) {
91
- if (explicitRegion) return {
92
- region: explicitRegion,
93
- requestScoped: false
94
- };
95
- if (process.env.QUEUE_REGION) return {
96
- region: process.env.QUEUE_REGION,
97
- requestScoped: false
98
- };
99
- if (process.env.VERCEL_REGION) return {
100
- region: process.env.VERCEL_REGION,
101
- requestScoped: false
102
- };
103
- const event = getQueueRuntimeEvent();
104
- let requestHeaders;
105
- try {
106
- const request = event?.request;
107
- requestHeaders = getRequestHeaders(request);
108
- } catch {
109
- requestHeaders = void 0;
110
- }
111
- if (!requestHeaders) try {
112
- requestHeaders = event?.req?.headers ?? event?.node?.req?.headers;
113
- } catch {
114
- requestHeaders = void 0;
115
- }
116
- return {
117
- region: readHeader(requestHeaders, "ce-vqsregion") || parseRegionFromVercelId(readHeader(requestHeaders, "x-vercel-id")),
118
- requestScoped: event !== void 0
119
- };
120
- }
121
- //#endregion
122
- export { resolveVercelQueueRegion as t };