semola 0.6.0 → 0.6.2

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.
Files changed (51) hide show
  1. package/README.md +19 -5
  2. package/dist/index-DISN0WKZ.d.mts +78 -0
  3. package/dist/lib/api/index.cjs +1 -537
  4. package/dist/lib/api/index.d.cts +1 -271
  5. package/dist/lib/api/index.d.mts +100 -164
  6. package/dist/lib/api/index.mjs +1 -535
  7. package/dist/lib/cache/index.cjs +1 -99
  8. package/dist/lib/cache/index.d.cts +1 -27
  9. package/dist/lib/cache/index.mjs +1 -98
  10. package/dist/lib/cli/index.cjs +3 -0
  11. package/dist/lib/cli/index.d.cts +1 -0
  12. package/dist/lib/cli/index.d.mts +90 -0
  13. package/dist/lib/cli/index.mjs +3 -0
  14. package/dist/lib/cron/index.cjs +1 -735
  15. package/dist/lib/cron/index.d.cts +1 -146
  16. package/dist/lib/cron/index.d.mts +99 -36
  17. package/dist/lib/cron/index.mjs +1 -726
  18. package/dist/lib/errors/index.cjs +1 -30
  19. package/dist/lib/errors/index.d.cts +1 -13
  20. package/dist/lib/errors/index.mjs +1 -26
  21. package/dist/lib/i18n/index.cjs +1 -42
  22. package/dist/lib/i18n/index.d.cts +1 -28
  23. package/dist/lib/i18n/index.d.mts +2 -2
  24. package/dist/lib/i18n/index.mjs +1 -41
  25. package/dist/lib/logging/index.cjs +1 -388
  26. package/dist/lib/logging/index.d.cts +1 -108
  27. package/dist/lib/logging/index.mjs +1 -374
  28. package/dist/lib/orm/index.cjs +1 -1642
  29. package/dist/lib/orm/index.d.cts +1 -402
  30. package/dist/lib/orm/index.d.mts +27 -25
  31. package/dist/lib/orm/index.mjs +1 -1630
  32. package/dist/lib/policy/index.cjs +1 -285
  33. package/dist/lib/policy/index.d.cts +1 -77
  34. package/dist/lib/policy/index.d.mts +1 -1
  35. package/dist/lib/policy/index.mjs +1 -265
  36. package/dist/lib/prompts/index.cjs +6 -769
  37. package/dist/lib/prompts/index.d.cts +1 -75
  38. package/dist/lib/prompts/index.mjs +6 -762
  39. package/dist/lib/pubsub/index.cjs +1 -141
  40. package/dist/lib/pubsub/index.d.cts +1 -26
  41. package/dist/lib/pubsub/index.mjs +1 -140
  42. package/dist/lib/queue/index.cjs +1 -212
  43. package/dist/lib/queue/index.d.cts +1 -81
  44. package/dist/lib/queue/index.mjs +1 -209
  45. package/dist/lib/workflow/index.cjs +1 -537
  46. package/dist/lib/workflow/index.d.cts +1 -150
  47. package/dist/lib/workflow/index.d.mts +0 -1
  48. package/dist/lib/workflow/index.mjs +1 -527
  49. package/dist/rolldown-runtime-CMqjfN_6.cjs +1 -0
  50. package/package.json +17 -5
  51. package/dist/chunk-CKQMccvm.cjs +0 -28
@@ -1,735 +1 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_lib_errors_index = require("../errors/index.cjs");
3
- //#region src/lib/cron/builder/index.ts
4
- const CRON_FIELD_ORDER = [
5
- "second",
6
- "minute",
7
- "hour",
8
- "day",
9
- "month",
10
- "weekday"
11
- ];
12
- var FieldWrapper = class {
13
- fields = [];
14
- add(expr) {
15
- this.fields.push(expr);
16
- }
17
- read() {
18
- return this.fields;
19
- }
20
- };
21
- var CronListBuilder = class {
22
- wrapper;
23
- constructor(wrapper) {
24
- this.wrapper = wrapper;
25
- }
26
- any() {
27
- this.wrapper.add({ type: "any" });
28
- return this;
29
- }
30
- range(options) {
31
- this.wrapper.add({
32
- type: "range",
33
- ...options
34
- });
35
- return this;
36
- }
37
- step(options) {
38
- this.wrapper.add({
39
- type: "step",
40
- ...options
41
- });
42
- return this;
43
- }
44
- number(value) {
45
- this.wrapper.add({
46
- type: "value",
47
- value
48
- });
49
- return this;
50
- }
51
- };
52
- function range(options) {
53
- return {
54
- type: "range",
55
- ...options
56
- };
57
- }
58
- function any() {
59
- return { type: "any" };
60
- }
61
- function step(options) {
62
- return {
63
- type: "step",
64
- ...options
65
- };
66
- }
67
- function list(builderFn) {
68
- const wrapper = new FieldWrapper();
69
- builderFn(new CronListBuilder(wrapper));
70
- return {
71
- type: "list",
72
- values: wrapper.read()
73
- };
74
- }
75
- function number(value) {
76
- return {
77
- type: "value",
78
- value
79
- };
80
- }
81
- function cronJobBuilder(buildFn) {
82
- const fields = {
83
- second: void 0,
84
- minute: void 0,
85
- hour: void 0,
86
- day: void 0,
87
- month: void 0,
88
- weekday: void 0
89
- };
90
- const obj = {
91
- second(expr) {
92
- fields.second = checkExpr(expr);
93
- return obj;
94
- },
95
- minute(expr) {
96
- fields.minute = checkExpr(expr);
97
- return obj;
98
- },
99
- hour(expr) {
100
- fields.hour = checkExpr(expr);
101
- return obj;
102
- },
103
- day(expr) {
104
- fields.day = checkExpr(expr);
105
- return obj;
106
- },
107
- month(expr) {
108
- fields.month = checkExpr(expr);
109
- return obj;
110
- },
111
- weekday(expr) {
112
- fields.weekday = checkExpr(expr);
113
- return obj;
114
- }
115
- };
116
- buildFn(obj);
117
- return generate(fields);
118
- }
119
- function checkExpr(expr) {
120
- if (expr.type === "list") {
121
- const { values } = expr;
122
- if (values.length === 0) throw new Error("EmptyListError: List expression cannot be empty");
123
- return values.map((e) => handleSimpleExpression(e)).join(",");
124
- }
125
- return handleSimpleExpression(expr);
126
- }
127
- function handleSimpleExpression(expr) {
128
- switch (expr.type) {
129
- case "any": return "*";
130
- case "value": return `${expr.value}`;
131
- case "range": {
132
- const { min, max } = expr;
133
- if (min > max) throw new Error(`OutOfBoundError: Expected ${min} <= ${max}`);
134
- return `${min}-${max}`;
135
- }
136
- case "step": {
137
- const { step, range } = expr;
138
- if (step === 0) throw new Error(`OutOfBoundError: Expected step value greater than zero`);
139
- if (!range) return `*/${step}`;
140
- const { min, max } = range;
141
- if (max === 0) {
142
- if (min > max) throw new Error(`OutOfBoundError: Expected max value greater than zero`);
143
- return `${min}-${max}/${step}`;
144
- }
145
- if (!max) return `${min}/${step}`;
146
- if (min > max) throw new Error(`OutOfBoundError: Expected ${min} <= ${max}`);
147
- return `${min}-${max}/${step}`;
148
- }
149
- default: return "*";
150
- }
151
- }
152
- function generate(fields) {
153
- const parts = [];
154
- for (let index = 0; index < CRON_FIELD_ORDER.length; index++) {
155
- const key = CRON_FIELD_ORDER[index];
156
- if (!key) return "";
157
- if (key === "second" && !fields[key]) continue;
158
- parts.push(fields[key] ?? "*");
159
- }
160
- return parts.join(" ");
161
- }
162
- //#endregion
163
- //#region src/lib/cron/builder/types.ts
164
- const WeekDay = {
165
- sun: 0,
166
- mon: 1,
167
- tue: 2,
168
- wed: 3,
169
- thu: 4,
170
- fri: 5,
171
- sat: 6
172
- };
173
- const Month = {
174
- jan: 1,
175
- feb: 2,
176
- mar: 3,
177
- apr: 4,
178
- may: 5,
179
- jun: 6,
180
- jul: 7,
181
- aug: 8,
182
- sep: 9,
183
- oct: 10,
184
- nov: 11,
185
- dec: 12
186
- };
187
- //#endregion
188
- //#region src/lib/cron/errors.ts
189
- var InvalidValueError = class extends Error {
190
- constructor(message) {
191
- super(message);
192
- this.name = "InvalidValueError";
193
- }
194
- };
195
- var OutOfBoundError = class extends Error {
196
- constructor(message) {
197
- super(message);
198
- this.name = "OutOfBoundError";
199
- }
200
- };
201
- var CronExpressionError = class extends Error {
202
- constructor(message) {
203
- super(message);
204
- this.name = "CronExpressionError";
205
- }
206
- };
207
- var EmptyCronExpressionError = class extends Error {
208
- constructor(message) {
209
- super(message);
210
- this.name = "EmptyCronExpressionError";
211
- }
212
- };
213
- var CronLengthError = class extends Error {
214
- constructor(message) {
215
- super(message);
216
- this.name = "CronLengthError";
217
- }
218
- };
219
- //#endregion
220
- //#region src/lib/cron/core/scanner.ts
221
- const FieldAmount = {
222
- min: 5,
223
- max: 6
224
- };
225
- var Token = class {
226
- type;
227
- component;
228
- value;
229
- field;
230
- constructor(component, type, value, field) {
231
- this.component = component;
232
- this.type = type;
233
- this.value = value;
234
- this.field = field;
235
- }
236
- getComponent() {
237
- return this.component;
238
- }
239
- getTokenType() {
240
- return this.type;
241
- }
242
- getTokenValue() {
243
- return this.value;
244
- }
245
- getField() {
246
- return this.field;
247
- }
248
- toString() {
249
- return `Token{${`component="${this.component}", type=${this.type}`}, ${`field="${this.field}", value=${this.value}`}}`;
250
- }
251
- equals(other) {
252
- if (!other) return false;
253
- const isComponentEqual = this.component === other.getComponent();
254
- const isTokenTypeEqual = this.type === other.getTokenType();
255
- const isTokenValueEqual = this.value === other.getTokenValue();
256
- const isFieldEqual = this.field === other.getField();
257
- return isComponentEqual && isTokenTypeEqual && isTokenValueEqual && isFieldEqual;
258
- }
259
- };
260
- var Scanner = class {
261
- expression;
262
- current;
263
- start;
264
- tokens;
265
- constructor(expression) {
266
- this.expression = expression;
267
- this.current = 0;
268
- this.start = 0;
269
- this.tokens = [];
270
- }
271
- scan() {
272
- if (this.expression.length === 0) throw new EmptyCronExpressionError("Cron expression have zero length");
273
- const fields = this.expression.trim().split(/\s+/);
274
- const hasMinLen = fields.length === FieldAmount.min;
275
- const hasMaxLen = fields.length === FieldAmount.max;
276
- if (!hasMinLen && !hasMaxLen) throw new CronLengthError(`Invalid number of fields for '${this.expression}'. Expected 5 or 6 fields but got ${fields.length} field(s)`);
277
- const components = this.createComponent(fields);
278
- for (let idx = 0; idx < components.length; idx++) {
279
- const component = components[idx];
280
- if (!component) throw new CronExpressionError(`Invalid cron expression: ${this.expression}`);
281
- this.current = 0;
282
- this.start = 0;
283
- this.scanComponent(component);
284
- }
285
- return this.tokens;
286
- }
287
- scanComponent(component) {
288
- const { field, content } = component;
289
- while (this.current < content.length) {
290
- let currentCh = this.advance(content);
291
- switch (currentCh) {
292
- case "*": {
293
- const ch = this.peek(content);
294
- if (this.match(content, "/")) this.handleStep(component);
295
- else if (!ch || ch === ",") this.addToken("*", "any", "*", field);
296
- else throw new CronExpressionError(`Invalid any expression '${content}' for field '${field}'`);
297
- break;
298
- }
299
- case "-":
300
- currentCh = this.advance(content);
301
- if (this.isDigit(currentCh)) this.handleRangeWithStep(component);
302
- else throw new CronExpressionError(`Invalid range expression '${content}' for field '${field}'`);
303
- break;
304
- case ",": {
305
- if (this.current === 1 && this.start === 0) throw new CronExpressionError(`Invalid list expression '${content}' for field '${field}'`);
306
- const next = this.peek(content);
307
- if (!next || next === ",") throw new CronExpressionError(`Invalid list expression '${content}' for field '${field}'`);
308
- break;
309
- }
310
- default:
311
- if (this.isDigit(currentCh)) this.handleNumber(component);
312
- else throw new CronExpressionError(`Invalid cron expression '${this.expression}' in field '${field}'`);
313
- break;
314
- }
315
- this.start = this.current;
316
- }
317
- return true;
318
- }
319
- addToken(component, type, value, field) {
320
- const token = new Token(component, type, value, field);
321
- this.tokens.push(token);
322
- }
323
- advance(content) {
324
- const currentCh = content.charAt(this.current);
325
- this.current += 1;
326
- return currentCh;
327
- }
328
- match(content, expected) {
329
- if (this.current >= content.length) return false;
330
- if (content.charAt(this.current) !== expected) return false;
331
- this.current += 1;
332
- return true;
333
- }
334
- peek(content) {
335
- if (this.current >= content.length) return void 0;
336
- return content.charAt(this.current);
337
- }
338
- handleStep(component) {
339
- const { field, content } = component;
340
- let ch = this.peek(content);
341
- const slashIdx = this.current - 1;
342
- while (ch && this.isDigit(ch)) {
343
- this.advance(content);
344
- ch = this.peek(content);
345
- }
346
- if (ch && ch !== ",") throw new CronExpressionError(`Invalid step expression '${content}' for field '${field}'`);
347
- const tokenContent = content.substring(this.start, this.current);
348
- const value = content.slice(slashIdx + 1, this.current);
349
- if (value.length === 0) throw new CronExpressionError(`Invalid step expression '${content}' for field '${field}'`);
350
- this.addToken(tokenContent, "step", Number(value), field);
351
- return true;
352
- }
353
- handleRangeWithStep(component) {
354
- const { field, content } = component;
355
- let ch = this.peek(content);
356
- while (ch && this.isDigit(ch)) {
357
- this.advance(content);
358
- ch = this.peek(content);
359
- }
360
- if (!ch) throw new CronExpressionError(`Invalid range expression '${content}' for field '${field}'`);
361
- if (this.match(content, "/")) {
362
- this.handleStep(component);
363
- return true;
364
- }
365
- throw new CronExpressionError(`Invalid range expression '${content}' for field '${field}'`);
366
- }
367
- handleNumber(component) {
368
- const { field, content } = component;
369
- let ch = this.peek(content);
370
- this.start = this.current - 1;
371
- while (ch && this.isDigit(ch)) {
372
- this.advance(content);
373
- ch = this.peek(content);
374
- }
375
- if (!ch) {
376
- const item = content.substring(this.start);
377
- this.addToken(item, "number", Number(item), field);
378
- return true;
379
- }
380
- if (this.match(content, "-")) {
381
- this.handleRange(component);
382
- return true;
383
- }
384
- if (this.match(content, "/")) {
385
- this.handleStep(component);
386
- return true;
387
- }
388
- if (!this.isDigit(ch) && ch !== ",") throw new CronExpressionError(`Invalid number '${content}' for field '${field}'`);
389
- const item = content.substring(this.start, this.current);
390
- this.addToken(item, "number", Number(item), field);
391
- return true;
392
- }
393
- handleRange(component) {
394
- const { field, content } = component;
395
- let ch = this.peek(content);
396
- if (!ch) throw new CronExpressionError(`Invalid range expression '${content}' for field '${field}'`);
397
- while (ch && this.isDigit(ch)) {
398
- this.advance(content);
399
- ch = this.peek(content);
400
- }
401
- if (!ch) {
402
- const tokenContent = content.substring(this.start);
403
- this.addToken(tokenContent, "range", tokenContent, field);
404
- return true;
405
- }
406
- if (this.match(content, "/")) {
407
- this.handleStep(component);
408
- return true;
409
- }
410
- if (ch && ch !== ",") throw new CronExpressionError(`Invalid range expression '${content}' for field '${field}'`);
411
- const tokenContent = content.substring(this.start, this.current);
412
- this.addToken(tokenContent, "range", tokenContent, field);
413
- return true;
414
- }
415
- isDigit(ch) {
416
- return ch >= "0" && ch <= "9";
417
- }
418
- createComponent(fields) {
419
- const fieldNames = [
420
- "second",
421
- "minute",
422
- "hour",
423
- "day",
424
- "month",
425
- "weekday"
426
- ];
427
- const components = [];
428
- let offset = 1;
429
- if (fields.length === FieldAmount.max) offset = 0;
430
- for (let idx = 0; idx < fields.length; idx++) {
431
- const fieldName = fieldNames[idx + offset];
432
- const content = fields[idx];
433
- if (!fieldName || !content) break;
434
- components.push({
435
- content,
436
- field: fieldName
437
- });
438
- }
439
- return components;
440
- }
441
- };
442
- //#endregion
443
- //#region src/lib/cron/core/index.ts
444
- const RETRY_DELAY_MS = 3600 * 1e3;
445
- const MAX_YEARS = 4;
446
- const ALIASES = {
447
- "@yearly": "0 0 1 1 *",
448
- "@monthly": "0 0 1 * *",
449
- "@weekly": "0 0 * * 0",
450
- "@daily": "0 0 * * *",
451
- "@hourly": "0 * * * *",
452
- "@minutely": "* * * * *"
453
- };
454
- const CronSecondRange = {
455
- min: 0,
456
- max: 59
457
- };
458
- const CronMinuteRange = {
459
- min: 0,
460
- max: 59
461
- };
462
- const CronHourRange = {
463
- min: 0,
464
- max: 23
465
- };
466
- const CronDayRange = {
467
- min: 1,
468
- max: 31
469
- };
470
- const CronMonthRange = {
471
- min: 1,
472
- max: 12
473
- };
474
- const CronDayOfWeekRange = {
475
- min: 0,
476
- max: 6
477
- };
478
- var Cron = class {
479
- options;
480
- status = "idle";
481
- timeoutId = null;
482
- second = Array(CronSecondRange.max + 1).fill(0);
483
- minute = Array(CronMinuteRange.max + 1).fill(0);
484
- hour = Array(CronHourRange.max + 1).fill(0);
485
- day = Array(CronDayRange.max + 1).fill(0);
486
- month = Array(CronMonthRange.max + 1).fill(0);
487
- dayOfWeek = Array(CronDayOfWeekRange.max + 1).fill(0);
488
- hasSeconds;
489
- _dayWildcard = false;
490
- _dowWildcard = false;
491
- fillRange(values, min, max) {
492
- for (let i = min; i <= max; i++) values[i] = 1;
493
- }
494
- handleStep(part, values, min, max) {
495
- const [rangePart, stepStr] = part.split("/");
496
- if (!rangePart) throw new InvalidValueError(`'${rangePart}' is empty`);
497
- if (!stepStr) throw new InvalidValueError(`'${stepStr}' is empty`);
498
- const step = Number(stepStr);
499
- if (!Number.isInteger(step)) throw new InvalidValueError(`'${step}' is not a valid number`);
500
- if (step <= 0) throw new OutOfBoundError(`Expected ${step} > 0`);
501
- if (rangePart === "*") {
502
- for (let i = min; i <= max; i += step) values[i] = 1;
503
- return;
504
- }
505
- if (rangePart.includes("-")) {
506
- this.handleStepRange(rangePart, step, values, min, max);
507
- return;
508
- }
509
- this.handleStepSingle(rangePart, step, values, min, max);
510
- }
511
- handleStepRange(range, step, values, min, max) {
512
- const [startStr, endStr] = range.split("-");
513
- if (!endStr) throw new InvalidValueError(`'${endStr}' is empty`);
514
- let start = min;
515
- if (startStr && startStr.length > 0) start = Number(startStr);
516
- const end = Number(endStr);
517
- if (!Number.isInteger(start)) throw new InvalidValueError(`'${start}' is not a valid number`);
518
- if (!Number.isInteger(end)) throw new InvalidValueError(`'${end}' is not a valid number`);
519
- if (start < min) throw new OutOfBoundError(`Expected ${start} >= ${min}`);
520
- if (end > max) throw new OutOfBoundError(`Expected ${end} <= ${max}`);
521
- if (start > end) throw new OutOfBoundError(`Expected ${start} <= ${end}`);
522
- for (let i = start; i <= end; i += step) values[i] = 1;
523
- }
524
- handleStepSingle(value, step, values, min, max) {
525
- const start = Number(value);
526
- if (!Number.isInteger(start)) throw new InvalidValueError(`'${start}' is not a valid number`);
527
- if (start < min) throw new OutOfBoundError(`Expected ${start} >= ${min}`);
528
- if (start > max) throw new OutOfBoundError(`Expected ${start} <= ${max}`);
529
- for (let i = start; i <= max; i += step) values[i] = 1;
530
- }
531
- handleRange(part, values, min, max) {
532
- const [startStr, endStr] = part.split("-");
533
- if (!startStr) throw new InvalidValueError(`'${startStr}' is empty`);
534
- if (!endStr) throw new InvalidValueError(`'${endStr}' is empty`);
535
- const start = Number(startStr);
536
- const end = Number(endStr);
537
- if (!Number.isInteger(start)) throw new InvalidValueError(`'${start}' is not a valid number`);
538
- if (!Number.isInteger(end)) throw new InvalidValueError(`'${end}' is not a valid number`);
539
- if (start < min) throw new OutOfBoundError(`Expected ${start} >= ${min}`);
540
- if (end > max) throw new OutOfBoundError(`Expected ${end} <= ${max}`);
541
- if (start > end) throw new OutOfBoundError(`Expected ${start} <= ${end}`);
542
- for (let i = start; i <= end; i++) values[i] = 1;
543
- }
544
- handleNumber(value, values, min, max) {
545
- const n = Number(value);
546
- if (!Number.isInteger(n)) throw new InvalidValueError(`'${value}' is not a valid number`);
547
- if (n < min) throw new OutOfBoundError(`Expected ${n} >= ${min}`);
548
- if (n > max) throw new OutOfBoundError(`Expected ${n} <= ${max}`);
549
- values[n] = 1;
550
- }
551
- constructor(options) {
552
- this.options = options;
553
- const expr = this.resolveAlias(options.schedule);
554
- const tokens = new Scanner(expr).scan();
555
- this.hasSeconds = expr.trim().split(/\s+/).length === FieldAmount.max;
556
- this.parse(tokens);
557
- }
558
- resolveAlias(schedule) {
559
- return ALIASES[schedule] || schedule;
560
- }
561
- parse(tokens) {
562
- for (let i = 0; i < tokens.length; i++) {
563
- const token = tokens[i];
564
- if (!token) throw new InvalidValueError("Undefined token");
565
- const tokenType = token.getTokenType();
566
- switch (token.getField()) {
567
- case "second":
568
- this.handleField(token, this.second, CronSecondRange.min, CronSecondRange.max);
569
- break;
570
- case "minute":
571
- this.handleField(token, this.minute, CronMinuteRange.min, CronMinuteRange.max);
572
- break;
573
- case "hour":
574
- this.handleField(token, this.hour, CronHourRange.min, CronHourRange.max);
575
- break;
576
- case "day":
577
- if (tokenType === "any") this._dayWildcard = true;
578
- this.handleField(token, this.day, CronDayRange.min, CronDayRange.max);
579
- break;
580
- case "month":
581
- this.handleField(token, this.month, CronMonthRange.min, CronMonthRange.max);
582
- break;
583
- case "weekday":
584
- if (tokenType === "any") this._dowWildcard = true;
585
- this.handleField(token, this.dayOfWeek, CronDayOfWeekRange.min, CronDayOfWeekRange.max);
586
- break;
587
- default: throw new InvalidValueError(`Invalid field '${token.getField()}'`);
588
- }
589
- }
590
- }
591
- handleField(token, field, min, max) {
592
- switch (token.getTokenType()) {
593
- case "any":
594
- this.fillRange(field, min, max);
595
- break;
596
- case "number":
597
- this.handleNumber(token.getComponent(), field, min, max);
598
- break;
599
- case "range": {
600
- const component = token.getComponent();
601
- this.handleRange(component, field, min, max);
602
- break;
603
- }
604
- case "step": {
605
- const component = token.getComponent();
606
- this.handleStep(component, field, min, max);
607
- break;
608
- }
609
- default: throw new InvalidValueError(`Invalid token type '${token.getTokenType()}'`);
610
- }
611
- }
612
- matches(date) {
613
- const s = date.getSeconds();
614
- const m = date.getMinutes();
615
- const h = date.getHours();
616
- const d = date.getDate();
617
- const mon = date.getMonth();
618
- const dow = date.getDay();
619
- const isSecondMatch = this.hasSeconds ? this.second[s] === 1 : true;
620
- const isMinuteMatch = this.minute[m] === 1;
621
- const isHourMatch = this.hour[h] === 1;
622
- const isMonthMatch = this.month[mon + 1] === 1;
623
- let isDayOrDowMatch;
624
- if (!this._dayWildcard && !this._dowWildcard) isDayOrDowMatch = this.day[d] === 1 || this.dayOfWeek[dow] === 1;
625
- else isDayOrDowMatch = this.day[d] === 1 && this.dayOfWeek[dow] === 1;
626
- return isSecondMatch && isMinuteMatch && isHourMatch && isDayOrDowMatch && isMonthMatch;
627
- }
628
- getNextRun() {
629
- const date = /* @__PURE__ */ new Date();
630
- if (this.hasSeconds) {
631
- date.setMilliseconds(0);
632
- date.setSeconds(date.getSeconds() + 1);
633
- } else {
634
- date.setSeconds(0, 0);
635
- date.setMinutes(date.getMinutes() + 1);
636
- }
637
- const deadline = new Date(date);
638
- deadline.setFullYear(deadline.getFullYear() + MAX_YEARS);
639
- while (date < deadline) {
640
- const s = date.getSeconds();
641
- const m = date.getMinutes();
642
- const h = date.getHours();
643
- const d = date.getDate();
644
- const mon = date.getMonth();
645
- const dow = date.getDay();
646
- if (this.month[mon + 1] === 0) {
647
- date.setDate(1);
648
- date.setMonth(mon + 1);
649
- date.setHours(0, 0, 0, 0);
650
- continue;
651
- }
652
- let isDayOrDowMatch;
653
- if (!this._dayWildcard && !this._dowWildcard) isDayOrDowMatch = this.day[d] === 0 && this.dayOfWeek[dow] === 0;
654
- else isDayOrDowMatch = this.day[d] === 0 || this.dayOfWeek[dow] === 0;
655
- if (isDayOrDowMatch) {
656
- date.setDate(d + 1);
657
- date.setHours(0, 0, 0, 0);
658
- continue;
659
- }
660
- if (this.hour[h] === 0) {
661
- date.setHours(h + 1);
662
- date.setMinutes(0, 0, 0);
663
- continue;
664
- }
665
- if (this.minute[m] === 0) {
666
- date.setMinutes(m + 1);
667
- date.setSeconds(0, 0);
668
- continue;
669
- }
670
- if (this.hasSeconds && this.second[s] === 0) {
671
- date.setSeconds(s + 1);
672
- date.setMilliseconds(0);
673
- continue;
674
- }
675
- return new Date(date);
676
- }
677
- return null;
678
- }
679
- start() {
680
- if (this.status !== "idle") return;
681
- this.status = "running";
682
- this.next();
683
- }
684
- stop() {
685
- this.status = "idle";
686
- if (this.timeoutId) {
687
- clearTimeout(this.timeoutId);
688
- this.timeoutId = null;
689
- }
690
- }
691
- pause() {
692
- if (this.status !== "running") return;
693
- this.status = "paused";
694
- if (this.timeoutId) {
695
- clearTimeout(this.timeoutId);
696
- this.timeoutId = null;
697
- }
698
- }
699
- resume() {
700
- if (this.status !== "paused") return;
701
- this.status = "running";
702
- this.next();
703
- }
704
- getStatus() {
705
- return this.status;
706
- }
707
- next() {
708
- if (this.status !== "running") return;
709
- const nextRun = this.getNextRun();
710
- if (!nextRun) {
711
- this.timeoutId = setTimeout(() => this.next(), RETRY_DELAY_MS);
712
- return;
713
- }
714
- const delay = nextRun.getTime() - Date.now();
715
- this.timeoutId = setTimeout(() => {
716
- this.run();
717
- }, Math.max(0, delay));
718
- }
719
- async run() {
720
- if (this.status !== "running") return;
721
- const handlerResult = this.options.handler();
722
- await require_lib_errors_index.mightThrow(Promise.resolve(handlerResult));
723
- if (this.status === "running") this.next();
724
- }
725
- };
726
- //#endregion
727
- exports.Cron = Cron;
728
- exports.Month = Month;
729
- exports.WeekDay = WeekDay;
730
- exports.any = any;
731
- exports.cronJobBuilder = cronJobBuilder;
732
- exports.list = list;
733
- exports.number = number;
734
- exports.range = range;
735
- exports.step = step;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../errors/index.cjs");var t=class extends Error{constructor(e){super(e),this.name=`OutOfBoundError`}},n=class extends TypeError{constructor(e){super(e),this.name=`InvalidRetryError`}};const r=[`minute`,`hour`,`day`,`month`,`weekday`];var i=class{fields=[];add(e){this.fields.push(e)}read(){return this.fields}},a=class{wrapper;constructor(e){this.wrapper=e}any(){return this.wrapper.add({type:`any`}),this}range(e){return this.wrapper.add({type:`range`,...e}),this}step(e){return this.wrapper.add({type:`step`,...e}),this}number(e){return this.wrapper.add({type:`value`,value:e}),this}};function o(e){return{type:`range`,...e}}function s(){return{type:`any`}}function c(e){return{type:`step`,...e}}function l(e){let t=new i;return e(new a(t)),{type:`list`,values:t.read()}}function u(e){return{type:`value`,value:e}}function d(e){let t={minute:void 0,hour:void 0,day:void 0,month:void 0,weekday:void 0},n={minute(e){return t.minute=f(e),n},hour(e){return t.hour=f(e),n},day(e){return t.day=f(e),n},month(e){return t.month=f(e),n},weekday(e){return t.weekday=f(e),n}};return e(n),m(t)}function f(e){if(e.type===`list`){let{values:t}=e;if(t.length===0)throw Error(`EmptyListError: List expression cannot be empty`);return t.map(e=>p(e)).join(`,`)}return p(e)}function p(e){switch(e.type){case`any`:return`*`;case`value`:return`${e.value}`;case`range`:{let{min:n,max:r}=e;if(n>r)throw new t(`Expected ${n} <= ${r}`);return`${n}-${r}`}case`step`:{let{step:n,range:r}=e;if(n===0)throw new t(`Expected step value greater than zero`);if(!r)return`*/${n}`;let{min:i,max:a}=r;if(a===0){if(i>a)throw new t(`Expected max value greater than zero`);return`${i}-${a}/${n}`}if(!a)return`${i}/${n}`;if(i>a)throw new t(`Expected ${i} <= ${a}`);return`${i}-${a}/${n}`}default:return`*`}}function m(e){let t=[];for(let n=0;n<r.length;n++){let i=r[n];if(!i)return``;t.push(e[i]??`*`)}return t.join(` `)}const h={sun:0,mon:1,tue:2,wed:3,thu:4,fri:5,sat:6},g={jan:1,feb:2,mar:3,apr:4,may:5,jun:6,jul:7,aug:8,sep:9,oct:10,nov:11,dec:12};var _=class{constructor(){}};const v={"@yearly":`0 0 1 1 *`,"@annually":`0 0 1 1 *`,"@monthly":`0 0 1 * *`,"@weekly":`0 0 * * 0`,"@daily":`0 0 * * *`,"@midnight":`0 0 * * *`,"@hourly":`0 * * * *`,"@minutely":`* * * * *`};var y=class{getExpression(e){return v[e]||e}getJobName(e){return e.name}next(t,n){let{schedule:r}=t,i=this.getExpression(r),[a,o]=e.mightThrowSync(()=>Bun.cron.parse(i,n));if(a)throw a;return o}},b=class{options;jobs=new Map;constructor(e){if(this.options=e,!this.checkAttempts())throw new n(`Expected 'maxAttempts' to be a finite non-negative integer`)}async update(e){if(e.type===`add`){this.jobs.set(e.name,0);return}let t=this.jobs.get(e.name);if(t===void 0)return;if(e.type===`success`){this.jobs.set(e.name,0);return}let{job:n,error:r,name:i}=e,{maxAttempts:a}=this.options,o=this.runOnRetryError(r,i);if(t<a&&o){let n=this.calculateDelay(t);if(this.options.onFailedAttempt){let e={attemptNumber:t+1,delay:n,error:r,retriesLeft:a-t,jobName:i};await this.options.onFailedAttempt(e)}this.jobs.set(e.name,t+1),await this.runDelay(n);return}if(n.stop(),!this.options.onError)throw r;let s={name:i,error:r,failedAt:Date.now()};await this.options.onError(s)}checkAttempts(){let{maxAttempts:e}=this.options;return e>=0&&Number.isSafeInteger(e)&&!Object.is(e,-0)}runOnRetryError(e,t){return!this.options.retryOnError||this.options.retryOnError({error:e,jobName:t})}async runDelay(e){await new Promise(t=>setTimeout(t,e))}calculateDelay(e){let t=1e3*2**e;return Math.round(Math.random()*(Math.min(t,6e4)+1))}},x=class{listener=null;subscribe(e){this.listener=e}unsubscribe(){this.listener=null}async notify(e){this.listener&&await this.listener.update(e)}},S=class extends _{options;status;cron=null;manager;common;constructor(e){super(),this.options=e,this.status=`idle`,this.common=new y,this.options.retry&&(this.manager=new x,this.manager.subscribe(this.options.retry),this.manager.notify({type:`add`,name:this.getJobName()}))}[Symbol.dispose](){this.stop()}getStatus(){return this.status}run(){if(this.status===`running`)return;let{schedule:t,handler:n}=this.options,[r,i]=e.mightThrowSync(()=>{let r=this.common.getExpression(t);return Bun.cron(r,async()=>{let[t]=await e.mightThrow(Promise.resolve().then(()=>n()));if(!t)return this.manager&&await this.manager.notify({type:`success`,name:this.getJobName()}),Promise.resolve();if(this.manager){let e={type:`error`,error:t,job:this,name:this.getJobName()};await this.manager.notify(e);return}await Promise.reject(t)})});if(!r){this.status=`running`,this.cron=i;return}throw r}stop(){this.status===`running`&&this.cron&&(this.cron.stop(),this.status=`idle`)}ref(){this.status===`running`&&this.cron&&this.cron.ref()}unref(){this.status===`running`&&this.cron&&this.cron.unref()}getExpression(){return this.common.getExpression(this.options.schedule)}getJobName(){return this.common.getJobName(this.options)}next(e){return this.common.next(this.options,e)}},C=class{options;common;constructor(e){this.options=e,this.common=new y}async run(){let{path:e,schedule:t,name:n}=this.options,r=this.common.getExpression(t);await Bun.cron(e,r,n)}async stop(){await Bun.cron.remove(this.options.name)}getExpression(){return this.common.getExpression(this.options.schedule)}getJobName(){return this.common.getJobName(this.options)}next(e){return this.common.next(this.options,e)}};exports.Cron=S,exports.CronOS=C,exports.InvalidRetryError=n,exports.Month=g,exports.OutOfBoundError=t,exports.RetryCronJob=b,exports.WeekDay=h,exports.any=s,exports.cronJobBuilder=d,exports.list=l,exports.number=u,exports.range=o,exports.step=c;