@stacksjs/cron 0.70.45 → 0.70.54
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/LICENSE.md +21 -0
- package/dist/index.js +1 -1
- package/dist/src/parser.d.ts +10 -3
- package/package.json +1 -1
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Open Web Foundation
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
var
|
|
2
|
+
var g={"@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 * * * *"},w={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},M={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},S=new Set;function D(q,b){if(S.has(q))return;S.add(q),console.warn(`[cron] 6-field cron expression '${q}' has seconds='${b}' \u2014 the parser is 5-field only. `+"Seconds field IGNORED. For second-precision use the scheduler's '.everySecond()' instead.")}function y(q,b){return q.replace(/[a-z]+/gi,(V)=>{let X=b[V.toLowerCase()];if(X===void 0)throw Error(`Invalid cron value: ${V}`);return String(X)})}function U(q,b,V,X){let _=X?y(q,X):q,Q=new Set;for(let H of _.split(",")){let $=H.match(/^(.+)\/(\d+)$/),R,Z=1;if($){if(R=$[1]??"",Z=Number.parseInt($[2]??"",10),Z<=0)throw Error(`Invalid step: ${Z}`)}else R=H;if(R==="*")for(let C=b;C<=V;C+=Z)Q.add(C);else if(R.includes("-")){let[C,J]=R.split("-"),K=Number.parseInt(C??"",10),Y=Number.parseInt(J??"",10);if(Number.isNaN(K)||Number.isNaN(Y))throw Error(`Invalid range: ${R}`);if(K<b||Y>V)throw Error(`Range out of bounds: ${R} (${b}-${V})`);for(let j=K;j<=Y;j+=Z)Q.add(j)}else{let C=Number.parseInt(R,10);if(Number.isNaN(C))throw Error(`Invalid cron value: ${R}`);if(C<b||C>V)throw Error(`Value out of range: ${C} (${b}-${V})`);Q.add(C)}}return Q}function P(q,b,V={}){let X=q.trim(),Q=(g[X.toLowerCase()]??X).split(/\s+/).filter(Boolean);if(Q.length===6){let L=Q[0];if(L!=="0"&&L!=="*")D(q,L);Q=Q.slice(1)}if(Q.length!==5)throw Error(`Invalid cron expression: expected 5 fields (or 6 with leading seconds), got ${Q.length}`);let[H,$,R,Z,C]=Q,J=U(H,0,59),K=U($,0,23),Y=U(R,1,31),j=U(Z,1,12,w),E=U(C,0,7,M);if(E.has(7))E.add(0),E.delete(7);let k=R==="*",z=C==="*",I=b instanceof Date?b.getTime():typeof b==="number"?b:Date.now();if(Number.isNaN(I)||!Number.isFinite(I))throw Error("Invalid date");let A=V.tz,T=A?x(A):(L)=>({year:L.getUTCFullYear(),month:L.getUTCMonth()+1,day:L.getUTCDate(),hour:L.getUTCHours(),minute:L.getUTCMinutes(),dow:L.getUTCDay()}),G=new Date(I);G.setUTCSeconds(0,0),G.setUTCMinutes(G.getUTCMinutes()+1);let W=G.getTime()+126230400000;while(G.getTime()<W){let L=T(G);if(!j.has(L.month)){G.setUTCMonth(G.getUTCMonth()+1,1),G.setUTCHours(0,0,0,0);continue}let N=Y.has(L.day),O=E.has(L.dow),B;if(k&&z)B=!0;else if(k)B=O;else if(z)B=N;else B=N||O;if(!B){G.setUTCDate(G.getUTCDate()+1),G.setUTCHours(0,0,0,0);continue}if(!K.has(L.hour)){G.setUTCHours(G.getUTCHours()+1,0,0,0);continue}if(!J.has(L.minute)){G.setUTCMinutes(G.getUTCMinutes()+1,0,0);continue}return new Date(G.getTime())}return null}function x(q){let b=new Intl.DateTimeFormat("en-US",{timeZone:q,hourCycle:"h23",year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"numeric",weekday:"short"}),V={Sun:0,Mon:1,Tue:2,Wed:3,Thu:4,Fri:5,Sat:6};return(X)=>{let _=b.formatToParts(X),Q=0,H=0,$=0,R=0,Z=0,C=0;for(let J of _)if(J.type==="year")Q=Number.parseInt(J.value,10);else if(J.type==="month")H=Number.parseInt(J.value,10);else if(J.type==="day")$=Number.parseInt(J.value,10);else if(J.type==="hour")R=Number.parseInt(J.value,10)%24;else if(J.type==="minute")Z=Number.parseInt(J.value,10);else if(J.type==="weekday")C=V[J.value]??0;return{year:Q,month:H,day:$,hour:R,minute:Z,dow:C}}}function v(q,b){if(typeof Bun<"u"&&Bun.cron?.parse)return Bun.cron.parse(q,b);return P(q,b)}async function f(q,b,V){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(q,b,V)}async function c(q){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(q)}export{c as remove,f as register,P as parseCron,v as parse};
|
package/dist/src/parser.d.ts
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Parse a cron expression and return the next matching
|
|
2
|
+
* Parse a cron expression and return the next matching Date.
|
|
3
3
|
*
|
|
4
4
|
* @param expression - A 5-field cron expression or nickname (@daily, etc.)
|
|
5
5
|
* @param relativeDate - Starting point for the search (defaults to Date.now())
|
|
6
|
-
* @
|
|
6
|
+
* @param options - Optional `{ tz }` for timezone-local interpretation
|
|
7
|
+
* @returns The next Date matching the expression, or null if no match within ~4 years
|
|
7
8
|
*/
|
|
8
|
-
export declare function parseCron(expression: string, relativeDate?: Date | number): Date | null;
|
|
9
|
+
export declare function parseCron(expression: string, relativeDate?: Date | number, options?: ParseCronOptions): Date | null;
|
|
10
|
+
/**
|
|
11
|
+
* Options for `parseCron` (stacksjs/stacks#1877 Cr-5).
|
|
12
|
+
*/
|
|
13
|
+
export declare interface ParseCronOptions {
|
|
14
|
+
tz?: string
|
|
15
|
+
}
|