awaitly 1.35.0 → 2.0.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 (241) hide show
  1. package/dist/{duration.d.ts → di-BDlT7InM.d.cts} +15 -1
  2. package/dist/{duration.d.cts → di-BbFFfO8y.d.ts} +15 -1
  3. package/dist/errors-DtXvrCiO.d.cts +708 -0
  4. package/dist/errors-DtXvrCiO.d.ts +708 -0
  5. package/dist/index.cjs +4594 -1
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.d.cts +1970 -141
  8. package/dist/index.d.ts +1970 -141
  9. package/dist/index.js +4398 -1
  10. package/dist/index.js.map +1 -1
  11. package/dist/result.cjs +641 -1
  12. package/dist/result.cjs.map +1 -1
  13. package/dist/result.d.cts +29 -4
  14. package/dist/result.d.ts +29 -4
  15. package/dist/result.js +561 -1
  16. package/dist/result.js.map +1 -1
  17. package/dist/testing.cjs +4202 -8
  18. package/dist/testing.cjs.map +1 -1
  19. package/dist/testing.d.cts +2 -6
  20. package/dist/testing.d.ts +2 -6
  21. package/dist/testing.js +4154 -8
  22. package/dist/testing.js.map +1 -1
  23. package/dist/{run-entry-DGs0tySr.d.cts → types-B8NfNRGX.d.ts} +1078 -1502
  24. package/dist/{run-entry-BOuNyVoO.d.ts → types-BZ2f4MRR.d.cts} +1078 -1502
  25. package/dist/workflow.cjs +7096 -6
  26. package/dist/workflow.cjs.map +1 -1
  27. package/dist/workflow.d.cts +3346 -22
  28. package/dist/workflow.d.ts +3346 -22
  29. package/dist/workflow.js +6929 -6
  30. package/dist/workflow.js.map +1 -1
  31. package/package.json +3 -168
  32. package/dist/adapters.cjs +0 -7
  33. package/dist/adapters.cjs.map +0 -1
  34. package/dist/adapters.d.cts +0 -179
  35. package/dist/adapters.d.ts +0 -179
  36. package/dist/adapters.js +0 -7
  37. package/dist/adapters.js.map +0 -1
  38. package/dist/batch.cjs +0 -7
  39. package/dist/batch.cjs.map +0 -1
  40. package/dist/batch.d.cts +0 -200
  41. package/dist/batch.d.ts +0 -200
  42. package/dist/batch.js +0 -7
  43. package/dist/batch.js.map +0 -1
  44. package/dist/bind-deps.cjs +0 -2
  45. package/dist/bind-deps.cjs.map +0 -1
  46. package/dist/bind-deps.d.cts +0 -28
  47. package/dist/bind-deps.d.ts +0 -28
  48. package/dist/bind-deps.js +0 -2
  49. package/dist/bind-deps.js.map +0 -1
  50. package/dist/cache.cjs +0 -2
  51. package/dist/cache.cjs.map +0 -1
  52. package/dist/cache.d.cts +0 -269
  53. package/dist/cache.d.ts +0 -269
  54. package/dist/cache.js +0 -2
  55. package/dist/cache.js.map +0 -1
  56. package/dist/circuit-breaker.cjs +0 -7
  57. package/dist/circuit-breaker.cjs.map +0 -1
  58. package/dist/circuit-breaker.d.cts +0 -211
  59. package/dist/circuit-breaker.d.ts +0 -211
  60. package/dist/circuit-breaker.js +0 -7
  61. package/dist/circuit-breaker.js.map +0 -1
  62. package/dist/conditional.cjs +0 -2
  63. package/dist/conditional.cjs.map +0 -1
  64. package/dist/conditional.d.cts +0 -252
  65. package/dist/conditional.d.ts +0 -252
  66. package/dist/conditional.js +0 -2
  67. package/dist/conditional.js.map +0 -1
  68. package/dist/core.cjs +0 -7
  69. package/dist/core.cjs.map +0 -1
  70. package/dist/core.d.cts +0 -5
  71. package/dist/core.d.ts +0 -5
  72. package/dist/core.js +0 -7
  73. package/dist/core.js.map +0 -1
  74. package/dist/di-By77n4Fa.d.ts +0 -15
  75. package/dist/di-OJfsohf-.d.cts +0 -15
  76. package/dist/diagnostics.cjs +0 -8
  77. package/dist/diagnostics.cjs.map +0 -1
  78. package/dist/diagnostics.d.cts +0 -68
  79. package/dist/diagnostics.d.ts +0 -68
  80. package/dist/diagnostics.js +0 -8
  81. package/dist/diagnostics.js.map +0 -1
  82. package/dist/durable.cjs +0 -11
  83. package/dist/durable.cjs.map +0 -1
  84. package/dist/durable.d.cts +0 -9
  85. package/dist/durable.d.ts +0 -9
  86. package/dist/durable.js +0 -11
  87. package/dist/durable.js.map +0 -1
  88. package/dist/duration.cjs +0 -2
  89. package/dist/duration.cjs.map +0 -1
  90. package/dist/duration.js +0 -2
  91. package/dist/duration.js.map +0 -1
  92. package/dist/engine.cjs +0 -11
  93. package/dist/engine.cjs.map +0 -1
  94. package/dist/engine.d.cts +0 -115
  95. package/dist/engine.d.ts +0 -115
  96. package/dist/engine.js +0 -11
  97. package/dist/engine.js.map +0 -1
  98. package/dist/errors.cjs +0 -2
  99. package/dist/errors.cjs.map +0 -1
  100. package/dist/errors.d.cts +0 -361
  101. package/dist/errors.d.ts +0 -361
  102. package/dist/errors.js +0 -2
  103. package/dist/errors.js.map +0 -1
  104. package/dist/fetch.cjs +0 -7
  105. package/dist/fetch.cjs.map +0 -1
  106. package/dist/fetch.d.cts +0 -86
  107. package/dist/fetch.d.ts +0 -86
  108. package/dist/fetch.js +0 -7
  109. package/dist/fetch.js.map +0 -1
  110. package/dist/flow.cjs +0 -7
  111. package/dist/flow.cjs.map +0 -1
  112. package/dist/flow.d.cts +0 -163
  113. package/dist/flow.d.ts +0 -163
  114. package/dist/flow.js +0 -7
  115. package/dist/flow.js.map +0 -1
  116. package/dist/functional.cjs +0 -2
  117. package/dist/functional.cjs.map +0 -1
  118. package/dist/functional.d.cts +0 -444
  119. package/dist/functional.d.ts +0 -444
  120. package/dist/functional.js +0 -2
  121. package/dist/functional.js.map +0 -1
  122. package/dist/guards-4sV7mTqj.d.cts +0 -72
  123. package/dist/guards-BIX05ALH.d.ts +0 -72
  124. package/dist/hitl-DFn4Xa_l.d.cts +0 -468
  125. package/dist/hitl-DU5VpKq7.d.ts +0 -468
  126. package/dist/hitl.cjs +0 -7
  127. package/dist/hitl.cjs.map +0 -1
  128. package/dist/hitl.d.cts +0 -442
  129. package/dist/hitl.d.ts +0 -442
  130. package/dist/hitl.js +0 -7
  131. package/dist/hitl.js.map +0 -1
  132. package/dist/index-CnvBryQB.d.ts +0 -417
  133. package/dist/index-DEZEf8Fs.d.cts +0 -417
  134. package/dist/match-entry-DjI2bLpD.d.cts +0 -209
  135. package/dist/match-entry-DjI2bLpD.d.ts +0 -209
  136. package/dist/match.cjs +0 -2
  137. package/dist/match.cjs.map +0 -1
  138. package/dist/match.d.cts +0 -1
  139. package/dist/match.d.ts +0 -1
  140. package/dist/match.js +0 -2
  141. package/dist/match.js.map +0 -1
  142. package/dist/otel.cjs +0 -2
  143. package/dist/otel.cjs.map +0 -1
  144. package/dist/otel.d.cts +0 -188
  145. package/dist/otel.d.ts +0 -188
  146. package/dist/otel.js +0 -2
  147. package/dist/otel.js.map +0 -1
  148. package/dist/persistence-entry-B-8PjnSR.d.cts +0 -831
  149. package/dist/persistence-entry-D8zRkLiT.d.ts +0 -831
  150. package/dist/persistence.cjs +0 -2
  151. package/dist/persistence.cjs.map +0 -1
  152. package/dist/persistence.d.cts +0 -7
  153. package/dist/persistence.d.ts +0 -7
  154. package/dist/persistence.js +0 -2
  155. package/dist/persistence.js.map +0 -1
  156. package/dist/policies.cjs +0 -2
  157. package/dist/policies.cjs.map +0 -1
  158. package/dist/policies.d.cts +0 -379
  159. package/dist/policies.d.ts +0 -379
  160. package/dist/policies.js +0 -2
  161. package/dist/policies.js.map +0 -1
  162. package/dist/ratelimit.cjs +0 -7
  163. package/dist/ratelimit.cjs.map +0 -1
  164. package/dist/ratelimit.d.cts +0 -458
  165. package/dist/ratelimit.d.ts +0 -458
  166. package/dist/ratelimit.js +0 -7
  167. package/dist/ratelimit.js.map +0 -1
  168. package/dist/reliability.cjs +0 -11
  169. package/dist/reliability.cjs.map +0 -1
  170. package/dist/reliability.d.cts +0 -11
  171. package/dist/reliability.d.ts +0 -11
  172. package/dist/reliability.js +0 -11
  173. package/dist/reliability.js.map +0 -1
  174. package/dist/resolver.cjs +0 -7
  175. package/dist/resolver.cjs.map +0 -1
  176. package/dist/resolver.d.cts +0 -68
  177. package/dist/resolver.d.ts +0 -68
  178. package/dist/resolver.js +0 -7
  179. package/dist/resolver.js.map +0 -1
  180. package/dist/resource.cjs +0 -7
  181. package/dist/resource.cjs.map +0 -1
  182. package/dist/resource.d.cts +0 -174
  183. package/dist/resource.d.ts +0 -174
  184. package/dist/resource.js +0 -7
  185. package/dist/resource.js.map +0 -1
  186. package/dist/result/retry.cjs +0 -2
  187. package/dist/result/retry.cjs.map +0 -1
  188. package/dist/result/retry.d.cts +0 -70
  189. package/dist/result/retry.d.ts +0 -70
  190. package/dist/result/retry.js +0 -2
  191. package/dist/result/retry.js.map +0 -1
  192. package/dist/retry.cjs +0 -2
  193. package/dist/retry.cjs.map +0 -1
  194. package/dist/retry.d.cts +0 -388
  195. package/dist/retry.d.ts +0 -388
  196. package/dist/retry.js +0 -2
  197. package/dist/retry.js.map +0 -1
  198. package/dist/run.cjs +0 -7
  199. package/dist/run.cjs.map +0 -1
  200. package/dist/run.d.cts +0 -4
  201. package/dist/run.d.ts +0 -4
  202. package/dist/run.js +0 -7
  203. package/dist/run.js.map +0 -1
  204. package/dist/saga.cjs +0 -11
  205. package/dist/saga.cjs.map +0 -1
  206. package/dist/saga.d.cts +0 -164
  207. package/dist/saga.d.ts +0 -164
  208. package/dist/saga.js +0 -11
  209. package/dist/saga.js.map +0 -1
  210. package/dist/singleflight.cjs +0 -2
  211. package/dist/singleflight.cjs.map +0 -1
  212. package/dist/singleflight.d.cts +0 -145
  213. package/dist/singleflight.d.ts +0 -145
  214. package/dist/singleflight.js +0 -2
  215. package/dist/singleflight.js.map +0 -1
  216. package/dist/slugs.cjs +0 -2
  217. package/dist/slugs.cjs.map +0 -1
  218. package/dist/slugs.d.cts +0 -67
  219. package/dist/slugs.d.ts +0 -67
  220. package/dist/slugs.js +0 -2
  221. package/dist/slugs.js.map +0 -1
  222. package/dist/streaming.cjs +0 -9
  223. package/dist/streaming.cjs.map +0 -1
  224. package/dist/streaming.d.cts +0 -596
  225. package/dist/streaming.d.ts +0 -596
  226. package/dist/streaming.js +0 -9
  227. package/dist/streaming.js.map +0 -1
  228. package/dist/tagged-error.cjs +0 -2
  229. package/dist/tagged-error.cjs.map +0 -1
  230. package/dist/tagged-error.d.cts +0 -275
  231. package/dist/tagged-error.d.ts +0 -275
  232. package/dist/tagged-error.js +0 -2
  233. package/dist/tagged-error.js.map +0 -1
  234. package/dist/types-BziYHFkD.d.ts +0 -323
  235. package/dist/types-C5jLEUqY.d.cts +0 -323
  236. package/dist/webhook.cjs +0 -7
  237. package/dist/webhook.cjs.map +0 -1
  238. package/dist/webhook.d.cts +0 -499
  239. package/dist/webhook.d.ts +0 -499
  240. package/dist/webhook.js +0 -7
  241. package/dist/webhook.js.map +0 -1
@@ -1,458 +0,0 @@
1
- import { R as Result, c as AsyncResult } from './run-entry-DGs0tySr.cjs';
2
- import './errors.cjs';
3
- import './tagged-error.cjs';
4
- import './slugs.cjs';
5
-
6
- /**
7
- * Rate Limiting / Concurrency Control
8
- *
9
- * Control throughput for steps that hit rate-limited APIs or shared resources.
10
- *
11
- * @example
12
- * ```typescript
13
- * import { createRateLimiter, createConcurrencyLimiter } from 'awaitly';
14
- *
15
- * // Rate limiting (requests per second)
16
- * const rateLimiter = createRateLimiter({ maxPerSecond: 10 });
17
- *
18
- * // Concurrency limiting (max concurrent)
19
- * const concurrencyLimiter = createConcurrencyLimiter({ maxConcurrent: 5 });
20
- *
21
- * const result = await workflow(async ({ step }) => {
22
- * // Wrap operations with rate limiting
23
- * const data = await rateLimiter.execute(() =>
24
- * step(() => callRateLimitedApi())
25
- * );
26
- *
27
- * // Wrap batch operations with concurrency control
28
- * const results = await concurrencyLimiter.executeAll(
29
- * ids.map(id => () => step(() => fetchItem(id)))
30
- * );
31
- *
32
- * return { data, results };
33
- * });
34
- * ```
35
- */
36
-
37
- /**
38
- * Configuration for rate limiter.
39
- */
40
- interface RateLimiterConfig {
41
- /**
42
- * Maximum operations per second.
43
- */
44
- maxPerSecond: number;
45
- /**
46
- * Burst capacity - allows brief spikes above the rate.
47
- * @default maxPerSecond * 2
48
- */
49
- burstCapacity?: number;
50
- /**
51
- * Strategy when rate limit is exceeded.
52
- * - 'wait': Wait until a slot is available (default)
53
- * - 'reject': Reject immediately with error
54
- * @default 'wait'
55
- */
56
- strategy?: "wait" | "reject";
57
- }
58
- /**
59
- * Configuration for concurrency limiter.
60
- */
61
- interface ConcurrencyLimiterConfig {
62
- /**
63
- * Maximum concurrent operations.
64
- */
65
- maxConcurrent: number;
66
- /**
67
- * Strategy when limit is reached.
68
- * - 'queue': Queue and wait (default)
69
- * - 'reject': Reject immediately
70
- * @default 'queue'
71
- */
72
- strategy?: "queue" | "reject";
73
- /**
74
- * Maximum queue size (only for 'queue' strategy).
75
- * @default Infinity
76
- */
77
- maxQueueSize?: number;
78
- }
79
- /**
80
- * Error when rate/concurrency limit is exceeded and strategy is 'reject'.
81
- */
82
- interface RateLimitExceededError {
83
- type: "RATE_LIMIT_EXCEEDED";
84
- limiterName: string;
85
- retryAfterMs?: number;
86
- }
87
- /**
88
- * Error when concurrency limit queue is full.
89
- */
90
- interface QueueFullError {
91
- type: "QUEUE_FULL";
92
- limiterName: string;
93
- queueSize: number;
94
- maxQueueSize: number;
95
- }
96
- /**
97
- * Type guard for RateLimitExceededError.
98
- */
99
- declare function isRateLimitExceededError(error: unknown): error is RateLimitExceededError;
100
- /**
101
- * Type guard for QueueFullError.
102
- */
103
- declare function isQueueFullError(error: unknown): error is QueueFullError;
104
- /**
105
- * Statistics for rate limiter.
106
- */
107
- interface RateLimiterStats {
108
- availableTokens: number;
109
- maxTokens: number;
110
- tokensPerSecond: number;
111
- waitingCount: number;
112
- }
113
- /**
114
- * Statistics for concurrency limiter.
115
- */
116
- interface ConcurrencyLimiterStats {
117
- activeCount: number;
118
- maxConcurrent: number;
119
- queueSize: number;
120
- maxQueueSize: number;
121
- }
122
- /**
123
- * Rate limiter interface.
124
- */
125
- interface RateLimiter {
126
- /**
127
- * Execute an operation with rate limiting.
128
- * @param operation - The operation to execute
129
- * @returns The operation result
130
- */
131
- execute<T>(operation: () => T | Promise<T>): Promise<T>;
132
- /**
133
- * Execute a Result-returning operation with rate limiting.
134
- */
135
- executeResult<T, E>(operation: () => Result<T, E> | AsyncResult<T, E>): AsyncResult<T, E | RateLimitExceededError>;
136
- /**
137
- * Get current statistics.
138
- */
139
- getStats(): RateLimiterStats;
140
- /**
141
- * Reset the rate limiter.
142
- */
143
- reset(): void;
144
- }
145
- /**
146
- * Create a token bucket rate limiter.
147
- *
148
- * @param name - Name for the limiter (used in errors)
149
- * @param config - Rate limiter configuration
150
- * @returns A RateLimiter instance
151
- *
152
- * @example
153
- * ```typescript
154
- * const limiter = createRateLimiter('api-calls', {
155
- * maxPerSecond: 10,
156
- * burstCapacity: 20,
157
- * });
158
- *
159
- * // In workflow
160
- * const data = await limiter.execute(() =>
161
- * step(() => callApi())
162
- * );
163
- * ```
164
- */
165
- declare function createRateLimiter(name: string, config: RateLimiterConfig): RateLimiter;
166
- /**
167
- * Concurrency limiter interface.
168
- */
169
- interface ConcurrencyLimiter {
170
- /**
171
- * Execute an operation with concurrency limiting.
172
- * @param operation - The operation to execute
173
- * @returns The operation result
174
- */
175
- execute<T>(operation: () => T | Promise<T>): Promise<T>;
176
- /**
177
- * Execute multiple operations with concurrency control.
178
- * @param operations - Array of operation factories
179
- * @returns Array of results (in order)
180
- */
181
- executeAll<T>(operations: Array<() => T | Promise<T>>): Promise<T[]>;
182
- /**
183
- * Execute a Result-returning operation with concurrency limiting.
184
- */
185
- executeResult<T, E>(operation: () => Result<T, E> | AsyncResult<T, E>): AsyncResult<T, E | QueueFullError>;
186
- /**
187
- * Get current statistics.
188
- */
189
- getStats(): ConcurrencyLimiterStats;
190
- /**
191
- * Reset the concurrency limiter.
192
- */
193
- reset(): void;
194
- }
195
- /**
196
- * Create a concurrency limiter.
197
- *
198
- * @param name - Name for the limiter (used in errors)
199
- * @param config - Concurrency limiter configuration
200
- * @returns A ConcurrencyLimiter instance
201
- *
202
- * @example
203
- * ```typescript
204
- * const limiter = createConcurrencyLimiter('db-pool', {
205
- * maxConcurrent: 10,
206
- * });
207
- *
208
- * // Execute with concurrency control
209
- * const results = await limiter.executeAll(
210
- * ids.map(id => () => fetchItem(id))
211
- * );
212
- * ```
213
- */
214
- declare function createConcurrencyLimiter(name: string, config: ConcurrencyLimiterConfig): ConcurrencyLimiter;
215
- /**
216
- * Configuration for combined rate + concurrency limiter.
217
- */
218
- interface CombinedLimiterConfig {
219
- /**
220
- * Rate limiting configuration.
221
- */
222
- rate?: RateLimiterConfig;
223
- /**
224
- * Concurrency limiting configuration.
225
- */
226
- concurrency?: ConcurrencyLimiterConfig;
227
- }
228
- /**
229
- * Create a combined rate + concurrency limiter.
230
- *
231
- * Operations are first rate-limited, then concurrency-limited.
232
- *
233
- * @param name - Name for the limiter
234
- * @param config - Combined limiter configuration
235
- * @returns An object with both limiters and a combined execute function
236
- *
237
- * @example
238
- * ```typescript
239
- * const limiter = createCombinedLimiter('api', {
240
- * rate: { maxPerSecond: 10 },
241
- * concurrency: { maxConcurrent: 5 },
242
- * });
243
- *
244
- * const result = await limiter.execute(() => callApi());
245
- * ```
246
- */
247
- declare function createCombinedLimiter(name: string, config: CombinedLimiterConfig): {
248
- rate?: RateLimiter;
249
- concurrency?: ConcurrencyLimiter;
250
- execute: <T>(operation: () => T | Promise<T>) => Promise<T>;
251
- };
252
- /**
253
- * Configuration for fixed window rate limiter.
254
- */
255
- interface FixedWindowLimiterConfig {
256
- /**
257
- * Maximum requests allowed per window.
258
- */
259
- limit: number;
260
- /**
261
- * Window duration in milliseconds.
262
- * @default 1000 (1 second)
263
- */
264
- windowMs?: number;
265
- /**
266
- * Strategy when rate limit is exceeded.
267
- * - 'wait': Wait until window resets (default)
268
- * - 'reject': Reject immediately with error
269
- * @default 'wait'
270
- */
271
- strategy?: "wait" | "reject";
272
- }
273
- /**
274
- * Statistics for fixed window rate limiter.
275
- */
276
- interface FixedWindowLimiterStats {
277
- /** Requests made in current window */
278
- requestCount: number;
279
- /** Maximum requests allowed per window */
280
- limit: number;
281
- /** Window duration in milliseconds */
282
- windowMs: number;
283
- /** Time remaining until window reset (ms) */
284
- remainingMs: number;
285
- /** Number of requests waiting for next window */
286
- waitingCount: number;
287
- }
288
- /**
289
- * Fixed window rate limiter interface.
290
- */
291
- interface FixedWindowLimiter {
292
- /**
293
- * Execute an operation with rate limiting.
294
- * @param operation - The operation to execute
295
- * @param cost - Optional cost for this operation (default: 1)
296
- * @returns The operation result
297
- */
298
- execute<T>(operation: () => T | Promise<T>, cost?: number): Promise<T>;
299
- /**
300
- * Execute a Result-returning operation with rate limiting.
301
- * @param operation - The operation to execute
302
- * @param cost - Optional cost for this operation (default: 1)
303
- */
304
- executeResult<T, E>(operation: () => Result<T, E> | AsyncResult<T, E>, cost?: number): AsyncResult<T, E | RateLimitExceededError>;
305
- /**
306
- * Get current statistics.
307
- */
308
- getStats(): FixedWindowLimiterStats;
309
- /**
310
- * Reset the rate limiter.
311
- */
312
- reset(): void;
313
- }
314
- /**
315
- * Create a fixed window rate limiter.
316
- *
317
- * Unlike token bucket, fixed window resets at fixed intervals.
318
- * Simpler to reason about but can allow bursts at window boundaries.
319
- *
320
- * @param name - Name for the limiter (used in errors)
321
- * @param config - Rate limiter configuration
322
- * @returns A FixedWindowLimiter instance
323
- *
324
- * @example
325
- * ```typescript
326
- * const limiter = createFixedWindowLimiter('api-calls', {
327
- * limit: 100, // 100 requests
328
- * windowMs: 60000, // per minute
329
- * });
330
- *
331
- * // In workflow
332
- * const data = await limiter.execute(() => callApi());
333
- *
334
- * // Cost-based limiting (e.g., batch operations cost more)
335
- * const batchData = await limiter.execute(() => callBatchApi(), 10);
336
- * ```
337
- */
338
- declare function createFixedWindowLimiter(name: string, config: FixedWindowLimiterConfig): FixedWindowLimiter;
339
- /**
340
- * Configuration for cost-based rate limiter.
341
- */
342
- interface CostBasedRateLimiterConfig {
343
- /**
344
- * Maximum tokens (credits) per second refill rate.
345
- */
346
- tokensPerSecond: number;
347
- /**
348
- * Maximum token capacity (burst capacity).
349
- * @default tokensPerSecond * 2
350
- */
351
- maxTokens?: number;
352
- /**
353
- * Strategy when rate limit is exceeded.
354
- * - 'wait': Wait until tokens are available (default)
355
- * - 'reject': Reject immediately with error
356
- * @default 'wait'
357
- */
358
- strategy?: "wait" | "reject";
359
- }
360
- /**
361
- * Statistics for cost-based rate limiter.
362
- */
363
- interface CostBasedRateLimiterStats {
364
- /** Available tokens (can be fractional) */
365
- availableTokens: number;
366
- /** Maximum token capacity */
367
- maxTokens: number;
368
- /** Token refill rate per second */
369
- tokensPerSecond: number;
370
- /** Number of operations waiting */
371
- waitingCount: number;
372
- }
373
- /**
374
- * Cost-based rate limiter interface.
375
- */
376
- interface CostBasedRateLimiter {
377
- /**
378
- * Execute an operation with cost-based rate limiting.
379
- * @param operation - The operation to execute
380
- * @param cost - Token cost for this operation (default: 1)
381
- * @returns The operation result
382
- */
383
- execute<T>(operation: () => T | Promise<T>, cost?: number): Promise<T>;
384
- /**
385
- * Execute a Result-returning operation with cost-based rate limiting.
386
- * @param operation - The operation to execute
387
- * @param cost - Token cost for this operation (default: 1)
388
- */
389
- executeResult<T, E>(operation: () => Result<T, E> | AsyncResult<T, E>, cost?: number): AsyncResult<T, E | RateLimitExceededError>;
390
- /**
391
- * Get current statistics.
392
- */
393
- getStats(): CostBasedRateLimiterStats;
394
- /**
395
- * Reset the rate limiter.
396
- */
397
- reset(): void;
398
- }
399
- /**
400
- * Create a cost-based token bucket rate limiter.
401
- *
402
- * Different operations can have different costs, allowing fine-grained
403
- * control over resource usage. For example, a batch API call might cost
404
- * 10 tokens while a simple query costs 1.
405
- *
406
- * @param name - Name for the limiter (used in errors)
407
- * @param config - Rate limiter configuration
408
- * @returns A CostBasedRateLimiter instance
409
- *
410
- * @example
411
- * ```typescript
412
- * const limiter = createCostBasedRateLimiter('api', {
413
- * tokensPerSecond: 100, // 100 tokens/second refill
414
- * maxTokens: 200, // Can burst up to 200 tokens
415
- * });
416
- *
417
- * // Simple query costs 1 token
418
- * await limiter.execute(() => simpleQuery());
419
- *
420
- * // Batch operation costs 10 tokens
421
- * await limiter.execute(() => batchOperation(), 10);
422
- *
423
- * // Heavy export costs 50 tokens
424
- * await limiter.execute(() => exportData(), 50);
425
- * ```
426
- */
427
- declare function createCostBasedRateLimiter(name: string, config: CostBasedRateLimiterConfig): CostBasedRateLimiter;
428
- /**
429
- * Preset configurations for common use cases.
430
- */
431
- declare const rateLimiterPresets: {
432
- /**
433
- * Typical API rate limit (10 req/s).
434
- */
435
- readonly api: {
436
- maxPerSecond: number;
437
- burstCapacity: number;
438
- strategy: "wait";
439
- };
440
- /**
441
- * Database pool limit (concurrent connections).
442
- */
443
- readonly database: {
444
- maxConcurrent: number;
445
- strategy: "queue";
446
- maxQueueSize: number;
447
- };
448
- /**
449
- * Aggressive rate limit for external APIs (5 req/s).
450
- */
451
- readonly external: {
452
- maxPerSecond: number;
453
- burstCapacity: number;
454
- strategy: "wait";
455
- };
456
- };
457
-
458
- export { type CombinedLimiterConfig, type ConcurrencyLimiter, type ConcurrencyLimiterConfig, type ConcurrencyLimiterStats, type CostBasedRateLimiter, type CostBasedRateLimiterConfig, type CostBasedRateLimiterStats, type FixedWindowLimiter, type FixedWindowLimiterConfig, type FixedWindowLimiterStats, type QueueFullError, type RateLimitExceededError, type RateLimiter, type RateLimiterConfig, type RateLimiterStats, createCombinedLimiter, createConcurrencyLimiter, createCostBasedRateLimiter, createFixedWindowLimiter, createRateLimiter, isQueueFullError, isRateLimitExceededError, rateLimiterPresets };