@stacksjs/cron 0.75.382 → 0.75.387
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/index.d.ts +8 -3
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
/// <reference path="./bun-cron.d.ts" />
|
|
2
2
|
import { parseCron } from './parser';
|
|
3
|
-
// Parse a cron expression and return the next matching Date
|
|
4
|
-
//
|
|
5
|
-
// to our own 5-field parser with identical behavior.
|
|
3
|
+
// Parse a cron expression and return the next matching Date, in UTC unless
|
|
4
|
+
// `options.tz` names a zone.
|
|
6
5
|
//
|
|
7
6
|
// Examples:
|
|
8
7
|
// parse('0 0 * * *') → next midnight UTC
|
|
9
8
|
// parse('@hourly') → next hour
|
|
10
9
|
// parse('0,15,30,45 * * * *') → next 15-min mark
|
|
10
|
+
//
|
|
11
|
+
// Not `Bun.cron.parse`, though it exists. That one answers in the process's
|
|
12
|
+
// local time, as crontab does, so handing it the call made "midnight" mean
|
|
13
|
+
// midnight UTC on a UTC server and midnight wherever the server was otherwise.
|
|
14
|
+
// The scheduler's UTC path calls this without a `tz`, so a UTC app on a New
|
|
15
|
+
// York box ran its daily tasks at 05:00 UTC.
|
|
11
16
|
export declare function parse(expression: string, relativeDate?: Date | number, options?: import('./parser').ParseCronOptions): Date | null;
|
|
12
17
|
/**
|
|
13
18
|
* Register an OS-level cron job that persists across process restarts.
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
var
|
|
2
|
+
var B={"@yearly":"0 0 1 1 *","@annually":"0 0 1 1 *","@monthly":"0 0 1 * *","@weekly":"0 0 * * 0","@daily":"0 0 * * *","@midnight":"0 0 * * *","@hourly":"0 * * * *"},F={jan:1,feb:2,mar:3,apr:4,may:5,jun:6,jul:7,aug:8,sep:9,oct:10,nov:11,dec:12,january:1,february:2,march:3,april:4,june:6,july:7,august:8,september:9,october:10,november:11,december:12},P={sun:0,mon:1,tue:2,wed:3,thu:4,fri:5,sat:6,sunday:0,monday:1,tuesday:2,wednesday:3,thursday:4,friday:5,saturday:6},O=/^(\*|\d+(?:-\d+)?)(?:\/(\d+))?$/;function R(i,r){return i.replace(/[a-z]+/gi,(o)=>{let c=r[o.toLowerCase()];if(c===void 0)throw Error(`Invalid cron value: ${o}`);return String(c)})}function y(i,r,o,c){let w=c?R(i,c):i,s=new Set;for(let m of w.split(",")){let f=O.exec(m);if(!f)throw Error(`Invalid cron value: ${m}`);let[,u="",l]=f,a=l===void 0?1:Number(l);if(a<=0)throw Error(`Invalid step: ${a}`);if(u==="*")for(let e=r;e<=o;e+=a)s.add(e);else if(u.includes("-")){let[e,d]=u.split("-").map(Number);if(e<r||d>o)throw Error(`Range out of bounds: ${u} (${r}-${o})`);if(e>d)throw Error(`Range must be ascending: ${u} (list both ends, e.g. '${e}-${o},${r}-${d}')`);for(let h=e;h<=d;h+=a)s.add(h)}else{let e=Number(u);if(e<r||e>o)throw Error(`Value out of range: ${e} (${r}-${o})`);if(l===void 0)s.add(e);else for(let d=e;d<=o;d+=a)s.add(d)}}return s}function U(i,r,o={}){let c=i.trim(),s=(B[c.toLowerCase()]??c).split(/\s+/).filter(Boolean);if(s.length===6){if(s[0]!=="0")throw Error(`Invalid cron expression: '${i}' runs on seconds ('${s[0]}'), and this parser has minute resolution. Use a seconds field of 0, or the scheduler's .everySecond() / .cron('*/N * * * * *')`);s=s.slice(1)}if(s.length!==5)throw Error(`Invalid cron expression: expected 5 fields (or 6 with leading seconds), got ${s.length}`);let[m,f,u,l,a]=s,e=y(m,0,59),d=y(f,0,23),h=y(u,1,31),M=y(l,1,12,F),g=y(a,0,7,P);if(g.has(7))g.add(0),g.delete(7);let b=u==="*",T=a==="*",v=r instanceof Date?r.getTime():typeof r==="number"?r:Date.now();if(Number.isNaN(v)||!Number.isFinite(v))throw Error("Invalid date");let C=o.tz,E=C?x(C):(t)=>({year:t.getUTCFullYear(),month:t.getUTCMonth()+1,day:t.getUTCDate(),hour:t.getUTCHours(),minute:t.getUTCMinutes(),dow:t.getUTCDay()}),n=new Date(v);n.setUTCSeconds(0,0),n.setUTCMinutes(n.getUTCMinutes()+1);let I=n.getTime()+126230400000;while(n.getTime()<I){let t=E(n);if(C){let S=b&&T?!0:b?g.has(t.dow):T?h.has(t.day):h.has(t.day)||g.has(t.dow);if(!M.has(t.month)||!S||!d.has(t.hour)){n.setTime(n.getTime()+(60-t.minute)*60000);continue}if(!e.has(t.minute)){n.setTime(n.getTime()+60000);continue}return new Date(n.getTime())}if(!M.has(t.month)){n.setUTCMonth(n.getUTCMonth()+1,1),n.setUTCHours(0,0,0,0);continue}let N=h.has(t.day),D=g.has(t.dow),p;if(b&&T)p=!0;else if(b)p=D;else if(T)p=N;else p=N||D;if(!p){n.setUTCDate(n.getUTCDate()+1),n.setUTCHours(0,0,0,0);continue}if(!d.has(t.hour)){n.setUTCHours(n.getUTCHours()+1,0,0,0);continue}if(!e.has(t.minute)){n.setUTCMinutes(n.getUTCMinutes()+1,0,0);continue}return new Date(n.getTime())}return null}function x(i){let r=new Intl.DateTimeFormat("en-US",{timeZone:i,hourCycle:"h23",year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"numeric",weekday:"short"}),o={Sun:0,Mon:1,Tue:2,Wed:3,Thu:4,Fri:5,Sat:6};return(c)=>{let w=r.formatToParts(c),s=0,m=0,f=0,u=0,l=0,a=0;for(let e of w)if(e.type==="year")s=Number.parseInt(e.value,10);else if(e.type==="month")m=Number.parseInt(e.value,10);else if(e.type==="day")f=Number.parseInt(e.value,10);else if(e.type==="hour")u=Number.parseInt(e.value,10)%24;else if(e.type==="minute")l=Number.parseInt(e.value,10);else if(e.type==="weekday")a=o[e.value]??0;return{year:s,month:m,day:f,hour:u,minute:l,dow:a}}}function A(i,r,o={}){return U(i,r,o)}async function H(i,r,o){if(typeof Bun>"u"||!Bun.cron)throw Error("Bun.cron is not available. OS-level cron requires a Bun version with native cron support.");await Bun.cron(i,r,o)}async function k(i){if(typeof Bun>"u"||!Bun.cron?.remove)throw Error("Bun.cron is not available. OS-level cron requires a Bun version with native cron support.");await Bun.cron.remove(i)}export{A as parse,U as parseCron,H as register,k as remove};
|