alepha 0.12.1 → 0.13.1

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 (198) hide show
  1. package/dist/api-notifications/index.d.ts +111 -111
  2. package/dist/api-users/index.d.ts +1240 -1240
  3. package/dist/api-verifications/index.d.ts +94 -94
  4. package/dist/cli/{dist-Sz2EXvQX.cjs → dist-Dl9Vl7Ur.js} +17 -13
  5. package/dist/cli/{dist-BBPjuQ56.js.map → dist-Dl9Vl7Ur.js.map} +1 -1
  6. package/dist/cli/index.d.ts +3 -11
  7. package/dist/cli/index.js +106 -74
  8. package/dist/cli/index.js.map +1 -1
  9. package/dist/email/index.js +71 -73
  10. package/dist/email/index.js.map +1 -1
  11. package/dist/orm/index.d.ts +1 -1
  12. package/dist/orm/index.js.map +1 -1
  13. package/dist/queue/index.d.ts +4 -4
  14. package/dist/redis/index.d.ts +10 -10
  15. package/dist/retry/index.d.ts +1 -1
  16. package/dist/retry/index.js +2 -2
  17. package/dist/retry/index.js.map +1 -1
  18. package/dist/scheduler/index.d.ts +6 -6
  19. package/dist/server/index.js +1 -1
  20. package/dist/server/index.js.map +1 -1
  21. package/dist/server-auth/index.d.ts +193 -193
  22. package/dist/server-health/index.d.ts +17 -17
  23. package/dist/server-links/index.d.ts +34 -34
  24. package/dist/server-metrics/index.js +170 -174
  25. package/dist/server-metrics/index.js.map +1 -1
  26. package/dist/server-security/index.d.ts +9 -9
  27. package/dist/vite/index.js +4 -5
  28. package/dist/vite/index.js.map +1 -1
  29. package/dist/websocket/index.d.ts +7 -7
  30. package/package.json +52 -103
  31. package/src/cli/apps/AlephaPackageBuilderCli.ts +7 -2
  32. package/src/cli/assets/appRouterTs.ts +9 -0
  33. package/src/cli/assets/indexHtml.ts +2 -1
  34. package/src/cli/assets/mainBrowserTs.ts +10 -0
  35. package/src/cli/commands/CoreCommands.ts +6 -5
  36. package/src/cli/commands/DrizzleCommands.ts +65 -57
  37. package/src/cli/commands/VerifyCommands.ts +1 -1
  38. package/src/cli/services/ProjectUtils.ts +44 -38
  39. package/src/orm/providers/DrizzleKitProvider.ts +1 -1
  40. package/src/retry/descriptors/$retry.ts +5 -3
  41. package/src/server/providers/NodeHttpServerProvider.ts +1 -1
  42. package/src/vite/helpers/boot.ts +3 -3
  43. package/dist/api-files/index.cjs +0 -1293
  44. package/dist/api-files/index.cjs.map +0 -1
  45. package/dist/api-files/index.d.cts +0 -829
  46. package/dist/api-jobs/index.cjs +0 -274
  47. package/dist/api-jobs/index.cjs.map +0 -1
  48. package/dist/api-jobs/index.d.cts +0 -654
  49. package/dist/api-notifications/index.cjs +0 -380
  50. package/dist/api-notifications/index.cjs.map +0 -1
  51. package/dist/api-notifications/index.d.cts +0 -289
  52. package/dist/api-parameters/index.cjs +0 -66
  53. package/dist/api-parameters/index.cjs.map +0 -1
  54. package/dist/api-parameters/index.d.cts +0 -84
  55. package/dist/api-users/index.cjs +0 -6009
  56. package/dist/api-users/index.cjs.map +0 -1
  57. package/dist/api-users/index.d.cts +0 -4740
  58. package/dist/api-verifications/index.cjs +0 -407
  59. package/dist/api-verifications/index.cjs.map +0 -1
  60. package/dist/api-verifications/index.d.cts +0 -207
  61. package/dist/batch/index.cjs +0 -408
  62. package/dist/batch/index.cjs.map +0 -1
  63. package/dist/batch/index.d.cts +0 -330
  64. package/dist/bin/index.cjs +0 -17
  65. package/dist/bin/index.cjs.map +0 -1
  66. package/dist/bin/index.d.cts +0 -1
  67. package/dist/bucket/index.cjs +0 -303
  68. package/dist/bucket/index.cjs.map +0 -1
  69. package/dist/bucket/index.d.cts +0 -355
  70. package/dist/cache/index.cjs +0 -241
  71. package/dist/cache/index.cjs.map +0 -1
  72. package/dist/cache/index.d.cts +0 -202
  73. package/dist/cache-redis/index.cjs +0 -84
  74. package/dist/cache-redis/index.cjs.map +0 -1
  75. package/dist/cache-redis/index.d.cts +0 -40
  76. package/dist/cli/chunk-DSlc6foC.cjs +0 -43
  77. package/dist/cli/dist-BBPjuQ56.js +0 -2778
  78. package/dist/cli/dist-Sz2EXvQX.cjs.map +0 -1
  79. package/dist/cli/index.cjs +0 -1241
  80. package/dist/cli/index.cjs.map +0 -1
  81. package/dist/cli/index.d.cts +0 -422
  82. package/dist/command/index.cjs +0 -693
  83. package/dist/command/index.cjs.map +0 -1
  84. package/dist/command/index.d.cts +0 -340
  85. package/dist/core/index.cjs +0 -2264
  86. package/dist/core/index.cjs.map +0 -1
  87. package/dist/core/index.d.cts +0 -1927
  88. package/dist/datetime/index.cjs +0 -318
  89. package/dist/datetime/index.cjs.map +0 -1
  90. package/dist/datetime/index.d.cts +0 -145
  91. package/dist/email/index.cjs +0 -10874
  92. package/dist/email/index.cjs.map +0 -1
  93. package/dist/email/index.d.cts +0 -186
  94. package/dist/fake/index.cjs +0 -34641
  95. package/dist/fake/index.cjs.map +0 -1
  96. package/dist/fake/index.d.cts +0 -74
  97. package/dist/file/index.cjs +0 -1212
  98. package/dist/file/index.cjs.map +0 -1
  99. package/dist/file/index.d.cts +0 -698
  100. package/dist/lock/index.cjs +0 -226
  101. package/dist/lock/index.cjs.map +0 -1
  102. package/dist/lock/index.d.cts +0 -361
  103. package/dist/lock-redis/index.cjs +0 -113
  104. package/dist/lock-redis/index.cjs.map +0 -1
  105. package/dist/lock-redis/index.d.cts +0 -24
  106. package/dist/logger/index.cjs +0 -521
  107. package/dist/logger/index.cjs.map +0 -1
  108. package/dist/logger/index.d.cts +0 -281
  109. package/dist/orm/index.cjs +0 -2986
  110. package/dist/orm/index.cjs.map +0 -1
  111. package/dist/orm/index.d.cts +0 -2213
  112. package/dist/queue/index.cjs +0 -1044
  113. package/dist/queue/index.cjs.map +0 -1
  114. package/dist/queue/index.d.cts +0 -1265
  115. package/dist/queue-redis/index.cjs +0 -873
  116. package/dist/queue-redis/index.cjs.map +0 -1
  117. package/dist/queue-redis/index.d.cts +0 -82
  118. package/dist/redis/index.cjs +0 -153
  119. package/dist/redis/index.cjs.map +0 -1
  120. package/dist/redis/index.d.cts +0 -82
  121. package/dist/retry/index.cjs +0 -146
  122. package/dist/retry/index.cjs.map +0 -1
  123. package/dist/retry/index.d.cts +0 -172
  124. package/dist/router/index.cjs +0 -111
  125. package/dist/router/index.cjs.map +0 -1
  126. package/dist/router/index.d.cts +0 -46
  127. package/dist/scheduler/index.cjs +0 -576
  128. package/dist/scheduler/index.cjs.map +0 -1
  129. package/dist/scheduler/index.d.cts +0 -145
  130. package/dist/security/index.cjs +0 -2402
  131. package/dist/security/index.cjs.map +0 -1
  132. package/dist/security/index.d.cts +0 -598
  133. package/dist/server/index.cjs +0 -1680
  134. package/dist/server/index.cjs.map +0 -1
  135. package/dist/server/index.d.cts +0 -810
  136. package/dist/server-auth/index.cjs +0 -3146
  137. package/dist/server-auth/index.cjs.map +0 -1
  138. package/dist/server-auth/index.d.cts +0 -1164
  139. package/dist/server-cache/index.cjs +0 -252
  140. package/dist/server-cache/index.cjs.map +0 -1
  141. package/dist/server-cache/index.d.cts +0 -164
  142. package/dist/server-compress/index.cjs +0 -141
  143. package/dist/server-compress/index.cjs.map +0 -1
  144. package/dist/server-compress/index.d.cts +0 -38
  145. package/dist/server-cookies/index.cjs +0 -234
  146. package/dist/server-cookies/index.cjs.map +0 -1
  147. package/dist/server-cookies/index.d.cts +0 -144
  148. package/dist/server-cors/index.cjs +0 -201
  149. package/dist/server-cors/index.cjs.map +0 -1
  150. package/dist/server-cors/index.d.cts +0 -140
  151. package/dist/server-health/index.cjs +0 -62
  152. package/dist/server-health/index.cjs.map +0 -1
  153. package/dist/server-health/index.d.cts +0 -58
  154. package/dist/server-helmet/index.cjs +0 -131
  155. package/dist/server-helmet/index.cjs.map +0 -1
  156. package/dist/server-helmet/index.d.cts +0 -97
  157. package/dist/server-links/index.cjs +0 -992
  158. package/dist/server-links/index.cjs.map +0 -1
  159. package/dist/server-links/index.d.cts +0 -513
  160. package/dist/server-metrics/index.cjs +0 -4535
  161. package/dist/server-metrics/index.cjs.map +0 -1
  162. package/dist/server-metrics/index.d.cts +0 -35
  163. package/dist/server-multipart/index.cjs +0 -237
  164. package/dist/server-multipart/index.cjs.map +0 -1
  165. package/dist/server-multipart/index.d.cts +0 -50
  166. package/dist/server-proxy/index.cjs +0 -186
  167. package/dist/server-proxy/index.cjs.map +0 -1
  168. package/dist/server-proxy/index.d.cts +0 -234
  169. package/dist/server-rate-limit/index.cjs +0 -241
  170. package/dist/server-rate-limit/index.cjs.map +0 -1
  171. package/dist/server-rate-limit/index.d.cts +0 -183
  172. package/dist/server-security/index.cjs +0 -316
  173. package/dist/server-security/index.cjs.map +0 -1
  174. package/dist/server-security/index.d.cts +0 -173
  175. package/dist/server-static/index.cjs +0 -170
  176. package/dist/server-static/index.cjs.map +0 -1
  177. package/dist/server-static/index.d.cts +0 -121
  178. package/dist/server-swagger/index.cjs +0 -1021
  179. package/dist/server-swagger/index.cjs.map +0 -1
  180. package/dist/server-swagger/index.d.cts +0 -382
  181. package/dist/sms/index.cjs +0 -221
  182. package/dist/sms/index.cjs.map +0 -1
  183. package/dist/sms/index.d.cts +0 -130
  184. package/dist/thread/index.cjs +0 -350
  185. package/dist/thread/index.cjs.map +0 -1
  186. package/dist/thread/index.d.cts +0 -260
  187. package/dist/topic/index.cjs +0 -282
  188. package/dist/topic/index.cjs.map +0 -1
  189. package/dist/topic/index.d.cts +0 -523
  190. package/dist/topic-redis/index.cjs +0 -71
  191. package/dist/topic-redis/index.cjs.map +0 -1
  192. package/dist/topic-redis/index.d.cts +0 -42
  193. package/dist/vite/index.cjs +0 -1077
  194. package/dist/vite/index.cjs.map +0 -1
  195. package/dist/vite/index.d.cts +0 -542
  196. package/dist/websocket/index.cjs +0 -1117
  197. package/dist/websocket/index.cjs.map +0 -1
  198. package/dist/websocket/index.d.cts +0 -861
@@ -1,576 +0,0 @@
1
- let alepha = require("alepha");
2
- let alepha_lock = require("alepha/lock");
3
- let alepha_datetime = require("alepha/datetime");
4
- let alepha_logger = require("alepha/logger");
5
-
6
- //#region ../../node_modules/cron-schedule/dist/utils.js
7
- function extractDateElements(date) {
8
- return {
9
- second: date.getSeconds(),
10
- minute: date.getMinutes(),
11
- hour: date.getHours(),
12
- day: date.getDate(),
13
- month: date.getMonth(),
14
- weekday: date.getDay(),
15
- year: date.getFullYear()
16
- };
17
- }
18
- function getDaysInMonth(year, month) {
19
- return new Date(year, month + 1, 0).getDate();
20
- }
21
- function getDaysBetweenWeekdays(weekday1, weekday2) {
22
- if (weekday1 <= weekday2) return weekday2 - weekday1;
23
- return 6 - weekday1 + weekday2 + 1;
24
- }
25
-
26
- //#endregion
27
- //#region ../../node_modules/cron-schedule/dist/cron.js
28
- var Cron = class {
29
- constructor({ seconds, minutes, hours, days, months, weekdays }) {
30
- if (!seconds || seconds.size === 0) throw new Error("There must be at least one allowed second.");
31
- if (!minutes || minutes.size === 0) throw new Error("There must be at least one allowed minute.");
32
- if (!hours || hours.size === 0) throw new Error("There must be at least one allowed hour.");
33
- if (!months || months.size === 0) throw new Error("There must be at least one allowed month.");
34
- if ((!weekdays || weekdays.size === 0) && (!days || days.size === 0)) throw new Error("There must be at least one allowed day or weekday.");
35
- this.seconds = Array.from(seconds).sort((a, b) => a - b);
36
- this.minutes = Array.from(minutes).sort((a, b) => a - b);
37
- this.hours = Array.from(hours).sort((a, b) => a - b);
38
- this.days = Array.from(days).sort((a, b) => a - b);
39
- this.months = Array.from(months).sort((a, b) => a - b);
40
- this.weekdays = Array.from(weekdays).sort((a, b) => a - b);
41
- const validateData = (name, data, constraint) => {
42
- if (data.some((x) => typeof x !== "number" || x % 1 !== 0 || x < constraint.min || x > constraint.max)) throw new Error(`${name} must only consist of integers which are within the range of ${constraint.min} and ${constraint.max}`);
43
- };
44
- validateData("seconds", this.seconds, {
45
- min: 0,
46
- max: 59
47
- });
48
- validateData("minutes", this.minutes, {
49
- min: 0,
50
- max: 59
51
- });
52
- validateData("hours", this.hours, {
53
- min: 0,
54
- max: 23
55
- });
56
- validateData("days", this.days, {
57
- min: 1,
58
- max: 31
59
- });
60
- validateData("months", this.months, {
61
- min: 0,
62
- max: 11
63
- });
64
- validateData("weekdays", this.weekdays, {
65
- min: 0,
66
- max: 6
67
- });
68
- this.reversed = {
69
- seconds: this.seconds.map((x) => x).reverse(),
70
- minutes: this.minutes.map((x) => x).reverse(),
71
- hours: this.hours.map((x) => x).reverse(),
72
- days: this.days.map((x) => x).reverse(),
73
- months: this.months.map((x) => x).reverse(),
74
- weekdays: this.weekdays.map((x) => x).reverse()
75
- };
76
- }
77
- /**
78
- * Find the next or previous hour, starting from the given start hour that matches the hour constraint.
79
- * startHour itself might also be allowed.
80
- */
81
- findAllowedHour(dir, startHour) {
82
- return dir === "next" ? this.hours.find((x) => x >= startHour) : this.reversed.hours.find((x) => x <= startHour);
83
- }
84
- /**
85
- * Find the next or previous minute, starting from the given start minute that matches the minute constraint.
86
- * startMinute itself might also be allowed.
87
- */
88
- findAllowedMinute(dir, startMinute) {
89
- return dir === "next" ? this.minutes.find((x) => x >= startMinute) : this.reversed.minutes.find((x) => x <= startMinute);
90
- }
91
- /**
92
- * Find the next or previous second, starting from the given start second that matches the second constraint.
93
- * startSecond itself IS NOT allowed.
94
- */
95
- findAllowedSecond(dir, startSecond) {
96
- return dir === "next" ? this.seconds.find((x) => x > startSecond) : this.reversed.seconds.find((x) => x < startSecond);
97
- }
98
- /**
99
- * Find the next or previous time, starting from the given start time that matches the hour, minute
100
- * and second constraints. startTime itself might also be allowed.
101
- */
102
- findAllowedTime(dir, startTime) {
103
- let hour = this.findAllowedHour(dir, startTime.hour);
104
- if (hour !== void 0) if (hour === startTime.hour) {
105
- let minute = this.findAllowedMinute(dir, startTime.minute);
106
- if (minute !== void 0) if (minute === startTime.minute) {
107
- const second = this.findAllowedSecond(dir, startTime.second);
108
- if (second !== void 0) return {
109
- hour,
110
- minute,
111
- second
112
- };
113
- minute = this.findAllowedMinute(dir, dir === "next" ? startTime.minute + 1 : startTime.minute - 1);
114
- if (minute !== void 0) return {
115
- hour,
116
- minute,
117
- second: dir === "next" ? this.seconds[0] : this.reversed.seconds[0]
118
- };
119
- } else return {
120
- hour,
121
- minute,
122
- second: dir === "next" ? this.seconds[0] : this.reversed.seconds[0]
123
- };
124
- hour = this.findAllowedHour(dir, dir === "next" ? startTime.hour + 1 : startTime.hour - 1);
125
- if (hour !== void 0) return {
126
- hour,
127
- minute: dir === "next" ? this.minutes[0] : this.reversed.minutes[0],
128
- second: dir === "next" ? this.seconds[0] : this.reversed.seconds[0]
129
- };
130
- } else return {
131
- hour,
132
- minute: dir === "next" ? this.minutes[0] : this.reversed.minutes[0],
133
- second: dir === "next" ? this.seconds[0] : this.reversed.seconds[0]
134
- };
135
- }
136
- /**
137
- * Find the next or previous day in the given month, starting from the given startDay
138
- * that matches either the day or the weekday constraint. startDay itself might also be allowed.
139
- */
140
- findAllowedDayInMonth(dir, year, month, startDay) {
141
- var _a, _b;
142
- if (startDay < 1) throw new Error("startDay must not be smaller than 1.");
143
- const daysInMonth = getDaysInMonth(year, month);
144
- const daysRestricted = this.days.length !== 31;
145
- const weekdaysRestricted = this.weekdays.length !== 7;
146
- if (!daysRestricted && !weekdaysRestricted) {
147
- if (startDay > daysInMonth) return dir === "next" ? void 0 : daysInMonth;
148
- return startDay;
149
- }
150
- let allowedDayByDays;
151
- if (daysRestricted) {
152
- allowedDayByDays = dir === "next" ? this.days.find((x) => x >= startDay) : this.reversed.days.find((x) => x <= startDay);
153
- if (allowedDayByDays !== void 0 && allowedDayByDays > daysInMonth) allowedDayByDays = void 0;
154
- }
155
- let allowedDayByWeekdays;
156
- if (weekdaysRestricted) {
157
- const startWeekday = new Date(year, month, startDay).getDay();
158
- const nearestAllowedWeekday = dir === "next" ? (_a = this.weekdays.find((x) => x >= startWeekday)) !== null && _a !== void 0 ? _a : this.weekdays[0] : (_b = this.reversed.weekdays.find((x) => x <= startWeekday)) !== null && _b !== void 0 ? _b : this.reversed.weekdays[0];
159
- if (nearestAllowedWeekday !== void 0) {
160
- const daysBetweenWeekdays = dir === "next" ? getDaysBetweenWeekdays(startWeekday, nearestAllowedWeekday) : getDaysBetweenWeekdays(nearestAllowedWeekday, startWeekday);
161
- allowedDayByWeekdays = dir === "next" ? startDay + daysBetweenWeekdays : startDay - daysBetweenWeekdays;
162
- if (allowedDayByWeekdays > daysInMonth || allowedDayByWeekdays < 1) allowedDayByWeekdays = void 0;
163
- }
164
- }
165
- if (allowedDayByDays !== void 0 && allowedDayByWeekdays !== void 0) return dir === "next" ? Math.min(allowedDayByDays, allowedDayByWeekdays) : Math.max(allowedDayByDays, allowedDayByWeekdays);
166
- if (allowedDayByDays !== void 0) return allowedDayByDays;
167
- if (allowedDayByWeekdays !== void 0) return allowedDayByWeekdays;
168
- }
169
- /** Gets the next date starting from the given start date or now. */
170
- getNextDate(startDate = /* @__PURE__ */ new Date()) {
171
- const startDateElements = extractDateElements(startDate);
172
- let minYear = startDateElements.year;
173
- let startIndexMonth = this.months.findIndex((x) => x >= startDateElements.month);
174
- if (startIndexMonth === -1) {
175
- startIndexMonth = 0;
176
- minYear++;
177
- }
178
- const maxIterations = this.months.length * 5;
179
- for (let i = 0; i < maxIterations; i++) {
180
- const year = minYear + Math.floor((startIndexMonth + i) / this.months.length);
181
- const month = this.months[(startIndexMonth + i) % this.months.length];
182
- const isStartMonth = year === startDateElements.year && month === startDateElements.month;
183
- let day = this.findAllowedDayInMonth("next", year, month, isStartMonth ? startDateElements.day : 1);
184
- let isStartDay = isStartMonth && day === startDateElements.day;
185
- if (day !== void 0 && isStartDay) {
186
- const nextTime = this.findAllowedTime("next", startDateElements);
187
- if (nextTime !== void 0) return new Date(year, month, day, nextTime.hour, nextTime.minute, nextTime.second);
188
- day = this.findAllowedDayInMonth("next", year, month, day + 1);
189
- isStartDay = false;
190
- }
191
- if (day !== void 0 && !isStartDay) return new Date(year, month, day, this.hours[0], this.minutes[0], this.seconds[0]);
192
- }
193
- throw new Error("No valid next date was found.");
194
- }
195
- /** Gets the specified amount of future dates starting from the given start date or now. */
196
- getNextDates(amount, startDate) {
197
- const dates = [];
198
- let nextDate;
199
- for (let i = 0; i < amount; i++) {
200
- nextDate = this.getNextDate(nextDate !== null && nextDate !== void 0 ? nextDate : startDate);
201
- dates.push(nextDate);
202
- }
203
- return dates;
204
- }
205
- /**
206
- * Get an ES6 compatible iterator which iterates over the next dates starting from startDate or now.
207
- * The iterator runs until the optional endDate is reached or forever.
208
- */
209
- *getNextDatesIterator(startDate, endDate) {
210
- let nextDate;
211
- while (true) {
212
- nextDate = this.getNextDate(nextDate !== null && nextDate !== void 0 ? nextDate : startDate);
213
- if (endDate && endDate.getTime() < nextDate.getTime()) return;
214
- yield nextDate;
215
- }
216
- }
217
- /** Gets the previous date starting from the given start date or now. */
218
- getPrevDate(startDate = /* @__PURE__ */ new Date()) {
219
- const startDateElements = extractDateElements(startDate);
220
- let maxYear = startDateElements.year;
221
- let startIndexMonth = this.reversed.months.findIndex((x) => x <= startDateElements.month);
222
- if (startIndexMonth === -1) {
223
- startIndexMonth = 0;
224
- maxYear--;
225
- }
226
- const maxIterations = this.reversed.months.length * 5;
227
- for (let i = 0; i < maxIterations; i++) {
228
- const year = maxYear - Math.floor((startIndexMonth + i) / this.reversed.months.length);
229
- const month = this.reversed.months[(startIndexMonth + i) % this.reversed.months.length];
230
- const isStartMonth = year === startDateElements.year && month === startDateElements.month;
231
- let day = this.findAllowedDayInMonth("prev", year, month, isStartMonth ? startDateElements.day : getDaysInMonth(year, month));
232
- let isStartDay = isStartMonth && day === startDateElements.day;
233
- if (day !== void 0 && isStartDay) {
234
- const prevTime = this.findAllowedTime("prev", startDateElements);
235
- if (prevTime !== void 0) return new Date(year, month, day, prevTime.hour, prevTime.minute, prevTime.second);
236
- if (day > 1) {
237
- day = this.findAllowedDayInMonth("prev", year, month, day - 1);
238
- isStartDay = false;
239
- }
240
- }
241
- if (day !== void 0 && !isStartDay) return new Date(year, month, day, this.reversed.hours[0], this.reversed.minutes[0], this.reversed.seconds[0]);
242
- }
243
- throw new Error("No valid previous date was found.");
244
- }
245
- /** Gets the specified amount of previous dates starting from the given start date or now. */
246
- getPrevDates(amount, startDate) {
247
- const dates = [];
248
- let prevDate;
249
- for (let i = 0; i < amount; i++) {
250
- prevDate = this.getPrevDate(prevDate !== null && prevDate !== void 0 ? prevDate : startDate);
251
- dates.push(prevDate);
252
- }
253
- return dates;
254
- }
255
- /**
256
- * Get an ES6 compatible iterator which iterates over the previous dates starting from startDate or now.
257
- * The iterator runs until the optional endDate is reached or forever.
258
- */
259
- *getPrevDatesIterator(startDate, endDate) {
260
- let prevDate;
261
- while (true) {
262
- prevDate = this.getPrevDate(prevDate !== null && prevDate !== void 0 ? prevDate : startDate);
263
- if (endDate && endDate.getTime() > prevDate.getTime()) return;
264
- yield prevDate;
265
- }
266
- }
267
- /** Returns true when there is a cron date at the given date. */
268
- matchDate(date) {
269
- const { second, minute, hour, day, month, weekday } = extractDateElements(date);
270
- if (this.seconds.indexOf(second) === -1 || this.minutes.indexOf(minute) === -1 || this.hours.indexOf(hour) === -1 || this.months.indexOf(month) === -1) return false;
271
- if (this.days.length !== 31 && this.weekdays.length !== 7) return this.days.indexOf(day) !== -1 || this.weekdays.indexOf(weekday) !== -1;
272
- return this.days.indexOf(day) !== -1 && this.weekdays.indexOf(weekday) !== -1;
273
- }
274
- };
275
-
276
- //#endregion
277
- //#region ../../node_modules/cron-schedule/dist/cron-parser.js
278
- const secondConstraint = {
279
- min: 0,
280
- max: 59
281
- };
282
- const minuteConstraint = {
283
- min: 0,
284
- max: 59
285
- };
286
- const hourConstraint = {
287
- min: 0,
288
- max: 23
289
- };
290
- const dayConstraint = {
291
- min: 1,
292
- max: 31
293
- };
294
- const monthConstraint = {
295
- min: 1,
296
- max: 12,
297
- aliases: {
298
- jan: "1",
299
- feb: "2",
300
- mar: "3",
301
- apr: "4",
302
- may: "5",
303
- jun: "6",
304
- jul: "7",
305
- aug: "8",
306
- sep: "9",
307
- oct: "10",
308
- nov: "11",
309
- dec: "12"
310
- }
311
- };
312
- const weekdayConstraint = {
313
- min: 0,
314
- max: 7,
315
- aliases: {
316
- mon: "1",
317
- tue: "2",
318
- wed: "3",
319
- thu: "4",
320
- fri: "5",
321
- sat: "6",
322
- sun: "7"
323
- }
324
- };
325
- const timeNicknames = {
326
- "@yearly": "0 0 1 1 *",
327
- "@annually": "0 0 1 1 *",
328
- "@monthly": "0 0 1 * *",
329
- "@weekly": "0 0 * * 0",
330
- "@daily": "0 0 * * *",
331
- "@hourly": "0 * * * *",
332
- "@minutely": "* * * * *"
333
- };
334
- function parseElement(element, constraint) {
335
- const result = /* @__PURE__ */ new Set();
336
- if (element === "*") {
337
- for (let i = constraint.min; i <= constraint.max; i = i + 1) result.add(i);
338
- return result;
339
- }
340
- const listElements = element.split(",");
341
- if (listElements.length > 1) {
342
- for (const listElement of listElements) {
343
- const parsedListElement = parseElement(listElement, constraint);
344
- for (const x of parsedListElement) result.add(x);
345
- }
346
- return result;
347
- }
348
- const parseSingleElement = (singleElement) => {
349
- var _a, _b;
350
- singleElement = (_b = (_a = constraint.aliases) === null || _a === void 0 ? void 0 : _a[singleElement.toLowerCase()]) !== null && _b !== void 0 ? _b : singleElement;
351
- const parsedElement = Number.parseInt(singleElement, 10);
352
- if (Number.isNaN(parsedElement)) throw new Error(`Failed to parse ${element}: ${singleElement} is NaN.`);
353
- if (parsedElement < constraint.min || parsedElement > constraint.max) throw new Error(`Failed to parse ${element}: ${singleElement} is outside of constraint range of ${constraint.min} - ${constraint.max}.`);
354
- return parsedElement;
355
- };
356
- const rangeSegments = /^(([0-9a-zA-Z]+)-([0-9a-zA-Z]+)|\*)(\/([0-9]+))?$/.exec(element);
357
- if (rangeSegments === null) {
358
- result.add(parseSingleElement(element));
359
- return result;
360
- }
361
- let parsedStart = rangeSegments[1] === "*" ? constraint.min : parseSingleElement(rangeSegments[2]);
362
- const parsedEnd = rangeSegments[1] === "*" ? constraint.max : parseSingleElement(rangeSegments[3]);
363
- if (constraint === weekdayConstraint && parsedStart === 7 && parsedEnd !== 7) parsedStart = 0;
364
- if (parsedStart > parsedEnd) throw new Error(`Failed to parse ${element}: Invalid range (start: ${parsedStart}, end: ${parsedEnd}).`);
365
- const step = rangeSegments[5];
366
- let parsedStep = 1;
367
- if (step !== void 0) {
368
- parsedStep = Number.parseInt(step, 10);
369
- if (Number.isNaN(parsedStep)) throw new Error(`Failed to parse step: ${step} is NaN.`);
370
- if (parsedStep < 1) throw new Error(`Failed to parse step: Expected ${step} to be greater than 0.`);
371
- }
372
- for (let i = parsedStart; i <= parsedEnd; i = i + parsedStep) result.add(i);
373
- return result;
374
- }
375
- /** Parses a cron expression into a Cron instance. */
376
- function parseCronExpression(cronExpression) {
377
- var _a;
378
- if (typeof cronExpression !== "string") throw new TypeError("Invalid cron expression: must be of type string.");
379
- cronExpression = (_a = timeNicknames[cronExpression.toLowerCase()]) !== null && _a !== void 0 ? _a : cronExpression;
380
- const elements = cronExpression.split(" ").filter((elem) => elem.length > 0);
381
- if (elements.length < 5 || elements.length > 6) throw new Error("Invalid cron expression: expected 5 or 6 elements.");
382
- const rawSeconds = elements.length === 6 ? elements[0] : "0";
383
- const rawMinutes = elements.length === 6 ? elements[1] : elements[0];
384
- const rawHours = elements.length === 6 ? elements[2] : elements[1];
385
- const rawDays = elements.length === 6 ? elements[3] : elements[2];
386
- const rawMonths = elements.length === 6 ? elements[4] : elements[3];
387
- const rawWeekdays = elements.length === 6 ? elements[5] : elements[4];
388
- return new Cron({
389
- seconds: parseElement(rawSeconds, secondConstraint),
390
- minutes: parseElement(rawMinutes, minuteConstraint),
391
- hours: parseElement(rawHours, hourConstraint),
392
- days: parseElement(rawDays, dayConstraint),
393
- months: new Set(Array.from(parseElement(rawMonths, monthConstraint)).map((x) => x - 1)),
394
- weekdays: new Set(Array.from(parseElement(rawWeekdays, weekdayConstraint)).map((x) => x % 7))
395
- });
396
- }
397
-
398
- //#endregion
399
- //#region src/scheduler/providers/CronProvider.ts
400
- var CronProvider = class {
401
- dt = (0, alepha.$inject)(alepha_datetime.DateTimeProvider);
402
- alepha = (0, alepha.$inject)(alepha.Alepha);
403
- log = (0, alepha_logger.$logger)();
404
- cronJobs = [];
405
- getCronJobs() {
406
- return this.cronJobs;
407
- }
408
- start = (0, alepha.$hook)({
409
- on: "start",
410
- handler: () => {
411
- for (const cron of this.cronJobs) if (!cron.running) {
412
- cron.running = true;
413
- this.log.debug(`Starting cron task '${cron.name}' with '${cron.expression}'`);
414
- this.run(cron);
415
- }
416
- }
417
- });
418
- stop = (0, alepha.$hook)({
419
- on: "stop",
420
- handler: () => {
421
- for (const cron of this.cronJobs) this.abort(cron);
422
- }
423
- });
424
- boot(name) {
425
- const cron = typeof name === "string" ? this.cronJobs.find((c) => c.name === name) : name;
426
- if (!cron) return;
427
- cron.running = true;
428
- this.log.debug(`Starting cron task '${cron.name}' with '${cron.expression}'`);
429
- this.run(cron);
430
- }
431
- abort(name) {
432
- const cron = typeof name === "string" ? this.cronJobs.find((c) => c.name === name) : name;
433
- if (!cron) return;
434
- cron.running = false;
435
- cron.abort.abort();
436
- this.log.debug(`Cron task '${cron.name}' stopped`);
437
- }
438
- /**
439
- * Registers a cron job.
440
- *
441
- * It's automatically done when using the `$scheduler` descriptor but can also be used manually.
442
- */
443
- createCronJob(name, expression, handler, start) {
444
- const cron = {
445
- name,
446
- cron: parseCronExpression(expression),
447
- expression,
448
- handler,
449
- loop: true,
450
- abort: new AbortController()
451
- };
452
- this.cronJobs.push(cron);
453
- if (start && this.alepha.isStarted()) this.boot(cron);
454
- }
455
- run(task, now = this.dt.now()) {
456
- if (!task.running) return;
457
- const [next] = task.cron.getNextDates(1, now.toDate());
458
- if (!next) return;
459
- const duration = next.getTime() - now.toDate().getTime();
460
- task.abort = new AbortController();
461
- this.dt.wait(duration, {
462
- now: now.valueOf(),
463
- signal: task.abort.signal
464
- }).then(() => {
465
- if (!task.running) {
466
- this.log.trace("Cron task stopped before execution");
467
- return;
468
- }
469
- this.log.trace("Running cron task");
470
- task.handler({ now: this.dt.of(next) }).catch((err) => {
471
- if (task.onError) task.onError(err);
472
- else this.log.error("Error in cron task:", err);
473
- });
474
- if (task.loop) this.run(task, this.dt.of(next));
475
- }).catch((err) => {
476
- this.log.warn("Issue during cron waiting timer", err);
477
- });
478
- }
479
- };
480
-
481
- //#endregion
482
- //#region src/scheduler/descriptors/$scheduler.ts
483
- /**
484
- * Scheduler descriptor.
485
- */
486
- const $scheduler = (options) => {
487
- return (0, alepha.createDescriptor)(SchedulerDescriptor, options);
488
- };
489
- const envSchema = alepha.t.object({ SCHEDULER_PREFIX: alepha.t.optional(alepha.t.text({ description: "Prefix store key" })) });
490
- var SchedulerDescriptor = class extends alepha.Descriptor {
491
- log = (0, alepha_logger.$logger)();
492
- env = (0, alepha.$env)(envSchema);
493
- alepha = (0, alepha.$inject)(alepha.Alepha);
494
- dateTimeProvider = (0, alepha.$inject)(alepha_datetime.DateTimeProvider);
495
- cronProvider = (0, alepha.$inject)(CronProvider);
496
- get name() {
497
- return this.options.name ?? `${this.config.service.name}.${this.config.propertyKey}`;
498
- }
499
- onInit() {
500
- if (this.options.interval) this.dateTimeProvider.createInterval(() => this.trigger(), this.options.interval);
501
- if (this.options.cron) this.cronProvider.createCronJob(this.name, this.options.cron, () => this.trigger());
502
- }
503
- async trigger() {
504
- if (!this.alepha.isStarted()) return;
505
- const context = this.alepha.context.createContextId();
506
- await this.alepha.context.run(async () => {
507
- try {
508
- const now = this.dateTimeProvider.now();
509
- await this.alepha.events.emit("scheduler:begin", {
510
- name: this.name,
511
- now,
512
- context
513
- });
514
- if (this.options.lock !== false) await this.schedulerLock.run({ now });
515
- else await this.options.handler({ now });
516
- await this.alepha.events.emit("scheduler:success", {
517
- name: this.name,
518
- context
519
- }, { catch: true });
520
- } catch (error) {
521
- await this.alepha.events.emit("scheduler:error", {
522
- name: this.name,
523
- error,
524
- context
525
- }, { catch: true });
526
- this.log.error("Error running scheduler:", error);
527
- }
528
- await this.alepha.events.emit("scheduler:end", {
529
- name: this.name,
530
- context
531
- }, { catch: true });
532
- }, { context });
533
- }
534
- schedulerLock = (0, alepha_lock.$lock)({
535
- name: () => {
536
- return `${this.env.SCHEDULER_PREFIX ? `${this.env.SCHEDULER_PREFIX}:` : ""}scheduler:${this.name}`;
537
- },
538
- handler: async (args) => {
539
- await this.options.handler(args);
540
- }
541
- });
542
- };
543
- $scheduler[alepha.KIND] = SchedulerDescriptor;
544
-
545
- //#endregion
546
- //#region src/scheduler/constants/CRON.ts
547
- const CRON = {
548
- EVERY_MINUTE: "* * * * *",
549
- EVERY_5_MINUTES: "*/5 * * * *",
550
- EVERY_15_MINUTES: "*/15 * * * *",
551
- EVERY_30_MINUTES: "*/30 * * * *",
552
- EVERY_HOUR: "0 * * * *",
553
- EVERY_DAY_AT_MIDNIGHT: "0 0 * * *"
554
- };
555
-
556
- //#endregion
557
- //#region src/scheduler/index.ts
558
- /**
559
- * Generic interface for scheduling tasks.
560
- *
561
- * @see {@link $scheduler}
562
- * @module alepha.scheduler
563
- */
564
- const AlephaScheduler = (0, alepha.$module)({
565
- name: "alepha.scheduler",
566
- descriptors: [$scheduler],
567
- services: [alepha_lock.AlephaLock, CronProvider]
568
- });
569
-
570
- //#endregion
571
- exports.$scheduler = $scheduler;
572
- exports.AlephaScheduler = AlephaScheduler;
573
- exports.CRON = CRON;
574
- exports.CronProvider = CronProvider;
575
- exports.SchedulerDescriptor = SchedulerDescriptor;
576
- //# sourceMappingURL=index.cjs.map