cron 3.0.0-beta.5 → 3.0.0-beta.7

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 CHANGED
@@ -1,3 +1,26 @@
1
+ ## [3.0.0-beta.7](https://github.com/kelektiv/node-cron/compare/v3.0.0-beta.6...v3.0.0-beta.7) (2023-09-26)
2
+
3
+
4
+ ### 📚 Documentation
5
+
6
+ * **readme:** remove outdated informations ([#695](https://github.com/kelektiv/node-cron/issues/695)) ([51d2bab](https://github.com/kelektiv/node-cron/commit/51d2bab4f1e6a0419f6592efbfdf72f17fa2fb07))
7
+
8
+ ## [3.0.0-beta.6](https://github.com/kelektiv/node-cron/compare/v3.0.0-beta.5...v3.0.0-beta.6) (2023-09-26)
9
+
10
+
11
+ ### ⚠ Breaking changes
12
+
13
+ * removed `cron.job()` method in favor of `new CronJob(...args)` /
14
+ `CronJob.from(argsObject)`
15
+ * removed `cron.time()` method in favor of `new CronTime()`
16
+ * `CronJob`: constructor no longer accepts an object as its first and
17
+ only params. Use `CronJob.from(argsObject)` instead.
18
+ * `CronJob`: callbacks are now called in the order they were registered
19
+
20
+ ### 📦 Code Refactoring
21
+
22
+ * migrate to TypeScript ([#694](https://github.com/kelektiv/node-cron/issues/694)) ([97a65e1](https://github.com/kelektiv/node-cron/commit/97a65e109d473cb037e2fb4690c18ac897d5316a))
23
+
1
24
  ## [3.0.0-beta.5](https://github.com/kelektiv/node-cron/compare/v3.0.0-beta.4...v3.0.0-beta.5) (2023-09-25)
2
25
 
3
26
 
package/README.md CHANGED
@@ -12,59 +12,66 @@
12
12
  [![Known Vulnerabilities](https://snyk.io/test/github/kelektiv/node-cron/badge.svg)](https://snyk.io/test/github/kelektiv/node-cron)
13
13
  [![Minified size](https://badgen.net/bundlephobia/min/cron)](https://badgen.net/bundlephobia/min/cron)
14
14
  [![Minzipped size](https://badgen.net/bundlephobia/minzip/cron)](https://badgen.net/bundlephobia/minzip/cron)
15
- [![monthly downloads](https://badgen.net/npm/dm/cron?icon=npm)](https://badgen.net/npm/dm/cron)
15
+ [![monthly downloads](https://badgen.net/npm/dm/cron?icon=npm)](https://badgen.net/npm/dm/cron)
16
16
 
17
17
  Cron is a tool that allows you to execute _something_ on a schedule. This is typically done using the cron syntax. We allow you to:
18
18
 
19
19
  - execute a function whenever your scheduled job triggers
20
- - execute a job external to the javascript process using `child_process`
21
- - use a Date object instead of cron syntax as the trigger for your callback
20
+ - execute a job external to the javascript process (like a system command) using `child_process`
21
+ - use a Date or Luxon DateTime object instead of cron syntax as the trigger for your callback
22
22
  - use an additional slot for seconds (leaving it off will default to 0 and match the Unix behavior)
23
23
 
24
- ## Installation
24
+ ## Installation
25
25
 
26
- ```
27
- npm install cron
26
+ ```bash
27
+ npm install cron
28
28
  ```
29
29
 
30
- ## Migrating from v2 to v3
30
+ ## Migrating from v2 to v3
31
31
 
32
- In version 3 of this library, we aligned our format for the cron patterns with the UNIX format. See below for the changes you need to make when upgrading:
32
+ In version 3 of this library, we migrated to TypeScript, aligned our cron patterns format with the UNIX standard, and released some other breaking changes. See below for the changes you need to make when upgrading:
33
33
 
34
34
  <details>
35
35
  <summary>Migrating from v2 to v3</summary>
36
36
 
37
- ### Month & day-of-week indexing changes
38
-
39
- **Month indexing went from `0-11` to `1-12`. So you need to increment all numeric months by 1.**
37
+ ### Month & day-of-week indexing changes
40
38
 
41
- For day-of-week indexing, we only added support for `7` as Sunday, so you don't need to change anything !
39
+ **Month indexing went from `0-11` to `1-12`. So you need to increment all numeric months by 1.**
42
40
 
43
- </details>
41
+ For day-of-week indexing, we only added support for `7` as Sunday, so you don't need to change anything!
42
+
43
+ ### CronJob changes
44
+
45
+ - **constructor no longer accepts an object as its first and only params. Use `CronJob.from(argsObject)` instead.**
46
+ - **callbacks are now called in the order they were registered.**
47
+
48
+ ### Removed methods
44
49
 
45
- ## Versions and Backwards compatibility breaks
50
+ - **removed `job()` method in favor of `new CronJob(...args)` / `CronJob.from(argsObject)`**
46
51
 
47
- As goes with semver, breaking backwards compatibility should be explicit in the versioning of your library. As such, we'll upgrade the version of this module in accordance with breaking changes (We're not always great about doing it this way so if you notice that there are breaking changes that haven't been bumped appropriately please let us know).
52
+ - **removed `time()` method in favor of `new CronTime()`**
53
+
54
+ </details>
48
55
 
49
56
  ## Usage (basic cron usage)
50
57
 
51
58
  ```javascript
52
- var CronJob = require('cron').CronJob;
53
- var job = new CronJob(
54
- '* * * * * *',
55
- function () {
56
- console.log('You will see this message every second');
57
- },
58
- null,
59
- true,
60
- 'America/Los_Angeles'
59
+ import { CronJob } from 'cron';
60
+ const job = new CronJob(
61
+ '* * * * * *',
62
+ function () {
63
+ console.log('You will see this message every second');
64
+ },
65
+ null,
66
+ true,
67
+ 'America/Los_Angeles'
61
68
  );
62
69
  // job.start() - See note below when to use this
63
- ```
70
+ ```
64
71
 
65
72
  Note - In the example above, the 4th parameter of `CronJob()` automatically starts the job on initialization. If this parameter is falsy or not provided, the job needs to be explicitly started using `job.start()`.
66
73
 
67
- There are more examples available in this repository at: [/examples](https://github.com/kelektiv/node-cron/tree/main/examples)
74
+ There are more examples available in this repository at: [/examples](https://github.com/kelektiv/node-cron/tree/main/examples)
68
75
 
69
76
  ## Available Cron patterns
70
77
 
@@ -74,11 +81,11 @@ Ranges e.g. 1-3,5
74
81
  Steps e.g. */2
75
82
  ```
76
83
 
77
- [Read up on cron patterns here](http://crontab.org). Note the examples in the link have five fields, and 1 minute as the finest granularity, but this library has six fields, with 1 second as the finest granularity.
84
+ [Read up on cron patterns here](http://crontab.org). Note the examples in the link have five fields, and 1 minute as the finest granularity, but this library has six fields, with 1 second as the finest granularity.
78
85
 
79
- There are tools that help when constructing your cronjobs. You might find something like https://crontab.guru/ or https://cronjob.xyz/ helpful. But, note that these don't necessarily accept the exact same syntax as this library, for instance, it doesn't accept the `seconds` field, so keep that in mind.
86
+ There are tools that help when constructing your cronjobs. You might find something like https://crontab.guru/ or https://cronjob.xyz/ helpful. But, note that these don't necessarily accept the exact same syntax as this library, for instance, it doesn't accept the `seconds` field, so keep that in mind.
80
87
 
81
- ### Cron Ranges
88
+ ### Cron Ranges
82
89
 
83
90
  This library follows the [UNIX Cron format](https://man7.org/linux/man-pages/man5/crontab.5.html), with an added field at the beginning for second granularity.
84
91
 
@@ -96,42 +103,42 @@ day of week 0-7 (0 or 7 is Sunday, or use names)
96
103
  > Names can also be used for the 'month' and 'day of week' fields. Use the first three letters of the particular day or month (case does not matter). Ranges and lists of names are allowed.
97
104
  > Examples: "mon,wed,fri", "jan-mar".
98
105
 
99
- ## Gotchas
106
+ ## Gotchas
100
107
 
101
- - Months are indexed as 0-11 instead of 1-12. This is different from Unix `cron` and is planned to updated to match Unix `cron` in v3.0.0 of `node-cron`.
102
108
  - Millisecond level granularity in JS `Date` or Luxon `DateTime` objects: Because computers take time to do things, there may be some delay in execution. This should be on the order of milliseconds. This module doesn't allow MS level granularity for the regular cron syntax, but _does_ allow you to specify a real date of execution in either a javascript `Date` object or a Luxon `DateTime` object. When this happens you may find that you aren't able to execute a job that _should_ run in the future like with `new Date().setMilliseconds(new Date().getMilliseconds() + 1)`. This is due to those cycles of execution above. This wont be the same for everyone because of compute speed. When we tried it locally we saw that somewhere around the 4-5 ms mark was where we got consistent ticks using real dates, but anything less than that would result in an exception. This could be really confusing. We could restrict the granularity for all dates to seconds, but felt that it wasn't a huge problem so long as you were made aware. If this becomes more of an issue, We can revisit it.
103
- - Arrow Functions for `onTick`: Arrow functions get their `this` context from their parent scope. Thus, if you use them, you will not get the `this` context of the cronjob. You can read a little more in issue [GH-47](https://github.com/kelektiv/node-cron/issues/47#issuecomment-459762775)
109
+ - Arrow Functions for `onTick`: Arrow functions get their `this` context from their parent scope. Thus, if you use them, you will not get the `this` context of the cronjob. You can read a little more in issue [GH-47](https://github.com/kelektiv/node-cron/issues/47#issuecomment-459762775)
104
110
 
105
- ## API
111
+ ## API
106
112
 
107
- Parameter Based
113
+ Parameter Based
108
114
 
109
- - `job` - shortcut to `new cron.CronJob()`.
110
- - `time` - shortcut to `new cron.CronTime()`.
111
115
  - `sendAt` - tells you when a `CronTime` will be run.
112
116
  - `timeout` - tells you when the next timeout is.
113
117
  - `CronJob`
114
- - `constructor(cronTime, onTick, onComplete, start, timeZone, context, runOnInit, utcOffset, unrefTimeout)` - Of note, the first parameter here can be a JSON object that has the below names and associated types (see examples above).
115
- - `cronTime` - [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS [Date](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date) object.
116
- - `onTick` - [REQUIRED] - The function to fire at the specified time. If an `onComplete` callback was provided, `onTick` will receive it as an argument. `onTick` may call `onComplete` when it has finished its work.
117
- - `onComplete` - [OPTIONAL] - A function that will fire when the job is stopped with `job.stop()`, and may also be called by `onTick` at the end of each run.
118
- - `start` - [OPTIONAL] - Specifies whether to start the job just before exiting the constructor. By default this is set to false. If left at default you will need to call `job.start()` in order to start the job (assuming `job` is the variable you set the cronjob to). This does not immediately fire your `onTick` function, it just gives you more control over the behavior of your jobs.
119
- - `timeZone` - [OPTIONAL] - Specify the time zone for the execution. This will modify the actual time relative to your time zone. If the time zone is invalid, an error is thrown. By default (if this is omitted) the local time zone will be used. You can check the various time zones format accepted in the [Luxon documentation](https://github.com/moment/luxon/blob/master/docs/zones.md#specifying-a-zone). Note: This parameter supports minutes offsets, e.g. `UTC+5:30`. **Warning**: Probably don't use both `timeZone` and `utcOffset` together or weird things may happen.
120
- - `context` - [OPTIONAL] - The context within which to execute the onTick method. This defaults to the cronjob itself allowing you to call `this.stop()`. However, if you change this you'll have access to the functions and values within your context object.
121
- - `runOnInit` - [OPTIONAL] - This will immediately fire your `onTick` function as soon as the requisite initialization has happened. This option is set to `false` by default for backwards compatibility.
122
- - `utcOffset` - [OPTIONAL] - This allows you to specify the offset of your time zone rather than using the `timeZone` param. This should be an integer representing the number of minutes offset (like `120` for +2 hours or `-90` for -1.5 hours). **Warning**: Minutes offsets < 60 and >-60 will be treated as an offset in hours. This means a minute offset of `30` means an offset of +30 hours. Use the `timeZone` param in this case. This behavior [is planned to be removed in V3](https://github.com/kelektiv/node-cron/pull/685#issuecomment-1676417917). **Warning**: Probably don't use both `timeZone` and `utcOffset` together or weird things may happen.
123
- - `unrefTimeout` - [OPTIONAL] - If you have code that keeps the event loop running and want to stop the node process when that finishes regardless of the state of your cronjob, you can do so making use of this parameter. This is off by default and cron will run as if it needs to control the event loop. For more information take a look at [timers#timers_timeout_unref](https://nodejs.org/api/timers.html#timers_timeout_unref) from the NodeJS docs.
124
- - `start` - Runs your job.
125
- - `stop` - Stops your job.
126
- - `setTime` - Stops and changes the time for the `CronJob`. Param must be a `CronTime`.
127
- - `lastDate` - Tells you the last execution date.
128
- - `nextDate` - Provides the next date that will trigger an `onTick`.
129
- - `nextDates` - Provides an array of the next set of dates that will trigger an `onTick`.
130
- - `fireOnTick` - Allows you to override the `onTick` calling behavior. This matters so only do this if you have a really good reason to do so.
131
- - `addCallback` - Allows you to add `onTick` callbacks.
118
+ - `constructor(cronTime, onTick, onComplete, start, timeZone, context, runOnInit, utcOffset, unrefTimeout)`
119
+ - `cronTime` - [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS [Date](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date) object.
120
+ - `onTick` - [REQUIRED] - The function to fire at the specified time. If an `onComplete` callback was provided, `onTick` will receive it as an argument. `onTick` may call `onComplete` when it has finished its work.
121
+ - `onComplete` - [OPTIONAL] - A function that will fire when the job is stopped with `job.stop()`, and may also be called by `onTick` at the end of each run.
122
+ - `start` - [OPTIONAL] - Specifies whether to start the job just before exiting the constructor. By default this is set to false. If left at default you will need to call `job.start()` in order to start the job (assuming `job` is the variable you set the cronjob to). This does not immediately fire your `onTick` function, it just gives you more control over the behavior of your jobs.
123
+ - `timeZone` - [OPTIONAL] - Specify the time zone for the execution. This will modify the actual time relative to your time zone. If the time zone is invalid, an error is thrown. By default (if this is omitted) the local time zone will be used. You can check the various time zones format accepted in the [Luxon documentation](https://github.com/moment/luxon/blob/master/docs/zones.md#specifying-a-zone). Note: This parameter supports minutes offsets, e.g. `UTC+5:30`. **Warning**: Probably don't use both `timeZone` and `utcOffset` together or weird things may happen.
124
+ - `context` - [OPTIONAL] - The context within which to execute the onTick method. This defaults to the cronjob itself allowing you to call `this.stop()`. However, if you change this you'll have access to the functions and values within your context object.
125
+ - `runOnInit` - [OPTIONAL] - This will immediately fire your `onTick` function as soon as the requisite initialization has happened. This option is set to `false` by default for backwards compatibility.
126
+ - `utcOffset` - [OPTIONAL] - This allows you to specify the offset of your time zone rather than using the `timeZone` param. This should be an integer representing the number of minutes offset (like `120` for +2 hours or `-90` for -1.5 hours). **Warning**: Minutes offsets < 60 and >-60 will be treated as an offset in hours. This means a minute offset of `30` means an offset of +30 hours. Use the `timeZone` param in this case. This behavior [is planned to be removed in V3](https://github.com/kelektiv/node-cron/pull/685#issuecomment-1676417917). **Warning**: Probably don't use both `timeZone` and `utcOffset` together or weird things may happen.
127
+ - `unrefTimeout` - [OPTIONAL] - If you have code that keeps the event loop running and want to stop the node process when that finishes regardless of the state of your cronjob, you can do so making use of this parameter. This is off by default and cron will run as if it needs to control the event loop. For more information take a look at [timers#timers_timeout_unref](https://nodejs.org/api/timers.html#timers_timeout_unref) from the NodeJS docs.
128
+ - `from` (static) - Create a new CronJob object providing arguments as an object. See argument names and descriptions above.
129
+ - `start` - Runs your job.
130
+ - `stop` - Stops your job.
131
+ - `setTime` - Stops and changes the time for the `CronJob`. Param must be a `CronTime`.
132
+ - `lastDate` - Tells you the last execution date.
133
+ - `nextDate` - Provides the next date that will trigger an `onTick`.
134
+ - `nextDates` - Provides an array of the next set of dates that will trigger an `onTick`.
135
+ - `fireOnTick` - Allows you to override the `onTick` calling behavior. This matters so only do this if you have a really good reason to do so.
136
+ - `addCallback` - Allows you to add `onTick` callbacks. Callbacks are run in the order they are registered.
132
137
  - `CronTime`
133
- - `constructor(time)`
134
- - `time` - [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS [Date](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date) object.
138
+ - `constructor(time, zone, utcOffset)`
139
+ - `time` - [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS [Date](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date) object.
140
+ - `zone` - [OPTIONAL] - Same as `timeZone` from `CronJob` parameters.
141
+ - `utcOffset` - [OPTIONAL] - Same as `utcOffset` from `CronJob` parameters.
135
142
 
136
143
  ## Community
137
144
 
@@ -151,8 +158,10 @@ Because we can't magically know what you are doing to expose an issue, it is bes
151
158
 
152
159
  ### Acknowledgements
153
160
 
154
- This is a community effort project. In the truest sense, this project started as an open source project from [cron.js](http://github.com/padolsey/cron.js) and grew into something else. Other people have contributed code, time, and oversight to the project. At this point there are too many to name here so We'll just say thanks.
161
+ This is a community effort project. In the truest sense, this project started as an open source project from [cron.js](http://github.com/padolsey/cron.js) and grew into something else. Other people have contributed code, time, and oversight to the project. At this point there are too many to name here so we'll just say thanks.
162
+
163
+ Special thanks to [Hiroki Horiuchi](https://github.com/horiuchi), [Lundarl Gholoi](https://github.com/winup) and [koooge](https://github.com/koooge) for their work on the [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped) typings before they were imported in v2.4.0.
155
164
 
156
- ## License
165
+ ## License
157
166
 
158
167
  MIT
@@ -0,0 +1,74 @@
1
+ export declare const CONSTRAINTS: Readonly<{
2
+ readonly second: readonly [0, 59];
3
+ readonly minute: readonly [0, 59];
4
+ readonly hour: readonly [0, 23];
5
+ readonly dayOfMonth: readonly [1, 31];
6
+ readonly month: readonly [1, 12];
7
+ readonly dayOfWeek: readonly [0, 7];
8
+ }>;
9
+ export declare const MONTH_CONSTRAINTS: Readonly<{
10
+ readonly 1: 31;
11
+ readonly 2: 29;
12
+ readonly 3: 31;
13
+ readonly 4: 30;
14
+ readonly 5: 31;
15
+ readonly 6: 30;
16
+ readonly 7: 31;
17
+ readonly 8: 31;
18
+ readonly 9: 30;
19
+ readonly 10: 31;
20
+ readonly 11: 30;
21
+ readonly 12: 31;
22
+ }>;
23
+ export declare const PARSE_DEFAULTS: Readonly<{
24
+ readonly second: "0";
25
+ readonly minute: "*";
26
+ readonly hour: "*";
27
+ readonly dayOfMonth: "*";
28
+ readonly month: "*";
29
+ readonly dayOfWeek: "*";
30
+ }>;
31
+ export declare const ALIASES: Readonly<{
32
+ readonly jan: 1;
33
+ readonly feb: 2;
34
+ readonly mar: 3;
35
+ readonly apr: 4;
36
+ readonly may: 5;
37
+ readonly jun: 6;
38
+ readonly jul: 7;
39
+ readonly aug: 8;
40
+ readonly sep: 9;
41
+ readonly oct: 10;
42
+ readonly nov: 11;
43
+ readonly dec: 12;
44
+ readonly sun: 0;
45
+ readonly mon: 1;
46
+ readonly tue: 2;
47
+ readonly wed: 3;
48
+ readonly thu: 4;
49
+ readonly fri: 5;
50
+ readonly sat: 6;
51
+ }>;
52
+ export declare const TIME_UNITS_MAP: Readonly<{
53
+ readonly SECOND: "second";
54
+ readonly MINUTE: "minute";
55
+ readonly HOUR: "hour";
56
+ readonly DAY_OF_MONTH: "dayOfMonth";
57
+ readonly MONTH: "month";
58
+ readonly DAY_OF_WEEK: "dayOfWeek";
59
+ }>;
60
+ export declare const TIME_UNITS: ["second", "minute", "hour", "dayOfMonth", "month", "dayOfWeek"];
61
+ export declare const TIME_UNITS_LEN: number;
62
+ export declare const PRESETS: Readonly<{
63
+ readonly '@yearly': "0 0 0 1 1 *";
64
+ readonly '@monthly': "0 0 0 1 * *";
65
+ readonly '@weekly': "0 0 0 * * 0";
66
+ readonly '@daily': "0 0 0 * * *";
67
+ readonly '@hourly': "0 0 * * * *";
68
+ readonly '@minutely': "0 * * * * *";
69
+ readonly '@secondly': "* * * * * *";
70
+ readonly '@weekdays': "0 0 0 * * 1-5";
71
+ readonly '@weekends': "0 0 0 * * 0,6";
72
+ }>;
73
+ export declare const RE_WILDCARDS: RegExp;
74
+ export declare const RE_RANGE: RegExp;
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RE_RANGE = exports.RE_WILDCARDS = exports.PRESETS = exports.TIME_UNITS_LEN = exports.TIME_UNITS = exports.TIME_UNITS_MAP = exports.ALIASES = exports.PARSE_DEFAULTS = exports.MONTH_CONSTRAINTS = exports.CONSTRAINTS = void 0;
4
+ exports.CONSTRAINTS = Object.freeze({
5
+ second: [0, 59],
6
+ minute: [0, 59],
7
+ hour: [0, 23],
8
+ dayOfMonth: [1, 31],
9
+ month: [1, 12],
10
+ dayOfWeek: [0, 7]
11
+ });
12
+ exports.MONTH_CONSTRAINTS = Object.freeze({
13
+ 1: 31,
14
+ 2: 29,
15
+ 3: 31,
16
+ 4: 30,
17
+ 5: 31,
18
+ 6: 30,
19
+ 7: 31,
20
+ 8: 31,
21
+ 9: 30,
22
+ 10: 31,
23
+ 11: 30,
24
+ 12: 31
25
+ });
26
+ exports.PARSE_DEFAULTS = Object.freeze({
27
+ second: '0',
28
+ minute: '*',
29
+ hour: '*',
30
+ dayOfMonth: '*',
31
+ month: '*',
32
+ dayOfWeek: '*'
33
+ });
34
+ exports.ALIASES = Object.freeze({
35
+ jan: 1,
36
+ feb: 2,
37
+ mar: 3,
38
+ apr: 4,
39
+ may: 5,
40
+ jun: 6,
41
+ jul: 7,
42
+ aug: 8,
43
+ sep: 9,
44
+ oct: 10,
45
+ nov: 11,
46
+ dec: 12,
47
+ sun: 0,
48
+ mon: 1,
49
+ tue: 2,
50
+ wed: 3,
51
+ thu: 4,
52
+ fri: 5,
53
+ sat: 6
54
+ });
55
+ exports.TIME_UNITS_MAP = Object.freeze({
56
+ SECOND: 'second',
57
+ MINUTE: 'minute',
58
+ HOUR: 'hour',
59
+ DAY_OF_MONTH: 'dayOfMonth',
60
+ MONTH: 'month',
61
+ DAY_OF_WEEK: 'dayOfWeek'
62
+ });
63
+ exports.TIME_UNITS = Object.freeze(Object.values(exports.TIME_UNITS_MAP));
64
+ exports.TIME_UNITS_LEN = exports.TIME_UNITS.length;
65
+ exports.PRESETS = Object.freeze({
66
+ '@yearly': '0 0 0 1 1 *',
67
+ '@monthly': '0 0 0 1 * *',
68
+ '@weekly': '0 0 0 * * 0',
69
+ '@daily': '0 0 0 * * *',
70
+ '@hourly': '0 0 * * * *',
71
+ '@minutely': '0 * * * * *',
72
+ '@secondly': '* * * * * *',
73
+ '@weekdays': '0 0 0 * * 1-5',
74
+ '@weekends': '0 0 0 * * 0,6'
75
+ });
76
+ exports.RE_WILDCARDS = /\*/g;
77
+ exports.RE_RANGE = /^(\d+)(?:-(\d+))?(?:\/(\d+))?$/g;
78
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1,5 @@
1
+ import { DateTime } from 'luxon';
2
+ export { CronJob } from './job';
3
+ export { CronTime } from './time';
4
+ export declare const sendAt: (cronTime: string | Date | DateTime) => DateTime;
5
+ export declare const timeout: (cronTime: string | Date | DateTime) => number;
package/dist/index.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.timeout = exports.sendAt = exports.CronTime = exports.CronJob = void 0;
4
+ const time_1 = require("./time");
5
+ var job_1 = require("./job");
6
+ Object.defineProperty(exports, "CronJob", { enumerable: true, get: function () { return job_1.CronJob; } });
7
+ var time_2 = require("./time");
8
+ Object.defineProperty(exports, "CronTime", { enumerable: true, get: function () { return time_2.CronTime; } });
9
+ const sendAt = (cronTime) => new time_1.CronTime(cronTime).sendAt();
10
+ exports.sendAt = sendAt;
11
+ const timeout = (cronTime) => new time_1.CronTime(cronTime).getTimeout();
12
+ exports.timeout = timeout;
13
+ //# sourceMappingURL=index.js.map
package/dist/job.d.ts ADDED
@@ -0,0 +1,24 @@
1
+ import { CronTime } from './time';
2
+ import { CronJobParams } from './types/cron.types';
3
+ export declare class CronJob {
4
+ cronTime: CronTime;
5
+ running: boolean;
6
+ unrefTimeout: boolean;
7
+ lastExecution: Date | null;
8
+ runOnce: boolean;
9
+ context: unknown;
10
+ onComplete?: (...args: any) => void;
11
+ private _timeout?;
12
+ private _callbacks;
13
+ constructor(cronTime: CronJobParams['cronTime'], onTick: CronJobParams['onTick'], onComplete?: CronJobParams['onComplete'], start?: CronJobParams['start'], timeZone?: CronJobParams['timeZone'], context?: CronJobParams['context'], runOnInit?: CronJobParams['runOnInit'], utcOffset?: CronJobParams['utcOffset'], unrefTimeout?: CronJobParams['unrefTimeout']);
14
+ static from(params: CronJobParams): CronJob;
15
+ private _fnWrap;
16
+ addCallback(callback: (...args: any) => void): void;
17
+ setTime(time: CronTime): void;
18
+ nextDate(): import("luxon").DateTime;
19
+ fireOnTick(): void;
20
+ nextDates(i?: number): import("luxon").DateTime[];
21
+ start(): void;
22
+ lastDate(): Date | null;
23
+ stop(): void;
24
+ }
package/dist/job.js ADDED
@@ -0,0 +1,145 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CronJob = void 0;
4
+ const child_process_1 = require("child_process");
5
+ const time_1 = require("./time");
6
+ class CronJob {
7
+ constructor(cronTime, onTick, onComplete, start, timeZone, context, runOnInit, utcOffset, unrefTimeout) {
8
+ this.running = false;
9
+ this.unrefTimeout = false;
10
+ this.lastExecution = null;
11
+ this.runOnce = false;
12
+ this._callbacks = [];
13
+ this.context = context || this;
14
+ this.cronTime = new time_1.CronTime(cronTime, timeZone, utcOffset);
15
+ if (unrefTimeout != null) {
16
+ this.unrefTimeout = unrefTimeout;
17
+ }
18
+ if (onComplete != null) {
19
+ this.onComplete = this._fnWrap(onComplete);
20
+ }
21
+ if (this.cronTime.realDate) {
22
+ this.runOnce = true;
23
+ }
24
+ this.addCallback(this._fnWrap(onTick));
25
+ if (runOnInit) {
26
+ this.lastExecution = new Date();
27
+ this.fireOnTick();
28
+ }
29
+ if (start)
30
+ this.start();
31
+ }
32
+ static from(params) {
33
+ return new CronJob(params.cronTime, params.onTick, params.onComplete, params.start, params.timeZone, params.context, params.runOnInit, params.utcOffset, params.unrefTimeout);
34
+ }
35
+ _fnWrap(cmd) {
36
+ var _a, _b;
37
+ switch (typeof cmd) {
38
+ case 'function': {
39
+ return cmd;
40
+ }
41
+ case 'string': {
42
+ const [command, ...args] = cmd.split(' ');
43
+ return child_process_1.spawn.bind(undefined, command !== null && command !== void 0 ? command : cmd, args);
44
+ }
45
+ case 'object': {
46
+ return child_process_1.spawn.bind(undefined, cmd.command, (_a = cmd.args) !== null && _a !== void 0 ? _a : [], (_b = cmd.options) !== null && _b !== void 0 ? _b : {});
47
+ }
48
+ }
49
+ }
50
+ addCallback(callback) {
51
+ if (typeof callback === 'function') {
52
+ this._callbacks.push(callback);
53
+ }
54
+ }
55
+ setTime(time) {
56
+ if (!(time instanceof time_1.CronTime)) {
57
+ throw new Error('time must be an instance of CronTime.');
58
+ }
59
+ const wasRunning = this.running;
60
+ this.stop();
61
+ this.cronTime = time;
62
+ if (wasRunning)
63
+ this.start();
64
+ }
65
+ nextDate() {
66
+ return this.cronTime.sendAt();
67
+ }
68
+ fireOnTick() {
69
+ for (const callback of this._callbacks) {
70
+ callback.call(this.context, this.onComplete);
71
+ }
72
+ }
73
+ nextDates(i) {
74
+ return this.cronTime.sendAt(i !== null && i !== void 0 ? i : 0);
75
+ }
76
+ start() {
77
+ if (this.running) {
78
+ return;
79
+ }
80
+ const MAXDELAY = 2147483647;
81
+ let timeout = this.cronTime.getTimeout();
82
+ let remaining = 0;
83
+ let startTime;
84
+ const setCronTimeout = (t) => {
85
+ startTime = Date.now();
86
+ this._timeout = setTimeout(callbackWrapper, t);
87
+ if (this.unrefTimeout && typeof this._timeout.unref === 'function') {
88
+ this._timeout.unref();
89
+ }
90
+ };
91
+ const callbackWrapper = () => {
92
+ const diff = startTime + timeout - Date.now();
93
+ if (diff > 0) {
94
+ let newTimeout = this.cronTime.getTimeout();
95
+ if (newTimeout > diff) {
96
+ newTimeout = diff;
97
+ }
98
+ remaining += newTimeout;
99
+ }
100
+ this.lastExecution = new Date();
101
+ if (remaining) {
102
+ if (remaining > MAXDELAY) {
103
+ remaining -= MAXDELAY;
104
+ timeout = MAXDELAY;
105
+ }
106
+ else {
107
+ timeout = remaining;
108
+ remaining = 0;
109
+ }
110
+ setCronTimeout(timeout);
111
+ }
112
+ else {
113
+ this.running = false;
114
+ if (!this.runOnce) {
115
+ this.start();
116
+ }
117
+ this.fireOnTick();
118
+ }
119
+ };
120
+ if (timeout >= 0) {
121
+ this.running = true;
122
+ if (timeout > MAXDELAY) {
123
+ remaining = timeout - MAXDELAY;
124
+ timeout = MAXDELAY;
125
+ }
126
+ setCronTimeout(timeout);
127
+ }
128
+ else {
129
+ this.stop();
130
+ }
131
+ }
132
+ lastDate() {
133
+ return this.lastExecution;
134
+ }
135
+ stop() {
136
+ if (this._timeout)
137
+ clearTimeout(this._timeout);
138
+ this.running = false;
139
+ if (typeof this.onComplete === 'function') {
140
+ this.onComplete();
141
+ }
142
+ }
143
+ }
144
+ exports.CronJob = CronJob;
145
+ //# sourceMappingURL=job.js.map
package/dist/time.d.ts ADDED
@@ -0,0 +1,31 @@
1
+ import { DateTime, Zone } from 'luxon';
2
+ export declare class CronTime {
3
+ source: string | DateTime;
4
+ zone?: string;
5
+ utcOffset?: number | string;
6
+ realDate: boolean;
7
+ private second;
8
+ private minute;
9
+ private hour;
10
+ private dayOfMonth;
11
+ private month;
12
+ private dayOfWeek;
13
+ constructor(source: string | Date | DateTime, zone?: string | null, utcOffset?: string | number | null);
14
+ private _getWeekDay;
15
+ private _verifyParse;
16
+ sendAt(): DateTime;
17
+ sendAt(i: number): DateTime[];
18
+ getTimeout(): number;
19
+ toString(): string;
20
+ toJSON(): string[];
21
+ getNextDateFrom(start: Date | DateTime, zone?: string | Zone): DateTime;
22
+ private _findPreviousDSTJump;
23
+ private _checkTimeInSkippedRange;
24
+ private _checkTimeInSkippedRangeSingleHour;
25
+ private _checkTimeInSkippedRangeMultiHour;
26
+ private _forwardDSTJump;
27
+ private _wcOrAll;
28
+ private _hasAll;
29
+ private _parse;
30
+ private _parseField;
31
+ }