@vite-hub/schedule 0.0.4 → 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 +139 -58
- package/dist/console-wEhr0LWn.d.ts +330 -0
- package/dist/definition.d.ts +8 -6
- package/dist/definition.js +34 -28
- package/dist/due-BVMRQ93v.js +144 -0
- package/dist/durability.d.ts +14 -0
- package/dist/durability.js +23 -0
- package/dist/error-diagnostics-DSkCJC_L.js +266 -0
- package/dist/error-diagnostics-Mpeks7J0.js +166 -0
- package/dist/index.d.ts +61 -40
- package/dist/index.js +27 -7
- package/dist/inspect-DTfAo5gS.js +118 -0
- package/dist/invariant-DolT9EEx.js +22 -0
- package/dist/kv-storage-C0KZZB6v.d.ts +18 -0
- package/dist/{kv-storage-DWinmLLy.js → kv-storage-iJChCS5A.js} +2 -2
- package/dist/object-Da1zyPCR.js +11 -0
- package/dist/operation-definition-8gOe-ubD.d.ts +234 -0
- package/dist/operations-DwZWFIUX.js +184 -0
- package/dist/operations.d.ts +84 -0
- package/dist/operations.js +318 -0
- package/dist/os.d.ts +167 -0
- package/dist/os.js +1659 -0
- package/dist/registry-module.d.ts +0 -5
- package/dist/runtime/console.d.ts +12 -0
- package/dist/runtime/console.js +85 -0
- package/dist/runtime/kv.d.ts +4 -0
- package/dist/{kv-BJ1I9IlJ.js → runtime/kv.js} +1 -1
- package/dist/runtime/process.d.ts +53 -5
- package/dist/runtime/process.js +501 -251
- package/dist/runtime/state.d.ts +2 -13
- package/dist/runtime/state.js +2 -2
- package/dist/runtime/static.d.ts +44 -12
- package/dist/runtime/static.js +2 -150
- package/dist/runtime.d.ts +38 -4
- package/dist/runtime.js +6 -3
- package/dist/started-BuGmnus4.js +201 -0
- package/dist/started-Bx7MDbrl.js +146 -0
- package/dist/started-errors-p7cu_rI7.js +250 -0
- package/dist/state-B-Fuyfb4.js +468 -0
- package/dist/state-Do2wV4Tx.d.ts +49 -0
- package/dist/static-DQwzzvPg.js +266 -0
- package/dist/store-Df0-fzCl.d.ts +53 -0
- package/dist/time-zone-SBldrUgt.js +59 -0
- package/dist/types-Cw5LjPuc.d.ts +167 -0
- package/dist/vite.d.ts +214 -11
- package/dist/vite.js +5876 -3
- package/dist/wait-until-Dy4Bn3ZN.js +112 -0
- package/package.json +65 -24
- package/dist/client-DpCD4YFE.js +0 -233
- package/dist/discovery-DkFAN7Wd.d.ts +0 -15
- package/dist/discovery-DvU3B6iD.js +0 -753
- package/dist/due-Bbh4yIxq.js +0 -59
- package/dist/execute-B-pii4wf.js +0 -211
- package/dist/nuxt.d.ts +0 -19
- package/dist/nuxt.js +0 -26
- package/dist/runtime/driver.d.ts +0 -26
- package/dist/runtime/driver.js +0 -337
- package/dist/state-By7WU_ks.js +0 -472
- package/dist/store-t-74rsM_.d.ts +0 -48
- package/dist/types-Bcje4dLV.d.ts +0 -136
- package/dist/vite-BHs17nWX.js +0 -4166
- package/dist/wait-until-BzIwzXw4.js +0 -23
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { n as isRecordOrFunction } from "./object-Da1zyPCR.js";
|
|
2
|
+
import { t as scheduleErrorDiagnostics } from "./error-diagnostics-Mpeks7J0.js";
|
|
3
|
+
import { a as validateScheduledAt, i as scheduleIdempotencyKey, n as isScheduleDate, o as createScheduleError, s as invalidScheduleValueDetails, t as runWithScheduleWaitUntil } from "./wait-until-Dy4Bn3ZN.js";
|
|
4
|
+
import { n as latestCronMinute, t as isScheduleDue } from "./due-BVMRQ93v.js";
|
|
5
|
+
import { normalizeRuntimeDiagnosticError } from "@vite-hub/runtime";
|
|
6
|
+
import { runInRunScopes } from "@vite-hub/runtime/internal/run-scope";
|
|
7
|
+
//#region ../internal/src/secret.ts
|
|
8
|
+
const secretEncoder = new TextEncoder();
|
|
9
|
+
/**
|
|
10
|
+
* Compares a request value with an expected secret in constant time for the length of `expected`. Returns `false`
|
|
11
|
+
* when either value is missing or empty. It uses no Node API, so it also runs in Worker runtimes.
|
|
12
|
+
*/
|
|
13
|
+
function isViteHubSecretEqual(actual, expected) {
|
|
14
|
+
if (!actual || !expected) return false;
|
|
15
|
+
const left = secretEncoder.encode(actual);
|
|
16
|
+
const right = secretEncoder.encode(expected);
|
|
17
|
+
let difference = left.length ^ right.length;
|
|
18
|
+
for (let index = 0; index < right.length; index += 1) difference |= (left[index] ?? 0) ^ right[index];
|
|
19
|
+
return difference === 0;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Compares the token of a `Bearer` `Authorization` header with an expected secret in constant time. The scheme is
|
|
23
|
+
* case-insensitive. Returns `false` for another scheme or a missing value.
|
|
24
|
+
*/
|
|
25
|
+
function isViteHubBearerSecretEqual(authorization, expected) {
|
|
26
|
+
return isViteHubSecretEqual(/^Bearer\s+(.+)$/i.exec(authorization ?? "")?.[1], expected);
|
|
27
|
+
}
|
|
28
|
+
Object.freeze({ prefix: "sha256=" });
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/runtime/static.ts
|
|
31
|
+
const intrinsicErrorIsError = Error.isError;
|
|
32
|
+
const intrinsicNativeErrorIsError = (() => {
|
|
33
|
+
try {
|
|
34
|
+
const process = globalThis.process;
|
|
35
|
+
const getBuiltinModule = process?.getBuiltinModule;
|
|
36
|
+
if (typeof getBuiltinModule !== "function") return void 0;
|
|
37
|
+
const util = getBuiltinModule.call(process, "util");
|
|
38
|
+
return typeof util?.types?.isNativeError === "function" ? util.types.isNativeError : void 0;
|
|
39
|
+
} catch {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
})();
|
|
43
|
+
function missingScheduleDefinitionError(name) {
|
|
44
|
+
return scheduleErrorDiagnostics.SCHEDULE_R0034({ message: `Missing schedule definition: ${name}` });
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Checks the `Authorization` header of a Vercel Cron request against `CRON_SECRET` in constant time.
|
|
48
|
+
*/
|
|
49
|
+
function isScheduleCronAuthorized(authorization, cronSecret) {
|
|
50
|
+
return isViteHubBearerSecretEqual(authorization, cronSecret);
|
|
51
|
+
}
|
|
52
|
+
function normalizeCron(cron) {
|
|
53
|
+
return cron.trim().split(/\s+/).join(" ");
|
|
54
|
+
}
|
|
55
|
+
function normalizeScheduleRuntimeError(error) {
|
|
56
|
+
if (isScheduleRuntimeError(error)) return error;
|
|
57
|
+
let message;
|
|
58
|
+
try {
|
|
59
|
+
message = String(error);
|
|
60
|
+
} catch {
|
|
61
|
+
message = "[unprintable thrown value]";
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
return scheduleErrorDiagnostics.SCHEDULE_R0035({
|
|
65
|
+
cause: error,
|
|
66
|
+
message
|
|
67
|
+
});
|
|
68
|
+
} catch {
|
|
69
|
+
const fallback = new Error(message);
|
|
70
|
+
Object.defineProperty(fallback, "code", {
|
|
71
|
+
value: "SCHEDULE_R0035",
|
|
72
|
+
enumerable: true
|
|
73
|
+
});
|
|
74
|
+
Object.defineProperty(fallback, "cause", { value: error });
|
|
75
|
+
return fallback;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function isScheduleRuntimeError(error) {
|
|
79
|
+
try {
|
|
80
|
+
if (error instanceof Error) return true;
|
|
81
|
+
} catch {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
if (error === null || typeof error !== "object") return false;
|
|
85
|
+
if (typeof intrinsicNativeErrorIsError === "function") try {
|
|
86
|
+
return intrinsicNativeErrorIsError(error);
|
|
87
|
+
} catch {}
|
|
88
|
+
try {
|
|
89
|
+
return typeof intrinsicErrorIsError === "function" && intrinsicErrorIsError(error) === true;
|
|
90
|
+
} catch {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Estimates a wake minute from callback time when the host supplies no scheduled timestamp. This is the newest UTC
|
|
96
|
+
* cron match within one day, or the current minute when none matches. It cannot identify a delayed or retried wake
|
|
97
|
+
* after another cron match; callers must not treat it as the provider's occurrence identity.
|
|
98
|
+
*/
|
|
99
|
+
function scheduledWakeTime(cron, now = /* @__PURE__ */ new Date()) {
|
|
100
|
+
return latestCronMinute(normalizeCron(cron), now);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Returns `true` when a host wake at `scheduledAt` is an occurrence of the Definition's cron in its `timeZone`. The host
|
|
104
|
+
* runs a wider UTC cron for a time-zoned Schedule, so the runtime skips the wakes that are not local occurrences.
|
|
105
|
+
*/
|
|
106
|
+
function isTimeZoneWakeDue(definition, name, scheduledAt) {
|
|
107
|
+
const minute = /* @__PURE__ */ new Date(Math.floor(scheduledAt.getTime() / 6e4) * 6e4);
|
|
108
|
+
return isScheduleDue({
|
|
109
|
+
cron: definition.cron,
|
|
110
|
+
id: name,
|
|
111
|
+
...definition.timeZone ? { timeZone: definition.timeZone } : {}
|
|
112
|
+
}, minute);
|
|
113
|
+
}
|
|
114
|
+
function createStaticScheduleRun(options) {
|
|
115
|
+
const scheduledAt = validateScheduledAt(options.scheduledAt);
|
|
116
|
+
return {
|
|
117
|
+
cron: options.cron,
|
|
118
|
+
id: `run_${options.name}_${scheduledAt.toISOString()}`,
|
|
119
|
+
idempotencyKey: scheduleIdempotencyKey(options.name, scheduledAt),
|
|
120
|
+
scheduleId: options.name,
|
|
121
|
+
scheduledAt,
|
|
122
|
+
waitUntil: options.waitUntil
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
async function executeStaticSchedule(options) {
|
|
126
|
+
return await runInRunScopes(() => runWithScheduleWaitUntil((waitUntil) => options.definition.handler(createStaticScheduleRun({
|
|
127
|
+
...options,
|
|
128
|
+
waitUntil
|
|
129
|
+
})), options.waitUntil));
|
|
130
|
+
}
|
|
131
|
+
async function executeMatchingStaticSchedules(options) {
|
|
132
|
+
if (typeof options.cron !== "string" || !options.cron.trim()) throw createScheduleError("SCHEDULE_INVALID_CRON", { details: invalidScheduleValueDetails("cron", options.cron) });
|
|
133
|
+
const cron = normalizeCron(options.cron);
|
|
134
|
+
const scheduledAt = validateScheduledAt(options.scheduledAt);
|
|
135
|
+
const runs = [];
|
|
136
|
+
for (const [name, load] of Object.entries(options.registry)) {
|
|
137
|
+
const definition = unwrapScheduleDefinition(await load());
|
|
138
|
+
if (!definition) continue;
|
|
139
|
+
const override = options.cronOverrides && Object.hasOwn(options.cronOverrides, name) ? options.cronOverrides[name] : void 0;
|
|
140
|
+
const timeZoneCron = override === void 0 && options.timeZoneCrons && Object.hasOwn(options.timeZoneCrons, name) ? options.timeZoneCrons[name] : void 0;
|
|
141
|
+
if (normalizeCron(override ?? timeZoneCron ?? definition.cron) !== cron) continue;
|
|
142
|
+
if (timeZoneCron !== void 0 && !isTimeZoneWakeDue(definition, name, scheduledAt)) continue;
|
|
143
|
+
runs.push({
|
|
144
|
+
name,
|
|
145
|
+
run: executeStaticSchedule({
|
|
146
|
+
cron: options.cron,
|
|
147
|
+
definition,
|
|
148
|
+
name,
|
|
149
|
+
scheduledAt,
|
|
150
|
+
waitUntil: options.waitUntil
|
|
151
|
+
})
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
const results = await Promise.allSettled(runs.map(({ run }) => run));
|
|
155
|
+
const failures = [];
|
|
156
|
+
results.forEach((result, index) => {
|
|
157
|
+
if (result.status === "fulfilled") return;
|
|
158
|
+
failures.push(result.reason);
|
|
159
|
+
console.error(`[vitehub:schedule] Schedule "${runs[index].name}" failed (cron "${options.cron}").`, normalizeRuntimeDiagnosticError(result.reason, { includeStack: true }));
|
|
160
|
+
});
|
|
161
|
+
if (failures.length === 1) throw failures[0];
|
|
162
|
+
if (failures.length) throw new AggregateError(failures, `[vitehub:schedule] ${failures.length} schedules failed for cron "${options.cron}".`);
|
|
163
|
+
return results.map((result) => result.status === "fulfilled" ? result.value : void 0);
|
|
164
|
+
}
|
|
165
|
+
async function executeCloudflareStaticSchedules(event, options) {
|
|
166
|
+
const scheduled = readCloudflareScheduledEvent(event);
|
|
167
|
+
return await runWithCloudflareServerEnv(event, async (waitUntil) => {
|
|
168
|
+
return await executeMatchingStaticSchedules({
|
|
169
|
+
cron: scheduled.cron,
|
|
170
|
+
cronOverrides: options.cronOverrides,
|
|
171
|
+
registry: options.registry,
|
|
172
|
+
scheduledAt: scheduled.scheduledAt,
|
|
173
|
+
timeZoneCrons: options.timeZoneCrons,
|
|
174
|
+
waitUntil
|
|
175
|
+
});
|
|
176
|
+
}, options.waitUntil ?? readCloudflareWaitUntil(event));
|
|
177
|
+
}
|
|
178
|
+
function readCloudflareScheduledEvent(event) {
|
|
179
|
+
const cron = event.controller?.cron ?? event.cron;
|
|
180
|
+
if (typeof cron !== "string" || !cron.trim()) throw scheduleErrorDiagnostics.SCHEDULE_R0027({ message: "[vitehub:schedule] Cloudflare scheduled event is missing a cron string." });
|
|
181
|
+
const scheduledTime = event.controller?.scheduledTime ?? event.scheduledTime;
|
|
182
|
+
const scheduledAt = isScheduleDate(scheduledTime) ? new Date(Date.prototype.getTime.call(scheduledTime)) : scheduledTime === void 0 ? /* @__PURE__ */ new Date() : new Date(scheduledTime);
|
|
183
|
+
if (Number.isNaN(scheduledAt.getTime())) throw scheduleErrorDiagnostics.SCHEDULE_R0028({ message: "[vitehub:schedule] Cloudflare scheduled event has an invalid scheduledTime." });
|
|
184
|
+
return {
|
|
185
|
+
cron,
|
|
186
|
+
scheduledAt
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
async function runWithCloudflareServerEnv(event, callback, hostWaitUntil) {
|
|
190
|
+
const globals = globalThis;
|
|
191
|
+
const hadGlobalEnv = Object.prototype.hasOwnProperty.call(globals, "__env__");
|
|
192
|
+
const previousGlobalEnv = globals.__env__;
|
|
193
|
+
const env = readCloudflareEventEnv(event);
|
|
194
|
+
if (env) globals.__env__ = env;
|
|
195
|
+
const pending = /* @__PURE__ */ new Set();
|
|
196
|
+
const waitUntil = hostWaitUntil ? (value) => {
|
|
197
|
+
const promise = Promise.resolve(value);
|
|
198
|
+
pending.add(promise);
|
|
199
|
+
promise.then(() => pending.delete(promise), () => pending.delete(promise));
|
|
200
|
+
hostWaitUntil(promise);
|
|
201
|
+
} : void 0;
|
|
202
|
+
try {
|
|
203
|
+
return await callback(waitUntil);
|
|
204
|
+
} finally {
|
|
205
|
+
if (pending.size === 0) restoreGlobalEnv(globals, hadGlobalEnv, previousGlobalEnv);
|
|
206
|
+
else hostWaitUntil(flushPendingWork(pending).finally(() => {
|
|
207
|
+
restoreGlobalEnv(globals, hadGlobalEnv, previousGlobalEnv);
|
|
208
|
+
}));
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
async function flushPendingWork(pending) {
|
|
212
|
+
while (pending.size > 0) await Promise.allSettled(pending);
|
|
213
|
+
}
|
|
214
|
+
function restoreGlobalEnv(globals, hadGlobalEnv, previousGlobalEnv) {
|
|
215
|
+
if (hadGlobalEnv) globals.__env__ = previousGlobalEnv;
|
|
216
|
+
else delete globals.__env__;
|
|
217
|
+
}
|
|
218
|
+
function isCallable(value) {
|
|
219
|
+
return typeof value === "function" || typeof value === "undefined" && value !== void 0;
|
|
220
|
+
}
|
|
221
|
+
function readCloudflareWaitUntil(event) {
|
|
222
|
+
const context = isRecordOrFunction(event.context) ? event.context : void 0;
|
|
223
|
+
const cloudflareContext = isRecordOrFunction(context?.cloudflare) ? context.cloudflare : void 0;
|
|
224
|
+
const platformContext = isRecordOrFunction(context?._platform) ? context._platform : void 0;
|
|
225
|
+
const platformCloudflareContext = isRecordOrFunction(platformContext?.cloudflare) ? platformContext.cloudflare : void 0;
|
|
226
|
+
const request = isRecordOrFunction(event.req) ? event.req : void 0;
|
|
227
|
+
const runtime = isRecordOrFunction(request?.runtime) ? request.runtime : void 0;
|
|
228
|
+
const runtimeCloudflare = isRecordOrFunction(runtime?.cloudflare) ? runtime.cloudflare : void 0;
|
|
229
|
+
const owners = [
|
|
230
|
+
event,
|
|
231
|
+
context,
|
|
232
|
+
cloudflareContext,
|
|
233
|
+
isRecordOrFunction(cloudflareContext?.context) ? cloudflareContext.context : void 0,
|
|
234
|
+
platformCloudflareContext,
|
|
235
|
+
isRecordOrFunction(platformCloudflareContext?.context) ? platformCloudflareContext.context : void 0,
|
|
236
|
+
request,
|
|
237
|
+
runtimeCloudflare,
|
|
238
|
+
isRecordOrFunction(runtimeCloudflare?.context) ? runtimeCloudflare.context : void 0
|
|
239
|
+
];
|
|
240
|
+
for (const owner of owners) {
|
|
241
|
+
const waitUntil = owner?.waitUntil;
|
|
242
|
+
if (isCallable(waitUntil)) return (promise) => Reflect.apply(waitUntil, owner, [promise]);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
function readCloudflareEventEnv(event) {
|
|
246
|
+
if (isRecordOrFunction(event.env)) return event.env;
|
|
247
|
+
const context = isRecordOrFunction(event.context) ? event.context : void 0;
|
|
248
|
+
const cloudflareContext = isRecordOrFunction(context?.cloudflare) ? context.cloudflare : void 0;
|
|
249
|
+
if (isRecordOrFunction(cloudflareContext?.env)) return cloudflareContext.env;
|
|
250
|
+
const platformContext = isRecordOrFunction(context?._platform) ? context._platform : void 0;
|
|
251
|
+
const platformCloudflareContext = isRecordOrFunction(platformContext?.cloudflare) ? platformContext.cloudflare : void 0;
|
|
252
|
+
if (isRecordOrFunction(platformCloudflareContext?.env)) return platformCloudflareContext.env;
|
|
253
|
+
const request = isRecordOrFunction(event.req) ? event.req : void 0;
|
|
254
|
+
const runtime = isRecordOrFunction(request?.runtime) ? request.runtime : void 0;
|
|
255
|
+
const runtimeCloudflare = isRecordOrFunction(runtime?.cloudflare) ? runtime.cloudflare : void 0;
|
|
256
|
+
return isRecordOrFunction(runtimeCloudflare?.env) ? runtimeCloudflare.env : void 0;
|
|
257
|
+
}
|
|
258
|
+
function isStaticScheduleDefinition(value) {
|
|
259
|
+
return isRecordOrFunction(value) && Object.hasOwn(value, "cron") && typeof value.cron === "string" && Object.hasOwn(value, "handler") && typeof value.handler === "function";
|
|
260
|
+
}
|
|
261
|
+
function unwrapScheduleDefinition(loaded) {
|
|
262
|
+
const candidate = isRecordOrFunction(loaded) && Object.hasOwn(loaded, "default") ? loaded.default : loaded;
|
|
263
|
+
return isStaticScheduleDefinition(candidate) ? candidate : void 0;
|
|
264
|
+
}
|
|
265
|
+
//#endregion
|
|
266
|
+
export { isScheduleCronAuthorized as a, normalizeCron as c, unwrapScheduleDefinition as d, executeStaticSchedule as i, normalizeScheduleRuntimeError as l, executeCloudflareStaticSchedules as n, isTimeZoneWakeDue as o, executeMatchingStaticSchedules as r, missingScheduleDefinitionError as s, createStaticScheduleRun as t, scheduledWakeTime as u };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { a as ScheduleDefinitionRegistry, g as ScheduleRunStore, n as ScheduleDefinition, p as ScheduleRunRecord } from "./types-Cw5LjPuc.js";
|
|
2
|
+
import { t as ScheduleKVStorage } from "./kv-storage-C0KZZB6v.js";
|
|
3
|
+
//#region src/runtime/execute.d.ts
|
|
4
|
+
interface ExecuteScheduleOptions {
|
|
5
|
+
/** Return the persisted failed run when the handler throws. Guards still throw. */
|
|
6
|
+
captureHandlerFailure?: boolean;
|
|
7
|
+
/**
|
|
8
|
+
* The caller holds the claim on this occurrence, so a run that is still pending or running belongs to a process that
|
|
9
|
+
* stopped. Such a run gets a new attempt, and its unfinished attempts are marked as failed.
|
|
10
|
+
*/
|
|
11
|
+
claimed?: boolean;
|
|
12
|
+
definition: ScheduleDefinition;
|
|
13
|
+
/** Signals that the handler can start, or an existing run already consumed this occurrence. */
|
|
14
|
+
onExecution?: () => void;
|
|
15
|
+
runStore?: ScheduleRunStore;
|
|
16
|
+
runIdSuffix?: string;
|
|
17
|
+
scheduleId: string;
|
|
18
|
+
source?: "direct" | "manual" | "started" | "static";
|
|
19
|
+
scheduledAt?: Date;
|
|
20
|
+
target: string;
|
|
21
|
+
waitUntil?: (promise: PromiseLike<unknown>) => void;
|
|
22
|
+
}
|
|
23
|
+
interface ExecuteStaticScheduleOptions {
|
|
24
|
+
claimed?: boolean;
|
|
25
|
+
cron: string;
|
|
26
|
+
definition: ScheduleDefinition;
|
|
27
|
+
name: string;
|
|
28
|
+
onExecution?: () => void;
|
|
29
|
+
scheduledAt?: Date;
|
|
30
|
+
waitUntil?: (promise: PromiseLike<unknown>) => void;
|
|
31
|
+
}
|
|
32
|
+
interface RunScheduleOptions {
|
|
33
|
+
/**
|
|
34
|
+
* The time the handler receives as `scheduledAt`, as if the cron fired then. A string is an ISO 8601 date-time with a
|
|
35
|
+
* time zone, such as `"2026-01-31T23:59:00Z"`. Defaults to now. The run records this time.
|
|
36
|
+
*/
|
|
37
|
+
scheduledAt?: Date | string;
|
|
38
|
+
}
|
|
39
|
+
declare function createScheduleRun(options: Omit<ExecuteScheduleOptions, "definition" | "waitUntil">): Promise<ScheduleRunRecord>;
|
|
40
|
+
declare function executeSchedule(options: ExecuteScheduleOptions): Promise<ScheduleRunRecord>;
|
|
41
|
+
declare function executeStaticSchedule(options: ExecuteStaticScheduleOptions): Promise<ScheduleRunRecord>;
|
|
42
|
+
/** Runs a manually dispatchable static Schedule Definition of `registry` immediately. */
|
|
43
|
+
declare function runScheduleFrom(registry: ScheduleDefinitionRegistry, name: string, options?: RunScheduleOptions): Promise<ScheduleRunRecord>;
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/runtime/store.d.ts
|
|
46
|
+
interface KVScheduleStoreOptions {
|
|
47
|
+
kvStore: ScheduleKVStorage;
|
|
48
|
+
prefix?: string;
|
|
49
|
+
}
|
|
50
|
+
declare function createMemoryScheduleRunStore(): ScheduleRunStore;
|
|
51
|
+
declare function createKVScheduleRunStore(options: KVScheduleStoreOptions): ScheduleRunStore;
|
|
52
|
+
//#endregion
|
|
53
|
+
export { createScheduleRun as a, runScheduleFrom as c, RunScheduleOptions as i, createKVScheduleRunStore as n, executeSchedule as o, createMemoryScheduleRunStore as r, executeStaticSchedule as s, KVScheduleStoreOptions as t };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
//#region ../internal/src/runtime/time-zone.ts
|
|
2
|
+
const slashlessIanaTimeZones = /* @__PURE__ */ new Set([
|
|
3
|
+
"CET",
|
|
4
|
+
"CST6CDT",
|
|
5
|
+
"Cuba",
|
|
6
|
+
"EET",
|
|
7
|
+
"EST",
|
|
8
|
+
"EST5EDT",
|
|
9
|
+
"Egypt",
|
|
10
|
+
"Eire",
|
|
11
|
+
"Factory",
|
|
12
|
+
"GB",
|
|
13
|
+
"GB-Eire",
|
|
14
|
+
"GMT",
|
|
15
|
+
"GMT+0",
|
|
16
|
+
"GMT-0",
|
|
17
|
+
"GMT0",
|
|
18
|
+
"Greenwich",
|
|
19
|
+
"HST",
|
|
20
|
+
"Hongkong",
|
|
21
|
+
"Iceland",
|
|
22
|
+
"Iran",
|
|
23
|
+
"Israel",
|
|
24
|
+
"Jamaica",
|
|
25
|
+
"Japan",
|
|
26
|
+
"Kwajalein",
|
|
27
|
+
"Libya",
|
|
28
|
+
"MET",
|
|
29
|
+
"MST",
|
|
30
|
+
"MST7MDT",
|
|
31
|
+
"NZ",
|
|
32
|
+
"NZ-CHAT",
|
|
33
|
+
"Navajo",
|
|
34
|
+
"PRC",
|
|
35
|
+
"PST8PDT",
|
|
36
|
+
"Poland",
|
|
37
|
+
"Portugal",
|
|
38
|
+
"ROC",
|
|
39
|
+
"ROK",
|
|
40
|
+
"Singapore",
|
|
41
|
+
"Turkey",
|
|
42
|
+
"UCT",
|
|
43
|
+
"UTC",
|
|
44
|
+
"Universal",
|
|
45
|
+
"W-SU",
|
|
46
|
+
"WET",
|
|
47
|
+
"Zulu"
|
|
48
|
+
]);
|
|
49
|
+
function isIanaTimeZone(timeZone) {
|
|
50
|
+
if (typeof timeZone !== "string" || !timeZone.includes("/") && !slashlessIanaTimeZones.has(timeZone)) return false;
|
|
51
|
+
try {
|
|
52
|
+
new Intl.DateTimeFormat("en-US", { timeZone });
|
|
53
|
+
return true;
|
|
54
|
+
} catch {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
export { isIanaTimeZone as t };
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { SerializedResponse } from "@vite-hub/runtime";
|
|
2
|
+
//#region src/types.d.ts
|
|
3
|
+
interface ScheduleRunContext {
|
|
4
|
+
id: string;
|
|
5
|
+
attemptId?: string;
|
|
6
|
+
/** The `payload` that `startSchedule()` stored. Absent for a run on the Definition's own cron. */
|
|
7
|
+
payload?: unknown;
|
|
8
|
+
/**
|
|
9
|
+
* Key derived from `scheduledAt`: `schedule:<name>:<scheduledAt ISO>`. Cloudflare and the Process Runtime retain
|
|
10
|
+
* occurrence identity. Vercel, Netlify, and Deno estimate it from callback time, so a late delivery or retry across
|
|
11
|
+
* a host cron boundary can have a different key. Pass it to Queue, Email, or a Channel to dedupe the same key.
|
|
12
|
+
*/
|
|
13
|
+
idempotencyKey: string;
|
|
14
|
+
runId?: string;
|
|
15
|
+
scheduleId?: string;
|
|
16
|
+
scheduledAt: Date;
|
|
17
|
+
/** Name of the Schedule Definition that runs. */
|
|
18
|
+
target?: string;
|
|
19
|
+
waitUntil(promise: PromiseLike<unknown>): void;
|
|
20
|
+
}
|
|
21
|
+
type ScheduleHandler<TResult = unknown> = {
|
|
22
|
+
bivarianceHack(context: ScheduleRunContext): TResult | Promise<TResult>;
|
|
23
|
+
}["bivarianceHack"];
|
|
24
|
+
interface ScheduleDefinitionInput<TResult = unknown> {
|
|
25
|
+
/** Allows `runSchedule()`, the Console, and `vitehub schedule run` to start a Definition with `cron` outside its cron. */
|
|
26
|
+
manual?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Five-field cron expression, evaluated in `timeZone`. Leave it out for a Schedule that runs only when code starts it
|
|
29
|
+
* with `startSchedule()`.
|
|
30
|
+
*/
|
|
31
|
+
cron?: string;
|
|
32
|
+
handler: ScheduleHandler<TResult>;
|
|
33
|
+
/**
|
|
34
|
+
* IANA time zone of `cron`, such as `"Europe/Madrid"`. Defaults to `"UTC"`. Every host follows daylight saving time:
|
|
35
|
+
* a skipped local time runs once, shifted by the gap, and a repeated local time runs once. For host cron output
|
|
36
|
+
* (Cloudflare, Vercel, Netlify, Deno), write it as a string literal so the build can read it.
|
|
37
|
+
*/
|
|
38
|
+
timeZone?: string;
|
|
39
|
+
}
|
|
40
|
+
interface ScheduleDefinitionOptions {
|
|
41
|
+
/** Allows `runSchedule()`, the Console, and `vitehub schedule run` to start a Definition with `cron` outside its cron. */
|
|
42
|
+
manual?: boolean;
|
|
43
|
+
/** IANA time zone of the cron, such as `"Europe/Madrid"`. Defaults to `"UTC"`. */
|
|
44
|
+
timeZone?: string;
|
|
45
|
+
}
|
|
46
|
+
interface ScheduleDefinition<TResult = unknown> {
|
|
47
|
+
/** Absent for a Schedule that runs only when code starts it with `startSchedule()`. */
|
|
48
|
+
cron?: string;
|
|
49
|
+
handler: ScheduleHandler<TResult>;
|
|
50
|
+
/** IANA time zone of `cron`. Absent means UTC. */
|
|
51
|
+
timeZone?: string;
|
|
52
|
+
options?: Omit<ScheduleDefinitionOptions, "timeZone">;
|
|
53
|
+
}
|
|
54
|
+
type ScheduleRegistryDefinition = ScheduleDefinition;
|
|
55
|
+
interface ScheduleDefinitionRegistry {
|
|
56
|
+
[name: string]: () => Promise<{
|
|
57
|
+
default?: ScheduleRegistryDefinition;
|
|
58
|
+
} | ScheduleRegistryDefinition>;
|
|
59
|
+
}
|
|
60
|
+
type ScheduleRunStatus = "pending" | "running" | "succeeded" | "failed";
|
|
61
|
+
type ScheduleRunAttemptStatus = "running" | "succeeded" | "failed";
|
|
62
|
+
interface ScheduleRunError {
|
|
63
|
+
message: string;
|
|
64
|
+
name?: string;
|
|
65
|
+
stack?: string;
|
|
66
|
+
}
|
|
67
|
+
/** Serializable representation of a schedule invocation response. */
|
|
68
|
+
type ScheduleRunResponse = SerializedResponse;
|
|
69
|
+
interface ScheduleRunRecord {
|
|
70
|
+
attemptCount: number;
|
|
71
|
+
completedAt?: Date;
|
|
72
|
+
createdAt: Date;
|
|
73
|
+
error?: ScheduleRunError;
|
|
74
|
+
id: string;
|
|
75
|
+
scheduleId: string;
|
|
76
|
+
response?: ScheduleRunResponse;
|
|
77
|
+
scheduledAt: Date;
|
|
78
|
+
startedAt?: Date;
|
|
79
|
+
status: ScheduleRunStatus;
|
|
80
|
+
/** Name of the Schedule Definition that ran. */
|
|
81
|
+
target: string;
|
|
82
|
+
updatedAt: Date;
|
|
83
|
+
}
|
|
84
|
+
interface ScheduleRunAttemptRecord {
|
|
85
|
+
completedAt?: Date;
|
|
86
|
+
createdAt: Date;
|
|
87
|
+
error?: ScheduleRunError;
|
|
88
|
+
id: string;
|
|
89
|
+
runId: string;
|
|
90
|
+
startedAt: Date;
|
|
91
|
+
status: ScheduleRunAttemptStatus;
|
|
92
|
+
updatedAt: Date;
|
|
93
|
+
}
|
|
94
|
+
interface ScheduleRunListOptions {
|
|
95
|
+
/** Only records whose ID starts with this prefix. Applied before `limit`. */
|
|
96
|
+
idPrefix?: string;
|
|
97
|
+
/** Maximum number of records, newest scheduled occurrence first. */
|
|
98
|
+
limit?: number;
|
|
99
|
+
scheduleId?: string;
|
|
100
|
+
}
|
|
101
|
+
interface ScheduleRunStore {
|
|
102
|
+
createAttempt: (attempt: ScheduleRunAttemptRecord) => Promise<ScheduleRunAttemptRecord> | ScheduleRunAttemptRecord;
|
|
103
|
+
createRun: (run: ScheduleRunRecord) => Promise<ScheduleRunRecord> | ScheduleRunRecord;
|
|
104
|
+
getAttempt: (id: string) => Promise<ScheduleRunAttemptRecord | undefined> | ScheduleRunAttemptRecord | undefined;
|
|
105
|
+
getRun: (id: string) => Promise<ScheduleRunRecord | undefined> | ScheduleRunRecord | undefined;
|
|
106
|
+
listAttempts: (runId: string) => Promise<ScheduleRunAttemptRecord[]> | ScheduleRunAttemptRecord[];
|
|
107
|
+
/** Reads several histories using one request-scoped key snapshot when supported. */
|
|
108
|
+
listRunsBatch?: (options: ScheduleRunListOptions[]) => Promise<ScheduleRunRecord[][]> | ScheduleRunRecord[][];
|
|
109
|
+
listRuns: (options?: ScheduleRunListOptions) => Promise<ScheduleRunRecord[]> | ScheduleRunRecord[];
|
|
110
|
+
updateAttempt: (id: string, patch: Partial<Pick<ScheduleRunAttemptRecord, "completedAt" | "error" | "status">> & {
|
|
111
|
+
updatedAt: Date;
|
|
112
|
+
}) => Promise<ScheduleRunAttemptRecord | undefined> | ScheduleRunAttemptRecord | undefined;
|
|
113
|
+
updateRun: (id: string, patch: Partial<Pick<ScheduleRunRecord, "attemptCount" | "completedAt" | "error" | "response" | "startedAt" | "status">> & {
|
|
114
|
+
updatedAt: Date;
|
|
115
|
+
}) => Promise<ScheduleRunRecord | undefined> | ScheduleRunRecord | undefined;
|
|
116
|
+
}
|
|
117
|
+
interface DiscoveredScheduleDefinition {
|
|
118
|
+
manual?: boolean;
|
|
119
|
+
/** True when the Definition declares no `cron`, so it runs only when code starts it with `startSchedule()`. */
|
|
120
|
+
startable?: boolean;
|
|
121
|
+
/** Plugin that contributed the Definition through `vitehub.schedules`. Only set for contributed Definitions. */
|
|
122
|
+
contributor?: string;
|
|
123
|
+
handler: string;
|
|
124
|
+
name: string;
|
|
125
|
+
/** Why the plugin contributed the Definition. Only set for contributed Definitions. */
|
|
126
|
+
reason?: string;
|
|
127
|
+
source?: "server-schedules" | "vite-plugin-contribution" | "vite-suffix";
|
|
128
|
+
}
|
|
129
|
+
/** When a started Schedule runs: once at `at`, or on each occurrence of `cron` until you cancel it. */
|
|
130
|
+
type StartScheduleTiming = {
|
|
131
|
+
/** One run at this time: a `Date`, or an ISO 8601 date-time with a time zone, such as `"2026-11-08T09:00:00Z"`. */
|
|
132
|
+
at: Date | string;
|
|
133
|
+
cron?: never;
|
|
134
|
+
timeZone?: never;
|
|
135
|
+
} | {
|
|
136
|
+
at?: never;
|
|
137
|
+
/** Five-field cron expression. The Schedule runs on each occurrence until you cancel it. */
|
|
138
|
+
cron: string;
|
|
139
|
+
/** IANA time zone of `cron`, such as `"Europe/Madrid"`. Defaults to `"UTC"`. */
|
|
140
|
+
timeZone?: string;
|
|
141
|
+
};
|
|
142
|
+
type StartScheduleOptions = StartScheduleTiming & {
|
|
143
|
+
/** JSON value that the handler receives as `context.payload`. */
|
|
144
|
+
payload?: unknown;
|
|
145
|
+
};
|
|
146
|
+
/** A Schedule that code started with `startSchedule()`. It stays in the store until its last run or `cancelSchedule()`. */
|
|
147
|
+
interface StartedSchedule {
|
|
148
|
+
/** One-time run. Absent for a cron Schedule. */
|
|
149
|
+
at?: Date;
|
|
150
|
+
createdAt: Date;
|
|
151
|
+
cron?: string;
|
|
152
|
+
/** Pass it to `getSchedule()` and `cancelSchedule()`. Its runs use it as `scheduleId`. */
|
|
153
|
+
id: string;
|
|
154
|
+
payload?: unknown;
|
|
155
|
+
/** Name of the Schedule Definition that runs. */
|
|
156
|
+
name: string;
|
|
157
|
+
timeZone?: string;
|
|
158
|
+
}
|
|
159
|
+
/** Stores the Schedules that code started. Records do not change after `put()`. */
|
|
160
|
+
interface StartedScheduleStore {
|
|
161
|
+
delete: (id: string) => Promise<boolean>;
|
|
162
|
+
get: (id: string) => Promise<StartedSchedule | undefined>;
|
|
163
|
+
list: () => Promise<StartedSchedule[]>;
|
|
164
|
+
put: (schedule: StartedSchedule) => Promise<void>;
|
|
165
|
+
}
|
|
166
|
+
//#endregion
|
|
167
|
+
export { StartScheduleOptions as _, ScheduleDefinitionRegistry as a, StartedScheduleStore as b, ScheduleRunAttemptRecord as c, ScheduleRunError as d, ScheduleRunListOptions as f, ScheduleRunStore as g, ScheduleRunStatus as h, ScheduleDefinitionOptions as i, ScheduleRunAttemptStatus as l, ScheduleRunResponse as m, ScheduleDefinition as n, ScheduleHandler as o, ScheduleRunRecord as p, ScheduleDefinitionInput as r, ScheduleRegistryDefinition as s, DiscoveredScheduleDefinition as t, ScheduleRunContext as u, StartScheduleTiming as v, StartedSchedule as y };
|