cron 2.4.2 → 2.4.4

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,22 @@
1
+ ## [2.4.4](https://github.com/kelektiv/node-cron/compare/v2.4.3...v2.4.4) (2023-09-25)
2
+
3
+
4
+ ### 🐛 Bug Fixes
5
+
6
+ * added fractional offset support ([#685](https://github.com/kelektiv/node-cron/issues/685)) ([ce78478](https://github.com/kelektiv/node-cron/commit/ce784784575b65bd75b8b1a4adda3d8fd42fe1c0))
7
+
8
+ ## [2.4.3](https://github.com/kelektiv/node-cron/compare/v2.4.2...v2.4.3) (2023-08-26)
9
+
10
+
11
+ ### 🐛 Bug Fixes
12
+
13
+ * fix range parsing when upper limit = 0 ([#687](https://github.com/kelektiv/node-cron/issues/687)) ([d96746f](https://github.com/kelektiv/node-cron/commit/d96746f7b8f357e565d1fad48c9f70d3d646da64))
14
+
15
+
16
+ ### 🚨 Tests
17
+
18
+ * add TS types check ([#690](https://github.com/kelektiv/node-cron/issues/690)) ([f046016](https://github.com/kelektiv/node-cron/commit/f046016dc64438c4a12a4615a919b046d3a846de))
19
+
1
20
  ## [2.4.2](https://github.com/kelektiv/node-cron/compare/v2.4.1...v2.4.2) (2023-08-26)
2
21
 
3
22
 
package/README.md CHANGED
@@ -94,10 +94,10 @@ Parameter Based
94
94
  - `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.
95
95
  - `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.
96
96
  - `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.
97
- - `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 all time zones available at [Moment Timezone Website](http://momentjs.com/timezone/). Probably don't use both `timeZone` and `utcOffset` together or weird things may happen.
97
+ - `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.
98
98
  - `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.
99
99
  - `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.
100
- - `utcOffset` - [OPTIONAL] - This allows you to specify the offset of your time zone rather than using the `timeZone` param. This should be an integer amount representing the number of minutes offset (like `120` for +2 hours or `-90` for -1.5 hours) Probably don't use both `timeZone` and `utcOffset` together or weird things may happen.
100
+ - `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.
101
101
  - `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.
102
102
  - `start` - Runs your job.
103
103
  - `stop` - Stops your job.
package/lib/time.js CHANGED
@@ -152,17 +152,26 @@ function CronTime(luxon) {
152
152
  }
153
153
 
154
154
  if (typeof this.utcOffset !== 'undefined') {
155
- let offset =
155
+ const offsetHours = parseInt(
156
156
  this.utcOffset >= 60 || this.utcOffset <= -60
157
157
  ? this.utcOffset / 60
158
- : this.utcOffset;
159
- offset = parseInt(offset);
158
+ : this.utcOffset
159
+ );
160
+
161
+ const offsetMins =
162
+ this.utcOffset >= 60 || this.utcOffset <= -60
163
+ ? Math.abs(this.utcOffset - offsetHours * 60)
164
+ : 0;
165
+ const offsetMinsStr = offsetMins >= 10 ? offsetMins : '0' + offsetMins;
160
166
 
161
167
  let utcZone = 'UTC';
162
- if (offset < 0) {
163
- utcZone += offset;
164
- } else if (offset > 0) {
165
- utcZone += `+${offset}`;
168
+
169
+ if (parseInt(this.utcOffset) < 0) {
170
+ utcZone += `${
171
+ offsetHours === 0 ? '-0' : offsetHours
172
+ }:${offsetMinsStr}`;
173
+ } else {
174
+ utcZone += `+${offsetHours}:${offsetMinsStr}`;
166
175
  }
167
176
 
168
177
  date = date.setZone(utcZone);
@@ -768,7 +777,7 @@ function CronTime(luxon) {
768
777
  if (allRanges[i].match(RE_RANGE)) {
769
778
  allRanges[i].replace(RE_RANGE, ($0, lower, upper, step) => {
770
779
  lower = parseInt(lower, 10);
771
- upper = parseInt(upper, 10) || undefined;
780
+ upper = upper !== undefined ? parseInt(upper, 10) : undefined;
772
781
 
773
782
  const wasStepDefined = !isNaN(parseInt(step, 10));
774
783
  if (step === '0') {
@@ -776,14 +785,14 @@ function CronTime(luxon) {
776
785
  }
777
786
  step = parseInt(step, 10) || 1;
778
787
 
779
- if (upper && lower > upper) {
788
+ if (upper !== undefined && lower > upper) {
780
789
  throw new Error(`Field (${type}) has an invalid range`);
781
790
  }
782
791
 
783
792
  const outOfRangeError =
784
793
  lower < low ||
785
- (upper && upper > high) ||
786
- (!upper && lower > high);
794
+ (upper !== undefined && upper > high) ||
795
+ (upper === undefined && lower > high);
787
796
 
788
797
  if (outOfRangeError) {
789
798
  throw new Error(`Field value (${value}) is out of range`);
@@ -793,7 +802,7 @@ function CronTime(luxon) {
793
802
  lower = Math.min(Math.max(low, ~~Math.abs(lower)), high);
794
803
 
795
804
  // Positive integer lower than constraints[1]
796
- if (upper) {
805
+ if (upper !== undefined) {
797
806
  upper = Math.min(high, ~~Math.abs(upper));
798
807
  } else {
799
808
  // If step is provided, the default upper range is the highest value
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "cron",
3
3
  "description": "Cron jobs for your node",
4
- "version": "2.4.2",
4
+ "version": "2.4.4",
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"
@@ -15,6 +15,7 @@
15
15
  "lint": "eslint {lib,tests}/*.js",
16
16
  "test": "jest --coverage",
17
17
  "test:watch": "jest --watch --coverage",
18
+ "test:types": "tsd",
18
19
  "prepare": "husky install",
19
20
  "release": "semantic-release"
20
21
  },
@@ -46,7 +47,8 @@
46
47
  "jest": "~29.5.x",
47
48
  "prettier": "~2.8.x",
48
49
  "semantic-release": "~21.0.x",
49
- "sinon": "^15.0.x"
50
+ "sinon": "^15.0.x",
51
+ "tsd": "^0.28.1"
50
52
  },
51
53
  "keywords": [
52
54
  "cron",
@@ -0,0 +1,85 @@
1
+ import * as luxon from 'luxon';
2
+ import { expectType } from 'tsd';
3
+ import * as cron from '..';
4
+
5
+ const CronJob = cron.CronJob;
6
+ const CronTime = cron.CronTime;
7
+
8
+ const DateTime = luxon.DateTime;
9
+
10
+ const timeZone = 'America/Los_Angeles';
11
+
12
+ // basic cron usage
13
+ const job = new CronJob(
14
+ '* * * * * *',
15
+ () => {},
16
+ null,
17
+ true
18
+ );
19
+
20
+ // using factory function with parameters object
21
+ expectType<cron.CronJob>(cron.job({
22
+ cronTime: '00 30 11 * * 1-5',
23
+ onTick: () => {},
24
+ start: false,
25
+ timeZone: 'America/Los_Angeles'
26
+ }));
27
+
28
+ // with timezone
29
+ new CronJob(
30
+ '* * * * * *',
31
+ () => {},
32
+ null,
33
+ true,
34
+ timeZone
35
+ );
36
+
37
+ // with onComplete
38
+ new CronJob(
39
+ '00 30 11 * * 1-5',
40
+ () => {},
41
+ () => {},
42
+ true,
43
+ timeZone
44
+ );
45
+
46
+ // with Date
47
+ new CronJob(
48
+ new Date(),
49
+ () => {},
50
+ null,
51
+ true,
52
+ timeZone
53
+ );
54
+
55
+ // with Luxon DateTime
56
+ new CronJob(
57
+ DateTime.local(),
58
+ () => {},
59
+ null,
60
+ true,
61
+ timeZone
62
+ );
63
+
64
+ // with system commands
65
+ new CronJob(
66
+ '00 30 11 * * 1-5',
67
+ 'ls',
68
+ { command: 'ls', args: ['./'] },
69
+ true,
70
+ timeZone
71
+ );
72
+
73
+ // check function return types
74
+ expectType<Date>(job.lastDate());
75
+ expectType<luxon.DateTime>(job.nextDates());
76
+ expectType<luxon.DateTime | luxon.DateTime[]>(job.nextDates(1));
77
+ expectType<boolean | undefined>(job.running);
78
+ job.setTime(new CronTime('00 30 11 * * 1-2'));
79
+ job.start();
80
+ job.stop();
81
+
82
+ // check cronTime format
83
+ new CronTime('* * * * * *');
84
+ new CronTime(new Date());
85
+ new CronTime(DateTime.local());