@catbee/utils 0.0.8-next.0 → 0.0.8-rc.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 +1 -2
- package/build/esm/config.d.ts +122 -0
- package/build/esm/config.js +133 -0
- package/build/esm/config.js.map +1 -0
- package/build/esm/index.d.ts +27 -0
- package/build/esm/index.js +50 -0
- package/build/esm/index.js.map +1 -0
- package/build/esm/servers/server.builder.d.ts +508 -0
- package/build/esm/servers/server.builder.js +702 -0
- package/build/esm/servers/server.builder.js.map +1 -0
- package/build/esm/servers/server.d.ts +256 -0
- package/build/esm/servers/server.js +1248 -0
- package/build/esm/servers/server.js.map +1 -0
- package/build/esm/types/api-response.d.ts +152 -0
- package/build/esm/types/api-response.js +34 -0
- package/build/esm/types/api-response.js.map +1 -0
- package/build/esm/types/index.d.ts +125 -0
- package/build/esm/types/index.js +25 -0
- package/build/esm/types/index.js.map +1 -0
- package/build/esm/types/server.d.ts +268 -0
- package/build/esm/types/server.js +25 -0
- package/build/esm/types/server.js.map +1 -0
- package/build/esm/utils/array.utils.d.ts +168 -0
- package/build/esm/utils/array.utils.js +382 -0
- package/build/esm/utils/array.utils.js.map +1 -0
- package/build/esm/utils/async.utils.d.ts +265 -0
- package/build/esm/utils/async.utils.js +896 -0
- package/build/esm/utils/async.utils.js.map +1 -0
- package/build/esm/utils/cache.utils.d.ts +153 -0
- package/build/esm/utils/cache.utils.js +499 -0
- package/build/esm/utils/cache.utils.js.map +1 -0
- package/build/esm/utils/context-store.utils.d.ts +189 -0
- package/build/esm/utils/context-store.utils.js +307 -0
- package/build/esm/utils/context-store.utils.js.map +1 -0
- package/build/esm/utils/crypto.utils.d.ts +160 -0
- package/build/esm/utils/crypto.utils.js +375 -0
- package/build/esm/utils/crypto.utils.js.map +1 -0
- package/build/esm/utils/date.utils.d.ts +159 -0
- package/build/esm/utils/date.utils.js +389 -0
- package/build/esm/utils/date.utils.js.map +1 -0
- package/build/esm/utils/decorators.utils.d.ts +508 -0
- package/build/esm/utils/decorators.utils.js +1057 -0
- package/build/esm/utils/decorators.utils.js.map +1 -0
- package/build/esm/utils/dir.utils.d.ts +196 -0
- package/build/esm/utils/dir.utils.js +998 -0
- package/build/esm/utils/dir.utils.js.map +1 -0
- package/build/esm/utils/env.utils.d.ts +377 -0
- package/build/esm/utils/env.utils.js +873 -0
- package/build/esm/utils/env.utils.js.map +1 -0
- package/build/esm/utils/exception.utils.d.ts +230 -0
- package/build/esm/utils/exception.utils.js +559 -0
- package/build/esm/utils/exception.utils.js.map +1 -0
- package/build/esm/utils/fs.utils.d.ts +164 -0
- package/build/esm/utils/fs.utils.js +580 -0
- package/build/esm/utils/fs.utils.js.map +1 -0
- package/build/esm/utils/http-status-codes.d.ts +266 -0
- package/build/esm/utils/http-status-codes.js +295 -0
- package/build/esm/utils/http-status-codes.js.map +1 -0
- package/build/esm/utils/id.utils.d.ts +36 -0
- package/build/esm/utils/id.utils.js +87 -0
- package/build/esm/utils/id.utils.js.map +1 -0
- package/build/esm/utils/logger.utils.d.ts +160 -0
- package/build/esm/utils/logger.utils.js +341 -0
- package/build/esm/utils/logger.utils.js.map +1 -0
- package/build/esm/utils/middleware.utils.d.ts +100 -0
- package/build/esm/utils/middleware.utils.js +325 -0
- package/build/esm/utils/middleware.utils.js.map +1 -0
- package/build/esm/utils/obj.utils.d.ts +124 -0
- package/build/esm/utils/obj.utils.js +519 -0
- package/build/esm/utils/obj.utils.js.map +1 -0
- package/build/esm/utils/performance.utils.d.ts +136 -0
- package/build/esm/utils/performance.utils.js +376 -0
- package/build/esm/utils/performance.utils.js.map +1 -0
- package/build/esm/utils/request.utils.d.ts +86 -0
- package/build/esm/utils/request.utils.js +223 -0
- package/build/esm/utils/request.utils.js.map +1 -0
- package/build/esm/utils/response.utils.d.ts +163 -0
- package/build/esm/utils/response.utils.js +284 -0
- package/build/esm/utils/response.utils.js.map +1 -0
- package/build/esm/utils/stream.utils.d.ts +88 -0
- package/build/esm/utils/stream.utils.js +284 -0
- package/build/esm/utils/stream.utils.js.map +1 -0
- package/build/esm/utils/string.utils.d.ts +93 -0
- package/build/esm/utils/string.utils.js +169 -0
- package/build/esm/utils/string.utils.js.map +1 -0
- package/build/esm/utils/type.utils.d.ts +90 -0
- package/build/esm/utils/type.utils.js +190 -0
- package/build/esm/utils/type.utils.js.map +1 -0
- package/build/esm/utils/url.utils.d.ts +141 -0
- package/build/esm/utils/url.utils.js +381 -0
- package/build/esm/utils/url.utils.js.map +1 -0
- package/build/esm/utils/validate.utils.d.ts +177 -0
- package/build/esm/utils/validate.utils.js +336 -0
- package/build/esm/utils/validate.utils.js.map +1 -0
- package/build/esnext/config.d.ts +122 -0
- package/build/esnext/config.js +133 -0
- package/build/esnext/config.js.map +1 -0
- package/build/esnext/index.d.ts +27 -0
- package/build/esnext/index.js +50 -0
- package/build/esnext/index.js.map +1 -0
- package/build/esnext/servers/server.builder.d.ts +508 -0
- package/build/esnext/servers/server.builder.js +657 -0
- package/build/esnext/servers/server.builder.js.map +1 -0
- package/build/esnext/servers/server.d.ts +256 -0
- package/build/esnext/servers/server.js +955 -0
- package/build/esnext/servers/server.js.map +1 -0
- package/build/esnext/types/api-response.d.ts +152 -0
- package/build/esnext/types/api-response.js +34 -0
- package/build/esnext/types/api-response.js.map +1 -0
- package/build/esnext/types/index.d.ts +125 -0
- package/build/esnext/types/index.js +25 -0
- package/build/esnext/types/index.js.map +1 -0
- package/build/esnext/types/server.d.ts +268 -0
- package/build/esnext/types/server.js +25 -0
- package/build/esnext/types/server.js.map +1 -0
- package/build/esnext/utils/array.utils.d.ts +168 -0
- package/build/esnext/utils/array.utils.js +345 -0
- package/build/esnext/utils/array.utils.js.map +1 -0
- package/build/esnext/utils/async.utils.d.ts +265 -0
- package/build/esnext/utils/async.utils.js +620 -0
- package/build/esnext/utils/async.utils.js.map +1 -0
- package/build/esnext/utils/cache.utils.d.ts +153 -0
- package/build/esnext/utils/cache.utils.js +293 -0
- package/build/esnext/utils/cache.utils.js.map +1 -0
- package/build/esnext/utils/context-store.utils.d.ts +189 -0
- package/build/esnext/utils/context-store.utils.js +289 -0
- package/build/esnext/utils/context-store.utils.js.map +1 -0
- package/build/esnext/utils/crypto.utils.d.ts +160 -0
- package/build/esnext/utils/crypto.utils.js +276 -0
- package/build/esnext/utils/crypto.utils.js.map +1 -0
- package/build/esnext/utils/date.utils.d.ts +159 -0
- package/build/esnext/utils/date.utils.js +384 -0
- package/build/esnext/utils/date.utils.js.map +1 -0
- package/build/esnext/utils/decorators.utils.d.ts +508 -0
- package/build/esnext/utils/decorators.utils.js +877 -0
- package/build/esnext/utils/decorators.utils.js.map +1 -0
- package/build/esnext/utils/dir.utils.d.ts +196 -0
- package/build/esnext/utils/dir.utils.js +477 -0
- package/build/esnext/utils/dir.utils.js.map +1 -0
- package/build/esnext/utils/env.utils.d.ts +377 -0
- package/build/esnext/utils/env.utils.js +785 -0
- package/build/esnext/utils/env.utils.js.map +1 -0
- package/build/esnext/utils/exception.utils.d.ts +230 -0
- package/build/esnext/utils/exception.utils.js +381 -0
- package/build/esnext/utils/exception.utils.js.map +1 -0
- package/build/esnext/utils/fs.utils.d.ts +164 -0
- package/build/esnext/utils/fs.utils.js +349 -0
- package/build/esnext/utils/fs.utils.js.map +1 -0
- package/build/esnext/utils/http-status-codes.d.ts +266 -0
- package/build/esnext/utils/http-status-codes.js +295 -0
- package/build/esnext/utils/http-status-codes.js.map +1 -0
- package/build/esnext/utils/id.utils.d.ts +36 -0
- package/build/esnext/utils/id.utils.js +84 -0
- package/build/esnext/utils/id.utils.js.map +1 -0
- package/build/esnext/utils/logger.utils.d.ts +160 -0
- package/build/esnext/utils/logger.utils.js +300 -0
- package/build/esnext/utils/logger.utils.js.map +1 -0
- package/build/esnext/utils/middleware.utils.d.ts +100 -0
- package/build/esnext/utils/middleware.utils.js +236 -0
- package/build/esnext/utils/middleware.utils.js.map +1 -0
- package/build/esnext/utils/obj.utils.d.ts +124 -0
- package/build/esnext/utils/obj.utils.js +413 -0
- package/build/esnext/utils/obj.utils.js.map +1 -0
- package/build/esnext/utils/performance.utils.d.ts +136 -0
- package/build/esnext/utils/performance.utils.js +271 -0
- package/build/esnext/utils/performance.utils.js.map +1 -0
- package/build/esnext/utils/request.utils.d.ts +86 -0
- package/build/esnext/utils/request.utils.js +186 -0
- package/build/esnext/utils/request.utils.js.map +1 -0
- package/build/esnext/utils/response.utils.d.ts +163 -0
- package/build/esnext/utils/response.utils.js +247 -0
- package/build/esnext/utils/response.utils.js.map +1 -0
- package/build/esnext/utils/stream.utils.d.ts +88 -0
- package/build/esnext/utils/stream.utils.js +210 -0
- package/build/esnext/utils/stream.utils.js.map +1 -0
- package/build/esnext/utils/string.utils.d.ts +93 -0
- package/build/esnext/utils/string.utils.js +165 -0
- package/build/esnext/utils/string.utils.js.map +1 -0
- package/build/esnext/utils/type.utils.d.ts +90 -0
- package/build/esnext/utils/type.utils.js +187 -0
- package/build/esnext/utils/type.utils.js.map +1 -0
- package/build/esnext/utils/url.utils.d.ts +141 -0
- package/build/esnext/utils/url.utils.js +304 -0
- package/build/esnext/utils/url.utils.js.map +1 -0
- package/build/esnext/utils/validate.utils.d.ts +177 -0
- package/build/esnext/utils/validate.utils.js +320 -0
- package/build/esnext/utils/validate.utils.js.map +1 -0
- package/build/src/config.d.ts +122 -0
- package/build/src/config.js +138 -0
- package/build/src/config.js.map +1 -0
- package/build/src/index.d.ts +27 -0
- package/build/src/index.js +69 -0
- package/build/src/index.js.map +1 -0
- package/build/src/servers/server.builder.d.ts +508 -0
- package/build/src/servers/server.builder.js +661 -0
- package/build/src/servers/server.builder.js.map +1 -0
- package/build/src/servers/server.d.ts +256 -0
- package/build/src/servers/server.js +962 -0
- package/build/src/servers/server.js.map +1 -0
- package/build/src/types/api-response.d.ts +152 -0
- package/build/src/types/api-response.js +37 -0
- package/build/src/types/api-response.js.map +1 -0
- package/build/src/types/index.d.ts +125 -0
- package/build/src/types/index.js +26 -0
- package/build/src/types/index.js.map +1 -0
- package/build/src/types/server.d.ts +268 -0
- package/build/src/types/server.js +26 -0
- package/build/src/types/server.js.map +1 -0
- package/build/src/utils/array.utils.d.ts +168 -0
- package/build/src/utils/array.utils.js +364 -0
- package/build/src/utils/array.utils.js.map +1 -0
- package/build/src/utils/async.utils.d.ts +265 -0
- package/build/src/utils/async.utils.js +641 -0
- package/build/src/utils/async.utils.js.map +1 -0
- package/build/src/utils/cache.utils.d.ts +153 -0
- package/build/src/utils/cache.utils.js +297 -0
- package/build/src/utils/cache.utils.js.map +1 -0
- package/build/src/utils/context-store.utils.d.ts +189 -0
- package/build/src/utils/context-store.utils.js +296 -0
- package/build/src/utils/context-store.utils.js.map +1 -0
- package/build/src/utils/crypto.utils.d.ts +160 -0
- package/build/src/utils/crypto.utils.js +293 -0
- package/build/src/utils/crypto.utils.js.map +1 -0
- package/build/src/utils/date.utils.d.ts +159 -0
- package/build/src/utils/date.utils.js +396 -0
- package/build/src/utils/date.utils.js.map +1 -0
- package/build/src/utils/decorators.utils.d.ts +508 -0
- package/build/src/utils/decorators.utils.js +896 -0
- package/build/src/utils/decorators.utils.js.map +1 -0
- package/build/src/utils/dir.utils.d.ts +196 -0
- package/build/src/utils/dir.utils.js +501 -0
- package/build/src/utils/dir.utils.js.map +1 -0
- package/build/src/utils/env.utils.d.ts +377 -0
- package/build/src/utils/env.utils.js +789 -0
- package/build/src/utils/env.utils.js.map +1 -0
- package/build/src/utils/exception.utils.d.ts +230 -0
- package/build/src/utils/exception.utils.js +407 -0
- package/build/src/utils/exception.utils.js.map +1 -0
- package/build/src/utils/fs.utils.d.ts +164 -0
- package/build/src/utils/fs.utils.js +372 -0
- package/build/src/utils/fs.utils.js.map +1 -0
- package/build/src/utils/http-status-codes.d.ts +266 -0
- package/build/src/utils/http-status-codes.js +298 -0
- package/build/src/utils/http-status-codes.js.map +1 -0
- package/build/src/utils/id.utils.d.ts +36 -0
- package/build/src/utils/id.utils.js +91 -0
- package/build/src/utils/id.utils.js.map +1 -0
- package/build/src/utils/logger.utils.d.ts +160 -0
- package/build/src/utils/logger.utils.js +346 -0
- package/build/src/utils/logger.utils.js.map +1 -0
- package/build/src/utils/middleware.utils.d.ts +100 -0
- package/build/src/utils/middleware.utils.js +244 -0
- package/build/src/utils/middleware.utils.js.map +1 -0
- package/build/src/utils/obj.utils.d.ts +124 -0
- package/build/src/utils/obj.utils.js +429 -0
- package/build/src/utils/obj.utils.js.map +1 -0
- package/build/src/utils/performance.utils.d.ts +136 -0
- package/build/src/utils/performance.utils.js +278 -0
- package/build/src/utils/performance.utils.js.map +1 -0
- package/build/src/utils/request.utils.d.ts +86 -0
- package/build/src/utils/request.utils.js +195 -0
- package/build/src/utils/request.utils.js.map +1 -0
- package/build/src/utils/response.utils.d.ts +163 -0
- package/build/src/utils/response.utils.js +260 -0
- package/build/src/utils/response.utils.js.map +1 -0
- package/build/src/utils/stream.utils.d.ts +88 -0
- package/build/src/utils/stream.utils.js +218 -0
- package/build/src/utils/stream.utils.js.map +1 -0
- package/build/src/utils/string.utils.d.ts +93 -0
- package/build/src/utils/string.utils.js +179 -0
- package/build/src/utils/string.utils.js.map +1 -0
- package/build/src/utils/type.utils.d.ts +90 -0
- package/build/src/utils/type.utils.js +196 -0
- package/build/src/utils/type.utils.js.map +1 -0
- package/build/src/utils/url.utils.d.ts +141 -0
- package/build/src/utils/url.utils.js +317 -0
- package/build/src/utils/url.utils.js.map +1 -0
- package/build/src/utils/validate.utils.d.ts +177 -0
- package/build/src/utils/validate.utils.js +345 -0
- package/build/src/utils/validate.utils.js.map +1 -0
- package/package.json +66 -12
- package/build/index.cjs +0 -6888
- package/build/index.d.ts +0 -5242
- package/build/index.mjs +0 -6615
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for timing function execution.
|
|
3
|
+
*/
|
|
4
|
+
export interface TimingOptions {
|
|
5
|
+
/** Optional label for the timing (defaults to function name) */
|
|
6
|
+
label?: string;
|
|
7
|
+
/** Whether to log the timing (default: false) */
|
|
8
|
+
log?: boolean;
|
|
9
|
+
/** Log level to use if logging is enabled (default: 'debug') */
|
|
10
|
+
logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error';
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Result of a timing operation.
|
|
14
|
+
*/
|
|
15
|
+
export interface TimingResult {
|
|
16
|
+
/** Duration in milliseconds */
|
|
17
|
+
durationMs: number;
|
|
18
|
+
/** Duration in seconds */
|
|
19
|
+
durationSec: number;
|
|
20
|
+
/** Start timestamp */
|
|
21
|
+
startTime: number;
|
|
22
|
+
/** End timestamp */
|
|
23
|
+
endTime: number;
|
|
24
|
+
/** Label used for the timing */
|
|
25
|
+
label: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Measure the execution time of a synchronous function.
|
|
29
|
+
*
|
|
30
|
+
* @param fn - Function to measure
|
|
31
|
+
* @param options - Timing options
|
|
32
|
+
* @returns Result containing the return value and timing information
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const { result, timing } = timeSync(() => {
|
|
37
|
+
* // Some expensive operation
|
|
38
|
+
* return computeResult();
|
|
39
|
+
* }, { label: 'Computation', log: true });
|
|
40
|
+
*
|
|
41
|
+
* console.log(`Result: ${result}, took ${timing.durationMs}ms`);
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function timeSync<T>(fn: () => T, options?: TimingOptions): {
|
|
45
|
+
result: T;
|
|
46
|
+
timing: TimingResult;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Measure the execution time of an asynchronous function.
|
|
50
|
+
*
|
|
51
|
+
* @param fn - Async function to measure
|
|
52
|
+
* @param options - Timing options
|
|
53
|
+
* @returns Promise resolving to result and timing information
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const { result, timing } = await timeAsync(async () => {
|
|
58
|
+
* // Some expensive async operation
|
|
59
|
+
* const data = await fetchData();
|
|
60
|
+
* return processData(data);
|
|
61
|
+
* }, { label: 'API Request', log: true });
|
|
62
|
+
*
|
|
63
|
+
* console.log(`Fetched ${result.length} items in ${timing.durationSec.toFixed(2)}s`);
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export declare function timeAsync<T>(fn: () => Promise<T>, options?: TimingOptions): Promise<{
|
|
67
|
+
result: T;
|
|
68
|
+
timing: TimingResult;
|
|
69
|
+
}>;
|
|
70
|
+
/**
|
|
71
|
+
* Create a timing decorator for class methods.
|
|
72
|
+
*
|
|
73
|
+
* @param options - Timing options
|
|
74
|
+
* @returns Method decorator
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* class DataService {
|
|
79
|
+
* @timed({ log: true, logLevel: 'info' })
|
|
80
|
+
* async fetchData() {
|
|
81
|
+
* // ...implementation
|
|
82
|
+
* }
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export declare function timed(options?: TimingOptions): (target: any, propertyKeyOrContext: string | symbol | any, descriptor?: PropertyDescriptor) => void;
|
|
87
|
+
/**
|
|
88
|
+
* Memoize function results with optional TTL and max cache size.
|
|
89
|
+
*
|
|
90
|
+
* @param fn - Function to memoize
|
|
91
|
+
* @param options - Memoization options
|
|
92
|
+
* @returns Memoized function
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* // Cache results for 30 seconds, with a maximum of 100 entries
|
|
97
|
+
* const cachedFetch = memoize(
|
|
98
|
+
* async (url) => {
|
|
99
|
+
* const response = await fetch(url);
|
|
100
|
+
* return response.json();
|
|
101
|
+
* },
|
|
102
|
+
* { ttl: 30000, maxSize: 100, cacheKey: (url) => url }
|
|
103
|
+
* );
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export declare function memoize<T, Args extends any[]>(fn: (...args: Args) => T, options?: {
|
|
107
|
+
/** Time-to-live in milliseconds (default: indefinite) */
|
|
108
|
+
ttl?: number;
|
|
109
|
+
/** Maximum cache size (default: unlimited) */
|
|
110
|
+
maxSize?: number;
|
|
111
|
+
/** Function to generate a cache key from arguments */
|
|
112
|
+
cacheKey?: (...args: Args) => string;
|
|
113
|
+
/** Auto-cleanup interval in milliseconds (default: disabled) */
|
|
114
|
+
autoCleanupMs?: number;
|
|
115
|
+
}): (...args: Args) => T;
|
|
116
|
+
/**
|
|
117
|
+
* Track memory usage for a function execution.
|
|
118
|
+
*
|
|
119
|
+
* @param fn - Function to track
|
|
120
|
+
* @param options - Memory tracking options
|
|
121
|
+
* @returns Result and memory usage information
|
|
122
|
+
*/
|
|
123
|
+
export declare function trackMemoryUsage<T>(fn: () => T, options?: {
|
|
124
|
+
/** Whether to log the memory usage (default: false) */
|
|
125
|
+
log?: boolean;
|
|
126
|
+
/** Label for the memory tracking (default: function name) */
|
|
127
|
+
label?: string;
|
|
128
|
+
}): {
|
|
129
|
+
result: T;
|
|
130
|
+
memoryUsage: {
|
|
131
|
+
before: NodeJS.MemoryUsage;
|
|
132
|
+
after: NodeJS.MemoryUsage;
|
|
133
|
+
diff: Record<string, number>;
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
//# sourceMappingURL=performance.utils.d.ts.map
|
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
var __assign = (this && this.__assign) || function () {
|
|
25
|
+
__assign = Object.assign || function(t) {
|
|
26
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
27
|
+
s = arguments[i];
|
|
28
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
29
|
+
t[p] = s[p];
|
|
30
|
+
}
|
|
31
|
+
return t;
|
|
32
|
+
};
|
|
33
|
+
return __assign.apply(this, arguments);
|
|
34
|
+
};
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
45
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
46
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
47
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
48
|
+
function step(op) {
|
|
49
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
50
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
51
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
52
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
53
|
+
switch (op[0]) {
|
|
54
|
+
case 0: case 1: t = op; break;
|
|
55
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
56
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
57
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
58
|
+
default:
|
|
59
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
60
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
61
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
62
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
63
|
+
if (t[2]) _.ops.pop();
|
|
64
|
+
_.trys.pop(); continue;
|
|
65
|
+
}
|
|
66
|
+
op = body.call(thisArg, _);
|
|
67
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
68
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
72
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
73
|
+
if (!m) return o;
|
|
74
|
+
var i = m.call(o), r, ar = [], e;
|
|
75
|
+
try {
|
|
76
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
77
|
+
}
|
|
78
|
+
catch (error) { e = { error: error }; }
|
|
79
|
+
finally {
|
|
80
|
+
try {
|
|
81
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
82
|
+
}
|
|
83
|
+
finally { if (e) throw e.error; }
|
|
84
|
+
}
|
|
85
|
+
return ar;
|
|
86
|
+
};
|
|
87
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
88
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
89
|
+
if (ar || !(i in from)) {
|
|
90
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
91
|
+
ar[i] = from[i];
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
95
|
+
};
|
|
96
|
+
import { TTLCache } from './cache.utils';
|
|
97
|
+
import { getLogger } from './logger.utils';
|
|
98
|
+
/**
|
|
99
|
+
* Measure the execution time of a synchronous function.
|
|
100
|
+
*
|
|
101
|
+
* @param fn - Function to measure
|
|
102
|
+
* @param options - Timing options
|
|
103
|
+
* @returns Result containing the return value and timing information
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* const { result, timing } = timeSync(() => {
|
|
108
|
+
* // Some expensive operation
|
|
109
|
+
* return computeResult();
|
|
110
|
+
* }, { label: 'Computation', log: true });
|
|
111
|
+
*
|
|
112
|
+
* console.log(`Result: ${result}, took ${timing.durationMs}ms`);
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
export function timeSync(fn, options) {
|
|
116
|
+
if (options === void 0) { options = {}; }
|
|
117
|
+
var _a = options.label, label = _a === void 0 ? fn.name || 'anonymous function' : _a, _b = options.log, log = _b === void 0 ? false : _b, _c = options.logLevel, logLevel = _c === void 0 ? 'debug' : _c;
|
|
118
|
+
var startTime = performance.now();
|
|
119
|
+
var result = fn();
|
|
120
|
+
var endTime = performance.now();
|
|
121
|
+
var durationMs = endTime - startTime;
|
|
122
|
+
var durationSec = durationMs / 1000;
|
|
123
|
+
var timing = {
|
|
124
|
+
durationMs: durationMs,
|
|
125
|
+
durationSec: durationSec,
|
|
126
|
+
startTime: startTime,
|
|
127
|
+
endTime: endTime,
|
|
128
|
+
label: label
|
|
129
|
+
};
|
|
130
|
+
if (log) {
|
|
131
|
+
var logger = getLogger();
|
|
132
|
+
var message = "".concat(label, " completed in ").concat(durationMs.toFixed(2), "ms");
|
|
133
|
+
switch (logLevel) {
|
|
134
|
+
case 'trace':
|
|
135
|
+
logger.trace({ timing: timing }, message);
|
|
136
|
+
break;
|
|
137
|
+
case 'debug':
|
|
138
|
+
logger.debug({ timing: timing }, message);
|
|
139
|
+
break;
|
|
140
|
+
case 'info':
|
|
141
|
+
logger.info({ timing: timing }, message);
|
|
142
|
+
break;
|
|
143
|
+
case 'warn':
|
|
144
|
+
logger.warn({ timing: timing }, message);
|
|
145
|
+
break;
|
|
146
|
+
case 'error':
|
|
147
|
+
logger.error({ timing: timing }, message);
|
|
148
|
+
break;
|
|
149
|
+
default:
|
|
150
|
+
logger.debug({ timing: timing }, message);
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return { result: result, timing: timing };
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Measure the execution time of an asynchronous function.
|
|
158
|
+
*
|
|
159
|
+
* @param fn - Async function to measure
|
|
160
|
+
* @param options - Timing options
|
|
161
|
+
* @returns Promise resolving to result and timing information
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```typescript
|
|
165
|
+
* const { result, timing } = await timeAsync(async () => {
|
|
166
|
+
* // Some expensive async operation
|
|
167
|
+
* const data = await fetchData();
|
|
168
|
+
* return processData(data);
|
|
169
|
+
* }, { label: 'API Request', log: true });
|
|
170
|
+
*
|
|
171
|
+
* console.log(`Fetched ${result.length} items in ${timing.durationSec.toFixed(2)}s`);
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
export function timeAsync(fn_1) {
|
|
175
|
+
return __awaiter(this, arguments, void 0, function (fn, options) {
|
|
176
|
+
var _a, label, _b, log, _c, logLevel, startTime, result, endTime, durationMs, durationSec, timing, logger, message;
|
|
177
|
+
if (options === void 0) { options = {}; }
|
|
178
|
+
return __generator(this, function (_d) {
|
|
179
|
+
switch (_d.label) {
|
|
180
|
+
case 0:
|
|
181
|
+
_a = options.label, label = _a === void 0 ? fn.name || 'anonymous async function' : _a, _b = options.log, log = _b === void 0 ? false : _b, _c = options.logLevel, logLevel = _c === void 0 ? 'debug' : _c;
|
|
182
|
+
startTime = performance.now();
|
|
183
|
+
return [4 /*yield*/, fn()];
|
|
184
|
+
case 1:
|
|
185
|
+
result = _d.sent();
|
|
186
|
+
endTime = performance.now();
|
|
187
|
+
durationMs = endTime - startTime;
|
|
188
|
+
durationSec = durationMs / 1000;
|
|
189
|
+
timing = {
|
|
190
|
+
durationMs: durationMs,
|
|
191
|
+
durationSec: durationSec,
|
|
192
|
+
startTime: startTime,
|
|
193
|
+
endTime: endTime,
|
|
194
|
+
label: label
|
|
195
|
+
};
|
|
196
|
+
if (log) {
|
|
197
|
+
logger = getLogger();
|
|
198
|
+
message = "".concat(label, " completed in ").concat(durationMs.toFixed(2), "ms");
|
|
199
|
+
switch (logLevel) {
|
|
200
|
+
case 'trace':
|
|
201
|
+
logger.trace({ timing: timing }, message);
|
|
202
|
+
break;
|
|
203
|
+
case 'debug':
|
|
204
|
+
logger.debug({ timing: timing }, message);
|
|
205
|
+
break;
|
|
206
|
+
case 'info':
|
|
207
|
+
logger.info({ timing: timing }, message);
|
|
208
|
+
break;
|
|
209
|
+
case 'warn':
|
|
210
|
+
logger.warn({ timing: timing }, message);
|
|
211
|
+
break;
|
|
212
|
+
case 'error':
|
|
213
|
+
logger.error({ timing: timing }, message);
|
|
214
|
+
break;
|
|
215
|
+
default:
|
|
216
|
+
logger.debug({ timing: timing }, message);
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return [2 /*return*/, { result: result, timing: timing }];
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Create a timing decorator for class methods.
|
|
227
|
+
*
|
|
228
|
+
* @param options - Timing options
|
|
229
|
+
* @returns Method decorator
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* ```typescript
|
|
233
|
+
* class DataService {
|
|
234
|
+
* @timed({ log: true, logLevel: 'info' })
|
|
235
|
+
* async fetchData() {
|
|
236
|
+
* // ...implementation
|
|
237
|
+
* }
|
|
238
|
+
* }
|
|
239
|
+
* ```
|
|
240
|
+
*/
|
|
241
|
+
export function timed(options) {
|
|
242
|
+
if (options === void 0) { options = {}; }
|
|
243
|
+
return function (target, propertyKeyOrContext, descriptor) {
|
|
244
|
+
var propertyKey;
|
|
245
|
+
var actualDescriptor;
|
|
246
|
+
// ESNext decorator: second argument is context object
|
|
247
|
+
if (typeof propertyKeyOrContext === 'object' && propertyKeyOrContext !== null && 'name' in propertyKeyOrContext) {
|
|
248
|
+
propertyKey = propertyKeyOrContext.name;
|
|
249
|
+
actualDescriptor = descriptor !== null && descriptor !== void 0 ? descriptor : Object.getOwnPropertyDescriptor(target, propertyKey);
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
// Legacy decorator: second argument is property key
|
|
253
|
+
propertyKey = propertyKeyOrContext;
|
|
254
|
+
actualDescriptor = descriptor !== null && descriptor !== void 0 ? descriptor : Object.getOwnPropertyDescriptor(target, propertyKey);
|
|
255
|
+
}
|
|
256
|
+
var originalMethod = actualDescriptor.value;
|
|
257
|
+
actualDescriptor.value = function () {
|
|
258
|
+
var _this = this;
|
|
259
|
+
var args = [];
|
|
260
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
261
|
+
args[_i] = arguments[_i];
|
|
262
|
+
}
|
|
263
|
+
var methodOptions = __assign(__assign({}, options), { label: options.label || "".concat(target.constructor.name, ".").concat(String(propertyKey)) });
|
|
264
|
+
if (originalMethod.constructor.name === 'AsyncFunction') {
|
|
265
|
+
return timeAsync(function () { return originalMethod.apply(_this, args); }, methodOptions).then(function (_a) {
|
|
266
|
+
var result = _a.result;
|
|
267
|
+
return result;
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
return timeSync(function () { return originalMethod.apply(_this, args); }, methodOptions).result;
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
// For legacy decorators, assign the new descriptor
|
|
275
|
+
if (descriptor) {
|
|
276
|
+
descriptor.value = actualDescriptor.value;
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
Object.defineProperty(target, propertyKey, actualDescriptor);
|
|
280
|
+
}
|
|
281
|
+
// No return needed for method decorators
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Memoize function results with optional TTL and max cache size.
|
|
286
|
+
*
|
|
287
|
+
* @param fn - Function to memoize
|
|
288
|
+
* @param options - Memoization options
|
|
289
|
+
* @returns Memoized function
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* ```typescript
|
|
293
|
+
* // Cache results for 30 seconds, with a maximum of 100 entries
|
|
294
|
+
* const cachedFetch = memoize(
|
|
295
|
+
* async (url) => {
|
|
296
|
+
* const response = await fetch(url);
|
|
297
|
+
* return response.json();
|
|
298
|
+
* },
|
|
299
|
+
* { ttl: 30000, maxSize: 100, cacheKey: (url) => url }
|
|
300
|
+
* );
|
|
301
|
+
* ```
|
|
302
|
+
*/
|
|
303
|
+
export function memoize(fn, options) {
|
|
304
|
+
if (options === void 0) { options = {}; }
|
|
305
|
+
var ttl = options.ttl, maxSize = options.maxSize, autoCleanupMs = options.autoCleanupMs, _a = options.cacheKey, cacheKey = _a === void 0 ? function () {
|
|
306
|
+
var args = [];
|
|
307
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
308
|
+
args[_i] = arguments[_i];
|
|
309
|
+
}
|
|
310
|
+
return JSON.stringify(args);
|
|
311
|
+
} : _a;
|
|
312
|
+
// Create a properly configured TTLCache instance
|
|
313
|
+
var cache = new TTLCache({
|
|
314
|
+
ttlMs: ttl,
|
|
315
|
+
maxSize: maxSize,
|
|
316
|
+
autoCleanupMs: autoCleanupMs
|
|
317
|
+
});
|
|
318
|
+
return function () {
|
|
319
|
+
var args = [];
|
|
320
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
321
|
+
args[_i] = arguments[_i];
|
|
322
|
+
}
|
|
323
|
+
var key = cacheKey.apply(void 0, __spreadArray([], __read(args), false));
|
|
324
|
+
// Try to get from cache first - TTLCache handles expiration internally
|
|
325
|
+
var cachedValue = cache.get(key);
|
|
326
|
+
if (cachedValue !== undefined) {
|
|
327
|
+
return cachedValue;
|
|
328
|
+
}
|
|
329
|
+
// Calculate the value and store in cache with configured TTL
|
|
330
|
+
var value = fn.apply(void 0, __spreadArray([], __read(args), false));
|
|
331
|
+
cache.set(key, value);
|
|
332
|
+
return value;
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Track memory usage for a function execution.
|
|
337
|
+
*
|
|
338
|
+
* @param fn - Function to track
|
|
339
|
+
* @param options - Memory tracking options
|
|
340
|
+
* @returns Result and memory usage information
|
|
341
|
+
*/
|
|
342
|
+
export function trackMemoryUsage(fn, options) {
|
|
343
|
+
if (options === void 0) { options = {}; }
|
|
344
|
+
var _a = options.log, log = _a === void 0 ? false : _a, _b = options.label, label = _b === void 0 ? fn.name || 'anonymous function' : _b;
|
|
345
|
+
// Collect garbage before measuring (optional)
|
|
346
|
+
if (global.gc) {
|
|
347
|
+
global.gc();
|
|
348
|
+
}
|
|
349
|
+
var beforeMemory = process.memoryUsage();
|
|
350
|
+
var result = fn();
|
|
351
|
+
var afterMemory = process.memoryUsage();
|
|
352
|
+
// Calculate differences
|
|
353
|
+
var diff = {};
|
|
354
|
+
for (var key in afterMemory) {
|
|
355
|
+
if (Object.prototype.hasOwnProperty.call(afterMemory, key)) {
|
|
356
|
+
diff[key] = afterMemory[key] - beforeMemory[key];
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
if (log) {
|
|
360
|
+
getLogger().info({
|
|
361
|
+
label: label,
|
|
362
|
+
memoryBefore: beforeMemory,
|
|
363
|
+
memoryAfter: afterMemory,
|
|
364
|
+
memoryDiff: diff
|
|
365
|
+
}, "Memory usage for ".concat(label));
|
|
366
|
+
}
|
|
367
|
+
return {
|
|
368
|
+
result: result,
|
|
369
|
+
memoryUsage: {
|
|
370
|
+
before: beforeMemory,
|
|
371
|
+
after: afterMemory,
|
|
372
|
+
diff: diff
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
//# sourceMappingURL=performance.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"performance.utils.js","sourceRoot":"","sources":["../../../src/utils/performance.utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AA8B3C;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,QAAQ,CAAI,EAAW,EAAE,OAA2B;IAA3B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E,8CAA8C;IAC9C,IAAI,MAAM,CAAC,EAAE,EAAE,CAAC;QACd,MAAM,CAAC,EAAE,EAAE,CAAC;IACd,CAAC;IAED,IAAM,YAAY,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAC3C,IAAM,MAAM,GAAG,EAAE,EAAE,CAAC;IACpB,IAAM,WAAW,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAE1C,wBAAwB;IACxB,IAAM,IAAI,GAA2B,EAAE,CAAC;IACxC,KAAK,IAAM,GAAG,IAAI,WAAW,EAAE,CAAC;QAC9B,IAAI,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,WAAW,EAAE,GAAG,CAAC,EAAE,CAAC;YAC3D,IAAI,CAAC,GAAG,CAAC,GAAI,WAAmB,CAAC,GAAG,CAAC,GAAI,YAAoB,CAAC,GAAG,CAAC,CAAC;QACrE,CAAC;IACH,CAAC;IAED,IAAI,GAAG,EAAE,CAAC;QACR,SAAS,EAAE,CAAC,IAAI,CACd;YACE,KAAK,OAAA;YACL,YAAY,EAAE,YAAY;YAC1B,WAAW,EAAE,WAAW;YACxB,UAAU,EAAE,IAAI;SACjB,EACD,2BAAoB,KAAK,CAAE,CAC5B,CAAC;IACJ,CAAC;IAED,OAAO;QACL,MAAM,QAAA;QACN,WAAW,EAAE;YACX,MAAM,EAAE,YAAY;YACpB,KAAK,EAAE,WAAW;YAClB,IAAI,MAAA;SACL;KACF,CAAC;AACJ,CAAC","sourcesContent":["/*\n * The MIT License\n *\n * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { TTLCache } from './cache.utils';\nimport { getLogger } from './logger.utils';\n\n/**\n * Options for timing function execution.\n */\nexport interface TimingOptions {\n /** Optional label for the timing (defaults to function name) */\n label?: string;\n /** Whether to log the timing (default: false) */\n log?: boolean;\n /** Log level to use if logging is enabled (default: 'debug') */\n logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error';\n}\n\n/**\n * Result of a timing operation.\n */\nexport interface TimingResult {\n /** Duration in milliseconds */\n durationMs: number;\n /** Duration in seconds */\n durationSec: number;\n /** Start timestamp */\n startTime: number;\n /** End timestamp */\n endTime: number;\n /** Label used for the timing */\n label: string;\n}\n\n/**\n * Measure the execution time of a synchronous function.\n *\n * @param fn - Function to measure\n * @param options - Timing options\n * @returns Result containing the return value and timing information\n *\n * @example\n * ```typescript\n * const { result, timing } = timeSync(() => {\n * // Some expensive operation\n * return computeResult();\n * }, { label: 'Computation', log: true });\n *\n * console.log(`Result: ${result}, took ${timing.durationMs}ms`);\n * ```\n */\nexport function timeSync<T>(fn: () => T, options: TimingOptions = {}): { result: T; timing: TimingResult } {\n const { label = fn.name || 'anonymous function', log = false, logLevel = 'debug' } = options;\n\n const startTime = performance.now();\n const result = fn();\n const endTime = performance.now();\n\n const durationMs = endTime - startTime;\n const durationSec = durationMs / 1000;\n\n const timing: TimingResult = {\n durationMs,\n durationSec,\n startTime,\n endTime,\n label\n };\n\n if (log) {\n const logger = getLogger();\n const message = `${label} completed in ${durationMs.toFixed(2)}ms`;\n\n switch (logLevel) {\n case 'trace':\n logger.trace({ timing }, message);\n break;\n case 'debug':\n logger.debug({ timing }, message);\n break;\n case 'info':\n logger.info({ timing }, message);\n break;\n case 'warn':\n logger.warn({ timing }, message);\n break;\n case 'error':\n logger.error({ timing }, message);\n break;\n default:\n logger.debug({ timing }, message);\n break;\n }\n }\n\n return { result, timing };\n}\n\n/**\n * Measure the execution time of an asynchronous function.\n *\n * @param fn - Async function to measure\n * @param options - Timing options\n * @returns Promise resolving to result and timing information\n *\n * @example\n * ```typescript\n * const { result, timing } = await timeAsync(async () => {\n * // Some expensive async operation\n * const data = await fetchData();\n * return processData(data);\n * }, { label: 'API Request', log: true });\n *\n * console.log(`Fetched ${result.length} items in ${timing.durationSec.toFixed(2)}s`);\n * ```\n */\nexport async function timeAsync<T>(\n fn: () => Promise<T>,\n options: TimingOptions = {}\n): Promise<{ result: T; timing: TimingResult }> {\n const { label = fn.name || 'anonymous async function', log = false, logLevel = 'debug' } = options;\n\n const startTime = performance.now();\n const result = await fn();\n const endTime = performance.now();\n\n const durationMs = endTime - startTime;\n const durationSec = durationMs / 1000;\n\n const timing: TimingResult = {\n durationMs,\n durationSec,\n startTime,\n endTime,\n label\n };\n\n if (log) {\n const logger = getLogger();\n const message = `${label} completed in ${durationMs.toFixed(2)}ms`;\n\n switch (logLevel) {\n case 'trace':\n logger.trace({ timing }, message);\n break;\n case 'debug':\n logger.debug({ timing }, message);\n break;\n case 'info':\n logger.info({ timing }, message);\n break;\n case 'warn':\n logger.warn({ timing }, message);\n break;\n case 'error':\n logger.error({ timing }, message);\n break;\n default:\n logger.debug({ timing }, message);\n break;\n }\n }\n\n return { result, timing };\n}\n\n/**\n * Create a timing decorator for class methods.\n *\n * @param options - Timing options\n * @returns Method decorator\n *\n * @example\n * ```typescript\n * class DataService {\n * @timed({ log: true, logLevel: 'info' })\n * async fetchData() {\n * // ...implementation\n * }\n * }\n * ```\n */\nexport function timed(options: TimingOptions = {}) {\n return function (target: any, propertyKeyOrContext: string | symbol | any, descriptor?: PropertyDescriptor): void {\n let propertyKey: string | symbol;\n let actualDescriptor: PropertyDescriptor;\n\n // ESNext decorator: second argument is context object\n if (typeof propertyKeyOrContext === 'object' && propertyKeyOrContext !== null && 'name' in propertyKeyOrContext) {\n propertyKey = propertyKeyOrContext.name;\n actualDescriptor = descriptor ?? Object.getOwnPropertyDescriptor(target, propertyKey)!;\n } else {\n // Legacy decorator: second argument is property key\n propertyKey = propertyKeyOrContext;\n actualDescriptor = descriptor ?? Object.getOwnPropertyDescriptor(target, propertyKey)!;\n }\n\n const originalMethod = actualDescriptor.value;\n\n actualDescriptor.value = function (...args: any[]) {\n const methodOptions = {\n ...options,\n label: options.label || `${target.constructor.name}.${String(propertyKey)}`\n };\n\n if (originalMethod.constructor.name === 'AsyncFunction') {\n return timeAsync(() => originalMethod.apply(this, args), methodOptions).then(({ result }) => result);\n } else {\n return timeSync(() => originalMethod.apply(this, args), methodOptions).result;\n }\n };\n\n // For legacy decorators, assign the new descriptor\n if (descriptor) {\n descriptor.value = actualDescriptor.value;\n } else {\n Object.defineProperty(target, propertyKey, actualDescriptor);\n }\n // No return needed for method decorators\n };\n}\n\n/**\n * Memoize function results with optional TTL and max cache size.\n *\n * @param fn - Function to memoize\n * @param options - Memoization options\n * @returns Memoized function\n *\n * @example\n * ```typescript\n * // Cache results for 30 seconds, with a maximum of 100 entries\n * const cachedFetch = memoize(\n * async (url) => {\n * const response = await fetch(url);\n * return response.json();\n * },\n * { ttl: 30000, maxSize: 100, cacheKey: (url) => url }\n * );\n * ```\n */\nexport function memoize<T, Args extends any[]>(\n fn: (...args: Args) => T,\n options: {\n /** Time-to-live in milliseconds (default: indefinite) */\n ttl?: number;\n /** Maximum cache size (default: unlimited) */\n maxSize?: number;\n /** Function to generate a cache key from arguments */\n cacheKey?: (...args: Args) => string;\n /** Auto-cleanup interval in milliseconds (default: disabled) */\n autoCleanupMs?: number;\n } = {}\n) {\n const { ttl, maxSize, autoCleanupMs, cacheKey = (...args) => JSON.stringify(args) } = options;\n\n // Create a properly configured TTLCache instance\n const cache = new TTLCache<string, T>({\n ttlMs: ttl,\n maxSize: maxSize,\n autoCleanupMs: autoCleanupMs\n });\n\n return function (...args: Args): T {\n const key = cacheKey(...args);\n\n // Try to get from cache first - TTLCache handles expiration internally\n const cachedValue = cache.get(key);\n if (cachedValue !== undefined) {\n return cachedValue;\n }\n\n // Calculate the value and store in cache with configured TTL\n const value = fn(...args);\n cache.set(key, value);\n return value;\n };\n}\n\n/**\n * Track memory usage for a function execution.\n *\n * @param fn - Function to track\n * @param options - Memory tracking options\n * @returns Result and memory usage information\n */\nexport function trackMemoryUsage<T>(\n fn: () => T,\n options: {\n /** Whether to log the memory usage (default: false) */\n log?: boolean;\n /** Label for the memory tracking (default: function name) */\n label?: string;\n } = {}\n): { result: T; memoryUsage: { before: NodeJS.MemoryUsage; after: NodeJS.MemoryUsage; diff: Record<string, number> } } {\n const { log = false, label = fn.name || 'anonymous function' } = options;\n\n // Collect garbage before measuring (optional)\n if (global.gc) {\n global.gc();\n }\n\n const beforeMemory = process.memoryUsage();\n const result = fn();\n const afterMemory = process.memoryUsage();\n\n // Calculate differences\n const diff: Record<string, number> = {};\n for (const key in afterMemory) {\n if (Object.prototype.hasOwnProperty.call(afterMemory, key)) {\n diff[key] = (afterMemory as any)[key] - (beforeMemory as any)[key];\n }\n }\n\n if (log) {\n getLogger().info(\n {\n label,\n memoryBefore: beforeMemory,\n memoryAfter: afterMemory,\n memoryDiff: diff\n },\n `Memory usage for ${label}`\n );\n }\n\n return {\n result,\n memoryUsage: {\n before: beforeMemory,\n after: afterMemory,\n diff\n }\n };\n}\n"]}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Request } from 'express';
|
|
2
|
+
import { WithPagination } from '../types/api-response';
|
|
3
|
+
/**
|
|
4
|
+
* Options for parsing and validating request parameters.
|
|
5
|
+
*/
|
|
6
|
+
export interface ValidationOptions {
|
|
7
|
+
/** Whether to throw an error on validation failure */
|
|
8
|
+
throwOnError?: boolean;
|
|
9
|
+
/** Custom error message for validation failures */
|
|
10
|
+
errorMessage?: string;
|
|
11
|
+
/** Default value to use if parameter is missing */
|
|
12
|
+
defaultValue?: any;
|
|
13
|
+
/** Whether the parameter is required */
|
|
14
|
+
required?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Result of a parameter validation operation.
|
|
18
|
+
*/
|
|
19
|
+
export interface ValidationResult<T> {
|
|
20
|
+
/** Whether validation was successful */
|
|
21
|
+
isValid: boolean;
|
|
22
|
+
/** The validated and potentially transformed value */
|
|
23
|
+
value: T | null;
|
|
24
|
+
/** Error message if validation failed */
|
|
25
|
+
error?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Extracts pagination, sorting, and optional search params from a request.
|
|
29
|
+
*
|
|
30
|
+
* @param req - Express request
|
|
31
|
+
* @returns Object containing validated pagination, sorting, and additional query params
|
|
32
|
+
*/
|
|
33
|
+
export declare const getPaginationParams: <T = {}>(req: Request) => WithPagination<T>;
|
|
34
|
+
/**
|
|
35
|
+
* Safely parses a string parameter to a number.
|
|
36
|
+
*
|
|
37
|
+
* @param value - String value to parse
|
|
38
|
+
* @param options - Validation options
|
|
39
|
+
* @returns Validation result containing the parsed number or error
|
|
40
|
+
*/
|
|
41
|
+
export declare function parseNumberParam(value: string | undefined, options?: ValidationOptions): ValidationResult<number>;
|
|
42
|
+
/**
|
|
43
|
+
* Safely parses a string parameter to a boolean.
|
|
44
|
+
*
|
|
45
|
+
* @param value - String value to parse
|
|
46
|
+
* @param options - Validation options
|
|
47
|
+
* @returns Validation result containing the parsed boolean or error
|
|
48
|
+
*/
|
|
49
|
+
export declare function parseBooleanParam(value: string | undefined, options?: ValidationOptions): ValidationResult<boolean>;
|
|
50
|
+
/**
|
|
51
|
+
* Extracts pagination parameters from request query parameters.
|
|
52
|
+
*
|
|
53
|
+
* @param query - Query parameters object
|
|
54
|
+
* @param defaultPage - Default page number if not specified (defaults to 1)
|
|
55
|
+
* @param defaultLimit - Default per page size if not specified (defaults to 20)
|
|
56
|
+
* @param maxLimitSize - Maximum allowed per page size (defaults to 100)
|
|
57
|
+
* @returns Object containing validated page and limit
|
|
58
|
+
*/
|
|
59
|
+
export declare function extractPaginationParams(query: Record<string, string | string[]>, defaultPage?: number, defaultLimit?: number, maxLimitSize?: number): {
|
|
60
|
+
page: number;
|
|
61
|
+
limit: number;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Extracts sorting parameters from request query parameters.
|
|
65
|
+
*
|
|
66
|
+
* @param query - Query parameters object
|
|
67
|
+
* @param allowedFields - Array of field names that are allowed to be sorted
|
|
68
|
+
* @param defaultSort - Default sort configuration if not specified
|
|
69
|
+
* @returns Object containing sort field and direction
|
|
70
|
+
*/
|
|
71
|
+
export declare function extractSortParams(query: Record<string, string | string[]>, allowedFields: string[], defaultSort?: {
|
|
72
|
+
sortBy: string;
|
|
73
|
+
sortOrder: 'asc' | 'desc';
|
|
74
|
+
}): {
|
|
75
|
+
sortBy: string;
|
|
76
|
+
sortOrder: 'asc' | 'desc';
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Extracts filter parameters from query parameters based on allowed filter fields.
|
|
80
|
+
*
|
|
81
|
+
* @param query - Query parameters object
|
|
82
|
+
* @param allowedFilters - Array of field names that are allowed to be used as filters
|
|
83
|
+
* @returns Object containing the filters as key-value pairs
|
|
84
|
+
*/
|
|
85
|
+
export declare function extractFilterParams(query: Record<string, string | string[]>, allowedFilters: string[]): Record<string, string | string[]>;
|
|
86
|
+
//# sourceMappingURL=request.utils.d.ts.map
|