@hotmeshio/hotmesh 0.14.8 → 0.14.9

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.
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.arrayToHash = exports.isStreamMessage = exports.parseStreamMessage = exports.normalizeRetryPolicy = exports.s = exports.isValidCron = exports.restoreHierarchy = exports.getValueByPath = exports.getIndexedHash = exports.getSymVal = exports.getSymKey = exports.formatISODate = exports.getTimeSeries = exports.getSubscriptionTopic = exports.findSubscriptionForTrigger = exports.findTopKey = exports.matchesStatus = exports.matchesStatusCode = exports.polyfill = exports.identifyProvider = exports.XSleepFor = exports.sleepImmediate = exports.sleepFor = exports.guid = exports.deterministicRandom = exports.deepCopy = exports.getSystemHealth = exports.hashOptions = void 0;
7
7
  const os_1 = __importDefault(require("os"));
8
8
  const crypto_1 = require("crypto");
9
+ const cron_parser_1 = require("cron-parser");
9
10
  const nanoid_1 = require("nanoid");
10
11
  const ms_1 = __importDefault(require("ms"));
11
12
  const logger_1 = require("../services/logger");
@@ -276,8 +277,13 @@ exports.restoreHierarchy = restoreHierarchy;
276
277
  * @private
277
278
  */
278
279
  function isValidCron(cronExpression) {
279
- const cronRegex = /^(\*|([0-5]?\d)) (\*|([01]?\d|2[0-3])) (\*|([12]?\d|3[01])) (\*|([1-9]|1[0-2])) (\*|([0-6](?:-[0-6])?(?:,[0-6])?))$/;
280
- return cronRegex.test(cronExpression);
280
+ try {
281
+ (0, cron_parser_1.parseExpression)(cronExpression);
282
+ return true;
283
+ }
284
+ catch {
285
+ return false;
286
+ }
281
287
  }
282
288
  exports.isValidCron = isValidCron;
283
289
  /**
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hotmeshio/hotmesh",
3
- "version": "0.14.8",
3
+ "version": "0.14.9",
4
4
  "description": "Durable Workflow",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -1,4 +1,3 @@
1
- import { ILogger } from '../../../types/logger';
2
1
  /**
3
2
  * Provides cron-related utility functions based on the
4
3
  * [cron](https://en.wikipedia.org/wiki/Cron) format for use
@@ -8,16 +7,14 @@ import { ILogger } from '../../../types/logger';
8
7
  * Invoked in mapping rules using `{@cron.<method>}` syntax.
9
8
  */
10
9
  declare class CronHandler {
11
- static logger: ILogger;
12
10
  /**
13
- * Safely calculates the delay in seconds until the next execution
14
- * of a cron job. Calculates the next date and time (per the system
15
- * clock) that satisfies the cron expression, then returns the value
16
- * in seconds from now. Fails silently and returns `-1` if the cron
17
- * expression is invalid or the next execution time is in the past.
11
+ * Calculates the delay in seconds until the next execution
12
+ * of a cron job. Throws on invalid expressions rather than
13
+ * degrading silently.
18
14
  *
19
15
  * @param {string} cronExpression - The cron expression to parse (e.g. `'0 0 * * *'`)
20
- * @returns {number} The delay in seconds until the next cron job execution (minimum `HMSH_FIDELITY_SECONDS`), or `-1` on error
16
+ * @returns {number} The delay in seconds until the next cron job execution (minimum `HMSH_FIDELITY_SECONDS`)
17
+ * @throws {Error} If the cron expression is invalid
21
18
  * @example
22
19
  * ```yaml
23
20
  * cron_next_result:
@@ -4,7 +4,6 @@ exports.CronHandler = void 0;
4
4
  const cron_parser_1 = require("cron-parser");
5
5
  const enums_1 = require("../../../modules/enums");
6
6
  const utils_1 = require("../../../modules/utils");
7
- const logger_1 = require("../../logger");
8
7
  /**
9
8
  * Provides cron-related utility functions based on the
10
9
  * [cron](https://en.wikipedia.org/wiki/Cron) format for use
@@ -15,14 +14,13 @@ const logger_1 = require("../../logger");
15
14
  */
16
15
  class CronHandler {
17
16
  /**
18
- * Safely calculates the delay in seconds until the next execution
19
- * of a cron job. Calculates the next date and time (per the system
20
- * clock) that satisfies the cron expression, then returns the value
21
- * in seconds from now. Fails silently and returns `-1` if the cron
22
- * expression is invalid or the next execution time is in the past.
17
+ * Calculates the delay in seconds until the next execution
18
+ * of a cron job. Throws on invalid expressions rather than
19
+ * degrading silently.
23
20
  *
24
21
  * @param {string} cronExpression - The cron expression to parse (e.g. `'0 0 * * *'`)
25
- * @returns {number} The delay in seconds until the next cron job execution (minimum `HMSH_FIDELITY_SECONDS`), or `-1` on error
22
+ * @returns {number} The delay in seconds until the next cron job execution (minimum `HMSH_FIDELITY_SECONDS`)
23
+ * @throws {Error} If the cron expression is invalid
26
24
  * @example
27
25
  * ```yaml
28
26
  * cron_next_result:
@@ -32,28 +30,17 @@ class CronHandler {
32
30
  * ```
33
31
  */
34
32
  nextDelay(cronExpression) {
35
- try {
36
- if (!(0, utils_1.isValidCron)(cronExpression)) {
37
- return -1;
38
- }
39
- const interval = (0, cron_parser_1.parseExpression)(cronExpression, { utc: true });
40
- const nextDate = interval.next().toDate();
41
- const now = new Date();
42
- const delay = (nextDate.getTime() - now.getTime()) / 1000;
43
- if (delay <= 0) {
44
- return -1;
45
- }
46
- if (delay < enums_1.HMSH_FIDELITY_SECONDS) {
47
- return enums_1.HMSH_FIDELITY_SECONDS;
48
- }
49
- const iDelay = Math.round(delay);
50
- return iDelay;
33
+ if (!(0, utils_1.isValidCron)(cronExpression)) {
34
+ throw new Error(`Invalid cron expression: ${cronExpression}`);
51
35
  }
52
- catch (error) {
53
- CronHandler.logger.error('Error calculating next cron job execution delay:', { error });
54
- return -1;
36
+ const interval = (0, cron_parser_1.parseExpression)(cronExpression, { utc: true });
37
+ const nextDate = interval.next().toDate();
38
+ const now = new Date();
39
+ const delay = (nextDate.getTime() - now.getTime()) / 1000;
40
+ if (delay <= 0) {
41
+ return enums_1.HMSH_FIDELITY_SECONDS;
55
42
  }
43
+ return Math.max(Math.round(delay), enums_1.HMSH_FIDELITY_SECONDS);
56
44
  }
57
45
  }
58
46
  exports.CronHandler = CronHandler;
59
- CronHandler.logger = new logger_1.LoggerService('hotmesh', 'cron');
@@ -320,11 +320,13 @@ class Virtual {
320
320
  if ((0, utils_1.isValidCron)(params.options.interval)) {
321
321
  //cron syntax
322
322
  cron = params.options.interval;
323
- const nextDelay = new cron_1.CronHandler().nextDelay(cron);
324
- delay = nextDelay > 0 ? nextDelay : undefined;
323
+ delay = new cron_1.CronHandler().nextDelay(cron);
325
324
  }
326
325
  else {
327
326
  const seconds = (0, utils_1.s)(params.options.interval);
327
+ if (isNaN(seconds)) {
328
+ throw new Error(`Invalid cron/interval expression: ${params.options.interval}`);
329
+ }
328
330
  interval = Math.max(seconds, enums_1.HMSH_FIDELITY_SECONDS);
329
331
  delay = params.options.delay ? (0, utils_1.s)(params.options.delay) : undefined;
330
332
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hotmeshio/hotmesh",
3
- "version": "0.14.8",
3
+ "version": "0.14.9",
4
4
  "description": "Durable Workflow",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",