croner 8.1.2 → 9.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.
@@ -0,0 +1,487 @@
1
+ declare module "helpers/minitz" {
2
+ interface TimePoint {
3
+ y: number;
4
+ m: number;
5
+ d: number;
6
+ h: number;
7
+ i: number;
8
+ s: number;
9
+ tz: string;
10
+ }
11
+ /**
12
+ * Converts a date/time from a specific timezone to a normal date object using the system local time
13
+ *
14
+ * Shortcut for minitz.fromTZ(minitz.tp(...));
15
+ *
16
+ * @constructor
17
+ *
18
+ * @param {Number} y - 1970--
19
+ * @param {Number} m - 1-12
20
+ * @param {Number} d - 1-31
21
+ * @param {Number} h - 0-24
22
+ * @param {Number} i - 0-60 Minute
23
+ * @param {Number} s - 0-60
24
+ * @param {string} tz - Time zone in IANA database format 'Europe/Stockholm'
25
+ * @param {boolean} [throwOnInvalid] - Default is to return the adjusted time if the call happens during a Daylight-Saving-Time switch.
26
+ * E.g. Value "01:01:01" is returned if input time is 00:01:01 while one hour got actually
27
+ * skipped, going from 23:59:59 to 01:00:00. Setting this flag makes the library throw an exception instead.
28
+ * @returns {date} - Normal date object with correct UTC and system local time
29
+ */
30
+ function minitz(y: number, m: number, d: number, h: number, i: number, s: number, tz: string, throwOnInvalid?: boolean): Date;
31
+ namespace minitz {
32
+ var fromTZISO: (localTimeStr: string, tz: string, throwOnInvalid?: boolean) => Date;
33
+ var fromTZ: (tp: TimePoint, throwOnInvalid?: boolean) => Date;
34
+ var toTZ: (d: Date, tzStr: string) => {
35
+ y: number;
36
+ m: number;
37
+ d: number;
38
+ h: number;
39
+ i: number;
40
+ s: number;
41
+ tz: string;
42
+ };
43
+ var tp: (y: number, m: number, d: number, h: number, i: number, s: number, tz: string) => {
44
+ y: number;
45
+ m: number;
46
+ d: number;
47
+ h: number;
48
+ i: number;
49
+ s: number;
50
+ tz: string;
51
+ };
52
+ var minitz: typeof import("helpers/minitz").default;
53
+ }
54
+ export default minitz;
55
+ export { minitz };
56
+ }
57
+ declare module "options" {
58
+ import { CronDate } from "date";
59
+ import type { Cron } from "croner";
60
+ type CatchCallbackFn = (e: unknown, job: Cron) => void;
61
+ type ProtectCallbackFn = (job: Cron) => void;
62
+ interface CronOptions {
63
+ name?: string;
64
+ paused?: boolean;
65
+ kill?: boolean;
66
+ catch?: boolean | CatchCallbackFn;
67
+ unref?: boolean;
68
+ maxRuns?: number;
69
+ interval?: number;
70
+ protect?: boolean | ProtectCallbackFn;
71
+ startAt?: string | Date | CronDate;
72
+ stopAt?: string | Date | CronDate;
73
+ timezone?: string;
74
+ utcOffset?: number;
75
+ legacyMode?: boolean;
76
+ context?: unknown;
77
+ }
78
+ function CronOptions(options?: CronOptions): CronOptions;
79
+ export { CronOptions };
80
+ }
81
+ declare module "pattern" {
82
+ /**
83
+ * Constants to represent different occurrences of a weekday in its month.
84
+ * - `LAST_OCCURRENCE`: The last occurrence of a weekday.
85
+ * - `ANY_OCCURRENCE`: Combines all individual weekday occurrence bitmasks, including the last.
86
+ * - `OCCURRENCE_BITMASKS`: An array of bitmasks, with each index representing the respective occurrence of a weekday (0-indexed).
87
+ */
88
+ export const LAST_OCCURRENCE = 32;
89
+ export const ANY_OCCURRENCE: number;
90
+ export const OCCURRENCE_BITMASKS: number[];
91
+ /**
92
+ * Create a CronPattern instance from pattern string ('* * * * * *')
93
+ * @constructor
94
+ * @param {string} pattern - Input pattern
95
+ * @param {string} timezone - Input timezone, used for '?'-substitution
96
+ */
97
+ class CronPattern {
98
+ pattern: string;
99
+ timezone?: string;
100
+ second: number[];
101
+ minute: number[];
102
+ hour: number[];
103
+ day: number[];
104
+ month: number[];
105
+ dayOfWeek: number[];
106
+ lastDayOfMonth: boolean;
107
+ starDOM: boolean;
108
+ starDOW: boolean;
109
+ constructor(pattern: string, timezone?: string);
110
+ /**
111
+ * Parse current pattern, will throw on any type of failure
112
+ * @private
113
+ */
114
+ private parse;
115
+ /**
116
+ * Convert current part (seconds/minutes etc) to an array of 1 or 0 depending on if the part is about to trigger a run or not.
117
+ */
118
+ private partToArray;
119
+ /**
120
+ * After converting JAN-DEC, SUN-SAT only 0-9 * , / - are allowed, throw if anything else pops up
121
+ * @throws On error
122
+ */
123
+ private throwAtIllegalCharacters;
124
+ /**
125
+ * Nothing but a number left, handle that
126
+ *
127
+ * @param conf Current part, expected to be a number, as a string
128
+ * @param type One of "seconds", "minutes" etc
129
+ * @param valueIndexOffset -1 for day of month, and month, as they start at 1. 0 for seconds, hours, minutes
130
+ */
131
+ private handleNumber;
132
+ /**
133
+ * Set a specific value for a specific part of the CronPattern.
134
+ *
135
+ * @param part The specific part of the CronPattern, e.g., "second", "minute", etc.
136
+ * @param index The index to modify.
137
+ * @param value The value to set, typically 0 or 1, in case of "nth weekday" it will be the weekday number used for further processing
138
+ */
139
+ private setPart;
140
+ /**
141
+ * Take care of ranges with stepping (e.g. 3-23/5)
142
+ *
143
+ * @param conf Current part, expected to be a string like 3-23/5
144
+ * @param type One of "seconds", "minutes" etc
145
+ * @param valueIndexOffset -1 for day of month, and month, as they start at 1. 0 for seconds, hours, minutes
146
+ */
147
+ private handleRangeWithStepping;
148
+ private extractNth;
149
+ /**
150
+ * Take care of ranges (e.g. 1-20)
151
+ *
152
+ * @param conf - Current part, expected to be a string like 1-20, can contain L for last
153
+ * @param type - One of "seconds", "minutes" etc
154
+ * @param valueIndexOffset - -1 for day of month, and month, as they start at 1. 0 for seconds, hours, minutes
155
+ */
156
+ private handleRange;
157
+ /**
158
+ * Handle stepping (e.g. * / 14)
159
+ *
160
+ * @param conf Current part, expected to be a string like * /20 (without the space)
161
+ * @param type One of "seconds", "minutes" etc
162
+ */
163
+ private handleStepping;
164
+ /**
165
+ * Replace day name with day numbers
166
+ *
167
+ * @param conf Current part, expected to be a string that might contain sun,mon etc.
168
+ *
169
+ * @returns Conf with 0 instead of sun etc.
170
+ */
171
+ private replaceAlphaDays;
172
+ /**
173
+ * Replace month name with month numbers
174
+ *
175
+ * @param conf Current part, expected to be a string that might contain jan,feb etc.
176
+ *
177
+ * @returns conf with 0 instead of sun etc.
178
+ */
179
+ private replaceAlphaMonths;
180
+ /**
181
+ * Replace nicknames with actual cron patterns
182
+ *
183
+ * @param pattern Pattern, may contain nicknames, or not
184
+ *
185
+ * @returns Pattern, with cron expression insted of nicknames
186
+ */
187
+ private handleNicknames;
188
+ /**
189
+ * Handle the nth weekday of the month logic using hash sign (e.g. FRI#2 for the second Friday of the month)
190
+ *
191
+ * @param index Weekday, example: 5 for friday
192
+ * @param nthWeekday bitmask, 2 (0x00010) for 2nd friday, 31 (ANY_OCCURRENCE, 0b100000) for any day
193
+ */
194
+ private setNthWeekdayOfMonth;
195
+ }
196
+ export { CronPattern };
197
+ }
198
+ declare module "date" {
199
+ import type { CronOptions as CronOptions } from "options";
200
+ import { type CronPattern } from "pattern";
201
+ /**
202
+ * Converts date to CronDate
203
+ *
204
+ * @param d Input date, if using string representation ISO 8001 (2015-11-24T19:40:00) local timezone is expected
205
+ * @param tz String representation of target timezone in Europe/Stockholm format, or a number representing offset in minutes.
206
+ */
207
+ class CronDate {
208
+ tz: string | number | undefined;
209
+ /**
210
+ * Current milliseconds
211
+ * @type {number}
212
+ */
213
+ ms: number;
214
+ /**
215
+ * Current second (0-59), in local time or target timezone specified by `this.tz`
216
+ * @type {number}
217
+ */
218
+ second: number;
219
+ /**
220
+ * Current minute (0-59), in local time or target timezone specified by `this.tz`
221
+ * @type {number}
222
+ */
223
+ minute: number;
224
+ /**
225
+ * Current hour (0-23), in local time or target timezone specified by `this.tz`
226
+ * @type {number}
227
+ */
228
+ hour: number;
229
+ /**
230
+ * Current day (1-31), in local time or target timezone specified by `this.tz`
231
+ * @type {number}
232
+ */
233
+ day: number;
234
+ /**
235
+ * Current month (1-12), in local time or target timezone specified by `this.tz`
236
+ * @type {number}
237
+ */
238
+ month: number;
239
+ /**
240
+ * Current full year, in local time or target timezone specified by `this.tz`
241
+ */
242
+ year: number;
243
+ constructor(d?: CronDate | Date | string, tz?: string | number);
244
+ /**
245
+ * Check if the given date is the nth occurrence of a weekday in its month.
246
+ *
247
+ * @param year The year.
248
+ * @param month The month (0 for January, 11 for December).
249
+ * @param day The day of the month.
250
+ * @param nth The nth occurrence (bitmask).
251
+ *
252
+ * @return True if the date is the nth occurrence of its weekday, false otherwise.
253
+ */
254
+ private isNthWeekdayOfMonth;
255
+ /**
256
+ * Sets internals using a Date
257
+ */
258
+ private fromDate;
259
+ /**
260
+ * Sets internals by deep copying another CronDate
261
+ *
262
+ * @param {CronDate} d - Input date
263
+ */
264
+ private fromCronDate;
265
+ /**
266
+ * Reset internal parameters (seconds, minutes, hours) if any of them have exceeded (or could have exceeded) their normal ranges
267
+ *
268
+ * Will alway return true on february 29th, as that is a date that _could_ be out of bounds
269
+ */
270
+ private apply;
271
+ /**
272
+ * Sets internals by parsing a string
273
+ */
274
+ private fromString;
275
+ /**
276
+ * Find next match of current part
277
+ */
278
+ private findNext;
279
+ /**
280
+ * Increment to next run time recursively
281
+ *
282
+ * This function is currently capped at year 3000. Do you have a reason to go further? Open an issue on GitHub!
283
+ *
284
+ * @param pattern The pattern used to increment current state
285
+ * @param options Cron options used for incrementing
286
+ * @param doing Which part to increment, 0 represent first item of RecursionSteps-array etc.
287
+ * @return Returns itthis for chaining, or null if increment wasnt possible
288
+ */
289
+ private recurse;
290
+ /**
291
+ * Increment to next run time
292
+ *
293
+ * @param pattern The pattern used to increment current state
294
+ * @param options Cron options used for incrementing
295
+ * @param hasPreviousRun If this run should adhere to minimum interval
296
+ * @return Returns itthis for chaining, or null if increment wasnt possible
297
+ */
298
+ increment(pattern: CronPattern, options: CronOptions, hasPreviousRun: boolean): CronDate | null;
299
+ /**
300
+ * Convert current state back to a javascript Date()
301
+ *
302
+ * @param internal If this is an internal call
303
+ */
304
+ getDate(internal?: boolean): Date;
305
+ /**
306
+ * Convert current state back to a javascript Date() and return UTC milliseconds
307
+ */
308
+ getTime(): number;
309
+ }
310
+ export { CronDate };
311
+ }
312
+ declare module "utils" {
313
+ /**
314
+ * Helper function to check if a variable is a function
315
+ * @private
316
+ *
317
+ * @param {?} [v] - Variable to check
318
+ * @returns {boolean}
319
+ */
320
+ function isFunction(v: unknown): boolean;
321
+ /**
322
+ * Helper function to unref a timer in both Deno and Node
323
+ * @private
324
+ * @param {unknown} [timer] - Timer to unref
325
+ */
326
+ function unrefTimer(timer: NodeJS.Timeout | number): void;
327
+ export { isFunction, unrefTimer };
328
+ }
329
+ declare module "croner" {
330
+ import { CronDate } from "date";
331
+ import { CronPattern } from "pattern";
332
+ import { CronOptions } from "options";
333
+ /**
334
+ * An array containing all named cron jobs.
335
+ */
336
+ const scheduledJobs: Cron[];
337
+ /**
338
+ * Encapsulate all internal states in the Cron instance.
339
+ * Duplicate all options that can change to internal states, for example maxRuns and paused.
340
+ */
341
+ type CronState = {
342
+ kill: boolean;
343
+ blocking: boolean;
344
+ /**
345
+ * Start time of previous trigger, updated after each trigger
346
+ *
347
+ * Stored to use as the actual previous run, even while a new trigger
348
+ * is started. Used by the public funtion `.previousRun()`
349
+ */
350
+ previousRun: CronDate | undefined;
351
+ /**
352
+ * Start time of current trigger, this is updated just before triggering
353
+ *
354
+ * This is used internally as "previous run", as we mostly want to know
355
+ * when the previous run _started_
356
+ */
357
+ currentRun: CronDate | undefined;
358
+ once: CronDate | undefined;
359
+ currentTimeout: NodeJS.Timer | number | undefined;
360
+ maxRuns: number | undefined;
361
+ paused: boolean | undefined;
362
+ pattern: CronPattern;
363
+ };
364
+ /**
365
+ * Cron entrypoint
366
+ *
367
+ * @constructor
368
+ * @param pattern - Input pattern, input date, or input ISO 8601 time string
369
+ * @param [fnOrOptions1] - Options or function to be run each iteration of pattern
370
+ * @param [fnOrOptions2] - Options or function to be run each iteration of pattern
371
+ */
372
+ class Cron {
373
+ name: string | undefined;
374
+ options: CronOptions;
375
+ _states: CronState;
376
+ fn?: Function;
377
+ constructor(pattern: string | Date, fnOrOptions1: CronOptions | Function, fnOrOptions2: CronOptions | Function);
378
+ /**
379
+ * Find next runtime, based on supplied date. Strips milliseconds.
380
+ *
381
+ * @param prev - Date to start from
382
+ * @returns Next run time
383
+ */
384
+ nextRun(prev?: CronDate | Date | string): Date | null;
385
+ /**
386
+ * Find next n runs, based on supplied date. Strips milliseconds.
387
+ *
388
+ * @param n - Number of runs to enumerate
389
+ * @param previous - Date to start from
390
+ * @returns - Next n run times
391
+ */
392
+ nextRuns(n: number, previous: Date | string): Date[];
393
+ /**
394
+ * Return the original pattern, if there was one
395
+ *
396
+ * @returns Original pattern
397
+ */
398
+ getPattern(): string | undefined;
399
+ /**
400
+ * Indicates whether or not the cron job is scheduled and running, e.g. awaiting next trigger
401
+ *
402
+ * @returns Running or not
403
+ */
404
+ isRunning(): boolean;
405
+ /**
406
+ * Indicates whether or not the cron job is permanently stopped
407
+ *
408
+ * @returns Running or not
409
+ */
410
+ isStopped(): boolean;
411
+ /**
412
+ * Indicates whether or not the cron job is currently working
413
+ *
414
+ * @returns Running or not
415
+ */
416
+ isBusy(): boolean;
417
+ /**
418
+ * Return current/previous run start time
419
+ *
420
+ * @returns Current (if running) or previous run time
421
+ */
422
+ currentRun(): Date | null;
423
+ /**
424
+ * Return previous run start time
425
+ *
426
+ * @returns Previous run time
427
+ */
428
+ previousRun(): Date | null;
429
+ /**
430
+ * Returns number of milliseconds to next run
431
+ *
432
+ * @param prev Starting date, defaults to now - minimum interval
433
+ */
434
+ msToNext(prev?: CronDate | Date | string): number | null;
435
+ /**
436
+ * Stop execution
437
+ *
438
+ * Running this will forcefully stop the job, and prevent furter exection. `.resume()` will not work after stopping.
439
+ * It will also be removed from the scheduledJobs array if it were named.
440
+ */
441
+ stop(): void;
442
+ /**
443
+ * Pause execution
444
+ *
445
+ * @returns Wether pause was successful
446
+ */
447
+ pause(): boolean;
448
+ /**
449
+ * Resume execution
450
+ *
451
+ * @returns Wether resume was successful
452
+ */
453
+ resume(): boolean;
454
+ /**
455
+ * Schedule a new job
456
+ *
457
+ * @param func - Function to be run each iteration of pattern
458
+ */
459
+ schedule(func?: Function): Cron;
460
+ /**
461
+ * Internal function to trigger a run, used by both scheduled and manual trigger
462
+ */
463
+ private _trigger;
464
+ /**
465
+ * Trigger a run manually
466
+ */
467
+ trigger(): Promise<void>;
468
+ /**
469
+ * Called when it's time to trigger.
470
+ * Checks if all conditions are currently met,
471
+ * then instantly triggers the scheduled function.
472
+ */
473
+ private _checkTrigger;
474
+ /**
475
+ * Internal version of next. Cron needs millseconds internally, hence _next.
476
+ */
477
+ private _next;
478
+ /**
479
+ * Calculate the previous run if no previous run is supplied, but startAt and interval are set.
480
+ * This calculation is only necessary if the startAt time is before the current time.
481
+ * Should only be called from the _next function.
482
+ */
483
+ private _calculatePreviousRun;
484
+ }
485
+ export default Cron;
486
+ export { Cron, scheduledJobs };
487
+ }