@vite-hub/queue 0.3.1 → 0.5.0
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 +22 -20
- package/dist/{client-Cj50hQYp.js → client-DYE_ZV8c.js} +11 -10
- package/dist/{cloudflare-client-DYsAjM8B.js → cloudflare-client-BasaJ68s.js} +22 -9
- package/dist/{cloudflare-resource-name-BJHYRCMQ.js → cloudflare-resource-name-BUNNqgkg.js} +1 -1
- package/dist/{config-BcTDeQIP.js → config-YEuaeQVq.js} +1 -3
- package/dist/definition-BL_3WHNu.js +27 -0
- package/dist/{errors-DL3pcNO4.js → duration-CshzgSSx.js} +48 -6
- package/dist/{error-diagnostics-DSkCJC_L.js → error-diagnostics-C0KyI408.js} +10 -2
- package/dist/{error-diagnostics-BRartC1z.js → error-diagnostics-DukggGP6.js} +14 -10
- package/dist/{handler-response-By0Fpspv.js → handler-response-ChtAjPNo.js} +8 -3
- package/dist/index.d.ts +7 -3
- package/dist/index.js +4 -12
- package/dist/internal/runtime/cloudflare-client.d.ts +1 -1
- package/dist/internal/runtime/cloudflare-client.js +1 -1
- package/dist/internal/runtime/cloudflare-vite.d.ts +1 -1
- package/dist/internal/runtime/cloudflare-vite.js +10 -9
- package/dist/internal/runtime/local-client.d.ts +3 -3
- package/dist/internal/runtime/local-client.js +4 -4
- package/dist/internal/runtime/postgres-client.d.ts +5 -4
- package/dist/internal/runtime/postgres-client.js +34 -17
- package/dist/internal/runtime/state.d.ts +2 -2
- package/dist/internal/runtime/state.js +1 -1
- package/dist/internal/runtime/vercel-client.d.ts +1 -1
- package/dist/internal/runtime/vercel-client.js +23 -12
- package/dist/internal/runtime/worker-client.d.ts +2 -2
- package/dist/internal/runtime/worker-client.js +1 -1
- package/dist/{local-CGckW5nr.js → local-CnBd5jdm.js} +50 -31
- package/dist/{local-CVVRVWeR.d.ts → local-DAL5ZgDg.d.ts} +2 -1
- package/dist/{operation-definition-8gOe-ubD.d.ts → operation-definition-CI2PJGqu.d.ts} +13 -2
- package/dist/{operations-Crwjca91.js → operations-BL0b9pte.js} +10 -11
- package/dist/operations.d.ts +5 -4
- package/dist/operations.js +16 -48
- package/dist/runtime/hosted.d.ts +1 -1
- package/dist/runtime/hosted.js +4 -4
- package/dist/scan-module-BbqyaLl6.js +311 -0
- package/dist/{state-mVA2X_wa.js → state-ER1YdCBF.js} +1 -1
- package/dist/{types-DS_rqu9l.d.ts → types-D6hDNcMY.d.ts} +1 -2
- package/dist/{types-BD-8U9zS.d.ts → types-klaR79hV.d.ts} +50 -24
- package/dist/{vercel-region-D1CivgdA.js → vercel-region-DOciq2kx.js} +1 -1
- package/dist/vite.d.ts +10 -4
- package/dist/vite.js +123 -94
- package/package.json +4 -4
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { t as queueErrorDiagnostics } from "../../error-diagnostics-
|
|
2
|
-
import { _ as runWithActiveCloudflareEnv, c as loadQueueDefinition, d as setQueueRuntimeConfig, h as createCloudflareRuntimeEvent, m as setQueueRuntimeRegistry, u as runWithQueueRuntimeEvent } from "../../state-
|
|
3
|
-
import { t as internalErrorDiagnostics } from "../../error-diagnostics-
|
|
4
|
-
import { n as normalizeQueueOptions } from "../../config-
|
|
1
|
+
import { t as queueErrorDiagnostics } from "../../error-diagnostics-DukggGP6.js";
|
|
2
|
+
import { _ as runWithActiveCloudflareEnv, c as loadQueueDefinition, d as setQueueRuntimeConfig, h as createCloudflareRuntimeEvent, m as setQueueRuntimeRegistry, u as runWithQueueRuntimeEvent } from "../../state-ER1YdCBF.js";
|
|
3
|
+
import { t as internalErrorDiagnostics } from "../../error-diagnostics-C0KyI408.js";
|
|
4
|
+
import { n as normalizeQueueOptions } from "../../config-YEuaeQVq.js";
|
|
5
5
|
import { n as getCloudflareQueueDefinitionName } from "../../cloudflare-GosJefIK.js";
|
|
6
|
-
import { t as getCloudflareQueueName } from "../../cloudflare-resource-name-
|
|
7
|
-
import { n as createCloudflareQueueBatchHandler, t as createCloudflareQueueRuntimeClient } from "../../cloudflare-client-
|
|
6
|
+
import { t as getCloudflareQueueName } from "../../cloudflare-resource-name-BUNNqgkg.js";
|
|
7
|
+
import { n as createCloudflareQueueBatchHandler, t as createCloudflareQueueRuntimeClient } from "../../cloudflare-client-BasaJ68s.js";
|
|
8
8
|
import { runInRunScopes } from "@vite-hub/runtime/internal/run-scope";
|
|
9
9
|
//#region ../internal/src/runtime/app.ts
|
|
10
10
|
function resolveAppFetch(label, app) {
|
|
@@ -96,13 +96,14 @@ function createQueueCloudflareWorker(options = {}) {
|
|
|
96
96
|
const definition = await loadQueueDefinition(definitionName);
|
|
97
97
|
if (!definition) throw queueErrorDiagnostics.QUEUE_DEFINITION_NOT_FOUND({ queue: definitionName });
|
|
98
98
|
await createCloudflareQueueBatchHandler({
|
|
99
|
-
concurrency: definition.
|
|
100
|
-
onError: definition.
|
|
99
|
+
concurrency: definition.concurrency,
|
|
100
|
+
onError: definition.onError,
|
|
101
101
|
onMessage: async (message, currentBatch) => {
|
|
102
102
|
await runWithQueueRuntimeEvent(runtimeEvent, async () => {
|
|
103
103
|
await runInRunScopes(() => definition.handler(createQueueJob(message, currentBatch)));
|
|
104
104
|
});
|
|
105
|
-
}
|
|
105
|
+
},
|
|
106
|
+
queue: definitionName
|
|
106
107
|
})(batch);
|
|
107
108
|
});
|
|
108
109
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as LocalQueueEngine } from "../../local-
|
|
3
|
-
import { n as QueueRuntimeOptions, t as QueueRuntimeDriverOptions } from "../../types-
|
|
1
|
+
import { b as QueueClient, h as LocalQueueStatus } from "../../types-klaR79hV.js";
|
|
2
|
+
import { t as LocalQueueEngine } from "../../local-DAL5ZgDg.js";
|
|
3
|
+
import { n as QueueRuntimeOptions, t as QueueRuntimeDriverOptions } from "../../types-D6hDNcMY.js";
|
|
4
4
|
//#region src/internal/runtime/local-client.d.ts
|
|
5
5
|
export declare function createLocalQueueRuntimeClient(options: QueueRuntimeDriverOptions): Promise<QueueClient>;
|
|
6
6
|
/**
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { t as queueErrorDiagnostics } from "../../error-diagnostics-
|
|
2
|
-
import { c as loadQueueDefinition, l as missingQueueDefinitionError } from "../../state-
|
|
3
|
-
import { t as defaultLocalQueueBase } from "../../config-
|
|
4
|
-
import { i as localQueueDeliveryRules, n as createLocalQueueEngine, t as createLocalQueueClient } from "../../local-
|
|
1
|
+
import { t as queueErrorDiagnostics } from "../../error-diagnostics-DukggGP6.js";
|
|
2
|
+
import { c as loadQueueDefinition, l as missingQueueDefinitionError } from "../../state-ER1YdCBF.js";
|
|
3
|
+
import { t as defaultLocalQueueBase } from "../../config-YEuaeQVq.js";
|
|
4
|
+
import { i as localQueueDeliveryRules, n as createLocalQueueEngine, t as createLocalQueueClient } from "../../local-CnBd5jdm.js";
|
|
5
5
|
import { resolve } from "pathe";
|
|
6
6
|
//#region src/internal/runtime/local-client.ts
|
|
7
7
|
const engines = /* @__PURE__ */ new Map();
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import "../../local-
|
|
3
|
-
import { n as QueueRuntimeOptions, t as QueueRuntimeDriverOptions } from "../../types-
|
|
1
|
+
import { b as QueueClient } from "../../types-klaR79hV.js";
|
|
2
|
+
import "../../local-DAL5ZgDg.js";
|
|
3
|
+
import { n as QueueRuntimeOptions, t as QueueRuntimeDriverOptions } from "../../types-D6hDNcMY.js";
|
|
4
4
|
import postgres from "postgres";
|
|
5
5
|
//#region src/providers/postgres.d.ts
|
|
6
6
|
/** A postgres.js client. */
|
|
@@ -24,12 +24,13 @@ export type PostgresQueueDatabases = Readonly<Record<string, {
|
|
|
24
24
|
export type PostgresQueueDatabasesLoader = () => Promise<PostgresQueueDatabases>;
|
|
25
25
|
interface ResolvedConnection {
|
|
26
26
|
connection: PostgresQueueConnection;
|
|
27
|
-
/** The URL, or the client of the Database. A Database can replace its client when its environment changes. */
|
|
28
27
|
key: object | string;
|
|
29
28
|
}
|
|
30
29
|
/**
|
|
31
30
|
* The connection of the jobs: `QUEUE_POSTGRES_URL` when it is set, else the postgres.js client of the Database that the
|
|
32
31
|
* build found on Postgres. The jobs then use the connection that the Database resolves, whatever variable it reads.
|
|
32
|
+
* Another driver, such as a node-postgres Pool, cannot run the jobs, so they get a postgres.js client on the connection
|
|
33
|
+
* URL of the Database.
|
|
33
34
|
*/
|
|
34
35
|
export declare function resolvePostgresQueueConnection(config: {
|
|
35
36
|
database?: string;
|
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import { t as queueErrorDiagnostics } from "../../error-diagnostics-
|
|
2
|
-
import { c as
|
|
3
|
-
import { c as loadQueueDefinition, l as missingQueueDefinitionError, p as setQueueRuntimeJobStore, s as listQueueDefinitionNames } from "../../state-
|
|
4
|
-
import { r as postgresQueueUrlVariable } from "../../config-
|
|
1
|
+
import { t as queueErrorDiagnostics } from "../../error-diagnostics-DukggGP6.js";
|
|
2
|
+
import { c as queueRemovedOptionsError, d as normalizeQueueEnqueueInput, l as queueUnsupportedOptionsError, s as queueProviderResult, t as readQueueTimes, u as createQueueMessageId } from "../../duration-CshzgSSx.js";
|
|
3
|
+
import { c as loadQueueDefinition, l as missingQueueDefinitionError, p as setQueueRuntimeJobStore, s as listQueueDefinitionNames } from "../../state-ER1YdCBF.js";
|
|
4
|
+
import { r as postgresQueueUrlVariable } from "../../config-YEuaeQVq.js";
|
|
5
5
|
import { t as invariant } from "../../invariant-D8_xBqmZ.js";
|
|
6
|
-
import { i as reportQueueDeliveryError, n as createQueueDeliveryErrorReport, r as isNonRetryableQueueError, t as checkQueueHandlerResponse } from "../../handler-response-
|
|
7
|
-
import { i as localQueueDeliveryRules, r as delayMs } from "../../local-
|
|
6
|
+
import { i as reportQueueDeliveryError, n as createQueueDeliveryErrorReport, r as isNonRetryableQueueError, t as checkQueueHandlerResponse } from "../../handler-response-ChtAjPNo.js";
|
|
7
|
+
import { i as localQueueDeliveryRules, r as delayMs } from "../../local-CnBd5jdm.js";
|
|
8
8
|
import { hostname } from "node:os";
|
|
9
9
|
import { randomUUID } from "node:crypto";
|
|
10
10
|
import { runInRunScopes } from "@vite-hub/runtime/internal/run-scope";
|
|
11
11
|
import postgres from "postgres";
|
|
12
|
-
/** Jobs that `jobs()` lists at most. */
|
|
13
12
|
const listLimit = 1e3;
|
|
14
13
|
const identifier = /^[a-z_][a-z\d_]{0,62}$/;
|
|
15
14
|
/** Returns `true` for a postgres.js client, such as the `$client` of a Postgres Database. */
|
|
@@ -65,7 +64,6 @@ function createPostgresQueueEngine(options) {
|
|
|
65
64
|
let pumping;
|
|
66
65
|
let pumpAgain = false;
|
|
67
66
|
let closed = false;
|
|
68
|
-
/** Creates the table once per process. Concurrent cold starts wait for one transaction lock, so they do not race. */
|
|
69
67
|
function ensureTable() {
|
|
70
68
|
ready ??= (async () => {
|
|
71
69
|
try {
|
|
@@ -120,12 +118,11 @@ function createPostgresQueueEngine(options) {
|
|
|
120
118
|
if (known !== void 0) return known;
|
|
121
119
|
const definition = await options.load(queue);
|
|
122
120
|
if (!definition) return void 0;
|
|
123
|
-
const limit = Number(definition.
|
|
121
|
+
const limit = Number(definition.concurrency ?? 1);
|
|
124
122
|
const value = Number.isFinite(limit) ? Math.max(1, Math.floor(limit)) : 1;
|
|
125
123
|
concurrency.set(queue, value);
|
|
126
124
|
return value;
|
|
127
125
|
}
|
|
128
|
-
/** Takes due jobs while a Queue Definition has a free slot, and starts them. */
|
|
129
126
|
async function pump() {
|
|
130
127
|
let wait = pollIntervalMs;
|
|
131
128
|
try {
|
|
@@ -402,8 +399,10 @@ function createPostgresQueueClient(engine, queue, resolveEngine) {
|
|
|
402
399
|
const normalized = normalizeQueueEnqueueInput(payload, options);
|
|
403
400
|
const optionsError = queueUnsupportedOptionsError(normalized.options, "postgres", engine.rules.unsupportedOptions);
|
|
404
401
|
if (optionsError) return [optionsError, void 0];
|
|
402
|
+
const [timeError, times] = readQueueTimes(normalized.options);
|
|
403
|
+
if (timeError) return [timeError, void 0];
|
|
405
404
|
const [error, ids] = await queueProviderResult("postgres", "send", () => enqueue([{
|
|
406
|
-
delaySeconds:
|
|
405
|
+
delaySeconds: times.delaySeconds,
|
|
407
406
|
id: normalized.id,
|
|
408
407
|
idempotencyKey: normalized.options.idempotencyKey,
|
|
409
408
|
payload: normalized.payload
|
|
@@ -415,12 +414,20 @@ function createPostgresQueueClient(engine, queue, resolveEngine) {
|
|
|
415
414
|
}];
|
|
416
415
|
},
|
|
417
416
|
async sendBatch(items, options) {
|
|
418
|
-
const
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
417
|
+
const removed = queueRemovedOptionsError("postgres", options, items);
|
|
418
|
+
if (removed) return [removed, void 0];
|
|
419
|
+
const entries = [];
|
|
420
|
+
for (const item of items) {
|
|
421
|
+
const [timeError, times] = readQueueTimes({ delay: item.delay ?? options?.delay });
|
|
422
|
+
if (timeError) return [timeError, void 0];
|
|
423
|
+
entries.push({
|
|
424
|
+
delaySeconds: times.delaySeconds,
|
|
425
|
+
id: createQueueMessageId(),
|
|
426
|
+
idempotencyKey: item.idempotencyKey,
|
|
427
|
+
payload: item.body
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
const [error, ids] = await queueProviderResult("postgres", "send-batch", () => enqueue(entries));
|
|
424
431
|
if (error) return [error, void 0];
|
|
425
432
|
return [void 0, ids.map((messageId) => ({
|
|
426
433
|
messageId,
|
|
@@ -432,6 +439,8 @@ function createPostgresQueueClient(engine, queue, resolveEngine) {
|
|
|
432
439
|
//#endregion
|
|
433
440
|
//#region src/internal/runtime/postgres-client.ts
|
|
434
441
|
const postgresUrl = /^postgres(?:ql)?:\/\//i;
|
|
442
|
+
/** A Postgres Database answers this key with a function that returns its connection URL. `@vite-hub/database` sets it. */
|
|
443
|
+
const databaseConnectionUrl = Symbol.for("vitehub.database.postgres.connection-url");
|
|
435
444
|
let loadDatabases;
|
|
436
445
|
let closing;
|
|
437
446
|
const engines = /* @__PURE__ */ new Map();
|
|
@@ -459,6 +468,8 @@ function startWorkers(state, engine) {
|
|
|
459
468
|
/**
|
|
460
469
|
* The connection of the jobs: `QUEUE_POSTGRES_URL` when it is set, else the postgres.js client of the Database that the
|
|
461
470
|
* build found on Postgres. The jobs then use the connection that the Database resolves, whatever variable it reads.
|
|
471
|
+
* Another driver, such as a node-postgres Pool, cannot run the jobs, so they get a postgres.js client on the connection
|
|
472
|
+
* URL of the Database.
|
|
462
473
|
*/
|
|
463
474
|
async function resolvePostgresQueueConnection(config, databases = loadDatabases, env = process.env) {
|
|
464
475
|
const url = env[postgresQueueUrlVariable]?.trim();
|
|
@@ -476,6 +487,12 @@ async function resolvePostgresQueueConnection(config, databases = loadDatabases,
|
|
|
476
487
|
connection: { client },
|
|
477
488
|
key: client
|
|
478
489
|
};
|
|
490
|
+
const readUrl = entry ? Reflect.get(entry.db, databaseConnectionUrl) : void 0;
|
|
491
|
+
const databaseUrl = typeof readUrl === "function" ? readUrl() : void 0;
|
|
492
|
+
if (typeof databaseUrl === "string") return {
|
|
493
|
+
connection: { url: databaseUrl },
|
|
494
|
+
key: databaseUrl
|
|
495
|
+
};
|
|
479
496
|
}
|
|
480
497
|
throw queueErrorDiagnostics.QUEUE_POSTGRES_MISSING_CONNECTION({ database: config.database });
|
|
481
498
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { n as QueueRuntimeOptions, t as QueueRuntimeDriverOptions } from "../../types-
|
|
1
|
+
import { A as QueueJobStore, C as QueueDefinitionRegistry, b as QueueClient, x as QueueDefinition } from "../../types-klaR79hV.js";
|
|
2
|
+
import { n as QueueRuntimeOptions, t as QueueRuntimeDriverOptions } from "../../types-D6hDNcMY.js";
|
|
3
3
|
//#region src/internal/runtime/state.d.ts
|
|
4
4
|
export type QueueRuntimeClientFactory = (options: QueueRuntimeDriverOptions) => QueueClient | Promise<QueueClient>;
|
|
5
5
|
export declare function missingQueueDefinitionError(queue: string): Error;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as getQueueRuntimeEvent, c as loadQueueDefinition, d as setQueueRuntimeConfig, f as setQueueRuntimeEventDefaults, i as getQueueRuntimeConfig, l as missingQueueDefinitionError, m as setQueueRuntimeRegistry, n as getOrCreateQueueClient, o as getQueueRuntimeJobStore, p as setQueueRuntimeJobStore, r as getQueueRuntimeClientFactory, s as listQueueDefinitionNames, t as enterQueueRuntimeEvent, u as runWithQueueRuntimeEvent } from "../../state-
|
|
1
|
+
import { a as getQueueRuntimeEvent, c as loadQueueDefinition, d as setQueueRuntimeConfig, f as setQueueRuntimeEventDefaults, i as getQueueRuntimeConfig, l as missingQueueDefinitionError, m as setQueueRuntimeRegistry, n as getOrCreateQueueClient, o as getQueueRuntimeJobStore, p as setQueueRuntimeJobStore, r as getQueueRuntimeClientFactory, s as listQueueDefinitionNames, t as enterQueueRuntimeEvent, u as runWithQueueRuntimeEvent } from "../../state-ER1YdCBF.js";
|
|
2
2
|
export { enterQueueRuntimeEvent, getOrCreateQueueClient, getQueueRuntimeClientFactory, getQueueRuntimeConfig, getQueueRuntimeEvent, getQueueRuntimeJobStore, listQueueDefinitionNames, loadQueueDefinition, missingQueueDefinitionError, runWithQueueRuntimeEvent, setQueueRuntimeConfig, setQueueRuntimeEventDefaults, setQueueRuntimeJobStore, setQueueRuntimeRegistry };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { T as QueueDriverOptions, b as QueueClient } from "../../types-klaR79hV.js";
|
|
2
2
|
//#region src/internal/runtime/vercel-client.d.ts
|
|
3
3
|
export declare function createVercelQueueRuntimeClient(options: QueueDriverOptions): Promise<QueueClient>;
|
|
4
4
|
//#endregion
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { t as queueErrorDiagnostics } from "../../error-diagnostics-
|
|
2
|
-
import { c as
|
|
3
|
-
import { t as resolveVercelQueueRegion } from "../../vercel-region-
|
|
1
|
+
import { t as queueErrorDiagnostics } from "../../error-diagnostics-DukggGP6.js";
|
|
2
|
+
import { c as queueRemovedOptionsError, d as normalizeQueueEnqueueInput, l as queueUnsupportedOptionsError, s as queueProviderResult, t as readQueueTimes, u as createQueueMessageId } from "../../duration-CshzgSSx.js";
|
|
3
|
+
import { t as resolveVercelQueueRegion } from "../../vercel-region-DOciq2kx.js";
|
|
4
4
|
//#region src/providers/vercel.ts
|
|
5
5
|
function invalidVercelSendResponse(cause) {
|
|
6
6
|
return [queueErrorDiagnostics.QUEUE_PROVIDER_INVALID_RESPONSE({ cause }), void 0];
|
|
@@ -48,7 +48,6 @@ function isVercelQueueSDK(value) {
|
|
|
48
48
|
};
|
|
49
49
|
return hasCallableMethod("send") && hasCallableMethod("handleCallback");
|
|
50
50
|
}
|
|
51
|
-
/** The cause of `QUEUE_PROVIDER_FAILED` when Vercel rejects jobs of a batch. `results` has each job's result in input order. */
|
|
52
51
|
function batchRejected(results) {
|
|
53
52
|
const failed = results.filter((result) => result.status === "failed").length;
|
|
54
53
|
return Object.assign(queueErrorDiagnostics.QUEUE_VERCEL_BATCH_REJECTED({
|
|
@@ -125,11 +124,13 @@ async function createVercelQueueClient(provider) {
|
|
|
125
124
|
const normalized = normalizeQueueEnqueueInput(payload, options);
|
|
126
125
|
const optionsError = queueUnsupportedOptionsError(normalized.options, "vercel");
|
|
127
126
|
if (optionsError) return [optionsError, void 0];
|
|
127
|
+
const [timeError, times] = readQueueTimes(normalized.options, "ceil");
|
|
128
|
+
if (timeError) return [timeError, void 0];
|
|
128
129
|
const [error, response] = await queueProviderResult("vercel", "send", () => client.send(topic, normalized.payload, {
|
|
129
|
-
delaySeconds:
|
|
130
|
+
delaySeconds: times.delaySeconds,
|
|
130
131
|
idempotencyKey: normalized.options.idempotencyKey || normalized.id,
|
|
131
132
|
region: normalized.options.region ?? provider.region,
|
|
132
|
-
retentionSeconds:
|
|
133
|
+
retentionSeconds: times.retentionSeconds
|
|
133
134
|
}));
|
|
134
135
|
if (error) return [error, void 0];
|
|
135
136
|
const [responseError, messageId] = parseVercelMessageId(response);
|
|
@@ -140,12 +141,22 @@ async function createVercelQueueClient(provider) {
|
|
|
140
141
|
}];
|
|
141
142
|
},
|
|
142
143
|
async sendBatch(items, options) {
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
144
|
+
const removed = queueRemovedOptionsError("vercel", options, items);
|
|
145
|
+
if (removed) return [removed, void 0];
|
|
146
|
+
const messages = [];
|
|
147
|
+
for (const item of items) {
|
|
148
|
+
const [timeError, times] = readQueueTimes({
|
|
149
|
+
delay: item.delay ?? options?.delay,
|
|
150
|
+
retention: item.retention ?? options?.retention
|
|
151
|
+
}, "ceil");
|
|
152
|
+
if (timeError) return [timeError, void 0];
|
|
153
|
+
messages.push({
|
|
154
|
+
delaySeconds: times.delaySeconds,
|
|
155
|
+
idempotencyKey: item.idempotencyKey || createQueueMessageId(),
|
|
156
|
+
payload: item.body,
|
|
157
|
+
retentionSeconds: times.retentionSeconds
|
|
158
|
+
});
|
|
159
|
+
}
|
|
149
160
|
const [error, response] = await queueProviderResult("vercel", "send-batch", async () => {
|
|
150
161
|
if (typeof client.sendBatch !== "function") throw queueErrorDiagnostics.QUEUE_VERCEL_SEND_BATCH_UNAVAILABLE();
|
|
151
162
|
const results = await client.sendBatch(topic, messages);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as QueueRuntimeDriverOptions } from "../../types-
|
|
1
|
+
import { b as QueueClient } from "../../types-klaR79hV.js";
|
|
2
|
+
import { t as QueueRuntimeDriverOptions } from "../../types-D6hDNcMY.js";
|
|
3
3
|
//#region src/internal/runtime/worker-client.d.ts
|
|
4
4
|
/** Worker package conditions keep the Node driver and its optional peer outside the build graph. */
|
|
5
5
|
export declare function createPostgresQueueRuntimeClient(options: QueueRuntimeDriverOptions): Promise<QueueClient>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as queueErrorDiagnostics } from "../../error-diagnostics-
|
|
1
|
+
import { t as queueErrorDiagnostics } from "../../error-diagnostics-DukggGP6.js";
|
|
2
2
|
//#region src/internal/runtime/worker-client.ts
|
|
3
3
|
/** Worker package conditions keep the Node driver and its optional peer outside the build graph. */
|
|
4
4
|
async function createPostgresQueueRuntimeClient(options) {
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import { t as queueErrorDiagnostics } from "./error-diagnostics-
|
|
2
|
-
import {
|
|
3
|
-
import { i as reportQueueDeliveryError, n as createQueueDeliveryErrorReport, r as isNonRetryableQueueError, t as checkQueueHandlerResponse } from "./handler-response-
|
|
1
|
+
import { t as queueErrorDiagnostics } from "./error-diagnostics-DukggGP6.js";
|
|
2
|
+
import { c as queueRemovedOptionsError, d as normalizeQueueEnqueueInput, i as localUnsupportedEnqueueOptions, l as queueUnsupportedOptionsError, n as cloudflareUnsupportedEnqueueOptions, o as postgresUnsupportedEnqueueOptions, s as queueProviderResult, t as readQueueTimes, u as createQueueMessageId } from "./duration-CshzgSSx.js";
|
|
3
|
+
import { i as reportQueueDeliveryError, n as createQueueDeliveryErrorReport, r as isNonRetryableQueueError, t as checkQueueHandlerResponse } from "./handler-response-ChtAjPNo.js";
|
|
4
4
|
import { mkdir, readFile, readdir, rename, rm, writeFile } from "node:fs/promises";
|
|
5
5
|
import { join, resolve } from "pathe";
|
|
6
6
|
import { randomUUID } from "node:crypto";
|
|
7
7
|
import { runInRunScopes } from "@vite-hub/runtime/internal/run-scope";
|
|
8
8
|
const maxTimerDelay = 2 ** 31 - 1;
|
|
9
|
+
const storeRetryMaxMs = 3e4;
|
|
10
|
+
function storeRetryDelayMs(failures) {
|
|
11
|
+
return Math.min(10 * 2 ** (failures - 1), storeRetryMaxMs);
|
|
12
|
+
}
|
|
9
13
|
/** Retries after 1, 2, and 4 seconds, up to 60 seconds. The local and postgres providers use it. */
|
|
10
14
|
function defaultRetryDelayMs(attempts) {
|
|
11
15
|
return Math.min(2 ** (attempts - 1), 60) * 1e3;
|
|
@@ -64,7 +68,6 @@ const providerNames = {
|
|
|
64
68
|
postgres: "The postgres provider",
|
|
65
69
|
vercel: "Vercel"
|
|
66
70
|
};
|
|
67
|
-
/** The JSON copy of the payload and its size in bytes. */
|
|
68
71
|
function toStoredPayload(payload) {
|
|
69
72
|
const text = JSON.stringify(payload);
|
|
70
73
|
return text === void 0 ? {
|
|
@@ -142,7 +145,7 @@ function toJobSummary(job, running) {
|
|
|
142
145
|
...job.failedAt === void 0 ? {} : { failedAt: isoTime(job.failedAt) },
|
|
143
146
|
id: job.id,
|
|
144
147
|
queue: job.queue,
|
|
145
|
-
state: job.state === "dead" ? "dead" :
|
|
148
|
+
state: running ? "running" : job.state === "dead" ? "dead" : "pending"
|
|
146
149
|
};
|
|
147
150
|
}
|
|
148
151
|
function createLocalQueueEngine(options) {
|
|
@@ -155,11 +158,13 @@ function createLocalQueueEngine(options) {
|
|
|
155
158
|
const writingKeys = /* @__PURE__ */ new Set();
|
|
156
159
|
const expiringKeys = /* @__PURE__ */ new Set();
|
|
157
160
|
const concurrency = /* @__PURE__ */ new Map();
|
|
161
|
+
const storeFailures = /* @__PURE__ */ new WeakMap();
|
|
158
162
|
let enqueueTail = Promise.resolve();
|
|
159
163
|
let started;
|
|
160
164
|
let timer;
|
|
161
165
|
let closed = false;
|
|
162
166
|
const file = (job) => join(base, `${job.key}.json`);
|
|
167
|
+
const attemptInFlight = (job) => runningKeys.has(job.key) || expiringKeys.has(job.key);
|
|
163
168
|
async function persist(job) {
|
|
164
169
|
const target = file(job);
|
|
165
170
|
await writeFile(`${target}.tmp`, JSON.stringify(job), "utf8");
|
|
@@ -194,7 +199,6 @@ function createLocalQueueEngine(options) {
|
|
|
194
199
|
}
|
|
195
200
|
arm(next);
|
|
196
201
|
}
|
|
197
|
-
/** Deletes the oldest dead jobs of `queue` above `localQueueDeadLimit`. */
|
|
198
202
|
async function pruneDead(queue) {
|
|
199
203
|
const dead = [...jobs.values()].filter((job) => job.queue === queue && job.state === "dead").sort((a, b) => (a.failedAt ?? 0) - (b.failedAt ?? 0));
|
|
200
204
|
for (const job of dead.slice(0, Math.max(0, dead.length - 100))) {
|
|
@@ -205,6 +209,7 @@ function createLocalQueueEngine(options) {
|
|
|
205
209
|
}
|
|
206
210
|
}
|
|
207
211
|
async function markDead(job, error, reason = `after ${job.attempts} attempts`) {
|
|
212
|
+
storeFailures.delete(job);
|
|
208
213
|
job.state = "dead";
|
|
209
214
|
job.error = error;
|
|
210
215
|
job.failedAt = Date.now();
|
|
@@ -213,7 +218,6 @@ function createLocalQueueEngine(options) {
|
|
|
213
218
|
delete job.payload;
|
|
214
219
|
await pruneDead(job.queue);
|
|
215
220
|
}
|
|
216
|
-
/** A job with a retention is not delivered after it expires, as on Vercel. */
|
|
217
221
|
async function expire(job) {
|
|
218
222
|
expiringKeys.add(job.key);
|
|
219
223
|
try {
|
|
@@ -234,7 +238,7 @@ function createLocalQueueEngine(options) {
|
|
|
234
238
|
jobs.delete(job.key);
|
|
235
239
|
} catch (error) {
|
|
236
240
|
console.error(`[vitehub:queue] The local provider ran a job, but cannot delete ${file(job)}.`, error);
|
|
237
|
-
await persist(job).catch(() => {});
|
|
241
|
+
await persist(job).catch((persistError) => console.error(`[vitehub:queue] The local provider cannot mark ${file(job)} done either. The job runs again after a restart.`, persistError));
|
|
238
242
|
}
|
|
239
243
|
}
|
|
240
244
|
async function deliver(job) {
|
|
@@ -253,12 +257,13 @@ function createLocalQueueEngine(options) {
|
|
|
253
257
|
...job,
|
|
254
258
|
attempts
|
|
255
259
|
});
|
|
260
|
+
storeFailures.delete(job);
|
|
256
261
|
job.attempts = attempts;
|
|
257
262
|
let handled = false;
|
|
258
263
|
try {
|
|
259
264
|
const definition = await options.load(job.queue);
|
|
260
265
|
if (!definition) throw options.missing(job.queue);
|
|
261
|
-
const limit = Number(definition.
|
|
266
|
+
const limit = Number(definition.concurrency ?? 1);
|
|
262
267
|
concurrency.set(job.queue, Number.isFinite(limit) ? Math.max(1, Math.floor(limit)) : 1);
|
|
263
268
|
schedule();
|
|
264
269
|
await runInRunScopes(async () => {
|
|
@@ -287,7 +292,11 @@ function createLocalQueueEngine(options) {
|
|
|
287
292
|
}
|
|
288
293
|
if (handled) await complete(job);
|
|
289
294
|
} catch (storeError) {
|
|
290
|
-
job.
|
|
295
|
+
if (job.state === "pending") {
|
|
296
|
+
const failures = (storeFailures.get(job) ?? 0) + 1;
|
|
297
|
+
storeFailures.set(job, failures);
|
|
298
|
+
job.availableAt = Date.now() + Math.max(retryDelayMs(Math.max(1, job.attempts)), storeRetryDelayMs(failures));
|
|
299
|
+
}
|
|
291
300
|
console.error(`[vitehub:queue] The local provider cannot update the job file in ${base}.`, storeError);
|
|
292
301
|
} finally {
|
|
293
302
|
runningKeys.delete(job.key);
|
|
@@ -327,7 +336,6 @@ function createLocalQueueEngine(options) {
|
|
|
327
336
|
for (const queue of new Set(recovered.map((job) => job.queue))) await pruneDead(queue);
|
|
328
337
|
schedule();
|
|
329
338
|
}
|
|
330
|
-
/** The newest job with this message ID. Message IDs from the caller can repeat. */
|
|
331
339
|
function findJob(id) {
|
|
332
340
|
let found;
|
|
333
341
|
for (const job of jobs.values()) {
|
|
@@ -336,7 +344,6 @@ function createLocalQueueEngine(options) {
|
|
|
336
344
|
}
|
|
337
345
|
return found;
|
|
338
346
|
}
|
|
339
|
-
/** Memory drops the payload of a dead job. Its file keeps it. */
|
|
340
347
|
async function readPayload(job) {
|
|
341
348
|
if (job.state !== "dead") return Object.hasOwn(job, "payload") ? { payload: job.payload } : {};
|
|
342
349
|
const stored = JSON.parse(await readFile(file(job), "utf8"));
|
|
@@ -381,7 +388,7 @@ function createLocalQueueEngine(options) {
|
|
|
381
388
|
});
|
|
382
389
|
checkBatch(entries, prepared.reduce((total, item) => total + item.bytes, 0), rules);
|
|
383
390
|
for (const { availableAt, entry, expiresAt, stored } of prepared) {
|
|
384
|
-
const duplicate = entry.idempotencyKey === void 0 ? void 0 : [...jobs.values(), ...created].find((job) => job.queue === queue && job.state === "pending"
|
|
391
|
+
const duplicate = entry.idempotencyKey === void 0 ? void 0 : [...jobs.values(), ...created].find((job) => job.queue === queue && job.idempotencyKey === entry.idempotencyKey && (job.state === "pending" || job.state === "dead" && attemptInFlight(job)));
|
|
385
392
|
if (duplicate) {
|
|
386
393
|
ids.push(duplicate.id);
|
|
387
394
|
continue;
|
|
@@ -424,7 +431,7 @@ function createLocalQueueEngine(options) {
|
|
|
424
431
|
const job = findJob(id);
|
|
425
432
|
if (!job) return void 0;
|
|
426
433
|
return {
|
|
427
|
-
...toJobSummary(job,
|
|
434
|
+
...toJobSummary(job, attemptInFlight(job)),
|
|
428
435
|
file: file(job),
|
|
429
436
|
...job.idempotencyKey === void 0 ? {} : { idempotencyKey: job.idempotencyKey },
|
|
430
437
|
...await readPayload(job)
|
|
@@ -432,13 +439,13 @@ function createLocalQueueEngine(options) {
|
|
|
432
439
|
},
|
|
433
440
|
async jobs() {
|
|
434
441
|
await this.start();
|
|
435
|
-
return [...jobs.values()].filter((job) => job.state !== "done" && !writingKeys.has(job.key)).sort((a, b) => b.enqueuedAt - a.enqueuedAt).map((job) => toJobSummary(job,
|
|
442
|
+
return [...jobs.values()].filter((job) => job.state !== "done" && !writingKeys.has(job.key)).sort((a, b) => b.enqueuedAt - a.enqueuedAt).map((job) => toJobSummary(job, attemptInFlight(job)));
|
|
436
443
|
},
|
|
437
444
|
async retry(id) {
|
|
438
445
|
await this.start();
|
|
439
446
|
const job = findJob(id);
|
|
440
447
|
if (!job) return "missing";
|
|
441
|
-
if (
|
|
448
|
+
if (attemptInFlight(job)) return "running";
|
|
442
449
|
const now = Date.now();
|
|
443
450
|
if (job.state === "dead") {
|
|
444
451
|
const { payload } = await readPayload(job);
|
|
@@ -458,12 +465,12 @@ function createLocalQueueEngine(options) {
|
|
|
458
465
|
await persist(retry);
|
|
459
466
|
jobs.set(job.key, retry);
|
|
460
467
|
schedule();
|
|
461
|
-
return toJobSummary(retry,
|
|
468
|
+
return toJobSummary(retry, attemptInFlight(retry));
|
|
462
469
|
}
|
|
463
470
|
job.availableAt = now;
|
|
464
471
|
await persist(job);
|
|
465
472
|
schedule();
|
|
466
|
-
return toJobSummary(job,
|
|
473
|
+
return toJobSummary(job, attemptInFlight(job));
|
|
467
474
|
},
|
|
468
475
|
provider: "local",
|
|
469
476
|
rules,
|
|
@@ -481,8 +488,8 @@ function createLocalQueueEngine(options) {
|
|
|
481
488
|
pending: 0,
|
|
482
489
|
running: 0
|
|
483
490
|
};
|
|
484
|
-
if (job
|
|
485
|
-
else if (
|
|
491
|
+
if (attemptInFlight(job)) entry.running++;
|
|
492
|
+
else if (job.state === "dead") entry.dead++;
|
|
486
493
|
else entry.pending++;
|
|
487
494
|
}
|
|
488
495
|
return {
|
|
@@ -494,11 +501,11 @@ function createLocalQueueEngine(options) {
|
|
|
494
501
|
}
|
|
495
502
|
};
|
|
496
503
|
}
|
|
497
|
-
/** The provider of the rules rejects the option, so `vite dev` fails as the host does. */
|
|
498
504
|
function unsupportedOptions(options, rules) {
|
|
499
505
|
return queueUnsupportedOptionsError(options, rules.rules, rules.unsupportedOptions);
|
|
500
506
|
}
|
|
501
507
|
function createLocalQueueClient(engine, queue) {
|
|
508
|
+
const round = engine.rules.rules === "vercel" || engine.rules.rules === "cloudflare" ? "ceil" : void 0;
|
|
502
509
|
return {
|
|
503
510
|
provider: "local",
|
|
504
511
|
native: engine,
|
|
@@ -507,12 +514,13 @@ function createLocalQueueClient(engine, queue) {
|
|
|
507
514
|
const normalized = normalizeQueueEnqueueInput(payload, options);
|
|
508
515
|
const optionsError = unsupportedOptions(normalized.options, engine.rules);
|
|
509
516
|
if (optionsError) return [optionsError, void 0];
|
|
517
|
+
const [timeError, times] = readQueueTimes(normalized.options, round);
|
|
518
|
+
if (timeError) return [timeError, void 0];
|
|
510
519
|
const [error, ids] = await queueProviderResult("local", "send", () => engine.enqueue(queue, [{
|
|
511
|
-
|
|
520
|
+
...times,
|
|
512
521
|
id: normalized.id,
|
|
513
522
|
idempotencyKey: normalized.options.idempotencyKey,
|
|
514
|
-
payload: normalized.payload
|
|
515
|
-
retentionSeconds: normalized.options.retentionSeconds
|
|
523
|
+
payload: normalized.payload
|
|
516
524
|
}]));
|
|
517
525
|
if (error) return [error, void 0];
|
|
518
526
|
return [void 0, {
|
|
@@ -521,13 +529,24 @@ function createLocalQueueClient(engine, queue) {
|
|
|
521
529
|
}];
|
|
522
530
|
},
|
|
523
531
|
async sendBatch(items, options) {
|
|
524
|
-
const
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
532
|
+
const removed = queueRemovedOptionsError(engine.rules.rules, options, items);
|
|
533
|
+
if (removed) return [removed, void 0];
|
|
534
|
+
const entries = [];
|
|
535
|
+
for (const item of items) {
|
|
536
|
+
const retention = ("retention" in item ? item.retention : void 0) ?? (options && "retention" in options ? options.retention : void 0);
|
|
537
|
+
const [timeError, times] = readQueueTimes({
|
|
538
|
+
delay: item.delay ?? options?.delay,
|
|
539
|
+
retention
|
|
540
|
+
}, round);
|
|
541
|
+
if (timeError) return [timeError, void 0];
|
|
542
|
+
entries.push({
|
|
543
|
+
...times,
|
|
544
|
+
id: createQueueMessageId(),
|
|
545
|
+
idempotencyKey: item.idempotencyKey,
|
|
546
|
+
payload: item.body
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
const [error, ids] = await queueProviderResult("local", "send-batch", () => engine.enqueue(queue, entries));
|
|
531
550
|
if (error) return [error, void 0];
|
|
532
551
|
return [void 0, ids.map((messageId) => ({
|
|
533
552
|
messageId,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { A as QueueJobStore } from "./types-klaR79hV.js";
|
|
2
2
|
//#region src/providers/local.d.ts
|
|
3
|
+
/** One job for the engine. `vite dev` and the clients read `delay` and `retention` into seconds first. */
|
|
3
4
|
interface LocalQueueEntry {
|
|
4
5
|
delaySeconds?: number;
|
|
5
6
|
id: string;
|
|
@@ -104,6 +104,11 @@ interface OperationAccess {
|
|
|
104
104
|
*/
|
|
105
105
|
readonly sections: "all" | readonly string[];
|
|
106
106
|
readonly user?: OperationUser;
|
|
107
|
+
/**
|
|
108
|
+
* Who signed the call: `"console"` when the ViteHub Console signed it for a person, whose `user` is then a Console
|
|
109
|
+
* account and not an app user, or `"cli"` for a key. Not set for other calls.
|
|
110
|
+
*/
|
|
111
|
+
readonly via?: "console" | "cli";
|
|
107
112
|
}
|
|
108
113
|
interface OperationContext {
|
|
109
114
|
/** The checked Console access of the call. */
|
|
@@ -115,6 +120,11 @@ interface OperationContext {
|
|
|
115
120
|
surface: "console" | "cli" | "mcp" | "agent";
|
|
116
121
|
/** Set by the host from generated build identity, never from request input or runtime NODE_ENV. */
|
|
117
122
|
development?: boolean;
|
|
123
|
+
/**
|
|
124
|
+
* The project root of a development server, for operations that read build files, such as `bundle.report`. Hosts set
|
|
125
|
+
* it only with `development`, so a deployed server never reads project files through it.
|
|
126
|
+
*/
|
|
127
|
+
projectRoot?: string;
|
|
118
128
|
env?: Record<string, unknown>;
|
|
119
129
|
waitUntil?: (task: Promise<unknown>) => void;
|
|
120
130
|
request?: Request;
|
|
@@ -161,7 +171,8 @@ interface OperationDefinition<Input, Output> {
|
|
|
161
171
|
/** Standard Schema (valibot in this repo). Its Standard JSON Schema builds CLI flags and the MCP input schema. */
|
|
162
172
|
input: StandardSchemaV1<unknown, Input>;
|
|
163
173
|
output?: StandardSchemaV1<unknown, Output>;
|
|
164
|
-
|
|
174
|
+
/** `false`: no `vitehub` command. The Console and MCP still call the operation. */
|
|
175
|
+
cli?: false | {
|
|
165
176
|
/** Defaults: namespace = section, command = verb. */
|
|
166
177
|
namespace?: string;
|
|
167
178
|
/** A space groups commands: `jobs list` and `jobs show` run as `vitehub <namespace> jobs list` and `jobs show`. */
|
|
@@ -202,7 +213,7 @@ interface OperationDefinition<Input, Output> {
|
|
|
202
213
|
/** An operation of any input and output, as a registry holds it. */
|
|
203
214
|
interface AnyOperation extends Omit<OperationDefinition<unknown, unknown>, "cli" | "input" | "mcp" | "output" | "readOnlyWhen" | "run" | "scope" | "section"> {
|
|
204
215
|
readOnlyWhen?: (input: never) => boolean;
|
|
205
|
-
cli?: {
|
|
216
|
+
cli?: false | {
|
|
206
217
|
namespace?: string;
|
|
207
218
|
command?: string;
|
|
208
219
|
default?: boolean;
|