@mastra/core
Version:
1 lines • 3.28 kB
Source Map (JSON)
{"version":3,"file":"cron-CrmzJKFJ.cjs","names":["Cron"],"sources":["../src/workflows/scheduler/cron.ts"],"sourcesContent":["import { Cron } from 'croner';\n\n/**\n * Validate a cron expression. Throws if the pattern is invalid.\n *\n * @param cron - Cron expression (5-, 6-, or 7-part).\n * @param timezone - Optional IANA timezone (e.g. 'America/New_York').\n */\nexport function validateCron(cron: string, timezone?: string): void {\n if (typeof cron !== 'string' || cron.trim() === '') {\n throw new Error(\n `Invalid cron expression: expected a non-empty cron string (e.g. \"0 * * * *\"), but received ${cron === undefined ? 'undefined' : JSON.stringify(cron)}.`,\n );\n }\n // Croner throws synchronously on an invalid pattern when the job is\n // constructed. Validate the pattern on its own first so timezone problems\n // (which croner only surfaces lazily) are not mislabeled as cron errors.\n let job: Cron;\n try {\n job = new Cron(cron);\n } catch (error) {\n const reason = error instanceof Error ? error.message : String(error);\n throw new Error(`Invalid cron expression \"${cron}\": ${reason}`);\n }\n // The timezone is only exercised when a fire time is computed.\n if (timezone !== undefined) {\n try {\n new Cron(cron, { timezone }).nextRun();\n } catch (error) {\n const reason = error instanceof Error ? error.message : String(error);\n throw new Error(`Invalid timezone \"${timezone}\": ${reason}`);\n }\n } else {\n job.nextRun();\n }\n}\n\n/**\n * Compute the next fire time (ms since epoch) for a cron expression.\n *\n * @param cron - Cron expression.\n * @param options - Optional timezone and reference time (`after`, ms since epoch).\n * The next fire time is the first cron occurrence strictly after `after`.\n * Defaults to `Date.now()`.\n * @returns The next fire time in ms since epoch.\n * @throws If the cron expression is invalid or has no future occurrence.\n */\nexport function computeNextFireAt(cron: string, options?: { timezone?: string; after?: number }): number {\n const job = new Cron(cron, { timezone: options?.timezone });\n const reference = options?.after !== undefined ? new Date(options.after) : new Date();\n const next = job.nextRun(reference);\n if (!next) {\n throw new Error(`Cron expression \"${cron}\" has no future occurrence after ${reference.toISOString()}`);\n }\n return next.getTime();\n}\n"],"mappings":";;;;;;;;AAQA,SAAgB,aAAa,MAAc,UAAyB;CAClE,IAAI,OAAO,SAAS,YAAY,KAAK,KAAK,MAAM,IAC9C,MAAM,IAAI,MACR,8FAA8F,SAAS,KAAA,IAAY,cAAc,KAAK,UAAU,IAAI,EAAE,EACxJ;CAKF,IAAI;CACJ,IAAI;EACF,MAAM,IAAIA,OAAAA,KAAK,IAAI;CACrB,SAAS,OAAO;EACd,MAAM,SAAS,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;EACpE,MAAM,IAAI,MAAM,4BAA4B,KAAK,KAAK,QAAQ;CAChE;CAEA,IAAI,aAAa,KAAA,GACf,IAAI;EACF,IAAIA,OAAAA,KAAK,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,QAAQ;CACvC,SAAS,OAAO;EACd,MAAM,SAAS,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK;EACpE,MAAM,IAAI,MAAM,qBAAqB,SAAS,KAAK,QAAQ;CAC7D;MAEA,IAAI,QAAQ;AAEhB;;;;;;;;;;;AAYA,SAAgB,kBAAkB,MAAc,SAAyD;CACvG,MAAM,MAAM,IAAIA,OAAAA,KAAK,MAAM,EAAE,UAAU,SAAS,SAAS,CAAC;CAC1D,MAAM,YAAY,SAAS,UAAU,KAAA,IAAY,IAAI,KAAK,QAAQ,KAAK,oBAAI,IAAI,KAAK;CACpF,MAAM,OAAO,IAAI,QAAQ,SAAS;CAClC,IAAI,CAAC,MACH,MAAM,IAAI,MAAM,oBAAoB,KAAK,mCAAmC,UAAU,YAAY,GAAG;CAEvG,OAAO,KAAK,QAAQ;AACtB"}