@vite-hub/schedule 0.0.1 → 0.0.2

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/dist/vite.d.ts CHANGED
@@ -1,9 +1,36 @@
1
- import { Plugin } from "vite";
2
-
3
1
  //#region src/vite.d.ts
4
- type ScheduleVitePlugin = Plugin & {
5
- nitro: unknown;
2
+ interface ScheduleProcessRuntimeOptions {
3
+ concurrency?: number;
4
+ driver: "process";
5
+ intervalMs?: number;
6
+ prefix?: string;
7
+ }
8
+ interface ScheduleVitePluginOptions {
9
+ providerOutput?: "auto" | "standalone" | "nitro" | false;
10
+ projectRoot?: string;
11
+ runtime?: ScheduleProcessRuntimeOptions;
12
+ }
13
+ interface ScheduleNitroConfigOptions extends ScheduleVitePluginOptions {
14
+ command?: "build" | "serve";
15
+ nitro?: unknown;
16
+ root?: string;
17
+ }
18
+ interface ScheduleVitePlugin {
19
+ name: string;
20
+ [hook: string]: unknown;
21
+ }
22
+ type NitroConfig = Record<string, unknown> & {
23
+ cloudflare?: {
24
+ wrangler?: {
25
+ triggers?: {
26
+ crons?: string[];
27
+ } & Record<string, unknown>;
28
+ } & Record<string, unknown>;
29
+ } & Record<string, unknown>;
30
+ modules?: unknown[];
31
+ plugins?: unknown[];
6
32
  };
7
- declare function hubSchedule(): ScheduleVitePlugin;
33
+ declare function createScheduleNitroConfig(options?: ScheduleNitroConfigOptions): Promise<NitroConfig | null>;
34
+ declare function hubSchedule(options?: ScheduleVitePluginOptions): ScheduleVitePlugin;
8
35
  //#endregion
9
- export { ScheduleVitePlugin, hubSchedule };
36
+ export { ScheduleNitroConfigOptions, ScheduleProcessRuntimeOptions, ScheduleVitePlugin, ScheduleVitePluginOptions, createScheduleNitroConfig, hubSchedule };
package/dist/vite.js CHANGED
@@ -1,110 +1,2 @@
1
- import { i as createRuntimeRegistryContents, t as discoverScheduleDefinitions } from "./discovery-Di9MD-SN.js";
2
- import { a as schedulePackageName, i as generateProviderOutputs, n as SCHEDULE_TARGETS_ID, o as shouldSkipViteProviderBuild, r as createScheduleTargetsContents, t as scheduleNitroModule } from "./module-C_j2ahfg.js";
3
- import { normalize, resolve } from "node:path";
4
- const VITEHUB_MODES = {
5
- e2e: "e2e",
6
- blob: "blob",
7
- chat: "chat",
8
- db: "db",
9
- env: "env",
10
- kv: "kv",
11
- queue: "queue",
12
- schedule: "schedule",
13
- sandbox: "sandbox",
14
- workspace: "workspace",
15
- workflow: "workflow"
16
- };
17
- function isViteCli(argv) {
18
- return argv.some((arg) => /(?:^|[/\\])vite(?:\.[cm]?js)?$/.test(arg) || arg === "vite");
19
- }
20
- function getViteCliMode(argv = process.argv) {
21
- if (!isViteCli(argv)) return;
22
- const modes = new Set(Object.values(VITEHUB_MODES));
23
- for (let index = 0; index < argv.length; index += 1) {
24
- const arg = argv[index];
25
- const mode = arg === "--mode" || arg === "-m" ? argv[index + 1] : arg.startsWith("--mode=") ? arg.slice(7) : void 0;
26
- if (mode && modes.has(mode)) return mode;
27
- }
28
- }
29
- function getViteMode(env = process.env) {
30
- return env["VITEHUB_VITE_MODE"] ?? getViteCliMode();
31
- }
32
- //#endregion
33
- //#region ../internal/src/build/vite.ts
34
- function createNoExternalMerger(packageName) {
35
- return (current) => {
36
- if (current === true) return true;
37
- if (!current) return [packageName];
38
- const values = Array.isArray(current) ? current : [current];
39
- return values.includes(packageName) ? values : [...values, packageName];
40
- };
41
- }
42
- function isServerEnvironment(name, config) {
43
- return name === "ssr" || config.consumer === "server";
44
- }
45
- //#endregion
46
- //#region src/vite.ts
47
- const SCHEDULE_VITE_PLUGIN_NAME = "@vite-hub/schedule/vite";
48
- const SCHEDULE_REGISTRY_ID = "#vitehub/schedule/registry";
49
- const RESOLVED_SCHEDULE_REGISTRY_ID = "\0#vitehub/schedule/registry";
50
- const RESOLVED_SCHEDULE_TARGETS_ID = `\0${SCHEDULE_TARGETS_ID}`;
51
- const registryImportAnchor = ".vitehub/schedule/registry.js";
52
- const mergeNoExternal = createNoExternalMerger(schedulePackageName);
53
- function resolveStringAliases(config) {
54
- const aliases = {};
55
- for (const alias of config.resolve.alias) if (typeof alias.find === "string" && typeof alias.replacement === "string") aliases[alias.find] = alias.replacement;
56
- return aliases;
57
- }
58
- function hubSchedule() {
59
- let resolved;
60
- function discoverViteSchedules() {
61
- if (!resolved) return [];
62
- return discoverScheduleDefinitions({
63
- mode: "vite-suffix",
64
- rootDir: resolved.root
65
- });
66
- }
67
- function createRegistryContents() {
68
- const definitions = discoverViteSchedules();
69
- return createRuntimeRegistryContents(resolved ? resolve(resolved.root, registryImportAnchor) : registryImportAnchor, definitions);
70
- }
71
- function createTargetsContents() {
72
- return createScheduleTargetsContents(discoverViteSchedules(), { types: false });
73
- }
74
- return {
75
- name: SCHEDULE_VITE_PLUGIN_NAME,
76
- nitro: scheduleNitroModule,
77
- configResolved(config) {
78
- resolved = config;
79
- },
80
- configEnvironment(name, config) {
81
- if (!isServerEnvironment(name, config)) return;
82
- return { resolve: { noExternal: mergeNoExternal(config.resolve?.noExternal) } };
83
- },
84
- handleHotUpdate(context) {
85
- if (!/\.schedule\.(?:c|m)?[jt]s$/i.test(normalize(context.file))) return;
86
- const registryModule = context.server.moduleGraph.getModuleById(RESOLVED_SCHEDULE_REGISTRY_ID);
87
- if (registryModule) context.server.moduleGraph.invalidateModule(registryModule);
88
- const targetsModule = context.server.moduleGraph.getModuleById(RESOLVED_SCHEDULE_TARGETS_ID);
89
- if (targetsModule) context.server.moduleGraph.invalidateModule(targetsModule);
90
- },
91
- resolveId(id) {
92
- if (id === SCHEDULE_REGISTRY_ID) return RESOLVED_SCHEDULE_REGISTRY_ID;
93
- if (id === "#vitehub/schedule/targets") return RESOLVED_SCHEDULE_TARGETS_ID;
94
- },
95
- load(id) {
96
- if (id === RESOLVED_SCHEDULE_REGISTRY_ID) return createRegistryContents();
97
- if (id === RESOLVED_SCHEDULE_TARGETS_ID) return createTargetsContents();
98
- },
99
- async closeBundle() {
100
- if (!resolved || shouldSkipViteProviderBuild(resolved.command, getViteMode())) return;
101
- await generateProviderOutputs({
102
- bundleAlias: resolveStringAliases(resolved),
103
- clientOutDir: resolved.build.outDir,
104
- rootDir: resolved.root
105
- });
106
- }
107
- };
108
- }
109
- //#endregion
110
- export { hubSchedule };
1
+ import { n as hubSchedule, t as createScheduleNitroConfig } from "./vite-Dfeg9ZY3.js";
2
+ export { createScheduleNitroConfig, hubSchedule };
@@ -0,0 +1,23 @@
1
+ //#region src/runtime/wait-until.ts
2
+ function createLocalWaitUntil() {
3
+ const pending = /* @__PURE__ */ new Set();
4
+ let error;
5
+ let failed = false;
6
+ return {
7
+ async flush() {
8
+ while (pending.size > 0) await Promise.allSettled([...pending]);
9
+ if (failed) throw error;
10
+ },
11
+ waitUntil(value) {
12
+ const promise = Promise.resolve(value);
13
+ pending.add(promise);
14
+ promise.then(() => pending.delete(promise), (reason) => {
15
+ pending.delete(promise);
16
+ if (!failed) error = reason;
17
+ failed = true;
18
+ });
19
+ }
20
+ };
21
+ }
22
+ //#endregion
23
+ export { createLocalWaitUntil as t };
package/package.json CHANGED
@@ -1,74 +1,71 @@
1
1
  {
2
2
  "name": "@vite-hub/schedule",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Schedule primitives and definition discovery for ViteHub.",
5
- "author": "onmax <maximogarciamtnez@gmail.com>",
6
- "license": "Apache-2.0",
5
+ "keywords": [
6
+ "cron",
7
+ "schedule",
8
+ "vite",
9
+ "vitehub"
10
+ ],
7
11
  "homepage": "https://github.com/vite-hub/vitehub#readme",
12
+ "bugs": "https://github.com/vite-hub/vitehub/issues",
13
+ "license": "Apache-2.0",
14
+ "author": "onmax <maximogarciamtnez@gmail.com>",
8
15
  "repository": {
9
16
  "type": "git",
10
17
  "url": "git+https://github.com/vite-hub/vitehub.git",
11
18
  "directory": "packages/schedule"
12
19
  },
13
- "bugs": "https://github.com/vite-hub/vitehub/issues",
14
- "keywords": [
15
- "vitehub",
16
- "vite",
17
- "nitro",
18
- "schedule",
19
- "cron"
20
+ "files": [
21
+ "dist",
22
+ "package.json"
20
23
  ],
21
- "dependencies": {
22
- "cron-schedule": "^6.0.0",
23
- "esbuild": "^0.28.0"
24
- },
25
- "sideEffects": false,
26
24
  "type": "module",
25
+ "sideEffects": false,
27
26
  "types": "./dist/index.d.ts",
28
27
  "exports": {
29
28
  ".": "./dist/index.js",
30
- "./nitro": "./dist/nitro.js",
29
+ "./nuxt": "./dist/nuxt.js",
31
30
  "./runtime": "./dist/runtime.js",
31
+ "./runtime/driver": "./dist/runtime/driver.js",
32
+ "./runtime/process": "./dist/runtime/process.js",
32
33
  "./runtime/state": "./dist/runtime/state.js",
33
34
  "./runtime/static": "./dist/runtime/static.js",
34
35
  "./vite": "./dist/vite.js",
35
36
  "./package.json": "./package.json"
36
37
  },
37
- "files": [
38
- "dist",
39
- "package.json"
40
- ],
38
+ "dependencies": {
39
+ "cron-schedule": "^6.0.0",
40
+ "effect": "4.0.0-beta.99",
41
+ "esbuild": "^0.28.1",
42
+ "@vite-hub/runtime": "0.0.2",
43
+ "@vite-hub/kv": "0.0.2"
44
+ },
45
+ "devDependencies": {
46
+ "@types/node": "^24.13.3",
47
+ "publint": "^0.3.21",
48
+ "typescript": "^6.0.3",
49
+ "vite": "^8.0.8",
50
+ "vite-plus": "^0.1.24",
51
+ "vitest": "^4.1.4",
52
+ "@vite-hub/internal": "0.0.1"
53
+ },
41
54
  "peerDependencies": {
42
- "nitro": "^3.0.0-0",
43
- "vite": "^8.0.0",
44
- "@vite-hub/kv": "0.0.1"
55
+ "vite": ">=8.0.0"
45
56
  },
46
57
  "peerDependenciesMeta": {
47
- "@vite-hub/kv": {
48
- "optional": true
49
- },
50
- "nitro": {
51
- "optional": true
52
- },
53
58
  "vite": {
54
59
  "optional": true
55
60
  }
56
61
  },
57
- "devDependencies": {
58
- "@types/node": "^25.6.0",
59
- "nitro": "3.0.260311-beta",
60
- "publint": "^0.3.15",
61
- "tsdown": "^0.21.8",
62
- "typescript": "^6.0.2",
63
- "vite": "^8.0.8",
64
- "vitest": "^4.1.4",
65
- "@vite-hub/internal": "0.0.1",
66
- "@vite-hub/kv": "0.0.1"
62
+ "engines": {
63
+ "node": ">=24.15.0"
67
64
  },
68
65
  "scripts": {
69
- "build": "tsdown --tsconfig tsconfig.build.json",
70
- "dev": "tsdown --watch",
66
+ "build": "vp pack",
67
+ "dev": "vp pack --watch",
71
68
  "typecheck": "tsc --noEmit -p ./tsconfig.json",
72
- "test": "pnpm --dir . build && vitest run"
69
+ "test": "vp run -t @vite-hub/schedule#build && vp test"
73
70
  }
74
71
  }
@@ -1,401 +0,0 @@
1
- import { i as loadScheduleDefinition, n as getScheduleRunStore, p as ScheduleError, t as getRuntimeScheduleStore } from "./state-DoONzfij.js";
2
- //#region ../internal/src/runtime/random.ts
3
- function randomId(prefix) {
4
- const body = `${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
5
- return prefix ? `${prefix}_${body}` : body;
6
- }
7
- //#endregion
8
- //#region src/runtime/execute.ts
9
- function assertRuntimeExecuteOptionsObject(options) {
10
- if (typeof options !== "object" || options === null || Array.isArray(options)) throw new ScheduleError("Runtime Schedule execute options must be a schedule id or options object.", {
11
- code: "SCHEDULE_INVALID_INPUT",
12
- details: { options },
13
- httpStatus: 400
14
- });
15
- }
16
- function normalizeRunSource(source) {
17
- return source ?? "direct";
18
- }
19
- function toRunId(source, scheduleId, scheduledAt) {
20
- return `srun_${normalizeRunSource(source)}_${encodeURIComponent(scheduleId)}_${scheduledAt.toISOString()}`;
21
- }
22
- function validateScheduledAt(scheduledAt) {
23
- if (!(scheduledAt instanceof Date) || Number.isNaN(scheduledAt.getTime())) throw new ScheduleError("Schedule Run scheduledAt must be a valid Date.", {
24
- code: "SCHEDULE_INVALID_SCHEDULED_AT",
25
- details: { scheduledAt },
26
- httpStatus: 400
27
- });
28
- return scheduledAt;
29
- }
30
- function toRunError(error) {
31
- if (error instanceof Error) return {
32
- message: error.message,
33
- name: error.name,
34
- stack: error.stack
35
- };
36
- return { message: String(error) };
37
- }
38
- function requireUpdatedRun(run) {
39
- if (!run) throw new ScheduleError("Schedule Run bookkeeping update failed.", {
40
- code: "SCHEDULE_RUN_NOT_FOUND",
41
- httpStatus: 500
42
- });
43
- return run;
44
- }
45
- async function createOrGetRun(options) {
46
- const store = options.runStore ?? getScheduleRunStore();
47
- const id = toRunId(options.source, options.scheduleId, options.scheduledAt);
48
- const existing = await store.getRun(id);
49
- if (existing) return {
50
- created: false,
51
- run: existing
52
- };
53
- const now = /* @__PURE__ */ new Date();
54
- const runInput = {
55
- attemptCount: 0,
56
- createdAt: now,
57
- id,
58
- scheduleId: options.scheduleId,
59
- scheduledAt: options.scheduledAt,
60
- status: "pending",
61
- target: options.target,
62
- updatedAt: now
63
- };
64
- try {
65
- return {
66
- created: true,
67
- run: await store.createRun(runInput)
68
- };
69
- } catch (error) {
70
- const run = await store.getRun(id);
71
- if (run) return {
72
- created: false,
73
- run
74
- };
75
- throw error;
76
- }
77
- }
78
- async function startAttempt(run, store = getScheduleRunStore()) {
79
- const now = /* @__PURE__ */ new Date();
80
- const attempt = await store.createAttempt({
81
- createdAt: now,
82
- id: randomId("satt"),
83
- runId: run.id,
84
- startedAt: now,
85
- status: "running",
86
- updatedAt: now
87
- });
88
- await store.updateRun(run.id, {
89
- attemptCount: run.attemptCount + 1,
90
- startedAt: run.startedAt ?? now,
91
- status: "running",
92
- updatedAt: now
93
- });
94
- return attempt;
95
- }
96
- function toHandlerContext(run, attempt) {
97
- return {
98
- attemptId: attempt.id,
99
- id: run.id,
100
- runId: run.id,
101
- scheduleId: run.scheduleId,
102
- scheduledAt: run.scheduledAt,
103
- target: run.target
104
- };
105
- }
106
- async function completeRun(run, attempt, store = getScheduleRunStore()) {
107
- const now = /* @__PURE__ */ new Date();
108
- await store.updateAttempt(attempt.id, {
109
- completedAt: now,
110
- status: "succeeded",
111
- updatedAt: now
112
- });
113
- return requireUpdatedRun(await store.updateRun(run.id, {
114
- completedAt: now,
115
- status: "succeeded",
116
- updatedAt: now
117
- }));
118
- }
119
- async function failRun(run, attempt, error, store = getScheduleRunStore()) {
120
- const now = /* @__PURE__ */ new Date();
121
- const serializedError = toRunError(error);
122
- await store.updateAttempt(attempt.id, {
123
- completedAt: now,
124
- error: serializedError,
125
- status: "failed",
126
- updatedAt: now
127
- });
128
- return requireUpdatedRun(await store.updateRun(run.id, {
129
- completedAt: now,
130
- error: serializedError,
131
- status: "failed",
132
- updatedAt: now
133
- }));
134
- }
135
- async function createScheduleRun(options) {
136
- const scheduledAt = validateScheduledAt(options.scheduledAt ?? /* @__PURE__ */ new Date());
137
- return (await createOrGetRun({
138
- ...options,
139
- scheduledAt
140
- })).run;
141
- }
142
- async function executeSchedule(options) {
143
- const scheduledAt = validateScheduledAt(options.scheduledAt ?? /* @__PURE__ */ new Date());
144
- const { created, run } = await createOrGetRun({
145
- ...options,
146
- scheduledAt
147
- });
148
- if (!created) return run;
149
- const runStore = options.runStore ?? getScheduleRunStore();
150
- const attempt = await startAttempt(run, runStore);
151
- try {
152
- await options.definition.handler(toHandlerContext(run, attempt));
153
- return await completeRun(run, attempt, runStore);
154
- } catch (error) {
155
- await failRun(run, attempt, error, runStore);
156
- throw error;
157
- }
158
- }
159
- async function executeStaticSchedule(options) {
160
- return await executeSchedule({
161
- definition: options.definition,
162
- scheduleId: options.name,
163
- source: "static",
164
- scheduledAt: options.scheduledAt,
165
- target: options.name
166
- });
167
- }
168
- async function loadRequiredRuntimeSchedule(id, store = getRuntimeScheduleStore()) {
169
- const schedule = await store.get(id);
170
- if (!schedule) throw new ScheduleError(`Runtime Schedule not found: ${id}`, {
171
- code: "SCHEDULE_NOT_FOUND",
172
- details: { id },
173
- httpStatus: 404
174
- });
175
- return schedule;
176
- }
177
- async function executeRuntimeSchedule(options) {
178
- const runtimeOptions = typeof options === "string" ? { id: options } : options;
179
- assertRuntimeExecuteOptionsObject(runtimeOptions);
180
- const id = runtimeOptions.id;
181
- const scheduledAt = validateScheduledAt(runtimeOptions.scheduledAt ?? /* @__PURE__ */ new Date());
182
- const runtimeScheduleStore = runtimeOptions.runtimeScheduleStore;
183
- const scheduleRunStore = runtimeOptions.scheduleRunStore;
184
- const existingRun = await (scheduleRunStore ?? getScheduleRunStore()).getRun(toRunId("runtime", id, scheduledAt));
185
- if (existingRun) return existingRun;
186
- const schedule = await loadRequiredRuntimeSchedule(id, runtimeScheduleStore);
187
- if (!schedule.enabled) throw new ScheduleError(`Runtime Schedule is disabled: ${id}`, {
188
- code: "SCHEDULE_DISABLED",
189
- details: { id },
190
- httpStatus: 409
191
- });
192
- const definition = await loadScheduleDefinition(schedule.target);
193
- if (!definition) throw new ScheduleError(`Unknown Runtime Schedule target: ${schedule.target}`, {
194
- code: "SCHEDULE_TARGET_NOT_FOUND",
195
- details: { target: schedule.target },
196
- httpStatus: 404
197
- });
198
- if (definition.options?.allowRuntimeSchedules !== true) throw new ScheduleError(`Runtime Schedule target is not eligible: ${schedule.target}`, {
199
- code: "SCHEDULE_TARGET_NOT_ELIGIBLE",
200
- details: { target: schedule.target },
201
- httpStatus: 400
202
- });
203
- return await executeSchedule({
204
- definition,
205
- runStore: scheduleRunStore,
206
- scheduleId: schedule.id,
207
- source: "runtime",
208
- scheduledAt,
209
- target: schedule.target
210
- });
211
- }
212
- //#endregion
213
- //#region src/runtime/client.ts
214
- const cronRange = {
215
- dayOfMonth: {
216
- max: 31,
217
- min: 1
218
- },
219
- dayOfWeek: {
220
- max: 7,
221
- min: 0
222
- },
223
- hour: {
224
- max: 23,
225
- min: 0
226
- },
227
- minute: {
228
- max: 59,
229
- min: 0
230
- },
231
- month: {
232
- max: 12,
233
- min: 1
234
- }
235
- };
236
- const cronFieldRanges = [
237
- cronRange.minute,
238
- cronRange.hour,
239
- cronRange.dayOfMonth,
240
- cronRange.month,
241
- cronRange.dayOfWeek
242
- ];
243
- function parseCronNumber(value, range) {
244
- if (!/^\d+$/.test(value)) throw new Error("not a number");
245
- const parsed = Number(value);
246
- if (!Number.isInteger(parsed) || parsed < range.min || parsed > range.max) throw new Error("out of range");
247
- return parsed;
248
- }
249
- function validateCronPart(part, range) {
250
- const [value, step, extra] = part.split("/");
251
- if (extra !== void 0 || value === void 0 || value.length === 0) throw new Error("invalid step");
252
- if (step !== void 0) {
253
- if (parseCronNumber(step, {
254
- max: range.max,
255
- min: 1
256
- }) < 1) throw new Error("invalid step");
257
- }
258
- if (value === "*") return;
259
- const [start, end, extraRange] = value.split("-");
260
- if (extraRange !== void 0 || start === void 0 || start.length === 0) throw new Error("invalid range");
261
- const parsedStart = parseCronNumber(start, range);
262
- if (end === void 0) return;
263
- if (parseCronNumber(end, range) < parsedStart) throw new Error("invalid range");
264
- }
265
- function validateRuntimeScheduleCron(cron) {
266
- if (typeof cron !== "string" || cron.trim() !== cron || cron.length === 0) throw new ScheduleError("Runtime Schedule cron must be a five-field UTC cron expression.", {
267
- code: "SCHEDULE_INVALID_CRON",
268
- details: { cron },
269
- httpStatus: 400
270
- });
271
- const fields = cron.split(/\s+/);
272
- if (fields.length !== 5) throw new ScheduleError("Runtime Schedule cron must be a five-field UTC cron expression.", {
273
- code: "SCHEDULE_INVALID_CRON",
274
- details: { cron },
275
- httpStatus: 400
276
- });
277
- try {
278
- fields.forEach((field, index) => {
279
- if (field.length === 0) throw new Error("empty field");
280
- for (const part of field.split(",")) validateCronPart(part, cronFieldRanges[index]);
281
- });
282
- } catch {
283
- throw new ScheduleError(`Invalid Runtime Schedule cron expression: ${cron}`, {
284
- code: "SCHEDULE_INVALID_CRON",
285
- details: { cron },
286
- httpStatus: 400
287
- });
288
- }
289
- }
290
- async function assertRuntimeTarget(target) {
291
- if (typeof target !== "string" || !target.trim()) throw new ScheduleError("Runtime Schedule target must be a non-empty string.", {
292
- code: "SCHEDULE_INVALID_TARGET",
293
- details: { target },
294
- httpStatus: 400
295
- });
296
- const definition = await loadScheduleDefinition(target);
297
- if (!definition) throw new ScheduleError(`Unknown Runtime Schedule target: ${target}`, {
298
- code: "SCHEDULE_TARGET_NOT_FOUND",
299
- details: { target },
300
- httpStatus: 404
301
- });
302
- if (definition.options?.allowRuntimeSchedules !== true) throw new ScheduleError(`Schedule target is not runtime-schedule eligible: ${target}`, {
303
- code: "SCHEDULE_TARGET_NOT_ELIGIBLE",
304
- details: { target },
305
- httpStatus: 400
306
- });
307
- }
308
- function assertRuntimeScheduleInputObject(input, operation) {
309
- if (typeof input !== "object" || input === null || Array.isArray(input)) throw new ScheduleError(`Runtime Schedule ${operation} input must be an object.`, {
310
- code: "SCHEDULE_INVALID_INPUT",
311
- details: { input },
312
- httpStatus: 400
313
- });
314
- }
315
- async function validateCreateInput(input) {
316
- assertRuntimeScheduleInputObject(input, "create");
317
- await assertRuntimeTarget(input.target);
318
- validateRuntimeScheduleCron(input.cron);
319
- if (input.id !== void 0 && (typeof input.id !== "string" || !input.id.trim())) throw new ScheduleError("Runtime Schedule id must be a non-empty string when provided.", {
320
- code: "SCHEDULE_INVALID_ID",
321
- details: { id: input.id },
322
- httpStatus: 400
323
- });
324
- if (input.enabled !== void 0 && typeof input.enabled !== "boolean") throw new ScheduleError("Runtime Schedule enabled must be a boolean when provided.", {
325
- code: "SCHEDULE_INVALID_ENABLED",
326
- details: { enabled: input.enabled },
327
- httpStatus: 400
328
- });
329
- }
330
- async function validateUpdateInput(input) {
331
- assertRuntimeScheduleInputObject(input, "update");
332
- if (input.target !== void 0) await assertRuntimeTarget(input.target);
333
- if (input.cron !== void 0) validateRuntimeScheduleCron(input.cron);
334
- if (input.enabled !== void 0 && typeof input.enabled !== "boolean") throw new ScheduleError("Runtime Schedule enabled must be a boolean when provided.", {
335
- code: "SCHEDULE_INVALID_ENABLED",
336
- details: { enabled: input.enabled },
337
- httpStatus: 400
338
- });
339
- }
340
- async function updateRuntimeSchedule(id, input) {
341
- await validateUpdateInput(input);
342
- const updated = await getRuntimeScheduleStore().update(id, {
343
- ...input,
344
- updatedAt: /* @__PURE__ */ new Date()
345
- });
346
- if (!updated) throw new ScheduleError(`Runtime Schedule not found: ${id}`, {
347
- code: "SCHEDULE_NOT_FOUND",
348
- details: { id },
349
- httpStatus: 404
350
- });
351
- return updated;
352
- }
353
- const schedules = {
354
- async create(input) {
355
- await validateCreateInput(input);
356
- const now = /* @__PURE__ */ new Date();
357
- return await getRuntimeScheduleStore().create({
358
- createdAt: now,
359
- cron: input.cron,
360
- enabled: input.enabled ?? true,
361
- id: input.id ?? randomId("sched"),
362
- target: input.target,
363
- updatedAt: now
364
- });
365
- },
366
- async delete(id) {
367
- return await getRuntimeScheduleStore().delete(id);
368
- },
369
- async disable(id) {
370
- return await updateRuntimeSchedule(id, { enabled: false });
371
- },
372
- async enable(id) {
373
- return await updateRuntimeSchedule(id, { enabled: true });
374
- },
375
- async get(id) {
376
- return await getRuntimeScheduleStore().get(id);
377
- },
378
- async getRun(id) {
379
- return await getScheduleRunStore().getRun(id);
380
- },
381
- async list() {
382
- return await getRuntimeScheduleStore().list();
383
- },
384
- async listAttempts(runId) {
385
- return await getScheduleRunStore().listAttempts(runId);
386
- },
387
- async listRuns() {
388
- return await getScheduleRunStore().listRuns();
389
- },
390
- async run(id, options = {}) {
391
- return await executeRuntimeSchedule({
392
- id,
393
- scheduledAt: options.scheduledAt
394
- });
395
- },
396
- async update(id, input) {
397
- return await updateRuntimeSchedule(id, input);
398
- }
399
- };
400
- //#endregion
401
- export { executeSchedule as a, executeRuntimeSchedule as i, validateRuntimeScheduleCron as n, executeStaticSchedule as o, createScheduleRun as r, schedules as t };