UNPKG

@mastra/core

Version:
57 lines (56 loc) 2.12 kB
let croner = require("croner"); //#region src/workflows/scheduler/cron.ts /** * Validate a cron expression. Throws if the pattern is invalid. * * @param cron - Cron expression (5-, 6-, or 7-part). * @param timezone - Optional IANA timezone (e.g. 'America/New_York'). */ function validateCron(cron, timezone) { if (typeof cron !== "string" || cron.trim() === "") throw new Error(`Invalid cron expression: expected a non-empty cron string (e.g. "0 * * * *"), but received ${cron === void 0 ? "undefined" : JSON.stringify(cron)}.`); let job; try { job = new croner.Cron(cron); } catch (error) { const reason = error instanceof Error ? error.message : String(error); throw new Error(`Invalid cron expression "${cron}": ${reason}`); } if (timezone !== void 0) try { new croner.Cron(cron, { timezone }).nextRun(); } catch (error) { const reason = error instanceof Error ? error.message : String(error); throw new Error(`Invalid timezone "${timezone}": ${reason}`); } else job.nextRun(); } /** * Compute the next fire time (ms since epoch) for a cron expression. * * @param cron - Cron expression. * @param options - Optional timezone and reference time (`after`, ms since epoch). * The next fire time is the first cron occurrence strictly after `after`. * Defaults to `Date.now()`. * @returns The next fire time in ms since epoch. * @throws If the cron expression is invalid or has no future occurrence. */ function computeNextFireAt(cron, options) { const job = new croner.Cron(cron, { timezone: options?.timezone }); const reference = options?.after !== void 0 ? new Date(options.after) : /* @__PURE__ */ new Date(); const next = job.nextRun(reference); if (!next) throw new Error(`Cron expression "${cron}" has no future occurrence after ${reference.toISOString()}`); return next.getTime(); } //#endregion Object.defineProperty(exports, "computeNextFireAt", { enumerable: true, get: function() { return computeNextFireAt; } }); Object.defineProperty(exports, "validateCron", { enumerable: true, get: function() { return validateCron; } }); //# sourceMappingURL=cron-CrmzJKFJ.cjs.map