@catbee/utils 0.0.8-rc.3 → 1.0.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.
- package/README.md +3 -2
- package/build/index.cjs +6912 -0
- package/build/index.d.ts +5284 -0
- package/build/index.mjs +6635 -0
- package/package.json +13 -292
- package/build/cjs/config.d.ts +0 -121
- package/build/cjs/config.js +0 -137
- package/build/cjs/index.d.ts +0 -26
- package/build/cjs/index.js +0 -68
- package/build/cjs/servers/server.builder.d.ts +0 -507
- package/build/cjs/servers/server.builder.js +0 -658
- package/build/cjs/servers/server.d.ts +0 -255
- package/build/cjs/servers/server.js +0 -970
- package/build/cjs/types/api-response.d.ts +0 -151
- package/build/cjs/types/api-response.js +0 -36
- package/build/cjs/types/index.d.ts +0 -124
- package/build/cjs/types/index.js +0 -25
- package/build/cjs/types/server.d.ts +0 -267
- package/build/cjs/types/server.js +0 -25
- package/build/cjs/utils/array.utils.d.ts +0 -167
- package/build/cjs/utils/array.utils.js +0 -363
- package/build/cjs/utils/async.utils.d.ts +0 -264
- package/build/cjs/utils/async.utils.js +0 -640
- package/build/cjs/utils/cache.utils.d.ts +0 -152
- package/build/cjs/utils/cache.utils.js +0 -298
- package/build/cjs/utils/context-store.utils.d.ts +0 -188
- package/build/cjs/utils/context-store.utils.js +0 -297
- package/build/cjs/utils/crypto.utils.d.ts +0 -159
- package/build/cjs/utils/crypto.utils.js +0 -295
- package/build/cjs/utils/date.utils.d.ts +0 -158
- package/build/cjs/utils/date.utils.js +0 -395
- package/build/cjs/utils/decorators.utils.d.ts +0 -511
- package/build/cjs/utils/decorators.utils.js +0 -1035
- package/build/cjs/utils/dir.utils.d.ts +0 -195
- package/build/cjs/utils/dir.utils.js +0 -500
- package/build/cjs/utils/env.utils.d.ts +0 -376
- package/build/cjs/utils/env.utils.js +0 -786
- package/build/cjs/utils/exception.utils.d.ts +0 -229
- package/build/cjs/utils/exception.utils.js +0 -408
- package/build/cjs/utils/fs.utils.d.ts +0 -163
- package/build/cjs/utils/fs.utils.js +0 -371
- package/build/cjs/utils/http-status-codes.d.ts +0 -265
- package/build/cjs/utils/http-status-codes.js +0 -297
- package/build/cjs/utils/id.utils.d.ts +0 -35
- package/build/cjs/utils/id.utils.js +0 -90
- package/build/cjs/utils/logger.utils.d.ts +0 -159
- package/build/cjs/utils/logger.utils.js +0 -350
- package/build/cjs/utils/middleware.utils.d.ts +0 -99
- package/build/cjs/utils/middleware.utils.js +0 -243
- package/build/cjs/utils/obj.utils.d.ts +0 -123
- package/build/cjs/utils/obj.utils.js +0 -428
- package/build/cjs/utils/performance.utils.d.ts +0 -135
- package/build/cjs/utils/performance.utils.js +0 -280
- package/build/cjs/utils/request.utils.d.ts +0 -85
- package/build/cjs/utils/request.utils.js +0 -199
- package/build/cjs/utils/response.utils.d.ts +0 -162
- package/build/cjs/utils/response.utils.js +0 -274
- package/build/cjs/utils/stream.utils.d.ts +0 -87
- package/build/cjs/utils/stream.utils.js +0 -217
- package/build/cjs/utils/string.utils.d.ts +0 -92
- package/build/cjs/utils/string.utils.js +0 -178
- package/build/cjs/utils/type.utils.d.ts +0 -89
- package/build/cjs/utils/type.utils.js +0 -195
- package/build/cjs/utils/url.utils.d.ts +0 -140
- package/build/cjs/utils/url.utils.js +0 -316
- package/build/cjs/utils/validate.utils.d.ts +0 -176
- package/build/cjs/utils/validate.utils.js +0 -344
- package/build/esm/config.d.ts +0 -121
- package/build/esm/config.js +0 -132
- package/build/esm/index.d.ts +0 -26
- package/build/esm/index.js +0 -49
- package/build/esm/servers/server.builder.d.ts +0 -507
- package/build/esm/servers/server.builder.js +0 -654
- package/build/esm/servers/server.d.ts +0 -255
- package/build/esm/servers/server.js +0 -963
- package/build/esm/types/api-response.d.ts +0 -151
- package/build/esm/types/api-response.js +0 -33
- package/build/esm/types/index.d.ts +0 -124
- package/build/esm/types/index.js +0 -24
- package/build/esm/types/server.d.ts +0 -267
- package/build/esm/types/server.js +0 -24
- package/build/esm/utils/array.utils.d.ts +0 -167
- package/build/esm/utils/array.utils.js +0 -344
- package/build/esm/utils/async.utils.d.ts +0 -264
- package/build/esm/utils/async.utils.js +0 -619
- package/build/esm/utils/cache.utils.d.ts +0 -152
- package/build/esm/utils/cache.utils.js +0 -294
- package/build/esm/utils/context-store.utils.d.ts +0 -188
- package/build/esm/utils/context-store.utils.js +0 -290
- package/build/esm/utils/crypto.utils.d.ts +0 -159
- package/build/esm/utils/crypto.utils.js +0 -278
- package/build/esm/utils/date.utils.d.ts +0 -158
- package/build/esm/utils/date.utils.js +0 -383
- package/build/esm/utils/decorators.utils.d.ts +0 -511
- package/build/esm/utils/decorators.utils.js +0 -1013
- package/build/esm/utils/dir.utils.d.ts +0 -195
- package/build/esm/utils/dir.utils.js +0 -476
- package/build/esm/utils/env.utils.d.ts +0 -376
- package/build/esm/utils/env.utils.js +0 -782
- package/build/esm/utils/exception.utils.d.ts +0 -229
- package/build/esm/utils/exception.utils.js +0 -382
- package/build/esm/utils/fs.utils.d.ts +0 -163
- package/build/esm/utils/fs.utils.js +0 -348
- package/build/esm/utils/http-status-codes.d.ts +0 -265
- package/build/esm/utils/http-status-codes.js +0 -294
- package/build/esm/utils/id.utils.d.ts +0 -35
- package/build/esm/utils/id.utils.js +0 -83
- package/build/esm/utils/logger.utils.d.ts +0 -159
- package/build/esm/utils/logger.utils.js +0 -304
- package/build/esm/utils/middleware.utils.d.ts +0 -99
- package/build/esm/utils/middleware.utils.js +0 -235
- package/build/esm/utils/obj.utils.d.ts +0 -123
- package/build/esm/utils/obj.utils.js +0 -412
- package/build/esm/utils/performance.utils.d.ts +0 -135
- package/build/esm/utils/performance.utils.js +0 -273
- package/build/esm/utils/request.utils.d.ts +0 -85
- package/build/esm/utils/request.utils.js +0 -190
- package/build/esm/utils/response.utils.d.ts +0 -162
- package/build/esm/utils/response.utils.js +0 -261
- package/build/esm/utils/stream.utils.d.ts +0 -87
- package/build/esm/utils/stream.utils.js +0 -209
- package/build/esm/utils/string.utils.d.ts +0 -92
- package/build/esm/utils/string.utils.js +0 -164
- package/build/esm/utils/type.utils.d.ts +0 -89
- package/build/esm/utils/type.utils.js +0 -186
- package/build/esm/utils/url.utils.d.ts +0 -140
- package/build/esm/utils/url.utils.js +0 -303
- package/build/esm/utils/validate.utils.d.ts +0 -176
- package/build/esm/utils/validate.utils.js +0 -319
|
@@ -1,640 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* The MIT License
|
|
4
|
-
*
|
|
5
|
-
* Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license
|
|
6
|
-
*
|
|
7
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
9
|
-
* in the Software without restriction, including without limitation the rights
|
|
10
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
12
|
-
* furnished to do so, subject to the following conditions:
|
|
13
|
-
*
|
|
14
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
15
|
-
* copies or substantial portions of the Software.
|
|
16
|
-
*
|
|
17
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
-
* SOFTWARE.
|
|
24
|
-
*/
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.CircuitBreakerState = exports.CircuitBreakerOpenError = void 0;
|
|
27
|
-
exports.sleep = sleep;
|
|
28
|
-
exports.debounce = debounce;
|
|
29
|
-
exports.throttle = throttle;
|
|
30
|
-
exports.retry = retry;
|
|
31
|
-
exports.withTimeout = withTimeout;
|
|
32
|
-
exports.runInBatches = runInBatches;
|
|
33
|
-
exports.singletonAsync = singletonAsync;
|
|
34
|
-
exports.settleAll = settleAll;
|
|
35
|
-
exports.createTaskQueue = createTaskQueue;
|
|
36
|
-
exports.runInSeries = runInSeries;
|
|
37
|
-
exports.memoizeAsync = memoizeAsync;
|
|
38
|
-
exports.abortable = abortable;
|
|
39
|
-
exports.createDeferred = createDeferred;
|
|
40
|
-
exports.waterfall = waterfall;
|
|
41
|
-
exports.rateLimit = rateLimit;
|
|
42
|
-
exports.circuitBreaker = circuitBreaker;
|
|
43
|
-
exports.runWithConcurrency = runWithConcurrency;
|
|
44
|
-
const logger_utils_1 = require("./logger.utils");
|
|
45
|
-
/**
|
|
46
|
-
* Delays execution for a specified number of milliseconds.
|
|
47
|
-
*
|
|
48
|
-
* @param {number} ms - The number of milliseconds to sleep.
|
|
49
|
-
* @returns {Promise<void>} A Promise that resolves after the given time.
|
|
50
|
-
*/
|
|
51
|
-
function sleep(ms) {
|
|
52
|
-
return new Promise(resolve => setTimeout(resolve, ms));
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Creates a debounced version of a function that delays its execution.
|
|
56
|
-
* Provides `.cancel()` and `.flush()` methods.
|
|
57
|
-
*
|
|
58
|
-
* @template T
|
|
59
|
-
* @param {T} fn - The function to debounce.
|
|
60
|
-
* @param {number} delay - Delay in milliseconds.
|
|
61
|
-
* @returns {T & { cancel: () => void; flush: () => void }} A debounced function.
|
|
62
|
-
*/
|
|
63
|
-
function debounce(fn, delay) {
|
|
64
|
-
let timer = null;
|
|
65
|
-
let pendingArgs = null;
|
|
66
|
-
function debounced(...args) {
|
|
67
|
-
pendingArgs = args;
|
|
68
|
-
if (timer)
|
|
69
|
-
clearTimeout(timer);
|
|
70
|
-
timer = setTimeout(() => {
|
|
71
|
-
fn(...pendingArgs);
|
|
72
|
-
timer = null;
|
|
73
|
-
}, delay);
|
|
74
|
-
}
|
|
75
|
-
debounced.cancel = () => {
|
|
76
|
-
if (timer)
|
|
77
|
-
clearTimeout(timer);
|
|
78
|
-
timer = null;
|
|
79
|
-
pendingArgs = null;
|
|
80
|
-
};
|
|
81
|
-
debounced.flush = () => {
|
|
82
|
-
if (timer) {
|
|
83
|
-
clearTimeout(timer);
|
|
84
|
-
fn(...pendingArgs);
|
|
85
|
-
timer = null;
|
|
86
|
-
pendingArgs = null;
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
return debounced;
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Creates a throttled version of a function that limits its execution rate.
|
|
93
|
-
* Allows control over leading/trailing invocation.
|
|
94
|
-
*
|
|
95
|
-
* @template T
|
|
96
|
-
* @param {T} fn - The function to throttle.
|
|
97
|
-
* @param {number} limit - Minimum time between calls in milliseconds.
|
|
98
|
-
* @param {{ leading?: boolean, trailing?: boolean }} [opts] - Options for leading/trailing edge throttling.
|
|
99
|
-
* @returns {(...args: Parameters<T>) => void} A throttled function.
|
|
100
|
-
*/
|
|
101
|
-
function throttle(fn, limit, opts = {
|
|
102
|
-
leading: true,
|
|
103
|
-
trailing: false
|
|
104
|
-
}) {
|
|
105
|
-
let lastCall = 0;
|
|
106
|
-
let timer = null;
|
|
107
|
-
let savedArgs = null;
|
|
108
|
-
return function (...args) {
|
|
109
|
-
const now = Date.now();
|
|
110
|
-
const { leading = true, trailing = false } = opts;
|
|
111
|
-
if (!lastCall && !leading)
|
|
112
|
-
lastCall = now;
|
|
113
|
-
const remaining = limit - (now - lastCall);
|
|
114
|
-
if (remaining <= 0) {
|
|
115
|
-
if (timer) {
|
|
116
|
-
clearTimeout(timer);
|
|
117
|
-
timer = null;
|
|
118
|
-
}
|
|
119
|
-
lastCall = now;
|
|
120
|
-
fn(...args);
|
|
121
|
-
}
|
|
122
|
-
else if (trailing) {
|
|
123
|
-
savedArgs = args;
|
|
124
|
-
if (!timer) {
|
|
125
|
-
timer = setTimeout(() => {
|
|
126
|
-
lastCall = leading ? Date.now() : 0;
|
|
127
|
-
timer = null;
|
|
128
|
-
if (savedArgs)
|
|
129
|
-
fn(...savedArgs);
|
|
130
|
-
}, remaining);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Retries an asynchronous function a given number of times with optional delay/backoff.
|
|
137
|
-
*
|
|
138
|
-
* @template T
|
|
139
|
-
* @param {() => Promise<T>} fn - The async function to retry.
|
|
140
|
-
* @param {number} [retries=3] - Number of retry attempts.
|
|
141
|
-
* @param {number} [delay=500] - Delay in milliseconds between retries.
|
|
142
|
-
* @param {boolean} [backoff=false] - Use exponential backoff between attempts.
|
|
143
|
-
* @param {(error: unknown, attempt: number) => void} [onRetry] - Callback for each retry attempt.
|
|
144
|
-
* @returns {Promise<T>} The result of the async function if successful.
|
|
145
|
-
* @throws {*} The last encountered error if all retries fail.
|
|
146
|
-
*/
|
|
147
|
-
async function retry(fn, retries = 3, delay = 500, backoff = false, onRetry) {
|
|
148
|
-
for (let i = 0; i < retries; i++) {
|
|
149
|
-
try {
|
|
150
|
-
return await fn();
|
|
151
|
-
}
|
|
152
|
-
catch (e) {
|
|
153
|
-
if (i === retries - 1)
|
|
154
|
-
throw e;
|
|
155
|
-
if (onRetry)
|
|
156
|
-
onRetry(e, i + 1);
|
|
157
|
-
await sleep(backoff ? delay * Math.pow(2, i) : delay);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
throw new Error('Retry failed'); // should never reach here
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Wraps a promise and rejects it if it doesn't resolve within the specified timeout.
|
|
164
|
-
*
|
|
165
|
-
* @template T
|
|
166
|
-
* @param {Promise<T>} promise - The original promise.
|
|
167
|
-
* @param {number} ms - Timeout in milliseconds.
|
|
168
|
-
* @param {string} [message="Operation timed out"] - Optional timeout message.
|
|
169
|
-
* @returns {Promise<T>} A promise that resolves or rejects within the timeout.
|
|
170
|
-
*/
|
|
171
|
-
function withTimeout(promise, ms, message = 'Operation timed out') {
|
|
172
|
-
return new Promise((resolve, reject) => {
|
|
173
|
-
const timeoutHandle = setTimeout(() => reject(new Error(message)), ms);
|
|
174
|
-
promise
|
|
175
|
-
.then(result => {
|
|
176
|
-
clearTimeout(timeoutHandle);
|
|
177
|
-
resolve(result);
|
|
178
|
-
})
|
|
179
|
-
.catch(err => {
|
|
180
|
-
clearTimeout(timeoutHandle);
|
|
181
|
-
reject(err);
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* Executes async tasks in true batches.
|
|
187
|
-
* Each batch runs in parallel, but batches run sequentially.
|
|
188
|
-
* All tasks in a batch start at the same time, next batch waits for full completion.
|
|
189
|
-
* NOTE: For more granular concurrency, use a "queue" or "pooled" approach.
|
|
190
|
-
*
|
|
191
|
-
* @template T
|
|
192
|
-
* @param {Array<() => Promise<T>>} tasks - An array of functions that return Promises.
|
|
193
|
-
* @param {number} limit - Number of tasks to run in parallel per batch.
|
|
194
|
-
* @returns {Promise<T[]>} A promise that resolves to an array of resolved values.
|
|
195
|
-
*/
|
|
196
|
-
async function runInBatches(tasks, limit) {
|
|
197
|
-
const results = [];
|
|
198
|
-
for (let i = 0; i < tasks.length; i += limit) {
|
|
199
|
-
const batch = tasks.slice(i, i + limit);
|
|
200
|
-
const batchResults = await Promise.all(batch.map(fn => fn()));
|
|
201
|
-
results.push(...batchResults);
|
|
202
|
-
}
|
|
203
|
-
return results;
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Wraps a function and ensures it is only called once at a time.
|
|
207
|
-
* Calls made while one is in progress will wait for the same Promise.
|
|
208
|
-
* Optionally, new calls can be dropped while in progress (drop=true).
|
|
209
|
-
*
|
|
210
|
-
* @template TArgs
|
|
211
|
-
* @template TResult
|
|
212
|
-
* @param {(...args: TArgs) => Promise<TResult>} fn - The async function to wrap.
|
|
213
|
-
* @param {boolean} [drop=false] - If true, new calls while one is pending are rejected.
|
|
214
|
-
* @returns {(...args: TArgs) => Promise<TResult>} A wrapped function with singleton behavior.
|
|
215
|
-
*/
|
|
216
|
-
function singletonAsync(fn, drop = false) {
|
|
217
|
-
let promise = null;
|
|
218
|
-
return async (...args) => {
|
|
219
|
-
if (!promise) {
|
|
220
|
-
promise = fn(...args).finally(() => {
|
|
221
|
-
promise = null;
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
else if (drop) {
|
|
225
|
-
return Promise.reject(new Error('Busy: function already running'));
|
|
226
|
-
}
|
|
227
|
-
return promise;
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* Resolves a list of async tasks in parallel, returning both resolved and rejected results.
|
|
232
|
-
*
|
|
233
|
-
* @template T
|
|
234
|
-
* @param {Array<() => Promise<T>>} tasks - Array of promise-returning functions.
|
|
235
|
-
* @returns {Promise<PromiseSettledResult<T>[]>} Results including status and value/reason.
|
|
236
|
-
*/
|
|
237
|
-
async function settleAll(tasks) {
|
|
238
|
-
return Promise.allSettled(tasks.map(task => task()));
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* A simple task queue that executes async tasks with a concurrency limit.
|
|
242
|
-
* Exposes pause, resume, and queue length getters.
|
|
243
|
-
*
|
|
244
|
-
* @param {number} limit - Maximum number of concurrent tasks.
|
|
245
|
-
* @returns {function & { pause: () => void, resume: () => void, length: number, isPaused: boolean }}
|
|
246
|
-
* Enqueue function plus queue controls.
|
|
247
|
-
*/
|
|
248
|
-
function createTaskQueue(limit) {
|
|
249
|
-
const queue = [];
|
|
250
|
-
let activeCount = 0;
|
|
251
|
-
let paused = false;
|
|
252
|
-
const state = {
|
|
253
|
-
/**
|
|
254
|
-
* Pause task processing.
|
|
255
|
-
*/
|
|
256
|
-
pause() {
|
|
257
|
-
paused = true;
|
|
258
|
-
},
|
|
259
|
-
/**
|
|
260
|
-
* Resume task processing.
|
|
261
|
-
*/
|
|
262
|
-
resume() {
|
|
263
|
-
paused = false;
|
|
264
|
-
next();
|
|
265
|
-
},
|
|
266
|
-
/**
|
|
267
|
-
* The current length of the queue.
|
|
268
|
-
* @type {number}
|
|
269
|
-
*/
|
|
270
|
-
get length() {
|
|
271
|
-
return queue.length;
|
|
272
|
-
},
|
|
273
|
-
/**
|
|
274
|
-
* Whether the queue is currently paused.
|
|
275
|
-
* @type {boolean}
|
|
276
|
-
*/
|
|
277
|
-
get isPaused() {
|
|
278
|
-
return paused;
|
|
279
|
-
}
|
|
280
|
-
};
|
|
281
|
-
const next = () => {
|
|
282
|
-
if (paused || queue.length === 0 || activeCount >= limit)
|
|
283
|
-
return;
|
|
284
|
-
const task = queue.shift();
|
|
285
|
-
activeCount++;
|
|
286
|
-
task().finally(() => {
|
|
287
|
-
activeCount--;
|
|
288
|
-
next();
|
|
289
|
-
});
|
|
290
|
-
};
|
|
291
|
-
/**
|
|
292
|
-
* Enqueues a new async task to the queue.
|
|
293
|
-
*
|
|
294
|
-
* @template T
|
|
295
|
-
* @param {() => Promise<T>} taskFn - The async task function.
|
|
296
|
-
* @returns {Promise<T>} Promise resolving when task completes.
|
|
297
|
-
*/
|
|
298
|
-
const enqueue = async function (taskFn) {
|
|
299
|
-
return new Promise((resolve, reject) => {
|
|
300
|
-
queue.push(async () => {
|
|
301
|
-
try {
|
|
302
|
-
const result = await taskFn();
|
|
303
|
-
resolve(result);
|
|
304
|
-
}
|
|
305
|
-
catch (err) {
|
|
306
|
-
reject(err);
|
|
307
|
-
}
|
|
308
|
-
});
|
|
309
|
-
next();
|
|
310
|
-
});
|
|
311
|
-
};
|
|
312
|
-
enqueue.pause = state.pause;
|
|
313
|
-
enqueue.resume = state.resume;
|
|
314
|
-
Object.defineProperty(enqueue, 'length', {
|
|
315
|
-
get: () => queue.length
|
|
316
|
-
});
|
|
317
|
-
Object.defineProperty(enqueue, 'isPaused', {
|
|
318
|
-
get: () => paused
|
|
319
|
-
});
|
|
320
|
-
return enqueue;
|
|
321
|
-
}
|
|
322
|
-
/**
|
|
323
|
-
* Executes async functions sequentially and collects results.
|
|
324
|
-
* Useful when order matters or tasks depend on each other.
|
|
325
|
-
*
|
|
326
|
-
* @template T
|
|
327
|
-
* @param {Array<() => Promise<T>>} tasks - Array of promise-returning functions.
|
|
328
|
-
* @returns {Promise<T[]>} Array of resolved values.
|
|
329
|
-
*/
|
|
330
|
-
async function runInSeries(tasks) {
|
|
331
|
-
const results = [];
|
|
332
|
-
for (const task of tasks) {
|
|
333
|
-
results.push(await task());
|
|
334
|
-
}
|
|
335
|
-
return results;
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* Memoizes an async function, caching results for repeated calls with identical arguments.
|
|
339
|
-
* Optional TTL (time-to-live) for cached entries.
|
|
340
|
-
*
|
|
341
|
-
* @template T Function return type
|
|
342
|
-
* @template Args Function arguments types
|
|
343
|
-
* @param {(...args: Args) => Promise<T>} fn - The async function to memoize
|
|
344
|
-
* @param {object} [options] - Memoization options
|
|
345
|
-
* @param {number} [options.ttl] - Cache TTL in milliseconds (optional)
|
|
346
|
-
* @param {(args: Args) => string} [options.keyFn] - Custom key generator function
|
|
347
|
-
* @returns {(...args: Args) => Promise<T>} Memoized function
|
|
348
|
-
*/
|
|
349
|
-
function memoizeAsync(fn, options = {}) {
|
|
350
|
-
const cache = new Map();
|
|
351
|
-
const { ttl, keyFn = JSON.stringify } = options;
|
|
352
|
-
return async function (...args) {
|
|
353
|
-
const key = keyFn(args);
|
|
354
|
-
const cached = cache.get(key);
|
|
355
|
-
if (cached && (!ttl || Date.now() < cached.expires)) {
|
|
356
|
-
return cached.value;
|
|
357
|
-
}
|
|
358
|
-
const result = await fn(...args);
|
|
359
|
-
cache.set(key, {
|
|
360
|
-
value: result,
|
|
361
|
-
expires: ttl ? Date.now() + ttl : Infinity
|
|
362
|
-
});
|
|
363
|
-
return result;
|
|
364
|
-
};
|
|
365
|
-
}
|
|
366
|
-
/**
|
|
367
|
-
* Creates an abortable version of a promise that can be cancelled using an AbortController.
|
|
368
|
-
*
|
|
369
|
-
* @template T
|
|
370
|
-
* @param {Promise<T>} promise - The promise to make abortable
|
|
371
|
-
* @param {AbortSignal} signal - AbortSignal from AbortController
|
|
372
|
-
* @param {any} [abortValue] - Value to use when rejecting on abort
|
|
373
|
-
* @returns {Promise<T>} Promise that rejects if the signal is aborted
|
|
374
|
-
*/
|
|
375
|
-
function abortable(promise, signal, abortValue = new Error('Operation aborted')) {
|
|
376
|
-
if (signal.aborted) {
|
|
377
|
-
return Promise.reject(abortValue);
|
|
378
|
-
}
|
|
379
|
-
return Promise.race([
|
|
380
|
-
promise,
|
|
381
|
-
new Promise((_, reject) => {
|
|
382
|
-
const abort = () => reject(abortValue);
|
|
383
|
-
signal.addEventListener('abort', abort, { once: true });
|
|
384
|
-
promise.finally(() => signal.removeEventListener('abort', abort));
|
|
385
|
-
})
|
|
386
|
-
]);
|
|
387
|
-
}
|
|
388
|
-
/**
|
|
389
|
-
* Creates a promise with external resolve/reject functions.
|
|
390
|
-
* Useful for creating promises that can be resolved or rejected from outside.
|
|
391
|
-
*
|
|
392
|
-
* @template T
|
|
393
|
-
* @returns {[Promise<T>, (value: T | PromiseLike<T>) => void, (reason?: any) => void]}
|
|
394
|
-
* Tuple of [promise, resolve, reject]
|
|
395
|
-
*/
|
|
396
|
-
function createDeferred() {
|
|
397
|
-
let resolve;
|
|
398
|
-
let reject;
|
|
399
|
-
const promise = new Promise((res, rej) => {
|
|
400
|
-
resolve = res;
|
|
401
|
-
reject = rej;
|
|
402
|
-
});
|
|
403
|
-
return [promise, resolve, reject];
|
|
404
|
-
}
|
|
405
|
-
/**
|
|
406
|
-
* Chains a series of async functions, passing the result of each to the next.
|
|
407
|
-
* Similar to function composition but for async functions.
|
|
408
|
-
*
|
|
409
|
-
* @template T
|
|
410
|
-
* @param {Array<(input: any) => Promise<any>>} fns - Array of async functions to compose
|
|
411
|
-
* @returns {(input: any) => Promise<T>} Composed function
|
|
412
|
-
*/
|
|
413
|
-
function waterfall(fns) {
|
|
414
|
-
return async (initialValue) => {
|
|
415
|
-
return fns.reduce(async (acc, fn) => fn(await acc), Promise.resolve(initialValue));
|
|
416
|
-
};
|
|
417
|
-
}
|
|
418
|
-
/**
|
|
419
|
-
* Creates a rate limiter that ensures functions aren't called more than
|
|
420
|
-
* a specified number of times per interval.
|
|
421
|
-
*
|
|
422
|
-
* @template T
|
|
423
|
-
* @param {(...args: any[]) => Promise<T>} fn - Function to rate limit
|
|
424
|
-
* @param {number} maxCalls - Maximum calls allowed per interval
|
|
425
|
-
* @param {number} interval - Time interval in milliseconds
|
|
426
|
-
* @returns {(...args: any[]) => Promise<T>} Rate limited function
|
|
427
|
-
*/
|
|
428
|
-
function rateLimit(fn, maxCalls, interval) {
|
|
429
|
-
const calls = [];
|
|
430
|
-
return async function (...args) {
|
|
431
|
-
const now = Date.now();
|
|
432
|
-
calls.splice(0, calls.length, ...calls.filter(time => now - time < interval));
|
|
433
|
-
if (calls.length >= maxCalls) {
|
|
434
|
-
const oldestCall = calls[0];
|
|
435
|
-
const delay = interval - (now - oldestCall);
|
|
436
|
-
await sleep(Math.max(1, delay));
|
|
437
|
-
// Remove potentially stale entries after sleep
|
|
438
|
-
const currentTime = Date.now();
|
|
439
|
-
calls.splice(0, calls.length, ...calls.filter(time => currentTime - time < interval));
|
|
440
|
-
// If still at limit after sleep, wait for another cycle
|
|
441
|
-
if (calls.length >= maxCalls) {
|
|
442
|
-
const nextDelay = interval - (currentTime - calls[0]);
|
|
443
|
-
await sleep(Math.max(1, nextDelay));
|
|
444
|
-
calls.splice(0, calls.length, ...calls.filter(time => Date.now() - time < interval));
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
calls.push(Date.now());
|
|
448
|
-
return fn(...args);
|
|
449
|
-
};
|
|
450
|
-
}
|
|
451
|
-
/**
|
|
452
|
-
* Circuit breaker pattern implementation for protecting against cascading failures.
|
|
453
|
-
* Tracks failures and prevents calling the function when too many failures occur.
|
|
454
|
-
*
|
|
455
|
-
* @param fn - Function to protect with circuit breaker
|
|
456
|
-
* @param options - Circuit breaker options
|
|
457
|
-
* @returns Function wrapped with circuit breaker logic
|
|
458
|
-
*
|
|
459
|
-
* @example
|
|
460
|
-
* ```typescript
|
|
461
|
-
* const protectedFetch = circuitBreaker(
|
|
462
|
-
* async (url) => {
|
|
463
|
-
* const response = await fetch(url);
|
|
464
|
-
* if (!response.ok) throw new Error(`HTTP error: ${response.status}`);
|
|
465
|
-
* return response.json();
|
|
466
|
-
* },
|
|
467
|
-
* {
|
|
468
|
-
* failureThreshold: 3,
|
|
469
|
-
* resetTimeout: 30000,
|
|
470
|
-
* onOpen: () => console.log('Circuit breaker opened'),
|
|
471
|
-
* onClose: () => console.log('Circuit breaker closed')
|
|
472
|
-
* }
|
|
473
|
-
* );
|
|
474
|
-
*
|
|
475
|
-
* // Will throw CircuitBreakerOpenError after failureThreshold consecutive failures
|
|
476
|
-
* try {
|
|
477
|
-
* const data = await protectedFetch('https://api.example.com');
|
|
478
|
-
* } catch (error) {
|
|
479
|
-
* if (error instanceof CircuitBreakerOpenError) {
|
|
480
|
-
* console.log('Service is currently unavailable, please try again later');
|
|
481
|
-
* }
|
|
482
|
-
* }
|
|
483
|
-
* ```
|
|
484
|
-
*/
|
|
485
|
-
class CircuitBreakerOpenError extends Error {
|
|
486
|
-
constructor(message = 'Circuit breaker is open') {
|
|
487
|
-
super(message);
|
|
488
|
-
this.name = 'CircuitBreakerOpenError';
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
exports.CircuitBreakerOpenError = CircuitBreakerOpenError;
|
|
492
|
-
/**
|
|
493
|
-
* Circuit breaker states
|
|
494
|
-
*/
|
|
495
|
-
var CircuitBreakerState;
|
|
496
|
-
(function (CircuitBreakerState) {
|
|
497
|
-
CircuitBreakerState["CLOSED"] = "CLOSED";
|
|
498
|
-
CircuitBreakerState["OPEN"] = "OPEN";
|
|
499
|
-
CircuitBreakerState["HALF_OPEN"] = "HALF_OPEN";
|
|
500
|
-
})(CircuitBreakerState || (exports.CircuitBreakerState = CircuitBreakerState = {}));
|
|
501
|
-
function circuitBreaker(fn, options = {}) {
|
|
502
|
-
const { failureThreshold = 5, resetTimeout = 10000, successThreshold = 1, onOpen, onClose, onHalfOpen } = options;
|
|
503
|
-
let state = CircuitBreakerState.CLOSED;
|
|
504
|
-
let failureCount = 0;
|
|
505
|
-
let successCount = 0;
|
|
506
|
-
let nextAttempt = Date.now();
|
|
507
|
-
return async function (...args) {
|
|
508
|
-
if (state === CircuitBreakerState.OPEN) {
|
|
509
|
-
if (Date.now() < nextAttempt) {
|
|
510
|
-
throw new CircuitBreakerOpenError();
|
|
511
|
-
}
|
|
512
|
-
// Move to half-open state
|
|
513
|
-
state = CircuitBreakerState.HALF_OPEN;
|
|
514
|
-
if (onHalfOpen)
|
|
515
|
-
onHalfOpen();
|
|
516
|
-
}
|
|
517
|
-
try {
|
|
518
|
-
const result = await fn(...args);
|
|
519
|
-
// On success in half-open state
|
|
520
|
-
if (state === CircuitBreakerState.HALF_OPEN) {
|
|
521
|
-
successCount++;
|
|
522
|
-
if (successCount >= successThreshold) {
|
|
523
|
-
successCount = 0;
|
|
524
|
-
failureCount = 0;
|
|
525
|
-
state = CircuitBreakerState.CLOSED;
|
|
526
|
-
if (onClose)
|
|
527
|
-
onClose();
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
else {
|
|
531
|
-
// Reset failure count on success in closed state
|
|
532
|
-
failureCount = 0;
|
|
533
|
-
}
|
|
534
|
-
return result;
|
|
535
|
-
}
|
|
536
|
-
catch (error) {
|
|
537
|
-
// Track failures
|
|
538
|
-
failureCount++;
|
|
539
|
-
// Check if we need to open the circuit
|
|
540
|
-
if ((state === CircuitBreakerState.CLOSED || state === CircuitBreakerState.HALF_OPEN) &&
|
|
541
|
-
failureCount >= failureThreshold) {
|
|
542
|
-
state = CircuitBreakerState.OPEN;
|
|
543
|
-
nextAttempt = Date.now() + resetTimeout;
|
|
544
|
-
if (onOpen)
|
|
545
|
-
onOpen();
|
|
546
|
-
}
|
|
547
|
-
throw error;
|
|
548
|
-
}
|
|
549
|
-
};
|
|
550
|
-
}
|
|
551
|
-
/**
|
|
552
|
-
* Run multiple async tasks with concurrency control.
|
|
553
|
-
*
|
|
554
|
-
* @param tasks - Array of async tasks to run
|
|
555
|
-
* @param options - Concurrency options
|
|
556
|
-
* @returns Promise that resolves when all tasks are complete
|
|
557
|
-
*
|
|
558
|
-
* @example
|
|
559
|
-
* ```typescript
|
|
560
|
-
* const urls = ['https://example.com/1', 'https://example.com/2', many more ];
|
|
561
|
-
*
|
|
562
|
-
* // Process up to 5 requests at a time, with progress reporting
|
|
563
|
-
* const results = await runWithConcurrency(
|
|
564
|
-
* urls.map(url => () => fetch(url).then(res => res.json())),
|
|
565
|
-
* {
|
|
566
|
-
* concurrency: 5,
|
|
567
|
-
* onProgress: (completed, total) => {
|
|
568
|
-
* console.log(`Progress: ${completed}/${total}`);
|
|
569
|
-
* }
|
|
570
|
-
* }
|
|
571
|
-
* );
|
|
572
|
-
* ```
|
|
573
|
-
*/
|
|
574
|
-
async function runWithConcurrency(tasks, options = {}) {
|
|
575
|
-
const { concurrency = 3, onProgress, signal } = options;
|
|
576
|
-
const results = [];
|
|
577
|
-
const totalTasks = tasks.length;
|
|
578
|
-
let completed = 0;
|
|
579
|
-
// If no tasks, return empty array
|
|
580
|
-
if (totalTasks === 0)
|
|
581
|
-
return results;
|
|
582
|
-
// Check if execution is already aborted
|
|
583
|
-
if (signal?.aborted) {
|
|
584
|
-
throw new Error('Aborted');
|
|
585
|
-
}
|
|
586
|
-
return new Promise((resolve, reject) => {
|
|
587
|
-
let taskIndex = 0;
|
|
588
|
-
// Process next task function
|
|
589
|
-
const processNext = async () => {
|
|
590
|
-
// Get current task index and increment
|
|
591
|
-
const currentTaskIndex = taskIndex++;
|
|
592
|
-
// Skip if we've processed all tasks
|
|
593
|
-
if (currentTaskIndex >= totalTasks)
|
|
594
|
-
return;
|
|
595
|
-
try {
|
|
596
|
-
// Run the task
|
|
597
|
-
const result = await tasks[currentTaskIndex]();
|
|
598
|
-
// Check if aborted during task execution
|
|
599
|
-
if (signal?.aborted) {
|
|
600
|
-
reject(new Error('Aborted'));
|
|
601
|
-
return;
|
|
602
|
-
}
|
|
603
|
-
// Store result and update counters
|
|
604
|
-
results[currentTaskIndex] = result;
|
|
605
|
-
completed++;
|
|
606
|
-
// Call progress callback if provided
|
|
607
|
-
if (onProgress) {
|
|
608
|
-
try {
|
|
609
|
-
onProgress(completed, totalTasks);
|
|
610
|
-
}
|
|
611
|
-
catch (err) {
|
|
612
|
-
(0, logger_utils_1.getLogger)().error({ err }, 'Error in onProgress callback');
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
catch (error) {
|
|
617
|
-
reject(error);
|
|
618
|
-
return;
|
|
619
|
-
}
|
|
620
|
-
// Check if all tasks are completed
|
|
621
|
-
if (completed === totalTasks) {
|
|
622
|
-
resolve(results);
|
|
623
|
-
return;
|
|
624
|
-
}
|
|
625
|
-
// Process next task
|
|
626
|
-
processNext();
|
|
627
|
-
};
|
|
628
|
-
// Start initial batch of tasks
|
|
629
|
-
const initialBatch = Math.min(concurrency, totalTasks);
|
|
630
|
-
for (let i = 0; i < initialBatch; i++) {
|
|
631
|
-
processNext();
|
|
632
|
-
}
|
|
633
|
-
// Set up abort handler
|
|
634
|
-
if (signal) {
|
|
635
|
-
signal.addEventListener('abort', () => {
|
|
636
|
-
reject(new Error('Aborted'));
|
|
637
|
-
}, { once: true });
|
|
638
|
-
}
|
|
639
|
-
});
|
|
640
|
-
}
|