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,226 +0,0 @@
1
- let alepha = require("alepha");
2
- let alepha_topic = require("alepha/topic");
3
- let alepha_datetime = require("alepha/datetime");
4
- let alepha_logger = require("alepha/logger");
5
-
6
- //#region src/lock/providers/LockProvider.ts
7
- /**
8
- * Store Provider Interface
9
- */
10
- var LockProvider = class {};
11
-
12
- //#endregion
13
- //#region src/lock/providers/LockTopicProvider.ts
14
- var LockTopicProvider = class extends alepha_topic.TopicProvider {};
15
-
16
- //#endregion
17
- //#region src/lock/descriptors/$lock.ts
18
- /**
19
- * Creates a distributed lock descriptor for ensuring single-instance execution across processes.
20
- *
21
- * Prevents multiple instances of the same operation from running simultaneously, essential for
22
- * maintaining data consistency and preventing race conditions in distributed applications.
23
- *
24
- * **Key Features**
25
- * - Distributed coordination across multiple processes, servers, and containers
26
- * - Automatic expiration to prevent deadlocks
27
- * - Configurable wait behavior (blocking vs. non-blocking)
28
- * - Optional grace periods for lock extension after completion
29
- * - Dynamic or static lock keys for fine-grained control
30
- *
31
- * **Common Use Cases**
32
- * - Database migrations and scheduled jobs
33
- * - File processing and batch operations
34
- * - Critical section protection and resource initialization
35
- *
36
- * @example
37
- * ```ts
38
- * class TaskService {
39
- * // Basic scheduled task - only one server executes
40
- * dailyReport = $lock({
41
- * handler: async () => {
42
- * const report = await this.generateDailyReport();
43
- * await this.sendReportToManagement(report);
44
- * }
45
- * });
46
- *
47
- * // Migration with wait - all instances wait for completion
48
- * migration = $lock({
49
- * wait: true,
50
- * maxDuration: [10, "minutes"],
51
- * handler: async (version: string) => {
52
- * await this.runMigrationScripts(version);
53
- * }
54
- * });
55
- *
56
- * // Dynamic lock keys for per-resource locking
57
- * processFile = $lock({
58
- * name: (fileId: string) => `file-processing:${fileId}`,
59
- * gracePeriod: [5, "minutes"],
60
- * handler: async (fileId: string) => {
61
- * await this.processFileData(fileId);
62
- * }
63
- * });
64
- * }
65
- * ```
66
- */
67
- const $lock = (options) => {
68
- return (0, alepha.createDescriptor)(LockDescriptor, options);
69
- };
70
- const envSchema = alepha.t.object({ LOCK_PREFIX_KEY: alepha.t.text({ default: "lock" }) });
71
- var LockDescriptor = class extends alepha.Descriptor {
72
- log = (0, alepha_logger.$logger)();
73
- provider = (0, alepha.$inject)(LockProvider);
74
- env = (0, alepha.$env)(envSchema);
75
- dateTimeProvider = (0, alepha.$inject)(alepha_datetime.DateTimeProvider);
76
- id = crypto.randomUUID();
77
- maxDuration = this.dateTimeProvider.duration(this.options.maxDuration ?? [5, "minutes"]);
78
- topicLockEnd = (0, alepha_topic.$topic)({
79
- name: `${this.env.LOCK_PREFIX_KEY}:lock-end`,
80
- provider: LockTopicProvider,
81
- schema: { payload: alepha.t.object({ name: alepha.t.text() }) }
82
- });
83
- async run(...args) {
84
- const key = this.key(...args);
85
- const handler = this.options.handler;
86
- const lock = await this.lock(key);
87
- if (lock.endedAt) return;
88
- if (lock.id !== this.id) {
89
- if (this.options.wait) try {
90
- await this.wait(key, this.maxDuration);
91
- } catch (error) {
92
- if (error instanceof alepha_topic.TopicTimeoutError) {
93
- this.log.warn(`Lock timeout for '${key}' has been reached. Retry...`);
94
- await this.run(...args);
95
- } else throw error;
96
- }
97
- return;
98
- }
99
- this.log.debug(`Lock '${key}' ...`);
100
- try {
101
- await handler(...args);
102
- } finally {
103
- await this.topicLockEnd.publish({ name: key });
104
- await this.setGracePeriod(key, lock, ...args);
105
- this.log.debug(`Lock '${key}' OK`);
106
- }
107
- }
108
- /**
109
- * Set the lock for the given key.
110
- */
111
- async lock(key) {
112
- const value = await this.provider.set(key, `${this.id},${this.dateTimeProvider.nowISOString()}`, true, this.maxDuration.as("milliseconds"));
113
- return this.parse(value);
114
- }
115
- async setGracePeriod(key, lock, ...args) {
116
- const gracePeriod = this.options.gracePeriod ? this.dateTimeProvider.isDurationLike(this.options.gracePeriod) ? this.options.gracePeriod : this.options.gracePeriod(...args) : void 0;
117
- if (gracePeriod) await this.provider.set(key, `${this.id},${lock.createdAt.toISOString()},${this.dateTimeProvider.nowISOString()}`, false, this.dateTimeProvider.duration(gracePeriod).as("milliseconds"));
118
- else await this.provider.del(key);
119
- }
120
- async wait(key, maxDuration) {
121
- this.log.debug(`Wait for lock '${key}' ...`);
122
- await this.topicLockEnd.wait({
123
- filter: (message) => message.payload.name === key,
124
- timeout: maxDuration
125
- });
126
- this.log.debug(`Wait for lock '${key}' OK`);
127
- }
128
- key(...args) {
129
- let base = "";
130
- if (this.options.name) if (typeof this.options.name === "string") base = this.options.name;
131
- else base = this.options.name(...args);
132
- else base = `${this.config.service.name}:${this.config.propertyKey}`;
133
- return `${this.env.LOCK_PREFIX_KEY}:${base}`;
134
- }
135
- parse(value) {
136
- const [id, createdAtStr, endedAtStr] = value.split(",");
137
- return {
138
- id,
139
- createdAt: this.dateTimeProvider.of(createdAtStr),
140
- endedAt: endedAtStr ? this.dateTimeProvider.of(endedAtStr) : void 0
141
- };
142
- }
143
- };
144
- $lock[alepha.KIND] = LockDescriptor;
145
-
146
- //#endregion
147
- //#region src/lock/providers/MemoryLockProvider.ts
148
- /**
149
- * A simple in-memory store provider.
150
- */
151
- var MemoryLockProvider = class {
152
- dateTimeProvider = (0, alepha.$inject)(alepha_datetime.DateTimeProvider);
153
- log = (0, alepha_logger.$logger)();
154
- /**
155
- * The in-memory store.
156
- */
157
- store = {};
158
- /**
159
- * Timeouts used to expire keys.
160
- */
161
- storeTimeout = {};
162
- async set(key, value, nx, px) {
163
- if (nx && this.store[key] != null) return this.store[key];
164
- if (px) this.ttl(key, px);
165
- this.store[key] = value;
166
- return this.store[key];
167
- }
168
- async del(...keys) {
169
- for (const key of keys) {
170
- delete this.store[key];
171
- if (this.storeTimeout[key] != null) {
172
- this.storeTimeout[key].clear();
173
- delete this.storeTimeout[key];
174
- }
175
- }
176
- }
177
- ttl(key, ms) {
178
- if (this.storeTimeout[key] != null) {
179
- this.storeTimeout[key].clear();
180
- delete this.storeTimeout[key];
181
- }
182
- this.storeTimeout[key] = this.dateTimeProvider.createTimeout(() => {
183
- delete this.store[key];
184
- delete this.storeTimeout[key];
185
- }, ms);
186
- }
187
- };
188
-
189
- //#endregion
190
- //#region src/lock/index.ts
191
- /**
192
- * Lock a resource for a certain period of time.
193
- *
194
- * This module provides a memory implementation of the lock provider.
195
- * You probably want to use an implementation like RedisLockProvider for distributed systems.
196
- *
197
- * @see {@link $lock}
198
- * @module alepha.lock
199
- */
200
- const AlephaLock = (0, alepha.$module)({
201
- name: "alepha.lock",
202
- descriptors: [$lock],
203
- services: [
204
- LockProvider,
205
- MemoryLockProvider,
206
- LockTopicProvider
207
- ],
208
- register: (alepha$1) => alepha$1.with({
209
- optional: true,
210
- provide: LockTopicProvider,
211
- use: alepha_topic.MemoryTopicProvider
212
- }).with({
213
- optional: true,
214
- provide: LockProvider,
215
- use: MemoryLockProvider
216
- })
217
- });
218
-
219
- //#endregion
220
- exports.$lock = $lock;
221
- exports.AlephaLock = AlephaLock;
222
- exports.LockDescriptor = LockDescriptor;
223
- exports.LockProvider = LockProvider;
224
- exports.LockTopicProvider = LockTopicProvider;
225
- exports.MemoryLockProvider = MemoryLockProvider;
226
- //# sourceMappingURL=index.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.cjs","names":["TopicProvider","t","Descriptor","DateTimeProvider","TopicTimeoutError","KIND","DateTimeProvider","alepha","MemoryTopicProvider"],"sources":["../../src/lock/providers/LockProvider.ts","../../src/lock/providers/LockTopicProvider.ts","../../src/lock/descriptors/$lock.ts","../../src/lock/providers/MemoryLockProvider.ts","../../src/lock/index.ts"],"sourcesContent":["/**\n * Store Provider Interface\n */\nexport abstract class LockProvider {\n /**\n * Set the string value of a key.\n *\n * @param key The key of the value to set.\n * @param value The value to set.\n * @param nx If set to true, the key will only be set if it does not already exist.\n * @param px Set the specified expire time, in milliseconds.\n */\n public abstract set(\n key: string,\n value: string,\n nx?: boolean,\n px?: number,\n ): Promise<string>;\n\n /**\n * Remove the specified keys.\n *\n * @param keys The keys to delete.\n */\n public abstract del(...keys: string[]): Promise<void>;\n}\n","import { TopicProvider } from \"alepha/topic\";\n\nexport abstract class LockTopicProvider extends TopicProvider {}\n","import {\n $env,\n $inject,\n type AsyncFn,\n createDescriptor,\n Descriptor,\n KIND,\n type Static,\n t,\n} from \"alepha\";\nimport {\n type DateTime,\n DateTimeProvider,\n type DurationLike,\n} from \"alepha/datetime\";\nimport { $logger } from \"alepha/logger\";\nimport { $topic, TopicTimeoutError } from \"alepha/topic\";\nimport { LockProvider } from \"../providers/LockProvider.ts\";\nimport { LockTopicProvider } from \"../providers/LockTopicProvider.ts\";\n\n/**\n * Creates a distributed lock descriptor for ensuring single-instance execution across processes.\n *\n * Prevents multiple instances of the same operation from running simultaneously, essential for\n * maintaining data consistency and preventing race conditions in distributed applications.\n *\n * **Key Features**\n * - Distributed coordination across multiple processes, servers, and containers\n * - Automatic expiration to prevent deadlocks\n * - Configurable wait behavior (blocking vs. non-blocking)\n * - Optional grace periods for lock extension after completion\n * - Dynamic or static lock keys for fine-grained control\n *\n * **Common Use Cases**\n * - Database migrations and scheduled jobs\n * - File processing and batch operations\n * - Critical section protection and resource initialization\n *\n * @example\n * ```ts\n * class TaskService {\n * // Basic scheduled task - only one server executes\n * dailyReport = $lock({\n * handler: async () => {\n * const report = await this.generateDailyReport();\n * await this.sendReportToManagement(report);\n * }\n * });\n *\n * // Migration with wait - all instances wait for completion\n * migration = $lock({\n * wait: true,\n * maxDuration: [10, \"minutes\"],\n * handler: async (version: string) => {\n * await this.runMigrationScripts(version);\n * }\n * });\n *\n * // Dynamic lock keys for per-resource locking\n * processFile = $lock({\n * name: (fileId: string) => `file-processing:${fileId}`,\n * gracePeriod: [5, \"minutes\"],\n * handler: async (fileId: string) => {\n * await this.processFileData(fileId);\n * }\n * });\n * }\n * ```\n */\nexport const $lock = <TFunc extends AsyncFn>(\n options: LockDescriptorOptions<TFunc>,\n): LockDescriptor<TFunc> => {\n return createDescriptor(LockDescriptor<TFunc>, options);\n};\n\n// ---------------------------------------------------------------------------------------------------------------------\n\nexport interface LockDescriptorOptions<TFunc extends AsyncFn> {\n /**\n * The function to execute when the lock is successfully acquired.\n *\n * This function:\n * - Only executes on the instance that successfully acquires the lock\n * - Has exclusive access to the protected resource during execution\n * - Should contain the critical section logic that must not run concurrently\n * - Can be async and perform any operations needed\n * - Will automatically release the lock upon completion or error\n * - Has access to the full Alepha dependency injection container\n *\n * **Handler Design Guidelines**:\n * - Keep critical sections as short as possible to minimize lock contention\n * - Include proper error handling to ensure locks are released\n * - Use timeouts for external operations to prevent deadlocks\n * - Log important operations for debugging and monitoring\n * - Consider idempotency for handlers that might be retried\n *\n * @param ...args - The arguments passed to the lock execution\n * @returns Promise that resolves when the protected operation is complete\n *\n * @example\n * ```ts\n * handler: async (batchId: string) => {\n * console.log(`Processing batch ${batchId} - only one instance will run this`);\n *\n * const batch = await this.getBatchData(batchId);\n * const results = await this.processBatchItems(batch.items);\n * await this.saveBatchResults(batchId, results);\n *\n * console.log(`Batch ${batchId} completed successfully`);\n * }\n * ```\n */\n handler: TFunc;\n\n /**\n * Whether the lock should wait for other instances to complete before giving up.\n *\n * **wait = false (default)**:\n * - Non-blocking behavior - if lock is held, immediately return without executing\n * - Perfect for scheduled tasks where you only want one execution per trigger\n * - Use when multiple triggers are acceptable but concurrent execution is not\n * - Examples: periodic cleanup, cron jobs, background maintenance\n *\n * **wait = true**:\n * - Blocking behavior - wait for the current lock holder to finish\n * - All instances will eventually execute (one after another)\n * - Perfect for initialization tasks where all instances need the work completed\n * - Examples: database migrations, cache warming, resource initialization\n *\n * **Trade-offs**:\n * - Non-waiting: Better performance, may miss executions if timing is off\n * - Waiting: Guaranteed execution order, slower overall throughput\n *\n * @default false\n *\n * @example\n * ```ts\n * // Scheduled task - don't wait, just skip if already running\n * scheduledCleanup = $lock({\n * wait: false, // Skip if cleanup already running\n * handler: async () => { } // perform cleanup\n * });\n *\n * // Migration - wait for completion before proceeding\n * migration = $lock({\n * wait: true, // All instances wait for migration to complete\n * handler: async () => { } // perform migration\n * });\n * ```\n */\n wait?: boolean;\n\n /**\n * The unique identifier for the lock.\n *\n * Can be either:\n * - **Static string**: A fixed identifier for the lock\n * - **Dynamic function**: A function that generates the lock key based on arguments\n *\n * **Dynamic Lock Keys**:\n * - Enable per-resource locking (e.g., per-user, per-file, per-product)\n * - Allow fine-grained concurrency control\n * - Prevent unnecessary blocking between unrelated operations\n *\n * **Key Design Guidelines**:\n * - Use descriptive names that indicate the protected resource\n * - Include relevant identifiers for dynamic keys\n * - Keep keys reasonably short but unique\n * - Consider using hierarchical naming (e.g., \"service:operation:resource\")\n *\n * If not provided, defaults to `{serviceName}:{propertyKey}`.\n *\n * @example \"user-migration\"\n * @example \"daily-report-generation\"\n * @example (userId: string) => `user-profile-update:${userId}`\n * @example (fileId: string, operation: string) => `file-${operation}:${fileId}`\n *\n * @example\n * ```ts\n * // Static lock key - all instances compete for the same lock\n * globalCleanup = $lock({\n * name: \"system-cleanup\",\n * handler: async () => { } // perform cleanup\n * });\n *\n * // Dynamic lock key - per-user locks, users don't block each other\n * updateUserProfile = $lock({\n * name: (userId: string) => `user-update:${userId}`,\n * handler: async (userId: string, data: UserData) => {\n * // Only one update per user at a time, but different users can update concurrently\n * }\n * });\n * ```\n */\n name?: string | ((...args: Parameters<TFunc>) => string);\n\n /**\n * Maximum duration the lock can be held before it expires automatically.\n *\n * This prevents deadlocks when a process dies while holding a lock or when\n * operations take longer than expected. The lock will be automatically released\n * after this duration, allowing other instances to proceed.\n *\n * **Duration Guidelines**:\n * - Set based on expected operation duration plus safety margin\n * - Too short: Operations may be interrupted by early expiration\n * - Too long: Failed processes block others for extended periods\n * - Consider worst-case scenarios and external dependency timeouts\n *\n * **Typical Values**:\n * - Quick operations: 30 seconds - 2 minutes\n * - Database operations: 5 - 15 minutes\n * - File processing: 10 - 30 minutes\n * - Large migrations: 30 minutes - 2 hours\n *\n * @default [5, \"minutes\"]\n *\n * @example [30, \"seconds\"] // Quick operations\n * @example [10, \"minutes\"] // Database migrations\n * @example [1, \"hour\"] // Long-running batch jobs\n *\n * @example\n * ```ts\n * quickTask = $lock({\n * maxDuration: [2, \"minutes\"], // Quick timeout for fast operations\n * handler: async () => { } // perform quick task\n * });\n *\n * heavyProcessing = $lock({\n * maxDuration: [30, \"minutes\"], // Longer timeout for heavy work\n * handler: async () => { } // perform heavy processing\n * });\n * ```\n */\n maxDuration?: DurationLike;\n\n /**\n * Additional time to keep the lock active after the handler completes successfully.\n *\n * This provides a \"cooling off\" period that can be useful for:\n * - Preventing immediate re-execution of the same operation\n * - Giving time for related systems to process the results\n * - Avoiding race conditions with dependent operations\n * - Providing a buffer for cleanup operations\n *\n * Can be either:\n * - **Static duration**: Fixed grace period for all executions\n * - **Dynamic function**: Grace period determined by execution arguments\n * - **undefined**: No grace period, lock released immediately after completion\n *\n * **Grace Period Use Cases**:\n * - File processing: Prevent immediate reprocessing of uploaded files\n * - Cache updates: Allow time for cache propagation\n * - Batch operations: Prevent overlapping batch processing\n * - External API calls: Respect rate limiting requirements\n *\n * @default undefined (no grace period)\n *\n * @example [5, \"minutes\"] // Fixed 5-minute grace period\n * @example [30, \"seconds\"] // Short grace for quick operations\n * @example (userId: string) => userId.startsWith(\"premium\") ? [10, \"minutes\"] : [2, \"minutes\"]\n *\n * @example\n * ```ts\n * fileProcessor = $lock({\n * gracePeriod: [10, \"minutes\"], // Prevent reprocessing same file immediately\n * handler: async (filePath: string) => {\n * await this.processFile(filePath);\n * }\n * });\n *\n * userOperation = $lock({\n * gracePeriod: (userId: string, operation: string) => {\n * // Dynamic grace based on operation type\n * return operation === 'migration' ? [30, \"minutes\"] : [5, \"minutes\"];\n * },\n * handler: async (userId: string, operation: string) => {\n * await this.performUserOperation(userId, operation);\n * }\n * });\n * ```\n */\n gracePeriod?:\n | ((...args: Parameters<TFunc>) => DurationLike | undefined)\n | DurationLike;\n}\n\n// ---------------------------------------------------------------------------------------------------------------------\n\nconst envSchema = t.object({\n LOCK_PREFIX_KEY: t.text({ default: \"lock\" }),\n});\n\ndeclare module \"alepha\" {\n interface Env extends Partial<Static<typeof envSchema>> {}\n}\n\nexport class LockDescriptor<TFunc extends AsyncFn> extends Descriptor<\n LockDescriptorOptions<TFunc>\n> {\n protected readonly log = $logger();\n protected readonly provider = $inject(LockProvider);\n protected readonly env = $env(envSchema);\n protected readonly dateTimeProvider = $inject(DateTimeProvider);\n protected readonly id = crypto.randomUUID();\n public readonly maxDuration = this.dateTimeProvider.duration(\n this.options.maxDuration ?? [5, \"minutes\"],\n );\n\n protected readonly topicLockEnd = $topic({\n name: `${this.env.LOCK_PREFIX_KEY}:lock-end`,\n provider: LockTopicProvider,\n schema: {\n payload: t.object({\n name: t.text(),\n }),\n },\n });\n\n public async run(...args: Parameters<TFunc>): Promise<void> {\n const key = this.key(...args);\n const handler = this.options.handler;\n\n const lock = await this.lock(key);\n if (lock.endedAt) {\n return;\n }\n\n if (lock.id !== this.id) {\n if (this.options.wait) {\n try {\n await this.wait(key, this.maxDuration);\n } catch (error) {\n if (error instanceof TopicTimeoutError) {\n this.log.warn(\n `Lock timeout for '${key}' has been reached. Retry...`,\n );\n await this.run(...args);\n } else {\n throw error;\n }\n }\n }\n\n return;\n }\n\n this.log.debug(`Lock '${key}' ...`);\n\n try {\n await handler(...args);\n } finally {\n await this.topicLockEnd.publish({\n name: key,\n });\n\n await this.setGracePeriod(key, lock, ...args);\n\n this.log.debug(`Lock '${key}' OK`);\n }\n }\n\n /**\n * Set the lock for the given key.\n */\n protected async lock(key: string): Promise<LockResult> {\n const value = await this.provider.set(\n key,\n `${this.id},${this.dateTimeProvider.nowISOString()}`,\n true,\n this.maxDuration.as(\"milliseconds\"),\n );\n\n return this.parse(value);\n }\n\n protected async setGracePeriod(\n key: string,\n lock: LockResult,\n ...args: Parameters<TFunc>\n ): Promise<void> {\n const gracePeriod = this.options.gracePeriod\n ? this.dateTimeProvider.isDurationLike(this.options.gracePeriod)\n ? this.options.gracePeriod\n : this.options.gracePeriod(...args)\n : undefined;\n\n if (gracePeriod) {\n await this.provider.set(\n key,\n `${this.id},${lock.createdAt.toISOString()},${this.dateTimeProvider.nowISOString()}`,\n false,\n this.dateTimeProvider.duration(gracePeriod).as(\"milliseconds\"),\n );\n } else {\n await this.provider.del(key);\n }\n }\n\n protected async wait(key: string, maxDuration: DurationLike): Promise<void> {\n this.log.debug(`Wait for lock '${key}' ...`);\n\n await this.topicLockEnd.wait({\n filter: (message) => message.payload.name === key,\n timeout: maxDuration,\n });\n\n this.log.debug(`Wait for lock '${key}' OK`);\n }\n\n protected key(...args: Parameters<TFunc>) {\n let base = \"\";\n\n if (this.options.name) {\n if (typeof this.options.name === \"string\") {\n base = this.options.name;\n } else {\n base = this.options.name(...args);\n }\n } else {\n base = `${this.config.service.name}:${this.config.propertyKey}`;\n }\n\n return `${this.env.LOCK_PREFIX_KEY}:${base}`;\n }\n\n protected parse(value: string): LockResult {\n const [id, createdAtStr, endedAtStr] = value.split(\",\");\n const createdAt = this.dateTimeProvider.of(createdAtStr);\n const endedAt = endedAtStr\n ? this.dateTimeProvider.of(endedAtStr)\n : undefined;\n\n return {\n id,\n createdAt,\n endedAt,\n };\n }\n}\n\n$lock[KIND] = LockDescriptor;\n\nexport interface LockResult {\n id: string;\n createdAt: DateTime;\n endedAt?: DateTime;\n response?: string;\n}\n","import { $inject } from \"alepha\";\nimport { DateTimeProvider, type Timeout } from \"alepha/datetime\";\nimport { $logger } from \"alepha/logger\";\nimport type { LockProvider } from \"./LockProvider.ts\";\n\n/**\n * A simple in-memory store provider.\n */\nexport class MemoryLockProvider implements LockProvider {\n protected readonly dateTimeProvider = $inject(DateTimeProvider);\n protected readonly log = $logger();\n\n /**\n * The in-memory store.\n */\n protected store: Record<string, string> = {};\n\n /**\n * Timeouts used to expire keys.\n */\n protected storeTimeout: Record<string, Timeout> = {};\n\n public async set(\n key: string,\n value: string,\n nx?: boolean,\n px?: number,\n ): Promise<string> {\n if (nx && this.store[key] != null) {\n return this.store[key];\n }\n\n if (px) {\n this.ttl(key, px);\n }\n\n this.store[key] = value;\n\n return this.store[key];\n }\n\n public async del(...keys: string[]): Promise<void> {\n for (const key of keys) {\n delete this.store[key];\n if (this.storeTimeout[key] != null) {\n this.storeTimeout[key].clear();\n delete this.storeTimeout[key];\n }\n }\n }\n\n private ttl(key: string, ms: number): void {\n if (this.storeTimeout[key] != null) {\n this.storeTimeout[key].clear();\n delete this.storeTimeout[key];\n }\n\n this.storeTimeout[key] = this.dateTimeProvider.createTimeout(() => {\n delete this.store[key];\n delete this.storeTimeout[key];\n }, ms);\n }\n}\n","import { $module } from \"alepha\";\nimport { MemoryTopicProvider } from \"alepha/topic\";\nimport { $lock } from \"./descriptors/$lock.ts\";\nimport { LockProvider } from \"./providers/LockProvider.ts\";\nimport { LockTopicProvider } from \"./providers/LockTopicProvider.ts\";\nimport { MemoryLockProvider } from \"./providers/MemoryLockProvider.ts\";\n\n// ---------------------------------------------------------------------------------------------------------------------\n\nexport * from \"./descriptors/$lock.ts\";\nexport * from \"./providers/LockProvider.ts\";\nexport * from \"./providers/LockTopicProvider.ts\";\nexport * from \"./providers/MemoryLockProvider.ts\";\n\n// ---------------------------------------------------------------------------------------------------------------------\n\n/**\n * Lock a resource for a certain period of time.\n *\n * This module provides a memory implementation of the lock provider.\n * You probably want to use an implementation like RedisLockProvider for distributed systems.\n *\n * @see {@link $lock}\n * @module alepha.lock\n */\nexport const AlephaLock = $module({\n name: \"alepha.lock\",\n descriptors: [$lock],\n services: [LockProvider, MemoryLockProvider, LockTopicProvider],\n register: (alepha) =>\n alepha\n .with({\n optional: true,\n provide: LockTopicProvider,\n use: MemoryTopicProvider,\n })\n .with({\n optional: true,\n provide: LockProvider,\n use: MemoryLockProvider,\n }),\n});\n"],"mappings":"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"}
@@ -1,361 +0,0 @@
1
- import * as alepha1 from "alepha";
2
- import { AsyncFn, Descriptor, KIND, Static } from "alepha";
3
- import * as alepha_logger0 from "alepha/logger";
4
- import * as dayjs_plugin_duration_js0 from "dayjs/plugin/duration.js";
5
- import * as alepha_topic0 from "alepha/topic";
6
- import { TopicProvider } from "alepha/topic";
7
- import { DateTime, DateTimeProvider, DurationLike, Timeout } from "alepha/datetime";
8
-
9
- //#region src/lock/providers/LockProvider.d.ts
10
- /**
11
- * Store Provider Interface
12
- */
13
- declare abstract class LockProvider {
14
- /**
15
- * Set the string value of a key.
16
- *
17
- * @param key The key of the value to set.
18
- * @param value The value to set.
19
- * @param nx If set to true, the key will only be set if it does not already exist.
20
- * @param px Set the specified expire time, in milliseconds.
21
- */
22
- abstract set(key: string, value: string, nx?: boolean, px?: number): Promise<string>;
23
- /**
24
- * Remove the specified keys.
25
- *
26
- * @param keys The keys to delete.
27
- */
28
- abstract del(...keys: string[]): Promise<void>;
29
- }
30
- //#endregion
31
- //#region src/lock/descriptors/$lock.d.ts
32
- /**
33
- * Creates a distributed lock descriptor for ensuring single-instance execution across processes.
34
- *
35
- * Prevents multiple instances of the same operation from running simultaneously, essential for
36
- * maintaining data consistency and preventing race conditions in distributed applications.
37
- *
38
- * **Key Features**
39
- * - Distributed coordination across multiple processes, servers, and containers
40
- * - Automatic expiration to prevent deadlocks
41
- * - Configurable wait behavior (blocking vs. non-blocking)
42
- * - Optional grace periods for lock extension after completion
43
- * - Dynamic or static lock keys for fine-grained control
44
- *
45
- * **Common Use Cases**
46
- * - Database migrations and scheduled jobs
47
- * - File processing and batch operations
48
- * - Critical section protection and resource initialization
49
- *
50
- * @example
51
- * ```ts
52
- * class TaskService {
53
- * // Basic scheduled task - only one server executes
54
- * dailyReport = $lock({
55
- * handler: async () => {
56
- * const report = await this.generateDailyReport();
57
- * await this.sendReportToManagement(report);
58
- * }
59
- * });
60
- *
61
- * // Migration with wait - all instances wait for completion
62
- * migration = $lock({
63
- * wait: true,
64
- * maxDuration: [10, "minutes"],
65
- * handler: async (version: string) => {
66
- * await this.runMigrationScripts(version);
67
- * }
68
- * });
69
- *
70
- * // Dynamic lock keys for per-resource locking
71
- * processFile = $lock({
72
- * name: (fileId: string) => `file-processing:${fileId}`,
73
- * gracePeriod: [5, "minutes"],
74
- * handler: async (fileId: string) => {
75
- * await this.processFileData(fileId);
76
- * }
77
- * });
78
- * }
79
- * ```
80
- */
81
- declare const $lock: {
82
- <TFunc extends AsyncFn>(options: LockDescriptorOptions<TFunc>): LockDescriptor<TFunc>;
83
- [KIND]: typeof LockDescriptor;
84
- };
85
- interface LockDescriptorOptions<TFunc extends AsyncFn> {
86
- /**
87
- * The function to execute when the lock is successfully acquired.
88
- *
89
- * This function:
90
- * - Only executes on the instance that successfully acquires the lock
91
- * - Has exclusive access to the protected resource during execution
92
- * - Should contain the critical section logic that must not run concurrently
93
- * - Can be async and perform any operations needed
94
- * - Will automatically release the lock upon completion or error
95
- * - Has access to the full Alepha dependency injection container
96
- *
97
- * **Handler Design Guidelines**:
98
- * - Keep critical sections as short as possible to minimize lock contention
99
- * - Include proper error handling to ensure locks are released
100
- * - Use timeouts for external operations to prevent deadlocks
101
- * - Log important operations for debugging and monitoring
102
- * - Consider idempotency for handlers that might be retried
103
- *
104
- * @param ...args - The arguments passed to the lock execution
105
- * @returns Promise that resolves when the protected operation is complete
106
- *
107
- * @example
108
- * ```ts
109
- * handler: async (batchId: string) => {
110
- * console.log(`Processing batch ${batchId} - only one instance will run this`);
111
- *
112
- * const batch = await this.getBatchData(batchId);
113
- * const results = await this.processBatchItems(batch.items);
114
- * await this.saveBatchResults(batchId, results);
115
- *
116
- * console.log(`Batch ${batchId} completed successfully`);
117
- * }
118
- * ```
119
- */
120
- handler: TFunc;
121
- /**
122
- * Whether the lock should wait for other instances to complete before giving up.
123
- *
124
- * **wait = false (default)**:
125
- * - Non-blocking behavior - if lock is held, immediately return without executing
126
- * - Perfect for scheduled tasks where you only want one execution per trigger
127
- * - Use when multiple triggers are acceptable but concurrent execution is not
128
- * - Examples: periodic cleanup, cron jobs, background maintenance
129
- *
130
- * **wait = true**:
131
- * - Blocking behavior - wait for the current lock holder to finish
132
- * - All instances will eventually execute (one after another)
133
- * - Perfect for initialization tasks where all instances need the work completed
134
- * - Examples: database migrations, cache warming, resource initialization
135
- *
136
- * **Trade-offs**:
137
- * - Non-waiting: Better performance, may miss executions if timing is off
138
- * - Waiting: Guaranteed execution order, slower overall throughput
139
- *
140
- * @default false
141
- *
142
- * @example
143
- * ```ts
144
- * // Scheduled task - don't wait, just skip if already running
145
- * scheduledCleanup = $lock({
146
- * wait: false, // Skip if cleanup already running
147
- * handler: async () => { } // perform cleanup
148
- * });
149
- *
150
- * // Migration - wait for completion before proceeding
151
- * migration = $lock({
152
- * wait: true, // All instances wait for migration to complete
153
- * handler: async () => { } // perform migration
154
- * });
155
- * ```
156
- */
157
- wait?: boolean;
158
- /**
159
- * The unique identifier for the lock.
160
- *
161
- * Can be either:
162
- * - **Static string**: A fixed identifier for the lock
163
- * - **Dynamic function**: A function that generates the lock key based on arguments
164
- *
165
- * **Dynamic Lock Keys**:
166
- * - Enable per-resource locking (e.g., per-user, per-file, per-product)
167
- * - Allow fine-grained concurrency control
168
- * - Prevent unnecessary blocking between unrelated operations
169
- *
170
- * **Key Design Guidelines**:
171
- * - Use descriptive names that indicate the protected resource
172
- * - Include relevant identifiers for dynamic keys
173
- * - Keep keys reasonably short but unique
174
- * - Consider using hierarchical naming (e.g., "service:operation:resource")
175
- *
176
- * If not provided, defaults to `{serviceName}:{propertyKey}`.
177
- *
178
- * @example "user-migration"
179
- * @example "daily-report-generation"
180
- * @example (userId: string) => `user-profile-update:${userId}`
181
- * @example (fileId: string, operation: string) => `file-${operation}:${fileId}`
182
- *
183
- * @example
184
- * ```ts
185
- * // Static lock key - all instances compete for the same lock
186
- * globalCleanup = $lock({
187
- * name: "system-cleanup",
188
- * handler: async () => { } // perform cleanup
189
- * });
190
- *
191
- * // Dynamic lock key - per-user locks, users don't block each other
192
- * updateUserProfile = $lock({
193
- * name: (userId: string) => `user-update:${userId}`,
194
- * handler: async (userId: string, data: UserData) => {
195
- * // Only one update per user at a time, but different users can update concurrently
196
- * }
197
- * });
198
- * ```
199
- */
200
- name?: string | ((...args: Parameters<TFunc>) => string);
201
- /**
202
- * Maximum duration the lock can be held before it expires automatically.
203
- *
204
- * This prevents deadlocks when a process dies while holding a lock or when
205
- * operations take longer than expected. The lock will be automatically released
206
- * after this duration, allowing other instances to proceed.
207
- *
208
- * **Duration Guidelines**:
209
- * - Set based on expected operation duration plus safety margin
210
- * - Too short: Operations may be interrupted by early expiration
211
- * - Too long: Failed processes block others for extended periods
212
- * - Consider worst-case scenarios and external dependency timeouts
213
- *
214
- * **Typical Values**:
215
- * - Quick operations: 30 seconds - 2 minutes
216
- * - Database operations: 5 - 15 minutes
217
- * - File processing: 10 - 30 minutes
218
- * - Large migrations: 30 minutes - 2 hours
219
- *
220
- * @default [5, "minutes"]
221
- *
222
- * @example [30, "seconds"] // Quick operations
223
- * @example [10, "minutes"] // Database migrations
224
- * @example [1, "hour"] // Long-running batch jobs
225
- *
226
- * @example
227
- * ```ts
228
- * quickTask = $lock({
229
- * maxDuration: [2, "minutes"], // Quick timeout for fast operations
230
- * handler: async () => { } // perform quick task
231
- * });
232
- *
233
- * heavyProcessing = $lock({
234
- * maxDuration: [30, "minutes"], // Longer timeout for heavy work
235
- * handler: async () => { } // perform heavy processing
236
- * });
237
- * ```
238
- */
239
- maxDuration?: DurationLike;
240
- /**
241
- * Additional time to keep the lock active after the handler completes successfully.
242
- *
243
- * This provides a "cooling off" period that can be useful for:
244
- * - Preventing immediate re-execution of the same operation
245
- * - Giving time for related systems to process the results
246
- * - Avoiding race conditions with dependent operations
247
- * - Providing a buffer for cleanup operations
248
- *
249
- * Can be either:
250
- * - **Static duration**: Fixed grace period for all executions
251
- * - **Dynamic function**: Grace period determined by execution arguments
252
- * - **undefined**: No grace period, lock released immediately after completion
253
- *
254
- * **Grace Period Use Cases**:
255
- * - File processing: Prevent immediate reprocessing of uploaded files
256
- * - Cache updates: Allow time for cache propagation
257
- * - Batch operations: Prevent overlapping batch processing
258
- * - External API calls: Respect rate limiting requirements
259
- *
260
- * @default undefined (no grace period)
261
- *
262
- * @example [5, "minutes"] // Fixed 5-minute grace period
263
- * @example [30, "seconds"] // Short grace for quick operations
264
- * @example (userId: string) => userId.startsWith("premium") ? [10, "minutes"] : [2, "minutes"]
265
- *
266
- * @example
267
- * ```ts
268
- * fileProcessor = $lock({
269
- * gracePeriod: [10, "minutes"], // Prevent reprocessing same file immediately
270
- * handler: async (filePath: string) => {
271
- * await this.processFile(filePath);
272
- * }
273
- * });
274
- *
275
- * userOperation = $lock({
276
- * gracePeriod: (userId: string, operation: string) => {
277
- * // Dynamic grace based on operation type
278
- * return operation === 'migration' ? [30, "minutes"] : [5, "minutes"];
279
- * },
280
- * handler: async (userId: string, operation: string) => {
281
- * await this.performUserOperation(userId, operation);
282
- * }
283
- * });
284
- * ```
285
- */
286
- gracePeriod?: ((...args: Parameters<TFunc>) => DurationLike | undefined) | DurationLike;
287
- }
288
- declare const envSchema: alepha1.TObject<{
289
- LOCK_PREFIX_KEY: alepha1.TString;
290
- }>;
291
- declare module "alepha" {
292
- interface Env extends Partial<Static<typeof envSchema>> {}
293
- }
294
- declare class LockDescriptor<TFunc extends AsyncFn> extends Descriptor<LockDescriptorOptions<TFunc>> {
295
- protected readonly log: alepha_logger0.Logger;
296
- protected readonly provider: LockProvider;
297
- protected readonly env: {
298
- LOCK_PREFIX_KEY: string;
299
- };
300
- protected readonly dateTimeProvider: DateTimeProvider;
301
- protected readonly id: `${string}-${string}-${string}-${string}-${string}`;
302
- readonly maxDuration: dayjs_plugin_duration_js0.Duration;
303
- protected readonly topicLockEnd: alepha_topic0.TopicDescriptor<{
304
- payload: alepha1.TObject<{
305
- name: alepha1.TString;
306
- }>;
307
- }>;
308
- run(...args: Parameters<TFunc>): Promise<void>;
309
- /**
310
- * Set the lock for the given key.
311
- */
312
- protected lock(key: string): Promise<LockResult>;
313
- protected setGracePeriod(key: string, lock: LockResult, ...args: Parameters<TFunc>): Promise<void>;
314
- protected wait(key: string, maxDuration: DurationLike): Promise<void>;
315
- protected key(...args: Parameters<TFunc>): string;
316
- protected parse(value: string): LockResult;
317
- }
318
- interface LockResult {
319
- id: string;
320
- createdAt: DateTime;
321
- endedAt?: DateTime;
322
- response?: string;
323
- }
324
- //#endregion
325
- //#region src/lock/providers/LockTopicProvider.d.ts
326
- declare abstract class LockTopicProvider extends TopicProvider {}
327
- //#endregion
328
- //#region src/lock/providers/MemoryLockProvider.d.ts
329
- /**
330
- * A simple in-memory store provider.
331
- */
332
- declare class MemoryLockProvider implements LockProvider {
333
- protected readonly dateTimeProvider: DateTimeProvider;
334
- protected readonly log: alepha_logger0.Logger;
335
- /**
336
- * The in-memory store.
337
- */
338
- protected store: Record<string, string>;
339
- /**
340
- * Timeouts used to expire keys.
341
- */
342
- protected storeTimeout: Record<string, Timeout>;
343
- set(key: string, value: string, nx?: boolean, px?: number): Promise<string>;
344
- del(...keys: string[]): Promise<void>;
345
- private ttl;
346
- }
347
- //#endregion
348
- //#region src/lock/index.d.ts
349
- /**
350
- * Lock a resource for a certain period of time.
351
- *
352
- * This module provides a memory implementation of the lock provider.
353
- * You probably want to use an implementation like RedisLockProvider for distributed systems.
354
- *
355
- * @see {@link $lock}
356
- * @module alepha.lock
357
- */
358
- declare const AlephaLock: alepha1.Service<alepha1.Module>;
359
- //#endregion
360
- export { $lock, AlephaLock, LockDescriptor, LockDescriptorOptions, LockProvider, LockResult, LockTopicProvider, MemoryLockProvider };
361
- //# sourceMappingURL=index.d.cts.map