@vite-hub/queue 0.1.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +141 -20
- package/dist/client-Cj50hQYp.js +180 -0
- package/dist/{cloudflare-client-COzQ8fHk.js → cloudflare-client-DYsAjM8B.js} +9 -16
- package/dist/{cloudflare-resource-name-CCuLDvSP.js → cloudflare-resource-name-BJHYRCMQ.js} +1 -1
- package/dist/config-BcTDeQIP.js +105 -0
- package/dist/durability.d.ts +13 -0
- package/dist/durability.js +47 -0
- package/dist/error-diagnostics-BRartC1z.js +181 -0
- package/dist/error-diagnostics-DSkCJC_L.js +266 -0
- package/dist/errors-DL3pcNO4.js +77 -0
- package/dist/{handler-response-CBwIkmTS.js → handler-response-By0Fpspv.js} +9 -4
- package/dist/index.d.ts +15 -31
- package/dist/index.js +23 -11
- 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 +560 -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 +52 -26
- package/dist/internal/runtime/worker-client.d.ts +6 -0
- package/dist/internal/runtime/worker-client.js +11 -0
- package/dist/invariant-D8_xBqmZ.js +22 -0
- package/dist/local-CGckW5nr.js +540 -0
- package/dist/local-CVVRVWeR.d.ts +22 -0
- package/dist/object-D8pRdprI.js +15 -0
- package/dist/{operations-tNUdj3G4.d.ts → operation-definition-8gOe-ubD.d.ts} +37 -15
- package/dist/operations-Crwjca91.js +124 -0
- package/dist/operations.d.ts +45 -8
- package/dist/operations.js +173 -24
- package/dist/runtime/hosted.d.ts +3 -5
- package/dist/runtime/hosted.js +21 -45
- package/dist/{state-G9s378-e.js → state-mVA2X_wa.js} +59 -47
- package/dist/types-BD-8U9zS.d.ts +420 -0
- package/dist/types-DS_rqu9l.d.ts +10 -0
- package/dist/vercel-region-D1CivgdA.js +26 -0
- package/dist/virtual.d.ts +4 -0
- package/dist/vite.d.ts +364 -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
package/dist/index.d.ts
CHANGED
|
@@ -1,53 +1,37 @@
|
|
|
1
|
-
import { C as
|
|
2
|
-
import { i as createQueueCloudflareWorker } from "./cloudflare-vite-CjdP5yBN.js";
|
|
1
|
+
import { A as QueueModuleDriverOptions, B as VercelQueueClient, C as QueueDriver, D as QueueHandler, E as QueueErrorCode, F as QueueSendResult, G as VercelQueueSendBatchOptions, H as VercelQueueRetryDirective, I as ResolvedLocalQueueOptions, K as VercelQueueSendResult, L as ResolvedQueueOptions, M as QueueOptions, N as QueuePayload, O as QueueJob, P as QueueRegistry, R as VercelQueueBatchSendResult, S as QueueDefinitionRegistry, T as QueueEnqueueOptions, U as VercelQueueRetryHandler, V as VercelQueueErrorCode, W as VercelQueueSendBatchMessage, _ as PostgresQueueDriverOptions, a as CloudflareQueueMessageBatch, b as QueueDefinition, c as LocalQueueDriverOptions, f as LocalQueueRules, g as PostgresQueueClient, h as NodeQueueDriver, i as CloudflareQueueMessage, j as QueueName, k as QueueJobStore, l as LocalQueueErrorCode, n as CloudflareQueueClient, o as DiscoveredQueueDefinition, p as LocalQueueSendBatchMessage, r as CloudflareQueueErrorCode, s as LocalQueueClient, t as CloudflareQueueBatchHandlerOptions, v as PostgresQueueErrorCode, w as QueueDriverOptions, x as QueueDefinitionOptions, y as QueueClient, z as VercelQueueCallbackOptions } from "./types-BD-8U9zS.js";
|
|
3
2
|
import { Result } from "@vite-hub/runtime";
|
|
4
|
-
//#region src/config.d.ts
|
|
5
|
-
interface QueueResolutionInput {
|
|
6
|
-
hosting?: string;
|
|
7
|
-
}
|
|
8
|
-
export declare function normalizeQueueOptions(options: QueueModuleOptions | undefined, input?: QueueResolutionInput): ResolvedQueueOptions | undefined;
|
|
9
|
-
//#endregion
|
|
10
3
|
//#region src/definition.d.ts
|
|
11
4
|
export declare function defineQueue<TPayload = unknown, TResult = unknown>(handler: QueueHandler<TPayload, TResult>, options?: QueueDefinitionOptions): QueueDefinition<TPayload, TResult>;
|
|
12
5
|
//#endregion
|
|
13
6
|
//#region src/enqueue.d.ts
|
|
14
7
|
export declare function createQueueMessageId(prefix?: string): string;
|
|
15
8
|
//#endregion
|
|
16
|
-
//#region src/providers/cloudflare.d.ts
|
|
17
|
-
export declare function createCloudflareQueueBatchHandler<TPayload = unknown>(options: CloudflareQueueBatchHandlerOptions<TPayload>): (batch: CloudflareQueueMessageBatch<TPayload>) => Promise<void>;
|
|
18
|
-
//#endregion
|
|
19
|
-
//#region src/integrations/cloudflare.d.ts
|
|
20
|
-
export declare function getCloudflareQueueBindingName(name: string): string;
|
|
21
|
-
export declare function getCloudflareQueueDefinitionName(name: string, namePrefix?: string): string;
|
|
22
|
-
//#endregion
|
|
23
|
-
//#region src/internal/cloudflare-resource-name.d.ts
|
|
24
|
-
export declare function getCloudflareQueueName(name: string, namePrefix?: string): string;
|
|
25
|
-
//#endregion
|
|
26
|
-
//#region src/integrations/vercel.d.ts
|
|
27
|
-
export declare function getVercelQueueTopicName(name: string): string;
|
|
28
|
-
//#endregion
|
|
29
9
|
//#region src/runtime/client.d.ts
|
|
30
|
-
declare function
|
|
31
|
-
declare function
|
|
10
|
+
declare function useDynamicQueue(name: string): Promise<QueueClient>;
|
|
11
|
+
declare function sendDynamicQueue(name: string, payload: unknown, options?: QueueEnqueueOptions): Promise<Result<QueueSendResult, QueueErrorCode>>;
|
|
32
12
|
declare function deferDynamicQueue(name: string, payload: unknown, options?: QueueEnqueueOptions): void;
|
|
33
13
|
/** Dispatch externally supplied names and payloads after application validation. */
|
|
34
14
|
export declare const dynamicQueue: {
|
|
35
15
|
defer: typeof deferDynamicQueue;
|
|
36
|
-
|
|
37
|
-
|
|
16
|
+
send: typeof sendDynamicQueue;
|
|
17
|
+
use: typeof useDynamicQueue;
|
|
38
18
|
};
|
|
39
19
|
/**
|
|
40
|
-
* Returns the QueueClient of a discovered Queue Definition. Config errors, for example an unknown name, throw.
|
|
20
|
+
* Returns the QueueClient of a discovered Queue Definition, for `sendBatch()` and provider methods. Config errors, for example an unknown name, throw.
|
|
41
21
|
*/
|
|
42
|
-
export declare function
|
|
22
|
+
export declare function useQueue<TName extends QueueName>(name: TName): Promise<QueueClient<QueueDriver, QueuePayload<TName>>>;
|
|
43
23
|
/**
|
|
44
|
-
*
|
|
24
|
+
* Sends one job to the Queue and returns `[error, result]`. `error.code` is a `QueueErrorCode`.
|
|
45
25
|
* Config errors, for example an unknown Queue name, throw.
|
|
46
26
|
*/
|
|
47
|
-
export declare function
|
|
27
|
+
export declare function sendQueue<TName extends QueueName>(name: TName, payload: NoInfer<QueuePayload<TName>>, options?: QueueEnqueueOptions): Promise<Result<QueueSendResult, QueueErrorCode>>;
|
|
28
|
+
/**
|
|
29
|
+
* Sends one job after the response, through `waitUntil`. It returns nothing: a failure goes to the log and to the
|
|
30
|
+
* `onDispatchError` option of the Queue Definition. Call `sendQueue()` when the caller needs the message ID.
|
|
31
|
+
*/
|
|
48
32
|
export declare function deferQueue<TName extends QueueName>(name: TName, payload: NoInfer<QueuePayload<TName>>, options?: QueueEnqueueOptions): void;
|
|
49
33
|
//#endregion
|
|
50
34
|
//#region src/runtime/create-client.d.ts
|
|
51
|
-
export declare function createQueueClient(options:
|
|
35
|
+
export declare function createQueueClient(options: QueueDriverOptions): Promise<QueueClient>;
|
|
52
36
|
//#endregion
|
|
53
|
-
export {
|
|
37
|
+
export type { CloudflareQueueBatchHandlerOptions, CloudflareQueueClient, CloudflareQueueErrorCode, CloudflareQueueMessage, CloudflareQueueMessageBatch, DiscoveredQueueDefinition, LocalQueueClient, LocalQueueDriverOptions, LocalQueueErrorCode, LocalQueueRules, LocalQueueSendBatchMessage, NodeQueueDriver, PostgresQueueClient, PostgresQueueDriverOptions, PostgresQueueErrorCode, QueueClient, QueueDefinition, QueueDefinitionOptions, QueueDefinitionRegistry, QueueDriver, QueueDriverOptions, QueueEnqueueOptions, QueueErrorCode, QueueHandler, QueueJob, QueueJobStore, QueueModuleDriverOptions, QueueName, QueueOptions, QueuePayload, QueueRegistry, QueueSendResult, ResolvedLocalQueueOptions, ResolvedQueueOptions, VercelQueueBatchSendResult, VercelQueueCallbackOptions, VercelQueueClient, VercelQueueErrorCode, VercelQueueRetryDirective, VercelQueueRetryHandler, VercelQueueSendBatchMessage, VercelQueueSendBatchOptions, VercelQueueSendResult };
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
import { t as
|
|
2
|
-
import {
|
|
3
|
-
import { t as
|
|
4
|
-
import { n as getCloudflareQueueDefinitionName, t as getCloudflareQueueBindingName } from "./cloudflare-GosJefIK.js";
|
|
5
|
-
import { n as createCloudflareQueueBatchHandler } from "./cloudflare-client-COzQ8fHk.js";
|
|
6
|
-
import { t as getCloudflareQueueName } from "./cloudflare-resource-name-CCuLDvSP.js";
|
|
7
|
-
import { t as getVercelQueueTopicName } from "./vercel-C_4vqxcl.js";
|
|
8
|
-
import { i as runQueue, n as dynamicQueue, r as getQueue, t as deferQueue } from "./client-CIc3jsj2.js";
|
|
9
|
-
import { t as createQueueCloudflareWorker } from "./cloudflare-vite-BCuJOXcT.js";
|
|
1
|
+
import { t as queueErrorDiagnostics } from "./error-diagnostics-BRartC1z.js";
|
|
2
|
+
import { c as createQueueMessageId } from "./errors-DL3pcNO4.js";
|
|
3
|
+
import { i as useQueue, n as dynamicQueue, r as sendQueue, t as deferQueue } from "./client-Cj50hQYp.js";
|
|
10
4
|
//#region src/definition.ts
|
|
11
5
|
function defineQueue(handler, options) {
|
|
12
6
|
if (typeof handler !== "function") throw queueErrorDiagnostics.QUEUE_INVALID_DEFINITION();
|
|
@@ -18,12 +12,30 @@ function defineQueue(handler, options) {
|
|
|
18
12
|
//#endregion
|
|
19
13
|
//#region src/runtime/create-client.ts
|
|
20
14
|
async function createQueueClient(options) {
|
|
21
|
-
if (options.
|
|
15
|
+
if (options.driver === void 0 && Object.hasOwn(options, "provider")) {
|
|
16
|
+
const provider = options.provider;
|
|
17
|
+
throw queueErrorDiagnostics.QUEUE_RENAMED_PROVIDER_KEY({ value: JSON.stringify(provider) ?? String(provider) });
|
|
18
|
+
}
|
|
19
|
+
if (options.driver === "cloudflare") {
|
|
22
20
|
const { createCloudflareQueueRuntimeClient } = await import("./internal/runtime/cloudflare-client.js");
|
|
23
21
|
return await createCloudflareQueueRuntimeClient(options);
|
|
24
22
|
}
|
|
23
|
+
if (options.driver === "local") {
|
|
24
|
+
const { createLocalQueueRuntimeClient } = await import("./internal/runtime/local-client.js");
|
|
25
|
+
return await createLocalQueueRuntimeClient({
|
|
26
|
+
base: options.base,
|
|
27
|
+
cache: options.cache,
|
|
28
|
+
driver: options.driver,
|
|
29
|
+
queue: options.queue,
|
|
30
|
+
rules: options.rules
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
if (options.driver === "postgres") {
|
|
34
|
+
const { createPostgresQueueRuntimeClient } = await import("#vitehub/queue/node-client");
|
|
35
|
+
return await createPostgresQueueRuntimeClient(options);
|
|
36
|
+
}
|
|
25
37
|
const { createVercelQueueRuntimeClient } = await import("./internal/runtime/vercel-client.js");
|
|
26
38
|
return await createVercelQueueRuntimeClient(options);
|
|
27
39
|
}
|
|
28
40
|
//#endregion
|
|
29
|
-
export {
|
|
41
|
+
export { createQueueClient, createQueueMessageId, deferQueue, defineQueue, dynamicQueue, sendQueue, useQueue };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { w as QueueDriverOptions, y as QueueClient } from "../../types-BD-8U9zS.js";
|
|
2
2
|
//#region src/internal/runtime/cloudflare-client.d.ts
|
|
3
|
-
export declare function createCloudflareQueueRuntimeClient(options:
|
|
3
|
+
export declare function createCloudflareQueueRuntimeClient(options: QueueDriverOptions): Promise<QueueClient>;
|
|
4
4
|
//#endregion
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as createCloudflareQueueRuntimeClient } from "../../cloudflare-client-
|
|
1
|
+
import { t as createCloudflareQueueRuntimeClient } from "../../cloudflare-client-DYsAjM8B.js";
|
|
2
2
|
export { createCloudflareQueueRuntimeClient };
|
|
@@ -1,2 +1,30 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { L as ResolvedQueueOptions, S as QueueDefinitionRegistry, a as CloudflareQueueMessageBatch } from "../../types-BD-8U9zS.js";
|
|
2
|
+
//#region ../internal/src/runtime/cloudflare-env-lookup.d.ts
|
|
3
|
+
type CloudflareWorkerEnv = Record<string, unknown>;
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region ../internal/src/runtime/cloudflare-env.d.ts
|
|
6
|
+
interface CloudflareWorkerExecutionContext {
|
|
7
|
+
waitUntil?: (promise: Promise<unknown>) => void;
|
|
8
|
+
}
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region ../internal/src/runtime/app.d.ts
|
|
11
|
+
type AppHandler = (request: Request, context?: Record<string, unknown>) => Response | Promise<Response>;
|
|
12
|
+
type VitehubApp = AppHandler | {
|
|
13
|
+
fetch?: AppHandler;
|
|
14
|
+
request?: (request: Request, options?: RequestInit, context?: Record<string, unknown>) => Response | Promise<Response>;
|
|
15
|
+
};
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/internal/runtime/cloudflare-vite.d.ts
|
|
18
|
+
export type CloudflareWorkerApp = VitehubApp;
|
|
19
|
+
export interface QueueCloudflareWorkerOptions {
|
|
20
|
+
app?: CloudflareWorkerApp;
|
|
21
|
+
definitions?: Record<string, string>;
|
|
22
|
+
queue?: false | ResolvedQueueOptions;
|
|
23
|
+
registry?: QueueDefinitionRegistry;
|
|
24
|
+
}
|
|
25
|
+
export interface QueueCloudflareWorker {
|
|
26
|
+
fetch: (request: Request, env: CloudflareWorkerEnv, context: CloudflareWorkerExecutionContext) => Promise<Response>;
|
|
27
|
+
queue: (batch: CloudflareQueueMessageBatch, env: CloudflareWorkerEnv, context: CloudflareWorkerExecutionContext) => Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
export declare function createQueueCloudflareWorker(options?: QueueCloudflareWorkerOptions): QueueCloudflareWorker;
|
|
30
|
+
//#endregion
|
|
@@ -1,2 +1,112 @@
|
|
|
1
|
-
import { t as
|
|
1
|
+
import { t as queueErrorDiagnostics } from "../../error-diagnostics-BRartC1z.js";
|
|
2
|
+
import { _ as runWithActiveCloudflareEnv, c as loadQueueDefinition, d as setQueueRuntimeConfig, h as createCloudflareRuntimeEvent, m as setQueueRuntimeRegistry, u as runWithQueueRuntimeEvent } from "../../state-mVA2X_wa.js";
|
|
3
|
+
import { t as internalErrorDiagnostics } from "../../error-diagnostics-DSkCJC_L.js";
|
|
4
|
+
import { n as normalizeQueueOptions } from "../../config-BcTDeQIP.js";
|
|
5
|
+
import { n as getCloudflareQueueDefinitionName } from "../../cloudflare-GosJefIK.js";
|
|
6
|
+
import { t as getCloudflareQueueName } from "../../cloudflare-resource-name-BJHYRCMQ.js";
|
|
7
|
+
import { n as createCloudflareQueueBatchHandler, t as createCloudflareQueueRuntimeClient } from "../../cloudflare-client-DYsAjM8B.js";
|
|
8
|
+
import { runInRunScopes } from "@vite-hub/runtime/internal/run-scope";
|
|
9
|
+
//#region ../internal/src/runtime/app.ts
|
|
10
|
+
function resolveAppFetch(label, app) {
|
|
11
|
+
if (!app) return;
|
|
12
|
+
if (typeof app === "function") return app;
|
|
13
|
+
if (typeof app.request === "function") return (request, context) => app.request(request, void 0, context);
|
|
14
|
+
if (typeof app.fetch === "function") return app.fetch.bind(app);
|
|
15
|
+
throw internalErrorDiagnostics.INTERNAL_R0011({ message: `Invalid ${label} app. Expected an h3 app or a fetch-compatible handler.` });
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region ../internal/src/runtime/cloudflare-hosted.ts
|
|
19
|
+
function createWebRequestHandler(appHandler) {
|
|
20
|
+
return async (request, context) => await Promise.resolve(appHandler ? appHandler(request, context) : new Response(JSON.stringify({
|
|
21
|
+
status: 404,
|
|
22
|
+
message: `Cannot find any route matching [${request.method}] ${request.url}`
|
|
23
|
+
}), {
|
|
24
|
+
headers: { "content-type": "application/json;charset=UTF-8" },
|
|
25
|
+
status: 404,
|
|
26
|
+
statusText: "Not Found"
|
|
27
|
+
}));
|
|
28
|
+
}
|
|
29
|
+
function createCloudflareHostedWorker(adapter) {
|
|
30
|
+
const requestHandler = createWebRequestHandler(resolveAppFetch(adapter.label, adapter.app));
|
|
31
|
+
return { async fetch(request, env, context) {
|
|
32
|
+
const handle = async (handlerContext) => await Promise.resolve(requestHandler(request, handlerContext));
|
|
33
|
+
if (adapter.onRequest) return await adapter.onRequest({
|
|
34
|
+
env,
|
|
35
|
+
executionContext: context,
|
|
36
|
+
handle
|
|
37
|
+
});
|
|
38
|
+
return await handle();
|
|
39
|
+
} };
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/runtime/cloudflare-shared.ts
|
|
43
|
+
function createQueueJob(message, batch) {
|
|
44
|
+
return {
|
|
45
|
+
attempts: typeof message.attempts === "number" ? message.attempts : 1,
|
|
46
|
+
id: message.id,
|
|
47
|
+
metadata: {
|
|
48
|
+
batch,
|
|
49
|
+
message
|
|
50
|
+
},
|
|
51
|
+
payload: message.body
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/internal/runtime/cloudflare-vite.ts
|
|
56
|
+
function createRegistryDefinitionNames(registry, namePrefix) {
|
|
57
|
+
if (!registry) return void 0;
|
|
58
|
+
const definitions = {};
|
|
59
|
+
for (const name of Object.keys(registry)) {
|
|
60
|
+
const physicalName = getCloudflareQueueName(name, namePrefix);
|
|
61
|
+
if (definitions[physicalName]) throw queueErrorDiagnostics.QUEUE_NAME_COLLISION({
|
|
62
|
+
first: definitions[physicalName],
|
|
63
|
+
resolved: physicalName,
|
|
64
|
+
second: name
|
|
65
|
+
});
|
|
66
|
+
definitions[physicalName] = name;
|
|
67
|
+
}
|
|
68
|
+
return definitions;
|
|
69
|
+
}
|
|
70
|
+
function createQueueCloudflareWorker(options = {}) {
|
|
71
|
+
const queueConfig = options.queue === false ? false : normalizeQueueOptions(options.queue, { hosting: "cloudflare" });
|
|
72
|
+
const registry = options.registry;
|
|
73
|
+
const definitions = options.definitions;
|
|
74
|
+
const registryDefinitions = !definitions && queueConfig !== false && queueConfig.driver === "cloudflare" ? createRegistryDefinitionNames(registry, queueConfig.namePrefix ?? "") : void 0;
|
|
75
|
+
const applyRuntimeState = () => {
|
|
76
|
+
setQueueRuntimeConfig(queueConfig, createCloudflareQueueRuntimeClient);
|
|
77
|
+
setQueueRuntimeRegistry(registry);
|
|
78
|
+
};
|
|
79
|
+
return {
|
|
80
|
+
...createCloudflareHostedWorker({
|
|
81
|
+
app: options.app,
|
|
82
|
+
label: "queue",
|
|
83
|
+
async onRequest({ env, executionContext, handle }) {
|
|
84
|
+
applyRuntimeState();
|
|
85
|
+
const runtimeEvent = createCloudflareRuntimeEvent(env, executionContext);
|
|
86
|
+
return await runWithActiveCloudflareEnv(env, () => runWithQueueRuntimeEvent(runtimeEvent, () => handle(runtimeEvent.context)));
|
|
87
|
+
}
|
|
88
|
+
}),
|
|
89
|
+
async queue(batch, env, context) {
|
|
90
|
+
applyRuntimeState();
|
|
91
|
+
if (queueConfig === false || queueConfig?.driver !== "cloudflare") return;
|
|
92
|
+
const runtimeEvent = createCloudflareRuntimeEvent(env, context);
|
|
93
|
+
await runWithActiveCloudflareEnv(env, async () => {
|
|
94
|
+
const definitionName = definitions ? Object.hasOwn(definitions, batch.queue) ? definitions[batch.queue] : void 0 : registryDefinitions?.[batch.queue] ?? (/-[0-9a-f]{32}$/.test(batch.queue) && !/(?:^|-)queue--(?:[0-9a-f]{2})+$/i.test(batch.queue) || queueConfig.namePrefix && !batch.queue.startsWith(queueConfig.namePrefix) ? void 0 : getCloudflareQueueDefinitionName(batch.queue, queueConfig.namePrefix));
|
|
95
|
+
if (!definitionName) throw queueErrorDiagnostics.QUEUE_DEFINITION_NOT_FOUND({ queue: batch.queue });
|
|
96
|
+
const definition = await loadQueueDefinition(definitionName);
|
|
97
|
+
if (!definition) throw queueErrorDiagnostics.QUEUE_DEFINITION_NOT_FOUND({ queue: definitionName });
|
|
98
|
+
await createCloudflareQueueBatchHandler({
|
|
99
|
+
concurrency: definition.options?.concurrency,
|
|
100
|
+
onError: definition.options?.onError,
|
|
101
|
+
onMessage: async (message, currentBatch) => {
|
|
102
|
+
await runWithQueueRuntimeEvent(runtimeEvent, async () => {
|
|
103
|
+
await runInRunScopes(() => definition.handler(createQueueJob(message, currentBatch)));
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
})(batch);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
2
112
|
export { createQueueCloudflareWorker };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { m as LocalQueueStatus, y as QueueClient } from "../../types-BD-8U9zS.js";
|
|
2
|
+
import { t as LocalQueueEngine } from "../../local-CVVRVWeR.js";
|
|
3
|
+
import { n as QueueRuntimeOptions, t as QueueRuntimeDriverOptions } from "../../types-DS_rqu9l.js";
|
|
4
|
+
//#region src/internal/runtime/local-client.d.ts
|
|
5
|
+
export declare function createLocalQueueRuntimeClient(options: QueueRuntimeDriverOptions): Promise<QueueClient>;
|
|
6
|
+
/**
|
|
7
|
+
* Starts delivery of the stored jobs when the server starts, so jobs from before a restart run without a new send.
|
|
8
|
+
* Logs once where the jobs are and what survives.
|
|
9
|
+
*/
|
|
10
|
+
export declare function startLocalQueue(config: false | QueueRuntimeOptions): void;
|
|
11
|
+
/** The engine of the stored jobs, for `vitehub queue list`, `vitehub queue jobs`, and the Console. */
|
|
12
|
+
export declare function useLocalQueueEngine(config: QueueRuntimeOptions): LocalQueueEngine | undefined;
|
|
13
|
+
/** Status of the stored jobs, for `vitehub queue list` and the Console. */
|
|
14
|
+
export declare function getLocalQueueStatus(config: QueueRuntimeOptions): Promise<LocalQueueStatus | undefined>;
|
|
15
|
+
/** Stops the timers of every local engine. The stored jobs stay. */
|
|
16
|
+
export declare function closeLocalQueues(): void;
|
|
17
|
+
//#endregion
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { t as queueErrorDiagnostics } from "../../error-diagnostics-BRartC1z.js";
|
|
2
|
+
import { c as loadQueueDefinition, l as missingQueueDefinitionError } from "../../state-mVA2X_wa.js";
|
|
3
|
+
import { t as defaultLocalQueueBase } from "../../config-BcTDeQIP.js";
|
|
4
|
+
import { i as localQueueDeliveryRules, n as createLocalQueueEngine, t as createLocalQueueClient } from "../../local-CGckW5nr.js";
|
|
5
|
+
import { resolve } from "pathe";
|
|
6
|
+
//#region src/internal/runtime/local-client.ts
|
|
7
|
+
const engines = /* @__PURE__ */ new Map();
|
|
8
|
+
function getLocalQueueEngine({ base = defaultLocalQueueBase, development, rules = "local" }) {
|
|
9
|
+
const directory = resolve(base);
|
|
10
|
+
const cached = engines.get(directory);
|
|
11
|
+
const discloseErrors = development === true;
|
|
12
|
+
if (cached) {
|
|
13
|
+
if (cached.development !== discloseErrors) throw queueErrorDiagnostics.QUEUE_LOCAL_MODE_MISMATCH();
|
|
14
|
+
return cached.engine;
|
|
15
|
+
}
|
|
16
|
+
const engine = createLocalQueueEngine({
|
|
17
|
+
base: directory,
|
|
18
|
+
development: discloseErrors,
|
|
19
|
+
load: loadQueueDefinition,
|
|
20
|
+
missing: missingQueueDefinitionError,
|
|
21
|
+
rules: localQueueDeliveryRules[rules]
|
|
22
|
+
});
|
|
23
|
+
engines.set(directory, {
|
|
24
|
+
development: discloseErrors,
|
|
25
|
+
engine
|
|
26
|
+
});
|
|
27
|
+
return engine;
|
|
28
|
+
}
|
|
29
|
+
async function createLocalQueueRuntimeClient(options) {
|
|
30
|
+
if (options.driver !== "local") throw queueErrorDiagnostics.QUEUE_DRIVER_MISMATCH({
|
|
31
|
+
driver: options.driver,
|
|
32
|
+
expected: "local"
|
|
33
|
+
});
|
|
34
|
+
if (!options.queue) throw queueErrorDiagnostics.QUEUE_MISSING_QUEUE();
|
|
35
|
+
return createLocalQueueClient(getLocalQueueEngine(options), options.queue);
|
|
36
|
+
}
|
|
37
|
+
const providerNames = {
|
|
38
|
+
cloudflare: "Cloudflare",
|
|
39
|
+
local: "local",
|
|
40
|
+
postgres: "Postgres",
|
|
41
|
+
vercel: "Vercel"
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Starts delivery of the stored jobs when the server starts, so jobs from before a restart run without a new send.
|
|
45
|
+
* Logs once where the jobs are and what survives.
|
|
46
|
+
*/
|
|
47
|
+
function startLocalQueue(config) {
|
|
48
|
+
if (config === false || config.driver !== "local") return;
|
|
49
|
+
const engine = getLocalQueueEngine(config);
|
|
50
|
+
if (config.rules && config.rules !== "local") console.warn(`[vitehub:queue] The dev server runs Queue jobs in this process with the ${providerNames[config.rules]} delivery rules. It stores them in ${engine.base}. The build uses ${providerNames[config.rules]} Queues. Run \`vitehub queue jobs list\` to inspect the jobs.`);
|
|
51
|
+
else console.warn(`[vitehub:queue] Queue uses the local driver. This process stores jobs in ${engine.base} and runs them. The jobs survive a restart on this host, but not a move to another host or a new container. Run one server process per directory.`);
|
|
52
|
+
engine.start().catch((error) => {
|
|
53
|
+
console.error(`[vitehub:queue] The local driver cannot read the stored jobs in ${engine.base}.`, error);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/** The engine of the stored jobs, for `vitehub queue list`, `vitehub queue jobs`, and the Console. */
|
|
57
|
+
function useLocalQueueEngine(config) {
|
|
58
|
+
if (config.driver !== "local") return;
|
|
59
|
+
return getLocalQueueEngine(config);
|
|
60
|
+
}
|
|
61
|
+
/** Status of the stored jobs, for `vitehub queue list` and the Console. */
|
|
62
|
+
async function getLocalQueueStatus(config) {
|
|
63
|
+
return await useLocalQueueEngine(config)?.status();
|
|
64
|
+
}
|
|
65
|
+
/** Stops the timers of every local engine. The stored jobs stay. */
|
|
66
|
+
function closeLocalQueues() {
|
|
67
|
+
for (const { engine } of engines.values()) engine.close();
|
|
68
|
+
engines.clear();
|
|
69
|
+
}
|
|
70
|
+
//#endregion
|
|
71
|
+
export { closeLocalQueues, createLocalQueueRuntimeClient, getLocalQueueStatus, startLocalQueue, useLocalQueueEngine };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { y as QueueClient } from "../../types-BD-8U9zS.js";
|
|
2
|
+
import "../../local-CVVRVWeR.js";
|
|
3
|
+
import { n as QueueRuntimeOptions, t as QueueRuntimeDriverOptions } from "../../types-DS_rqu9l.js";
|
|
4
|
+
import postgres from "postgres";
|
|
5
|
+
//#region src/providers/postgres.d.ts
|
|
6
|
+
/** A postgres.js client. */
|
|
7
|
+
type PostgresQueueSql = postgres.Sql;
|
|
8
|
+
/**
|
|
9
|
+
* Where the jobs are. `client` borrows the postgres.js client of the app's Database, which keeps owning it. `url` opens
|
|
10
|
+
* a client that the engine owns and closes.
|
|
11
|
+
*/
|
|
12
|
+
type PostgresQueueConnection = {
|
|
13
|
+
client: PostgresQueueSql;
|
|
14
|
+
} | {
|
|
15
|
+
url: string;
|
|
16
|
+
};
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/internal/runtime/postgres-client.d.ts
|
|
19
|
+
/** The Databases of the app, as `@vite-hub/database/drizzle` exports them. */
|
|
20
|
+
export type PostgresQueueDatabases = Readonly<Record<string, {
|
|
21
|
+
db: object;
|
|
22
|
+
} | undefined>>;
|
|
23
|
+
/** Loads the Databases of the app. The generated server passes it when the jobs use the connection of a Database. */
|
|
24
|
+
export type PostgresQueueDatabasesLoader = () => Promise<PostgresQueueDatabases>;
|
|
25
|
+
interface ResolvedConnection {
|
|
26
|
+
connection: PostgresQueueConnection;
|
|
27
|
+
/** The URL, or the client of the Database. A Database can replace its client when its environment changes. */
|
|
28
|
+
key: object | string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The connection of the jobs: `QUEUE_POSTGRES_URL` when it is set, else the postgres.js client of the Database that the
|
|
32
|
+
* build found on Postgres. The jobs then use the connection that the Database resolves, whatever variable it reads.
|
|
33
|
+
*/
|
|
34
|
+
export declare function resolvePostgresQueueConnection(config: {
|
|
35
|
+
database?: string;
|
|
36
|
+
}, databases?: PostgresQueueDatabasesLoader | undefined, env?: Record<string, string | undefined>): Promise<ResolvedConnection>;
|
|
37
|
+
export declare function createPostgresQueueRuntimeClient(options: QueueRuntimeDriverOptions): Promise<QueueClient>;
|
|
38
|
+
/**
|
|
39
|
+
* Starts the workers when the server starts: this process takes the due jobs of every Queue Definition from Postgres.
|
|
40
|
+
* `databases` loads the Databases of the app, for a build that found its Default Database on Postgres. Logs once where
|
|
41
|
+
* the jobs are.
|
|
42
|
+
*/
|
|
43
|
+
export declare function startPostgresQueue(config: false | QueueRuntimeOptions, databases?: PostgresQueueDatabasesLoader): void;
|
|
44
|
+
/** Stops taking jobs, waits for the running jobs up to 10 seconds, and closes the connections it opened. The jobs stay. */
|
|
45
|
+
export declare function closePostgresQueues(): Promise<void>;
|
|
46
|
+
//#endregion
|