croner 9.1.1-dev.0 → 10.0.0-dev.0
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/README.md +37 -22
- package/dist/croner.cjs +1 -1
- package/dist/croner.d.cts +8 -8
- package/dist/croner.d.ts +8 -8
- package/dist/croner.js +1 -1
- package/dist/croner.umd.js +1 -1
- package/dist/date.d.ts +17 -7
- package/dist/options.d.ts +5 -5
- package/dist/pattern.d.ts +5 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ Trigger functions or evaluate cron expressions in JavaScript or TypeScript. No d
|
|
|
10
10
|
|
|
11
11
|
* Trigger functions in JavaScript using [Cron](https://en.wikipedia.org/wiki/Cron#CRON_expression) syntax.
|
|
12
12
|
* Evaluate cron expressions and get a list of upcoming run times.
|
|
13
|
-
*
|
|
13
|
+
* **OCPS 1.0-1.4 compliant**: Fully supports the [Open Cron Pattern Specification](https://github.com/open-source-cron/ocps) including advanced features like seconds/year fields, `L` (last), `W` (weekday), `#` (nth occurrence), and `+` (AND logic).
|
|
14
14
|
* Works in Node.js >=18 (both require and import), Deno >=1.16 and Bun >=1.0.0.
|
|
15
15
|
* Works in browsers as standalone, UMD or ES-module.
|
|
16
16
|
* Target different [time zones](https://croner.56k.guru/usage/examples/#time-zone).
|
|
@@ -69,10 +69,10 @@ Using Deno
|
|
|
69
69
|
|
|
70
70
|
```typescript
|
|
71
71
|
// From deno.land/x
|
|
72
|
-
import { Cron } from "https://deno.land/x/croner@
|
|
72
|
+
import { Cron } from "https://deno.land/x/croner@10.0.0/dist/croner.js";
|
|
73
73
|
|
|
74
74
|
// ... or jsr.io
|
|
75
|
-
import { Cron } from "jsr:@hexagon/croner@
|
|
75
|
+
import { Cron } from "jsr:@hexagon/croner@10.0.0";
|
|
76
76
|
```
|
|
77
77
|
|
|
78
78
|
In a webpage using the UMD-module
|
|
@@ -160,7 +160,7 @@ job.name // Optional job name, populated if a name were passed to options
|
|
|
160
160
|
|
|
161
161
|
#### Pattern
|
|
162
162
|
|
|
163
|
-
|
|
163
|
+
Croner is fully compliant with the [Open Cron Pattern Specification (OCPS)](https://github.com/open-source-cron/ocps) versions 1.0 through 1.4. The expressions are based on Vixie Cron with powerful extensions:
|
|
164
164
|
|
|
165
165
|
```javascript
|
|
166
166
|
// ┌──────────────── (optional) second (0 - 59)
|
|
@@ -174,40 +174,51 @@ The expressions used by Croner are very similar to those of Vixie Cron, but with
|
|
|
174
174
|
// * * * * * *
|
|
175
175
|
```
|
|
176
176
|
|
|
177
|
-
*
|
|
178
|
-
-
|
|
179
|
-
-
|
|
180
|
-
-
|
|
181
|
-
|
|
177
|
+
* **OCPS 1.2**: Optional second and year fields for enhanced precision:
|
|
178
|
+
- 6-field format: `SECOND MINUTE HOUR DAY-OF-MONTH MONTH DAY-OF-WEEK`
|
|
179
|
+
- 7-field format: `SECOND MINUTE HOUR DAY-OF-MONTH MONTH DAY-OF-WEEK YEAR`
|
|
180
|
+
- Supported year range: 1-9999
|
|
181
|
+
|
|
182
|
+
* **OCPS 1.3**: Advanced calendar modifiers:
|
|
183
|
+
- *L*: Last day of month or last occurrence of a weekday. `L` in day-of-month = last day of month; `5#L` or `FRI#L` = last Friday of the month.
|
|
184
|
+
- *W*: Nearest weekday. `15W` triggers on the weekday closest to the 15th (moves to Friday if 15th is Saturday, Monday if 15th is Sunday). Won't cross month boundaries.
|
|
185
|
+
- *#*: Nth occurrence of a weekday. `5#2` = second Friday; `MON#1` = first Monday of the month.
|
|
186
|
+
|
|
187
|
+
* **OCPS 1.4**: Enhanced logical control:
|
|
188
|
+
- *+*: Explicit AND logic modifier. Prefix the day-of-week field with `+` to require both day-of-month AND day-of-week to match. Example: `0 12 1 * +MON` only triggers when the 1st is also a Monday.
|
|
189
|
+
- *?*: Wildcard alias (behaves identically to `*`). **Non-portable**: Its use is discouraged in patterns intended for cross-system use. Supported in all fields for compatibility, but primarily meaningful in day-of-month and day-of-week fields.
|
|
190
|
+
- Proper DST handling: Jobs scheduled during DST gaps are skipped; jobs in DST overlaps run once at first occurrence.
|
|
182
191
|
|
|
183
192
|
* Croner allows you to pass a JavaScript Date object or an ISO 8601 formatted string as a pattern. The scheduled function will trigger at the specified date/time and only once. If you use a timezone different from the local timezone, you should pass the ISO 8601 local time in the target location and specify the timezone using the options (2nd parameter).
|
|
184
193
|
|
|
185
|
-
*
|
|
194
|
+
* By default, Croner uses OR logic for day-of-month and day-of-week (OCPS 1.0 compliant). Example: `0 20 1 * MON` triggers on the 1st of the month OR on Mondays. Use the `+` modifier (`0 20 1 * +MON`) or `{ legacyMode: false }` option for AND logic. For more information, see issue [#53](https://github.com/Hexagon/croner/issues/53).
|
|
186
195
|
|
|
187
196
|
| Field | Required | Allowed values | Allowed special characters | Remarks |
|
|
188
197
|
|--------------|----------|----------------|----------------------------|---------------------------------------|
|
|
189
|
-
| Seconds | Optional | 0-59 | * , - / ? |
|
|
198
|
+
| Seconds | Optional | 0-59 | * , - / ? | OCPS 1.2: Optional, defaults to 0 |
|
|
190
199
|
| Minutes | Yes | 0-59 | * , - / ? | |
|
|
191
200
|
| Hours | Yes | 0-23 | * , - / ? | |
|
|
192
|
-
| Day of Month | Yes | 1-31 | * , - / ? L
|
|
201
|
+
| Day of Month | Yes | 1-31 | * , - / ? L W | L = last day, W = nearest weekday |
|
|
193
202
|
| Month | Yes | 1-12 or JAN-DEC| * , - / ? | |
|
|
194
|
-
| Day of Week | Yes | 0-7 or SUN-MON | * , - / ? L #
|
|
203
|
+
| Day of Week | Yes | 0-7 or SUN-MON | * , - / ? L # + | 0 and 7 = Sunday<br># = nth occurrence (e.g. MON#2)<br>+ = AND logic modifier (OCPS 1.4) |
|
|
204
|
+
| Year | Optional | 1-9999 | * , - / | OCPS 1.2: Optional, defaults to * |
|
|
195
205
|
|
|
196
206
|
> **Note**
|
|
197
207
|
> Weekday and month names are case-insensitive. Both `MON` and `mon` work.
|
|
198
|
-
> When using `L` in the Day of Week field, it affects all specified weekdays. For example, `5-6#L` means the last Friday and Saturday in the month.
|
|
199
|
-
> The
|
|
208
|
+
> When using `L` in the Day of Week field with a range, it affects all specified weekdays. For example, `5-6#L` means the last Friday and Saturday in the month.
|
|
209
|
+
> The `#` character specifies the "nth" weekday of the month. For example, `5#2` = second Friday, `MON#1` = first Monday.
|
|
210
|
+
> The `W` character operates within the current month and won't cross month boundaries. If the 1st is a Saturday, `1W` matches Monday the 3rd.
|
|
211
|
+
> The `+` modifier (OCPS 1.4) enforces AND logic: `0 12 1 * +MON` only runs when the 1st is also a Monday.
|
|
200
212
|
|
|
201
|
-
|
|
213
|
+
**OCPS 1.1**: Predefined schedule nicknames are supported:
|
|
202
214
|
|
|
203
215
|
| Nickname | Description |
|
|
204
216
|
| -------- | ----------- |
|
|
205
|
-
| \@yearly | Run once a year,
|
|
206
|
-
| \@
|
|
207
|
-
| \@
|
|
208
|
-
| \@
|
|
209
|
-
| \@
|
|
210
|
-
| \@hourly | Run once an hour, ie. "0 * * * *". |
|
|
217
|
+
| \@yearly / \@annually | Run once a year, i.e. "0 0 1 1 *". |
|
|
218
|
+
| \@monthly | Run once a month, i.e. "0 0 1 * *". |
|
|
219
|
+
| \@weekly | Run once a week, i.e. "0 0 * * 0". |
|
|
220
|
+
| \@daily / \@midnight | Run once a day, i.e. "0 0 * * *". |
|
|
221
|
+
| \@hourly | Run once an hour, i.e. "0 * * * *". |
|
|
211
222
|
|
|
212
223
|
## Why another JavaScript cron implementation
|
|
213
224
|
|
|
@@ -233,8 +244,12 @@ Because the existing ones are not good enough. They have serious bugs, use bloat
|
|
|
233
244
|
| Controls (stop/resume) | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
234
245
|
| Range (0-13) | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
235
246
|
| Stepping (*/5) | ✓ | ✓ | ✓ | ✓ | ✓ |
|
|
247
|
+
| Seconds field (OCPS 1.2) | ✓ | | | | |
|
|
248
|
+
| Year field (OCPS 1.2) | ✓ | | | | |
|
|
236
249
|
| Last day of month (L) | ✓ | ✓ | | | |
|
|
237
250
|
| Nth weekday of month (#) | ✓ | ✓ | | | |
|
|
251
|
+
| Nearest weekday (W) | ✓ | | | | |
|
|
252
|
+
| AND logic modifier (+) | ✓ | | | | |
|
|
238
253
|
|
|
239
254
|
<details>
|
|
240
255
|
<summary>In depth comparison of various libraries</summary>
|
package/dist/croner.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var O=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var W=(r,t)=>{for(var e in t)O(r,e,{get:t[e],enumerable:!0})},F=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of I(t))!z.call(r,i)&&i!==e&&O(r,i,{get:()=>t[i],enumerable:!(n=A(t,i))||n.enumerable});return r};var j=r=>F(O({},"__esModule",{value:!0}),r);var H={};W(H,{Cron:()=>R,CronDate:()=>l,CronPattern:()=>C,scheduledJobs:()=>T});module.exports=j(H);function b(r){return Date.UTC(r.y,r.m-1,r.d,r.h,r.i,r.s)}function P(r,t){return r.y===t.y&&r.m===t.m&&r.d===t.d&&r.h===t.h&&r.i===t.i&&r.s===t.s}function L(r,t){let e=new Date(Date.parse(r));if(isNaN(e))throw new Error("Invalid ISO8601 passed to timezone parser.");let n=r.substring(9);return n.includes("Z")||n.includes("+")||n.includes("-")?w(e.getUTCFullYear(),e.getUTCMonth()+1,e.getUTCDate(),e.getUTCHours(),e.getUTCMinutes(),e.getUTCSeconds(),"Etc/UTC"):w(e.getFullYear(),e.getMonth()+1,e.getDate(),e.getHours(),e.getMinutes(),e.getSeconds(),t)}function N(r,t,e){return k(L(r,t),e)}function k(r,t){let e=new Date(b(r)),n=g(e,r.tz),i=b(r),s=b(n),a=i-s,u=new Date(e.getTime()+a),o=g(u,r.tz);if(P(o,r)){let m=new Date(u.getTime()-36e5),p=g(m,r.tz);return P(p,r)?m:u}let h=new Date(u.getTime()+b(r)-b(o)),c=g(h,r.tz);if(P(c,r))return h;if(t)throw new Error("Invalid date passed to fromTZ()");return u.getTime()>h.getTime()?u:h}function g(r,t){let n=new Intl.DateTimeFormat("en-US",{timeZone:t,year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"numeric",second:"numeric",hour12:!1}).formatToParts(r),i={year:0,month:0,day:0,hour:0,minute:0,second:0};for(let s of n)(s.type==="year"||s.type==="month"||s.type==="day"||s.type==="hour"||s.type==="minute"||s.type==="second")&&(i[s.type]=parseInt(s.value,10));return i.hour===24&&(i.hour=0),{y:i.year,m:i.month,d:i.day,h:i.hour,i:i.minute,s:i.second,tz:t}}function w(r,t,e,n,i,s,a){return{y:r,m:t,d:e,h:n,i,s,tz:a}}var D=32,d=31|D,_=[1,2,4,8,16],C=class{pattern;timezone;second;minute;hour;day;month;dayOfWeek;lastDayOfMonth;starDOM;starDOW;constructor(t,e){this.pattern=t,this.timezone=e,this.second=Array(60).fill(0),this.minute=Array(60).fill(0),this.hour=Array(24).fill(0),this.day=Array(31).fill(0),this.month=Array(12).fill(0),this.dayOfWeek=Array(7).fill(0),this.lastDayOfMonth=!1,this.starDOM=!1,this.starDOW=!1,this.parse()}parse(){if(!(typeof this.pattern=="string"||this.pattern instanceof String))throw new TypeError("CronPattern: Pattern has to be of type string.");this.pattern.indexOf("@")>=0&&(this.pattern=this.handleNicknames(this.pattern).trim());let t=this.pattern.replace(/\s+/g," ").split(" ");if(t.length<5||t.length>6)throw new TypeError("CronPattern: invalid configuration format ('"+this.pattern+"'), exactly five or six space separated parts are required.");if(t.length===5&&t.unshift("0"),t[3].indexOf("L")>=0&&(t[3]=t[3].replace("L",""),this.lastDayOfMonth=!0),t[3]=="*"&&(this.starDOM=!0),t[4].length>=3&&(t[4]=this.replaceAlphaMonths(t[4])),t[5].length>=3&&(t[5]=this.replaceAlphaDays(t[5])),t[5]=="*"&&(this.starDOW=!0),this.pattern.indexOf("?")>=0){let e=new l(new Date,this.timezone).getDate(!0);t[0]=t[0].replace("?",e.getSeconds().toString()),t[1]=t[1].replace("?",e.getMinutes().toString()),t[2]=t[2].replace("?",e.getHours().toString()),this.starDOM||(t[3]=t[3].replace("?",e.getDate().toString())),t[4]=t[4].replace("?",(e.getMonth()+1).toString()),this.starDOW||(t[5]=t[5].replace("?",e.getDay().toString()))}this.throwAtIllegalCharacters(t),this.partToArray("second",t[0],0,1),this.partToArray("minute",t[1],0,1),this.partToArray("hour",t[2],0,1),this.partToArray("day",t[3],-1,1),this.partToArray("month",t[4],-1,1),this.partToArray("dayOfWeek",t[5],0,d),this.dayOfWeek[7]&&(this.dayOfWeek[0]=this.dayOfWeek[7])}partToArray(t,e,n,i){let s=this[t],a=t==="day"&&this.lastDayOfMonth;if(e===""&&!a)throw new TypeError("CronPattern: configuration entry "+t+" ("+e+") is empty, check for trailing spaces.");if(e==="*")return s.fill(i);let u=e.split(",");if(u.length>1)for(let o=0;o<u.length;o++)this.partToArray(t,u[o],n,i);else e.indexOf("-")!==-1&&e.indexOf("/")!==-1?this.handleRangeWithStepping(e,t,n,i):e.indexOf("-")!==-1?this.handleRange(e,t,n,i):e.indexOf("/")!==-1?this.handleStepping(e,t,n,i):e!==""&&this.handleNumber(e,t,n,i)}throwAtIllegalCharacters(t){for(let e=0;e<t.length;e++)if((e===5?/[^/*0-9,\-#L]+/:/[^/*0-9,-]+/).test(t[e]))throw new TypeError("CronPattern: configuration entry "+e+" ("+t[e]+") contains illegal characters.")}handleNumber(t,e,n,i){let s=this.extractNth(t,e),a=parseInt(s[0],10)+n;if(isNaN(a))throw new TypeError("CronPattern: "+e+" is not a number: '"+t+"'");this.setPart(e,a,s[1]||i)}setPart(t,e,n){if(!Object.prototype.hasOwnProperty.call(this,t))throw new TypeError("CronPattern: Invalid part specified: "+t);if(t==="dayOfWeek"){if(e===7&&(e=0),e<0||e>6)throw new RangeError("CronPattern: Invalid value for dayOfWeek: "+e);this.setNthWeekdayOfMonth(e,n);return}if(t==="second"||t==="minute"){if(e<0||e>=60)throw new RangeError("CronPattern: Invalid value for "+t+": "+e)}else if(t==="hour"){if(e<0||e>=24)throw new RangeError("CronPattern: Invalid value for "+t+": "+e)}else if(t==="day"){if(e<0||e>=31)throw new RangeError("CronPattern: Invalid value for "+t+": "+e)}else if(t==="month"&&(e<0||e>=12))throw new RangeError("CronPattern: Invalid value for "+t+": "+e);this[t][e]=n}handleRangeWithStepping(t,e,n,i){let s=this.extractNth(t,e),a=s[0].match(/^(\d+)-(\d+)\/(\d+)$/);if(a===null)throw new TypeError("CronPattern: Syntax error, illegal range with stepping: '"+t+"'");let[,u,o,h]=a,c=parseInt(u,10)+n,m=parseInt(o,10)+n,p=parseInt(h,10);if(isNaN(c))throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)");if(isNaN(m))throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)");if(isNaN(p))throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");if(p===0)throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");if(p>this[e].length)throw new TypeError("CronPattern: Syntax error, steps cannot be greater than maximum value of part ("+this[e].length+")");if(c>m)throw new TypeError("CronPattern: From value is larger than to value: '"+t+"'");for(let v=c;v<=m;v+=p)this.setPart(e,v,s[1]||i)}extractNth(t,e){let n=t,i;if(n.includes("#")){if(e!=="dayOfWeek")throw new Error("CronPattern: nth (#) only allowed in day-of-week field");i=n.split("#")[1],n=n.split("#")[0]}return[n,i]}handleRange(t,e,n,i){let s=this.extractNth(t,e),a=s[0].split("-");if(a.length!==2)throw new TypeError("CronPattern: Syntax error, illegal range: '"+t+"'");let u=parseInt(a[0],10)+n,o=parseInt(a[1],10)+n;if(isNaN(u))throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)");if(isNaN(o))throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)");if(u>o)throw new TypeError("CronPattern: From value is larger than to value: '"+t+"'");for(let h=u;h<=o;h++)this.setPart(e,h,s[1]||i)}handleStepping(t,e,n,i){let s=this.extractNth(t,e),a=s[0].split("/");if(a.length!==2)throw new TypeError("CronPattern: Syntax error, illegal stepping: '"+t+"'");a[0]===""&&(a[0]="*");let u=0;a[0]!=="*"&&(u=parseInt(a[0],10)+n);let o=parseInt(a[1],10);if(isNaN(o))throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");if(o===0)throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");if(o>this[e].length)throw new TypeError("CronPattern: Syntax error, max steps for part is ("+this[e].length+")");for(let h=u;h<this[e].length;h+=o)this.setPart(e,h,s[1]||i)}replaceAlphaDays(t){return t.replace(/-sun/gi,"-7").replace(/sun/gi,"0").replace(/mon/gi,"1").replace(/tue/gi,"2").replace(/wed/gi,"3").replace(/thu/gi,"4").replace(/fri/gi,"5").replace(/sat/gi,"6")}replaceAlphaMonths(t){return t.replace(/jan/gi,"1").replace(/feb/gi,"2").replace(/mar/gi,"3").replace(/apr/gi,"4").replace(/may/gi,"5").replace(/jun/gi,"6").replace(/jul/gi,"7").replace(/aug/gi,"8").replace(/sep/gi,"9").replace(/oct/gi,"10").replace(/nov/gi,"11").replace(/dec/gi,"12")}handleNicknames(t){let e=t.trim().toLowerCase();return e==="@yearly"||e==="@annually"?"0 0 1 1 *":e==="@monthly"?"0 0 1 * *":e==="@weekly"?"0 0 * * 0":e==="@daily"?"0 0 * * *":e==="@hourly"?"0 * * * *":t}setNthWeekdayOfMonth(t,e){if(typeof e!="number"&&e==="L")this.dayOfWeek[t]=this.dayOfWeek[t]|D;else if(e===d)this.dayOfWeek[t]=d;else if(e<6&&e>0)this.dayOfWeek[t]=this.dayOfWeek[t]|_[e-1];else throw new TypeError(`CronPattern: nth weekday out of range, should be 1-5 or L. Value: ${e}, Type: ${typeof e}`)}};var x=[31,28,31,30,31,30,31,31,30,31,30,31],f=[["month","year",0],["day","month",-1],["hour","day",0],["minute","hour",0],["second","minute",0]],l=class r{tz;ms;second;minute;hour;day;month;year;constructor(t,e){if(this.tz=e,t&&t instanceof Date)if(!isNaN(t))this.fromDate(t);else throw new TypeError("CronDate: Invalid date passed to CronDate constructor");else if(t===void 0)this.fromDate(new Date);else if(t&&typeof t=="string")this.fromString(t);else if(t instanceof r)this.fromCronDate(t);else throw new TypeError("CronDate: Invalid type ("+typeof t+") passed to CronDate constructor")}isNthWeekdayOfMonth(t,e,n,i){let a=new Date(Date.UTC(t,e,n)).getUTCDay(),u=0;for(let o=1;o<=n;o++)new Date(Date.UTC(t,e,o)).getUTCDay()===a&&u++;if(i&d&&_[u-1]&i)return!0;if(i&D){let o=new Date(Date.UTC(t,e+1,0)).getUTCDate();for(let h=n+1;h<=o;h++)if(new Date(Date.UTC(t,e,h)).getUTCDay()===a)return!1;return!0}return!1}fromDate(t){if(this.tz!==void 0)if(typeof this.tz=="number")this.ms=t.getUTCMilliseconds(),this.second=t.getUTCSeconds(),this.minute=t.getUTCMinutes()+this.tz,this.hour=t.getUTCHours(),this.day=t.getUTCDate(),this.month=t.getUTCMonth(),this.year=t.getUTCFullYear(),this.apply();else{let e=g(t,this.tz);this.ms=t.getMilliseconds(),this.second=e.s,this.minute=e.i,this.hour=e.h,this.day=e.d,this.month=e.m-1,this.year=e.y}else this.ms=t.getMilliseconds(),this.second=t.getSeconds(),this.minute=t.getMinutes(),this.hour=t.getHours(),this.day=t.getDate(),this.month=t.getMonth(),this.year=t.getFullYear()}fromCronDate(t){this.tz=t.tz,this.year=t.year,this.month=t.month,this.day=t.day,this.hour=t.hour,this.minute=t.minute,this.second=t.second,this.ms=t.ms}apply(){if(this.month>11||this.day>x[this.month]||this.hour>59||this.minute>59||this.second>59||this.hour<0||this.minute<0||this.second<0){let t=new Date(Date.UTC(this.year,this.month,this.day,this.hour,this.minute,this.second,this.ms));return this.ms=t.getUTCMilliseconds(),this.second=t.getUTCSeconds(),this.minute=t.getUTCMinutes(),this.hour=t.getUTCHours(),this.day=t.getUTCDate(),this.month=t.getUTCMonth(),this.year=t.getUTCFullYear(),!0}else return!1}fromString(t){if(typeof this.tz=="number"){let e=N(t);this.ms=e.getUTCMilliseconds(),this.second=e.getUTCSeconds(),this.minute=e.getUTCMinutes(),this.hour=e.getUTCHours(),this.day=e.getUTCDate(),this.month=e.getUTCMonth(),this.year=e.getUTCFullYear(),this.apply()}else return this.fromDate(N(t,this.tz))}findNext(t,e,n,i){let s=this[e],a;n.lastDayOfMonth&&(this.month!==1?a=x[this.month]:a=new Date(Date.UTC(this.year,this.month+1,0,0,0,0,0)).getUTCDate());let u=!n.starDOW&&e=="day"?new Date(Date.UTC(this.year,this.month,1,0,0,0,0)).getUTCDay():void 0;for(let o=this[e]+i;o<n[e].length;o++){let h=n[e][o];if(e==="day"&&n.lastDayOfMonth&&o-i==a&&(h=1),e==="day"&&!n.starDOW){let c=n.dayOfWeek[(u+(o-i-1))%7];if(c&&c&d)c=this.isNthWeekdayOfMonth(this.year,this.month,o-i,c)?1:0;else if(c)throw new Error(`CronDate: Invalid value for dayOfWeek encountered. ${c}`);t.legacyMode&&!n.starDOM?h=h||c:h=h&&c}if(h)return this[e]=o-i,s!==this[e]?2:1}return 3}recurse(t,e,n){let i=this.findNext(e,f[n][0],t,f[n][2]);if(i>1){let s=n+1;for(;s<f.length;)this[f[s][0]]=-f[s][2],s++;if(i===3)return this[f[n][1]]++,this[f[n][0]]=-f[n][2],this.apply(),this.recurse(t,e,0);if(this.apply())return this.recurse(t,e,n-1)}return n+=1,n>=f.length?this:this.year>=3e3?null:this.recurse(t,e,n)}increment(t,e,n){return this.second+=e.interval!==void 0&&e.interval>1&&n?e.interval:1,this.ms=0,this.apply(),this.recurse(t,e,0)}getDate(t){return t||this.tz===void 0?new Date(this.year,this.month,this.day,this.hour,this.minute,this.second,this.ms):typeof this.tz=="number"?new Date(Date.UTC(this.year,this.month,this.day,this.hour,this.minute-this.tz,this.second,this.ms)):k(w(this.year,this.month+1,this.day,this.hour,this.minute,this.second,this.tz),!1)}getTime(){return this.getDate(!1).getTime()}};function S(r){if(r===void 0&&(r={}),delete r.name,r.legacyMode=r.legacyMode===void 0?!0:r.legacyMode,r.paused=r.paused===void 0?!1:r.paused,r.maxRuns=r.maxRuns===void 0?1/0:r.maxRuns,r.catch=r.catch===void 0?!1:r.catch,r.interval=r.interval===void 0?0:parseInt(r.interval.toString(),10),r.utcOffset=r.utcOffset===void 0?void 0:parseInt(r.utcOffset.toString(),10),r.unref=r.unref===void 0?!1:r.unref,r.startAt&&(r.startAt=new l(r.startAt,r.timezone)),r.stopAt&&(r.stopAt=new l(r.stopAt,r.timezone)),r.interval!==null){if(isNaN(r.interval))throw new Error("CronOptions: Supplied value for interval is not a number");if(r.interval<0)throw new Error("CronOptions: Supplied value for interval can not be negative")}if(r.utcOffset!==void 0){if(isNaN(r.utcOffset))throw new Error("CronOptions: Invalid value passed for utcOffset, should be number representing minutes offset from UTC.");if(r.utcOffset<-870||r.utcOffset>870)throw new Error("CronOptions: utcOffset out of bounds.");if(r.utcOffset!==void 0&&r.timezone)throw new Error("CronOptions: Combining 'utcOffset' with 'timezone' is not allowed.")}if(r.unref!==!0&&r.unref!==!1)throw new Error("CronOptions: Unref should be either true, false or undefined(false).");return r}function y(r){return Object.prototype.toString.call(r)==="[object Function]"||typeof r=="function"||r instanceof Function}function E(r){return y(r)}function M(r){typeof Deno<"u"&&typeof Deno.unrefTimer<"u"?Deno.unrefTimer(r):r&&typeof r.unref<"u"&&r.unref()}var U=30*1e3,T=[],R=class{name;options;_states;fn;constructor(t,e,n){let i,s;if(y(e))s=e;else if(typeof e=="object")i=e;else if(e!==void 0)throw new Error("Cron: Invalid argument passed for optionsIn. Should be one of function, or object (options).");if(y(n))s=n;else if(typeof n=="object")i=n;else if(n!==void 0)throw new Error("Cron: Invalid argument passed for funcIn. Should be one of function, or object (options).");if(this.name=i?.name,this.options=S(i),this._states={kill:!1,blocking:!1,previousRun:void 0,currentRun:void 0,once:void 0,currentTimeout:void 0,maxRuns:i?i.maxRuns:void 0,paused:i?i.paused:!1,pattern:new C("* * * * *")},t&&(t instanceof Date||typeof t=="string"&&t.indexOf(":")>0)?this._states.once=new l(t,this.options.timezone||this.options.utcOffset):this._states.pattern=new C(t,this.options.timezone),this.name){if(T.find(u=>u.name===this.name))throw new Error("Cron: Tried to initialize new named job '"+this.name+"', but name already taken.");T.push(this)}return s!==void 0&&E(s)&&(this.fn=s,this.schedule()),this}nextRun(t){let e=this._next(t);return e?e.getDate(!1):null}nextRuns(t,e){this._states.maxRuns!==void 0&&t>this._states.maxRuns&&(t=this._states.maxRuns);let n=[],i=e||this._states.currentRun||void 0;for(;t--&&(i=this.nextRun(i));)n.push(i);return n}getPattern(){return this._states.pattern?this._states.pattern.pattern:void 0}isRunning(){let t=this.nextRun(this._states.currentRun),e=!this._states.paused,n=this.fn!==void 0,i=!this._states.kill;return e&&n&&i&&t!==null}isStopped(){return this._states.kill}isBusy(){return this._states.blocking}currentRun(){return this._states.currentRun?this._states.currentRun.getDate():null}previousRun(){return this._states.previousRun?this._states.previousRun.getDate():null}msToNext(t){let e=this._next(t);return e?t instanceof l||t instanceof Date?e.getTime()-t.getTime():e.getTime()-new l(t).getTime():null}stop(){this._states.kill=!0,this._states.currentTimeout&&clearTimeout(this._states.currentTimeout);let t=T.indexOf(this);t>=0&&T.splice(t,1)}pause(){return this._states.paused=!0,!this._states.kill}resume(){return this._states.paused=!1,!this._states.kill}schedule(t){if(t&&this.fn)throw new Error("Cron: It is not allowed to schedule two functions using the same Croner instance.");t&&(this.fn=t);let e=this.msToNext(),n=this.nextRun(this._states.currentRun);return e==null||isNaN(e)||n===null?this:(e>U&&(e=U),this._states.currentTimeout=setTimeout(()=>this._checkTrigger(n),e),this._states.currentTimeout&&this.options.unref&&M(this._states.currentTimeout),this)}async _trigger(t){if(this._states.blocking=!0,this._states.currentRun=new l(void 0,this.options.timezone||this.options.utcOffset),this.options.catch)try{this.fn!==void 0&&await this.fn(this,this.options.context)}catch(e){y(this.options.catch)&&this.options.catch(e,this)}else this.fn!==void 0&&await this.fn(this,this.options.context);this._states.previousRun=new l(t,this.options.timezone||this.options.utcOffset),this._states.blocking=!1}async trigger(){await this._trigger()}runsLeft(){return this._states.maxRuns}_checkTrigger(t){let e=new Date,n=!this._states.paused&&e.getTime()>=t.getTime(),i=this._states.blocking&&this.options.protect;n&&!i?(this._states.maxRuns!==void 0&&this._states.maxRuns--,this._trigger()):n&&i&&y(this.options.protect)&&setTimeout(()=>this.options.protect(this),0),this.schedule()}_next(t){let e=!!(t||this._states.currentRun),n=!1;!t&&this.options.startAt&&this.options.interval&&([t,e]=this._calculatePreviousRun(t,e),n=!t),t=new l(t,this.options.timezone||this.options.utcOffset),this.options.startAt&&t&&t.getTime()<this.options.startAt.getTime()&&(t=this.options.startAt);let i=this._states.once||new l(t,this.options.timezone||this.options.utcOffset);return!n&&i!==this._states.once&&(i=i.increment(this._states.pattern,this.options,e)),this._states.once&&this._states.once.getTime()<=t.getTime()||i===null||this._states.maxRuns!==void 0&&this._states.maxRuns<=0||this._states.kill||this.options.stopAt&&i.getTime()>=this.options.stopAt.getTime()?null:i}_calculatePreviousRun(t,e){let n=new l(void 0,this.options.timezone||this.options.utcOffset),i=t;if(this.options.startAt.getTime()<=n.getTime()){i=this.options.startAt;let s=i.getTime()+this.options.interval*1e3;for(;s<=n.getTime();)i=new l(i,this.options.timezone||this.options.utcOffset).increment(this._states.pattern,this.options,!0),s=i.getTime()+this.options.interval*1e3;e=!0}return i===null&&(i=void 0),[i,e]}};0&&(module.exports={Cron,CronDate,CronPattern,scheduledJobs});
|
|
1
|
+
var D=Object.defineProperty;var M=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var W=Object.prototype.hasOwnProperty;var I=(r,e)=>{for(var t in e)D(r,t,{get:e[t],enumerable:!0})},z=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of A(e))!W.call(r,i)&&i!==t&&D(r,i,{get:()=>e[i],enumerable:!(n=M(e,i))||n.enumerable});return r};var F=r=>z(D({},"__esModule",{value:!0}),r);var j={};I(j,{Cron:()=>N,CronDate:()=>f,CronPattern:()=>p,scheduledJobs:()=>T});module.exports=F(j);function C(r){return Date.UTC(r.y,r.m-1,r.d,r.h,r.i,r.s)}function v(r,e){return r.y===e.y&&r.m===e.m&&r.d===e.d&&r.h===e.h&&r.i===e.i&&r.s===e.s}function L(r,e){let t=new Date(Date.parse(r));if(isNaN(t))throw new Error("Invalid ISO8601 passed to timezone parser.");let n=r.substring(9);return n.includes("Z")||n.includes("+")||n.includes("-")?b(t.getUTCFullYear(),t.getUTCMonth()+1,t.getUTCDate(),t.getUTCHours(),t.getUTCMinutes(),t.getUTCSeconds(),"Etc/UTC"):b(t.getFullYear(),t.getMonth()+1,t.getDate(),t.getHours(),t.getMinutes(),t.getSeconds(),e)}function O(r,e,t){return k(L(r,e),t)}function k(r,e){let t=new Date(C(r)),n=g(t,r.tz),i=C(r),s=C(n),a=i-s,h=new Date(t.getTime()+a),o=g(h,r.tz);if(v(o,r)){let m=new Date(h.getTime()-36e5),d=g(m,r.tz);return v(d,r)?m:h}let u=new Date(h.getTime()+C(r)-C(o)),l=g(u,r.tz);if(v(l,r))return u;if(e)throw new Error("Invalid date passed to fromTZ()");return h.getTime()>u.getTime()?h:u}function g(r,e){let n=new Intl.DateTimeFormat("en-US",{timeZone:e,year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"numeric",second:"numeric",hour12:!1}).formatToParts(r),i={year:0,month:0,day:0,hour:0,minute:0,second:0};for(let s of n)(s.type==="year"||s.type==="month"||s.type==="day"||s.type==="hour"||s.type==="minute"||s.type==="second")&&(i[s.type]=parseInt(s.value,10));return i.hour===24&&(i.hour=0),{y:i.year,m:i.month,d:i.day,h:i.hour,i:i.minute,s:i.second,tz:e}}function b(r,e,t,n,i,s,a){return{y:r,m:e,d:t,h:n,i,s,tz:a}}var P=[1,2,4,8,16],p=class{pattern;timezone;second;minute;hour;day;month;dayOfWeek;year;lastDayOfMonth;nearestWeekdays;starDOM;starDOW;starYear;useAndLogic;constructor(e,t){this.pattern=e,this.timezone=t,this.second=Array(60).fill(0),this.minute=Array(60).fill(0),this.hour=Array(24).fill(0),this.day=Array(31).fill(0),this.month=Array(12).fill(0),this.dayOfWeek=Array(7).fill(0),this.year=Array(1e4).fill(0),this.lastDayOfMonth=!1,this.nearestWeekdays=Array(31).fill(0),this.starDOM=!1,this.starDOW=!1,this.starYear=!1,this.useAndLogic=!1,this.parse()}parse(){if(!(typeof this.pattern=="string"||this.pattern instanceof String))throw new TypeError("CronPattern: Pattern has to be of type string.");this.pattern.indexOf("@")>=0&&(this.pattern=this.handleNicknames(this.pattern).trim());let e=this.pattern.match(/\S+/g)||[""];if(e.length<5||e.length>7)throw new TypeError("CronPattern: invalid configuration format ('"+this.pattern+"'), exactly five, six, or seven space separated parts are required.");if(e.length===5&&e.unshift("0"),e.length===6&&e.push("*"),e[3].indexOf("L")>=0&&(e[3]=e[3].replace("L",""),this.lastDayOfMonth=!0),e[3]=="*"&&(this.starDOM=!0),e[6]=="*"&&(this.starYear=!0),e[4].length>=3&&(e[4]=this.replaceAlphaMonths(e[4])),e[5].length>=3&&(e[5]=this.replaceAlphaDays(e[5])),e[5].startsWith("+")&&(this.useAndLogic=!0,e[5]=e[5].substring(1),e[5]===""))throw new TypeError("CronPattern: Day-of-week field cannot be empty after '+' modifier.");e[5]=="*"&&(this.starDOW=!0),this.pattern.indexOf("?")>=0&&(e[0]=e[0].replace(/\?/g,"*"),e[1]=e[1].replace(/\?/g,"*"),e[2]=e[2].replace(/\?/g,"*"),e[3]=e[3].replace(/\?/g,"*"),e[4]=e[4].replace(/\?/g,"*"),e[5]=e[5].replace(/\?/g,"*"),e[6]&&(e[6]=e[6].replace(/\?/g,"*"))),this.throwAtIllegalCharacters(e),this.partToArray("second",e[0],0,1),this.partToArray("minute",e[1],0,1),this.partToArray("hour",e[2],0,1),this.partToArray("day",e[3],-1,1),this.partToArray("month",e[4],-1,1),this.partToArray("dayOfWeek",e[5],0,63),this.partToArray("year",e[6],0,1),this.dayOfWeek[7]&&(this.dayOfWeek[0]=this.dayOfWeek[7])}partToArray(e,t,n,i){let s=this[e],a=e==="day"&&this.lastDayOfMonth;if(t===""&&!a)throw new TypeError("CronPattern: configuration entry "+e+" ("+t+") is empty, check for trailing spaces.");if(t==="*")return s.fill(i);let h=t.split(",");if(h.length>1)for(let o=0;o<h.length;o++)this.partToArray(e,h[o],n,i);else t.indexOf("-")!==-1&&t.indexOf("/")!==-1?this.handleRangeWithStepping(t,e,n,i):t.indexOf("-")!==-1?this.handleRange(t,e,n,i):t.indexOf("/")!==-1?this.handleStepping(t,e,n,i):t!==""&&this.handleNumber(t,e,n,i)}throwAtIllegalCharacters(e){for(let t=0;t<e.length;t++)if((t===3?/[^/*0-9,-WL]+/:t===5?/[^/*0-9,\-#L]+/:/[^/*0-9,-]+/).test(e[t]))throw new TypeError("CronPattern: configuration entry "+t+" ("+e[t]+") contains illegal characters.")}handleNumber(e,t,n,i){let s=this.extractNth(e,t),a=e.toUpperCase().includes("W");if(t!=="day"&&a)throw new TypeError("CronPattern: Nearest weekday modifier (W) only allowed in day-of-month.");a&&(t="nearestWeekdays");let h=parseInt(s[0],10)+n;if(isNaN(h))throw new TypeError("CronPattern: "+t+" is not a number: '"+e+"'");this.setPart(t,h,s[1]||i)}setPart(e,t,n){if(!Object.prototype.hasOwnProperty.call(this,e))throw new TypeError("CronPattern: Invalid part specified: "+e);if(e==="dayOfWeek"){if(t===7&&(t=0),t<0||t>6)throw new RangeError("CronPattern: Invalid value for dayOfWeek: "+t);this.setNthWeekdayOfMonth(t,n);return}if(e==="second"||e==="minute"){if(t<0||t>=60)throw new RangeError("CronPattern: Invalid value for "+e+": "+t)}else if(e==="hour"){if(t<0||t>=24)throw new RangeError("CronPattern: Invalid value for "+e+": "+t)}else if(e==="day"||e==="nearestWeekdays"){if(t<0||t>=31)throw new RangeError("CronPattern: Invalid value for "+e+": "+t)}else if(e==="month"){if(t<0||t>=12)throw new RangeError("CronPattern: Invalid value for "+e+": "+t)}else if(e==="year"&&(t<1||t>=1e4))throw new RangeError("CronPattern: Invalid value for "+e+": "+t+" (supported range: 1-9999)");this[e][t]=n}handleRangeWithStepping(e,t,n,i){if(e.toUpperCase().includes("W"))throw new TypeError("CronPattern: Syntax error, W is not allowed in ranges with stepping.");let s=this.extractNth(e,t),a=s[0].match(/^(\d+)-(\d+)\/(\d+)$/);if(a===null)throw new TypeError("CronPattern: Syntax error, illegal range with stepping: '"+e+"'");let[,h,o,u]=a,l=parseInt(h,10)+n,m=parseInt(o,10)+n,d=parseInt(u,10);if(isNaN(l))throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)");if(isNaN(m))throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)");if(isNaN(d))throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");if(d===0)throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");if(d>this[t].length)throw new TypeError("CronPattern: Syntax error, steps cannot be greater than maximum value of part ("+this[t].length+")");if(l>m)throw new TypeError("CronPattern: From value is larger than to value: '"+e+"'");for(let w=l;w<=m;w+=d)this.setPart(t,w,s[1]||i)}extractNth(e,t){let n=e,i;if(n.includes("#")){if(t!=="dayOfWeek")throw new Error("CronPattern: nth (#) only allowed in day-of-week field");i=n.split("#")[1],n=n.split("#")[0]}return[n,i]}handleRange(e,t,n,i){if(e.toUpperCase().includes("W"))throw new TypeError("CronPattern: Syntax error, W is not allowed in a range.");let s=this.extractNth(e,t),a=s[0].split("-");if(a.length!==2)throw new TypeError("CronPattern: Syntax error, illegal range: '"+e+"'");let h=parseInt(a[0],10)+n,o=parseInt(a[1],10)+n;if(isNaN(h))throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)");if(isNaN(o))throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)");if(h>o)throw new TypeError("CronPattern: From value is larger than to value: '"+e+"'");for(let u=h;u<=o;u++)this.setPart(t,u,s[1]||i)}handleStepping(e,t,n,i){if(e.toUpperCase().includes("W"))throw new TypeError("CronPattern: Syntax error, W is not allowed in parts with stepping.");let s=this.extractNth(e,t),a=s[0].split("/");if(a.length!==2)throw new TypeError("CronPattern: Syntax error, illegal stepping: '"+e+"'");a[0]===""&&(a[0]="*");let h=0;a[0]!=="*"&&(h=parseInt(a[0],10)+n);let o=parseInt(a[1],10);if(isNaN(o))throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");if(o===0)throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");if(o>this[t].length)throw new TypeError("CronPattern: Syntax error, max steps for part is ("+this[t].length+")");for(let u=h;u<this[t].length;u+=o)this.setPart(t,u,s[1]||i)}replaceAlphaDays(e){return e.replace(/-sun/gi,"-7").replace(/sun/gi,"0").replace(/mon/gi,"1").replace(/tue/gi,"2").replace(/wed/gi,"3").replace(/thu/gi,"4").replace(/fri/gi,"5").replace(/sat/gi,"6")}replaceAlphaMonths(e){return e.replace(/jan/gi,"1").replace(/feb/gi,"2").replace(/mar/gi,"3").replace(/apr/gi,"4").replace(/may/gi,"5").replace(/jun/gi,"6").replace(/jul/gi,"7").replace(/aug/gi,"8").replace(/sep/gi,"9").replace(/oct/gi,"10").replace(/nov/gi,"11").replace(/dec/gi,"12")}handleNicknames(e){let t=e.trim().toLowerCase();if(t==="@yearly"||t==="@annually")return"0 0 1 1 *";if(t==="@monthly")return"0 0 1 * *";if(t==="@weekly")return"0 0 * * 0";if(t==="@daily"||t==="@midnight")return"0 0 * * *";if(t==="@hourly")return"0 * * * *";if(t==="@reboot")throw new TypeError("CronPattern: @reboot is not supported in this environment. This is an event-based trigger that requires system startup detection.");return e}setNthWeekdayOfMonth(e,t){if(typeof t!="number"&&t==="L")this.dayOfWeek[e]=this.dayOfWeek[e]|32;else if(t===63)this.dayOfWeek[e]=63;else if(t<6&&t>0)this.dayOfWeek[e]=this.dayOfWeek[e]|P[t-1];else throw new TypeError(`CronPattern: nth weekday out of range, should be 1-5 or L. Value: ${t}, Type: ${typeof t}`)}};var R=[31,28,31,30,31,30,31,31,30,31,30,31],c=[["month","year",0],["day","month",-1],["hour","day",0],["minute","hour",0],["second","minute",0]],f=class r{tz;ms;second;minute;hour;day;month;year;constructor(e,t){if(this.tz=t,e&&e instanceof Date)if(!isNaN(e))this.fromDate(e);else throw new TypeError("CronDate: Invalid date passed to CronDate constructor");else if(e===void 0)this.fromDate(new Date);else if(e&&typeof e=="string")this.fromString(e);else if(e instanceof r)this.fromCronDate(e);else throw new TypeError("CronDate: Invalid type ("+typeof e+") passed to CronDate constructor")}getNearestWeekday(e,t,n){let s=new Date(Date.UTC(e,t,n)).getUTCDay();if(s===0){let a=new Date(Date.UTC(e,t+1,0)).getUTCDate();return n===a?n-2:n+1}return s===6?n===1?n+2:n-1:n}isNthWeekdayOfMonth(e,t,n,i){let a=new Date(Date.UTC(e,t,n)).getUTCDay(),h=0;for(let o=1;o<=n;o++)new Date(Date.UTC(e,t,o)).getUTCDay()===a&&h++;if(i&63&&P[h-1]&i)return!0;if(i&32){let o=new Date(Date.UTC(e,t+1,0)).getUTCDate();for(let u=n+1;u<=o;u++)if(new Date(Date.UTC(e,t,u)).getUTCDay()===a)return!1;return!0}return!1}fromDate(e){if(this.tz!==void 0)if(typeof this.tz=="number")this.ms=e.getUTCMilliseconds(),this.second=e.getUTCSeconds(),this.minute=e.getUTCMinutes()+this.tz,this.hour=e.getUTCHours(),this.day=e.getUTCDate(),this.month=e.getUTCMonth(),this.year=e.getUTCFullYear(),this.apply();else{let t=g(e,this.tz);this.ms=e.getMilliseconds(),this.second=t.s,this.minute=t.i,this.hour=t.h,this.day=t.d,this.month=t.m-1,this.year=t.y}else this.ms=e.getMilliseconds(),this.second=e.getSeconds(),this.minute=e.getMinutes(),this.hour=e.getHours(),this.day=e.getDate(),this.month=e.getMonth(),this.year=e.getFullYear()}fromCronDate(e){this.tz=e.tz,this.year=e.year,this.month=e.month,this.day=e.day,this.hour=e.hour,this.minute=e.minute,this.second=e.second,this.ms=e.ms}apply(){if(this.month>11||this.day>R[this.month]||this.hour>59||this.minute>59||this.second>59||this.hour<0||this.minute<0||this.second<0){let e=new Date(Date.UTC(this.year,this.month,this.day,this.hour,this.minute,this.second,this.ms));return this.ms=e.getUTCMilliseconds(),this.second=e.getUTCSeconds(),this.minute=e.getUTCMinutes(),this.hour=e.getUTCHours(),this.day=e.getUTCDate(),this.month=e.getUTCMonth(),this.year=e.getUTCFullYear(),!0}else return!1}fromString(e){if(typeof this.tz=="number"){let t=O(e);this.ms=t.getUTCMilliseconds(),this.second=t.getUTCSeconds(),this.minute=t.getUTCMinutes(),this.hour=t.getUTCHours(),this.day=t.getUTCDate(),this.month=t.getUTCMonth(),this.year=t.getUTCFullYear(),this.apply()}else return this.fromDate(O(e,this.tz))}findNext(e,t,n,i){let s=this[t],a;n.lastDayOfMonth&&(this.month!==1?a=R[this.month]:a=new Date(Date.UTC(this.year,this.month+1,0,0,0,0,0)).getUTCDate());let h=!n.starDOW&&t=="day"?new Date(Date.UTC(this.year,this.month,1,0,0,0,0)).getUTCDay():void 0;for(let o=this[t]+i;o<n[t].length;o++){let u=n[t][o];if(t==="day"&&!u){for(let l=0;l<n.nearestWeekdays.length;l++)if(n.nearestWeekdays[l]&&this.getNearestWeekday(this.year,this.month,l-i)===o-i){u=1;break}}if(t==="day"&&n.lastDayOfMonth&&o-i==a&&(u=1),t==="day"&&!n.starDOW){let l=n.dayOfWeek[(h+(o-i-1))%7];if(l&&l&63)l=this.isNthWeekdayOfMonth(this.year,this.month,o-i,l)?1:0;else if(l)throw new Error(`CronDate: Invalid value for dayOfWeek encountered. ${l}`);n.useAndLogic?u=u&&l:e.legacyMode&&!n.starDOM?u=u||l:u=u&&l}if(u)return this[t]=o-i,s!==this[t]?2:1}return 3}recurse(e,t,n){if(n===0&&!e.starYear){if(this.year>=0&&this.year<e.year.length&&e.year[this.year]===0){let s=-1;for(let a=this.year+1;a<e.year.length&&a<1e4;a++)if(e.year[a]===1){s=a;break}if(s===-1)return null;this.year=s,this.month=0,this.day=1,this.hour=0,this.minute=0,this.second=0,this.ms=0}if(this.year>=1e4)return null}let i=this.findNext(t,c[n][0],e,c[n][2]);if(i>1){let s=n+1;for(;s<c.length;)this[c[s][0]]=-c[s][2],s++;if(i===3){if(this[c[n][1]]++,this[c[n][0]]=-c[n][2],this.apply(),n===0&&!e.starYear){for(;this.year>=0&&this.year<e.year.length&&e.year[this.year]===0&&this.year<1e4;)this.year++;if(this.year>=1e4||this.year>=e.year.length)return null}return this.recurse(e,t,0)}else if(this.apply())return this.recurse(e,t,n-1)}return n+=1,n>=c.length?this:(e.starYear?this.year>=3e3:this.year>=1e4)?null:this.recurse(e,t,n)}increment(e,t,n){return this.second+=t.interval!==void 0&&t.interval>1&&n?t.interval:1,this.ms=0,this.apply(),this.recurse(e,t,0)}getDate(e){return e||this.tz===void 0?new Date(this.year,this.month,this.day,this.hour,this.minute,this.second,this.ms):typeof this.tz=="number"?new Date(Date.UTC(this.year,this.month,this.day,this.hour,this.minute-this.tz,this.second,this.ms)):k(b(this.year,this.month+1,this.day,this.hour,this.minute,this.second,this.tz),!1)}getTime(){return this.getDate(!1).getTime()}};function _(r){if(r===void 0&&(r={}),delete r.name,r.legacyMode=r.legacyMode===void 0?!0:r.legacyMode,r.paused=r.paused===void 0?!1:r.paused,r.maxRuns=r.maxRuns===void 0?1/0:r.maxRuns,r.catch=r.catch===void 0?!1:r.catch,r.interval=r.interval===void 0?0:parseInt(r.interval.toString(),10),r.utcOffset=r.utcOffset===void 0?void 0:parseInt(r.utcOffset.toString(),10),r.unref=r.unref===void 0?!1:r.unref,r.startAt&&(r.startAt=new f(r.startAt,r.timezone)),r.stopAt&&(r.stopAt=new f(r.stopAt,r.timezone)),r.interval!==null){if(isNaN(r.interval))throw new Error("CronOptions: Supplied value for interval is not a number");if(r.interval<0)throw new Error("CronOptions: Supplied value for interval can not be negative")}if(r.utcOffset!==void 0){if(isNaN(r.utcOffset))throw new Error("CronOptions: Invalid value passed for utcOffset, should be number representing minutes offset from UTC.");if(r.utcOffset<-870||r.utcOffset>870)throw new Error("CronOptions: utcOffset out of bounds.");if(r.utcOffset!==void 0&&r.timezone)throw new Error("CronOptions: Combining 'utcOffset' with 'timezone' is not allowed.")}if(r.unref!==!0&&r.unref!==!1)throw new Error("CronOptions: Unref should be either true, false or undefined(false).");return r}function y(r){return Object.prototype.toString.call(r)==="[object Function]"||typeof r=="function"||r instanceof Function}function U(r){return y(r)}function x(r){typeof Deno<"u"&&typeof Deno.unrefTimer<"u"?Deno.unrefTimer(r):r&&typeof r.unref<"u"&&r.unref()}var S=30*1e3,T=[],N=class{name;options;_states;fn;constructor(e,t,n){let i,s;if(y(t))s=t;else if(typeof t=="object")i=t;else if(t!==void 0)throw new Error("Cron: Invalid argument passed for optionsIn. Should be one of function, or object (options).");if(y(n))s=n;else if(typeof n=="object")i=n;else if(n!==void 0)throw new Error("Cron: Invalid argument passed for funcIn. Should be one of function, or object (options).");if(this.name=i?.name,this.options=_(i),this._states={kill:!1,blocking:!1,previousRun:void 0,currentRun:void 0,once:void 0,currentTimeout:void 0,maxRuns:i?i.maxRuns:void 0,paused:i?i.paused:!1,pattern:new p("* * * * *")},e&&(e instanceof Date||typeof e=="string"&&e.indexOf(":")>0)?this._states.once=new f(e,this.options.timezone||this.options.utcOffset):this._states.pattern=new p(e,this.options.timezone),this.name){if(T.find(h=>h.name===this.name))throw new Error("Cron: Tried to initialize new named job '"+this.name+"', but name already taken.");T.push(this)}return s!==void 0&&U(s)&&(this.fn=s,this.schedule()),this}nextRun(e){let t=this._next(e);return t?t.getDate(!1):null}nextRuns(e,t){this._states.maxRuns!==void 0&&e>this._states.maxRuns&&(e=this._states.maxRuns);let n=[],i=t||this._states.currentRun||void 0;for(;e--&&(i=this.nextRun(i));)n.push(i);return n}getPattern(){return this._states.pattern?this._states.pattern.pattern:void 0}isRunning(){let e=this.nextRun(this._states.currentRun),t=!this._states.paused,n=this.fn!==void 0,i=!this._states.kill;return t&&n&&i&&e!==null}isStopped(){return this._states.kill}isBusy(){return this._states.blocking}currentRun(){return this._states.currentRun?this._states.currentRun.getDate():null}previousRun(){return this._states.previousRun?this._states.previousRun.getDate():null}msToNext(e){let t=this._next(e);return t?e instanceof f||e instanceof Date?t.getTime()-e.getTime():t.getTime()-new f(e).getTime():null}stop(){this._states.kill=!0,this._states.currentTimeout&&clearTimeout(this._states.currentTimeout);let e=T.indexOf(this);e>=0&&T.splice(e,1)}pause(){return this._states.paused=!0,!this._states.kill}resume(){return this._states.paused=!1,!this._states.kill}schedule(e){if(e&&this.fn)throw new Error("Cron: It is not allowed to schedule two functions using the same Croner instance.");e&&(this.fn=e);let t=this.msToNext(),n=this.nextRun(this._states.currentRun);return t==null||isNaN(t)||n===null?this:(t>S&&(t=S),this._states.currentTimeout=setTimeout(()=>this._checkTrigger(n),t),this._states.currentTimeout&&this.options.unref&&x(this._states.currentTimeout),this)}async _trigger(e){if(this._states.blocking=!0,this._states.currentRun=new f(void 0,this.options.timezone||this.options.utcOffset),this.options.catch)try{this.fn!==void 0&&await this.fn(this,this.options.context)}catch(t){y(this.options.catch)&&this.options.catch(t,this)}else this.fn!==void 0&&await this.fn(this,this.options.context);this._states.previousRun=new f(e,this.options.timezone||this.options.utcOffset),this._states.blocking=!1}async trigger(){await this._trigger()}runsLeft(){return this._states.maxRuns}_checkTrigger(e){let t=new Date,n=!this._states.paused&&t.getTime()>=e.getTime(),i=this._states.blocking&&this.options.protect;n&&!i?(this._states.maxRuns!==void 0&&this._states.maxRuns--,this._trigger()):n&&i&&y(this.options.protect)&&setTimeout(()=>this.options.protect(this),0),this.schedule()}_next(e){let t=!!(e||this._states.currentRun),n=!1;!e&&this.options.startAt&&this.options.interval&&([e,t]=this._calculatePreviousRun(e,t),n=!e),e=new f(e,this.options.timezone||this.options.utcOffset),this.options.startAt&&e&&e.getTime()<this.options.startAt.getTime()&&(e=this.options.startAt);let i=this._states.once||new f(e,this.options.timezone||this.options.utcOffset);return!n&&i!==this._states.once&&(i=i.increment(this._states.pattern,this.options,t)),this._states.once&&this._states.once.getTime()<=e.getTime()||i===null||this._states.maxRuns!==void 0&&this._states.maxRuns<=0||this._states.kill||this.options.stopAt&&i.getTime()>=this.options.stopAt.getTime()?null:i}_calculatePreviousRun(e,t){let n=new f(void 0,this.options.timezone||this.options.utcOffset),i=e;if(this.options.startAt.getTime()<=n.getTime()){i=this.options.startAt;let s=i.getTime()+this.options.interval*1e3;for(;s<=n.getTime();)i=new f(i,this.options.timezone||this.options.utcOffset).increment(this._states.pattern,this.options,!0),s=i.getTime()+this.options.interval*1e3;t=!0}return i===null&&(i=void 0),[i,t]}};0&&(module.exports={Cron,CronDate,CronPattern,scheduledJobs});
|
package/dist/croner.d.cts
CHANGED
|
@@ -4,7 +4,7 @@ import { type CronOptions } from "./options.ts";
|
|
|
4
4
|
/**
|
|
5
5
|
* An array containing all named cron jobs.
|
|
6
6
|
*/
|
|
7
|
-
declare const scheduledJobs: Cron[];
|
|
7
|
+
declare const scheduledJobs: Cron<any>[];
|
|
8
8
|
/**
|
|
9
9
|
* Callback function type
|
|
10
10
|
*
|
|
@@ -13,7 +13,7 @@ declare const scheduledJobs: Cron[];
|
|
|
13
13
|
*
|
|
14
14
|
* @returns void or Promise<void> for async callbacks
|
|
15
15
|
*/
|
|
16
|
-
export type CronCallback = (self: InstanceType<typeof Cron
|
|
16
|
+
export type CronCallback<T = undefined> = (self: InstanceType<typeof Cron<T>>, context: T) => void | Promise<void>;
|
|
17
17
|
/**
|
|
18
18
|
* Cron entrypoint
|
|
19
19
|
*
|
|
@@ -22,19 +22,19 @@ export type CronCallback = (self: InstanceType<typeof Cron>, context: unknown) =
|
|
|
22
22
|
* @param [fnOrOptions1] - Options or function to be run each iteration of pattern
|
|
23
23
|
* @param [fnOrOptions2] - Options or function to be run each iteration of pattern
|
|
24
24
|
*/
|
|
25
|
-
declare class Cron {
|
|
25
|
+
declare class Cron<T = undefined> {
|
|
26
26
|
name: string | undefined;
|
|
27
|
-
options: CronOptions
|
|
27
|
+
options: CronOptions<T>;
|
|
28
28
|
private _states;
|
|
29
29
|
private fn?;
|
|
30
|
-
constructor(pattern: string | Date, fnOrOptions1?: CronOptions | CronCallback
|
|
30
|
+
constructor(pattern: string | Date, fnOrOptions1?: CronOptions<T> | CronCallback<T>, fnOrOptions2?: CronOptions<T> | CronCallback<T>);
|
|
31
31
|
/**
|
|
32
32
|
* Find next runtime, based on supplied date. Strips milliseconds.
|
|
33
33
|
*
|
|
34
34
|
* @param prev - Optional. Date to start from. Can be a CronDate, Date object, or a string representing a date.
|
|
35
35
|
* @returns The next run time as a Date object, or null if there is no next run.
|
|
36
36
|
*/
|
|
37
|
-
nextRun(prev?: CronDate | Date | string | null): Date | null;
|
|
37
|
+
nextRun(prev?: CronDate<T> | Date | string | null): Date | null;
|
|
38
38
|
/**
|
|
39
39
|
* Find next n runs, based on supplied date. Strips milliseconds.
|
|
40
40
|
*
|
|
@@ -84,7 +84,7 @@ declare class Cron {
|
|
|
84
84
|
*
|
|
85
85
|
* @param prev Starting date, defaults to now - minimum interval
|
|
86
86
|
*/
|
|
87
|
-
msToNext(prev?: CronDate | Date | string): number | null;
|
|
87
|
+
msToNext(prev?: CronDate<T> | Date | string): number | null;
|
|
88
88
|
/**
|
|
89
89
|
* Stop execution
|
|
90
90
|
*
|
|
@@ -109,7 +109,7 @@ declare class Cron {
|
|
|
109
109
|
*
|
|
110
110
|
* @param func - Function to be run each iteration of pattern
|
|
111
111
|
*/
|
|
112
|
-
schedule(func?: CronCallback): Cron
|
|
112
|
+
schedule(func?: CronCallback<T>): Cron<T>;
|
|
113
113
|
/**
|
|
114
114
|
* Internal function to trigger a run, used by both scheduled and manual trigger
|
|
115
115
|
*/
|
package/dist/croner.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { type CronOptions } from "./options.ts";
|
|
|
4
4
|
/**
|
|
5
5
|
* An array containing all named cron jobs.
|
|
6
6
|
*/
|
|
7
|
-
declare const scheduledJobs: Cron[];
|
|
7
|
+
declare const scheduledJobs: Cron<any>[];
|
|
8
8
|
/**
|
|
9
9
|
* Callback function type
|
|
10
10
|
*
|
|
@@ -13,7 +13,7 @@ declare const scheduledJobs: Cron[];
|
|
|
13
13
|
*
|
|
14
14
|
* @returns void or Promise<void> for async callbacks
|
|
15
15
|
*/
|
|
16
|
-
export type CronCallback = (self: InstanceType<typeof Cron
|
|
16
|
+
export type CronCallback<T = undefined> = (self: InstanceType<typeof Cron<T>>, context: T) => void | Promise<void>;
|
|
17
17
|
/**
|
|
18
18
|
* Cron entrypoint
|
|
19
19
|
*
|
|
@@ -22,19 +22,19 @@ export type CronCallback = (self: InstanceType<typeof Cron>, context: unknown) =
|
|
|
22
22
|
* @param [fnOrOptions1] - Options or function to be run each iteration of pattern
|
|
23
23
|
* @param [fnOrOptions2] - Options or function to be run each iteration of pattern
|
|
24
24
|
*/
|
|
25
|
-
declare class Cron {
|
|
25
|
+
declare class Cron<T = undefined> {
|
|
26
26
|
name: string | undefined;
|
|
27
|
-
options: CronOptions
|
|
27
|
+
options: CronOptions<T>;
|
|
28
28
|
private _states;
|
|
29
29
|
private fn?;
|
|
30
|
-
constructor(pattern: string | Date, fnOrOptions1?: CronOptions | CronCallback
|
|
30
|
+
constructor(pattern: string | Date, fnOrOptions1?: CronOptions<T> | CronCallback<T>, fnOrOptions2?: CronOptions<T> | CronCallback<T>);
|
|
31
31
|
/**
|
|
32
32
|
* Find next runtime, based on supplied date. Strips milliseconds.
|
|
33
33
|
*
|
|
34
34
|
* @param prev - Optional. Date to start from. Can be a CronDate, Date object, or a string representing a date.
|
|
35
35
|
* @returns The next run time as a Date object, or null if there is no next run.
|
|
36
36
|
*/
|
|
37
|
-
nextRun(prev?: CronDate | Date | string | null): Date | null;
|
|
37
|
+
nextRun(prev?: CronDate<T> | Date | string | null): Date | null;
|
|
38
38
|
/**
|
|
39
39
|
* Find next n runs, based on supplied date. Strips milliseconds.
|
|
40
40
|
*
|
|
@@ -84,7 +84,7 @@ declare class Cron {
|
|
|
84
84
|
*
|
|
85
85
|
* @param prev Starting date, defaults to now - minimum interval
|
|
86
86
|
*/
|
|
87
|
-
msToNext(prev?: CronDate | Date | string): number | null;
|
|
87
|
+
msToNext(prev?: CronDate<T> | Date | string): number | null;
|
|
88
88
|
/**
|
|
89
89
|
* Stop execution
|
|
90
90
|
*
|
|
@@ -109,7 +109,7 @@ declare class Cron {
|
|
|
109
109
|
*
|
|
110
110
|
* @param func - Function to be run each iteration of pattern
|
|
111
111
|
*/
|
|
112
|
-
schedule(func?: CronCallback): Cron
|
|
112
|
+
schedule(func?: CronCallback<T>): Cron<T>;
|
|
113
113
|
/**
|
|
114
114
|
* Internal function to trigger a run, used by both scheduled and manual trigger
|
|
115
115
|
*/
|
package/dist/croner.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function y(r){return Date.UTC(r.y,r.m-1,r.d,r.h,r.i,r.s)}function O(r,t){return r.y===t.y&&r.m===t.m&&r.d===t.d&&r.h===t.h&&r.i===t.i&&r.s===t.s}function U(r,t){let e=new Date(Date.parse(r));if(isNaN(e))throw new Error("Invalid ISO8601 passed to timezone parser.");let n=r.substring(9);return n.includes("Z")||n.includes("+")||n.includes("-")?T(e.getUTCFullYear(),e.getUTCMonth()+1,e.getUTCDate(),e.getUTCHours(),e.getUTCMinutes(),e.getUTCSeconds(),"Etc/UTC"):T(e.getFullYear(),e.getMonth()+1,e.getDate(),e.getHours(),e.getMinutes(),e.getSeconds(),t)}function P(r,t,e){return N(U(r,t),e)}function N(r,t){let e=new Date(y(r)),n=g(e,r.tz),i=y(r),s=y(n),a=i-s,u=new Date(e.getTime()+a),o=g(u,r.tz);if(O(o,r)){let m=new Date(u.getTime()-36e5),p=g(m,r.tz);return O(p,r)?m:u}let h=new Date(u.getTime()+y(r)-y(o)),c=g(h,r.tz);if(O(c,r))return h;if(t)throw new Error("Invalid date passed to fromTZ()");return u.getTime()>h.getTime()?u:h}function g(r,t){let n=new Intl.DateTimeFormat("en-US",{timeZone:t,year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"numeric",second:"numeric",hour12:!1}).formatToParts(r),i={year:0,month:0,day:0,hour:0,minute:0,second:0};for(let s of n)(s.type==="year"||s.type==="month"||s.type==="day"||s.type==="hour"||s.type==="minute"||s.type==="second")&&(i[s.type]=parseInt(s.value,10));return i.hour===24&&(i.hour=0),{y:i.year,m:i.month,d:i.day,h:i.hour,i:i.minute,s:i.second,tz:t}}function T(r,t,e,n,i,s,a){return{y:r,m:t,d:e,h:n,i,s,tz:a}}var w=32,d=31|w,k=[1,2,4,8,16],b=class{pattern;timezone;second;minute;hour;day;month;dayOfWeek;lastDayOfMonth;starDOM;starDOW;constructor(t,e){this.pattern=t,this.timezone=e,this.second=Array(60).fill(0),this.minute=Array(60).fill(0),this.hour=Array(24).fill(0),this.day=Array(31).fill(0),this.month=Array(12).fill(0),this.dayOfWeek=Array(7).fill(0),this.lastDayOfMonth=!1,this.starDOM=!1,this.starDOW=!1,this.parse()}parse(){if(!(typeof this.pattern=="string"||this.pattern instanceof String))throw new TypeError("CronPattern: Pattern has to be of type string.");this.pattern.indexOf("@")>=0&&(this.pattern=this.handleNicknames(this.pattern).trim());let t=this.pattern.replace(/\s+/g," ").split(" ");if(t.length<5||t.length>6)throw new TypeError("CronPattern: invalid configuration format ('"+this.pattern+"'), exactly five or six space separated parts are required.");if(t.length===5&&t.unshift("0"),t[3].indexOf("L")>=0&&(t[3]=t[3].replace("L",""),this.lastDayOfMonth=!0),t[3]=="*"&&(this.starDOM=!0),t[4].length>=3&&(t[4]=this.replaceAlphaMonths(t[4])),t[5].length>=3&&(t[5]=this.replaceAlphaDays(t[5])),t[5]=="*"&&(this.starDOW=!0),this.pattern.indexOf("?")>=0){let e=new l(new Date,this.timezone).getDate(!0);t[0]=t[0].replace("?",e.getSeconds().toString()),t[1]=t[1].replace("?",e.getMinutes().toString()),t[2]=t[2].replace("?",e.getHours().toString()),this.starDOM||(t[3]=t[3].replace("?",e.getDate().toString())),t[4]=t[4].replace("?",(e.getMonth()+1).toString()),this.starDOW||(t[5]=t[5].replace("?",e.getDay().toString()))}this.throwAtIllegalCharacters(t),this.partToArray("second",t[0],0,1),this.partToArray("minute",t[1],0,1),this.partToArray("hour",t[2],0,1),this.partToArray("day",t[3],-1,1),this.partToArray("month",t[4],-1,1),this.partToArray("dayOfWeek",t[5],0,d),this.dayOfWeek[7]&&(this.dayOfWeek[0]=this.dayOfWeek[7])}partToArray(t,e,n,i){let s=this[t],a=t==="day"&&this.lastDayOfMonth;if(e===""&&!a)throw new TypeError("CronPattern: configuration entry "+t+" ("+e+") is empty, check for trailing spaces.");if(e==="*")return s.fill(i);let u=e.split(",");if(u.length>1)for(let o=0;o<u.length;o++)this.partToArray(t,u[o],n,i);else e.indexOf("-")!==-1&&e.indexOf("/")!==-1?this.handleRangeWithStepping(e,t,n,i):e.indexOf("-")!==-1?this.handleRange(e,t,n,i):e.indexOf("/")!==-1?this.handleStepping(e,t,n,i):e!==""&&this.handleNumber(e,t,n,i)}throwAtIllegalCharacters(t){for(let e=0;e<t.length;e++)if((e===5?/[^/*0-9,\-#L]+/:/[^/*0-9,-]+/).test(t[e]))throw new TypeError("CronPattern: configuration entry "+e+" ("+t[e]+") contains illegal characters.")}handleNumber(t,e,n,i){let s=this.extractNth(t,e),a=parseInt(s[0],10)+n;if(isNaN(a))throw new TypeError("CronPattern: "+e+" is not a number: '"+t+"'");this.setPart(e,a,s[1]||i)}setPart(t,e,n){if(!Object.prototype.hasOwnProperty.call(this,t))throw new TypeError("CronPattern: Invalid part specified: "+t);if(t==="dayOfWeek"){if(e===7&&(e=0),e<0||e>6)throw new RangeError("CronPattern: Invalid value for dayOfWeek: "+e);this.setNthWeekdayOfMonth(e,n);return}if(t==="second"||t==="minute"){if(e<0||e>=60)throw new RangeError("CronPattern: Invalid value for "+t+": "+e)}else if(t==="hour"){if(e<0||e>=24)throw new RangeError("CronPattern: Invalid value for "+t+": "+e)}else if(t==="day"){if(e<0||e>=31)throw new RangeError("CronPattern: Invalid value for "+t+": "+e)}else if(t==="month"&&(e<0||e>=12))throw new RangeError("CronPattern: Invalid value for "+t+": "+e);this[t][e]=n}handleRangeWithStepping(t,e,n,i){let s=this.extractNth(t,e),a=s[0].match(/^(\d+)-(\d+)\/(\d+)$/);if(a===null)throw new TypeError("CronPattern: Syntax error, illegal range with stepping: '"+t+"'");let[,u,o,h]=a,c=parseInt(u,10)+n,m=parseInt(o,10)+n,p=parseInt(h,10);if(isNaN(c))throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)");if(isNaN(m))throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)");if(isNaN(p))throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");if(p===0)throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");if(p>this[e].length)throw new TypeError("CronPattern: Syntax error, steps cannot be greater than maximum value of part ("+this[e].length+")");if(c>m)throw new TypeError("CronPattern: From value is larger than to value: '"+t+"'");for(let v=c;v<=m;v+=p)this.setPart(e,v,s[1]||i)}extractNth(t,e){let n=t,i;if(n.includes("#")){if(e!=="dayOfWeek")throw new Error("CronPattern: nth (#) only allowed in day-of-week field");i=n.split("#")[1],n=n.split("#")[0]}return[n,i]}handleRange(t,e,n,i){let s=this.extractNth(t,e),a=s[0].split("-");if(a.length!==2)throw new TypeError("CronPattern: Syntax error, illegal range: '"+t+"'");let u=parseInt(a[0],10)+n,o=parseInt(a[1],10)+n;if(isNaN(u))throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)");if(isNaN(o))throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)");if(u>o)throw new TypeError("CronPattern: From value is larger than to value: '"+t+"'");for(let h=u;h<=o;h++)this.setPart(e,h,s[1]||i)}handleStepping(t,e,n,i){let s=this.extractNth(t,e),a=s[0].split("/");if(a.length!==2)throw new TypeError("CronPattern: Syntax error, illegal stepping: '"+t+"'");a[0]===""&&(a[0]="*");let u=0;a[0]!=="*"&&(u=parseInt(a[0],10)+n);let o=parseInt(a[1],10);if(isNaN(o))throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");if(o===0)throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");if(o>this[e].length)throw new TypeError("CronPattern: Syntax error, max steps for part is ("+this[e].length+")");for(let h=u;h<this[e].length;h+=o)this.setPart(e,h,s[1]||i)}replaceAlphaDays(t){return t.replace(/-sun/gi,"-7").replace(/sun/gi,"0").replace(/mon/gi,"1").replace(/tue/gi,"2").replace(/wed/gi,"3").replace(/thu/gi,"4").replace(/fri/gi,"5").replace(/sat/gi,"6")}replaceAlphaMonths(t){return t.replace(/jan/gi,"1").replace(/feb/gi,"2").replace(/mar/gi,"3").replace(/apr/gi,"4").replace(/may/gi,"5").replace(/jun/gi,"6").replace(/jul/gi,"7").replace(/aug/gi,"8").replace(/sep/gi,"9").replace(/oct/gi,"10").replace(/nov/gi,"11").replace(/dec/gi,"12")}handleNicknames(t){let e=t.trim().toLowerCase();return e==="@yearly"||e==="@annually"?"0 0 1 1 *":e==="@monthly"?"0 0 1 * *":e==="@weekly"?"0 0 * * 0":e==="@daily"?"0 0 * * *":e==="@hourly"?"0 * * * *":t}setNthWeekdayOfMonth(t,e){if(typeof e!="number"&&e==="L")this.dayOfWeek[t]=this.dayOfWeek[t]|w;else if(e===d)this.dayOfWeek[t]=d;else if(e<6&&e>0)this.dayOfWeek[t]=this.dayOfWeek[t]|k[e-1];else throw new TypeError(`CronPattern: nth weekday out of range, should be 1-5 or L. Value: ${e}, Type: ${typeof e}`)}};var _=[31,28,31,30,31,30,31,31,30,31,30,31],f=[["month","year",0],["day","month",-1],["hour","day",0],["minute","hour",0],["second","minute",0]],l=class r{tz;ms;second;minute;hour;day;month;year;constructor(t,e){if(this.tz=e,t&&t instanceof Date)if(!isNaN(t))this.fromDate(t);else throw new TypeError("CronDate: Invalid date passed to CronDate constructor");else if(t===void 0)this.fromDate(new Date);else if(t&&typeof t=="string")this.fromString(t);else if(t instanceof r)this.fromCronDate(t);else throw new TypeError("CronDate: Invalid type ("+typeof t+") passed to CronDate constructor")}isNthWeekdayOfMonth(t,e,n,i){let a=new Date(Date.UTC(t,e,n)).getUTCDay(),u=0;for(let o=1;o<=n;o++)new Date(Date.UTC(t,e,o)).getUTCDay()===a&&u++;if(i&d&&k[u-1]&i)return!0;if(i&w){let o=new Date(Date.UTC(t,e+1,0)).getUTCDate();for(let h=n+1;h<=o;h++)if(new Date(Date.UTC(t,e,h)).getUTCDay()===a)return!1;return!0}return!1}fromDate(t){if(this.tz!==void 0)if(typeof this.tz=="number")this.ms=t.getUTCMilliseconds(),this.second=t.getUTCSeconds(),this.minute=t.getUTCMinutes()+this.tz,this.hour=t.getUTCHours(),this.day=t.getUTCDate(),this.month=t.getUTCMonth(),this.year=t.getUTCFullYear(),this.apply();else{let e=g(t,this.tz);this.ms=t.getMilliseconds(),this.second=e.s,this.minute=e.i,this.hour=e.h,this.day=e.d,this.month=e.m-1,this.year=e.y}else this.ms=t.getMilliseconds(),this.second=t.getSeconds(),this.minute=t.getMinutes(),this.hour=t.getHours(),this.day=t.getDate(),this.month=t.getMonth(),this.year=t.getFullYear()}fromCronDate(t){this.tz=t.tz,this.year=t.year,this.month=t.month,this.day=t.day,this.hour=t.hour,this.minute=t.minute,this.second=t.second,this.ms=t.ms}apply(){if(this.month>11||this.day>_[this.month]||this.hour>59||this.minute>59||this.second>59||this.hour<0||this.minute<0||this.second<0){let t=new Date(Date.UTC(this.year,this.month,this.day,this.hour,this.minute,this.second,this.ms));return this.ms=t.getUTCMilliseconds(),this.second=t.getUTCSeconds(),this.minute=t.getUTCMinutes(),this.hour=t.getUTCHours(),this.day=t.getUTCDate(),this.month=t.getUTCMonth(),this.year=t.getUTCFullYear(),!0}else return!1}fromString(t){if(typeof this.tz=="number"){let e=P(t);this.ms=e.getUTCMilliseconds(),this.second=e.getUTCSeconds(),this.minute=e.getUTCMinutes(),this.hour=e.getUTCHours(),this.day=e.getUTCDate(),this.month=e.getUTCMonth(),this.year=e.getUTCFullYear(),this.apply()}else return this.fromDate(P(t,this.tz))}findNext(t,e,n,i){let s=this[e],a;n.lastDayOfMonth&&(this.month!==1?a=_[this.month]:a=new Date(Date.UTC(this.year,this.month+1,0,0,0,0,0)).getUTCDate());let u=!n.starDOW&&e=="day"?new Date(Date.UTC(this.year,this.month,1,0,0,0,0)).getUTCDay():void 0;for(let o=this[e]+i;o<n[e].length;o++){let h=n[e][o];if(e==="day"&&n.lastDayOfMonth&&o-i==a&&(h=1),e==="day"&&!n.starDOW){let c=n.dayOfWeek[(u+(o-i-1))%7];if(c&&c&d)c=this.isNthWeekdayOfMonth(this.year,this.month,o-i,c)?1:0;else if(c)throw new Error(`CronDate: Invalid value for dayOfWeek encountered. ${c}`);t.legacyMode&&!n.starDOM?h=h||c:h=h&&c}if(h)return this[e]=o-i,s!==this[e]?2:1}return 3}recurse(t,e,n){let i=this.findNext(e,f[n][0],t,f[n][2]);if(i>1){let s=n+1;for(;s<f.length;)this[f[s][0]]=-f[s][2],s++;if(i===3)return this[f[n][1]]++,this[f[n][0]]=-f[n][2],this.apply(),this.recurse(t,e,0);if(this.apply())return this.recurse(t,e,n-1)}return n+=1,n>=f.length?this:this.year>=3e3?null:this.recurse(t,e,n)}increment(t,e,n){return this.second+=e.interval!==void 0&&e.interval>1&&n?e.interval:1,this.ms=0,this.apply(),this.recurse(t,e,0)}getDate(t){return t||this.tz===void 0?new Date(this.year,this.month,this.day,this.hour,this.minute,this.second,this.ms):typeof this.tz=="number"?new Date(Date.UTC(this.year,this.month,this.day,this.hour,this.minute-this.tz,this.second,this.ms)):N(T(this.year,this.month+1,this.day,this.hour,this.minute,this.second,this.tz),!1)}getTime(){return this.getDate(!1).getTime()}};function R(r){if(r===void 0&&(r={}),delete r.name,r.legacyMode=r.legacyMode===void 0?!0:r.legacyMode,r.paused=r.paused===void 0?!1:r.paused,r.maxRuns=r.maxRuns===void 0?1/0:r.maxRuns,r.catch=r.catch===void 0?!1:r.catch,r.interval=r.interval===void 0?0:parseInt(r.interval.toString(),10),r.utcOffset=r.utcOffset===void 0?void 0:parseInt(r.utcOffset.toString(),10),r.unref=r.unref===void 0?!1:r.unref,r.startAt&&(r.startAt=new l(r.startAt,r.timezone)),r.stopAt&&(r.stopAt=new l(r.stopAt,r.timezone)),r.interval!==null){if(isNaN(r.interval))throw new Error("CronOptions: Supplied value for interval is not a number");if(r.interval<0)throw new Error("CronOptions: Supplied value for interval can not be negative")}if(r.utcOffset!==void 0){if(isNaN(r.utcOffset))throw new Error("CronOptions: Invalid value passed for utcOffset, should be number representing minutes offset from UTC.");if(r.utcOffset<-870||r.utcOffset>870)throw new Error("CronOptions: utcOffset out of bounds.");if(r.utcOffset!==void 0&&r.timezone)throw new Error("CronOptions: Combining 'utcOffset' with 'timezone' is not allowed.")}if(r.unref!==!0&&r.unref!==!1)throw new Error("CronOptions: Unref should be either true, false or undefined(false).");return r}function C(r){return Object.prototype.toString.call(r)==="[object Function]"||typeof r=="function"||r instanceof Function}function x(r){return C(r)}function S(r){typeof Deno<"u"&&typeof Deno.unrefTimer<"u"?Deno.unrefTimer(r):r&&typeof r.unref<"u"&&r.unref()}var E=30*1e3,D=[],M=class{name;options;_states;fn;constructor(t,e,n){let i,s;if(C(e))s=e;else if(typeof e=="object")i=e;else if(e!==void 0)throw new Error("Cron: Invalid argument passed for optionsIn. Should be one of function, or object (options).");if(C(n))s=n;else if(typeof n=="object")i=n;else if(n!==void 0)throw new Error("Cron: Invalid argument passed for funcIn. Should be one of function, or object (options).");if(this.name=i?.name,this.options=R(i),this._states={kill:!1,blocking:!1,previousRun:void 0,currentRun:void 0,once:void 0,currentTimeout:void 0,maxRuns:i?i.maxRuns:void 0,paused:i?i.paused:!1,pattern:new b("* * * * *")},t&&(t instanceof Date||typeof t=="string"&&t.indexOf(":")>0)?this._states.once=new l(t,this.options.timezone||this.options.utcOffset):this._states.pattern=new b(t,this.options.timezone),this.name){if(D.find(u=>u.name===this.name))throw new Error("Cron: Tried to initialize new named job '"+this.name+"', but name already taken.");D.push(this)}return s!==void 0&&x(s)&&(this.fn=s,this.schedule()),this}nextRun(t){let e=this._next(t);return e?e.getDate(!1):null}nextRuns(t,e){this._states.maxRuns!==void 0&&t>this._states.maxRuns&&(t=this._states.maxRuns);let n=[],i=e||this._states.currentRun||void 0;for(;t--&&(i=this.nextRun(i));)n.push(i);return n}getPattern(){return this._states.pattern?this._states.pattern.pattern:void 0}isRunning(){let t=this.nextRun(this._states.currentRun),e=!this._states.paused,n=this.fn!==void 0,i=!this._states.kill;return e&&n&&i&&t!==null}isStopped(){return this._states.kill}isBusy(){return this._states.blocking}currentRun(){return this._states.currentRun?this._states.currentRun.getDate():null}previousRun(){return this._states.previousRun?this._states.previousRun.getDate():null}msToNext(t){let e=this._next(t);return e?t instanceof l||t instanceof Date?e.getTime()-t.getTime():e.getTime()-new l(t).getTime():null}stop(){this._states.kill=!0,this._states.currentTimeout&&clearTimeout(this._states.currentTimeout);let t=D.indexOf(this);t>=0&&D.splice(t,1)}pause(){return this._states.paused=!0,!this._states.kill}resume(){return this._states.paused=!1,!this._states.kill}schedule(t){if(t&&this.fn)throw new Error("Cron: It is not allowed to schedule two functions using the same Croner instance.");t&&(this.fn=t);let e=this.msToNext(),n=this.nextRun(this._states.currentRun);return e==null||isNaN(e)||n===null?this:(e>E&&(e=E),this._states.currentTimeout=setTimeout(()=>this._checkTrigger(n),e),this._states.currentTimeout&&this.options.unref&&S(this._states.currentTimeout),this)}async _trigger(t){if(this._states.blocking=!0,this._states.currentRun=new l(void 0,this.options.timezone||this.options.utcOffset),this.options.catch)try{this.fn!==void 0&&await this.fn(this,this.options.context)}catch(e){C(this.options.catch)&&this.options.catch(e,this)}else this.fn!==void 0&&await this.fn(this,this.options.context);this._states.previousRun=new l(t,this.options.timezone||this.options.utcOffset),this._states.blocking=!1}async trigger(){await this._trigger()}runsLeft(){return this._states.maxRuns}_checkTrigger(t){let e=new Date,n=!this._states.paused&&e.getTime()>=t.getTime(),i=this._states.blocking&&this.options.protect;n&&!i?(this._states.maxRuns!==void 0&&this._states.maxRuns--,this._trigger()):n&&i&&C(this.options.protect)&&setTimeout(()=>this.options.protect(this),0),this.schedule()}_next(t){let e=!!(t||this._states.currentRun),n=!1;!t&&this.options.startAt&&this.options.interval&&([t,e]=this._calculatePreviousRun(t,e),n=!t),t=new l(t,this.options.timezone||this.options.utcOffset),this.options.startAt&&t&&t.getTime()<this.options.startAt.getTime()&&(t=this.options.startAt);let i=this._states.once||new l(t,this.options.timezone||this.options.utcOffset);return!n&&i!==this._states.once&&(i=i.increment(this._states.pattern,this.options,e)),this._states.once&&this._states.once.getTime()<=t.getTime()||i===null||this._states.maxRuns!==void 0&&this._states.maxRuns<=0||this._states.kill||this.options.stopAt&&i.getTime()>=this.options.stopAt.getTime()?null:i}_calculatePreviousRun(t,e){let n=new l(void 0,this.options.timezone||this.options.utcOffset),i=t;if(this.options.startAt.getTime()<=n.getTime()){i=this.options.startAt;let s=i.getTime()+this.options.interval*1e3;for(;s<=n.getTime();)i=new l(i,this.options.timezone||this.options.utcOffset).increment(this._states.pattern,this.options,!0),s=i.getTime()+this.options.interval*1e3;e=!0}return i===null&&(i=void 0),[i,e]}};export{M as Cron,l as CronDate,b as CronPattern,D as scheduledJobs};
|
|
1
|
+
function y(n){return Date.UTC(n.y,n.m-1,n.d,n.h,n.i,n.s)}function D(n,e){return n.y===e.y&&n.m===e.m&&n.d===e.d&&n.h===e.h&&n.i===e.i&&n.s===e.s}function S(n,e){let t=new Date(Date.parse(n));if(isNaN(t))throw new Error("Invalid ISO8601 passed to timezone parser.");let r=n.substring(9);return r.includes("Z")||r.includes("+")||r.includes("-")?T(t.getUTCFullYear(),t.getUTCMonth()+1,t.getUTCDate(),t.getUTCHours(),t.getUTCMinutes(),t.getUTCSeconds(),"Etc/UTC"):T(t.getFullYear(),t.getMonth()+1,t.getDate(),t.getHours(),t.getMinutes(),t.getSeconds(),e)}function v(n,e,t){return O(S(n,e),t)}function O(n,e){let t=new Date(y(n)),r=g(t,n.tz),i=y(n),s=y(r),a=i-s,h=new Date(t.getTime()+a),o=g(h,n.tz);if(D(o,n)){let m=new Date(h.getTime()-36e5),d=g(m,n.tz);return D(d,n)?m:h}let u=new Date(h.getTime()+y(n)-y(o)),l=g(u,n.tz);if(D(l,n))return u;if(e)throw new Error("Invalid date passed to fromTZ()");return h.getTime()>u.getTime()?h:u}function g(n,e){let r=new Intl.DateTimeFormat("en-US",{timeZone:e,year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"numeric",second:"numeric",hour12:!1}).formatToParts(n),i={year:0,month:0,day:0,hour:0,minute:0,second:0};for(let s of r)(s.type==="year"||s.type==="month"||s.type==="day"||s.type==="hour"||s.type==="minute"||s.type==="second")&&(i[s.type]=parseInt(s.value,10));return i.hour===24&&(i.hour=0),{y:i.year,m:i.month,d:i.day,h:i.hour,i:i.minute,s:i.second,tz:e}}function T(n,e,t,r,i,s,a){return{y:n,m:e,d:t,h:r,i,s,tz:a}}var k=[1,2,4,8,16],C=class{pattern;timezone;second;minute;hour;day;month;dayOfWeek;year;lastDayOfMonth;nearestWeekdays;starDOM;starDOW;starYear;useAndLogic;constructor(e,t){this.pattern=e,this.timezone=t,this.second=Array(60).fill(0),this.minute=Array(60).fill(0),this.hour=Array(24).fill(0),this.day=Array(31).fill(0),this.month=Array(12).fill(0),this.dayOfWeek=Array(7).fill(0),this.year=Array(1e4).fill(0),this.lastDayOfMonth=!1,this.nearestWeekdays=Array(31).fill(0),this.starDOM=!1,this.starDOW=!1,this.starYear=!1,this.useAndLogic=!1,this.parse()}parse(){if(!(typeof this.pattern=="string"||this.pattern instanceof String))throw new TypeError("CronPattern: Pattern has to be of type string.");this.pattern.indexOf("@")>=0&&(this.pattern=this.handleNicknames(this.pattern).trim());let e=this.pattern.match(/\S+/g)||[""];if(e.length<5||e.length>7)throw new TypeError("CronPattern: invalid configuration format ('"+this.pattern+"'), exactly five, six, or seven space separated parts are required.");if(e.length===5&&e.unshift("0"),e.length===6&&e.push("*"),e[3].indexOf("L")>=0&&(e[3]=e[3].replace("L",""),this.lastDayOfMonth=!0),e[3]=="*"&&(this.starDOM=!0),e[6]=="*"&&(this.starYear=!0),e[4].length>=3&&(e[4]=this.replaceAlphaMonths(e[4])),e[5].length>=3&&(e[5]=this.replaceAlphaDays(e[5])),e[5].startsWith("+")&&(this.useAndLogic=!0,e[5]=e[5].substring(1),e[5]===""))throw new TypeError("CronPattern: Day-of-week field cannot be empty after '+' modifier.");e[5]=="*"&&(this.starDOW=!0),this.pattern.indexOf("?")>=0&&(e[0]=e[0].replace(/\?/g,"*"),e[1]=e[1].replace(/\?/g,"*"),e[2]=e[2].replace(/\?/g,"*"),e[3]=e[3].replace(/\?/g,"*"),e[4]=e[4].replace(/\?/g,"*"),e[5]=e[5].replace(/\?/g,"*"),e[6]&&(e[6]=e[6].replace(/\?/g,"*"))),this.throwAtIllegalCharacters(e),this.partToArray("second",e[0],0,1),this.partToArray("minute",e[1],0,1),this.partToArray("hour",e[2],0,1),this.partToArray("day",e[3],-1,1),this.partToArray("month",e[4],-1,1),this.partToArray("dayOfWeek",e[5],0,63),this.partToArray("year",e[6],0,1),this.dayOfWeek[7]&&(this.dayOfWeek[0]=this.dayOfWeek[7])}partToArray(e,t,r,i){let s=this[e],a=e==="day"&&this.lastDayOfMonth;if(t===""&&!a)throw new TypeError("CronPattern: configuration entry "+e+" ("+t+") is empty, check for trailing spaces.");if(t==="*")return s.fill(i);let h=t.split(",");if(h.length>1)for(let o=0;o<h.length;o++)this.partToArray(e,h[o],r,i);else t.indexOf("-")!==-1&&t.indexOf("/")!==-1?this.handleRangeWithStepping(t,e,r,i):t.indexOf("-")!==-1?this.handleRange(t,e,r,i):t.indexOf("/")!==-1?this.handleStepping(t,e,r,i):t!==""&&this.handleNumber(t,e,r,i)}throwAtIllegalCharacters(e){for(let t=0;t<e.length;t++)if((t===3?/[^/*0-9,-WL]+/:t===5?/[^/*0-9,\-#L]+/:/[^/*0-9,-]+/).test(e[t]))throw new TypeError("CronPattern: configuration entry "+t+" ("+e[t]+") contains illegal characters.")}handleNumber(e,t,r,i){let s=this.extractNth(e,t),a=e.toUpperCase().includes("W");if(t!=="day"&&a)throw new TypeError("CronPattern: Nearest weekday modifier (W) only allowed in day-of-month.");a&&(t="nearestWeekdays");let h=parseInt(s[0],10)+r;if(isNaN(h))throw new TypeError("CronPattern: "+t+" is not a number: '"+e+"'");this.setPart(t,h,s[1]||i)}setPart(e,t,r){if(!Object.prototype.hasOwnProperty.call(this,e))throw new TypeError("CronPattern: Invalid part specified: "+e);if(e==="dayOfWeek"){if(t===7&&(t=0),t<0||t>6)throw new RangeError("CronPattern: Invalid value for dayOfWeek: "+t);this.setNthWeekdayOfMonth(t,r);return}if(e==="second"||e==="minute"){if(t<0||t>=60)throw new RangeError("CronPattern: Invalid value for "+e+": "+t)}else if(e==="hour"){if(t<0||t>=24)throw new RangeError("CronPattern: Invalid value for "+e+": "+t)}else if(e==="day"||e==="nearestWeekdays"){if(t<0||t>=31)throw new RangeError("CronPattern: Invalid value for "+e+": "+t)}else if(e==="month"){if(t<0||t>=12)throw new RangeError("CronPattern: Invalid value for "+e+": "+t)}else if(e==="year"&&(t<1||t>=1e4))throw new RangeError("CronPattern: Invalid value for "+e+": "+t+" (supported range: 1-9999)");this[e][t]=r}handleRangeWithStepping(e,t,r,i){if(e.toUpperCase().includes("W"))throw new TypeError("CronPattern: Syntax error, W is not allowed in ranges with stepping.");let s=this.extractNth(e,t),a=s[0].match(/^(\d+)-(\d+)\/(\d+)$/);if(a===null)throw new TypeError("CronPattern: Syntax error, illegal range with stepping: '"+e+"'");let[,h,o,u]=a,l=parseInt(h,10)+r,m=parseInt(o,10)+r,d=parseInt(u,10);if(isNaN(l))throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)");if(isNaN(m))throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)");if(isNaN(d))throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");if(d===0)throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");if(d>this[t].length)throw new TypeError("CronPattern: Syntax error, steps cannot be greater than maximum value of part ("+this[t].length+")");if(l>m)throw new TypeError("CronPattern: From value is larger than to value: '"+e+"'");for(let w=l;w<=m;w+=d)this.setPart(t,w,s[1]||i)}extractNth(e,t){let r=e,i;if(r.includes("#")){if(t!=="dayOfWeek")throw new Error("CronPattern: nth (#) only allowed in day-of-week field");i=r.split("#")[1],r=r.split("#")[0]}return[r,i]}handleRange(e,t,r,i){if(e.toUpperCase().includes("W"))throw new TypeError("CronPattern: Syntax error, W is not allowed in a range.");let s=this.extractNth(e,t),a=s[0].split("-");if(a.length!==2)throw new TypeError("CronPattern: Syntax error, illegal range: '"+e+"'");let h=parseInt(a[0],10)+r,o=parseInt(a[1],10)+r;if(isNaN(h))throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)");if(isNaN(o))throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)");if(h>o)throw new TypeError("CronPattern: From value is larger than to value: '"+e+"'");for(let u=h;u<=o;u++)this.setPart(t,u,s[1]||i)}handleStepping(e,t,r,i){if(e.toUpperCase().includes("W"))throw new TypeError("CronPattern: Syntax error, W is not allowed in parts with stepping.");let s=this.extractNth(e,t),a=s[0].split("/");if(a.length!==2)throw new TypeError("CronPattern: Syntax error, illegal stepping: '"+e+"'");a[0]===""&&(a[0]="*");let h=0;a[0]!=="*"&&(h=parseInt(a[0],10)+r);let o=parseInt(a[1],10);if(isNaN(o))throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");if(o===0)throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");if(o>this[t].length)throw new TypeError("CronPattern: Syntax error, max steps for part is ("+this[t].length+")");for(let u=h;u<this[t].length;u+=o)this.setPart(t,u,s[1]||i)}replaceAlphaDays(e){return e.replace(/-sun/gi,"-7").replace(/sun/gi,"0").replace(/mon/gi,"1").replace(/tue/gi,"2").replace(/wed/gi,"3").replace(/thu/gi,"4").replace(/fri/gi,"5").replace(/sat/gi,"6")}replaceAlphaMonths(e){return e.replace(/jan/gi,"1").replace(/feb/gi,"2").replace(/mar/gi,"3").replace(/apr/gi,"4").replace(/may/gi,"5").replace(/jun/gi,"6").replace(/jul/gi,"7").replace(/aug/gi,"8").replace(/sep/gi,"9").replace(/oct/gi,"10").replace(/nov/gi,"11").replace(/dec/gi,"12")}handleNicknames(e){let t=e.trim().toLowerCase();if(t==="@yearly"||t==="@annually")return"0 0 1 1 *";if(t==="@monthly")return"0 0 1 * *";if(t==="@weekly")return"0 0 * * 0";if(t==="@daily"||t==="@midnight")return"0 0 * * *";if(t==="@hourly")return"0 * * * *";if(t==="@reboot")throw new TypeError("CronPattern: @reboot is not supported in this environment. This is an event-based trigger that requires system startup detection.");return e}setNthWeekdayOfMonth(e,t){if(typeof t!="number"&&t==="L")this.dayOfWeek[e]=this.dayOfWeek[e]|32;else if(t===63)this.dayOfWeek[e]=63;else if(t<6&&t>0)this.dayOfWeek[e]=this.dayOfWeek[e]|k[t-1];else throw new TypeError(`CronPattern: nth weekday out of range, should be 1-5 or L. Value: ${t}, Type: ${typeof t}`)}};var N=[31,28,31,30,31,30,31,31,30,31,30,31],c=[["month","year",0],["day","month",-1],["hour","day",0],["minute","hour",0],["second","minute",0]],f=class n{tz;ms;second;minute;hour;day;month;year;constructor(e,t){if(this.tz=t,e&&e instanceof Date)if(!isNaN(e))this.fromDate(e);else throw new TypeError("CronDate: Invalid date passed to CronDate constructor");else if(e===void 0)this.fromDate(new Date);else if(e&&typeof e=="string")this.fromString(e);else if(e instanceof n)this.fromCronDate(e);else throw new TypeError("CronDate: Invalid type ("+typeof e+") passed to CronDate constructor")}getNearestWeekday(e,t,r){let s=new Date(Date.UTC(e,t,r)).getUTCDay();if(s===0){let a=new Date(Date.UTC(e,t+1,0)).getUTCDate();return r===a?r-2:r+1}return s===6?r===1?r+2:r-1:r}isNthWeekdayOfMonth(e,t,r,i){let a=new Date(Date.UTC(e,t,r)).getUTCDay(),h=0;for(let o=1;o<=r;o++)new Date(Date.UTC(e,t,o)).getUTCDay()===a&&h++;if(i&63&&k[h-1]&i)return!0;if(i&32){let o=new Date(Date.UTC(e,t+1,0)).getUTCDate();for(let u=r+1;u<=o;u++)if(new Date(Date.UTC(e,t,u)).getUTCDay()===a)return!1;return!0}return!1}fromDate(e){if(this.tz!==void 0)if(typeof this.tz=="number")this.ms=e.getUTCMilliseconds(),this.second=e.getUTCSeconds(),this.minute=e.getUTCMinutes()+this.tz,this.hour=e.getUTCHours(),this.day=e.getUTCDate(),this.month=e.getUTCMonth(),this.year=e.getUTCFullYear(),this.apply();else{let t=g(e,this.tz);this.ms=e.getMilliseconds(),this.second=t.s,this.minute=t.i,this.hour=t.h,this.day=t.d,this.month=t.m-1,this.year=t.y}else this.ms=e.getMilliseconds(),this.second=e.getSeconds(),this.minute=e.getMinutes(),this.hour=e.getHours(),this.day=e.getDate(),this.month=e.getMonth(),this.year=e.getFullYear()}fromCronDate(e){this.tz=e.tz,this.year=e.year,this.month=e.month,this.day=e.day,this.hour=e.hour,this.minute=e.minute,this.second=e.second,this.ms=e.ms}apply(){if(this.month>11||this.day>N[this.month]||this.hour>59||this.minute>59||this.second>59||this.hour<0||this.minute<0||this.second<0){let e=new Date(Date.UTC(this.year,this.month,this.day,this.hour,this.minute,this.second,this.ms));return this.ms=e.getUTCMilliseconds(),this.second=e.getUTCSeconds(),this.minute=e.getUTCMinutes(),this.hour=e.getUTCHours(),this.day=e.getUTCDate(),this.month=e.getUTCMonth(),this.year=e.getUTCFullYear(),!0}else return!1}fromString(e){if(typeof this.tz=="number"){let t=v(e);this.ms=t.getUTCMilliseconds(),this.second=t.getUTCSeconds(),this.minute=t.getUTCMinutes(),this.hour=t.getUTCHours(),this.day=t.getUTCDate(),this.month=t.getUTCMonth(),this.year=t.getUTCFullYear(),this.apply()}else return this.fromDate(v(e,this.tz))}findNext(e,t,r,i){let s=this[t],a;r.lastDayOfMonth&&(this.month!==1?a=N[this.month]:a=new Date(Date.UTC(this.year,this.month+1,0,0,0,0,0)).getUTCDate());let h=!r.starDOW&&t=="day"?new Date(Date.UTC(this.year,this.month,1,0,0,0,0)).getUTCDay():void 0;for(let o=this[t]+i;o<r[t].length;o++){let u=r[t][o];if(t==="day"&&!u){for(let l=0;l<r.nearestWeekdays.length;l++)if(r.nearestWeekdays[l]&&this.getNearestWeekday(this.year,this.month,l-i)===o-i){u=1;break}}if(t==="day"&&r.lastDayOfMonth&&o-i==a&&(u=1),t==="day"&&!r.starDOW){let l=r.dayOfWeek[(h+(o-i-1))%7];if(l&&l&63)l=this.isNthWeekdayOfMonth(this.year,this.month,o-i,l)?1:0;else if(l)throw new Error(`CronDate: Invalid value for dayOfWeek encountered. ${l}`);r.useAndLogic?u=u&&l:e.legacyMode&&!r.starDOM?u=u||l:u=u&&l}if(u)return this[t]=o-i,s!==this[t]?2:1}return 3}recurse(e,t,r){if(r===0&&!e.starYear){if(this.year>=0&&this.year<e.year.length&&e.year[this.year]===0){let s=-1;for(let a=this.year+1;a<e.year.length&&a<1e4;a++)if(e.year[a]===1){s=a;break}if(s===-1)return null;this.year=s,this.month=0,this.day=1,this.hour=0,this.minute=0,this.second=0,this.ms=0}if(this.year>=1e4)return null}let i=this.findNext(t,c[r][0],e,c[r][2]);if(i>1){let s=r+1;for(;s<c.length;)this[c[s][0]]=-c[s][2],s++;if(i===3){if(this[c[r][1]]++,this[c[r][0]]=-c[r][2],this.apply(),r===0&&!e.starYear){for(;this.year>=0&&this.year<e.year.length&&e.year[this.year]===0&&this.year<1e4;)this.year++;if(this.year>=1e4||this.year>=e.year.length)return null}return this.recurse(e,t,0)}else if(this.apply())return this.recurse(e,t,r-1)}return r+=1,r>=c.length?this:(e.starYear?this.year>=3e3:this.year>=1e4)?null:this.recurse(e,t,r)}increment(e,t,r){return this.second+=t.interval!==void 0&&t.interval>1&&r?t.interval:1,this.ms=0,this.apply(),this.recurse(e,t,0)}getDate(e){return e||this.tz===void 0?new Date(this.year,this.month,this.day,this.hour,this.minute,this.second,this.ms):typeof this.tz=="number"?new Date(Date.UTC(this.year,this.month,this.day,this.hour,this.minute-this.tz,this.second,this.ms)):O(T(this.year,this.month+1,this.day,this.hour,this.minute,this.second,this.tz),!1)}getTime(){return this.getDate(!1).getTime()}};function E(n){if(n===void 0&&(n={}),delete n.name,n.legacyMode=n.legacyMode===void 0?!0:n.legacyMode,n.paused=n.paused===void 0?!1:n.paused,n.maxRuns=n.maxRuns===void 0?1/0:n.maxRuns,n.catch=n.catch===void 0?!1:n.catch,n.interval=n.interval===void 0?0:parseInt(n.interval.toString(),10),n.utcOffset=n.utcOffset===void 0?void 0:parseInt(n.utcOffset.toString(),10),n.unref=n.unref===void 0?!1:n.unref,n.startAt&&(n.startAt=new f(n.startAt,n.timezone)),n.stopAt&&(n.stopAt=new f(n.stopAt,n.timezone)),n.interval!==null){if(isNaN(n.interval))throw new Error("CronOptions: Supplied value for interval is not a number");if(n.interval<0)throw new Error("CronOptions: Supplied value for interval can not be negative")}if(n.utcOffset!==void 0){if(isNaN(n.utcOffset))throw new Error("CronOptions: Invalid value passed for utcOffset, should be number representing minutes offset from UTC.");if(n.utcOffset<-870||n.utcOffset>870)throw new Error("CronOptions: utcOffset out of bounds.");if(n.utcOffset!==void 0&&n.timezone)throw new Error("CronOptions: Combining 'utcOffset' with 'timezone' is not allowed.")}if(n.unref!==!0&&n.unref!==!1)throw new Error("CronOptions: Unref should be either true, false or undefined(false).");return n}function p(n){return Object.prototype.toString.call(n)==="[object Function]"||typeof n=="function"||n instanceof Function}function R(n){return p(n)}function _(n){typeof Deno<"u"&&typeof Deno.unrefTimer<"u"?Deno.unrefTimer(n):n&&typeof n.unref<"u"&&n.unref()}var U=30*1e3,b=[],x=class{name;options;_states;fn;constructor(e,t,r){let i,s;if(p(t))s=t;else if(typeof t=="object")i=t;else if(t!==void 0)throw new Error("Cron: Invalid argument passed for optionsIn. Should be one of function, or object (options).");if(p(r))s=r;else if(typeof r=="object")i=r;else if(r!==void 0)throw new Error("Cron: Invalid argument passed for funcIn. Should be one of function, or object (options).");if(this.name=i?.name,this.options=E(i),this._states={kill:!1,blocking:!1,previousRun:void 0,currentRun:void 0,once:void 0,currentTimeout:void 0,maxRuns:i?i.maxRuns:void 0,paused:i?i.paused:!1,pattern:new C("* * * * *")},e&&(e instanceof Date||typeof e=="string"&&e.indexOf(":")>0)?this._states.once=new f(e,this.options.timezone||this.options.utcOffset):this._states.pattern=new C(e,this.options.timezone),this.name){if(b.find(h=>h.name===this.name))throw new Error("Cron: Tried to initialize new named job '"+this.name+"', but name already taken.");b.push(this)}return s!==void 0&&R(s)&&(this.fn=s,this.schedule()),this}nextRun(e){let t=this._next(e);return t?t.getDate(!1):null}nextRuns(e,t){this._states.maxRuns!==void 0&&e>this._states.maxRuns&&(e=this._states.maxRuns);let r=[],i=t||this._states.currentRun||void 0;for(;e--&&(i=this.nextRun(i));)r.push(i);return r}getPattern(){return this._states.pattern?this._states.pattern.pattern:void 0}isRunning(){let e=this.nextRun(this._states.currentRun),t=!this._states.paused,r=this.fn!==void 0,i=!this._states.kill;return t&&r&&i&&e!==null}isStopped(){return this._states.kill}isBusy(){return this._states.blocking}currentRun(){return this._states.currentRun?this._states.currentRun.getDate():null}previousRun(){return this._states.previousRun?this._states.previousRun.getDate():null}msToNext(e){let t=this._next(e);return t?e instanceof f||e instanceof Date?t.getTime()-e.getTime():t.getTime()-new f(e).getTime():null}stop(){this._states.kill=!0,this._states.currentTimeout&&clearTimeout(this._states.currentTimeout);let e=b.indexOf(this);e>=0&&b.splice(e,1)}pause(){return this._states.paused=!0,!this._states.kill}resume(){return this._states.paused=!1,!this._states.kill}schedule(e){if(e&&this.fn)throw new Error("Cron: It is not allowed to schedule two functions using the same Croner instance.");e&&(this.fn=e);let t=this.msToNext(),r=this.nextRun(this._states.currentRun);return t==null||isNaN(t)||r===null?this:(t>U&&(t=U),this._states.currentTimeout=setTimeout(()=>this._checkTrigger(r),t),this._states.currentTimeout&&this.options.unref&&_(this._states.currentTimeout),this)}async _trigger(e){if(this._states.blocking=!0,this._states.currentRun=new f(void 0,this.options.timezone||this.options.utcOffset),this.options.catch)try{this.fn!==void 0&&await this.fn(this,this.options.context)}catch(t){p(this.options.catch)&&this.options.catch(t,this)}else this.fn!==void 0&&await this.fn(this,this.options.context);this._states.previousRun=new f(e,this.options.timezone||this.options.utcOffset),this._states.blocking=!1}async trigger(){await this._trigger()}runsLeft(){return this._states.maxRuns}_checkTrigger(e){let t=new Date,r=!this._states.paused&&t.getTime()>=e.getTime(),i=this._states.blocking&&this.options.protect;r&&!i?(this._states.maxRuns!==void 0&&this._states.maxRuns--,this._trigger()):r&&i&&p(this.options.protect)&&setTimeout(()=>this.options.protect(this),0),this.schedule()}_next(e){let t=!!(e||this._states.currentRun),r=!1;!e&&this.options.startAt&&this.options.interval&&([e,t]=this._calculatePreviousRun(e,t),r=!e),e=new f(e,this.options.timezone||this.options.utcOffset),this.options.startAt&&e&&e.getTime()<this.options.startAt.getTime()&&(e=this.options.startAt);let i=this._states.once||new f(e,this.options.timezone||this.options.utcOffset);return!r&&i!==this._states.once&&(i=i.increment(this._states.pattern,this.options,t)),this._states.once&&this._states.once.getTime()<=e.getTime()||i===null||this._states.maxRuns!==void 0&&this._states.maxRuns<=0||this._states.kill||this.options.stopAt&&i.getTime()>=this.options.stopAt.getTime()?null:i}_calculatePreviousRun(e,t){let r=new f(void 0,this.options.timezone||this.options.utcOffset),i=e;if(this.options.startAt.getTime()<=r.getTime()){i=this.options.startAt;let s=i.getTime()+this.options.interval*1e3;for(;s<=r.getTime();)i=new f(i,this.options.timezone||this.options.utcOffset).increment(this._states.pattern,this.options,!0),s=i.getTime()+this.options.interval*1e3;t=!0}return i===null&&(i=void 0),[i,t]}};export{x as Cron,f as CronDate,C as CronPattern,b as scheduledJobs};
|
package/dist/croner.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var Cron=(()=>{var g=(r,t)=>()=>(r&&(t=r(r=0)),t);var H=(r,t)=>()=>(t||r((t={exports:{}}).exports,t),t.exports);function b(r){return Date.UTC(r.y,r.m-1,r.d,r.h,r.i,r.s)}function k(r,t){return r.y===t.y&&r.m===t.m&&r.d===t.d&&r.h===t.h&&r.i===t.i&&r.s===t.s}function Z(r,t){let e=new Date(Date.parse(r));if(isNaN(e))throw new Error("Invalid ISO8601 passed to timezone parser.");let n=r.substring(9);return n.includes("Z")||n.includes("+")||n.includes("-")?w(e.getUTCFullYear(),e.getUTCMonth()+1,e.getUTCDate(),e.getUTCHours(),e.getUTCMinutes(),e.getUTCSeconds(),"Etc/UTC"):w(e.getFullYear(),e.getMonth()+1,e.getDate(),e.getHours(),e.getMinutes(),e.getSeconds(),t)}function _(r,t,e){return x(Z(r,t),e)}function x(r,t){let e=new Date(b(r)),n=d(e,r.tz),i=b(r),s=b(n),a=i-s,u=new Date(e.getTime()+a),o=d(u,r.tz);if(k(o,r)){let m=new Date(u.getTime()-36e5),p=d(m,r.tz);return k(p,r)?m:u}let h=new Date(u.getTime()+b(r)-b(o)),c=d(h,r.tz);if(k(c,r))return h;if(t)throw new Error("Invalid date passed to fromTZ()");return u.getTime()>h.getTime()?u:h}function d(r,t){let n=new Intl.DateTimeFormat("en-US",{timeZone:t,year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"numeric",second:"numeric",hour12:!1}).formatToParts(r),i={year:0,month:0,day:0,hour:0,minute:0,second:0};for(let s of n)(s.type==="year"||s.type==="month"||s.type==="day"||s.type==="hour"||s.type==="minute"||s.type==="second")&&(i[s.type]=parseInt(s.value,10));return i.hour===24&&(i.hour=0),{y:i.year,m:i.month,d:i.day,h:i.hour,i:i.minute,s:i.second,tz:t}}function w(r,t,e,n,i,s,a){return{y:r,m:t,d:e,h:n,i,s,tz:a}}var E=g(()=>{});var D,C,R,T,S=g(()=>{v();D=32,C=31|D,R=[1,2,4,8,16],T=class{pattern;timezone;second;minute;hour;day;month;dayOfWeek;lastDayOfMonth;starDOM;starDOW;constructor(t,e){this.pattern=t,this.timezone=e,this.second=Array(60).fill(0),this.minute=Array(60).fill(0),this.hour=Array(24).fill(0),this.day=Array(31).fill(0),this.month=Array(12).fill(0),this.dayOfWeek=Array(7).fill(0),this.lastDayOfMonth=!1,this.starDOM=!1,this.starDOW=!1,this.parse()}parse(){if(!(typeof this.pattern=="string"||this.pattern instanceof String))throw new TypeError("CronPattern: Pattern has to be of type string.");this.pattern.indexOf("@")>=0&&(this.pattern=this.handleNicknames(this.pattern).trim());let t=this.pattern.replace(/\s+/g," ").split(" ");if(t.length<5||t.length>6)throw new TypeError("CronPattern: invalid configuration format ('"+this.pattern+"'), exactly five or six space separated parts are required.");if(t.length===5&&t.unshift("0"),t[3].indexOf("L")>=0&&(t[3]=t[3].replace("L",""),this.lastDayOfMonth=!0),t[3]=="*"&&(this.starDOM=!0),t[4].length>=3&&(t[4]=this.replaceAlphaMonths(t[4])),t[5].length>=3&&(t[5]=this.replaceAlphaDays(t[5])),t[5]=="*"&&(this.starDOW=!0),this.pattern.indexOf("?")>=0){let e=new l(new Date,this.timezone).getDate(!0);t[0]=t[0].replace("?",e.getSeconds().toString()),t[1]=t[1].replace("?",e.getMinutes().toString()),t[2]=t[2].replace("?",e.getHours().toString()),this.starDOM||(t[3]=t[3].replace("?",e.getDate().toString())),t[4]=t[4].replace("?",(e.getMonth()+1).toString()),this.starDOW||(t[5]=t[5].replace("?",e.getDay().toString()))}this.throwAtIllegalCharacters(t),this.partToArray("second",t[0],0,1),this.partToArray("minute",t[1],0,1),this.partToArray("hour",t[2],0,1),this.partToArray("day",t[3],-1,1),this.partToArray("month",t[4],-1,1),this.partToArray("dayOfWeek",t[5],0,C),this.dayOfWeek[7]&&(this.dayOfWeek[0]=this.dayOfWeek[7])}partToArray(t,e,n,i){let s=this[t],a=t==="day"&&this.lastDayOfMonth;if(e===""&&!a)throw new TypeError("CronPattern: configuration entry "+t+" ("+e+") is empty, check for trailing spaces.");if(e==="*")return s.fill(i);let u=e.split(",");if(u.length>1)for(let o=0;o<u.length;o++)this.partToArray(t,u[o],n,i);else e.indexOf("-")!==-1&&e.indexOf("/")!==-1?this.handleRangeWithStepping(e,t,n,i):e.indexOf("-")!==-1?this.handleRange(e,t,n,i):e.indexOf("/")!==-1?this.handleStepping(e,t,n,i):e!==""&&this.handleNumber(e,t,n,i)}throwAtIllegalCharacters(t){for(let e=0;e<t.length;e++)if((e===5?/[^/*0-9,\-#L]+/:/[^/*0-9,-]+/).test(t[e]))throw new TypeError("CronPattern: configuration entry "+e+" ("+t[e]+") contains illegal characters.")}handleNumber(t,e,n,i){let s=this.extractNth(t,e),a=parseInt(s[0],10)+n;if(isNaN(a))throw new TypeError("CronPattern: "+e+" is not a number: '"+t+"'");this.setPart(e,a,s[1]||i)}setPart(t,e,n){if(!Object.prototype.hasOwnProperty.call(this,t))throw new TypeError("CronPattern: Invalid part specified: "+t);if(t==="dayOfWeek"){if(e===7&&(e=0),e<0||e>6)throw new RangeError("CronPattern: Invalid value for dayOfWeek: "+e);this.setNthWeekdayOfMonth(e,n);return}if(t==="second"||t==="minute"){if(e<0||e>=60)throw new RangeError("CronPattern: Invalid value for "+t+": "+e)}else if(t==="hour"){if(e<0||e>=24)throw new RangeError("CronPattern: Invalid value for "+t+": "+e)}else if(t==="day"){if(e<0||e>=31)throw new RangeError("CronPattern: Invalid value for "+t+": "+e)}else if(t==="month"&&(e<0||e>=12))throw new RangeError("CronPattern: Invalid value for "+t+": "+e);this[t][e]=n}handleRangeWithStepping(t,e,n,i){let s=this.extractNth(t,e),a=s[0].match(/^(\d+)-(\d+)\/(\d+)$/);if(a===null)throw new TypeError("CronPattern: Syntax error, illegal range with stepping: '"+t+"'");let[,u,o,h]=a,c=parseInt(u,10)+n,m=parseInt(o,10)+n,p=parseInt(h,10);if(isNaN(c))throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)");if(isNaN(m))throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)");if(isNaN(p))throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");if(p===0)throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");if(p>this[e].length)throw new TypeError("CronPattern: Syntax error, steps cannot be greater than maximum value of part ("+this[e].length+")");if(c>m)throw new TypeError("CronPattern: From value is larger than to value: '"+t+"'");for(let N=c;N<=m;N+=p)this.setPart(e,N,s[1]||i)}extractNth(t,e){let n=t,i;if(n.includes("#")){if(e!=="dayOfWeek")throw new Error("CronPattern: nth (#) only allowed in day-of-week field");i=n.split("#")[1],n=n.split("#")[0]}return[n,i]}handleRange(t,e,n,i){let s=this.extractNth(t,e),a=s[0].split("-");if(a.length!==2)throw new TypeError("CronPattern: Syntax error, illegal range: '"+t+"'");let u=parseInt(a[0],10)+n,o=parseInt(a[1],10)+n;if(isNaN(u))throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)");if(isNaN(o))throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)");if(u>o)throw new TypeError("CronPattern: From value is larger than to value: '"+t+"'");for(let h=u;h<=o;h++)this.setPart(e,h,s[1]||i)}handleStepping(t,e,n,i){let s=this.extractNth(t,e),a=s[0].split("/");if(a.length!==2)throw new TypeError("CronPattern: Syntax error, illegal stepping: '"+t+"'");a[0]===""&&(a[0]="*");let u=0;a[0]!=="*"&&(u=parseInt(a[0],10)+n);let o=parseInt(a[1],10);if(isNaN(o))throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");if(o===0)throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");if(o>this[e].length)throw new TypeError("CronPattern: Syntax error, max steps for part is ("+this[e].length+")");for(let h=u;h<this[e].length;h+=o)this.setPart(e,h,s[1]||i)}replaceAlphaDays(t){return t.replace(/-sun/gi,"-7").replace(/sun/gi,"0").replace(/mon/gi,"1").replace(/tue/gi,"2").replace(/wed/gi,"3").replace(/thu/gi,"4").replace(/fri/gi,"5").replace(/sat/gi,"6")}replaceAlphaMonths(t){return t.replace(/jan/gi,"1").replace(/feb/gi,"2").replace(/mar/gi,"3").replace(/apr/gi,"4").replace(/may/gi,"5").replace(/jun/gi,"6").replace(/jul/gi,"7").replace(/aug/gi,"8").replace(/sep/gi,"9").replace(/oct/gi,"10").replace(/nov/gi,"11").replace(/dec/gi,"12")}handleNicknames(t){let e=t.trim().toLowerCase();return e==="@yearly"||e==="@annually"?"0 0 1 1 *":e==="@monthly"?"0 0 1 * *":e==="@weekly"?"0 0 * * 0":e==="@daily"?"0 0 * * *":e==="@hourly"?"0 * * * *":t}setNthWeekdayOfMonth(t,e){if(typeof e!="number"&&e==="L")this.dayOfWeek[t]=this.dayOfWeek[t]|D;else if(e===C)this.dayOfWeek[t]=C;else if(e<6&&e>0)this.dayOfWeek[t]=this.dayOfWeek[t]|R[e-1];else throw new TypeError(`CronPattern: nth weekday out of range, should be 1-5 or L. Value: ${e}, Type: ${typeof e}`)}}});var M,f,l,v=g(()=>{E();S();M=[31,28,31,30,31,30,31,31,30,31,30,31],f=[["month","year",0],["day","month",-1],["hour","day",0],["minute","hour",0],["second","minute",0]],l=class r{tz;ms;second;minute;hour;day;month;year;constructor(t,e){if(this.tz=e,t&&t instanceof Date)if(!isNaN(t))this.fromDate(t);else throw new TypeError("CronDate: Invalid date passed to CronDate constructor");else if(t===void 0)this.fromDate(new Date);else if(t&&typeof t=="string")this.fromString(t);else if(t instanceof r)this.fromCronDate(t);else throw new TypeError("CronDate: Invalid type ("+typeof t+") passed to CronDate constructor")}isNthWeekdayOfMonth(t,e,n,i){let a=new Date(Date.UTC(t,e,n)).getUTCDay(),u=0;for(let o=1;o<=n;o++)new Date(Date.UTC(t,e,o)).getUTCDay()===a&&u++;if(i&C&&R[u-1]&i)return!0;if(i&D){let o=new Date(Date.UTC(t,e+1,0)).getUTCDate();for(let h=n+1;h<=o;h++)if(new Date(Date.UTC(t,e,h)).getUTCDay()===a)return!1;return!0}return!1}fromDate(t){if(this.tz!==void 0)if(typeof this.tz=="number")this.ms=t.getUTCMilliseconds(),this.second=t.getUTCSeconds(),this.minute=t.getUTCMinutes()+this.tz,this.hour=t.getUTCHours(),this.day=t.getUTCDate(),this.month=t.getUTCMonth(),this.year=t.getUTCFullYear(),this.apply();else{let e=d(t,this.tz);this.ms=t.getMilliseconds(),this.second=e.s,this.minute=e.i,this.hour=e.h,this.day=e.d,this.month=e.m-1,this.year=e.y}else this.ms=t.getMilliseconds(),this.second=t.getSeconds(),this.minute=t.getMinutes(),this.hour=t.getHours(),this.day=t.getDate(),this.month=t.getMonth(),this.year=t.getFullYear()}fromCronDate(t){this.tz=t.tz,this.year=t.year,this.month=t.month,this.day=t.day,this.hour=t.hour,this.minute=t.minute,this.second=t.second,this.ms=t.ms}apply(){if(this.month>11||this.day>M[this.month]||this.hour>59||this.minute>59||this.second>59||this.hour<0||this.minute<0||this.second<0){let t=new Date(Date.UTC(this.year,this.month,this.day,this.hour,this.minute,this.second,this.ms));return this.ms=t.getUTCMilliseconds(),this.second=t.getUTCSeconds(),this.minute=t.getUTCMinutes(),this.hour=t.getUTCHours(),this.day=t.getUTCDate(),this.month=t.getUTCMonth(),this.year=t.getUTCFullYear(),!0}else return!1}fromString(t){if(typeof this.tz=="number"){let e=_(t);this.ms=e.getUTCMilliseconds(),this.second=e.getUTCSeconds(),this.minute=e.getUTCMinutes(),this.hour=e.getUTCHours(),this.day=e.getUTCDate(),this.month=e.getUTCMonth(),this.year=e.getUTCFullYear(),this.apply()}else return this.fromDate(_(t,this.tz))}findNext(t,e,n,i){let s=this[e],a;n.lastDayOfMonth&&(this.month!==1?a=M[this.month]:a=new Date(Date.UTC(this.year,this.month+1,0,0,0,0,0)).getUTCDate());let u=!n.starDOW&&e=="day"?new Date(Date.UTC(this.year,this.month,1,0,0,0,0)).getUTCDay():void 0;for(let o=this[e]+i;o<n[e].length;o++){let h=n[e][o];if(e==="day"&&n.lastDayOfMonth&&o-i==a&&(h=1),e==="day"&&!n.starDOW){let c=n.dayOfWeek[(u+(o-i-1))%7];if(c&&c&C)c=this.isNthWeekdayOfMonth(this.year,this.month,o-i,c)?1:0;else if(c)throw new Error(`CronDate: Invalid value for dayOfWeek encountered. ${c}`);t.legacyMode&&!n.starDOM?h=h||c:h=h&&c}if(h)return this[e]=o-i,s!==this[e]?2:1}return 3}recurse(t,e,n){let i=this.findNext(e,f[n][0],t,f[n][2]);if(i>1){let s=n+1;for(;s<f.length;)this[f[s][0]]=-f[s][2],s++;if(i===3)return this[f[n][1]]++,this[f[n][0]]=-f[n][2],this.apply(),this.recurse(t,e,0);if(this.apply())return this.recurse(t,e,n-1)}return n+=1,n>=f.length?this:this.year>=3e3?null:this.recurse(t,e,n)}increment(t,e,n){return this.second+=e.interval!==void 0&&e.interval>1&&n?e.interval:1,this.ms=0,this.apply(),this.recurse(t,e,0)}getDate(t){return t||this.tz===void 0?new Date(this.year,this.month,this.day,this.hour,this.minute,this.second,this.ms):typeof this.tz=="number"?new Date(Date.UTC(this.year,this.month,this.day,this.hour,this.minute-this.tz,this.second,this.ms)):x(w(this.year,this.month+1,this.day,this.hour,this.minute,this.second,this.tz),!1)}getTime(){return this.getDate(!1).getTime()}}});function U(r){if(r===void 0&&(r={}),delete r.name,r.legacyMode=r.legacyMode===void 0?!0:r.legacyMode,r.paused=r.paused===void 0?!1:r.paused,r.maxRuns=r.maxRuns===void 0?1/0:r.maxRuns,r.catch=r.catch===void 0?!1:r.catch,r.interval=r.interval===void 0?0:parseInt(r.interval.toString(),10),r.utcOffset=r.utcOffset===void 0?void 0:parseInt(r.utcOffset.toString(),10),r.unref=r.unref===void 0?!1:r.unref,r.startAt&&(r.startAt=new l(r.startAt,r.timezone)),r.stopAt&&(r.stopAt=new l(r.stopAt,r.timezone)),r.interval!==null){if(isNaN(r.interval))throw new Error("CronOptions: Supplied value for interval is not a number");if(r.interval<0)throw new Error("CronOptions: Supplied value for interval can not be negative")}if(r.utcOffset!==void 0){if(isNaN(r.utcOffset))throw new Error("CronOptions: Invalid value passed for utcOffset, should be number representing minutes offset from UTC.");if(r.utcOffset<-870||r.utcOffset>870)throw new Error("CronOptions: utcOffset out of bounds.");if(r.utcOffset!==void 0&&r.timezone)throw new Error("CronOptions: Combining 'utcOffset' with 'timezone' is not allowed.")}if(r.unref!==!0&&r.unref!==!1)throw new Error("CronOptions: Unref should be either true, false or undefined(false).");return r}var A=g(()=>{v()});function y(r){return Object.prototype.toString.call(r)==="[object Function]"||typeof r=="function"||r instanceof Function}function I(r){return y(r)}function z(r){typeof Deno<"u"&&typeof Deno.unrefTimer<"u"?Deno.unrefTimer(r):r&&typeof r.unref<"u"&&r.unref()}var W=g(()=>{});var F,O,P,j=g(()=>{v();S();A();W();F=30*1e3,O=[],P=class{name;options;_states;fn;constructor(t,e,n){let i,s;if(y(e))s=e;else if(typeof e=="object")i=e;else if(e!==void 0)throw new Error("Cron: Invalid argument passed for optionsIn. Should be one of function, or object (options).");if(y(n))s=n;else if(typeof n=="object")i=n;else if(n!==void 0)throw new Error("Cron: Invalid argument passed for funcIn. Should be one of function, or object (options).");if(this.name=i?.name,this.options=U(i),this._states={kill:!1,blocking:!1,previousRun:void 0,currentRun:void 0,once:void 0,currentTimeout:void 0,maxRuns:i?i.maxRuns:void 0,paused:i?i.paused:!1,pattern:new T("* * * * *")},t&&(t instanceof Date||typeof t=="string"&&t.indexOf(":")>0)?this._states.once=new l(t,this.options.timezone||this.options.utcOffset):this._states.pattern=new T(t,this.options.timezone),this.name){if(O.find(u=>u.name===this.name))throw new Error("Cron: Tried to initialize new named job '"+this.name+"', but name already taken.");O.push(this)}return s!==void 0&&I(s)&&(this.fn=s,this.schedule()),this}nextRun(t){let e=this._next(t);return e?e.getDate(!1):null}nextRuns(t,e){this._states.maxRuns!==void 0&&t>this._states.maxRuns&&(t=this._states.maxRuns);let n=[],i=e||this._states.currentRun||void 0;for(;t--&&(i=this.nextRun(i));)n.push(i);return n}getPattern(){return this._states.pattern?this._states.pattern.pattern:void 0}isRunning(){let t=this.nextRun(this._states.currentRun),e=!this._states.paused,n=this.fn!==void 0,i=!this._states.kill;return e&&n&&i&&t!==null}isStopped(){return this._states.kill}isBusy(){return this._states.blocking}currentRun(){return this._states.currentRun?this._states.currentRun.getDate():null}previousRun(){return this._states.previousRun?this._states.previousRun.getDate():null}msToNext(t){let e=this._next(t);return e?t instanceof l||t instanceof Date?e.getTime()-t.getTime():e.getTime()-new l(t).getTime():null}stop(){this._states.kill=!0,this._states.currentTimeout&&clearTimeout(this._states.currentTimeout);let t=O.indexOf(this);t>=0&&O.splice(t,1)}pause(){return this._states.paused=!0,!this._states.kill}resume(){return this._states.paused=!1,!this._states.kill}schedule(t){if(t&&this.fn)throw new Error("Cron: It is not allowed to schedule two functions using the same Croner instance.");t&&(this.fn=t);let e=this.msToNext(),n=this.nextRun(this._states.currentRun);return e==null||isNaN(e)||n===null?this:(e>F&&(e=F),this._states.currentTimeout=setTimeout(()=>this._checkTrigger(n),e),this._states.currentTimeout&&this.options.unref&&z(this._states.currentTimeout),this)}async _trigger(t){if(this._states.blocking=!0,this._states.currentRun=new l(void 0,this.options.timezone||this.options.utcOffset),this.options.catch)try{this.fn!==void 0&&await this.fn(this,this.options.context)}catch(e){y(this.options.catch)&&this.options.catch(e,this)}else this.fn!==void 0&&await this.fn(this,this.options.context);this._states.previousRun=new l(t,this.options.timezone||this.options.utcOffset),this._states.blocking=!1}async trigger(){await this._trigger()}runsLeft(){return this._states.maxRuns}_checkTrigger(t){let e=new Date,n=!this._states.paused&&e.getTime()>=t.getTime(),i=this._states.blocking&&this.options.protect;n&&!i?(this._states.maxRuns!==void 0&&this._states.maxRuns--,this._trigger()):n&&i&&y(this.options.protect)&&setTimeout(()=>this.options.protect(this),0),this.schedule()}_next(t){let e=!!(t||this._states.currentRun),n=!1;!t&&this.options.startAt&&this.options.interval&&([t,e]=this._calculatePreviousRun(t,e),n=!t),t=new l(t,this.options.timezone||this.options.utcOffset),this.options.startAt&&t&&t.getTime()<this.options.startAt.getTime()&&(t=this.options.startAt);let i=this._states.once||new l(t,this.options.timezone||this.options.utcOffset);return!n&&i!==this._states.once&&(i=i.increment(this._states.pattern,this.options,e)),this._states.once&&this._states.once.getTime()<=t.getTime()||i===null||this._states.maxRuns!==void 0&&this._states.maxRuns<=0||this._states.kill||this.options.stopAt&&i.getTime()>=this.options.stopAt.getTime()?null:i}_calculatePreviousRun(t,e){let n=new l(void 0,this.options.timezone||this.options.utcOffset),i=t;if(this.options.startAt.getTime()<=n.getTime()){i=this.options.startAt;let s=i.getTime()+this.options.interval*1e3;for(;s<=n.getTime();)i=new l(i,this.options.timezone||this.options.utcOffset).increment(this._states.pattern,this.options,!0),s=i.getTime()+this.options.interval*1e3;e=!0}return i===null&&(i=void 0),[i,e]}}});var Y=H((at,L)=>{j();L.exports=P});return Y();})();
|
|
1
|
+
var Cron=(()=>{var p=(r,e)=>()=>(r&&(e=r(r=0)),e);var Y=(r,e)=>()=>(e||r((e={exports:{}}).exports,e),e.exports);function C(r){return Date.UTC(r.y,r.m-1,r.d,r.h,r.i,r.s)}function O(r,e){return r.y===e.y&&r.m===e.m&&r.d===e.d&&r.h===e.h&&r.i===e.i&&r.s===e.s}function j(r,e){let t=new Date(Date.parse(r));if(isNaN(t))throw new Error("Invalid ISO8601 passed to timezone parser.");let n=r.substring(9);return n.includes("Z")||n.includes("+")||n.includes("-")?b(t.getUTCFullYear(),t.getUTCMonth()+1,t.getUTCDate(),t.getUTCHours(),t.getUTCMinutes(),t.getUTCSeconds(),"Etc/UTC"):b(t.getFullYear(),t.getMonth()+1,t.getDate(),t.getHours(),t.getMinutes(),t.getSeconds(),e)}function k(r,e,t){return P(j(r,e),t)}function P(r,e){let t=new Date(C(r)),n=g(t,r.tz),i=C(r),s=C(n),a=i-s,h=new Date(t.getTime()+a),o=g(h,r.tz);if(O(o,r)){let m=new Date(h.getTime()-36e5),d=g(m,r.tz);return O(d,r)?m:h}let u=new Date(h.getTime()+C(r)-C(o)),l=g(u,r.tz);if(O(l,r))return u;if(e)throw new Error("Invalid date passed to fromTZ()");return h.getTime()>u.getTime()?h:u}function g(r,e){let n=new Intl.DateTimeFormat("en-US",{timeZone:e,year:"numeric",month:"numeric",day:"numeric",hour:"numeric",minute:"numeric",second:"numeric",hour12:!1}).formatToParts(r),i={year:0,month:0,day:0,hour:0,minute:0,second:0};for(let s of n)(s.type==="year"||s.type==="month"||s.type==="day"||s.type==="hour"||s.type==="minute"||s.type==="second")&&(i[s.type]=parseInt(s.value,10));return i.hour===24&&(i.hour=0),{y:i.year,m:i.month,d:i.day,h:i.hour,i:i.minute,s:i.second,tz:e}}function b(r,e,t,n,i,s,a){return{y:r,m:e,d:t,h:n,i,s,tz:a}}var _=p(()=>{});var N,T,E=p(()=>{N=[1,2,4,8,16],T=class{pattern;timezone;second;minute;hour;day;month;dayOfWeek;year;lastDayOfMonth;nearestWeekdays;starDOM;starDOW;starYear;useAndLogic;constructor(e,t){this.pattern=e,this.timezone=t,this.second=Array(60).fill(0),this.minute=Array(60).fill(0),this.hour=Array(24).fill(0),this.day=Array(31).fill(0),this.month=Array(12).fill(0),this.dayOfWeek=Array(7).fill(0),this.year=Array(1e4).fill(0),this.lastDayOfMonth=!1,this.nearestWeekdays=Array(31).fill(0),this.starDOM=!1,this.starDOW=!1,this.starYear=!1,this.useAndLogic=!1,this.parse()}parse(){if(!(typeof this.pattern=="string"||this.pattern instanceof String))throw new TypeError("CronPattern: Pattern has to be of type string.");this.pattern.indexOf("@")>=0&&(this.pattern=this.handleNicknames(this.pattern).trim());let e=this.pattern.match(/\S+/g)||[""];if(e.length<5||e.length>7)throw new TypeError("CronPattern: invalid configuration format ('"+this.pattern+"'), exactly five, six, or seven space separated parts are required.");if(e.length===5&&e.unshift("0"),e.length===6&&e.push("*"),e[3].indexOf("L")>=0&&(e[3]=e[3].replace("L",""),this.lastDayOfMonth=!0),e[3]=="*"&&(this.starDOM=!0),e[6]=="*"&&(this.starYear=!0),e[4].length>=3&&(e[4]=this.replaceAlphaMonths(e[4])),e[5].length>=3&&(e[5]=this.replaceAlphaDays(e[5])),e[5].startsWith("+")&&(this.useAndLogic=!0,e[5]=e[5].substring(1),e[5]===""))throw new TypeError("CronPattern: Day-of-week field cannot be empty after '+' modifier.");e[5]=="*"&&(this.starDOW=!0),this.pattern.indexOf("?")>=0&&(e[0]=e[0].replace(/\?/g,"*"),e[1]=e[1].replace(/\?/g,"*"),e[2]=e[2].replace(/\?/g,"*"),e[3]=e[3].replace(/\?/g,"*"),e[4]=e[4].replace(/\?/g,"*"),e[5]=e[5].replace(/\?/g,"*"),e[6]&&(e[6]=e[6].replace(/\?/g,"*"))),this.throwAtIllegalCharacters(e),this.partToArray("second",e[0],0,1),this.partToArray("minute",e[1],0,1),this.partToArray("hour",e[2],0,1),this.partToArray("day",e[3],-1,1),this.partToArray("month",e[4],-1,1),this.partToArray("dayOfWeek",e[5],0,63),this.partToArray("year",e[6],0,1),this.dayOfWeek[7]&&(this.dayOfWeek[0]=this.dayOfWeek[7])}partToArray(e,t,n,i){let s=this[e],a=e==="day"&&this.lastDayOfMonth;if(t===""&&!a)throw new TypeError("CronPattern: configuration entry "+e+" ("+t+") is empty, check for trailing spaces.");if(t==="*")return s.fill(i);let h=t.split(",");if(h.length>1)for(let o=0;o<h.length;o++)this.partToArray(e,h[o],n,i);else t.indexOf("-")!==-1&&t.indexOf("/")!==-1?this.handleRangeWithStepping(t,e,n,i):t.indexOf("-")!==-1?this.handleRange(t,e,n,i):t.indexOf("/")!==-1?this.handleStepping(t,e,n,i):t!==""&&this.handleNumber(t,e,n,i)}throwAtIllegalCharacters(e){for(let t=0;t<e.length;t++)if((t===3?/[^/*0-9,-WL]+/:t===5?/[^/*0-9,\-#L]+/:/[^/*0-9,-]+/).test(e[t]))throw new TypeError("CronPattern: configuration entry "+t+" ("+e[t]+") contains illegal characters.")}handleNumber(e,t,n,i){let s=this.extractNth(e,t),a=e.toUpperCase().includes("W");if(t!=="day"&&a)throw new TypeError("CronPattern: Nearest weekday modifier (W) only allowed in day-of-month.");a&&(t="nearestWeekdays");let h=parseInt(s[0],10)+n;if(isNaN(h))throw new TypeError("CronPattern: "+t+" is not a number: '"+e+"'");this.setPart(t,h,s[1]||i)}setPart(e,t,n){if(!Object.prototype.hasOwnProperty.call(this,e))throw new TypeError("CronPattern: Invalid part specified: "+e);if(e==="dayOfWeek"){if(t===7&&(t=0),t<0||t>6)throw new RangeError("CronPattern: Invalid value for dayOfWeek: "+t);this.setNthWeekdayOfMonth(t,n);return}if(e==="second"||e==="minute"){if(t<0||t>=60)throw new RangeError("CronPattern: Invalid value for "+e+": "+t)}else if(e==="hour"){if(t<0||t>=24)throw new RangeError("CronPattern: Invalid value for "+e+": "+t)}else if(e==="day"||e==="nearestWeekdays"){if(t<0||t>=31)throw new RangeError("CronPattern: Invalid value for "+e+": "+t)}else if(e==="month"){if(t<0||t>=12)throw new RangeError("CronPattern: Invalid value for "+e+": "+t)}else if(e==="year"&&(t<1||t>=1e4))throw new RangeError("CronPattern: Invalid value for "+e+": "+t+" (supported range: 1-9999)");this[e][t]=n}handleRangeWithStepping(e,t,n,i){if(e.toUpperCase().includes("W"))throw new TypeError("CronPattern: Syntax error, W is not allowed in ranges with stepping.");let s=this.extractNth(e,t),a=s[0].match(/^(\d+)-(\d+)\/(\d+)$/);if(a===null)throw new TypeError("CronPattern: Syntax error, illegal range with stepping: '"+e+"'");let[,h,o,u]=a,l=parseInt(h,10)+n,m=parseInt(o,10)+n,d=parseInt(u,10);if(isNaN(l))throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)");if(isNaN(m))throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)");if(isNaN(d))throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");if(d===0)throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");if(d>this[t].length)throw new TypeError("CronPattern: Syntax error, steps cannot be greater than maximum value of part ("+this[t].length+")");if(l>m)throw new TypeError("CronPattern: From value is larger than to value: '"+e+"'");for(let v=l;v<=m;v+=d)this.setPart(t,v,s[1]||i)}extractNth(e,t){let n=e,i;if(n.includes("#")){if(t!=="dayOfWeek")throw new Error("CronPattern: nth (#) only allowed in day-of-week field");i=n.split("#")[1],n=n.split("#")[0]}return[n,i]}handleRange(e,t,n,i){if(e.toUpperCase().includes("W"))throw new TypeError("CronPattern: Syntax error, W is not allowed in a range.");let s=this.extractNth(e,t),a=s[0].split("-");if(a.length!==2)throw new TypeError("CronPattern: Syntax error, illegal range: '"+e+"'");let h=parseInt(a[0],10)+n,o=parseInt(a[1],10)+n;if(isNaN(h))throw new TypeError("CronPattern: Syntax error, illegal lower range (NaN)");if(isNaN(o))throw new TypeError("CronPattern: Syntax error, illegal upper range (NaN)");if(h>o)throw new TypeError("CronPattern: From value is larger than to value: '"+e+"'");for(let u=h;u<=o;u++)this.setPart(t,u,s[1]||i)}handleStepping(e,t,n,i){if(e.toUpperCase().includes("W"))throw new TypeError("CronPattern: Syntax error, W is not allowed in parts with stepping.");let s=this.extractNth(e,t),a=s[0].split("/");if(a.length!==2)throw new TypeError("CronPattern: Syntax error, illegal stepping: '"+e+"'");a[0]===""&&(a[0]="*");let h=0;a[0]!=="*"&&(h=parseInt(a[0],10)+n);let o=parseInt(a[1],10);if(isNaN(o))throw new TypeError("CronPattern: Syntax error, illegal stepping: (NaN)");if(o===0)throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");if(o>this[t].length)throw new TypeError("CronPattern: Syntax error, max steps for part is ("+this[t].length+")");for(let u=h;u<this[t].length;u+=o)this.setPart(t,u,s[1]||i)}replaceAlphaDays(e){return e.replace(/-sun/gi,"-7").replace(/sun/gi,"0").replace(/mon/gi,"1").replace(/tue/gi,"2").replace(/wed/gi,"3").replace(/thu/gi,"4").replace(/fri/gi,"5").replace(/sat/gi,"6")}replaceAlphaMonths(e){return e.replace(/jan/gi,"1").replace(/feb/gi,"2").replace(/mar/gi,"3").replace(/apr/gi,"4").replace(/may/gi,"5").replace(/jun/gi,"6").replace(/jul/gi,"7").replace(/aug/gi,"8").replace(/sep/gi,"9").replace(/oct/gi,"10").replace(/nov/gi,"11").replace(/dec/gi,"12")}handleNicknames(e){let t=e.trim().toLowerCase();if(t==="@yearly"||t==="@annually")return"0 0 1 1 *";if(t==="@monthly")return"0 0 1 * *";if(t==="@weekly")return"0 0 * * 0";if(t==="@daily"||t==="@midnight")return"0 0 * * *";if(t==="@hourly")return"0 * * * *";if(t==="@reboot")throw new TypeError("CronPattern: @reboot is not supported in this environment. This is an event-based trigger that requires system startup detection.");return e}setNthWeekdayOfMonth(e,t){if(typeof t!="number"&&t==="L")this.dayOfWeek[e]=this.dayOfWeek[e]|32;else if(t===63)this.dayOfWeek[e]=63;else if(t<6&&t>0)this.dayOfWeek[e]=this.dayOfWeek[e]|N[t-1];else throw new TypeError(`CronPattern: nth weekday out of range, should be 1-5 or L. Value: ${t}, Type: ${typeof t}`)}}});var U,c,f,R=p(()=>{_();E();U=[31,28,31,30,31,30,31,31,30,31,30,31],c=[["month","year",0],["day","month",-1],["hour","day",0],["minute","hour",0],["second","minute",0]],f=class r{tz;ms;second;minute;hour;day;month;year;constructor(e,t){if(this.tz=t,e&&e instanceof Date)if(!isNaN(e))this.fromDate(e);else throw new TypeError("CronDate: Invalid date passed to CronDate constructor");else if(e===void 0)this.fromDate(new Date);else if(e&&typeof e=="string")this.fromString(e);else if(e instanceof r)this.fromCronDate(e);else throw new TypeError("CronDate: Invalid type ("+typeof e+") passed to CronDate constructor")}getNearestWeekday(e,t,n){let s=new Date(Date.UTC(e,t,n)).getUTCDay();if(s===0){let a=new Date(Date.UTC(e,t+1,0)).getUTCDate();return n===a?n-2:n+1}return s===6?n===1?n+2:n-1:n}isNthWeekdayOfMonth(e,t,n,i){let a=new Date(Date.UTC(e,t,n)).getUTCDay(),h=0;for(let o=1;o<=n;o++)new Date(Date.UTC(e,t,o)).getUTCDay()===a&&h++;if(i&63&&N[h-1]&i)return!0;if(i&32){let o=new Date(Date.UTC(e,t+1,0)).getUTCDate();for(let u=n+1;u<=o;u++)if(new Date(Date.UTC(e,t,u)).getUTCDay()===a)return!1;return!0}return!1}fromDate(e){if(this.tz!==void 0)if(typeof this.tz=="number")this.ms=e.getUTCMilliseconds(),this.second=e.getUTCSeconds(),this.minute=e.getUTCMinutes()+this.tz,this.hour=e.getUTCHours(),this.day=e.getUTCDate(),this.month=e.getUTCMonth(),this.year=e.getUTCFullYear(),this.apply();else{let t=g(e,this.tz);this.ms=e.getMilliseconds(),this.second=t.s,this.minute=t.i,this.hour=t.h,this.day=t.d,this.month=t.m-1,this.year=t.y}else this.ms=e.getMilliseconds(),this.second=e.getSeconds(),this.minute=e.getMinutes(),this.hour=e.getHours(),this.day=e.getDate(),this.month=e.getMonth(),this.year=e.getFullYear()}fromCronDate(e){this.tz=e.tz,this.year=e.year,this.month=e.month,this.day=e.day,this.hour=e.hour,this.minute=e.minute,this.second=e.second,this.ms=e.ms}apply(){if(this.month>11||this.day>U[this.month]||this.hour>59||this.minute>59||this.second>59||this.hour<0||this.minute<0||this.second<0){let e=new Date(Date.UTC(this.year,this.month,this.day,this.hour,this.minute,this.second,this.ms));return this.ms=e.getUTCMilliseconds(),this.second=e.getUTCSeconds(),this.minute=e.getUTCMinutes(),this.hour=e.getUTCHours(),this.day=e.getUTCDate(),this.month=e.getUTCMonth(),this.year=e.getUTCFullYear(),!0}else return!1}fromString(e){if(typeof this.tz=="number"){let t=k(e);this.ms=t.getUTCMilliseconds(),this.second=t.getUTCSeconds(),this.minute=t.getUTCMinutes(),this.hour=t.getUTCHours(),this.day=t.getUTCDate(),this.month=t.getUTCMonth(),this.year=t.getUTCFullYear(),this.apply()}else return this.fromDate(k(e,this.tz))}findNext(e,t,n,i){let s=this[t],a;n.lastDayOfMonth&&(this.month!==1?a=U[this.month]:a=new Date(Date.UTC(this.year,this.month+1,0,0,0,0,0)).getUTCDate());let h=!n.starDOW&&t=="day"?new Date(Date.UTC(this.year,this.month,1,0,0,0,0)).getUTCDay():void 0;for(let o=this[t]+i;o<n[t].length;o++){let u=n[t][o];if(t==="day"&&!u){for(let l=0;l<n.nearestWeekdays.length;l++)if(n.nearestWeekdays[l]&&this.getNearestWeekday(this.year,this.month,l-i)===o-i){u=1;break}}if(t==="day"&&n.lastDayOfMonth&&o-i==a&&(u=1),t==="day"&&!n.starDOW){let l=n.dayOfWeek[(h+(o-i-1))%7];if(l&&l&63)l=this.isNthWeekdayOfMonth(this.year,this.month,o-i,l)?1:0;else if(l)throw new Error(`CronDate: Invalid value for dayOfWeek encountered. ${l}`);n.useAndLogic?u=u&&l:e.legacyMode&&!n.starDOM?u=u||l:u=u&&l}if(u)return this[t]=o-i,s!==this[t]?2:1}return 3}recurse(e,t,n){if(n===0&&!e.starYear){if(this.year>=0&&this.year<e.year.length&&e.year[this.year]===0){let s=-1;for(let a=this.year+1;a<e.year.length&&a<1e4;a++)if(e.year[a]===1){s=a;break}if(s===-1)return null;this.year=s,this.month=0,this.day=1,this.hour=0,this.minute=0,this.second=0,this.ms=0}if(this.year>=1e4)return null}let i=this.findNext(t,c[n][0],e,c[n][2]);if(i>1){let s=n+1;for(;s<c.length;)this[c[s][0]]=-c[s][2],s++;if(i===3){if(this[c[n][1]]++,this[c[n][0]]=-c[n][2],this.apply(),n===0&&!e.starYear){for(;this.year>=0&&this.year<e.year.length&&e.year[this.year]===0&&this.year<1e4;)this.year++;if(this.year>=1e4||this.year>=e.year.length)return null}return this.recurse(e,t,0)}else if(this.apply())return this.recurse(e,t,n-1)}return n+=1,n>=c.length?this:(e.starYear?this.year>=3e3:this.year>=1e4)?null:this.recurse(e,t,n)}increment(e,t,n){return this.second+=t.interval!==void 0&&t.interval>1&&n?t.interval:1,this.ms=0,this.apply(),this.recurse(e,t,0)}getDate(e){return e||this.tz===void 0?new Date(this.year,this.month,this.day,this.hour,this.minute,this.second,this.ms):typeof this.tz=="number"?new Date(Date.UTC(this.year,this.month,this.day,this.hour,this.minute-this.tz,this.second,this.ms)):P(b(this.year,this.month+1,this.day,this.hour,this.minute,this.second,this.tz),!1)}getTime(){return this.getDate(!1).getTime()}}});function S(r){if(r===void 0&&(r={}),delete r.name,r.legacyMode=r.legacyMode===void 0?!0:r.legacyMode,r.paused=r.paused===void 0?!1:r.paused,r.maxRuns=r.maxRuns===void 0?1/0:r.maxRuns,r.catch=r.catch===void 0?!1:r.catch,r.interval=r.interval===void 0?0:parseInt(r.interval.toString(),10),r.utcOffset=r.utcOffset===void 0?void 0:parseInt(r.utcOffset.toString(),10),r.unref=r.unref===void 0?!1:r.unref,r.startAt&&(r.startAt=new f(r.startAt,r.timezone)),r.stopAt&&(r.stopAt=new f(r.stopAt,r.timezone)),r.interval!==null){if(isNaN(r.interval))throw new Error("CronOptions: Supplied value for interval is not a number");if(r.interval<0)throw new Error("CronOptions: Supplied value for interval can not be negative")}if(r.utcOffset!==void 0){if(isNaN(r.utcOffset))throw new Error("CronOptions: Invalid value passed for utcOffset, should be number representing minutes offset from UTC.");if(r.utcOffset<-870||r.utcOffset>870)throw new Error("CronOptions: utcOffset out of bounds.");if(r.utcOffset!==void 0&&r.timezone)throw new Error("CronOptions: Combining 'utcOffset' with 'timezone' is not allowed.")}if(r.unref!==!0&&r.unref!==!1)throw new Error("CronOptions: Unref should be either true, false or undefined(false).");return r}var M=p(()=>{R()});function y(r){return Object.prototype.toString.call(r)==="[object Function]"||typeof r=="function"||r instanceof Function}function A(r){return y(r)}function W(r){typeof Deno<"u"&&typeof Deno.unrefTimer<"u"?Deno.unrefTimer(r):r&&typeof r.unref<"u"&&r.unref()}var I=p(()=>{});var z,w,D,F=p(()=>{R();E();M();I();z=30*1e3,w=[],D=class{name;options;_states;fn;constructor(e,t,n){let i,s;if(y(t))s=t;else if(typeof t=="object")i=t;else if(t!==void 0)throw new Error("Cron: Invalid argument passed for optionsIn. Should be one of function, or object (options).");if(y(n))s=n;else if(typeof n=="object")i=n;else if(n!==void 0)throw new Error("Cron: Invalid argument passed for funcIn. Should be one of function, or object (options).");if(this.name=i?.name,this.options=S(i),this._states={kill:!1,blocking:!1,previousRun:void 0,currentRun:void 0,once:void 0,currentTimeout:void 0,maxRuns:i?i.maxRuns:void 0,paused:i?i.paused:!1,pattern:new T("* * * * *")},e&&(e instanceof Date||typeof e=="string"&&e.indexOf(":")>0)?this._states.once=new f(e,this.options.timezone||this.options.utcOffset):this._states.pattern=new T(e,this.options.timezone),this.name){if(w.find(h=>h.name===this.name))throw new Error("Cron: Tried to initialize new named job '"+this.name+"', but name already taken.");w.push(this)}return s!==void 0&&A(s)&&(this.fn=s,this.schedule()),this}nextRun(e){let t=this._next(e);return t?t.getDate(!1):null}nextRuns(e,t){this._states.maxRuns!==void 0&&e>this._states.maxRuns&&(e=this._states.maxRuns);let n=[],i=t||this._states.currentRun||void 0;for(;e--&&(i=this.nextRun(i));)n.push(i);return n}getPattern(){return this._states.pattern?this._states.pattern.pattern:void 0}isRunning(){let e=this.nextRun(this._states.currentRun),t=!this._states.paused,n=this.fn!==void 0,i=!this._states.kill;return t&&n&&i&&e!==null}isStopped(){return this._states.kill}isBusy(){return this._states.blocking}currentRun(){return this._states.currentRun?this._states.currentRun.getDate():null}previousRun(){return this._states.previousRun?this._states.previousRun.getDate():null}msToNext(e){let t=this._next(e);return t?e instanceof f||e instanceof Date?t.getTime()-e.getTime():t.getTime()-new f(e).getTime():null}stop(){this._states.kill=!0,this._states.currentTimeout&&clearTimeout(this._states.currentTimeout);let e=w.indexOf(this);e>=0&&w.splice(e,1)}pause(){return this._states.paused=!0,!this._states.kill}resume(){return this._states.paused=!1,!this._states.kill}schedule(e){if(e&&this.fn)throw new Error("Cron: It is not allowed to schedule two functions using the same Croner instance.");e&&(this.fn=e);let t=this.msToNext(),n=this.nextRun(this._states.currentRun);return t==null||isNaN(t)||n===null?this:(t>z&&(t=z),this._states.currentTimeout=setTimeout(()=>this._checkTrigger(n),t),this._states.currentTimeout&&this.options.unref&&W(this._states.currentTimeout),this)}async _trigger(e){if(this._states.blocking=!0,this._states.currentRun=new f(void 0,this.options.timezone||this.options.utcOffset),this.options.catch)try{this.fn!==void 0&&await this.fn(this,this.options.context)}catch(t){y(this.options.catch)&&this.options.catch(t,this)}else this.fn!==void 0&&await this.fn(this,this.options.context);this._states.previousRun=new f(e,this.options.timezone||this.options.utcOffset),this._states.blocking=!1}async trigger(){await this._trigger()}runsLeft(){return this._states.maxRuns}_checkTrigger(e){let t=new Date,n=!this._states.paused&&t.getTime()>=e.getTime(),i=this._states.blocking&&this.options.protect;n&&!i?(this._states.maxRuns!==void 0&&this._states.maxRuns--,this._trigger()):n&&i&&y(this.options.protect)&&setTimeout(()=>this.options.protect(this),0),this.schedule()}_next(e){let t=!!(e||this._states.currentRun),n=!1;!e&&this.options.startAt&&this.options.interval&&([e,t]=this._calculatePreviousRun(e,t),n=!e),e=new f(e,this.options.timezone||this.options.utcOffset),this.options.startAt&&e&&e.getTime()<this.options.startAt.getTime()&&(e=this.options.startAt);let i=this._states.once||new f(e,this.options.timezone||this.options.utcOffset);return!n&&i!==this._states.once&&(i=i.increment(this._states.pattern,this.options,t)),this._states.once&&this._states.once.getTime()<=e.getTime()||i===null||this._states.maxRuns!==void 0&&this._states.maxRuns<=0||this._states.kill||this.options.stopAt&&i.getTime()>=this.options.stopAt.getTime()?null:i}_calculatePreviousRun(e,t){let n=new f(void 0,this.options.timezone||this.options.utcOffset),i=e;if(this.options.startAt.getTime()<=n.getTime()){i=this.options.startAt;let s=i.getTime()+this.options.interval*1e3;for(;s<=n.getTime();)i=new f(i,this.options.timezone||this.options.utcOffset).increment(this._states.pattern,this.options,!0),s=i.getTime()+this.options.interval*1e3;t=!0}return i===null&&(i=void 0),[i,t]}}});var H=Y((ae,L)=>{F();L.exports=D});return H();})();
|
package/dist/date.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { type CronPattern } from "./pattern.ts";
|
|
|
6
6
|
* @param d Input date, if using string representation ISO 8001 (2015-11-24T19:40:00) local timezone is expected
|
|
7
7
|
* @param tz String representation of target timezone in Europe/Stockholm format, or a number representing offset in minutes.
|
|
8
8
|
*/
|
|
9
|
-
declare class CronDate {
|
|
9
|
+
declare class CronDate<T = undefined> {
|
|
10
10
|
tz: string | number | undefined;
|
|
11
11
|
/**
|
|
12
12
|
* Current milliseconds
|
|
@@ -42,7 +42,17 @@ declare class CronDate {
|
|
|
42
42
|
* Current full year, in local time or target timezone specified by `this.tz`
|
|
43
43
|
*/
|
|
44
44
|
year: number;
|
|
45
|
-
constructor(d?: CronDate | Date | string | null, tz?: string | number);
|
|
45
|
+
constructor(d?: CronDate<T> | Date | string | null, tz?: string | number);
|
|
46
|
+
/**
|
|
47
|
+
* Calculates the nearest weekday (Mon-Fri) to a given day of the month.
|
|
48
|
+
* Handles month boundaries.
|
|
49
|
+
*
|
|
50
|
+
* @param year The target year.
|
|
51
|
+
* @param month The target month (0-11).
|
|
52
|
+
* @param day The target day (1-31).
|
|
53
|
+
* @returns The day of the month (1-31) that is the nearest weekday.
|
|
54
|
+
*/
|
|
55
|
+
private getNearestWeekday;
|
|
46
56
|
/**
|
|
47
57
|
* Check if the given date is the nth occurrence of a weekday in its month.
|
|
48
58
|
*
|
|
@@ -86,9 +96,9 @@ declare class CronDate {
|
|
|
86
96
|
* approach to handle the dependencies between different components. For example,
|
|
87
97
|
* if the day changes, the hour, minute, and second need to be reset.
|
|
88
98
|
*
|
|
89
|
-
* The recursion is
|
|
90
|
-
* infinite loops or excessive stack depth
|
|
91
|
-
*
|
|
99
|
+
* The recursion is limited to the year 10000 to prevent potential
|
|
100
|
+
* infinite loops or excessive stack depth, and to match the maximum supported
|
|
101
|
+
* year in OCPS 1.2 (years 1-9999).
|
|
92
102
|
*
|
|
93
103
|
* @param pattern The cron pattern used to determine the next run time.
|
|
94
104
|
* @param options The cron options that influence the incrementing behavior.
|
|
@@ -96,7 +106,7 @@ declare class CronDate {
|
|
|
96
106
|
* date component being processed. 0 represents "month", 1 represents "day", etc.
|
|
97
107
|
*
|
|
98
108
|
* @returns This `CronDate` instance for chaining, or null if incrementing
|
|
99
|
-
* was not possible (e.g., reached year
|
|
109
|
+
* was not possible (e.g., reached year 10000 limit or no matching date).
|
|
100
110
|
*
|
|
101
111
|
* @private
|
|
102
112
|
*/
|
|
@@ -109,7 +119,7 @@ declare class CronDate {
|
|
|
109
119
|
* @param hasPreviousRun True if there was a previous run, false otherwise. This is used to determine whether to apply the minimum interval.
|
|
110
120
|
* @returns This CronDate instance for chaining, or null if incrementing was not possible (e.g., reached year 3000 limit).
|
|
111
121
|
*/
|
|
112
|
-
increment(pattern: CronPattern, options: CronOptions
|
|
122
|
+
increment(pattern: CronPattern, options: CronOptions<T>, hasPreviousRun: boolean): CronDate<T> | null;
|
|
113
123
|
/**
|
|
114
124
|
* Convert current state back to a javascript Date()
|
|
115
125
|
*
|
package/dist/options.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ type ProtectCallbackFn = (job: Cron) => void;
|
|
|
7
7
|
*
|
|
8
8
|
* @interface
|
|
9
9
|
*/
|
|
10
|
-
interface CronOptions {
|
|
10
|
+
interface CronOptions<T = undefined> {
|
|
11
11
|
/**
|
|
12
12
|
* The name of the cron job. If provided, the job will be added to the
|
|
13
13
|
* `scheduledJobs` array, allowing it to be accessed by name.
|
|
@@ -54,11 +54,11 @@ interface CronOptions {
|
|
|
54
54
|
/**
|
|
55
55
|
* The date and time at which the job should start running.
|
|
56
56
|
*/
|
|
57
|
-
startAt?: string | Date | CronDate
|
|
57
|
+
startAt?: string | Date | CronDate<T>;
|
|
58
58
|
/**
|
|
59
59
|
* The date and time at which the job should stop running.
|
|
60
60
|
*/
|
|
61
|
-
stopAt?: string | Date | CronDate
|
|
61
|
+
stopAt?: string | Date | CronDate<T>;
|
|
62
62
|
/**
|
|
63
63
|
* The timezone for the cron job.
|
|
64
64
|
*/
|
|
@@ -75,7 +75,7 @@ interface CronOptions {
|
|
|
75
75
|
/**
|
|
76
76
|
* An optional context object that will be passed to the job function.
|
|
77
77
|
*/
|
|
78
|
-
context?:
|
|
78
|
+
context?: T;
|
|
79
79
|
}
|
|
80
80
|
/**
|
|
81
81
|
* Processes and validates cron options.
|
|
@@ -84,5 +84,5 @@ interface CronOptions {
|
|
|
84
84
|
* @returns The processed and validated cron options.
|
|
85
85
|
* @throws {Error} If any of the options are invalid.
|
|
86
86
|
*/
|
|
87
|
-
declare function CronOptionsHandler(options?: CronOptions): CronOptions
|
|
87
|
+
declare function CronOptionsHandler<T = undefined>(options?: CronOptions<T>): CronOptions<T>;
|
|
88
88
|
export { type CronOptions, CronOptionsHandler };
|
package/dist/pattern.d.ts
CHANGED
|
@@ -22,9 +22,13 @@ declare class CronPattern {
|
|
|
22
22
|
day: number[];
|
|
23
23
|
month: number[];
|
|
24
24
|
dayOfWeek: number[];
|
|
25
|
+
year: number[];
|
|
25
26
|
lastDayOfMonth: boolean;
|
|
27
|
+
nearestWeekdays: number[];
|
|
26
28
|
starDOM: boolean;
|
|
27
29
|
starDOW: boolean;
|
|
30
|
+
starYear: boolean;
|
|
31
|
+
useAndLogic: boolean;
|
|
28
32
|
constructor(pattern: string, timezone?: string);
|
|
29
33
|
/**
|
|
30
34
|
* Parse current pattern, will throw on any type of failure
|
|
@@ -41,7 +45,7 @@ declare class CronPattern {
|
|
|
41
45
|
*/
|
|
42
46
|
private throwAtIllegalCharacters;
|
|
43
47
|
/**
|
|
44
|
-
* Nothing but a number
|
|
48
|
+
* Nothing but a number, potentially with a modifier, left - handle that
|
|
45
49
|
*
|
|
46
50
|
* @param conf Current part, expected to be a number, as a string
|
|
47
51
|
* @param type One of "seconds", "minutes" etc
|
package/package.json
CHANGED