croner 10.0.0 → 10.0.2-dev.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/croner.d.ts CHANGED
@@ -1,10 +1,522 @@
1
- import { CronDate } from "./date.ts";
2
- import { CronPattern } from "./pattern.ts";
3
- import { type CronOptions } from "./options.ts";
1
+ // Generated by dts-bundle-generator v9.5.1
2
+
3
+ /**
4
+ * Cron pattern mode for controlling precision level
5
+ */
6
+ export type CronMode = "auto" | "5-part" | "6-part" | "7-part" | "5-or-6-parts" | "6-or-7-parts";
7
+ /**
8
+ * Create a CronPattern instance from pattern string ('* * * * * *')
9
+ * @constructor
10
+ * @param {string} pattern - Input pattern
11
+ * @param {string} timezone - Input timezone, used for '?'-substitution
12
+ * @param {object} options - Cron options including mode
13
+ */
14
+ export declare class CronPattern {
15
+ pattern: string;
16
+ timezone?: string;
17
+ mode: CronMode;
18
+ alternativeWeekdays: boolean;
19
+ sloppyRanges: boolean;
20
+ second: number[];
21
+ minute: number[];
22
+ hour: number[];
23
+ day: number[];
24
+ month: number[];
25
+ dayOfWeek: number[];
26
+ year: number[];
27
+ lastDayOfMonth: boolean;
28
+ lastWeekday: boolean;
29
+ nearestWeekdays: number[];
30
+ starDOM: boolean;
31
+ starDOW: boolean;
32
+ starYear: boolean;
33
+ useAndLogic: boolean;
34
+ constructor(pattern: string, timezone?: string, options?: {
35
+ mode?: CronMode;
36
+ alternativeWeekdays?: boolean;
37
+ sloppyRanges?: boolean;
38
+ });
39
+ /**
40
+ * Parse current pattern, will throw on any type of failure
41
+ * @private
42
+ */
43
+ private parse;
44
+ /**
45
+ * 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.
46
+ */
47
+ private partToArray;
48
+ /**
49
+ * After converting JAN-DEC, SUN-SAT only 0-9 * , / - are allowed, throw if anything else pops up
50
+ * @throws On error
51
+ */
52
+ private throwAtIllegalCharacters;
53
+ /**
54
+ * Nothing but a number, potentially with a modifier, left - handle that
55
+ *
56
+ * @param conf Current part, expected to be a number, as a string
57
+ * @param type One of "seconds", "minutes" etc
58
+ * @param valueIndexOffset -1 for day of month, and month, as they start at 1. 0 for seconds, hours, minutes
59
+ */
60
+ private handleNumber;
61
+ /**
62
+ * Set a specific value for a specific part of the CronPattern.
63
+ *
64
+ * @param part The specific part of the CronPattern, e.g., "second", "minute", etc.
65
+ * @param index The index to modify.
66
+ * @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
67
+ */
68
+ private setPart;
69
+ /**
70
+ * Validates that a parsed number is not NaN.
71
+ * Throws a TypeError with a descriptive message if the value is NaN.
72
+ *
73
+ * @param value - The value to validate
74
+ * @param errorMessage - The error message to throw if validation fails
75
+ * @throws {TypeError} If the value is NaN
76
+ * @private
77
+ */
78
+ private validateNotNaN;
79
+ /**
80
+ * Validates that a range is valid (lower <= upper) and that steps are valid (> 0 and <= array length).
81
+ *
82
+ * @param lower - Lower bound of range
83
+ * @param upper - Upper bound of range
84
+ * @param steps - Optional step value to validate
85
+ * @param type - The type of pattern part being validated
86
+ * @param conf - The original configuration string for error messages
87
+ * @throws {TypeError} If validation fails
88
+ * @private
89
+ */
90
+ private validateRange;
91
+ /**
92
+ * Take care of ranges with stepping (e.g. 3-23/5)
93
+ *
94
+ * @param conf Current part, expected to be a string like 3-23/5
95
+ * @param type One of "seconds", "minutes" etc
96
+ * @param valueIndexOffset -1 for day of month, and month, as they start at 1. 0 for seconds, hours, minutes
97
+ */
98
+ private handleRangeWithStepping;
99
+ private extractNth;
100
+ /**
101
+ * Take care of ranges (e.g. 1-20)
102
+ *
103
+ * @param conf - Current part, expected to be a string like 1-20, can contain L for last
104
+ * @param type - One of "seconds", "minutes" etc
105
+ * @param valueIndexOffset - -1 for day of month, and month, as they start at 1. 0 for seconds, hours, minutes
106
+ */
107
+ private handleRange;
108
+ /**
109
+ * Handle stepping (e.g. * / 14)
110
+ *
111
+ * @param conf Current part, expected to be a string like * /20 (without the space)
112
+ * @param type One of "seconds", "minutes" etc
113
+ */
114
+ private handleStepping;
115
+ /**
116
+ * Replace day name with day numbers
117
+ *
118
+ * @param conf Current part, expected to be a string that might contain sun,mon etc.
119
+ *
120
+ * @returns Conf with 0 instead of sun etc.
121
+ */
122
+ private replaceAlphaDays;
123
+ /**
124
+ * Replace day name with day numbers (Quartz mode)
125
+ * In Quartz mode: Sunday=1, Monday=2, ..., Saturday=7
126
+ *
127
+ * @param conf Current part, expected to be a string that might contain sun,mon etc.
128
+ *
129
+ * @returns Conf with Quartz-style numbering
130
+ */
131
+ private replaceAlphaDaysQuartz;
132
+ /**
133
+ * Replace month name with month numbers
134
+ *
135
+ * @param conf Current part, expected to be a string that might contain jan,feb etc.
136
+ *
137
+ * @returns conf with 0 instead of sun etc.
138
+ */
139
+ private replaceAlphaMonths;
140
+ /**
141
+ * Replace nicknames with actual cron patterns
142
+ *
143
+ * @param pattern Pattern, may contain nicknames, or not
144
+ *
145
+ * @returns Pattern, with cron expression insted of nicknames
146
+ */
147
+ private handleNicknames;
148
+ /**
149
+ * Handle the nth weekday of the month logic using hash sign (e.g. FRI#2 for the second Friday of the month)
150
+ *
151
+ * @param index Weekday, example: 5 for friday
152
+ * @param nthWeekday bitmask, 2 (0x00010) for 2nd friday, 31 (ANY_OCCURRENCE, 0b100000) for any day
153
+ */
154
+ private setNthWeekdayOfMonth;
155
+ }
156
+ export type CatchCallbackFn = (e: unknown, job: Cron) => void;
157
+ export type ProtectCallbackFn = (job: Cron) => void;
158
+ /**
159
+ * Options for configuring cron jobs.
160
+ *
161
+ * @interface
162
+ */
163
+ export interface CronOptions<T = undefined> {
164
+ /**
165
+ * The name of the cron job. If provided, the job will be added to the
166
+ * `scheduledJobs` array, allowing it to be accessed by name.
167
+ */
168
+ name?: string;
169
+ /**
170
+ * If true, the job will be paused initially.
171
+ * @default false
172
+ */
173
+ paused?: boolean;
174
+ /**
175
+ * If true, the job will be stopped permanently.
176
+ * @default false
177
+ */
178
+ kill?: boolean;
179
+ /**
180
+ * If true, errors thrown by the job function will be caught.
181
+ * If a function is provided, it will be called with the error and the job instance.
182
+ * @default false
183
+ */
184
+ catch?: boolean | CatchCallbackFn;
185
+ /**
186
+ * If true, the underlying timer will be unreferenced, allowing the Node.js
187
+ * process to exit even if the job is still running.
188
+ * @default false
189
+ */
190
+ unref?: boolean;
191
+ /**
192
+ * The maximum number of times the job will run.
193
+ * @default Infinity
194
+ */
195
+ maxRuns?: number;
196
+ /**
197
+ * The minimum interval between job executions, in seconds.
198
+ * @default 1
199
+ */
200
+ interval?: number;
201
+ /**
202
+ * If true, prevents the job from running if the previous execution is still in progress.
203
+ * If a function is provided, it will be called if the job is blocked.
204
+ * @default false
205
+ */
206
+ protect?: boolean | ProtectCallbackFn;
207
+ /**
208
+ * The date and time at which the job should start running.
209
+ */
210
+ startAt?: string | Date | CronDate<T>;
211
+ /**
212
+ * The date and time at which the job should stop running.
213
+ */
214
+ stopAt?: string | Date | CronDate<T>;
215
+ /**
216
+ * The timezone for the cron job.
217
+ */
218
+ timezone?: string;
219
+ /**
220
+ * The UTC offset for the cron job, in minutes.
221
+ */
222
+ utcOffset?: number;
223
+ /**
224
+ * If true, uses AND logic when combining day-of-month and day-of-week.
225
+ * If false, uses OR logic for combining day-of-month and day-of-week (legacy behavior).
226
+ * @default false
227
+ */
228
+ domAndDow?: boolean;
229
+ /**
230
+ * @deprecated Use domAndDow instead. This option will be removed in a future version.
231
+ * If true, enables legacy mode (OR logic) for compatibility with older cron implementations.
232
+ * Offset the scheduled date by a number of days.
233
+ * Positive values shift the date forward, negative values shift it backward.
234
+ * For example, dayOffset: -1 schedules the job one day before the pattern match.
235
+ * @default 0
236
+ */
237
+ dayOffset?: number;
238
+ /**
239
+ * If true, enables legacy mode for compatibility with older cron implementations.
240
+ * @default true
241
+ */
242
+ legacyMode?: boolean;
243
+ /**
244
+ * Specifies the cron pattern mode to use for parsing and execution.
245
+ *
246
+ * - "auto": Automatically detect pattern format (default behavior)
247
+ * - "5-part": Traditional 5-field cron (minute-level precision, seconds forced to 0, years wildcarded)
248
+ * - "6-part": Extended 6-field cron (second-level precision, years wildcarded)
249
+ * - "7-part": Full 7-field cron (second-level and year-specific precision)
250
+ * - "5-or-6-parts": Accept 5 or 6 field patterns (years wildcarded)
251
+ * - "6-or-7-parts": Accept 6 or 7 field patterns (no additional constraints)
252
+ *
253
+ * @default "auto"
254
+ */
255
+ mode?: CronMode;
256
+ /**
257
+ * An optional context object that will be passed to the job function.
258
+ */
259
+ context?: T;
260
+ /**
261
+ * If true, enables alternative weekday numbering (Quartz mode).
262
+ * In standard mode (false): Sunday=0, Monday=1, ..., Saturday=6
263
+ * In Quartz mode (true): Sunday=1, Monday=2, ..., Saturday=7
264
+ * @default false
265
+ */
266
+ alternativeWeekdays?: boolean;
267
+ /**
268
+ * If true, allows non-standard stepping formats for backward compatibility.
269
+ * When false (default), only wildcard (*\/step) or range (min-max\/step) formats are allowed.
270
+ * When true, allows numeric prefix formats like /10, 5/5, 30/30.
271
+ * @default false
272
+ */
273
+ sloppyRanges?: boolean;
274
+ }
275
+ /**
276
+ * Converts date to CronDate
277
+ *
278
+ * @param d Input date, if using string representation ISO 8001 (2015-11-24T19:40:00) local timezone is expected
279
+ * @param tz String representation of target timezone in Europe/Stockholm format, or a number representing offset in minutes.
280
+ */
281
+ export declare class CronDate<T = undefined> {
282
+ tz: string | number | undefined;
283
+ /**
284
+ * Optional UTC ms threshold for resolving DST overlap ambiguity.
285
+ * When set, getTime()/getDate() will return the earliest occurrence
286
+ * that is at or after this threshold.
287
+ * @private
288
+ */
289
+ private afterMs?;
290
+ /**
291
+ * Current milliseconds
292
+ * @type {number}
293
+ */
294
+ ms: number;
295
+ /**
296
+ * Current second (0-59), in local time or target timezone specified by `this.tz`
297
+ * @type {number}
298
+ */
299
+ second: number;
300
+ /**
301
+ * Current minute (0-59), in local time or target timezone specified by `this.tz`
302
+ * @type {number}
303
+ */
304
+ minute: number;
305
+ /**
306
+ * Current hour (0-23), in local time or target timezone specified by `this.tz`
307
+ * @type {number}
308
+ */
309
+ hour: number;
310
+ /**
311
+ * Current day (1-31), in local time or target timezone specified by `this.tz`
312
+ * @type {number}
313
+ */
314
+ day: number;
315
+ /**
316
+ * Current month (1-12), in local time or target timezone specified by `this.tz`
317
+ * @type {number}
318
+ */
319
+ month: number;
320
+ /**
321
+ * Current full year, in local time or target timezone specified by `this.tz`
322
+ */
323
+ year: number;
324
+ constructor(d?: CronDate<T> | Date | string | null, tz?: string | number);
325
+ /**
326
+ * Calculates the last day of a given month.
327
+ * Uses a performance optimization for months other than February.
328
+ *
329
+ * @param year The year
330
+ * @param month The month (0-11)
331
+ * @returns The last day of the month (1-31)
332
+ * @private
333
+ */
334
+ private getLastDayOfMonth;
335
+ /**
336
+ * Calculates the last weekday (Mon-Fri) of a given month.
337
+ *
338
+ * @param year The target year.
339
+ * @param month The target month (0-11).
340
+ * @returns The day of the month (1-31) that is the last weekday.
341
+ * @private
342
+ */
343
+ private getLastWeekday;
344
+ /**
345
+ * Calculates the nearest weekday (Mon-Fri) to a given day of the month.
346
+ * Handles month boundaries.
347
+ *
348
+ * @param year The target year.
349
+ * @param month The target month (0-11).
350
+ * @param day The target day (1-31).
351
+ * @returns The day of the month (1-31) that is the nearest weekday, or -1 if the day doesn't exist in the month.
352
+ */
353
+ private getNearestWeekday;
354
+ /**
355
+ * Check if the given date is the nth occurrence of a weekday in its month.
356
+ *
357
+ * @param year The year.
358
+ * @param month The month (0 for January, 11 for December).
359
+ * @param day The day of the month.
360
+ * @param nth The nth occurrence (bitmask).
361
+ *
362
+ * @return True if the date is the nth occurrence of its weekday, false otherwise.
363
+ */
364
+ private isNthWeekdayOfMonth;
365
+ /**
366
+ * Sets internals using a Date
367
+ */
368
+ private fromDate;
369
+ /**
370
+ * Sets internals by deep copying another CronDate
371
+ *
372
+ * @param {CronDate} d - Input date
373
+ */
374
+ private fromCronDate;
375
+ /**
376
+ * Reset internal parameters (seconds, minutes, hours) if any of them have exceeded (or could have exceeded) their normal ranges
377
+ *
378
+ * Will alway return true on february 29th, as that is a date that _could_ be out of bounds
379
+ */
380
+ private apply;
381
+ /**
382
+ * Sets internals by parsing a string
383
+ */
384
+ private fromString;
385
+ /**
386
+ * Find next match of current part
387
+ */
388
+ private findNext;
389
+ /**
390
+ * Internal unified method to find a matching time component in either direction.
391
+ * This method searches through the pattern to find the next or previous valid value
392
+ * for the specified target component (second, minute, hour, day, or month).
393
+ *
394
+ * @param options Cron options
395
+ * @param target Target property (second, minute, hour, day, month)
396
+ * @param pattern Pattern to use
397
+ * @param offset Offset to use
398
+ * @param direction 1 for forward (next), -1 for backward (previous)
399
+ * @returns Status code: 1 = same value matches, 2 = value changed, 3 = no match found
400
+ *
401
+ * @private
402
+ */
403
+ private _findMatch;
404
+ /**
405
+ * Increment to next run time recursively.
406
+ *
407
+ * This function traverses the date components (year, month, day, hour, minute, second)
408
+ * to find the next date and time that matches the cron pattern. It uses a recursive
409
+ * approach to handle the dependencies between different components. For example,
410
+ * if the day changes, the hour, minute, and second need to be reset.
411
+ *
412
+ * The recursion is limited to the year 10000 to prevent potential
413
+ * infinite loops or excessive stack depth, and to match the maximum supported
414
+ * year in OCPS 1.2 (years 1-9999).
415
+ *
416
+ * @param pattern The cron pattern used to determine the next run time.
417
+ * @param options The cron options that influence the incrementing behavior.
418
+ * @param doing The index of the `RecursionSteps` array indicating the current
419
+ * date component being processed. 0 represents "month", 1 represents "day", etc.
420
+ *
421
+ * @returns This `CronDate` instance for chaining, or null if incrementing
422
+ * was not possible (e.g., reached year 10000 limit or no matching date).
423
+ *
424
+ * @private
425
+ */
426
+ private recurse;
427
+ /**
428
+ * Increment to next run time
429
+ *
430
+ * @param pattern The pattern used to increment the current date.
431
+ * @param options Cron options used for incrementing.
432
+ * @param hasPreviousRun True if there was a previous run, false otherwise. This is used to determine whether to apply the minimum interval.
433
+ * @returns This CronDate instance for chaining, or null if incrementing was not possible (e.g., reached year 3000 limit).
434
+ */
435
+ increment(pattern: CronPattern, options: CronOptions<T>, hasPreviousRun: boolean): CronDate<T> | null;
436
+ /**
437
+ * Decrement to previous run time
438
+ *
439
+ * @param pattern The pattern used to decrement the current date.
440
+ * @param options Cron options used for decrementing.
441
+ * @returns This CronDate instance for chaining, or null if decrementing was not possible (e.g., reached year 0).
442
+ */
443
+ decrement(pattern: CronPattern, options: CronOptions<T>): CronDate<T> | null;
444
+ /**
445
+ * Find previous match by recursively checking pattern parts in reverse.
446
+ *
447
+ * This is the backward equivalent of the recurse() method. It searches backwards
448
+ * through time to find the previous date/time that matches the cron pattern.
449
+ *
450
+ * @param pattern The cron pattern used to determine the previous run time.
451
+ * @param options The cron options that influence the decrementing behavior.
452
+ * @param doing The index of the `RecursionSteps` array indicating the current
453
+ * date component being processed.
454
+ *
455
+ * @returns This `CronDate` instance for chaining, or null if decrementing
456
+ * was not possible (e.g., reached year 0 or no matching date).
457
+ *
458
+ * @private
459
+ */
460
+ private recurseBackward;
461
+ /**
462
+ * Get the maximum value in a pattern for a given target.
463
+ * Used when resetting components during backward recursion.
464
+ *
465
+ * @param target The target component (second, minute, hour, day, month)
466
+ * @param pattern The cron pattern
467
+ * @param offset The offset to apply
468
+ * @returns The maximum valid value for the target component
469
+ *
470
+ * @private
471
+ */
472
+ private getMaxPatternValue;
473
+ /**
474
+ * Find previous match for a specific component going backwards in time.
475
+ * This is the backward equivalent of the findNext() method.
476
+ *
477
+ * @param options Cron options
478
+ * @param target Target property (second, minute, hour, day, month)
479
+ * @param pattern Pattern to use
480
+ * @param offset Offset to use
481
+ * @returns Status code: 1 = same value matches, 2 = value changed to earlier value, 3 = no match found
482
+ *
483
+ * @private
484
+ */
485
+ private findPrevious;
486
+ /**
487
+ * Convert current state back to a javascript Date()
488
+ *
489
+ * @param internal If this is an internal call
490
+ * @param afterMs Optional UTC ms threshold for resolving DST overlap ambiguity
491
+ */
492
+ getDate(internal?: boolean, afterMs?: number): Date;
493
+ /**
494
+ * Convert current state back to a javascript Date() and return UTC milliseconds
495
+ *
496
+ * @param afterMs Optional UTC ms threshold for resolving DST overlap ambiguity
497
+ */
498
+ getTime(afterMs?: number): number;
499
+ /**
500
+ * Set the afterMs threshold for DST overlap resolution.
501
+ * When set, getTime()/getDate() will return the earliest occurrence
502
+ * that is at or after this threshold.
503
+ *
504
+ * @param ms UTC milliseconds threshold
505
+ */
506
+ setAfterMs(ms: number): void;
507
+ /**
508
+ * Check if the current CronDate matches a cron pattern
509
+ *
510
+ * @param pattern The cron pattern to match against
511
+ * @param options The cron options that influence matching
512
+ * @returns true if the date matches the pattern, false otherwise
513
+ */
514
+ match(pattern: CronPattern, options: CronOptions<T>): boolean;
515
+ }
4
516
  /**
5
517
  * An array containing all named cron jobs.
6
518
  */
7
- declare const scheduledJobs: Cron<any>[];
519
+ export declare const scheduledJobs: Cron<any>[];
8
520
  /**
9
521
  * Callback function type
10
522
  *
@@ -22,177 +534,178 @@ export type CronCallback<T = undefined> = (self: InstanceType<typeof Cron<T>>, c
22
534
  * @param [fnOrOptions1] - Options or function to be run each iteration of pattern
23
535
  * @param [fnOrOptions2] - Options or function to be run each iteration of pattern
24
536
  */
25
- declare class Cron<T = undefined> {
26
- name: string | undefined;
27
- options: CronOptions<T>;
28
- private _states;
29
- private fn?;
30
- /**
31
- * Internal helper to get the timezone or UTC offset for date operations.
32
- * Reduces duplication of `this.options.timezone || this.options.utcOffset` throughout the codebase.
33
- *
34
- * @returns The timezone string or UTC offset number
35
- * @private
36
- */
37
- private getTz;
38
- /**
39
- * Internal helper to apply dayOffset to a date if configured.
40
- * Reduces duplication of dayOffset calculation logic.
41
- *
42
- * @param date - The base date to apply offset to
43
- * @returns The date with dayOffset applied, or the original date if no offset is configured
44
- * @private
45
- */
46
- private applyDayOffset;
47
- constructor(pattern: string | Date, fnOrOptions1?: CronOptions<T> | CronCallback<T>, fnOrOptions2?: CronOptions<T> | CronCallback<T>);
48
- /**
49
- * Find next runtime, based on supplied date. Strips milliseconds.
50
- *
51
- * @param prev - Optional. Date to start from. Can be a CronDate, Date object, or a string representing a date.
52
- * @returns The next run time as a Date object, or null if there is no next run.
53
- */
54
- nextRun(prev?: CronDate<T> | Date | string | null): Date | null;
55
- /**
56
- * Find next n runs, based on supplied date. Strips milliseconds.
57
- *
58
- * @param n - Number of runs to enumerate
59
- * @param previous - Date to start from
60
- * @returns - Next n run times
61
- */
62
- nextRuns(n: number, previous?: Date | string): Date[];
63
- /**
64
- * Find previous n runs, based on supplied date. Strips milliseconds.
65
- *
66
- * @param n - Number of runs to enumerate
67
- * @param reference - Date to start from (defaults to now)
68
- * @returns - Previous n run times in reverse chronological order (most recent first)
69
- */
70
- previousRuns(n: number, reference?: Date | string): Date[];
71
- /**
72
- * Internal helper to enumerate runs in either direction.
73
- *
74
- * @param n - Number of runs to enumerate
75
- * @param startDate - Date to start from
76
- * @param direction - Direction to enumerate ("next" or "previous")
77
- * @returns Array of run times with dayOffset applied
78
- * @private
79
- */
80
- private _enumerateRuns;
81
- /**
82
- * Check if a given date matches the cron pattern
83
- *
84
- * @param date - Date to check. Can be a Date object or a string representing a date.
85
- * @returns true if the date matches the pattern, false otherwise
86
- */
87
- match(date: Date | string): boolean;
88
- /**
89
- * Return the original pattern, if there was one.
90
- * Returns undefined when the job was created with a Date or ISO 8601 string instead of a cron pattern.
91
- *
92
- * @returns Original cron pattern, or undefined for date-based jobs
93
- */
94
- getPattern(): string | undefined;
95
- /**
96
- * Return the original run-once date, if there was one
97
- *
98
- * @returns Original run-once date, or null if not a run-once job
99
- */
100
- getOnce(): Date | null;
101
- /**
102
- * Indicates whether or not the cron job is scheduled and running, e.g. awaiting next trigger
103
- *
104
- * @returns Running or not
105
- */
106
- isRunning(): boolean;
107
- /**
108
- * Indicates whether or not the cron job is permanently stopped
109
- *
110
- * @returns Running or not
111
- */
112
- isStopped(): boolean;
113
- /**
114
- * Indicates whether or not the cron job is currently working
115
- *
116
- * @returns Running or not
117
- */
118
- isBusy(): boolean;
119
- /**
120
- * Return current/previous run start time
121
- *
122
- * @returns Current (if running) or previous run time
123
- */
124
- currentRun(): Date | null;
125
- /**
126
- * Return previous run start time
127
- *
128
- * @returns Previous run time
129
- */
130
- previousRun(): Date | null;
131
- /**
132
- * Returns number of milliseconds to next run
133
- *
134
- * @param prev Starting date, defaults to now - minimum interval
135
- */
136
- msToNext(prev?: CronDate<T> | Date | string): number | null;
137
- /**
138
- * Stop execution
139
- *
140
- * Running this will forcefully stop the job, and prevent furter exection. `.resume()` will not work after stopping.
141
- * It will also be removed from the scheduledJobs array if it were named.
142
- */
143
- stop(): void;
144
- /**
145
- * Pause execution
146
- *
147
- * @returns Wether pause was successful
148
- */
149
- pause(): boolean;
150
- /**
151
- * Resume execution
152
- *
153
- * @returns Wether resume was successful
154
- */
155
- resume(): boolean;
156
- /**
157
- * Schedule a new job
158
- *
159
- * @param func - Function to be run each iteration of pattern
160
- */
161
- schedule(func?: CronCallback<T>): Cron<T>;
162
- /**
163
- * Internal function to trigger a run, used by both scheduled and manual trigger
164
- */
165
- private _trigger;
166
- /**
167
- * Trigger a run manually
168
- */
169
- trigger(): Promise<void>;
170
- /**
171
- * Returns number of runs left, undefined = unlimited
172
- */
173
- runsLeft(): number | undefined;
174
- /**
175
- * Called when it's time to trigger.
176
- * Checks if all conditions are currently met,
177
- * then instantly triggers the scheduled function.
178
- */
179
- private _checkTrigger;
180
- /**
181
- * Internal version of next. Cron needs millseconds internally, hence _next.
182
- */
183
- private _next;
184
- /**
185
- * Internal version of previous. Finds the previous scheduled run time.
186
- *
187
- * @param referenceDate - Optional reference date to search backwards from (defaults to now)
188
- * @returns Previous scheduled run time, or null if no previous run exists
189
- */
190
- private _previous;
191
- /**
192
- * Calculate the previous run if no previous run is supplied, but startAt and interval are set.
193
- * This calculation is only necessary if the startAt time is before the current time.
194
- * Should only be called from the _next function.
195
- */
196
- private _calculatePreviousRun;
537
+ export declare class Cron<T = undefined> {
538
+ name: string | undefined;
539
+ options: CronOptions<T>;
540
+ private _states;
541
+ private fn?;
542
+ /**
543
+ * Internal helper to get the timezone or UTC offset for date operations.
544
+ * Reduces duplication of `this.options.timezone || this.options.utcOffset` throughout the codebase.
545
+ *
546
+ * @returns The timezone string or UTC offset number
547
+ * @private
548
+ */
549
+ private getTz;
550
+ /**
551
+ * Internal helper to apply dayOffset to a date if configured.
552
+ * Reduces duplication of dayOffset calculation logic.
553
+ *
554
+ * @param date - The base date to apply offset to
555
+ * @returns The date with dayOffset applied, or the original date if no offset is configured
556
+ * @private
557
+ */
558
+ private applyDayOffset;
559
+ constructor(pattern: string | Date, fnOrOptions1?: CronOptions<T> | CronCallback<T>, fnOrOptions2?: CronOptions<T> | CronCallback<T>);
560
+ /**
561
+ * Find next runtime, based on supplied date. Strips milliseconds.
562
+ *
563
+ * @param prev - Optional. Date to start from. Can be a CronDate, Date object, or a string representing a date.
564
+ * @returns The next run time as a Date object, or null if there is no next run.
565
+ */
566
+ nextRun(prev?: CronDate<T> | Date | string | null): Date | null;
567
+ /**
568
+ * Find next n runs, based on supplied date. Strips milliseconds.
569
+ *
570
+ * @param n - Number of runs to enumerate
571
+ * @param previous - Date to start from
572
+ * @returns - Next n run times
573
+ */
574
+ nextRuns(n: number, previous?: Date | string): Date[];
575
+ /**
576
+ * Find previous n runs, based on supplied date. Strips milliseconds.
577
+ *
578
+ * @param n - Number of runs to enumerate
579
+ * @param reference - Date to start from (defaults to now)
580
+ * @returns - Previous n run times in reverse chronological order (most recent first)
581
+ */
582
+ previousRuns(n: number, reference?: Date | string): Date[];
583
+ /**
584
+ * Internal helper to enumerate runs in either direction.
585
+ *
586
+ * @param n - Number of runs to enumerate
587
+ * @param startDate - Date to start from
588
+ * @param direction - Direction to enumerate ("next" or "previous")
589
+ * @returns Array of run times with dayOffset applied
590
+ * @private
591
+ */
592
+ private _enumerateRuns;
593
+ /**
594
+ * Check if a given date matches the cron pattern
595
+ *
596
+ * @param date - Date to check. Can be a Date object or a string representing a date.
597
+ * @returns true if the date matches the pattern, false otherwise
598
+ */
599
+ match(date: Date | string): boolean;
600
+ /**
601
+ * Return the original pattern, if there was one.
602
+ * Returns undefined when the job was created with a Date or ISO 8601 string instead of a cron pattern.
603
+ *
604
+ * @returns Original cron pattern, or undefined for date-based jobs
605
+ */
606
+ getPattern(): string | undefined;
607
+ /**
608
+ * Return the original run-once date, if there was one
609
+ *
610
+ * @returns Original run-once date, or null if not a run-once job
611
+ */
612
+ getOnce(): Date | null;
613
+ /**
614
+ * Indicates whether or not the cron job is scheduled and running, e.g. awaiting next trigger
615
+ *
616
+ * @returns Running or not
617
+ */
618
+ isRunning(): boolean;
619
+ /**
620
+ * Indicates whether or not the cron job is permanently stopped
621
+ *
622
+ * @returns Running or not
623
+ */
624
+ isStopped(): boolean;
625
+ /**
626
+ * Indicates whether or not the cron job is currently working
627
+ *
628
+ * @returns Running or not
629
+ */
630
+ isBusy(): boolean;
631
+ /**
632
+ * Return current/previous run start time
633
+ *
634
+ * @returns Current (if running) or previous run time
635
+ */
636
+ currentRun(): Date | null;
637
+ /**
638
+ * Return previous run start time
639
+ *
640
+ * @returns Previous run time
641
+ */
642
+ previousRun(): Date | null;
643
+ /**
644
+ * Returns number of milliseconds to next run
645
+ *
646
+ * @param prev Starting date, defaults to now - minimum interval
647
+ */
648
+ msToNext(prev?: CronDate<T> | Date | string): number | null;
649
+ /**
650
+ * Stop execution
651
+ *
652
+ * Running this will forcefully stop the job, and prevent furter exection. `.resume()` will not work after stopping.
653
+ * It will also be removed from the scheduledJobs array if it were named.
654
+ */
655
+ stop(): void;
656
+ /**
657
+ * Pause execution
658
+ *
659
+ * @returns Wether pause was successful
660
+ */
661
+ pause(): boolean;
662
+ /**
663
+ * Resume execution
664
+ *
665
+ * @returns Wether resume was successful
666
+ */
667
+ resume(): boolean;
668
+ /**
669
+ * Schedule a new job
670
+ *
671
+ * @param func - Function to be run each iteration of pattern
672
+ */
673
+ schedule(func?: CronCallback<T>): Cron<T>;
674
+ /**
675
+ * Internal function to trigger a run, used by both scheduled and manual trigger
676
+ */
677
+ private _trigger;
678
+ /**
679
+ * Trigger a run manually
680
+ */
681
+ trigger(): Promise<void>;
682
+ /**
683
+ * Returns number of runs left, undefined = unlimited
684
+ */
685
+ runsLeft(): number | undefined;
686
+ /**
687
+ * Called when it's time to trigger.
688
+ * Checks if all conditions are currently met,
689
+ * then instantly triggers the scheduled function.
690
+ */
691
+ private _checkTrigger;
692
+ /**
693
+ * Internal version of next. Cron needs millseconds internally, hence _next.
694
+ */
695
+ private _next;
696
+ /**
697
+ * Internal version of previous. Finds the previous scheduled run time.
698
+ *
699
+ * @param referenceDate - Optional reference date to search backwards from (defaults to now)
700
+ * @returns Previous scheduled run time, or null if no previous run exists
701
+ */
702
+ private _previous;
703
+ /**
704
+ * Calculate the previous run if no previous run is supplied, but startAt and interval are set.
705
+ * This calculation is only necessary if the startAt time is before the current time.
706
+ * Should only be called from the _next function.
707
+ */
708
+ private _calculatePreviousRun;
197
709
  }
198
- export { Cron, CronDate, type CronOptions, CronPattern, scheduledJobs };
710
+
711
+ export {};