@queuebase/core 1.4.0 → 1.5.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/dist/index.cjs +805 -2
- package/dist/index.d.cts +93 -4
- package/dist/index.d.ts +93 -4
- package/dist/index.js +799 -2
- package/package.json +2 -1
package/dist/index.cjs
CHANGED
|
@@ -24,12 +24,18 @@ __export(index_exports, {
|
|
|
24
24
|
WEBHOOK_HEADERS: () => WEBHOOK_HEADERS,
|
|
25
25
|
calculateBackoff: () => calculateBackoff,
|
|
26
26
|
createJobRouter: () => createJobRouter,
|
|
27
|
+
extractSchedules: () => extractSchedules,
|
|
27
28
|
generateJobId: () => generateJobId,
|
|
28
29
|
generatePublicId: () => generatePublicId,
|
|
29
30
|
job: () => job,
|
|
31
|
+
nextRun: () => nextRun,
|
|
32
|
+
nextRuns: () => nextRuns,
|
|
30
33
|
parseDelay: () => parseDelay,
|
|
34
|
+
parsePlainEnglish: () => parsePlainEnglish,
|
|
31
35
|
processJobCallback: () => processJobCallback,
|
|
36
|
+
resolveSchedule: () => resolveSchedule,
|
|
32
37
|
signPayload: () => signPayload,
|
|
38
|
+
validateCron: () => validateCron,
|
|
33
39
|
verifySignature: () => verifySignature
|
|
34
40
|
});
|
|
35
41
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -44,6 +50,657 @@ function createJobRouter(jobs) {
|
|
|
44
50
|
return jobs;
|
|
45
51
|
}
|
|
46
52
|
|
|
53
|
+
// ../../node_modules/.pnpm/croner@10.0.1/node_modules/croner/dist/croner.js
|
|
54
|
+
function T(s2) {
|
|
55
|
+
return Date.UTC(s2.y, s2.m - 1, s2.d, s2.h, s2.i, s2.s);
|
|
56
|
+
}
|
|
57
|
+
function D(s2, e) {
|
|
58
|
+
return s2.y === e.y && s2.m === e.m && s2.d === e.d && s2.h === e.h && s2.i === e.i && s2.s === e.s;
|
|
59
|
+
}
|
|
60
|
+
function A(s2, e) {
|
|
61
|
+
let t = new Date(Date.parse(s2));
|
|
62
|
+
if (isNaN(t)) throw new Error("Invalid ISO8601 passed to timezone parser.");
|
|
63
|
+
let r = s2.substring(9);
|
|
64
|
+
return r.includes("Z") || r.includes("+") || r.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);
|
|
65
|
+
}
|
|
66
|
+
function v(s2, e, t) {
|
|
67
|
+
return k(A(s2, e), t);
|
|
68
|
+
}
|
|
69
|
+
function k(s2, e) {
|
|
70
|
+
let t = new Date(T(s2)), r = g(t, s2.tz), n = T(s2), i = T(r), a = n - i, o = new Date(t.getTime() + a), h = g(o, s2.tz);
|
|
71
|
+
if (D(h, s2)) {
|
|
72
|
+
let u = new Date(o.getTime() - 36e5), d = g(u, s2.tz);
|
|
73
|
+
return D(d, s2) ? u : o;
|
|
74
|
+
}
|
|
75
|
+
let l = new Date(o.getTime() + T(s2) - T(h)), y = g(l, s2.tz);
|
|
76
|
+
if (D(y, s2)) return l;
|
|
77
|
+
if (e) throw new Error("Invalid date passed to fromTZ()");
|
|
78
|
+
return o.getTime() > l.getTime() ? o : l;
|
|
79
|
+
}
|
|
80
|
+
function g(s2, e) {
|
|
81
|
+
let t, r;
|
|
82
|
+
try {
|
|
83
|
+
t = new Intl.DateTimeFormat("en-US", { timeZone: e, year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric", hour12: false }), r = t.formatToParts(s2);
|
|
84
|
+
} catch (i) {
|
|
85
|
+
let a = i instanceof Error ? i.message : String(i);
|
|
86
|
+
throw new RangeError(`toTZ: Invalid timezone '${e}' or date. Please provide a valid IANA timezone (e.g., 'America/New_York', 'Europe/Stockholm'). Original error: ${a}`);
|
|
87
|
+
}
|
|
88
|
+
let n = { year: 0, month: 0, day: 0, hour: 0, minute: 0, second: 0 };
|
|
89
|
+
for (let i of r) (i.type === "year" || i.type === "month" || i.type === "day" || i.type === "hour" || i.type === "minute" || i.type === "second") && (n[i.type] = parseInt(i.value, 10));
|
|
90
|
+
if (isNaN(n.year) || isNaN(n.month) || isNaN(n.day) || isNaN(n.hour) || isNaN(n.minute) || isNaN(n.second)) throw new Error(`toTZ: Failed to parse all date components from timezone '${e}'. This may indicate an invalid date or timezone configuration. Parsed components: ${JSON.stringify(n)}`);
|
|
91
|
+
return n.hour === 24 && (n.hour = 0), { y: n.year, m: n.month, d: n.day, h: n.hour, i: n.minute, s: n.second, tz: e };
|
|
92
|
+
}
|
|
93
|
+
function b(s2, e, t, r, n, i, a) {
|
|
94
|
+
return { y: s2, m: e, d: t, h: r, i: n, s: i, tz: a };
|
|
95
|
+
}
|
|
96
|
+
var O = [1, 2, 4, 8, 16];
|
|
97
|
+
var C = class {
|
|
98
|
+
pattern;
|
|
99
|
+
timezone;
|
|
100
|
+
mode;
|
|
101
|
+
alternativeWeekdays;
|
|
102
|
+
sloppyRanges;
|
|
103
|
+
second;
|
|
104
|
+
minute;
|
|
105
|
+
hour;
|
|
106
|
+
day;
|
|
107
|
+
month;
|
|
108
|
+
dayOfWeek;
|
|
109
|
+
year;
|
|
110
|
+
lastDayOfMonth;
|
|
111
|
+
lastWeekday;
|
|
112
|
+
nearestWeekdays;
|
|
113
|
+
starDOM;
|
|
114
|
+
starDOW;
|
|
115
|
+
starYear;
|
|
116
|
+
useAndLogic;
|
|
117
|
+
constructor(e, t, r) {
|
|
118
|
+
this.pattern = e, this.timezone = t, this.mode = r?.mode ?? "auto", this.alternativeWeekdays = r?.alternativeWeekdays ?? false, this.sloppyRanges = r?.sloppyRanges ?? false, 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 = false, this.lastWeekday = false, this.nearestWeekdays = Array(31).fill(0), this.starDOM = false, this.starDOW = false, this.starYear = false, this.useAndLogic = false, this.parse();
|
|
119
|
+
}
|
|
120
|
+
parse() {
|
|
121
|
+
if (!(typeof this.pattern == "string" || this.pattern instanceof String)) throw new TypeError("CronPattern: Pattern has to be of type string.");
|
|
122
|
+
this.pattern.indexOf("@") >= 0 && (this.pattern = this.handleNicknames(this.pattern).trim());
|
|
123
|
+
let e = this.pattern.match(/\S+/g) || [""], t = e.length;
|
|
124
|
+
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.");
|
|
125
|
+
if (this.mode !== "auto") {
|
|
126
|
+
let n;
|
|
127
|
+
switch (this.mode) {
|
|
128
|
+
case "5-part":
|
|
129
|
+
n = 5;
|
|
130
|
+
break;
|
|
131
|
+
case "6-part":
|
|
132
|
+
n = 6;
|
|
133
|
+
break;
|
|
134
|
+
case "7-part":
|
|
135
|
+
n = 7;
|
|
136
|
+
break;
|
|
137
|
+
case "5-or-6-parts":
|
|
138
|
+
n = [5, 6];
|
|
139
|
+
break;
|
|
140
|
+
case "6-or-7-parts":
|
|
141
|
+
n = [6, 7];
|
|
142
|
+
break;
|
|
143
|
+
default:
|
|
144
|
+
n = 0;
|
|
145
|
+
}
|
|
146
|
+
if (!(Array.isArray(n) ? n.includes(t) : t === n)) {
|
|
147
|
+
let a = Array.isArray(n) ? n.join(" or ") : n.toString();
|
|
148
|
+
throw new TypeError(`CronPattern: mode '${this.mode}' requires exactly ${a} parts, but pattern '${this.pattern}' has ${t} parts.`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (e.length === 5 && e.unshift("0"), e.length === 6 && e.push("*"), e[3].toUpperCase() === "LW" ? (this.lastWeekday = true, e[3] = "") : e[3].toUpperCase().indexOf("L") >= 0 && (e[3] = e[3].replace(/L/gi, ""), this.lastDayOfMonth = true), e[3] == "*" && (this.starDOM = true), e[6] == "*" && (this.starYear = true), e[4].length >= 3 && (e[4] = this.replaceAlphaMonths(e[4])), e[5].length >= 3 && (e[5] = this.alternativeWeekdays ? this.replaceAlphaDaysQuartz(e[5]) : this.replaceAlphaDays(e[5])), e[5].startsWith("+") && (this.useAndLogic = true, e[5] = e[5].substring(1), e[5] === "")) throw new TypeError("CronPattern: Day-of-week field cannot be empty after '+' modifier.");
|
|
152
|
+
switch (e[5] == "*" && (this.starDOW = true), 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.mode) {
|
|
153
|
+
case "5-part":
|
|
154
|
+
e[0] = "0", e[6] = "*";
|
|
155
|
+
break;
|
|
156
|
+
case "6-part":
|
|
157
|
+
e[6] = "*";
|
|
158
|
+
break;
|
|
159
|
+
case "5-or-6-parts":
|
|
160
|
+
e[6] = "*";
|
|
161
|
+
break;
|
|
162
|
+
case "6-or-7-parts":
|
|
163
|
+
break;
|
|
164
|
+
case "7-part":
|
|
165
|
+
case "auto":
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
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);
|
|
169
|
+
let r = this.alternativeWeekdays ? -1 : 0;
|
|
170
|
+
this.partToArray("dayOfWeek", e[5], r, 63), this.partToArray("year", e[6], 0, 1), !this.alternativeWeekdays && this.dayOfWeek[7] && (this.dayOfWeek[0] = this.dayOfWeek[7]);
|
|
171
|
+
}
|
|
172
|
+
partToArray(e, t, r, n) {
|
|
173
|
+
let i = this[e], a = e === "day" && this.lastDayOfMonth, o = e === "day" && this.lastWeekday;
|
|
174
|
+
if (t === "" && !a && !o) throw new TypeError("CronPattern: configuration entry " + e + " (" + t + ") is empty, check for trailing spaces.");
|
|
175
|
+
if (t === "*") return i.fill(n);
|
|
176
|
+
let h = t.split(",");
|
|
177
|
+
if (h.length > 1) for (let l = 0; l < h.length; l++) this.partToArray(e, h[l], r, n);
|
|
178
|
+
else t.indexOf("-") !== -1 && t.indexOf("/") !== -1 ? this.handleRangeWithStepping(t, e, r, n) : t.indexOf("-") !== -1 ? this.handleRange(t, e, r, n) : t.indexOf("/") !== -1 ? this.handleStepping(t, e, r, n) : t !== "" && this.handleNumber(t, e, r, n);
|
|
179
|
+
}
|
|
180
|
+
throwAtIllegalCharacters(e) {
|
|
181
|
+
for (let t = 0; t < e.length; t++) if ((t === 3 ? /[^/*0-9,\-WwLl]+/ : t === 5 ? /[^/*0-9,\-#Ll]+/ : /[^/*0-9,\-]+/).test(e[t])) throw new TypeError("CronPattern: configuration entry " + t + " (" + e[t] + ") contains illegal characters.");
|
|
182
|
+
}
|
|
183
|
+
handleNumber(e, t, r, n) {
|
|
184
|
+
let i = this.extractNth(e, t), a = e.toUpperCase().includes("W");
|
|
185
|
+
if (t !== "day" && a) throw new TypeError("CronPattern: Nearest weekday modifier (W) only allowed in day-of-month.");
|
|
186
|
+
a && (t = "nearestWeekdays");
|
|
187
|
+
let o = parseInt(i[0], 10) + r;
|
|
188
|
+
if (isNaN(o)) throw new TypeError("CronPattern: " + t + " is not a number: '" + e + "'");
|
|
189
|
+
this.setPart(t, o, i[1] || n);
|
|
190
|
+
}
|
|
191
|
+
setPart(e, t, r) {
|
|
192
|
+
if (!Object.prototype.hasOwnProperty.call(this, e)) throw new TypeError("CronPattern: Invalid part specified: " + e);
|
|
193
|
+
if (e === "dayOfWeek") {
|
|
194
|
+
if (t === 7 && (t = 0), t < 0 || t > 6) throw new RangeError("CronPattern: Invalid value for dayOfWeek: " + t);
|
|
195
|
+
this.setNthWeekdayOfMonth(t, r);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (e === "second" || e === "minute") {
|
|
199
|
+
if (t < 0 || t >= 60) throw new RangeError("CronPattern: Invalid value for " + e + ": " + t);
|
|
200
|
+
} else if (e === "hour") {
|
|
201
|
+
if (t < 0 || t >= 24) throw new RangeError("CronPattern: Invalid value for " + e + ": " + t);
|
|
202
|
+
} else if (e === "day" || e === "nearestWeekdays") {
|
|
203
|
+
if (t < 0 || t >= 31) throw new RangeError("CronPattern: Invalid value for " + e + ": " + t);
|
|
204
|
+
} else if (e === "month") {
|
|
205
|
+
if (t < 0 || t >= 12) throw new RangeError("CronPattern: Invalid value for " + e + ": " + t);
|
|
206
|
+
} else if (e === "year" && (t < 1 || t >= 1e4)) throw new RangeError("CronPattern: Invalid value for " + e + ": " + t + " (supported range: 1-9999)");
|
|
207
|
+
this[e][t] = r;
|
|
208
|
+
}
|
|
209
|
+
validateNotNaN(e, t) {
|
|
210
|
+
if (isNaN(e)) throw new TypeError(t);
|
|
211
|
+
}
|
|
212
|
+
validateRange(e, t, r, n, i) {
|
|
213
|
+
if (e > t) throw new TypeError("CronPattern: From value is larger than to value: '" + i + "'");
|
|
214
|
+
if (r !== void 0) {
|
|
215
|
+
if (r === 0) throw new TypeError("CronPattern: Syntax error, illegal stepping: 0");
|
|
216
|
+
if (r > this[n].length) throw new TypeError("CronPattern: Syntax error, steps cannot be greater than maximum value of part (" + this[n].length + ")");
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
handleRangeWithStepping(e, t, r, n) {
|
|
220
|
+
if (e.toUpperCase().includes("W")) throw new TypeError("CronPattern: Syntax error, W is not allowed in ranges with stepping.");
|
|
221
|
+
let i = this.extractNth(e, t), a = i[0].match(/^(\d+)-(\d+)\/(\d+)$/);
|
|
222
|
+
if (a === null) throw new TypeError("CronPattern: Syntax error, illegal range with stepping: '" + e + "'");
|
|
223
|
+
let [, o, h, l] = a, y = parseInt(o, 10) + r, u = parseInt(h, 10) + r, d = parseInt(l, 10);
|
|
224
|
+
this.validateNotNaN(y, "CronPattern: Syntax error, illegal lower range (NaN)"), this.validateNotNaN(u, "CronPattern: Syntax error, illegal upper range (NaN)"), this.validateNotNaN(d, "CronPattern: Syntax error, illegal stepping: (NaN)"), this.validateRange(y, u, d, t, e);
|
|
225
|
+
for (let c = y; c <= u; c += d) this.setPart(t, c, i[1] || n);
|
|
226
|
+
}
|
|
227
|
+
extractNth(e, t) {
|
|
228
|
+
let r = e, n;
|
|
229
|
+
if (r.includes("#")) {
|
|
230
|
+
if (t !== "dayOfWeek") throw new Error("CronPattern: nth (#) only allowed in day-of-week field");
|
|
231
|
+
n = r.split("#")[1], r = r.split("#")[0];
|
|
232
|
+
} else if (r.toUpperCase().endsWith("L")) {
|
|
233
|
+
if (t !== "dayOfWeek") throw new Error("CronPattern: L modifier only allowed in day-of-week field (use L alone for day-of-month)");
|
|
234
|
+
n = "L", r = r.slice(0, -1);
|
|
235
|
+
}
|
|
236
|
+
return [r, n];
|
|
237
|
+
}
|
|
238
|
+
handleRange(e, t, r, n) {
|
|
239
|
+
if (e.toUpperCase().includes("W")) throw new TypeError("CronPattern: Syntax error, W is not allowed in a range.");
|
|
240
|
+
let i = this.extractNth(e, t), a = i[0].split("-");
|
|
241
|
+
if (a.length !== 2) throw new TypeError("CronPattern: Syntax error, illegal range: '" + e + "'");
|
|
242
|
+
let o = parseInt(a[0], 10) + r, h = parseInt(a[1], 10) + r;
|
|
243
|
+
this.validateNotNaN(o, "CronPattern: Syntax error, illegal lower range (NaN)"), this.validateNotNaN(h, "CronPattern: Syntax error, illegal upper range (NaN)"), this.validateRange(o, h, void 0, t, e);
|
|
244
|
+
for (let l = o; l <= h; l++) this.setPart(t, l, i[1] || n);
|
|
245
|
+
}
|
|
246
|
+
handleStepping(e, t, r, n) {
|
|
247
|
+
if (e.toUpperCase().includes("W")) throw new TypeError("CronPattern: Syntax error, W is not allowed in parts with stepping.");
|
|
248
|
+
let i = this.extractNth(e, t), a = i[0].split("/");
|
|
249
|
+
if (a.length !== 2) throw new TypeError("CronPattern: Syntax error, illegal stepping: '" + e + "'");
|
|
250
|
+
if (this.sloppyRanges) a[0] === "" && (a[0] = "*");
|
|
251
|
+
else {
|
|
252
|
+
if (a[0] === "") throw new TypeError("CronPattern: Syntax error, stepping with missing prefix ('" + e + "') is not allowed. Use wildcard (*/step) or range (min-max/step) instead.");
|
|
253
|
+
if (a[0] !== "*") throw new TypeError("CronPattern: Syntax error, stepping with numeric prefix ('" + e + "') is not allowed. Use wildcard (*/step) or range (min-max/step) instead.");
|
|
254
|
+
}
|
|
255
|
+
let o = 0;
|
|
256
|
+
a[0] !== "*" && (o = parseInt(a[0], 10) + r);
|
|
257
|
+
let h = parseInt(a[1], 10);
|
|
258
|
+
this.validateNotNaN(h, "CronPattern: Syntax error, illegal stepping: (NaN)"), this.validateRange(0, this[t].length - 1, h, t, e);
|
|
259
|
+
for (let l = o; l < this[t].length; l += h) this.setPart(t, l, i[1] || n);
|
|
260
|
+
}
|
|
261
|
+
replaceAlphaDays(e) {
|
|
262
|
+
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");
|
|
263
|
+
}
|
|
264
|
+
replaceAlphaDaysQuartz(e) {
|
|
265
|
+
return e.replace(/sun/gi, "1").replace(/mon/gi, "2").replace(/tue/gi, "3").replace(/wed/gi, "4").replace(/thu/gi, "5").replace(/fri/gi, "6").replace(/sat/gi, "7");
|
|
266
|
+
}
|
|
267
|
+
replaceAlphaMonths(e) {
|
|
268
|
+
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");
|
|
269
|
+
}
|
|
270
|
+
handleNicknames(e) {
|
|
271
|
+
let t = e.trim().toLowerCase();
|
|
272
|
+
if (t === "@yearly" || t === "@annually") return "0 0 1 1 *";
|
|
273
|
+
if (t === "@monthly") return "0 0 1 * *";
|
|
274
|
+
if (t === "@weekly") return "0 0 * * 0";
|
|
275
|
+
if (t === "@daily" || t === "@midnight") return "0 0 * * *";
|
|
276
|
+
if (t === "@hourly") return "0 * * * *";
|
|
277
|
+
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.");
|
|
278
|
+
return e;
|
|
279
|
+
}
|
|
280
|
+
setNthWeekdayOfMonth(e, t) {
|
|
281
|
+
if (typeof t != "number" && t.toUpperCase() === "L") this.dayOfWeek[e] = this.dayOfWeek[e] | 32;
|
|
282
|
+
else if (t === 63) this.dayOfWeek[e] = 63;
|
|
283
|
+
else if (t < 6 && t > 0) this.dayOfWeek[e] = this.dayOfWeek[e] | O[t - 1];
|
|
284
|
+
else throw new TypeError(`CronPattern: nth weekday out of range, should be 1-5 or L. Value: ${t}, Type: ${typeof t}`);
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
var P = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
288
|
+
var f = [["month", "year", 0], ["day", "month", -1], ["hour", "day", 0], ["minute", "hour", 0], ["second", "minute", 0]];
|
|
289
|
+
var m = class s {
|
|
290
|
+
tz;
|
|
291
|
+
ms;
|
|
292
|
+
second;
|
|
293
|
+
minute;
|
|
294
|
+
hour;
|
|
295
|
+
day;
|
|
296
|
+
month;
|
|
297
|
+
year;
|
|
298
|
+
constructor(e, t) {
|
|
299
|
+
if (this.tz = t, e && e instanceof Date) if (!isNaN(e)) this.fromDate(e);
|
|
300
|
+
else throw new TypeError("CronDate: Invalid date passed to CronDate constructor");
|
|
301
|
+
else if (e == null) this.fromDate(/* @__PURE__ */ new Date());
|
|
302
|
+
else if (e && typeof e == "string") this.fromString(e);
|
|
303
|
+
else if (e instanceof s) this.fromCronDate(e);
|
|
304
|
+
else throw new TypeError("CronDate: Invalid type (" + typeof e + ") passed to CronDate constructor");
|
|
305
|
+
}
|
|
306
|
+
getLastDayOfMonth(e, t) {
|
|
307
|
+
return t !== 1 ? P[t] : new Date(Date.UTC(e, t + 1, 0)).getUTCDate();
|
|
308
|
+
}
|
|
309
|
+
getLastWeekday(e, t) {
|
|
310
|
+
let r = this.getLastDayOfMonth(e, t), i = new Date(Date.UTC(e, t, r)).getUTCDay();
|
|
311
|
+
return i === 0 ? r - 2 : i === 6 ? r - 1 : r;
|
|
312
|
+
}
|
|
313
|
+
getNearestWeekday(e, t, r) {
|
|
314
|
+
let n = this.getLastDayOfMonth(e, t);
|
|
315
|
+
if (r > n) return -1;
|
|
316
|
+
let a = new Date(Date.UTC(e, t, r)).getUTCDay();
|
|
317
|
+
return a === 0 ? r === n ? r - 2 : r + 1 : a === 6 ? r === 1 ? r + 2 : r - 1 : r;
|
|
318
|
+
}
|
|
319
|
+
isNthWeekdayOfMonth(e, t, r, n) {
|
|
320
|
+
let a = new Date(Date.UTC(e, t, r)).getUTCDay(), o = 0;
|
|
321
|
+
for (let h = 1; h <= r; h++) new Date(Date.UTC(e, t, h)).getUTCDay() === a && o++;
|
|
322
|
+
if (n & 63 && O[o - 1] & n) return true;
|
|
323
|
+
if (n & 32) {
|
|
324
|
+
let h = this.getLastDayOfMonth(e, t);
|
|
325
|
+
for (let l = r + 1; l <= h; l++) if (new Date(Date.UTC(e, t, l)).getUTCDay() === a) return false;
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
fromDate(e) {
|
|
331
|
+
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();
|
|
332
|
+
else try {
|
|
333
|
+
let t = g(e, this.tz);
|
|
334
|
+
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;
|
|
335
|
+
} catch (t) {
|
|
336
|
+
let r = t instanceof Error ? t.message : String(t);
|
|
337
|
+
throw new TypeError(`CronDate: Failed to convert date to timezone '${this.tz}'. This may happen with invalid timezone names or dates. Original error: ${r}`);
|
|
338
|
+
}
|
|
339
|
+
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();
|
|
340
|
+
}
|
|
341
|
+
fromCronDate(e) {
|
|
342
|
+
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;
|
|
343
|
+
}
|
|
344
|
+
apply() {
|
|
345
|
+
if (this.month > 11 || this.month < 0 || this.day > P[this.month] || this.day < 1 || this.hour > 59 || this.minute > 59 || this.second > 59 || this.hour < 0 || this.minute < 0 || this.second < 0) {
|
|
346
|
+
let e = new Date(Date.UTC(this.year, this.month, this.day, this.hour, this.minute, this.second, this.ms));
|
|
347
|
+
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(), true;
|
|
348
|
+
} else return false;
|
|
349
|
+
}
|
|
350
|
+
fromString(e) {
|
|
351
|
+
if (typeof this.tz == "number") {
|
|
352
|
+
let t = v(e);
|
|
353
|
+
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();
|
|
354
|
+
} else return this.fromDate(v(e, this.tz));
|
|
355
|
+
}
|
|
356
|
+
findNext(e, t, r, n) {
|
|
357
|
+
return this._findMatch(e, t, r, n, 1);
|
|
358
|
+
}
|
|
359
|
+
_findMatch(e, t, r, n, i) {
|
|
360
|
+
let a = this[t], o;
|
|
361
|
+
r.lastDayOfMonth && (o = this.getLastDayOfMonth(this.year, this.month));
|
|
362
|
+
let h = !r.starDOW && t == "day" ? new Date(Date.UTC(this.year, this.month, 1, 0, 0, 0, 0)).getUTCDay() : void 0, l = this[t] + n, y = i === 1 ? (u) => u < r[t].length : (u) => u >= 0;
|
|
363
|
+
for (let u = l; y(u); u += i) {
|
|
364
|
+
let d = r[t][u];
|
|
365
|
+
if (t === "day" && !d) {
|
|
366
|
+
for (let c = 0; c < r.nearestWeekdays.length; c++) if (r.nearestWeekdays[c]) {
|
|
367
|
+
let M = this.getNearestWeekday(this.year, this.month, c - n);
|
|
368
|
+
if (M === -1) continue;
|
|
369
|
+
if (M === u - n) {
|
|
370
|
+
d = 1;
|
|
371
|
+
break;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (t === "day" && r.lastWeekday) {
|
|
376
|
+
let c = this.getLastWeekday(this.year, this.month);
|
|
377
|
+
u - n === c && (d = 1);
|
|
378
|
+
}
|
|
379
|
+
if (t === "day" && r.lastDayOfMonth && u - n == o && (d = 1), t === "day" && !r.starDOW) {
|
|
380
|
+
let c = r.dayOfWeek[(h + (u - n - 1)) % 7];
|
|
381
|
+
if (c && c & 63) c = this.isNthWeekdayOfMonth(this.year, this.month, u - n, c) ? 1 : 0;
|
|
382
|
+
else if (c) throw new Error(`CronDate: Invalid value for dayOfWeek encountered. ${c}`);
|
|
383
|
+
r.useAndLogic ? d = d && c : !e.domAndDow && !r.starDOM ? d = d || c : d = d && c;
|
|
384
|
+
}
|
|
385
|
+
if (d) return this[t] = u - n, a !== this[t] ? 2 : 1;
|
|
386
|
+
}
|
|
387
|
+
return 3;
|
|
388
|
+
}
|
|
389
|
+
recurse(e, t, r) {
|
|
390
|
+
if (r === 0 && !e.starYear) {
|
|
391
|
+
if (this.year >= 0 && this.year < e.year.length && e.year[this.year] === 0) {
|
|
392
|
+
let i = -1;
|
|
393
|
+
for (let a = this.year + 1; a < e.year.length && a < 1e4; a++) if (e.year[a] === 1) {
|
|
394
|
+
i = a;
|
|
395
|
+
break;
|
|
396
|
+
}
|
|
397
|
+
if (i === -1) return null;
|
|
398
|
+
this.year = i, this.month = 0, this.day = 1, this.hour = 0, this.minute = 0, this.second = 0, this.ms = 0;
|
|
399
|
+
}
|
|
400
|
+
if (this.year >= 1e4) return null;
|
|
401
|
+
}
|
|
402
|
+
let n = this.findNext(t, f[r][0], e, f[r][2]);
|
|
403
|
+
if (n > 1) {
|
|
404
|
+
let i = r + 1;
|
|
405
|
+
for (; i < f.length; ) this[f[i][0]] = -f[i][2], i++;
|
|
406
|
+
if (n === 3) {
|
|
407
|
+
if (this[f[r][1]]++, this[f[r][0]] = -f[r][2], this.apply(), r === 0 && !e.starYear) {
|
|
408
|
+
for (; this.year >= 0 && this.year < e.year.length && e.year[this.year] === 0 && this.year < 1e4; ) this.year++;
|
|
409
|
+
if (this.year >= 1e4 || this.year >= e.year.length) return null;
|
|
410
|
+
}
|
|
411
|
+
return this.recurse(e, t, 0);
|
|
412
|
+
} else if (this.apply()) return this.recurse(e, t, r - 1);
|
|
413
|
+
}
|
|
414
|
+
return r += 1, r >= f.length ? this : (e.starYear ? this.year >= 3e3 : this.year >= 1e4) ? null : this.recurse(e, t, r);
|
|
415
|
+
}
|
|
416
|
+
increment(e, t, r) {
|
|
417
|
+
return this.second += t.interval !== void 0 && t.interval > 1 && r ? t.interval : 1, this.ms = 0, this.apply(), this.recurse(e, t, 0);
|
|
418
|
+
}
|
|
419
|
+
decrement(e, t) {
|
|
420
|
+
return this.second -= t.interval !== void 0 && t.interval > 1 ? t.interval : 1, this.ms = 0, this.apply(), this.recurseBackward(e, t, 0, 0);
|
|
421
|
+
}
|
|
422
|
+
recurseBackward(e, t, r, n = 0) {
|
|
423
|
+
if (n > 1e4) return null;
|
|
424
|
+
if (r === 0 && !e.starYear) {
|
|
425
|
+
if (this.year >= 0 && this.year < e.year.length && e.year[this.year] === 0) {
|
|
426
|
+
let a = -1;
|
|
427
|
+
for (let o = this.year - 1; o >= 0; o--) if (e.year[o] === 1) {
|
|
428
|
+
a = o;
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
if (a === -1) return null;
|
|
432
|
+
this.year = a, this.month = 11, this.day = 31, this.hour = 23, this.minute = 59, this.second = 59, this.ms = 0;
|
|
433
|
+
}
|
|
434
|
+
if (this.year < 0) return null;
|
|
435
|
+
}
|
|
436
|
+
let i = this.findPrevious(t, f[r][0], e, f[r][2]);
|
|
437
|
+
if (i > 1) {
|
|
438
|
+
let a = r + 1;
|
|
439
|
+
for (; a < f.length; ) {
|
|
440
|
+
let o = f[a][0], h = f[a][2], l = this.getMaxPatternValue(o, e, h);
|
|
441
|
+
this[o] = l, a++;
|
|
442
|
+
}
|
|
443
|
+
if (i === 3) {
|
|
444
|
+
if (this[f[r][1]]--, r === 0) {
|
|
445
|
+
let y = this.getLastDayOfMonth(this.year, this.month);
|
|
446
|
+
this.day > y && (this.day = y);
|
|
447
|
+
}
|
|
448
|
+
if (r === 1) if (this.day <= 0) this.day = 1;
|
|
449
|
+
else {
|
|
450
|
+
let y = this.year, u = this.month;
|
|
451
|
+
for (; u < 0; ) u += 12, y--;
|
|
452
|
+
for (; u > 11; ) u -= 12, y++;
|
|
453
|
+
let d = u !== 1 ? P[u] : new Date(Date.UTC(y, u + 1, 0)).getUTCDate();
|
|
454
|
+
this.day > d && (this.day = d);
|
|
455
|
+
}
|
|
456
|
+
this.apply();
|
|
457
|
+
let o = f[r][0], h = f[r][2], l = this.getMaxPatternValue(o, e, h);
|
|
458
|
+
if (o === "day") {
|
|
459
|
+
let y = this.getLastDayOfMonth(this.year, this.month);
|
|
460
|
+
this[o] = Math.min(l, y);
|
|
461
|
+
} else this[o] = l;
|
|
462
|
+
if (this.apply(), r === 0) {
|
|
463
|
+
let y = f[1][2], u = this.getMaxPatternValue("day", e, y), d = this.getLastDayOfMonth(this.year, this.month), c = Math.min(u, d);
|
|
464
|
+
c !== this.day && (this.day = c, this.hour = this.getMaxPatternValue("hour", e, f[2][2]), this.minute = this.getMaxPatternValue("minute", e, f[3][2]), this.second = this.getMaxPatternValue("second", e, f[4][2]));
|
|
465
|
+
}
|
|
466
|
+
if (r === 0 && !e.starYear) {
|
|
467
|
+
for (; this.year >= 0 && this.year < e.year.length && e.year[this.year] === 0; ) this.year--;
|
|
468
|
+
if (this.year < 0) return null;
|
|
469
|
+
}
|
|
470
|
+
return this.recurseBackward(e, t, 0, n + 1);
|
|
471
|
+
} else if (this.apply()) return this.recurseBackward(e, t, r - 1, n + 1);
|
|
472
|
+
}
|
|
473
|
+
return r += 1, r >= f.length ? this : this.year < 0 ? null : this.recurseBackward(e, t, r, n + 1);
|
|
474
|
+
}
|
|
475
|
+
getMaxPatternValue(e, t, r) {
|
|
476
|
+
if (e === "day" && t.lastDayOfMonth) return this.getLastDayOfMonth(this.year, this.month);
|
|
477
|
+
if (e === "day" && !t.starDOW) return this.getLastDayOfMonth(this.year, this.month);
|
|
478
|
+
for (let n = t[e].length - 1; n >= 0; n--) if (t[e][n]) return n - r;
|
|
479
|
+
return t[e].length - 1 - r;
|
|
480
|
+
}
|
|
481
|
+
findPrevious(e, t, r, n) {
|
|
482
|
+
return this._findMatch(e, t, r, n, -1);
|
|
483
|
+
}
|
|
484
|
+
getDate(e) {
|
|
485
|
+
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), false);
|
|
486
|
+
}
|
|
487
|
+
getTime() {
|
|
488
|
+
return this.getDate(false).getTime();
|
|
489
|
+
}
|
|
490
|
+
match(e, t) {
|
|
491
|
+
if (!e.starYear && (this.year < 0 || this.year >= e.year.length || e.year[this.year] === 0)) return false;
|
|
492
|
+
for (let r = 0; r < f.length; r++) {
|
|
493
|
+
let n = f[r][0], i = f[r][2], a = this[n];
|
|
494
|
+
if (a + i < 0 || a + i >= e[n].length) return false;
|
|
495
|
+
let o = e[n][a + i];
|
|
496
|
+
if (n === "day") {
|
|
497
|
+
if (!o) {
|
|
498
|
+
for (let h = 0; h < e.nearestWeekdays.length; h++) if (e.nearestWeekdays[h]) {
|
|
499
|
+
let l = this.getNearestWeekday(this.year, this.month, h - i);
|
|
500
|
+
if (l !== -1 && l === a) {
|
|
501
|
+
o = 1;
|
|
502
|
+
break;
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
if (e.lastWeekday) {
|
|
507
|
+
let h = this.getLastWeekday(this.year, this.month);
|
|
508
|
+
a === h && (o = 1);
|
|
509
|
+
}
|
|
510
|
+
if (e.lastDayOfMonth) {
|
|
511
|
+
let h = this.getLastDayOfMonth(this.year, this.month);
|
|
512
|
+
a === h && (o = 1);
|
|
513
|
+
}
|
|
514
|
+
if (!e.starDOW) {
|
|
515
|
+
let h = new Date(Date.UTC(this.year, this.month, 1, 0, 0, 0, 0)).getUTCDay(), l = e.dayOfWeek[(h + (a - 1)) % 7];
|
|
516
|
+
l && l & 63 && (l = this.isNthWeekdayOfMonth(this.year, this.month, a, l) ? 1 : 0), e.useAndLogic ? o = o && l : !t.domAndDow && !e.starDOM ? o = o || l : o = o && l;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
if (!o) return false;
|
|
520
|
+
}
|
|
521
|
+
return true;
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
function R(s2) {
|
|
525
|
+
if (s2 === void 0 && (s2 = {}), delete s2.name, s2.legacyMode !== void 0 && s2.domAndDow === void 0 ? s2.domAndDow = !s2.legacyMode : s2.domAndDow === void 0 && (s2.domAndDow = false), s2.legacyMode = !s2.domAndDow, s2.paused = s2.paused === void 0 ? false : s2.paused, s2.maxRuns = s2.maxRuns === void 0 ? 1 / 0 : s2.maxRuns, s2.catch = s2.catch === void 0 ? false : s2.catch, s2.interval = s2.interval === void 0 ? 0 : parseInt(s2.interval.toString(), 10), s2.utcOffset = s2.utcOffset === void 0 ? void 0 : parseInt(s2.utcOffset.toString(), 10), s2.dayOffset = s2.dayOffset === void 0 ? 0 : parseInt(s2.dayOffset.toString(), 10), s2.unref = s2.unref === void 0 ? false : s2.unref, s2.mode = s2.mode === void 0 ? "auto" : s2.mode, s2.alternativeWeekdays = s2.alternativeWeekdays === void 0 ? false : s2.alternativeWeekdays, s2.sloppyRanges = s2.sloppyRanges === void 0 ? false : s2.sloppyRanges, !["auto", "5-part", "6-part", "7-part", "5-or-6-parts", "6-or-7-parts"].includes(s2.mode)) throw new Error("CronOptions: mode must be one of 'auto', '5-part', '6-part', '7-part', '5-or-6-parts', or '6-or-7-parts'.");
|
|
526
|
+
if (s2.startAt && (s2.startAt = new m(s2.startAt, s2.timezone)), s2.stopAt && (s2.stopAt = new m(s2.stopAt, s2.timezone)), s2.interval !== null) {
|
|
527
|
+
if (isNaN(s2.interval)) throw new Error("CronOptions: Supplied value for interval is not a number");
|
|
528
|
+
if (s2.interval < 0) throw new Error("CronOptions: Supplied value for interval can not be negative");
|
|
529
|
+
}
|
|
530
|
+
if (s2.utcOffset !== void 0) {
|
|
531
|
+
if (isNaN(s2.utcOffset)) throw new Error("CronOptions: Invalid value passed for utcOffset, should be number representing minutes offset from UTC.");
|
|
532
|
+
if (s2.utcOffset < -870 || s2.utcOffset > 870) throw new Error("CronOptions: utcOffset out of bounds.");
|
|
533
|
+
if (s2.utcOffset !== void 0 && s2.timezone) throw new Error("CronOptions: Combining 'utcOffset' with 'timezone' is not allowed.");
|
|
534
|
+
}
|
|
535
|
+
if (s2.unref !== true && s2.unref !== false) throw new Error("CronOptions: Unref should be either true, false or undefined(false).");
|
|
536
|
+
if (s2.dayOffset !== void 0 && s2.dayOffset !== 0 && isNaN(s2.dayOffset)) throw new Error("CronOptions: Invalid value passed for dayOffset, should be a number representing days to offset.");
|
|
537
|
+
return s2;
|
|
538
|
+
}
|
|
539
|
+
function p(s2) {
|
|
540
|
+
return Object.prototype.toString.call(s2) === "[object Function]" || typeof s2 == "function" || s2 instanceof Function;
|
|
541
|
+
}
|
|
542
|
+
function _(s2) {
|
|
543
|
+
return p(s2);
|
|
544
|
+
}
|
|
545
|
+
function x(s2) {
|
|
546
|
+
typeof Deno < "u" && typeof Deno.unrefTimer < "u" ? Deno.unrefTimer(s2) : s2 && typeof s2.unref < "u" && s2.unref();
|
|
547
|
+
}
|
|
548
|
+
var W = 30 * 1e3;
|
|
549
|
+
var w = [];
|
|
550
|
+
var E = class {
|
|
551
|
+
name;
|
|
552
|
+
options;
|
|
553
|
+
_states;
|
|
554
|
+
fn;
|
|
555
|
+
getTz() {
|
|
556
|
+
return this.options.timezone || this.options.utcOffset;
|
|
557
|
+
}
|
|
558
|
+
applyDayOffset(e) {
|
|
559
|
+
if (this.options.dayOffset !== void 0 && this.options.dayOffset !== 0) {
|
|
560
|
+
let t = this.options.dayOffset * 24 * 60 * 60 * 1e3;
|
|
561
|
+
return new Date(e.getTime() + t);
|
|
562
|
+
}
|
|
563
|
+
return e;
|
|
564
|
+
}
|
|
565
|
+
constructor(e, t, r) {
|
|
566
|
+
let n, i;
|
|
567
|
+
if (p(t)) i = t;
|
|
568
|
+
else if (typeof t == "object") n = t;
|
|
569
|
+
else if (t !== void 0) throw new Error("Cron: Invalid argument passed for optionsIn. Should be one of function, or object (options).");
|
|
570
|
+
if (p(r)) i = r;
|
|
571
|
+
else if (typeof r == "object") n = r;
|
|
572
|
+
else if (r !== void 0) throw new Error("Cron: Invalid argument passed for funcIn. Should be one of function, or object (options).");
|
|
573
|
+
if (this.name = n?.name, this.options = R(n), this._states = { kill: false, blocking: false, previousRun: void 0, currentRun: void 0, once: void 0, currentTimeout: void 0, maxRuns: n ? n.maxRuns : void 0, paused: n ? n.paused : false, pattern: new C("* * * * *", void 0, { mode: "auto" }) }, e && (e instanceof Date || typeof e == "string" && e.indexOf(":") > 0) ? this._states.once = new m(e, this.getTz()) : this._states.pattern = new C(e, this.options.timezone, { mode: this.options.mode, alternativeWeekdays: this.options.alternativeWeekdays, sloppyRanges: this.options.sloppyRanges }), this.name) {
|
|
574
|
+
if (w.find((o) => o.name === this.name)) throw new Error("Cron: Tried to initialize new named job '" + this.name + "', but name already taken.");
|
|
575
|
+
w.push(this);
|
|
576
|
+
}
|
|
577
|
+
return i !== void 0 && _(i) && (this.fn = i, this.schedule()), this;
|
|
578
|
+
}
|
|
579
|
+
nextRun(e) {
|
|
580
|
+
let t = this._next(e);
|
|
581
|
+
return t ? this.applyDayOffset(t.getDate(false)) : null;
|
|
582
|
+
}
|
|
583
|
+
nextRuns(e, t) {
|
|
584
|
+
this._states.maxRuns !== void 0 && e > this._states.maxRuns && (e = this._states.maxRuns);
|
|
585
|
+
let r = t || this._states.currentRun || void 0;
|
|
586
|
+
return this._enumerateRuns(e, r, "next");
|
|
587
|
+
}
|
|
588
|
+
previousRuns(e, t) {
|
|
589
|
+
return this._enumerateRuns(e, t || void 0, "previous");
|
|
590
|
+
}
|
|
591
|
+
_enumerateRuns(e, t, r) {
|
|
592
|
+
let n = [], i = t ? new m(t, this.getTz()) : null, a = r === "next" ? this._next : this._previous;
|
|
593
|
+
for (; e--; ) {
|
|
594
|
+
let o = a.call(this, i);
|
|
595
|
+
if (!o) break;
|
|
596
|
+
let h = o.getDate(false);
|
|
597
|
+
n.push(this.applyDayOffset(h)), i = o;
|
|
598
|
+
}
|
|
599
|
+
return n;
|
|
600
|
+
}
|
|
601
|
+
match(e) {
|
|
602
|
+
if (this._states.once) {
|
|
603
|
+
let r = new m(e, this.getTz());
|
|
604
|
+
r.ms = 0;
|
|
605
|
+
let n = new m(this._states.once, this.getTz());
|
|
606
|
+
return n.ms = 0, r.getTime() === n.getTime();
|
|
607
|
+
}
|
|
608
|
+
let t = new m(e, this.getTz());
|
|
609
|
+
return t.ms = 0, t.match(this._states.pattern, this.options);
|
|
610
|
+
}
|
|
611
|
+
getPattern() {
|
|
612
|
+
if (!this._states.once) return this._states.pattern ? this._states.pattern.pattern : void 0;
|
|
613
|
+
}
|
|
614
|
+
getOnce() {
|
|
615
|
+
return this._states.once ? this._states.once.getDate() : null;
|
|
616
|
+
}
|
|
617
|
+
isRunning() {
|
|
618
|
+
let e = this.nextRun(this._states.currentRun), t = !this._states.paused, r = this.fn !== void 0, n = !this._states.kill;
|
|
619
|
+
return t && r && n && e !== null;
|
|
620
|
+
}
|
|
621
|
+
isStopped() {
|
|
622
|
+
return this._states.kill;
|
|
623
|
+
}
|
|
624
|
+
isBusy() {
|
|
625
|
+
return this._states.blocking;
|
|
626
|
+
}
|
|
627
|
+
currentRun() {
|
|
628
|
+
return this._states.currentRun ? this._states.currentRun.getDate() : null;
|
|
629
|
+
}
|
|
630
|
+
previousRun() {
|
|
631
|
+
return this._states.previousRun ? this._states.previousRun.getDate() : null;
|
|
632
|
+
}
|
|
633
|
+
msToNext(e) {
|
|
634
|
+
let t = this._next(e);
|
|
635
|
+
return t ? e instanceof m || e instanceof Date ? t.getTime() - e.getTime() : t.getTime() - new m(e).getTime() : null;
|
|
636
|
+
}
|
|
637
|
+
stop() {
|
|
638
|
+
this._states.kill = true, this._states.currentTimeout && clearTimeout(this._states.currentTimeout);
|
|
639
|
+
let e = w.indexOf(this);
|
|
640
|
+
e >= 0 && w.splice(e, 1);
|
|
641
|
+
}
|
|
642
|
+
pause() {
|
|
643
|
+
return this._states.paused = true, !this._states.kill;
|
|
644
|
+
}
|
|
645
|
+
resume() {
|
|
646
|
+
return this._states.paused = false, !this._states.kill;
|
|
647
|
+
}
|
|
648
|
+
schedule(e) {
|
|
649
|
+
if (e && this.fn) throw new Error("Cron: It is not allowed to schedule two functions using the same Croner instance.");
|
|
650
|
+
e && (this.fn = e);
|
|
651
|
+
let t = this.msToNext(), r = this.nextRun(this._states.currentRun);
|
|
652
|
+
return t == null || isNaN(t) || r === null ? this : (t > W && (t = W), this._states.currentTimeout = setTimeout(() => this._checkTrigger(r), t), this._states.currentTimeout && this.options.unref && x(this._states.currentTimeout), this);
|
|
653
|
+
}
|
|
654
|
+
async _trigger(e) {
|
|
655
|
+
this._states.blocking = true, this._states.currentRun = new m(void 0, this.getTz());
|
|
656
|
+
try {
|
|
657
|
+
if (this.options.catch) try {
|
|
658
|
+
this.fn !== void 0 && await this.fn(this, this.options.context);
|
|
659
|
+
} catch (t) {
|
|
660
|
+
if (p(this.options.catch)) try {
|
|
661
|
+
this.options.catch(t, this);
|
|
662
|
+
} catch {
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
else this.fn !== void 0 && await this.fn(this, this.options.context);
|
|
666
|
+
} finally {
|
|
667
|
+
this._states.previousRun = new m(e, this.getTz()), this._states.blocking = false;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
async trigger() {
|
|
671
|
+
await this._trigger();
|
|
672
|
+
}
|
|
673
|
+
runsLeft() {
|
|
674
|
+
return this._states.maxRuns;
|
|
675
|
+
}
|
|
676
|
+
_checkTrigger(e) {
|
|
677
|
+
let t = /* @__PURE__ */ new Date(), r = !this._states.paused && t.getTime() >= e.getTime(), n = this._states.blocking && this.options.protect;
|
|
678
|
+
r && !n ? (this._states.maxRuns !== void 0 && this._states.maxRuns--, this._trigger()) : r && n && p(this.options.protect) && setTimeout(() => this.options.protect(this), 0), this.schedule();
|
|
679
|
+
}
|
|
680
|
+
_next(e) {
|
|
681
|
+
let t = !!(e || this._states.currentRun), r = false;
|
|
682
|
+
!e && this.options.startAt && this.options.interval && ([e, t] = this._calculatePreviousRun(e, t), r = !e), e = new m(e, this.getTz()), this.options.startAt && e && e.getTime() < this.options.startAt.getTime() && (e = this.options.startAt);
|
|
683
|
+
let n = this._states.once || new m(e, this.getTz());
|
|
684
|
+
return !r && n !== this._states.once && (n = n.increment(this._states.pattern, this.options, t)), this._states.once && this._states.once.getTime() <= e.getTime() || n === null || this._states.maxRuns !== void 0 && this._states.maxRuns <= 0 || this._states.kill || this.options.stopAt && n.getTime() >= this.options.stopAt.getTime() ? null : n;
|
|
685
|
+
}
|
|
686
|
+
_previous(e) {
|
|
687
|
+
let t = new m(e, this.getTz());
|
|
688
|
+
this.options.stopAt && t.getTime() > this.options.stopAt.getTime() && (t = this.options.stopAt);
|
|
689
|
+
let r = new m(t, this.getTz());
|
|
690
|
+
return this._states.once ? this._states.once.getTime() < t.getTime() ? this._states.once : null : (r = r.decrement(this._states.pattern, this.options), r === null || this.options.startAt && r.getTime() < this.options.startAt.getTime() ? null : r);
|
|
691
|
+
}
|
|
692
|
+
_calculatePreviousRun(e, t) {
|
|
693
|
+
let r = new m(void 0, this.getTz()), n = e;
|
|
694
|
+
if (this.options.startAt.getTime() <= r.getTime()) {
|
|
695
|
+
n = this.options.startAt;
|
|
696
|
+
let i = n.getTime() + this.options.interval * 1e3;
|
|
697
|
+
for (; i <= r.getTime(); ) n = new m(n, this.getTz()).increment(this._states.pattern, this.options, true), i = n.getTime() + this.options.interval * 1e3;
|
|
698
|
+
t = true;
|
|
699
|
+
}
|
|
700
|
+
return n === null && (n = void 0), [n, t];
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
|
|
47
704
|
// ../../node_modules/.pnpm/nanoid@5.1.6/node_modules/nanoid/index.js
|
|
48
705
|
var import_node_crypto = require("crypto");
|
|
49
706
|
var POOL_SIZE_MULTIPLIER = 128;
|
|
@@ -134,6 +791,146 @@ function generatePublicId(prefix = "job") {
|
|
|
134
791
|
return `${prefix}_${nanoid(21)}`;
|
|
135
792
|
}
|
|
136
793
|
|
|
794
|
+
// src/schedule.ts
|
|
795
|
+
var PLAIN_ENGLISH_PATTERNS = [
|
|
796
|
+
// "every 5 minutes", "every 30 minutes"
|
|
797
|
+
{
|
|
798
|
+
regex: /^every (\d+) minutes?$/i,
|
|
799
|
+
toCron: (minutes) => `*/${minutes} * * * *`
|
|
800
|
+
},
|
|
801
|
+
// "every minute"
|
|
802
|
+
{
|
|
803
|
+
regex: /^every minute$/i,
|
|
804
|
+
toCron: () => "* * * * *"
|
|
805
|
+
},
|
|
806
|
+
// "every 2 hours", "every hour"
|
|
807
|
+
{
|
|
808
|
+
regex: /^every (\d+) hours?$/i,
|
|
809
|
+
toCron: (hours) => `0 */${hours} * * *`
|
|
810
|
+
},
|
|
811
|
+
{
|
|
812
|
+
regex: /^every hour$/i,
|
|
813
|
+
toCron: () => "0 * * * *"
|
|
814
|
+
},
|
|
815
|
+
// "every day at 9am", "every day at 2pm", "every day at 14:30"
|
|
816
|
+
{
|
|
817
|
+
regex: /^every day at (\d{1,2})(:\d{2})?\s*(am|pm)?$/i,
|
|
818
|
+
toCron: (hour, minutes, ampm) => {
|
|
819
|
+
const { h, m: m2 } = parseTime(hour, minutes, ampm);
|
|
820
|
+
return `${m2} ${h} * * *`;
|
|
821
|
+
}
|
|
822
|
+
},
|
|
823
|
+
// "every weekday at 9am"
|
|
824
|
+
{
|
|
825
|
+
regex: /^every weekday at (\d{1,2})(:\d{2})?\s*(am|pm)?$/i,
|
|
826
|
+
toCron: (hour, minutes, ampm) => {
|
|
827
|
+
const { h, m: m2 } = parseTime(hour, minutes, ampm);
|
|
828
|
+
return `${m2} ${h} * * 1-5`;
|
|
829
|
+
}
|
|
830
|
+
},
|
|
831
|
+
// "every monday at 9am", "every friday at 2pm"
|
|
832
|
+
{
|
|
833
|
+
regex: /^every (monday|tuesday|wednesday|thursday|friday|saturday|sunday) at (\d{1,2})(:\d{2})?\s*(am|pm)?$/i,
|
|
834
|
+
toCron: (day, hour, minutes, ampm) => {
|
|
835
|
+
const { h, m: m2 } = parseTime(hour, minutes, ampm);
|
|
836
|
+
return `${m2} ${h} * * ${dayToNumber(day)}`;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
];
|
|
840
|
+
var DAY_MAP = {
|
|
841
|
+
sunday: 0,
|
|
842
|
+
monday: 1,
|
|
843
|
+
tuesday: 2,
|
|
844
|
+
wednesday: 3,
|
|
845
|
+
thursday: 4,
|
|
846
|
+
friday: 5,
|
|
847
|
+
saturday: 6
|
|
848
|
+
};
|
|
849
|
+
function dayToNumber(day) {
|
|
850
|
+
const num = DAY_MAP[day.toLowerCase()];
|
|
851
|
+
if (num === void 0) {
|
|
852
|
+
throw new Error(`Unknown day: ${day}`);
|
|
853
|
+
}
|
|
854
|
+
return num;
|
|
855
|
+
}
|
|
856
|
+
function parseTime(hourStr, minutesStr, ampm) {
|
|
857
|
+
let h = Number.parseInt(hourStr, 10);
|
|
858
|
+
const m2 = minutesStr ? Number.parseInt(minutesStr.slice(1), 10) : 0;
|
|
859
|
+
if (ampm) {
|
|
860
|
+
const period = ampm.toLowerCase();
|
|
861
|
+
if (period === "pm" && h !== 12) {
|
|
862
|
+
h += 12;
|
|
863
|
+
}
|
|
864
|
+
if (period === "am" && h === 12) {
|
|
865
|
+
h = 0;
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
if (h < 0 || h > 23) {
|
|
869
|
+
throw new Error(`Invalid hour: ${h}`);
|
|
870
|
+
}
|
|
871
|
+
if (m2 < 0 || m2 > 59) {
|
|
872
|
+
throw new Error(`Invalid minutes: ${m2}`);
|
|
873
|
+
}
|
|
874
|
+
return { h, m: m2 };
|
|
875
|
+
}
|
|
876
|
+
function parsePlainEnglish(expression) {
|
|
877
|
+
for (const { regex, toCron } of PLAIN_ENGLISH_PATTERNS) {
|
|
878
|
+
const match = expression.match(regex);
|
|
879
|
+
if (match) {
|
|
880
|
+
const args = match.slice(1).map((v2) => v2 ?? "");
|
|
881
|
+
return toCron(...args);
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
return expression;
|
|
885
|
+
}
|
|
886
|
+
function validateCron(expression) {
|
|
887
|
+
try {
|
|
888
|
+
new E(expression);
|
|
889
|
+
} catch (error) {
|
|
890
|
+
throw new Error(
|
|
891
|
+
`Invalid cron expression "${expression}": ${error instanceof Error ? error.message : String(error)}`
|
|
892
|
+
);
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
function nextRun(expression, timezone = "UTC") {
|
|
896
|
+
const cron = new E(expression, { timezone });
|
|
897
|
+
return cron.nextRun();
|
|
898
|
+
}
|
|
899
|
+
function nextRuns(expression, count, timezone = "UTC") {
|
|
900
|
+
const cron = new E(expression, { timezone });
|
|
901
|
+
return cron.nextRuns(count);
|
|
902
|
+
}
|
|
903
|
+
function normalizeInput(input) {
|
|
904
|
+
if (typeof input === "string") {
|
|
905
|
+
return { cron: input };
|
|
906
|
+
}
|
|
907
|
+
return input;
|
|
908
|
+
}
|
|
909
|
+
function resolveSchedule(input) {
|
|
910
|
+
const config = normalizeInput(input);
|
|
911
|
+
const cronExpression = parsePlainEnglish(config.cron);
|
|
912
|
+
validateCron(cronExpression);
|
|
913
|
+
return {
|
|
914
|
+
cronExpression,
|
|
915
|
+
timezone: config.timezone ?? "UTC",
|
|
916
|
+
enabled: config.enabled ?? true,
|
|
917
|
+
overlap: config.overlap ?? "skip",
|
|
918
|
+
timeoutMs: config.timeout != null ? parseDelay(config.timeout) : null
|
|
919
|
+
};
|
|
920
|
+
}
|
|
921
|
+
function extractSchedules(router) {
|
|
922
|
+
const schedules = [];
|
|
923
|
+
for (const [jobName, definition] of Object.entries(router)) {
|
|
924
|
+
if (definition.schedule != null) {
|
|
925
|
+
schedules.push({
|
|
926
|
+
jobName,
|
|
927
|
+
schedule: resolveSchedule(definition.schedule)
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
return schedules;
|
|
932
|
+
}
|
|
933
|
+
|
|
137
934
|
// src/webhook.ts
|
|
138
935
|
var import_node_crypto2 = require("crypto");
|
|
139
936
|
var WEBHOOK_HEADERS = {
|
|
@@ -151,8 +948,8 @@ function signPayload(payload, secret) {
|
|
|
151
948
|
}
|
|
152
949
|
function verifySignature(payload, signature, secret, toleranceSeconds = DEFAULT_TOLERANCE_SECONDS) {
|
|
153
950
|
const parts = signature.split(",");
|
|
154
|
-
const tPart = parts.find((
|
|
155
|
-
const v1Part = parts.find((
|
|
951
|
+
const tPart = parts.find((p2) => p2.startsWith("t="));
|
|
952
|
+
const v1Part = parts.find((p2) => p2.startsWith("v1="));
|
|
156
953
|
if (!tPart || !v1Part) {
|
|
157
954
|
return false;
|
|
158
955
|
}
|
|
@@ -239,11 +1036,17 @@ async function processJobCallback(router, request, options) {
|
|
|
239
1036
|
WEBHOOK_HEADERS,
|
|
240
1037
|
calculateBackoff,
|
|
241
1038
|
createJobRouter,
|
|
1039
|
+
extractSchedules,
|
|
242
1040
|
generateJobId,
|
|
243
1041
|
generatePublicId,
|
|
244
1042
|
job,
|
|
1043
|
+
nextRun,
|
|
1044
|
+
nextRuns,
|
|
245
1045
|
parseDelay,
|
|
1046
|
+
parsePlainEnglish,
|
|
246
1047
|
processJobCallback,
|
|
1048
|
+
resolveSchedule,
|
|
247
1049
|
signPayload,
|
|
1050
|
+
validateCron,
|
|
248
1051
|
verifySignature
|
|
249
1052
|
});
|