@guren/server 2.10.0 → 2.11.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.
Files changed (91) hide show
  1. package/dist/DocsViewerServiceProvider-qFCaeiUv.js +77 -0
  2. package/dist/Hash-CzrwKeDp.js +133 -0
  3. package/dist/HttpException-BkS3euNG.js +161 -0
  4. package/dist/Job-BVT813DO.js +441 -0
  5. package/dist/McpServiceProvider-BoAN-UcD.js +367 -0
  6. package/dist/MessageSigner-Bd25ZJAV.js +93 -0
  7. package/dist/Random-DbIJ22KQ.js +191 -0
  8. package/dist/address-D0Iq-XIH.js +20 -0
  9. package/dist/auth/index.js +2 -122
  10. package/dist/auth-C7sVmYgc.js +1954 -0
  11. package/dist/authorization/index.js +2 -38
  12. package/dist/authorization-Dbcuc5ZK.js +610 -0
  13. package/dist/broadcasting/index.js +2 -26
  14. package/dist/broadcasting-Dw-FhyjN.js +1055 -0
  15. package/dist/cache/index.js +2 -17
  16. package/dist/cache/types.d.ts +4 -4
  17. package/dist/cache-BYnZf2FR.js +812 -0
  18. package/dist/client-Dd7vLcqp.js +27 -0
  19. package/dist/console/Output.d.ts +18 -18
  20. package/dist/encryption/index.js +5 -96
  21. package/dist/encryption-fOWQaKqX.js +250 -0
  22. package/dist/endpoint-D-20PiHY.js +46 -0
  23. package/dist/endpoint-D6GIqOpe.js +31 -0
  24. package/dist/errors-quJOkkCn.js +81 -0
  25. package/dist/events/index.js +2 -28
  26. package/dist/events-aIH_bWRx.js +410 -0
  27. package/dist/health/index.js +2 -24
  28. package/dist/health-VWdWXRwh.js +345 -0
  29. package/dist/hot-disposables-WsTX2ySN.js +281 -0
  30. package/dist/hot-reload/testing.d.ts +1 -1
  31. package/dist/http/resources/Resource.d.ts +23 -3
  32. package/dist/i18n/index.js +2 -30
  33. package/dist/i18n-mcWi7LCd.js +617 -0
  34. package/dist/index.js +7515 -7276
  35. package/dist/lambda/index.js +114 -71
  36. package/dist/logging/index.js +2 -24
  37. package/dist/logging-CPaRMuPP.js +518 -0
  38. package/dist/loopback-guard-D0Kc7-xc.js +224 -0
  39. package/dist/mail/index.js +2 -27
  40. package/dist/mail-DfEfSQEr.js +699 -0
  41. package/dist/mcp/index.js +2 -10
  42. package/dist/notifications/index.js +2 -33
  43. package/dist/notifications-BGqmUNZ8.js +759 -0
  44. package/dist/queue/index.js +3 -42
  45. package/dist/queue-CXrRK85G.js +948 -0
  46. package/dist/redis/index.js +672 -642
  47. package/dist/rolldown-runtime-8H4AJuhK.js +14 -0
  48. package/dist/runtime/index.js +472 -616
  49. package/dist/scheduling/index.js +2 -29
  50. package/dist/scheduling-CPiEdtQd.js +839 -0
  51. package/dist/storage/index.js +2 -15
  52. package/dist/storage-tWHMv2H_.js +841 -0
  53. package/dist/support/expiry.js +69 -12
  54. package/dist/trim-slashes-ZJ_VsY5s.js +25 -0
  55. package/dist/vite/index.js +118 -181
  56. package/dist/vite/plugin.d.ts +1 -1
  57. package/dist/vite-dev-server-BSJkduhZ.js +131 -0
  58. package/package.json +6 -5
  59. package/dist/DocsViewerServiceProvider-RUYDFBMW.js +0 -75
  60. package/dist/McpServiceProvider-SL7DUZ4G.js +0 -8
  61. package/dist/chunk-2SC566CD.js +0 -376
  62. package/dist/chunk-2V3YID4H.js +0 -20
  63. package/dist/chunk-3KV5X2FL.js +0 -31
  64. package/dist/chunk-63SKW5PS.js +0 -457
  65. package/dist/chunk-6J6B5KOB.js +0 -425
  66. package/dist/chunk-A5JJ7XTH.js +0 -24
  67. package/dist/chunk-AZOYI43J.js +0 -18
  68. package/dist/chunk-B5E5VBDI.js +0 -115
  69. package/dist/chunk-DAQKYKLH.js +0 -182
  70. package/dist/chunk-DRNPVBAA.js +0 -1002
  71. package/dist/chunk-EI5GYMAT.js +0 -853
  72. package/dist/chunk-H3B23O6T.js +0 -271
  73. package/dist/chunk-HPET6XY5.js +0 -853
  74. package/dist/chunk-HR2KZ3WL.js +0 -616
  75. package/dist/chunk-IOTWFHZU.js +0 -558
  76. package/dist/chunk-K6CUBJX6.js +0 -642
  77. package/dist/chunk-KTNJG7R7.js +0 -911
  78. package/dist/chunk-LAJQCSSF.js +0 -355
  79. package/dist/chunk-LHR6OHI7.js +0 -189
  80. package/dist/chunk-N7PLD7MB.js +0 -729
  81. package/dist/chunk-Q2P6YLRR.js +0 -24
  82. package/dist/chunk-QQKTH5KX.js +0 -114
  83. package/dist/chunk-SFQP6O52.js +0 -126
  84. package/dist/chunk-SQR7277M.js +0 -911
  85. package/dist/chunk-T2FFUU6J.js +0 -484
  86. package/dist/chunk-TVV5YNEC.js +0 -1842
  87. package/dist/chunk-UY3AZSYL.js +0 -14
  88. package/dist/chunk-V4DNC2ER.js +0 -114
  89. package/dist/chunk-VSA3QY5Q.js +0 -274
  90. package/dist/chunk-YSOJKAAU.js +0 -427
  91. package/dist/errors-PUNHAFL2.js +0 -34
@@ -0,0 +1,839 @@
1
+ import { n as isHotReloadRuntime, t as claimHotDisposable } from "./hot-disposables-WsTX2ySN.js";
2
+ //#region src/scheduling/CronParser.ts
3
+ /**
4
+ * Parse a cron field value.
5
+ */
6
+ function parseField(field, min, max) {
7
+ const values = /* @__PURE__ */ new Set();
8
+ const parts = field.split(",");
9
+ for (const part of parts) {
10
+ const stepMatch = part.match(/^(.+)\/(\d+)$/);
11
+ let range;
12
+ let step = 1;
13
+ if (stepMatch) {
14
+ range = stepMatch[1];
15
+ step = parseInt(stepMatch[2], 10);
16
+ } else range = part;
17
+ if (range === "*") {
18
+ for (let i = min; i <= max; i += step) values.add(i);
19
+ continue;
20
+ }
21
+ const rangeMatch = range.match(/^(\d+)-(\d+)$/);
22
+ if (rangeMatch) {
23
+ const start = parseInt(rangeMatch[1], 10);
24
+ const end = parseInt(rangeMatch[2], 10);
25
+ for (let i = start; i <= end; i += step) if (i >= min && i <= max) values.add(i);
26
+ continue;
27
+ }
28
+ const value = parseInt(range, 10);
29
+ if (!isNaN(value) && value >= min && value <= max) values.add(value);
30
+ }
31
+ return Array.from(values).sort((a, b) => a - b);
32
+ }
33
+ /**
34
+ * Parse a cron expression.
35
+ *
36
+ * Cron format: minute hour dayOfMonth month dayOfWeek
37
+ *
38
+ * Examples:
39
+ * - Every minute: "* * * * *"
40
+ * - Every 5 minutes: "*​/5 * * * *" (step value)
41
+ * - At 3:00 AM daily: "0 3 * * *"
42
+ * - Every Monday at 9:00 AM: "0 9 * * 1"
43
+ */
44
+ function parseCron(expression) {
45
+ const parts = expression.trim().split(/\s+/);
46
+ if (parts.length !== 5) throw new Error(`Invalid cron expression: ${expression}. Expected 5 fields.`);
47
+ return {
48
+ minute: parseField(parts[0], 0, 59),
49
+ hour: parseField(parts[1], 0, 23),
50
+ dayOfMonth: parseField(parts[2], 1, 31),
51
+ month: parseField(parts[3], 1, 12),
52
+ dayOfWeek: parseField(parts[4], 0, 6)
53
+ };
54
+ }
55
+ /**
56
+ * Check if a date matches a parsed cron expression.
57
+ */
58
+ function matchesCron(date, cron) {
59
+ const minute = date.getMinutes();
60
+ const hour = date.getHours();
61
+ const dayOfMonth = date.getDate();
62
+ const month = date.getMonth() + 1;
63
+ const dayOfWeek = date.getDay();
64
+ return cron.minute.includes(minute) && cron.hour.includes(hour) && cron.dayOfMonth.includes(dayOfMonth) && cron.month.includes(month) && cron.dayOfWeek.includes(dayOfWeek);
65
+ }
66
+ /**
67
+ * Get the next occurrence of a cron expression.
68
+ */
69
+ function getNextOccurrence(expression, from = /* @__PURE__ */ new Date()) {
70
+ const cron = parseCron(expression);
71
+ const next = new Date(from);
72
+ next.setSeconds(0);
73
+ next.setMilliseconds(0);
74
+ next.setMinutes(next.getMinutes() + 1);
75
+ const maxIterations = 525600;
76
+ let iterations = 0;
77
+ while (iterations < maxIterations) {
78
+ if (matchesCron(next, cron)) return next;
79
+ next.setMinutes(next.getMinutes() + 1);
80
+ iterations++;
81
+ }
82
+ throw new Error(`Could not find next occurrence for: ${expression}`);
83
+ }
84
+ /**
85
+ * Check if a cron expression is due at the given date.
86
+ */
87
+ function isDue(expression, date = /* @__PURE__ */ new Date()) {
88
+ return matchesCron(date, parseCron(expression));
89
+ }
90
+ /**
91
+ * Get multiple next occurrences.
92
+ */
93
+ function getNextOccurrences(expression, count, from = /* @__PURE__ */ new Date()) {
94
+ const occurrences = [];
95
+ let current = from;
96
+ for (let i = 0; i < count; i++) {
97
+ const next = getNextOccurrence(expression, current);
98
+ occurrences.push(next);
99
+ current = next;
100
+ }
101
+ return occurrences;
102
+ }
103
+ /**
104
+ * Convert a Date to a specific timezone.
105
+ */
106
+ function toTimezone(date, timezone) {
107
+ const options = {
108
+ timeZone: timezone,
109
+ year: "numeric",
110
+ month: "2-digit",
111
+ day: "2-digit",
112
+ hour: "2-digit",
113
+ minute: "2-digit",
114
+ second: "2-digit",
115
+ hour12: false
116
+ };
117
+ const parts = new Intl.DateTimeFormat("en-US", options).formatToParts(date);
118
+ const get = (type) => parts.find((p) => p.type === type)?.value ?? "0";
119
+ const year = parseInt(get("year"), 10);
120
+ const month = parseInt(get("month"), 10) - 1;
121
+ const day = parseInt(get("day"), 10);
122
+ const hour = parseInt(get("hour"), 10);
123
+ const minute = parseInt(get("minute"), 10);
124
+ const second = parseInt(get("second"), 10);
125
+ return new Date(year, month, day, hour, minute, second);
126
+ }
127
+ /**
128
+ * Check if a cron expression is due in a specific timezone.
129
+ */
130
+ function isDueInTimezone(expression, timezone, date = /* @__PURE__ */ new Date()) {
131
+ return isDue(expression, toTimezone(date, timezone));
132
+ }
133
+ //#endregion
134
+ //#region src/scheduling/ScheduledTask.ts
135
+ /**
136
+ * Represents a scheduled task.
137
+ */
138
+ var ScheduledTask = class {
139
+ definition;
140
+ lastRun = null;
141
+ isRunning = false;
142
+ constructor(definition) {
143
+ this.definition = definition;
144
+ }
145
+ /**
146
+ * Get the task name.
147
+ */
148
+ getName() {
149
+ return this.definition.name ?? "anonymous";
150
+ }
151
+ /**
152
+ * Get the cron expression.
153
+ */
154
+ getExpression() {
155
+ return this.definition.expression;
156
+ }
157
+ /**
158
+ * Get the timezone.
159
+ */
160
+ getTimezone() {
161
+ return this.definition.timezone;
162
+ }
163
+ /**
164
+ * Check if the task is due.
165
+ */
166
+ isDue(date = /* @__PURE__ */ new Date()) {
167
+ if (this.definition.timezone) return isDueInTimezone(this.definition.expression, this.definition.timezone, date);
168
+ return isDue(this.definition.expression, date);
169
+ }
170
+ /**
171
+ * Check if the task should run based on conditions.
172
+ * Note: Overlap checking is handled separately in run() to prevent race conditions.
173
+ */
174
+ async shouldRun() {
175
+ if (this.definition.when) {
176
+ if (!await this.definition.when()) return false;
177
+ }
178
+ if (this.definition.skip) {
179
+ if (await this.definition.skip()) return false;
180
+ }
181
+ return true;
182
+ }
183
+ /**
184
+ * Run the task.
185
+ */
186
+ async run() {
187
+ if (this.definition.withoutOverlapping) {
188
+ if (this.isRunning) return;
189
+ this.isRunning = true;
190
+ }
191
+ if (!await this.shouldRun()) {
192
+ if (this.definition.withoutOverlapping) this.isRunning = false;
193
+ return;
194
+ }
195
+ if (!this.definition.withoutOverlapping) this.isRunning = true;
196
+ let error = null;
197
+ try {
198
+ if (this.definition.before) await this.definition.before();
199
+ await this.definition.callback();
200
+ this.lastRun = /* @__PURE__ */ new Date();
201
+ if (this.definition.onSuccess) await this.definition.onSuccess();
202
+ } catch (err) {
203
+ error = err instanceof Error ? err : new Error(String(err));
204
+ if (this.definition.onFailure) await this.definition.onFailure(error);
205
+ } finally {
206
+ this.isRunning = false;
207
+ if (this.definition.after) await this.definition.after();
208
+ }
209
+ if (error) throw error;
210
+ }
211
+ /**
212
+ * Get the last run time.
213
+ */
214
+ getLastRun() {
215
+ return this.lastRun;
216
+ }
217
+ /**
218
+ * Check if the task is currently running.
219
+ */
220
+ isCurrentlyRunning() {
221
+ return this.isRunning;
222
+ }
223
+ /**
224
+ * Get the task definition.
225
+ */
226
+ getDefinition() {
227
+ return { ...this.definition };
228
+ }
229
+ };
230
+ //#endregion
231
+ //#region src/scheduling/Schedule.ts
232
+ /**
233
+ * Pending schedule builder for fluent API.
234
+ */
235
+ var PendingSchedule = class {
236
+ callback;
237
+ expression = "* * * * *";
238
+ timezone;
239
+ taskName;
240
+ withoutOverlapping = false;
241
+ overlapExpiresAt;
242
+ onOneServer = false;
243
+ whenCallback;
244
+ skipCallback;
245
+ beforeCallback;
246
+ afterCallback;
247
+ successCallback;
248
+ failureCallback;
249
+ constructor(callback) {
250
+ this.callback = callback;
251
+ }
252
+ /**
253
+ * Run every minute.
254
+ */
255
+ everyMinute() {
256
+ this.expression = "* * * * *";
257
+ return this;
258
+ }
259
+ /**
260
+ * Run every two minutes.
261
+ */
262
+ everyTwoMinutes() {
263
+ this.expression = "*/2 * * * *";
264
+ return this;
265
+ }
266
+ /**
267
+ * Run every three minutes.
268
+ */
269
+ everyThreeMinutes() {
270
+ this.expression = "*/3 * * * *";
271
+ return this;
272
+ }
273
+ /**
274
+ * Run every four minutes.
275
+ */
276
+ everyFourMinutes() {
277
+ this.expression = "*/4 * * * *";
278
+ return this;
279
+ }
280
+ /**
281
+ * Run every five minutes.
282
+ */
283
+ everyFiveMinutes() {
284
+ this.expression = "*/5 * * * *";
285
+ return this;
286
+ }
287
+ /**
288
+ * Run every ten minutes.
289
+ */
290
+ everyTenMinutes() {
291
+ this.expression = "*/10 * * * *";
292
+ return this;
293
+ }
294
+ /**
295
+ * Run every fifteen minutes.
296
+ */
297
+ everyFifteenMinutes() {
298
+ this.expression = "*/15 * * * *";
299
+ return this;
300
+ }
301
+ /**
302
+ * Run every thirty minutes.
303
+ */
304
+ everyThirtyMinutes() {
305
+ this.expression = "*/30 * * * *";
306
+ return this;
307
+ }
308
+ /**
309
+ * Run hourly.
310
+ */
311
+ hourly() {
312
+ this.expression = "0 * * * *";
313
+ return this;
314
+ }
315
+ /**
316
+ * Run hourly at a specific minute.
317
+ */
318
+ hourlyAt(minute) {
319
+ this.expression = `${minute} * * * *`;
320
+ return this;
321
+ }
322
+ /**
323
+ * Run every two hours.
324
+ */
325
+ everyTwoHours() {
326
+ this.expression = "0 */2 * * *";
327
+ return this;
328
+ }
329
+ /**
330
+ * Run every three hours.
331
+ */
332
+ everyThreeHours() {
333
+ this.expression = "0 */3 * * *";
334
+ return this;
335
+ }
336
+ /**
337
+ * Run every four hours.
338
+ */
339
+ everyFourHours() {
340
+ this.expression = "0 */4 * * *";
341
+ return this;
342
+ }
343
+ /**
344
+ * Run every six hours.
345
+ */
346
+ everySixHours() {
347
+ this.expression = "0 */6 * * *";
348
+ return this;
349
+ }
350
+ /**
351
+ * Run daily at midnight.
352
+ */
353
+ daily() {
354
+ this.expression = "0 0 * * *";
355
+ return this;
356
+ }
357
+ /**
358
+ * Run daily at a specific time.
359
+ * @param time Time in HH:MM format
360
+ */
361
+ dailyAt(time) {
362
+ const [hour, minute] = time.split(":").map((n) => parseInt(n, 10));
363
+ this.expression = `${minute ?? 0} ${hour ?? 0} * * *`;
364
+ return this;
365
+ }
366
+ /**
367
+ * Alias for dailyAt.
368
+ */
369
+ at(time) {
370
+ return this.dailyAt(time);
371
+ }
372
+ /**
373
+ * Run twice daily.
374
+ */
375
+ twiceDaily(firstHour = 1, secondHour = 13) {
376
+ this.expression = `0 ${firstHour},${secondHour} * * *`;
377
+ return this;
378
+ }
379
+ /**
380
+ * Run weekly on Sunday at midnight.
381
+ */
382
+ weekly() {
383
+ this.expression = "0 0 * * 0";
384
+ return this;
385
+ }
386
+ /**
387
+ * Run weekly on a specific day at a specific time.
388
+ * @param day Day of week (0 = Sunday, 6 = Saturday)
389
+ * @param time Time in HH:MM format
390
+ */
391
+ weeklyOn(day, time = "00:00") {
392
+ const [hour, minute] = time.split(":").map((n) => parseInt(n, 10));
393
+ this.expression = `${minute ?? 0} ${hour ?? 0} * * ${day}`;
394
+ return this;
395
+ }
396
+ sundays() {
397
+ const parts = this.expression.split(" ");
398
+ parts[4] = "0";
399
+ this.expression = parts.join(" ");
400
+ return this;
401
+ }
402
+ mondays() {
403
+ const parts = this.expression.split(" ");
404
+ parts[4] = "1";
405
+ this.expression = parts.join(" ");
406
+ return this;
407
+ }
408
+ tuesdays() {
409
+ const parts = this.expression.split(" ");
410
+ parts[4] = "2";
411
+ this.expression = parts.join(" ");
412
+ return this;
413
+ }
414
+ wednesdays() {
415
+ const parts = this.expression.split(" ");
416
+ parts[4] = "3";
417
+ this.expression = parts.join(" ");
418
+ return this;
419
+ }
420
+ thursdays() {
421
+ const parts = this.expression.split(" ");
422
+ parts[4] = "4";
423
+ this.expression = parts.join(" ");
424
+ return this;
425
+ }
426
+ fridays() {
427
+ const parts = this.expression.split(" ");
428
+ parts[4] = "5";
429
+ this.expression = parts.join(" ");
430
+ return this;
431
+ }
432
+ saturdays() {
433
+ const parts = this.expression.split(" ");
434
+ parts[4] = "6";
435
+ this.expression = parts.join(" ");
436
+ return this;
437
+ }
438
+ /**
439
+ * Run on weekdays (Monday-Friday).
440
+ */
441
+ weekdays() {
442
+ const parts = this.expression.split(" ");
443
+ parts[4] = "1-5";
444
+ this.expression = parts.join(" ");
445
+ return this;
446
+ }
447
+ /**
448
+ * Run on weekends (Saturday-Sunday).
449
+ */
450
+ weekends() {
451
+ const parts = this.expression.split(" ");
452
+ parts[4] = "0,6";
453
+ this.expression = parts.join(" ");
454
+ return this;
455
+ }
456
+ /**
457
+ * Run monthly on the first day at midnight.
458
+ */
459
+ monthly() {
460
+ this.expression = "0 0 1 * *";
461
+ return this;
462
+ }
463
+ /**
464
+ * Run monthly on a specific day at a specific time.
465
+ */
466
+ monthlyOn(day, time = "00:00") {
467
+ const [hour, minute] = time.split(":").map((n) => parseInt(n, 10));
468
+ this.expression = `${minute ?? 0} ${hour ?? 0} ${day} * *`;
469
+ return this;
470
+ }
471
+ /**
472
+ * Run on the last day of the month.
473
+ */
474
+ lastDayOfMonth(time = "00:00") {
475
+ const [hour, minute] = time.split(":").map((n) => parseInt(n, 10));
476
+ this.expression = `${minute ?? 0} ${hour ?? 0} 28-31 * *`;
477
+ return this;
478
+ }
479
+ /**
480
+ * Run quarterly.
481
+ */
482
+ quarterly() {
483
+ this.expression = "0 0 1 1,4,7,10 *";
484
+ return this;
485
+ }
486
+ /**
487
+ * Run yearly on January 1st at midnight.
488
+ */
489
+ yearly() {
490
+ this.expression = "0 0 1 1 *";
491
+ return this;
492
+ }
493
+ /**
494
+ * Run yearly on a specific date.
495
+ */
496
+ yearlyOn(month, day, time = "00:00") {
497
+ const [hour, minute] = time.split(":").map((n) => parseInt(n, 10));
498
+ this.expression = `${minute ?? 0} ${hour ?? 0} ${day} ${month} *`;
499
+ return this;
500
+ }
501
+ /**
502
+ * Set a custom cron expression.
503
+ */
504
+ cron(expression) {
505
+ this.expression = expression;
506
+ return this;
507
+ }
508
+ /**
509
+ * Set the task name.
510
+ */
511
+ name(name) {
512
+ this.taskName = name;
513
+ return this;
514
+ }
515
+ /**
516
+ * Set the timezone.
517
+ */
518
+ tz(timezone) {
519
+ this.timezone = timezone;
520
+ return this;
521
+ }
522
+ /**
523
+ * Alias for tz().
524
+ */
525
+ setTimezone(timezone) {
526
+ return this.tz(timezone);
527
+ }
528
+ /**
529
+ * Prevent overlapping executions.
530
+ */
531
+ preventOverlapping(expiresAt) {
532
+ this.withoutOverlapping = true;
533
+ this.overlapExpiresAt = expiresAt;
534
+ return this;
535
+ }
536
+ /**
537
+ * Alias for preventOverlapping().
538
+ */
539
+ withoutOverlaps(expiresAt) {
540
+ return this.preventOverlapping(expiresAt);
541
+ }
542
+ /**
543
+ * Only run on one server.
544
+ */
545
+ runOnOneServer() {
546
+ this.onOneServer = true;
547
+ return this;
548
+ }
549
+ /**
550
+ * Set a condition for running.
551
+ */
552
+ when(callback) {
553
+ this.whenCallback = callback;
554
+ return this;
555
+ }
556
+ /**
557
+ * Set a condition to skip execution.
558
+ */
559
+ skip(callback) {
560
+ this.skipCallback = callback;
561
+ return this;
562
+ }
563
+ /**
564
+ * Set a before callback.
565
+ */
566
+ before(callback) {
567
+ this.beforeCallback = callback;
568
+ return this;
569
+ }
570
+ /**
571
+ * Set an after callback.
572
+ */
573
+ after(callback) {
574
+ this.afterCallback = callback;
575
+ return this;
576
+ }
577
+ /**
578
+ * Set a success callback.
579
+ */
580
+ onSuccess(callback) {
581
+ this.successCallback = callback;
582
+ return this;
583
+ }
584
+ /**
585
+ * Set a failure callback.
586
+ */
587
+ onFailure(callback) {
588
+ this.failureCallback = callback;
589
+ return this;
590
+ }
591
+ /**
592
+ * Build the task definition.
593
+ */
594
+ build() {
595
+ return {
596
+ name: this.taskName,
597
+ expression: this.expression,
598
+ timezone: this.timezone,
599
+ callback: this.callback,
600
+ withoutOverlapping: this.withoutOverlapping,
601
+ overlapExpiresAt: this.overlapExpiresAt,
602
+ onOneServer: this.onOneServer,
603
+ when: this.whenCallback,
604
+ skip: this.skipCallback,
605
+ before: this.beforeCallback,
606
+ after: this.afterCallback,
607
+ onSuccess: this.successCallback,
608
+ onFailure: this.failureCallback
609
+ };
610
+ }
611
+ /**
612
+ * Build as a ScheduledTask.
613
+ */
614
+ toTask() {
615
+ return new ScheduledTask(this.build());
616
+ }
617
+ };
618
+ /**
619
+ * Schedule builder for defining multiple tasks.
620
+ */
621
+ var Schedule = class {
622
+ tasks = [];
623
+ /**
624
+ * Schedule a callback.
625
+ */
626
+ call(callback) {
627
+ const pending = new PendingSchedule(callback);
628
+ this.tasks.push(pending);
629
+ return pending;
630
+ }
631
+ /**
632
+ * Schedule a job.
633
+ */
634
+ job(jobClass, payload) {
635
+ return this.call(async () => {
636
+ await jobClass.dispatch(payload);
637
+ });
638
+ }
639
+ /**
640
+ * Schedule a shell command.
641
+ */
642
+ command(cmd) {
643
+ return this.call(async () => {
644
+ const { exec } = await import("node:child_process");
645
+ const { promisify } = await import("node:util");
646
+ await promisify(exec)(cmd);
647
+ });
648
+ }
649
+ /**
650
+ * Get all pending schedules.
651
+ */
652
+ getPendingSchedules() {
653
+ return [...this.tasks];
654
+ }
655
+ /**
656
+ * Build all tasks.
657
+ */
658
+ buildTasks() {
659
+ return this.tasks.map((pending) => pending.toTask());
660
+ }
661
+ };
662
+ //#endregion
663
+ //#region src/scheduling/Scheduler.ts
664
+ /**
665
+ * Task scheduler for running periodic tasks.
666
+ *
667
+ * @example
668
+ * ```ts
669
+ * const scheduler = new Scheduler()
670
+ *
671
+ * scheduler.schedule((schedule) => {
672
+ * schedule.call(async () => {
673
+ * await cleanupOldSessions()
674
+ * }).daily().at('03:00').name('cleanup-sessions')
675
+ *
676
+ * schedule.job(SendWeeklyDigestJob, {})
677
+ * .weekly()
678
+ * .sundays()
679
+ * .at('09:00')
680
+ * .tz('Asia/Tokyo')
681
+ * })
682
+ *
683
+ * scheduler.start()
684
+ * ```
685
+ */
686
+ var Scheduler = class {
687
+ tasks = [];
688
+ options;
689
+ interval = null;
690
+ isRunning = false;
691
+ lastCheck = null;
692
+ /** This scheduler's claim on its hot-reload slot while it is running. */
693
+ hotReloadClaim;
694
+ constructor(options = {}) {
695
+ this.options = {
696
+ timezone: options.timezone ?? "UTC",
697
+ checkInterval: options.checkInterval ?? 6e4,
698
+ logger: options.logger ?? (() => {})
699
+ };
700
+ }
701
+ /**
702
+ * Define schedules using a callback.
703
+ */
704
+ schedule(definer) {
705
+ const schedule = new Schedule();
706
+ definer(schedule);
707
+ this.tasks.push(...schedule.buildTasks());
708
+ }
709
+ /**
710
+ * Add a pre-built task.
711
+ */
712
+ addTask(task) {
713
+ this.tasks.push(task);
714
+ }
715
+ /**
716
+ * Get all scheduled tasks.
717
+ */
718
+ getTasks() {
719
+ return [...this.tasks];
720
+ }
721
+ /**
722
+ * Get tasks that are due now.
723
+ */
724
+ getDueTasks(date = /* @__PURE__ */ new Date()) {
725
+ return this.tasks.filter((task) => task.isDue(date));
726
+ }
727
+ /**
728
+ * Run all due tasks.
729
+ */
730
+ async runDueTasks(date = /* @__PURE__ */ new Date()) {
731
+ const dueTasks = this.getDueTasks(date);
732
+ for (const task of dueTasks) {
733
+ this.options.logger(`Running task: ${task.getName()}`);
734
+ try {
735
+ await task.run();
736
+ this.options.logger(`Task completed: ${task.getName()}`);
737
+ } catch (error) {
738
+ const message = error instanceof Error ? error.message : String(error);
739
+ this.options.logger(`Task failed: ${task.getName()} - ${message}`);
740
+ }
741
+ }
742
+ }
743
+ /**
744
+ * Start the scheduler.
745
+ */
746
+ start() {
747
+ if (this.isRunning) return;
748
+ this.isRunning = true;
749
+ this.options.logger("Scheduler started");
750
+ this.hotReloadClaim = claimHotDisposable("scheduler", isHotReloadRuntime() ? (/* @__PURE__ */ new Error()).stack : void 0, this.options.timezone, () => this.stop());
751
+ this.tick();
752
+ this.interval = setInterval(() => {
753
+ this.tick();
754
+ }, this.options.checkInterval);
755
+ if (this.interval.unref) this.interval.unref();
756
+ }
757
+ /**
758
+ * Stop the scheduler.
759
+ */
760
+ stop() {
761
+ if (!this.isRunning) return;
762
+ if (this.interval) {
763
+ clearInterval(this.interval);
764
+ this.interval = null;
765
+ }
766
+ this.hotReloadClaim?.release();
767
+ this.hotReloadClaim = void 0;
768
+ this.isRunning = false;
769
+ this.options.logger("Scheduler stopped");
770
+ }
771
+ /**
772
+ * Check and run due tasks.
773
+ */
774
+ tick() {
775
+ const now = /* @__PURE__ */ new Date();
776
+ if (this.lastCheck) {
777
+ if (Math.floor(this.lastCheck.getTime() / 6e4) === Math.floor(now.getTime() / 6e4)) return;
778
+ }
779
+ this.lastCheck = now;
780
+ this.runDueTasks(now).catch((error) => {
781
+ this.options.logger(`Scheduler error: ${error}`);
782
+ });
783
+ }
784
+ /**
785
+ * Check if the scheduler is running.
786
+ */
787
+ getIsRunning() {
788
+ return this.isRunning;
789
+ }
790
+ /**
791
+ * Get the check interval.
792
+ */
793
+ getCheckInterval() {
794
+ return this.options.checkInterval;
795
+ }
796
+ /**
797
+ * Get the default timezone.
798
+ */
799
+ getTimezone() {
800
+ return this.options.timezone;
801
+ }
802
+ /**
803
+ * Clear all tasks.
804
+ */
805
+ clear() {
806
+ this.tasks = [];
807
+ }
808
+ /**
809
+ * Get the number of tasks.
810
+ */
811
+ count() {
812
+ return this.tasks.length;
813
+ }
814
+ /**
815
+ * Get task by name.
816
+ */
817
+ getTask(name) {
818
+ return this.tasks.find((task) => task.getName() === name);
819
+ }
820
+ /**
821
+ * Remove a task by name.
822
+ */
823
+ removeTask(name) {
824
+ const index = this.tasks.findIndex((task) => task.getName() === name);
825
+ if (index !== -1) {
826
+ this.tasks.splice(index, 1);
827
+ return true;
828
+ }
829
+ return false;
830
+ }
831
+ };
832
+ /**
833
+ * Create a scheduler instance.
834
+ */
835
+ function createScheduler(options) {
836
+ return new Scheduler(options);
837
+ }
838
+ //#endregion
839
+ export { ScheduledTask as a, isDue as c, parseCron as d, toTimezone as f, Schedule as i, isDueInTimezone as l, createScheduler as n, getNextOccurrence as o, PendingSchedule as r, getNextOccurrences as s, Scheduler as t, matchesCron as u };