cron 3.0.0-beta.5 → 3.0.0-beta.6
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/CHANGELOG.md +16 -0
- package/README.md +66 -52
- package/dist/constants.d.ts +74 -0
- package/dist/constants.js +78 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +13 -0
- package/dist/job.d.ts +24 -0
- package/dist/job.js +145 -0
- package/dist/time.d.ts +31 -0
- package/dist/time.js +450 -0
- package/dist/types/cron.types.d.ts +38 -0
- package/dist/types/cron.types.js +3 -0
- package/dist/types/utils.d.ts +2 -0
- package/dist/types/utils.js +3 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +8 -0
- package/package.json +16 -27
- package/lib/cron.js +0 -55
- package/lib/job.js +0 -215
- package/lib/time.js +0 -843
- package/types/index.d.ts +0 -181
- package/types/index.test-d.ts +0 -85
package/dist/time.js
ADDED
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CronTime = void 0;
|
|
4
|
+
const luxon_1 = require("luxon");
|
|
5
|
+
const constants_1 = require("./constants");
|
|
6
|
+
const utils_1 = require("./utils");
|
|
7
|
+
class CronTime {
|
|
8
|
+
constructor(source, zone, utcOffset) {
|
|
9
|
+
this.realDate = false;
|
|
10
|
+
this.second = {};
|
|
11
|
+
this.minute = {};
|
|
12
|
+
this.hour = {};
|
|
13
|
+
this.dayOfMonth = {};
|
|
14
|
+
this.month = {};
|
|
15
|
+
this.dayOfWeek = {};
|
|
16
|
+
if (zone) {
|
|
17
|
+
const dt = luxon_1.DateTime.fromObject({}, { zone });
|
|
18
|
+
if (!dt.isValid) {
|
|
19
|
+
throw new Error('Invalid timezone.');
|
|
20
|
+
}
|
|
21
|
+
this.zone = zone;
|
|
22
|
+
}
|
|
23
|
+
if (utcOffset != null) {
|
|
24
|
+
this.utcOffset = utcOffset;
|
|
25
|
+
}
|
|
26
|
+
if (source instanceof Date || source instanceof luxon_1.DateTime) {
|
|
27
|
+
this.source =
|
|
28
|
+
source instanceof Date ? luxon_1.DateTime.fromJSDate(source) : source;
|
|
29
|
+
this.realDate = true;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
this.source = source;
|
|
33
|
+
this._parse(this.source);
|
|
34
|
+
this._verifyParse();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
_getWeekDay(date) {
|
|
38
|
+
return date.weekday === 7 ? 0 : date.weekday;
|
|
39
|
+
}
|
|
40
|
+
_verifyParse() {
|
|
41
|
+
const months = (0, utils_1.getRecordKeys)(this.month);
|
|
42
|
+
const daysOfMonth = (0, utils_1.getRecordKeys)(this.dayOfMonth);
|
|
43
|
+
let isOk = false;
|
|
44
|
+
let lastWrongMonth = null;
|
|
45
|
+
for (const m of months) {
|
|
46
|
+
const con = constants_1.MONTH_CONSTRAINTS[m];
|
|
47
|
+
for (const day of daysOfMonth) {
|
|
48
|
+
if (day <= con) {
|
|
49
|
+
isOk = true;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (!isOk) {
|
|
53
|
+
lastWrongMonth = m;
|
|
54
|
+
console.warn(`Month '${m}' is limited to '${con}' days.`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (!isOk && lastWrongMonth !== null) {
|
|
58
|
+
const notOkCon = constants_1.MONTH_CONSTRAINTS[lastWrongMonth];
|
|
59
|
+
for (const notOkDay of daysOfMonth) {
|
|
60
|
+
if (notOkDay > notOkCon) {
|
|
61
|
+
delete this.dayOfMonth[notOkDay];
|
|
62
|
+
const fixedDay = (notOkDay % notOkCon);
|
|
63
|
+
this.dayOfMonth[fixedDay] = true;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
sendAt(i) {
|
|
69
|
+
let date = this.realDate && this.source instanceof luxon_1.DateTime
|
|
70
|
+
? this.source
|
|
71
|
+
: luxon_1.DateTime.local();
|
|
72
|
+
if (this.zone) {
|
|
73
|
+
date = date.setZone(this.zone);
|
|
74
|
+
}
|
|
75
|
+
if (this.utcOffset != null) {
|
|
76
|
+
const offsetHours = parseInt(this.utcOffset >= 60 || this.utcOffset <= -60
|
|
77
|
+
?
|
|
78
|
+
this.utcOffset / 60
|
|
79
|
+
: this.utcOffset);
|
|
80
|
+
const offsetMins = this.utcOffset >= 60 || this.utcOffset <= -60
|
|
81
|
+
?
|
|
82
|
+
Math.abs(this.utcOffset - offsetHours * 60)
|
|
83
|
+
: 0;
|
|
84
|
+
const offsetMinsStr = offsetMins >= 10 ? offsetMins : `0${offsetMins}`;
|
|
85
|
+
let utcZone = 'UTC';
|
|
86
|
+
if (parseInt(this.utcOffset) < 0) {
|
|
87
|
+
utcZone += `${offsetHours === 0 ? '-0' : offsetHours}:${offsetMinsStr}`;
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
utcZone += `+${offsetHours}:${offsetMinsStr}`;
|
|
91
|
+
}
|
|
92
|
+
date = date.setZone(utcZone);
|
|
93
|
+
if (!date.isValid) {
|
|
94
|
+
throw new Error('ERROR: You specified an invalid UTC offset.');
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (this.realDate) {
|
|
98
|
+
if (luxon_1.DateTime.local() > date) {
|
|
99
|
+
throw new Error('WARNING: Date in past. Will never be fired.');
|
|
100
|
+
}
|
|
101
|
+
return date;
|
|
102
|
+
}
|
|
103
|
+
if (i === undefined || isNaN(i) || i < 0) {
|
|
104
|
+
return this.getNextDateFrom(date);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
const dates = [];
|
|
108
|
+
for (; i > 0; i--) {
|
|
109
|
+
date = this.getNextDateFrom(date);
|
|
110
|
+
dates.push(date);
|
|
111
|
+
}
|
|
112
|
+
return dates;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
getTimeout() {
|
|
116
|
+
return Math.max(-1, this.sendAt().toMillis() - luxon_1.DateTime.local().toMillis());
|
|
117
|
+
}
|
|
118
|
+
toString() {
|
|
119
|
+
return this.toJSON().join(' ');
|
|
120
|
+
}
|
|
121
|
+
toJSON() {
|
|
122
|
+
return constants_1.TIME_UNITS.map(unit => {
|
|
123
|
+
return this._wcOrAll(unit);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
getNextDateFrom(start, zone) {
|
|
127
|
+
var _a;
|
|
128
|
+
if (start instanceof Date) {
|
|
129
|
+
start = luxon_1.DateTime.fromJSDate(start);
|
|
130
|
+
}
|
|
131
|
+
let date = start;
|
|
132
|
+
const firstDate = start.toMillis();
|
|
133
|
+
if (zone) {
|
|
134
|
+
date = date.setZone(zone);
|
|
135
|
+
}
|
|
136
|
+
if (!this.realDate) {
|
|
137
|
+
if (date.millisecond > 0) {
|
|
138
|
+
date = date.set({ millisecond: 0, second: date.second + 1 });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (!date.isValid) {
|
|
142
|
+
throw new Error('ERROR: You specified an invalid date.');
|
|
143
|
+
}
|
|
144
|
+
const maxMatch = luxon_1.DateTime.now().plus({ years: 8 });
|
|
145
|
+
while (true) {
|
|
146
|
+
const diff = date.toMillis() - start.toMillis();
|
|
147
|
+
if (date > maxMatch) {
|
|
148
|
+
throw new Error(`Something went wrong. No execution date was found in the next 8 years.
|
|
149
|
+
Please provide the following string if you would like to help debug:
|
|
150
|
+
Time Zone: ${(_a = zone === null || zone === void 0 ? void 0 : zone.toString()) !== null && _a !== void 0 ? _a : '""'} - Cron String: ${this.source.toString()} - UTC offset: ${date.offset} - current Date: ${luxon_1.DateTime.local().toString()}`);
|
|
151
|
+
}
|
|
152
|
+
if (!(date.month in this.month) &&
|
|
153
|
+
Object.keys(this.month).length !== 12) {
|
|
154
|
+
date = date.plus({ months: 1 });
|
|
155
|
+
date = date.set({ day: 1, hour: 0, minute: 0, second: 0 });
|
|
156
|
+
if (this._forwardDSTJump(0, 0, date)) {
|
|
157
|
+
const [isDone, newDate] = this._findPreviousDSTJump(date);
|
|
158
|
+
date = newDate;
|
|
159
|
+
if (isDone)
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (!(date.day in this.dayOfMonth) &&
|
|
165
|
+
Object.keys(this.dayOfMonth).length !== 31 &&
|
|
166
|
+
!(this._getWeekDay(date) in this.dayOfWeek &&
|
|
167
|
+
Object.keys(this.dayOfWeek).length !== 7)) {
|
|
168
|
+
date = date.plus({ days: 1 });
|
|
169
|
+
date = date.set({ hour: 0, minute: 0, second: 0 });
|
|
170
|
+
if (this._forwardDSTJump(0, 0, date)) {
|
|
171
|
+
const [isDone, newDate] = this._findPreviousDSTJump(date);
|
|
172
|
+
date = newDate;
|
|
173
|
+
if (isDone)
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
if (!(this._getWeekDay(date) in this.dayOfWeek) &&
|
|
179
|
+
Object.keys(this.dayOfWeek).length !== 7 &&
|
|
180
|
+
!(date.day in this.dayOfMonth &&
|
|
181
|
+
Object.keys(this.dayOfMonth).length !== 31)) {
|
|
182
|
+
date = date.plus({ days: 1 });
|
|
183
|
+
date = date.set({ hour: 0, minute: 0, second: 0 });
|
|
184
|
+
if (this._forwardDSTJump(0, 0, date)) {
|
|
185
|
+
const [isDone, newDate] = this._findPreviousDSTJump(date);
|
|
186
|
+
date = newDate;
|
|
187
|
+
if (isDone)
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
if (!(date.hour in this.hour) && Object.keys(this.hour).length !== 24) {
|
|
193
|
+
const expectedHour = date.hour === 23 && diff > 86400000 ? 0 : date.hour + 1;
|
|
194
|
+
const expectedMinute = date.minute;
|
|
195
|
+
date = date.set({ hour: expectedHour });
|
|
196
|
+
date = date.set({ minute: 0, second: 0 });
|
|
197
|
+
if (this._forwardDSTJump(expectedHour, expectedMinute, date)) {
|
|
198
|
+
const [isDone, newDate] = this._findPreviousDSTJump(date);
|
|
199
|
+
date = newDate;
|
|
200
|
+
if (isDone)
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
if (!(date.minute in this.minute) &&
|
|
206
|
+
Object.keys(this.minute).length !== 60) {
|
|
207
|
+
const expectedMinute = date.minute === 59 && diff > 3600000 ? 0 : date.minute + 1;
|
|
208
|
+
const expectedHour = date.hour + (expectedMinute === 60 ? 1 : 0);
|
|
209
|
+
date = date.set({ minute: expectedMinute });
|
|
210
|
+
date = date.set({ second: 0 });
|
|
211
|
+
if (this._forwardDSTJump(expectedHour, expectedMinute, date)) {
|
|
212
|
+
const [isDone, newDate] = this._findPreviousDSTJump(date);
|
|
213
|
+
date = newDate;
|
|
214
|
+
if (isDone)
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
if (!(date.second in this.second) &&
|
|
220
|
+
Object.keys(this.second).length !== 60) {
|
|
221
|
+
const expectedSecond = date.second === 59 && diff > 60000 ? 0 : date.second + 1;
|
|
222
|
+
const expectedMinute = date.minute + (expectedSecond === 60 ? 1 : 0);
|
|
223
|
+
const expectedHour = date.hour + (expectedMinute === 60 ? 1 : 0);
|
|
224
|
+
date = date.set({ second: expectedSecond });
|
|
225
|
+
if (this._forwardDSTJump(expectedHour, expectedMinute, date)) {
|
|
226
|
+
const [isDone, newDate] = this._findPreviousDSTJump(date);
|
|
227
|
+
date = newDate;
|
|
228
|
+
if (isDone)
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
if (date.toMillis() === firstDate) {
|
|
234
|
+
const expectedSecond = date.second + 1;
|
|
235
|
+
const expectedMinute = date.minute + (expectedSecond === 60 ? 1 : 0);
|
|
236
|
+
const expectedHour = date.hour + (expectedMinute === 60 ? 1 : 0);
|
|
237
|
+
date = date.set({ second: expectedSecond });
|
|
238
|
+
if (this._forwardDSTJump(expectedHour, expectedMinute, date)) {
|
|
239
|
+
const [isDone, newDate] = this._findPreviousDSTJump(date);
|
|
240
|
+
date = newDate;
|
|
241
|
+
if (isDone)
|
|
242
|
+
break;
|
|
243
|
+
}
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
return date;
|
|
249
|
+
}
|
|
250
|
+
_findPreviousDSTJump(date) {
|
|
251
|
+
var _a;
|
|
252
|
+
let expectedMinute, expectedHour, actualMinute, actualHour;
|
|
253
|
+
let maybeJumpingPoint = date;
|
|
254
|
+
const iterationLimit = 60 * 24;
|
|
255
|
+
let iteration = 0;
|
|
256
|
+
do {
|
|
257
|
+
if (++iteration > iterationLimit) {
|
|
258
|
+
throw new Error(`ERROR: This DST checking related function assumes the input DateTime (${(_a = date.toISO()) !== null && _a !== void 0 ? _a : date.toMillis()}) is within 24 hours of a DST jump.`);
|
|
259
|
+
}
|
|
260
|
+
expectedMinute = maybeJumpingPoint.minute - 1;
|
|
261
|
+
expectedHour = maybeJumpingPoint.hour;
|
|
262
|
+
if (expectedMinute < 0) {
|
|
263
|
+
expectedMinute += 60;
|
|
264
|
+
expectedHour = (expectedHour + 24 - 1) % 24;
|
|
265
|
+
}
|
|
266
|
+
maybeJumpingPoint = maybeJumpingPoint.minus({ minute: 1 });
|
|
267
|
+
actualMinute = maybeJumpingPoint.minute;
|
|
268
|
+
actualHour = maybeJumpingPoint.hour;
|
|
269
|
+
} while (expectedMinute === actualMinute && expectedHour === actualHour);
|
|
270
|
+
const afterJumpingPoint = maybeJumpingPoint
|
|
271
|
+
.plus({ minute: 1 })
|
|
272
|
+
.set({ second: 0, millisecond: 0 });
|
|
273
|
+
const beforeJumpingPoint = afterJumpingPoint.minus({ second: 1 });
|
|
274
|
+
if (date.month + 1 in this.month &&
|
|
275
|
+
date.day in this.dayOfMonth &&
|
|
276
|
+
this._getWeekDay(date) in this.dayOfWeek) {
|
|
277
|
+
return [
|
|
278
|
+
this._checkTimeInSkippedRange(beforeJumpingPoint, afterJumpingPoint),
|
|
279
|
+
afterJumpingPoint
|
|
280
|
+
];
|
|
281
|
+
}
|
|
282
|
+
return [false, afterJumpingPoint];
|
|
283
|
+
}
|
|
284
|
+
_checkTimeInSkippedRange(beforeJumpingPoint, afterJumpingPoint) {
|
|
285
|
+
const startingMinute = (beforeJumpingPoint.minute + 1) % 60;
|
|
286
|
+
const startingHour = (beforeJumpingPoint.hour + (startingMinute === 0 ? 1 : 0)) % 24;
|
|
287
|
+
const hourRangeSize = afterJumpingPoint.hour - startingHour + 1;
|
|
288
|
+
const isHourJump = startingMinute === 0 && afterJumpingPoint.minute === 0;
|
|
289
|
+
if (hourRangeSize === 2 && isHourJump) {
|
|
290
|
+
return startingHour in this.hour;
|
|
291
|
+
}
|
|
292
|
+
else if (hourRangeSize === 1) {
|
|
293
|
+
return (startingHour in this.hour &&
|
|
294
|
+
this._checkTimeInSkippedRangeSingleHour(startingMinute, afterJumpingPoint.minute));
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
return this._checkTimeInSkippedRangeMultiHour(startingHour, startingMinute, afterJumpingPoint.hour, afterJumpingPoint.minute);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
_checkTimeInSkippedRangeSingleHour(startMinute, endMinute) {
|
|
301
|
+
for (let minute = startMinute; minute < endMinute; ++minute) {
|
|
302
|
+
if (minute in this.minute)
|
|
303
|
+
return true;
|
|
304
|
+
}
|
|
305
|
+
return endMinute in this.minute && 0 in this.second;
|
|
306
|
+
}
|
|
307
|
+
_checkTimeInSkippedRangeMultiHour(startHour, startMinute, endHour, endMinute) {
|
|
308
|
+
if (startHour >= endHour) {
|
|
309
|
+
throw new Error(`ERROR: This DST checking related function assumes the forward jump starting hour (${startHour}) is less than the end hour (${endHour})`);
|
|
310
|
+
}
|
|
311
|
+
const firstHourMinuteRange = Array.from({ length: 60 - startMinute }, (_, k) => startMinute + k);
|
|
312
|
+
const lastHourMinuteRange = Array.from({ length: endMinute }, (_, k) => k);
|
|
313
|
+
const middleHourMinuteRange = Array.from({ length: 60 }, (_, k) => k);
|
|
314
|
+
const selectRange = (forHour) => {
|
|
315
|
+
if (forHour === startHour) {
|
|
316
|
+
return firstHourMinuteRange;
|
|
317
|
+
}
|
|
318
|
+
else if (forHour === endHour) {
|
|
319
|
+
return lastHourMinuteRange;
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
return middleHourMinuteRange;
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
for (let hour = startHour; hour <= endHour; ++hour) {
|
|
326
|
+
if (!(hour in this.hour))
|
|
327
|
+
continue;
|
|
328
|
+
const usingRange = selectRange(hour);
|
|
329
|
+
for (const minute of usingRange) {
|
|
330
|
+
if (minute in this.minute)
|
|
331
|
+
return true;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return endHour in this.hour && endMinute in this.minute && 0 in this.second;
|
|
335
|
+
}
|
|
336
|
+
_forwardDSTJump(expectedHour, expectedMinute, actualDate) {
|
|
337
|
+
const actualHour = actualDate.hour;
|
|
338
|
+
const actualMinute = actualDate.minute;
|
|
339
|
+
const didHoursJumped = expectedHour % 24 < actualHour;
|
|
340
|
+
const didMinutesJumped = expectedMinute % 60 < actualMinute;
|
|
341
|
+
return didHoursJumped || didMinutesJumped;
|
|
342
|
+
}
|
|
343
|
+
_wcOrAll(unit) {
|
|
344
|
+
if (this._hasAll(unit)) {
|
|
345
|
+
return '*';
|
|
346
|
+
}
|
|
347
|
+
const all = [];
|
|
348
|
+
for (const time in this[unit]) {
|
|
349
|
+
all.push(time);
|
|
350
|
+
}
|
|
351
|
+
return all.join(',');
|
|
352
|
+
}
|
|
353
|
+
_hasAll(unit) {
|
|
354
|
+
const constraints = constants_1.CONSTRAINTS[unit];
|
|
355
|
+
const low = constraints[0];
|
|
356
|
+
const high = unit === constants_1.TIME_UNITS_MAP.DAY_OF_WEEK ? constraints[1] - 1 : constraints[1];
|
|
357
|
+
for (let i = low, n = high; i < n; i++) {
|
|
358
|
+
if (!(i in this[unit])) {
|
|
359
|
+
return false;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return true;
|
|
363
|
+
}
|
|
364
|
+
_parse(source) {
|
|
365
|
+
var _a;
|
|
366
|
+
source = source.toLowerCase();
|
|
367
|
+
if (Object.keys(constants_1.PRESETS).includes(source)) {
|
|
368
|
+
source = constants_1.PRESETS[source];
|
|
369
|
+
}
|
|
370
|
+
source = source.replace(/[a-z]{1,3}/gi, (alias) => {
|
|
371
|
+
if (Object.keys(constants_1.ALIASES).includes(alias)) {
|
|
372
|
+
return constants_1.ALIASES[alias].toString();
|
|
373
|
+
}
|
|
374
|
+
throw new Error(`Unknown alias: ${alias}`);
|
|
375
|
+
});
|
|
376
|
+
const units = source.trim().split(/\s+/);
|
|
377
|
+
if (units.length < constants_1.TIME_UNITS_LEN - 1) {
|
|
378
|
+
throw new Error('Too few fields');
|
|
379
|
+
}
|
|
380
|
+
if (units.length > constants_1.TIME_UNITS_LEN) {
|
|
381
|
+
throw new Error('Too many fields');
|
|
382
|
+
}
|
|
383
|
+
const unitsLen = units.length;
|
|
384
|
+
for (const unit of constants_1.TIME_UNITS) {
|
|
385
|
+
const i = constants_1.TIME_UNITS.indexOf(unit);
|
|
386
|
+
const cur = (_a = units[i - (constants_1.TIME_UNITS_LEN - unitsLen)]) !== null && _a !== void 0 ? _a : constants_1.PARSE_DEFAULTS[unit];
|
|
387
|
+
this._parseField(cur, unit);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
_parseField(value, unit) {
|
|
391
|
+
const typeObj = this[unit];
|
|
392
|
+
let pointer;
|
|
393
|
+
const constraints = constants_1.CONSTRAINTS[unit];
|
|
394
|
+
const low = constraints[0];
|
|
395
|
+
const high = constraints[1];
|
|
396
|
+
const fields = value.split(',');
|
|
397
|
+
fields.forEach(field => {
|
|
398
|
+
const wildcardIndex = field.indexOf('*');
|
|
399
|
+
if (wildcardIndex !== -1 && wildcardIndex !== 0) {
|
|
400
|
+
throw new Error(`Field (${field}) has an invalid wildcard expression`);
|
|
401
|
+
}
|
|
402
|
+
});
|
|
403
|
+
value = value.replace(constants_1.RE_WILDCARDS, `${low}-${high}`);
|
|
404
|
+
const allRanges = value.split(',');
|
|
405
|
+
for (const range of allRanges) {
|
|
406
|
+
const match = [...range.matchAll(constants_1.RE_RANGE)][0];
|
|
407
|
+
if ((match === null || match === void 0 ? void 0 : match[1]) !== undefined) {
|
|
408
|
+
const [, mLower, mUpper, mStep] = match;
|
|
409
|
+
let lower = parseInt(mLower, 10);
|
|
410
|
+
let upper = mUpper !== undefined ? parseInt(mUpper, 10) : undefined;
|
|
411
|
+
const wasStepDefined = mStep !== undefined;
|
|
412
|
+
if (mStep === '0') {
|
|
413
|
+
throw new Error(`Field (${unit}) has a step of zero`);
|
|
414
|
+
}
|
|
415
|
+
const step = parseInt(mStep !== null && mStep !== void 0 ? mStep : '1', 10);
|
|
416
|
+
if (upper !== undefined && lower > upper) {
|
|
417
|
+
throw new Error(`Field (${unit}) has an invalid range`);
|
|
418
|
+
}
|
|
419
|
+
const isOutOfRange = lower < low ||
|
|
420
|
+
(upper !== undefined && upper > high) ||
|
|
421
|
+
(upper === undefined && lower > high);
|
|
422
|
+
if (isOutOfRange) {
|
|
423
|
+
throw new Error(`Field value (${value}) is out of range`);
|
|
424
|
+
}
|
|
425
|
+
lower = Math.min(Math.max(low, ~~Math.abs(lower)), high);
|
|
426
|
+
if (upper !== undefined) {
|
|
427
|
+
upper = Math.min(high, ~~Math.abs(upper));
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
upper = wasStepDefined ? high : lower;
|
|
431
|
+
}
|
|
432
|
+
pointer = lower;
|
|
433
|
+
do {
|
|
434
|
+
typeObj[pointer] = true;
|
|
435
|
+
pointer += step;
|
|
436
|
+
} while (pointer <= upper);
|
|
437
|
+
if (unit === 'dayOfWeek') {
|
|
438
|
+
if (!typeObj[0] && !!typeObj[7])
|
|
439
|
+
typeObj[0] = typeObj[7];
|
|
440
|
+
delete typeObj[7];
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
else {
|
|
444
|
+
throw new Error(`Field (${unit}) cannot be parsed`);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
exports.CronTime = CronTime;
|
|
450
|
+
//# sourceMappingURL=time.js.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { SpawnOptions } from 'child_process';
|
|
3
|
+
import { DateTime } from 'luxon';
|
|
4
|
+
import { CONSTRAINTS, TIME_UNITS_MAP } from '../constants';
|
|
5
|
+
import { CronJob } from '../job';
|
|
6
|
+
import { IntRange } from './utils';
|
|
7
|
+
export interface CronJobParams {
|
|
8
|
+
cronTime: string | Date | DateTime;
|
|
9
|
+
onTick: CronCommand;
|
|
10
|
+
onComplete?: CronCommand | null;
|
|
11
|
+
start?: boolean | null;
|
|
12
|
+
timeZone?: string | null;
|
|
13
|
+
context?: unknown | null;
|
|
14
|
+
runOnInit?: boolean | null;
|
|
15
|
+
utcOffset?: string | number | null;
|
|
16
|
+
unrefTimeout?: boolean | null;
|
|
17
|
+
}
|
|
18
|
+
export type CronCommand = ((this: CronJob | any) => void) | string | {
|
|
19
|
+
command: string;
|
|
20
|
+
args?: readonly string[] | null;
|
|
21
|
+
options?: SpawnOptions | null;
|
|
22
|
+
};
|
|
23
|
+
export type TimeUnit = (typeof TIME_UNITS_MAP)[keyof typeof TIME_UNITS_MAP];
|
|
24
|
+
export type TimeUnitField<T extends TimeUnit> = Partial<Record<Ranges[T], boolean>>;
|
|
25
|
+
export interface Ranges {
|
|
26
|
+
second: SecondRange;
|
|
27
|
+
minute: MinuteRange;
|
|
28
|
+
hour: HourRange;
|
|
29
|
+
dayOfMonth: DayOfMonthRange;
|
|
30
|
+
month: MonthRange;
|
|
31
|
+
dayOfWeek: DayOfWeekRange;
|
|
32
|
+
}
|
|
33
|
+
export type SecondRange = IntRange<(typeof CONSTRAINTS)['second'][0], (typeof CONSTRAINTS)['second'][1]>;
|
|
34
|
+
export type MinuteRange = IntRange<(typeof CONSTRAINTS)['minute'][0], (typeof CONSTRAINTS)['minute'][1]>;
|
|
35
|
+
export type HourRange = IntRange<(typeof CONSTRAINTS)['hour'][0], (typeof CONSTRAINTS)['hour'][1]>;
|
|
36
|
+
export type DayOfMonthRange = IntRange<(typeof CONSTRAINTS)['dayOfMonth'][0], (typeof CONSTRAINTS)['dayOfMonth'][1]>;
|
|
37
|
+
export type MonthRange = IntRange<(typeof CONSTRAINTS)['month'][0], (typeof CONSTRAINTS)['month'][1]>;
|
|
38
|
+
export type DayOfWeekRange = IntRange<(typeof CONSTRAINTS)['dayOfWeek'][0], (typeof CONSTRAINTS)['dayOfWeek'][1]>;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export type Enumerate<N extends number, WithTail extends boolean = true, Acc extends number[] = []> = Acc['length'] extends N ? WithTail extends true ? [...Acc, Acc['length']][number] : Acc[number] : Enumerate<N, WithTail, [...Acc, Acc['length']]>;
|
|
2
|
+
export type IntRange<F extends number, T extends number> = Exclude<Enumerate<T>, Enumerate<F, false>>;
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getRecordKeys: <K extends 0 | 59 | 2 | 23 | 1 | 31 | 12 | 7 | 29 | 30 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 24 | 25 | 26 | 27 | 28 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58>(record: Partial<Record<K, boolean>>) => K[];
|
package/dist/utils.js
ADDED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cron",
|
|
3
3
|
"description": "Cron jobs for your node",
|
|
4
|
-
"version": "3.0.0-beta.
|
|
4
|
+
"version": "3.0.0-beta.6",
|
|
5
5
|
"author": "Nick Campbell <nicholas.j.campbell@gmail.com> (https://github.com/ncb000gt)",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/kelektiv/node-cron/issues"
|
|
@@ -10,16 +10,18 @@
|
|
|
10
10
|
"type": "git",
|
|
11
11
|
"url": "https://github.com/kelektiv/node-cron.git"
|
|
12
12
|
},
|
|
13
|
-
"main": "
|
|
13
|
+
"main": "dist/index",
|
|
14
14
|
"scripts": {
|
|
15
|
-
"
|
|
15
|
+
"build": "tsc -b tsconfig.build.json",
|
|
16
|
+
"lint:eslint": "eslint src/ tests/ --ext .ts",
|
|
17
|
+
"lint:prettier": "prettier ./**/*.{json,md,yml} --check",
|
|
18
|
+
"lint": "npm run lint:eslint && npm run lint:prettier",
|
|
19
|
+
"lint:fix": "npm run lint:eslint -- --fix && npm run lint:prettier -- --write",
|
|
16
20
|
"test": "jest --coverage",
|
|
17
21
|
"test:watch": "jest --watch --coverage",
|
|
18
|
-
"test:types": "tsd",
|
|
19
22
|
"prepare": "husky install",
|
|
20
23
|
"release": "semantic-release"
|
|
21
24
|
},
|
|
22
|
-
"types": "types/index.d.ts",
|
|
23
25
|
"dependencies": {
|
|
24
26
|
"@types/luxon": "~3.3.0",
|
|
25
27
|
"luxon": "~3.3.0"
|
|
@@ -34,21 +36,22 @@
|
|
|
34
36
|
"@semantic-release/github": "~8.1.x",
|
|
35
37
|
"@semantic-release/npm": "~10.0.x",
|
|
36
38
|
"@semantic-release/release-notes-generator": "~11.0.x",
|
|
39
|
+
"@types/jest": "^29.5.5",
|
|
40
|
+
"@types/node": "^20.6.0",
|
|
41
|
+
"@types/sinon": "^10.0.16",
|
|
42
|
+
"@typescript-eslint/eslint-plugin": "^5.62.0",
|
|
37
43
|
"chai": "~4.2.x",
|
|
38
44
|
"eslint": "~8.36.x",
|
|
39
45
|
"eslint-config-prettier": "^8.7.x",
|
|
40
|
-
"eslint-
|
|
41
|
-
"eslint-plugin-import": "~2.27.x",
|
|
42
|
-
"eslint-plugin-jest": "~27.2.x",
|
|
43
|
-
"eslint-plugin-n": "~15.6.x",
|
|
46
|
+
"eslint-plugin-jest": "^27.4.0",
|
|
44
47
|
"eslint-plugin-prettier": "~4.2.x",
|
|
45
|
-
"eslint-plugin-promise": "~6.1.x",
|
|
46
48
|
"husky": "^8.0.3",
|
|
47
49
|
"jest": "~29.5.x",
|
|
48
50
|
"prettier": "~2.8.x",
|
|
49
51
|
"semantic-release": "~21.0.x",
|
|
50
52
|
"sinon": "^15.0.x",
|
|
51
|
-
"
|
|
53
|
+
"ts-jest": "^29.1.1",
|
|
54
|
+
"typescript": "~5.1.6"
|
|
52
55
|
},
|
|
53
56
|
"keywords": [
|
|
54
57
|
"cron",
|
|
@@ -77,23 +80,9 @@
|
|
|
77
80
|
"Masakazu Matsushita <matsukaz@gmail.com> (matsukaz)",
|
|
78
81
|
"Christopher Lunt <me@kirisu.co.uk> (https://github.com/kirisu)"
|
|
79
82
|
],
|
|
80
|
-
"jest": {
|
|
81
|
-
"collectCoverage": true,
|
|
82
|
-
"collectCoverageFrom": [
|
|
83
|
-
"lib/*.js"
|
|
84
|
-
],
|
|
85
|
-
"coverageThreshold": {
|
|
86
|
-
"global": {
|
|
87
|
-
"statements": 80,
|
|
88
|
-
"branches": 80,
|
|
89
|
-
"functions": 70,
|
|
90
|
-
"lines": 80
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
},
|
|
94
83
|
"files": [
|
|
95
|
-
"
|
|
96
|
-
"
|
|
84
|
+
"dist/**/*.js",
|
|
85
|
+
"dist/**/*.d.ts",
|
|
97
86
|
"CHANGELOG.md",
|
|
98
87
|
"LICENSE",
|
|
99
88
|
"README.md"
|
package/lib/cron.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
(function (root, factory) {
|
|
2
|
-
if (typeof define === 'function' && define.amd) {
|
|
3
|
-
define(['luxon'], factory);
|
|
4
|
-
} else if (typeof exports === 'object') {
|
|
5
|
-
module.exports = factory(require('luxon'), require('child_process'));
|
|
6
|
-
} else {
|
|
7
|
-
root.Cron = factory(root.luxon);
|
|
8
|
-
}
|
|
9
|
-
})(this, function (luxon, childProcess) {
|
|
10
|
-
const exports = {};
|
|
11
|
-
const spawn = childProcess && childProcess.spawn;
|
|
12
|
-
const CronTime = require('./time')(luxon);
|
|
13
|
-
const CronJob = require('./job')(CronTime, spawn);
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Extend Luxon DateTime
|
|
17
|
-
*/
|
|
18
|
-
luxon.DateTime.prototype.getWeekDay = function () {
|
|
19
|
-
return this.weekday === 7 ? 0 : this.weekday;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
exports.job = (
|
|
23
|
-
cronTime,
|
|
24
|
-
onTick,
|
|
25
|
-
onComplete,
|
|
26
|
-
startNow,
|
|
27
|
-
timeZone,
|
|
28
|
-
context,
|
|
29
|
-
runOnInit,
|
|
30
|
-
utcOffset,
|
|
31
|
-
unrefTimeout
|
|
32
|
-
) =>
|
|
33
|
-
new CronJob(
|
|
34
|
-
cronTime,
|
|
35
|
-
onTick,
|
|
36
|
-
onComplete,
|
|
37
|
-
startNow,
|
|
38
|
-
timeZone,
|
|
39
|
-
context,
|
|
40
|
-
runOnInit,
|
|
41
|
-
utcOffset,
|
|
42
|
-
unrefTimeout
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
exports.time = (cronTime, timeZone) => new CronTime(cronTime, timeZone);
|
|
46
|
-
|
|
47
|
-
exports.sendAt = cronTime => exports.time(cronTime).sendAt();
|
|
48
|
-
|
|
49
|
-
exports.timeout = cronTime => exports.time(cronTime).getTimeout();
|
|
50
|
-
|
|
51
|
-
exports.CronJob = CronJob;
|
|
52
|
-
exports.CronTime = CronTime;
|
|
53
|
-
|
|
54
|
-
return exports;
|
|
55
|
-
});
|