@catbee/utils 1.1.0 → 2.0.0-next.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.
- package/LICENSE +1 -1
- package/README.md +27 -27
- package/array/array.utils.d.ts +191 -0
- package/array/index.cjs +246 -0
- package/array/index.d.ts +25 -0
- package/array/index.mjs +228 -0
- package/async/async.utils.d.ts +296 -0
- package/async/index.cjs +428 -0
- package/async/index.d.ts +25 -0
- package/async/index.mjs +407 -0
- package/cache/cache.utils.d.ts +176 -0
- package/cache/index.cjs +292 -0
- package/cache/index.d.ts +25 -0
- package/cache/index.mjs +290 -0
- package/config/config.d.ts +57 -0
- package/config/index.cjs +136 -0
- package/config/index.d.ts +26 -0
- package/config/index.mjs +131 -0
- package/context-store/context-store.utils.d.ts +212 -0
- package/context-store/index.cjs +267 -0
- package/context-store/index.d.ts +25 -0
- package/context-store/index.mjs +261 -0
- package/crypto/crypto.utils.d.ts +183 -0
- package/crypto/index.cjs +182 -0
- package/crypto/index.d.ts +25 -0
- package/crypto/index.mjs +166 -0
- package/date/date.utils.d.ts +190 -0
- package/date/index.cjs +295 -0
- package/date/index.d.ts +25 -0
- package/date/index.mjs +283 -0
- package/decorators/decorators.utils.d.ts +705 -0
- package/decorators/index.cjs +913 -0
- package/decorators/index.d.ts +25 -0
- package/decorators/index.mjs +872 -0
- package/dir/dir.utils.d.ts +216 -0
- package/dir/index.cjs +416 -0
- package/dir/index.d.ts +25 -0
- package/dir/index.mjs +389 -0
- package/env/env.utils.d.ts +400 -0
- package/env/index.cjs +761 -0
- package/env/index.d.ts +25 -0
- package/env/index.mjs +758 -0
- package/exception/exception.utils.d.ts +253 -0
- package/exception/index.cjs +362 -0
- package/exception/index.d.ts +25 -0
- package/exception/index.mjs +338 -0
- package/fs/fs.utils.d.ts +196 -0
- package/fs/index.cjs +253 -0
- package/fs/index.d.ts +25 -0
- package/fs/index.mjs +228 -0
- package/http-status-codes/http-status-codes.d.ts +289 -0
- package/http-status-codes/index.cjs +96 -0
- package/http-status-codes/index.d.ts +25 -0
- package/http-status-codes/index.mjs +94 -0
- package/id/id.utils.d.ts +59 -0
- package/id/index.cjs +62 -0
- package/id/index.d.ts +25 -0
- package/id/index.mjs +56 -0
- package/index.cjs +218 -0
- package/index.d.ts +51 -0
- package/index.mjs +51 -0
- package/logger/index.cjs +334 -0
- package/logger/index.d.ts +25 -0
- package/logger/index.mjs +313 -0
- package/logger/logger.utils.d.ts +210 -0
- package/middleware/index.cjs +177 -0
- package/middleware/index.d.ts +25 -0
- package/middleware/index.mjs +170 -0
- package/middleware/middleware.utils.d.ts +123 -0
- package/obj/index.cjs +317 -0
- package/obj/index.d.ts +25 -0
- package/obj/index.mjs +301 -0
- package/obj/obj.utils.d.ts +156 -0
- package/package.json +171 -19
- package/performance/index.cjs +231 -0
- package/performance/index.d.ts +25 -0
- package/performance/index.mjs +225 -0
- package/performance/performance.utils.d.ts +159 -0
- package/request/index.cjs +202 -0
- package/request/index.d.ts +26 -0
- package/request/index.mjs +194 -0
- package/request/request.utils.d.ts +109 -0
- package/response/index.cjs +234 -0
- package/response/index.d.ts +26 -0
- package/response/index.mjs +222 -0
- package/response/response.utils.d.ts +186 -0
- package/server/index.cjs +1627 -0
- package/server/index.d.ts +28 -0
- package/server/index.mjs +1617 -0
- package/server/server.builder.d.ts +531 -0
- package/server/server.d.ts +303 -0
- package/stream/index.cjs +151 -0
- package/stream/index.d.ts +25 -0
- package/stream/index.mjs +144 -0
- package/stream/stream.utils.d.ts +111 -0
- package/string/index.cjs +109 -0
- package/string/index.d.ts +25 -0
- package/string/index.mjs +95 -0
- package/string/string.utils.d.ts +124 -0
- package/type/index.cjs +129 -0
- package/type/index.d.ts +25 -0
- package/type/index.mjs +119 -0
- package/type/type.utils.d.ts +129 -0
- package/types/api-response.d.ts +175 -0
- package/types/common.d.ts +148 -0
- package/types/config.d.ts +88 -0
- package/types/index.cjs +34 -0
- package/types/index.d.ts +28 -0
- package/types/index.mjs +32 -0
- package/types/server.d.ts +291 -0
- package/url/index.cjs +201 -0
- package/url/index.d.ts +25 -0
- package/url/index.mjs +189 -0
- package/url/url.utils.d.ts +164 -0
- package/validate/index.cjs +212 -0
- package/validate/index.d.ts +25 -0
- package/validate/index.mjs +188 -0
- package/validate/validate.utils.d.ts +200 -0
- package/build/index.cjs +0 -7694
- package/build/index.d.ts +0 -5792
- package/build/index.mjs +0 -7386
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/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
|
+
|
|
25
|
+
'use strict';
|
|
26
|
+
|
|
27
|
+
var cache = require('@catbee/utils/cache');
|
|
28
|
+
var logger = require('@catbee/utils/logger');
|
|
29
|
+
|
|
30
|
+
var __defProp = Object.defineProperty;
|
|
31
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
32
|
+
function timeSync(fn, options = {}) {
|
|
33
|
+
const { label = fn.name || "anonymous function", log = false, logLevel = "debug" } = options;
|
|
34
|
+
const startTime = performance.now();
|
|
35
|
+
const result = fn();
|
|
36
|
+
const endTime = performance.now();
|
|
37
|
+
const durationMs = endTime - startTime;
|
|
38
|
+
const durationSec = durationMs / 1e3;
|
|
39
|
+
const timing = {
|
|
40
|
+
durationMs,
|
|
41
|
+
durationSec,
|
|
42
|
+
startTime,
|
|
43
|
+
endTime,
|
|
44
|
+
label
|
|
45
|
+
};
|
|
46
|
+
if (log) {
|
|
47
|
+
const logger$1 = logger.getLogger();
|
|
48
|
+
const message = `${label} completed in ${durationMs.toFixed(2)}ms`;
|
|
49
|
+
switch (logLevel) {
|
|
50
|
+
case "trace":
|
|
51
|
+
logger$1.trace({
|
|
52
|
+
timing
|
|
53
|
+
}, message);
|
|
54
|
+
break;
|
|
55
|
+
case "debug":
|
|
56
|
+
logger$1.debug({
|
|
57
|
+
timing
|
|
58
|
+
}, message);
|
|
59
|
+
break;
|
|
60
|
+
case "info":
|
|
61
|
+
logger$1.info({
|
|
62
|
+
timing
|
|
63
|
+
}, message);
|
|
64
|
+
break;
|
|
65
|
+
case "warn":
|
|
66
|
+
logger$1.warn({
|
|
67
|
+
timing
|
|
68
|
+
}, message);
|
|
69
|
+
break;
|
|
70
|
+
case "error":
|
|
71
|
+
logger$1.error({
|
|
72
|
+
timing
|
|
73
|
+
}, message);
|
|
74
|
+
break;
|
|
75
|
+
default:
|
|
76
|
+
logger$1.debug({
|
|
77
|
+
timing
|
|
78
|
+
}, message);
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
result,
|
|
84
|
+
timing
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
__name(timeSync, "timeSync");
|
|
88
|
+
async function timeAsync(fn, options = {}) {
|
|
89
|
+
const { label = fn.name || "anonymous async function", log = false, logLevel = "debug" } = options;
|
|
90
|
+
const startTime = performance.now();
|
|
91
|
+
const result = await fn();
|
|
92
|
+
const endTime = performance.now();
|
|
93
|
+
const durationMs = endTime - startTime;
|
|
94
|
+
const durationSec = durationMs / 1e3;
|
|
95
|
+
const timing = {
|
|
96
|
+
durationMs,
|
|
97
|
+
durationSec,
|
|
98
|
+
startTime,
|
|
99
|
+
endTime,
|
|
100
|
+
label
|
|
101
|
+
};
|
|
102
|
+
if (log) {
|
|
103
|
+
const logger$1 = logger.getLogger();
|
|
104
|
+
const message = `${label} completed in ${durationMs.toFixed(2)}ms`;
|
|
105
|
+
switch (logLevel) {
|
|
106
|
+
case "trace":
|
|
107
|
+
logger$1.trace({
|
|
108
|
+
timing
|
|
109
|
+
}, message);
|
|
110
|
+
break;
|
|
111
|
+
case "debug":
|
|
112
|
+
logger$1.debug({
|
|
113
|
+
timing
|
|
114
|
+
}, message);
|
|
115
|
+
break;
|
|
116
|
+
case "info":
|
|
117
|
+
logger$1.info({
|
|
118
|
+
timing
|
|
119
|
+
}, message);
|
|
120
|
+
break;
|
|
121
|
+
case "warn":
|
|
122
|
+
logger$1.warn({
|
|
123
|
+
timing
|
|
124
|
+
}, message);
|
|
125
|
+
break;
|
|
126
|
+
case "error":
|
|
127
|
+
logger$1.error({
|
|
128
|
+
timing
|
|
129
|
+
}, message);
|
|
130
|
+
break;
|
|
131
|
+
default:
|
|
132
|
+
logger$1.debug({
|
|
133
|
+
timing
|
|
134
|
+
}, message);
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
result,
|
|
140
|
+
timing
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
__name(timeAsync, "timeAsync");
|
|
144
|
+
function timed(options = {}) {
|
|
145
|
+
return function(target, propertyKeyOrContext, descriptor) {
|
|
146
|
+
let propertyKey;
|
|
147
|
+
let actualDescriptor;
|
|
148
|
+
if (typeof propertyKeyOrContext === "object" && propertyKeyOrContext !== null && "name" in propertyKeyOrContext) {
|
|
149
|
+
propertyKey = propertyKeyOrContext.name;
|
|
150
|
+
actualDescriptor = descriptor ?? Object.getOwnPropertyDescriptor(target, propertyKey);
|
|
151
|
+
} else {
|
|
152
|
+
propertyKey = propertyKeyOrContext;
|
|
153
|
+
actualDescriptor = descriptor ?? Object.getOwnPropertyDescriptor(target, propertyKey);
|
|
154
|
+
}
|
|
155
|
+
const originalMethod = actualDescriptor.value;
|
|
156
|
+
actualDescriptor.value = function(...args) {
|
|
157
|
+
const methodOptions = {
|
|
158
|
+
...options,
|
|
159
|
+
label: options.label || `${target.constructor.name}.${String(propertyKey)}`
|
|
160
|
+
};
|
|
161
|
+
if (originalMethod.constructor.name === "AsyncFunction") {
|
|
162
|
+
return timeAsync(() => originalMethod.apply(this, args), methodOptions).then(({ result }) => result);
|
|
163
|
+
} else {
|
|
164
|
+
return timeSync(() => originalMethod.apply(this, args), methodOptions).result;
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
if (descriptor) {
|
|
168
|
+
descriptor.value = actualDescriptor.value;
|
|
169
|
+
} else {
|
|
170
|
+
Object.defineProperty(target, propertyKey, actualDescriptor);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
__name(timed, "timed");
|
|
175
|
+
function memoize(fn, options = {}) {
|
|
176
|
+
const { ttl, maxSize, autoCleanupMs, cacheKey = /* @__PURE__ */ __name((...args) => JSON.stringify(args), "cacheKey") } = options;
|
|
177
|
+
const cache$1 = new cache.TTLCache({
|
|
178
|
+
ttlMs: ttl,
|
|
179
|
+
maxSize,
|
|
180
|
+
autoCleanupMs
|
|
181
|
+
});
|
|
182
|
+
return function(...args) {
|
|
183
|
+
const key = cacheKey(...args);
|
|
184
|
+
const cachedValue = cache$1.get(key);
|
|
185
|
+
if (cachedValue !== void 0) {
|
|
186
|
+
return cachedValue;
|
|
187
|
+
}
|
|
188
|
+
const value = fn(...args);
|
|
189
|
+
cache$1.set(key, value);
|
|
190
|
+
return value;
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
__name(memoize, "memoize");
|
|
194
|
+
function trackMemoryUsage(fn, options = {}) {
|
|
195
|
+
const { log = false, label = fn.name || "anonymous function" } = options;
|
|
196
|
+
if (global.gc) {
|
|
197
|
+
global.gc();
|
|
198
|
+
}
|
|
199
|
+
const beforeMemory = process.memoryUsage();
|
|
200
|
+
const result = fn();
|
|
201
|
+
const afterMemory = process.memoryUsage();
|
|
202
|
+
const diff = {};
|
|
203
|
+
for (const key in afterMemory) {
|
|
204
|
+
if (Object.prototype.hasOwnProperty.call(afterMemory, key)) {
|
|
205
|
+
diff[key] = afterMemory[key] - beforeMemory[key];
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (log) {
|
|
209
|
+
logger.getLogger().info({
|
|
210
|
+
label,
|
|
211
|
+
memoryBefore: beforeMemory,
|
|
212
|
+
memoryAfter: afterMemory,
|
|
213
|
+
memoryDiff: diff
|
|
214
|
+
}, `Memory usage for ${label}`);
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
result,
|
|
218
|
+
memoryUsage: {
|
|
219
|
+
before: beforeMemory,
|
|
220
|
+
after: afterMemory,
|
|
221
|
+
diff
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
__name(trackMemoryUsage, "trackMemoryUsage");
|
|
226
|
+
|
|
227
|
+
exports.memoize = memoize;
|
|
228
|
+
exports.timeAsync = timeAsync;
|
|
229
|
+
exports.timeSync = timeSync;
|
|
230
|
+
exports.timed = timed;
|
|
231
|
+
exports.trackMemoryUsage = trackMemoryUsage;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/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
|
+
|
|
25
|
+
export * from './performance.utils';
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/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
|
+
|
|
25
|
+
import { TTLCache } from '@catbee/utils/cache';
|
|
26
|
+
import { getLogger } from '@catbee/utils/logger';
|
|
27
|
+
|
|
28
|
+
var __defProp = Object.defineProperty;
|
|
29
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
30
|
+
function timeSync(fn, options = {}) {
|
|
31
|
+
const { label = fn.name || "anonymous function", log = false, logLevel = "debug" } = options;
|
|
32
|
+
const startTime = performance.now();
|
|
33
|
+
const result = fn();
|
|
34
|
+
const endTime = performance.now();
|
|
35
|
+
const durationMs = endTime - startTime;
|
|
36
|
+
const durationSec = durationMs / 1e3;
|
|
37
|
+
const timing = {
|
|
38
|
+
durationMs,
|
|
39
|
+
durationSec,
|
|
40
|
+
startTime,
|
|
41
|
+
endTime,
|
|
42
|
+
label
|
|
43
|
+
};
|
|
44
|
+
if (log) {
|
|
45
|
+
const logger = getLogger();
|
|
46
|
+
const message = `${label} completed in ${durationMs.toFixed(2)}ms`;
|
|
47
|
+
switch (logLevel) {
|
|
48
|
+
case "trace":
|
|
49
|
+
logger.trace({
|
|
50
|
+
timing
|
|
51
|
+
}, message);
|
|
52
|
+
break;
|
|
53
|
+
case "debug":
|
|
54
|
+
logger.debug({
|
|
55
|
+
timing
|
|
56
|
+
}, message);
|
|
57
|
+
break;
|
|
58
|
+
case "info":
|
|
59
|
+
logger.info({
|
|
60
|
+
timing
|
|
61
|
+
}, message);
|
|
62
|
+
break;
|
|
63
|
+
case "warn":
|
|
64
|
+
logger.warn({
|
|
65
|
+
timing
|
|
66
|
+
}, message);
|
|
67
|
+
break;
|
|
68
|
+
case "error":
|
|
69
|
+
logger.error({
|
|
70
|
+
timing
|
|
71
|
+
}, message);
|
|
72
|
+
break;
|
|
73
|
+
default:
|
|
74
|
+
logger.debug({
|
|
75
|
+
timing
|
|
76
|
+
}, message);
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
result,
|
|
82
|
+
timing
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
__name(timeSync, "timeSync");
|
|
86
|
+
async function timeAsync(fn, options = {}) {
|
|
87
|
+
const { label = fn.name || "anonymous async function", log = false, logLevel = "debug" } = options;
|
|
88
|
+
const startTime = performance.now();
|
|
89
|
+
const result = await fn();
|
|
90
|
+
const endTime = performance.now();
|
|
91
|
+
const durationMs = endTime - startTime;
|
|
92
|
+
const durationSec = durationMs / 1e3;
|
|
93
|
+
const timing = {
|
|
94
|
+
durationMs,
|
|
95
|
+
durationSec,
|
|
96
|
+
startTime,
|
|
97
|
+
endTime,
|
|
98
|
+
label
|
|
99
|
+
};
|
|
100
|
+
if (log) {
|
|
101
|
+
const logger = getLogger();
|
|
102
|
+
const message = `${label} completed in ${durationMs.toFixed(2)}ms`;
|
|
103
|
+
switch (logLevel) {
|
|
104
|
+
case "trace":
|
|
105
|
+
logger.trace({
|
|
106
|
+
timing
|
|
107
|
+
}, message);
|
|
108
|
+
break;
|
|
109
|
+
case "debug":
|
|
110
|
+
logger.debug({
|
|
111
|
+
timing
|
|
112
|
+
}, message);
|
|
113
|
+
break;
|
|
114
|
+
case "info":
|
|
115
|
+
logger.info({
|
|
116
|
+
timing
|
|
117
|
+
}, message);
|
|
118
|
+
break;
|
|
119
|
+
case "warn":
|
|
120
|
+
logger.warn({
|
|
121
|
+
timing
|
|
122
|
+
}, message);
|
|
123
|
+
break;
|
|
124
|
+
case "error":
|
|
125
|
+
logger.error({
|
|
126
|
+
timing
|
|
127
|
+
}, message);
|
|
128
|
+
break;
|
|
129
|
+
default:
|
|
130
|
+
logger.debug({
|
|
131
|
+
timing
|
|
132
|
+
}, message);
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
result,
|
|
138
|
+
timing
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
__name(timeAsync, "timeAsync");
|
|
142
|
+
function timed(options = {}) {
|
|
143
|
+
return function(target, propertyKeyOrContext, descriptor) {
|
|
144
|
+
let propertyKey;
|
|
145
|
+
let actualDescriptor;
|
|
146
|
+
if (typeof propertyKeyOrContext === "object" && propertyKeyOrContext !== null && "name" in propertyKeyOrContext) {
|
|
147
|
+
propertyKey = propertyKeyOrContext.name;
|
|
148
|
+
actualDescriptor = descriptor ?? Object.getOwnPropertyDescriptor(target, propertyKey);
|
|
149
|
+
} else {
|
|
150
|
+
propertyKey = propertyKeyOrContext;
|
|
151
|
+
actualDescriptor = descriptor ?? Object.getOwnPropertyDescriptor(target, propertyKey);
|
|
152
|
+
}
|
|
153
|
+
const originalMethod = actualDescriptor.value;
|
|
154
|
+
actualDescriptor.value = function(...args) {
|
|
155
|
+
const methodOptions = {
|
|
156
|
+
...options,
|
|
157
|
+
label: options.label || `${target.constructor.name}.${String(propertyKey)}`
|
|
158
|
+
};
|
|
159
|
+
if (originalMethod.constructor.name === "AsyncFunction") {
|
|
160
|
+
return timeAsync(() => originalMethod.apply(this, args), methodOptions).then(({ result }) => result);
|
|
161
|
+
} else {
|
|
162
|
+
return timeSync(() => originalMethod.apply(this, args), methodOptions).result;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
if (descriptor) {
|
|
166
|
+
descriptor.value = actualDescriptor.value;
|
|
167
|
+
} else {
|
|
168
|
+
Object.defineProperty(target, propertyKey, actualDescriptor);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
__name(timed, "timed");
|
|
173
|
+
function memoize(fn, options = {}) {
|
|
174
|
+
const { ttl, maxSize, autoCleanupMs, cacheKey = /* @__PURE__ */ __name((...args) => JSON.stringify(args), "cacheKey") } = options;
|
|
175
|
+
const cache = new TTLCache({
|
|
176
|
+
ttlMs: ttl,
|
|
177
|
+
maxSize,
|
|
178
|
+
autoCleanupMs
|
|
179
|
+
});
|
|
180
|
+
return function(...args) {
|
|
181
|
+
const key = cacheKey(...args);
|
|
182
|
+
const cachedValue = cache.get(key);
|
|
183
|
+
if (cachedValue !== void 0) {
|
|
184
|
+
return cachedValue;
|
|
185
|
+
}
|
|
186
|
+
const value = fn(...args);
|
|
187
|
+
cache.set(key, value);
|
|
188
|
+
return value;
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
__name(memoize, "memoize");
|
|
192
|
+
function trackMemoryUsage(fn, options = {}) {
|
|
193
|
+
const { log = false, label = fn.name || "anonymous function" } = options;
|
|
194
|
+
if (global.gc) {
|
|
195
|
+
global.gc();
|
|
196
|
+
}
|
|
197
|
+
const beforeMemory = process.memoryUsage();
|
|
198
|
+
const result = fn();
|
|
199
|
+
const afterMemory = process.memoryUsage();
|
|
200
|
+
const diff = {};
|
|
201
|
+
for (const key in afterMemory) {
|
|
202
|
+
if (Object.prototype.hasOwnProperty.call(afterMemory, key)) {
|
|
203
|
+
diff[key] = afterMemory[key] - beforeMemory[key];
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (log) {
|
|
207
|
+
getLogger().info({
|
|
208
|
+
label,
|
|
209
|
+
memoryBefore: beforeMemory,
|
|
210
|
+
memoryAfter: afterMemory,
|
|
211
|
+
memoryDiff: diff
|
|
212
|
+
}, `Memory usage for ${label}`);
|
|
213
|
+
}
|
|
214
|
+
return {
|
|
215
|
+
result,
|
|
216
|
+
memoryUsage: {
|
|
217
|
+
before: beforeMemory,
|
|
218
|
+
after: afterMemory,
|
|
219
|
+
diff
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
__name(trackMemoryUsage, "trackMemoryUsage");
|
|
224
|
+
|
|
225
|
+
export { memoize, timeAsync, timeSync, timed, trackMemoryUsage };
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/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
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Options for timing function execution.
|
|
27
|
+
*/
|
|
28
|
+
export interface TimingOptions {
|
|
29
|
+
/** Optional label for the timing (defaults to function name) */
|
|
30
|
+
label?: string;
|
|
31
|
+
/** Whether to log the timing (default: false) */
|
|
32
|
+
log?: boolean;
|
|
33
|
+
/** Log level to use if logging is enabled (default: 'debug') */
|
|
34
|
+
logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error';
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Result of a timing operation.
|
|
38
|
+
*/
|
|
39
|
+
export interface TimingResult {
|
|
40
|
+
/** Duration in milliseconds */
|
|
41
|
+
durationMs: number;
|
|
42
|
+
/** Duration in seconds */
|
|
43
|
+
durationSec: number;
|
|
44
|
+
/** Start timestamp */
|
|
45
|
+
startTime: number;
|
|
46
|
+
/** End timestamp */
|
|
47
|
+
endTime: number;
|
|
48
|
+
/** Label used for the timing */
|
|
49
|
+
label: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Measure the execution time of a synchronous function.
|
|
53
|
+
*
|
|
54
|
+
* @param fn - Function to measure
|
|
55
|
+
* @param options - Timing options
|
|
56
|
+
* @returns Result containing the return value and timing information
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```typescript
|
|
60
|
+
* const { result, timing } = timeSync(() => {
|
|
61
|
+
* // Some expensive operation
|
|
62
|
+
* return computeResult();
|
|
63
|
+
* }, { label: 'Computation', log: true });
|
|
64
|
+
*
|
|
65
|
+
* console.log(`Result: ${result}, took ${timing.durationMs}ms`);
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare function timeSync<T>(fn: () => T, options?: TimingOptions): {
|
|
69
|
+
result: T;
|
|
70
|
+
timing: TimingResult;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Measure the execution time of an asynchronous function.
|
|
74
|
+
*
|
|
75
|
+
* @param fn - Async function to measure
|
|
76
|
+
* @param options - Timing options
|
|
77
|
+
* @returns Promise resolving to result and timing information
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* const { result, timing } = await timeAsync(async () => {
|
|
82
|
+
* // Some expensive async operation
|
|
83
|
+
* const data = await fetchData();
|
|
84
|
+
* return processData(data);
|
|
85
|
+
* }, { label: 'API Request', log: true });
|
|
86
|
+
*
|
|
87
|
+
* console.log(`Fetched ${result.length} items in ${timing.durationSec.toFixed(2)}s`);
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export declare function timeAsync<T>(fn: () => Promise<T>, options?: TimingOptions): Promise<{
|
|
91
|
+
result: T;
|
|
92
|
+
timing: TimingResult;
|
|
93
|
+
}>;
|
|
94
|
+
/**
|
|
95
|
+
* Create a timing decorator for class methods.
|
|
96
|
+
*
|
|
97
|
+
* @param options - Timing options
|
|
98
|
+
* @returns Method decorator
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```typescript
|
|
102
|
+
* class DataService {
|
|
103
|
+
* @timed({ log: true, logLevel: 'info' })
|
|
104
|
+
* async fetchData() {
|
|
105
|
+
* // ...implementation
|
|
106
|
+
* }
|
|
107
|
+
* }
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export declare function timed(options?: TimingOptions): (target: any, propertyKeyOrContext: string | symbol | any, descriptor?: PropertyDescriptor) => void;
|
|
111
|
+
/**
|
|
112
|
+
* Memoize function results with optional TTL and max cache size.
|
|
113
|
+
*
|
|
114
|
+
* @param fn - Function to memoize
|
|
115
|
+
* @param options - Memoization options
|
|
116
|
+
* @returns Memoized function
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```typescript
|
|
120
|
+
* // Cache results for 30 seconds, with a maximum of 100 entries
|
|
121
|
+
* const cachedFetch = memoize(
|
|
122
|
+
* async (url) => {
|
|
123
|
+
* const response = await fetch(url);
|
|
124
|
+
* return response.json();
|
|
125
|
+
* },
|
|
126
|
+
* { ttl: 30000, maxSize: 100, cacheKey: (url) => url }
|
|
127
|
+
* );
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
export declare function memoize<T, Args extends any[]>(fn: (...args: Args) => T, options?: {
|
|
131
|
+
/** Time-to-live in milliseconds (default: indefinite) */
|
|
132
|
+
ttl?: number;
|
|
133
|
+
/** Maximum cache size (default: unlimited) */
|
|
134
|
+
maxSize?: number;
|
|
135
|
+
/** Function to generate a cache key from arguments */
|
|
136
|
+
cacheKey?: (...args: Args) => string;
|
|
137
|
+
/** Auto-cleanup interval in milliseconds (default: disabled) */
|
|
138
|
+
autoCleanupMs?: number;
|
|
139
|
+
}): (...args: Args) => T;
|
|
140
|
+
/**
|
|
141
|
+
* Track memory usage for a function execution.
|
|
142
|
+
*
|
|
143
|
+
* @param fn - Function to track
|
|
144
|
+
* @param options - Memory tracking options
|
|
145
|
+
* @returns Result and memory usage information
|
|
146
|
+
*/
|
|
147
|
+
export declare function trackMemoryUsage<T>(fn: () => T, options?: {
|
|
148
|
+
/** Whether to log the memory usage (default: false) */
|
|
149
|
+
log?: boolean;
|
|
150
|
+
/** Label for the memory tracking (default: function name) */
|
|
151
|
+
label?: string;
|
|
152
|
+
}): {
|
|
153
|
+
result: T;
|
|
154
|
+
memoryUsage: {
|
|
155
|
+
before: NodeJS.MemoryUsage;
|
|
156
|
+
after: NodeJS.MemoryUsage;
|
|
157
|
+
diff: Record<string, number>;
|
|
158
|
+
};
|
|
159
|
+
};
|