@vite-hub/schedule 0.0.1 → 0.0.3

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.
@@ -1,4 +1,4 @@
1
- import { m as ScheduleRunContext, s as ScheduleDefinition } from "../types-DOjXDq0s.js";
1
+ import { c as ScheduleDefinition, d as ScheduleDefinitionRegistry, g as ScheduleRunContext } from "../types-Bcje4dLV.js";
2
2
 
3
3
  //#region src/runtime/static.d.ts
4
4
  interface ExecuteStaticScheduleOptions {
@@ -6,12 +6,36 @@ interface ExecuteStaticScheduleOptions {
6
6
  definition: ScheduleDefinition;
7
7
  name: string;
8
8
  scheduledAt?: Date;
9
+ waitUntil?: (promise: PromiseLike<unknown>) => void;
10
+ }
11
+ interface ExecuteMatchingStaticSchedulesOptions {
12
+ cron: string;
13
+ registry: ScheduleDefinitionRegistry;
14
+ scheduledAt?: Date;
15
+ waitUntil?: (promise: PromiseLike<unknown>) => void;
16
+ }
17
+ interface ExecuteCloudflareStaticSchedulesOptions {
18
+ registry: ScheduleDefinitionRegistry;
19
+ waitUntil?: (promise: PromiseLike<unknown>) => void;
20
+ }
21
+ interface CloudflareScheduledEventLike {
22
+ [key: string]: unknown;
23
+ controller?: {
24
+ cron?: string;
25
+ scheduledTime?: number | string | Date;
26
+ };
27
+ cron?: string;
28
+ scheduledTime?: number | string | Date;
9
29
  }
10
30
  interface StaticScheduleRun extends ScheduleRunContext {
11
31
  cron: string;
12
32
  scheduleId: string;
13
33
  }
14
- declare function createStaticScheduleRun(options: Omit<ExecuteStaticScheduleOptions, "definition">): StaticScheduleRun;
34
+ declare function createStaticScheduleRun(options: Omit<ExecuteStaticScheduleOptions, "definition" | "waitUntil"> & {
35
+ waitUntil: NonNullable<ExecuteStaticScheduleOptions["waitUntil"]>;
36
+ }): StaticScheduleRun;
15
37
  declare function executeStaticSchedule(options: ExecuteStaticScheduleOptions): Promise<unknown>;
38
+ declare function executeMatchingStaticSchedules(options: ExecuteMatchingStaticSchedulesOptions): Promise<unknown[]>;
39
+ declare function executeCloudflareStaticSchedules(event: CloudflareScheduledEventLike, options: ExecuteCloudflareStaticSchedulesOptions): Promise<unknown[]>;
16
40
  //#endregion
17
- export { ExecuteStaticScheduleOptions, StaticScheduleRun, createStaticScheduleRun, executeStaticSchedule };
41
+ export { ExecuteCloudflareStaticSchedulesOptions, ExecuteMatchingStaticSchedulesOptions, ExecuteStaticScheduleOptions, StaticScheduleRun, createStaticScheduleRun, executeCloudflareStaticSchedules, executeMatchingStaticSchedules, executeStaticSchedule };
@@ -1,3 +1,4 @@
1
+ import { t as createLocalWaitUntil } from "../wait-until-CR-JjIOb.js";
1
2
  //#region src/runtime/static.ts
2
3
  function createStaticScheduleRun(options) {
3
4
  const scheduledAt = options.scheduledAt ?? /* @__PURE__ */ new Date();
@@ -5,11 +6,133 @@ function createStaticScheduleRun(options) {
5
6
  cron: options.cron,
6
7
  id: `run_${options.name}_${scheduledAt.toISOString()}`,
7
8
  scheduleId: options.name,
8
- scheduledAt
9
+ scheduledAt,
10
+ waitUntil: options.waitUntil
9
11
  };
10
12
  }
11
13
  async function executeStaticSchedule(options) {
12
- return await options.definition.handler(createStaticScheduleRun(options));
14
+ const localWaitUntil = createLocalWaitUntil();
15
+ try {
16
+ const result = await options.definition.handler(createStaticScheduleRun({
17
+ ...options,
18
+ waitUntil: options.waitUntil ?? localWaitUntil.waitUntil
19
+ }));
20
+ if (!options.waitUntil) await localWaitUntil.flush();
21
+ return result;
22
+ } catch (error) {
23
+ if (!options.waitUntil) try {
24
+ await localWaitUntil.flush();
25
+ } catch {}
26
+ throw error;
27
+ }
28
+ }
29
+ async function executeMatchingStaticSchedules(options) {
30
+ const scheduledAt = options.scheduledAt ?? /* @__PURE__ */ new Date();
31
+ const runs = [];
32
+ for (const [name, load] of Object.entries(options.registry)) {
33
+ const definition = unwrapScheduleDefinition(await load());
34
+ if (!definition || definition.cron !== options.cron) continue;
35
+ runs.push(executeStaticSchedule({
36
+ cron: options.cron,
37
+ definition,
38
+ name,
39
+ scheduledAt,
40
+ waitUntil: options.waitUntil
41
+ }));
42
+ }
43
+ return await Promise.all(runs);
44
+ }
45
+ async function executeCloudflareStaticSchedules(event, options) {
46
+ const scheduled = readCloudflareScheduledEvent(event);
47
+ return await runWithCloudflareServerEnv(event, async (waitUntil) => {
48
+ return await executeMatchingStaticSchedules({
49
+ cron: scheduled.cron,
50
+ registry: options.registry,
51
+ scheduledAt: scheduled.scheduledAt,
52
+ waitUntil
53
+ });
54
+ }, options.waitUntil ?? readCloudflareWaitUntil(event));
55
+ }
56
+ function readCloudflareScheduledEvent(event) {
57
+ const cron = event.controller?.cron ?? event.cron;
58
+ if (!cron) throw new TypeError("[vitehub:schedule] Cloudflare scheduled event is missing a cron string.");
59
+ const scheduledTime = event.controller?.scheduledTime ?? event.scheduledTime;
60
+ const scheduledAt = scheduledTime instanceof Date ? scheduledTime : typeof scheduledTime === "number" || typeof scheduledTime === "string" ? new Date(scheduledTime) : /* @__PURE__ */ new Date();
61
+ if (Number.isNaN(scheduledAt.getTime())) throw new TypeError("[vitehub:schedule] Cloudflare scheduled event has an invalid scheduledTime.");
62
+ return {
63
+ cron,
64
+ scheduledAt
65
+ };
66
+ }
67
+ async function runWithCloudflareServerEnv(event, callback, hostWaitUntil) {
68
+ const globals = globalThis;
69
+ const hadGlobalEnv = Object.prototype.hasOwnProperty.call(globals, "__env__");
70
+ const previousGlobalEnv = globals.__env__;
71
+ const env = readCloudflareEventEnv(event);
72
+ if (env) globals.__env__ = env;
73
+ const pending = /* @__PURE__ */ new Set();
74
+ const waitUntil = hostWaitUntil ? (value) => {
75
+ const promise = Promise.resolve(value);
76
+ pending.add(promise);
77
+ promise.then(() => pending.delete(promise), () => pending.delete(promise));
78
+ hostWaitUntil(promise);
79
+ } : void 0;
80
+ try {
81
+ return await callback(waitUntil);
82
+ } finally {
83
+ if (pending.size === 0) restoreGlobalEnv(globals, hadGlobalEnv, previousGlobalEnv);
84
+ else hostWaitUntil(flushPendingWork(pending).finally(() => {
85
+ restoreGlobalEnv(globals, hadGlobalEnv, previousGlobalEnv);
86
+ }));
87
+ }
88
+ }
89
+ async function flushPendingWork(pending) {
90
+ while (pending.size > 0) await Promise.allSettled([...pending]);
91
+ }
92
+ function restoreGlobalEnv(globals, hadGlobalEnv, previousGlobalEnv) {
93
+ if (hadGlobalEnv) globals.__env__ = previousGlobalEnv;
94
+ else delete globals.__env__;
95
+ }
96
+ function isRecord(value) {
97
+ return typeof value === "object" && value !== null && !Array.isArray(value);
98
+ }
99
+ function readCloudflareWaitUntil(event) {
100
+ const context = isRecord(event.context) ? event.context : void 0;
101
+ const cloudflareContext = isRecord(context?.cloudflare) ? context.cloudflare : void 0;
102
+ const platformContext = isRecord(context?._platform) ? context._platform : void 0;
103
+ const platformCloudflareContext = isRecord(platformContext?.cloudflare) ? platformContext.cloudflare : void 0;
104
+ const request = isRecord(event.req) ? event.req : void 0;
105
+ const runtime = isRecord(request?.runtime) ? request.runtime : void 0;
106
+ const runtimeCloudflare = isRecord(runtime?.cloudflare) ? runtime.cloudflare : void 0;
107
+ const owners = [
108
+ event,
109
+ context,
110
+ cloudflareContext,
111
+ isRecord(cloudflareContext?.context) ? cloudflareContext.context : void 0,
112
+ platformCloudflareContext,
113
+ isRecord(platformCloudflareContext?.context) ? platformCloudflareContext.context : void 0,
114
+ request,
115
+ runtimeCloudflare,
116
+ isRecord(runtimeCloudflare?.context) ? runtimeCloudflare.context : void 0
117
+ ];
118
+ for (const owner of owners) if (typeof owner?.waitUntil === "function") return owner.waitUntil.bind(owner);
119
+ }
120
+ function readCloudflareEventEnv(event) {
121
+ if (isRecord(event.env)) return event.env;
122
+ const context = isRecord(event.context) ? event.context : void 0;
123
+ const cloudflareContext = isRecord(context?.cloudflare) ? context.cloudflare : void 0;
124
+ if (isRecord(cloudflareContext?.env)) return cloudflareContext.env;
125
+ const platformContext = isRecord(context?._platform) ? context._platform : void 0;
126
+ const platformCloudflareContext = isRecord(platformContext?.cloudflare) ? platformContext.cloudflare : void 0;
127
+ if (isRecord(platformCloudflareContext?.env)) return platformCloudflareContext.env;
128
+ const request = isRecord(event.req) ? event.req : void 0;
129
+ const runtime = isRecord(request?.runtime) ? request.runtime : void 0;
130
+ const runtimeCloudflare = isRecord(runtime?.cloudflare) ? runtime.cloudflare : void 0;
131
+ return isRecord(runtimeCloudflare?.env) ? runtimeCloudflare.env : void 0;
132
+ }
133
+ function unwrapScheduleDefinition(loaded) {
134
+ const definition = typeof loaded === "object" && loaded !== null && "default" in loaded ? loaded.default : loaded;
135
+ return definition && "cron" in definition ? definition : void 0;
13
136
  }
14
137
  //#endregion
15
- export { createStaticScheduleRun, executeStaticSchedule };
138
+ export { createStaticScheduleRun, executeCloudflareStaticSchedules, executeMatchingStaticSchedules, executeStaticSchedule };
package/dist/runtime.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { a as RuntimeScheduleStore, d as ScheduleHandler, i as RuntimeScheduleRecord, l as ScheduleDefinitionOptions, m as ScheduleRunContext, n as RuntimeScheduleCreateInput, o as RuntimeScheduleUpdateInput, r as RuntimeScheduleMetadata, s as ScheduleDefinition, u as ScheduleDefinitionRegistry, y as ScheduleTargetName } from "./types-DOjXDq0s.js";
2
- import { a as createMemoryRuntimeScheduleStore, c as validateRuntimeScheduleCron, l as ScheduleError, s as schedules } from "./store-CeeSsInC.js";
1
+ /// <reference path="./registry-module.d.ts" />
2
+ import { C as ScheduleTargetName, S as ScheduleTargetDefinitionInput, a as RuntimeScheduleStore, c as ScheduleDefinition, d as ScheduleDefinitionRegistry, f as ScheduleHandler, g as ScheduleRunContext, i as RuntimeScheduleRecord, n as RuntimeScheduleCreateInput, o as RuntimeScheduleUpdateInput, p as ScheduleRegistryDefinition, r as RuntimeScheduleMetadata, u as ScheduleDefinitionOptions, x as ScheduleTargetDefinition } from "./types-Bcje4dLV.js";
3
+ import { c as schedules, i as createMemoryRuntimeScheduleStore, l as validateRuntimeScheduleCron } from "./store-t-74rsM_.js";
3
4
  import { getRuntimeScheduleStore, getScheduleRuntimeRegistry, loadScheduleDefinition, resetScheduleRuntime, setRuntimeScheduleStore, setScheduleRuntimeRegistry } from "./runtime/state.js";
4
- export { type RuntimeScheduleCreateInput, type RuntimeScheduleMetadata, type RuntimeScheduleRecord, type RuntimeScheduleStore, type RuntimeScheduleUpdateInput, type ScheduleDefinition, type ScheduleDefinitionOptions, type ScheduleDefinitionRegistry, ScheduleError, type ScheduleHandler, type ScheduleRunContext, type ScheduleTargetName, createMemoryRuntimeScheduleStore, getRuntimeScheduleStore, getScheduleRuntimeRegistry, loadScheduleDefinition, resetScheduleRuntime, schedules, setRuntimeScheduleStore, setScheduleRuntimeRegistry, validateRuntimeScheduleCron };
5
+ export { type RuntimeScheduleCreateInput, type RuntimeScheduleMetadata, type RuntimeScheduleRecord, type RuntimeScheduleStore, type RuntimeScheduleUpdateInput, type ScheduleDefinition, type ScheduleDefinitionOptions, type ScheduleDefinitionRegistry, type ScheduleHandler, type ScheduleRegistryDefinition, type ScheduleRunContext, type ScheduleTargetDefinition, type ScheduleTargetDefinitionInput, type ScheduleTargetName, createMemoryRuntimeScheduleStore, getRuntimeScheduleStore, getScheduleRuntimeRegistry, loadScheduleDefinition, resetScheduleRuntime, schedules, setRuntimeScheduleStore, setScheduleRuntimeRegistry, validateRuntimeScheduleCron };
package/dist/runtime.js CHANGED
@@ -1,3 +1,3 @@
1
- import { a as resetScheduleRuntime, c as setScheduleRuntimeRegistry, d as createMemoryRuntimeScheduleStore, i as loadScheduleDefinition, o as setRuntimeScheduleStore, p as ScheduleError, r as getScheduleRuntimeRegistry, t as getRuntimeScheduleStore } from "./state-DoONzfij.js";
2
- import { n as validateRuntimeScheduleCron, t as schedules } from "./client-BJ33CRLg.js";
3
- export { ScheduleError, createMemoryRuntimeScheduleStore, getRuntimeScheduleStore, getScheduleRuntimeRegistry, loadScheduleDefinition, resetScheduleRuntime, schedules, setRuntimeScheduleStore, setScheduleRuntimeRegistry, validateRuntimeScheduleCron };
1
+ import { a as resetScheduleRuntime, c as setScheduleRuntimeRegistry, d as createMemoryRuntimeScheduleStore, i as loadScheduleDefinition, o as setRuntimeScheduleStore, r as getScheduleRuntimeRegistry, t as getRuntimeScheduleStore } from "./state-By7WU_ks.js";
2
+ import { n as validateRuntimeScheduleCron, t as schedules } from "./client-BMFCy0dX.js";
3
+ export { createMemoryRuntimeScheduleStore, getRuntimeScheduleStore, getScheduleRuntimeRegistry, loadScheduleDefinition, resetScheduleRuntime, schedules, setRuntimeScheduleStore, setScheduleRuntimeRegistry, validateRuntimeScheduleCron };
@@ -1,17 +1,40 @@
1
+ import { ViteHubError } from "@vite-hub/runtime";
1
2
  import { AsyncLocalStorage } from "node:async_hooks";
2
3
  //#region src/errors.ts
3
- var ScheduleError = class extends Error {
4
- code;
5
- details;
6
- httpStatus;
7
- constructor(message, options = {}) {
8
- super(message, { cause: options.cause });
9
- this.name = "ScheduleError";
10
- this.code = options.code;
11
- this.details = options.details;
12
- this.httpStatus = options.httpStatus;
13
- }
4
+ const scheduleErrorMessages = {
5
+ SCHEDULE_ALREADY_EXISTS: "Runtime Schedule already exists.",
6
+ SCHEDULE_DISABLED: "Runtime Schedule is disabled.",
7
+ SCHEDULE_INVALID_CRON: "Runtime Schedule cron is invalid.",
8
+ SCHEDULE_INVALID_ENABLED: "Runtime Schedule enabled value is invalid.",
9
+ SCHEDULE_INVALID_ID: "Runtime Schedule id is invalid.",
10
+ SCHEDULE_INVALID_INPUT: "Runtime Schedule input is invalid.",
11
+ SCHEDULE_INVALID_SCHEDULED_AT: "Runtime Schedule scheduledAt value is invalid.",
12
+ SCHEDULE_INVALID_TARGET: "Runtime Schedule target is invalid.",
13
+ SCHEDULE_INVALID_TIME_ZONE: "Runtime Schedule time zone is invalid.",
14
+ SCHEDULE_NOT_DUE: "Schedule is not due.",
15
+ SCHEDULE_NOT_FOUND: "Runtime Schedule was not found.",
16
+ SCHEDULE_RUN_NOT_FOUND: "Schedule Run was not found.",
17
+ SCHEDULE_TARGET_NOT_ELIGIBLE: "Runtime Schedule target is not eligible.",
18
+ SCHEDULE_TARGET_NOT_FOUND: "Runtime Schedule target was not found."
14
19
  };
20
+ function invalidScheduleValueDetails(field, value) {
21
+ let valueType;
22
+ try {
23
+ valueType = value === null ? "null" : Array.isArray(value) ? "array" : value instanceof Date ? "date" : typeof value;
24
+ } catch {
25
+ valueType = "unknown";
26
+ }
27
+ return {
28
+ field,
29
+ valueType
30
+ };
31
+ }
32
+ function createScheduleError(code, options = {}) {
33
+ return new ViteHubError(code, scheduleErrorMessages[code], options);
34
+ }
35
+ function assertRuntimeScheduleId(id) {
36
+ if (typeof id !== "string" || !id.trim()) throw createScheduleError("SCHEDULE_INVALID_ID", { details: invalidScheduleValueDetails("id", id) });
37
+ }
15
38
  //#endregion
16
39
  //#region src/runtime/store.ts
17
40
  function trimPrefix(prefix) {
@@ -40,13 +63,23 @@ function scheduleRunBase(prefix) {
40
63
  function scheduleRunAttemptBase(prefix) {
41
64
  return joinKey(prefix, "schedule-run-attempts");
42
65
  }
66
+ function isMissingKVPackage(error) {
67
+ if (!(error instanceof Error)) return false;
68
+ return error.code === "ERR_MODULE_NOT_FOUND" && error.message.includes("Cannot find package '@vite-hub/kv'");
69
+ }
43
70
  async function resolveDefaultKVStore() {
44
- return (await import("@vite-hub/kv")).kv;
71
+ try {
72
+ return (await import("./kv-BJ1I9IlJ.js")).scheduleKVStorage;
73
+ } catch (error) {
74
+ if (isMissingKVPackage(error)) throw new Error("[vitehub:schedule] The default KV-backed stores require @vite-hub/kv. Install it with: pnpm add @vite-hub/kv", { cause: error });
75
+ throw error;
76
+ }
45
77
  }
46
78
  function cloneRuntimeSchedule(record) {
47
79
  return {
48
80
  ...record,
49
81
  createdAt: new Date(record.createdAt),
82
+ ...record.input !== void 0 ? { input: structuredClone(record.input) } : {},
50
83
  updatedAt: new Date(record.updatedAt)
51
84
  };
52
85
  }
@@ -54,6 +87,7 @@ function serializeRuntimeSchedule(record) {
54
87
  return {
55
88
  ...record,
56
89
  createdAt: record.createdAt.toISOString(),
90
+ ...record.input !== void 0 ? { input: structuredClone(record.input) } : {},
57
91
  updatedAt: record.updatedAt.toISOString()
58
92
  };
59
93
  }
@@ -61,6 +95,7 @@ function deserializeRuntimeSchedule(record) {
61
95
  return {
62
96
  ...record,
63
97
  createdAt: new Date(record.createdAt),
98
+ ...record.input !== void 0 ? { input: structuredClone(record.input) } : {},
64
99
  updatedAt: new Date(record.updatedAt)
65
100
  };
66
101
  }
@@ -68,7 +103,9 @@ function omitUndefinedPatch(patch) {
68
103
  return {
69
104
  ...patch.cron !== void 0 ? { cron: patch.cron } : {},
70
105
  ...patch.enabled !== void 0 ? { enabled: patch.enabled } : {},
71
- ...patch.target !== void 0 ? { target: patch.target } : {}
106
+ ...Object.hasOwn(patch, "input") ? { input: structuredClone(patch.input) } : {},
107
+ ...patch.target !== void 0 ? { target: patch.target } : {},
108
+ ...patch.timeZone !== void 0 ? { timeZone: patch.timeZone } : {}
72
109
  };
73
110
  }
74
111
  const keyLocks = /* @__PURE__ */ new Map();
@@ -92,11 +129,8 @@ function createMemoryRuntimeScheduleStore() {
92
129
  const records = /* @__PURE__ */ new Map();
93
130
  return {
94
131
  create(record) {
95
- if (records.has(record.id)) throw new ScheduleError(`Runtime Schedule already exists: ${record.id}`, {
96
- code: "SCHEDULE_ALREADY_EXISTS",
97
- details: { id: record.id },
98
- httpStatus: 409
99
- });
132
+ assertRuntimeScheduleId(record.id);
133
+ if (records.has(record.id)) throw createScheduleError("SCHEDULE_ALREADY_EXISTS");
100
134
  records.set(record.id, cloneRuntimeSchedule(record));
101
135
  return cloneRuntimeSchedule(record);
102
136
  },
@@ -130,14 +164,11 @@ function createKVRuntimeScheduleStore(options = {}) {
130
164
  }
131
165
  return {
132
166
  async create(record) {
167
+ assertRuntimeScheduleId(record.id);
133
168
  const store = await getKVStore();
134
169
  const key = runtimeScheduleKey(prefix, record.id);
135
170
  return await withKVKeyLock(key, async () => {
136
- if (await store.has(key)) throw new ScheduleError(`Runtime Schedule already exists: ${record.id}`, {
137
- code: "SCHEDULE_ALREADY_EXISTS",
138
- details: { id: record.id },
139
- httpStatus: 409
140
- });
171
+ if (await store.has(key)) throw createScheduleError("SCHEDULE_ALREADY_EXISTS");
141
172
  await store.set(key, serializeRuntimeSchedule(record));
142
173
  return cloneRuntimeSchedule(record);
143
174
  });
@@ -438,4 +469,4 @@ function resetScheduleRuntime() {
438
469
  loadingRegistryEntries.clear();
439
470
  }
440
471
  //#endregion
441
- export { resetScheduleRuntime as a, setScheduleRuntimeRegistry as c, createMemoryRuntimeScheduleStore as d, createMemoryScheduleRunStore as f, loadScheduleDefinition as i, createKVRuntimeScheduleStore as l, getScheduleRunStore as n, setRuntimeScheduleStore as o, ScheduleError as p, getScheduleRuntimeRegistry as r, setScheduleRunStore as s, getRuntimeScheduleStore as t, createKVScheduleRunStore as u };
472
+ export { resetScheduleRuntime as a, setScheduleRuntimeRegistry as c, createMemoryRuntimeScheduleStore as d, createMemoryScheduleRunStore as f, invalidScheduleValueDetails as h, loadScheduleDefinition as i, createKVRuntimeScheduleStore as l, createScheduleError as m, getScheduleRunStore as n, setRuntimeScheduleStore as o, assertRuntimeScheduleId as p, getScheduleRuntimeRegistry as r, setScheduleRunStore as s, getRuntimeScheduleStore as t, createKVScheduleRunStore as u };
@@ -0,0 +1,48 @@
1
+ import { C as ScheduleTargetName, a as RuntimeScheduleStore, b as ScheduleRunStore, i as RuntimeScheduleRecord, m as ScheduleRunAttemptRecord, n as RuntimeScheduleCreateInput, o as RuntimeScheduleUpdateInput, v as ScheduleRunRecord } from "./types-Bcje4dLV.js";
2
+
3
+ //#region src/runtime/client.d.ts
4
+ declare function validateRuntimeScheduleCron(cron: unknown): void;
5
+ declare const schedules: {
6
+ create<TTarget extends ScheduleTargetName, TInput = unknown>(input: RuntimeScheduleCreateInput<TTarget, TInput>): Promise<RuntimeScheduleRecord<TInput>>;
7
+ delete(id: string): Promise<boolean>;
8
+ disable(id: string): Promise<RuntimeScheduleRecord>;
9
+ enable(id: string): Promise<RuntimeScheduleRecord>;
10
+ get(id: string): Promise<RuntimeScheduleRecord | undefined>;
11
+ getRun(id: string): Promise<ScheduleRunRecord | undefined>;
12
+ list(): Promise<RuntimeScheduleRecord[]>;
13
+ listAttempts(runId: string): Promise<ScheduleRunAttemptRecord[]>;
14
+ listRuns(): Promise<ScheduleRunRecord[]>;
15
+ run(id: string, options?: {
16
+ scheduledAt?: Date;
17
+ }): Promise<ScheduleRunRecord>;
18
+ update<TTarget extends ScheduleTargetName, TInput = unknown>(id: string, input: RuntimeScheduleUpdateInput<TTarget, TInput>): Promise<RuntimeScheduleRecord<TInput>>;
19
+ };
20
+ //#endregion
21
+ //#region src/runtime/kv-storage.d.ts
22
+ interface ScheduleKVStorage {
23
+ del(key: string): boolean | Promise<boolean> | Promise<void> | void;
24
+ get<T = unknown>(key: string): Promise<T | null | undefined> | T | null | undefined;
25
+ has(key: string): boolean | Promise<boolean>;
26
+ keys(base?: string): Promise<string[]> | string[];
27
+ set<T = unknown>(key: string, value: T): Promise<void> | void;
28
+ }
29
+ interface ViteHubKVStorage {
30
+ del(key: string): Promise<readonly [unknown, unknown]>;
31
+ get<T = unknown>(key: string): Promise<readonly [unknown, T | null | undefined]>;
32
+ has(key: string): Promise<readonly [unknown, boolean | undefined]>;
33
+ keys(base?: string): Promise<readonly [unknown, string[] | undefined]>;
34
+ set<T = unknown>(key: string, value: T): Promise<readonly [unknown, unknown]>;
35
+ }
36
+ declare function createScheduleKVStorage(kvStore: ViteHubKVStorage): ScheduleKVStorage;
37
+ //#endregion
38
+ //#region src/runtime/store.d.ts
39
+ interface KVScheduleStoreOptions {
40
+ kvStore?: ScheduleKVStorage;
41
+ prefix?: string;
42
+ }
43
+ declare function createMemoryRuntimeScheduleStore(): RuntimeScheduleStore;
44
+ declare function createKVRuntimeScheduleStore(options?: KVScheduleStoreOptions): RuntimeScheduleStore;
45
+ declare function createMemoryScheduleRunStore(): ScheduleRunStore;
46
+ declare function createKVScheduleRunStore(options?: KVScheduleStoreOptions): ScheduleRunStore;
47
+ //#endregion
48
+ export { createMemoryScheduleRunStore as a, schedules as c, createMemoryRuntimeScheduleStore as i, validateRuntimeScheduleCron as l, createKVRuntimeScheduleStore as n, ScheduleKVStorage as o, createKVScheduleRunStore as r, createScheduleKVStorage as s, KVScheduleStoreOptions as t };
@@ -1,13 +1,17 @@
1
1
  //#region src/types.d.ts
2
- interface ScheduleRunContext {
2
+ interface ScheduleRunContext<TInput = unknown> {
3
3
  id: string;
4
4
  attemptId?: string;
5
+ input?: TInput;
5
6
  runId?: string;
6
7
  scheduleId?: string;
7
8
  scheduledAt: Date;
8
9
  target?: ScheduleTargetName;
10
+ waitUntil(promise: PromiseLike<unknown>): void;
9
11
  }
10
- type ScheduleHandler<TResult = unknown> = (context: ScheduleRunContext) => TResult | Promise<TResult>;
12
+ type ScheduleHandler<TResult = unknown, TInput = unknown> = {
13
+ bivarianceHack(context: ScheduleRunContext<TInput>): TResult | Promise<TResult>;
14
+ }["bivarianceHack"];
11
15
  interface ScheduleDefinitionInput<TResult = unknown> {
12
16
  allowRuntimeSchedules?: boolean;
13
17
  cron: string;
@@ -21,32 +25,52 @@ interface ScheduleDefinition<TResult = unknown> {
21
25
  handler: ScheduleHandler<TResult>;
22
26
  options?: ScheduleDefinitionOptions;
23
27
  }
28
+ interface ScheduleTargetDefinitionInput<TInput = unknown, TResult = unknown> {
29
+ handler: ScheduleHandler<TResult, TInput>;
30
+ }
31
+ interface ScheduleTargetDefinition<TInput = unknown, TResult = unknown> {
32
+ handler: ScheduleHandler<TResult, TInput>;
33
+ options: {
34
+ allowRuntimeSchedules: true;
35
+ };
36
+ }
37
+ type ScheduleRegistryDefinition = ScheduleDefinition | ScheduleTargetDefinition;
24
38
  interface ScheduleDefinitionRegistry {
25
39
  [name: string]: () => Promise<{
26
- default?: ScheduleDefinition;
27
- } | ScheduleDefinition>;
40
+ default?: ScheduleRegistryDefinition;
41
+ } | ScheduleRegistryDefinition>;
28
42
  }
29
43
  type ScheduleTargetName = string & {};
30
44
  interface RuntimeScheduleMetadata {
31
45
  createdAt: Date;
32
46
  updatedAt: Date;
33
47
  }
34
- interface RuntimeScheduleRecord extends RuntimeScheduleMetadata {
48
+ interface RuntimeScheduleRecord<TInput = unknown> extends RuntimeScheduleMetadata {
35
49
  cron: string;
36
50
  enabled: boolean;
37
51
  id: string;
52
+ input?: TInput;
38
53
  target: ScheduleTargetName;
54
+ timeZone?: string;
55
+ }
56
+ interface RuntimeScheduleWake {
57
+ scheduleId: string;
58
+ scheduledAt: Date;
39
59
  }
40
- interface RuntimeScheduleCreateInput<TTarget extends ScheduleTargetName = ScheduleTargetName> {
60
+ interface RuntimeScheduleCreateInput<TTarget extends ScheduleTargetName = ScheduleTargetName, TInput = unknown> {
41
61
  cron: string;
42
62
  enabled?: boolean;
43
63
  id?: string;
64
+ input?: TInput;
44
65
  target: TTarget;
66
+ timeZone?: string;
45
67
  }
46
- interface RuntimeScheduleUpdateInput<TTarget extends ScheduleTargetName = ScheduleTargetName> {
68
+ interface RuntimeScheduleUpdateInput<TTarget extends ScheduleTargetName = ScheduleTargetName, TInput = unknown> {
47
69
  cron?: string;
48
70
  enabled?: boolean;
71
+ input?: TInput;
49
72
  target?: TTarget;
73
+ timeZone?: string;
50
74
  }
51
75
  interface RuntimeScheduleStore {
52
76
  create: (record: RuntimeScheduleRecord) => Promise<RuntimeScheduleRecord> | RuntimeScheduleRecord;
@@ -105,7 +129,8 @@ interface DiscoveredScheduleDefinition {
105
129
  allowRuntimeSchedules?: boolean;
106
130
  handler: string;
107
131
  name: string;
108
- source?: "nitro-server-schedules" | "vite-suffix";
132
+ runtimeOnly?: boolean;
133
+ source?: "server-schedules" | "vite-suffix";
109
134
  }
110
135
  //#endregion
111
- export { ScheduleRunStatus as _, RuntimeScheduleStore as a, ScheduleDefinitionInput as c, ScheduleHandler as d, ScheduleRunAttemptRecord as f, ScheduleRunRecord as g, ScheduleRunError as h, RuntimeScheduleRecord as i, ScheduleDefinitionOptions as l, ScheduleRunContext as m, RuntimeScheduleCreateInput as n, RuntimeScheduleUpdateInput as o, ScheduleRunAttemptStatus as p, RuntimeScheduleMetadata as r, ScheduleDefinition as s, DiscoveredScheduleDefinition as t, ScheduleDefinitionRegistry as u, ScheduleRunStore as v, ScheduleTargetName as y };
136
+ export { ScheduleTargetName as C, ScheduleTargetDefinitionInput as S, ScheduleRunError as _, RuntimeScheduleStore as a, ScheduleRunStore as b, ScheduleDefinition as c, ScheduleDefinitionRegistry as d, ScheduleHandler as f, ScheduleRunContext as g, ScheduleRunAttemptStatus as h, RuntimeScheduleRecord as i, ScheduleDefinitionInput as l, ScheduleRunAttemptRecord as m, RuntimeScheduleCreateInput as n, RuntimeScheduleUpdateInput as o, ScheduleRegistryDefinition as p, RuntimeScheduleMetadata as r, RuntimeScheduleWake as s, DiscoveredScheduleDefinition as t, ScheduleDefinitionOptions as u, ScheduleRunRecord as v, ScheduleTargetDefinition as x, ScheduleRunStatus as y };