@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.
Files changed (121) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +27 -27
  3. package/array/array.utils.d.ts +191 -0
  4. package/array/index.cjs +246 -0
  5. package/array/index.d.ts +25 -0
  6. package/array/index.mjs +228 -0
  7. package/async/async.utils.d.ts +296 -0
  8. package/async/index.cjs +428 -0
  9. package/async/index.d.ts +25 -0
  10. package/async/index.mjs +407 -0
  11. package/cache/cache.utils.d.ts +176 -0
  12. package/cache/index.cjs +292 -0
  13. package/cache/index.d.ts +25 -0
  14. package/cache/index.mjs +290 -0
  15. package/config/config.d.ts +57 -0
  16. package/config/index.cjs +136 -0
  17. package/config/index.d.ts +26 -0
  18. package/config/index.mjs +131 -0
  19. package/context-store/context-store.utils.d.ts +212 -0
  20. package/context-store/index.cjs +267 -0
  21. package/context-store/index.d.ts +25 -0
  22. package/context-store/index.mjs +261 -0
  23. package/crypto/crypto.utils.d.ts +183 -0
  24. package/crypto/index.cjs +182 -0
  25. package/crypto/index.d.ts +25 -0
  26. package/crypto/index.mjs +166 -0
  27. package/date/date.utils.d.ts +190 -0
  28. package/date/index.cjs +295 -0
  29. package/date/index.d.ts +25 -0
  30. package/date/index.mjs +283 -0
  31. package/decorators/decorators.utils.d.ts +705 -0
  32. package/decorators/index.cjs +913 -0
  33. package/decorators/index.d.ts +25 -0
  34. package/decorators/index.mjs +872 -0
  35. package/dir/dir.utils.d.ts +216 -0
  36. package/dir/index.cjs +416 -0
  37. package/dir/index.d.ts +25 -0
  38. package/dir/index.mjs +389 -0
  39. package/env/env.utils.d.ts +400 -0
  40. package/env/index.cjs +761 -0
  41. package/env/index.d.ts +25 -0
  42. package/env/index.mjs +758 -0
  43. package/exception/exception.utils.d.ts +253 -0
  44. package/exception/index.cjs +362 -0
  45. package/exception/index.d.ts +25 -0
  46. package/exception/index.mjs +338 -0
  47. package/fs/fs.utils.d.ts +196 -0
  48. package/fs/index.cjs +253 -0
  49. package/fs/index.d.ts +25 -0
  50. package/fs/index.mjs +228 -0
  51. package/http-status-codes/http-status-codes.d.ts +289 -0
  52. package/http-status-codes/index.cjs +96 -0
  53. package/http-status-codes/index.d.ts +25 -0
  54. package/http-status-codes/index.mjs +94 -0
  55. package/id/id.utils.d.ts +59 -0
  56. package/id/index.cjs +62 -0
  57. package/id/index.d.ts +25 -0
  58. package/id/index.mjs +56 -0
  59. package/index.cjs +218 -0
  60. package/index.d.ts +51 -0
  61. package/index.mjs +51 -0
  62. package/logger/index.cjs +334 -0
  63. package/logger/index.d.ts +25 -0
  64. package/logger/index.mjs +313 -0
  65. package/logger/logger.utils.d.ts +210 -0
  66. package/middleware/index.cjs +177 -0
  67. package/middleware/index.d.ts +25 -0
  68. package/middleware/index.mjs +170 -0
  69. package/middleware/middleware.utils.d.ts +123 -0
  70. package/obj/index.cjs +317 -0
  71. package/obj/index.d.ts +25 -0
  72. package/obj/index.mjs +301 -0
  73. package/obj/obj.utils.d.ts +156 -0
  74. package/package.json +171 -19
  75. package/performance/index.cjs +231 -0
  76. package/performance/index.d.ts +25 -0
  77. package/performance/index.mjs +225 -0
  78. package/performance/performance.utils.d.ts +159 -0
  79. package/request/index.cjs +202 -0
  80. package/request/index.d.ts +26 -0
  81. package/request/index.mjs +194 -0
  82. package/request/request.utils.d.ts +109 -0
  83. package/response/index.cjs +234 -0
  84. package/response/index.d.ts +26 -0
  85. package/response/index.mjs +222 -0
  86. package/response/response.utils.d.ts +186 -0
  87. package/server/index.cjs +1627 -0
  88. package/server/index.d.ts +28 -0
  89. package/server/index.mjs +1617 -0
  90. package/server/server.builder.d.ts +531 -0
  91. package/server/server.d.ts +303 -0
  92. package/stream/index.cjs +151 -0
  93. package/stream/index.d.ts +25 -0
  94. package/stream/index.mjs +144 -0
  95. package/stream/stream.utils.d.ts +111 -0
  96. package/string/index.cjs +109 -0
  97. package/string/index.d.ts +25 -0
  98. package/string/index.mjs +95 -0
  99. package/string/string.utils.d.ts +124 -0
  100. package/type/index.cjs +129 -0
  101. package/type/index.d.ts +25 -0
  102. package/type/index.mjs +119 -0
  103. package/type/type.utils.d.ts +129 -0
  104. package/types/api-response.d.ts +175 -0
  105. package/types/common.d.ts +148 -0
  106. package/types/config.d.ts +88 -0
  107. package/types/index.cjs +34 -0
  108. package/types/index.d.ts +28 -0
  109. package/types/index.mjs +32 -0
  110. package/types/server.d.ts +291 -0
  111. package/url/index.cjs +201 -0
  112. package/url/index.d.ts +25 -0
  113. package/url/index.mjs +189 -0
  114. package/url/url.utils.d.ts +164 -0
  115. package/validate/index.cjs +212 -0
  116. package/validate/index.d.ts +25 -0
  117. package/validate/index.mjs +188 -0
  118. package/validate/validate.utils.d.ts +200 -0
  119. package/build/index.cjs +0 -7694
  120. package/build/index.d.ts +0 -5792
  121. package/build/index.mjs +0 -7386
@@ -0,0 +1,292 @@
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 config = require('@catbee/utils/config');
28
+
29
+ var __defProp = Object.defineProperty;
30
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
31
+ var TTLCache = class {
32
+ static {
33
+ __name(this, "TTLCache");
34
+ }
35
+ cache = /* @__PURE__ */ new Map();
36
+ ttlMs;
37
+ maxSize;
38
+ cleanupInterval;
39
+ /**
40
+ * @param options - Configuration options for the cache
41
+ */
42
+ constructor(options = {}) {
43
+ this.ttlMs = options.ttlMs ?? config.defaultCatbeeConfig.cache.defaultTtl;
44
+ this.maxSize = options.maxSize;
45
+ const autoCleanupMs = options.autoCleanupMs;
46
+ if (autoCleanupMs && autoCleanupMs > 0) {
47
+ this.cleanupInterval = setInterval(() => {
48
+ this.cleanup();
49
+ }, autoCleanupMs);
50
+ }
51
+ }
52
+ /**
53
+ * Sets a key-value pair in the cache with the default TTL.
54
+ *
55
+ * @param key - The key to set.
56
+ * @param value - The value to associate with the key.
57
+ */
58
+ set(key, value) {
59
+ this.setWithTTL(key, value, this.ttlMs);
60
+ }
61
+ /**
62
+ * Sets a key-value pair in the cache with a custom TTL.
63
+ *
64
+ * @param key - The key to set.
65
+ * @param value - The value to associate with the key.
66
+ * @param ttlMs - Time-to-live in milliseconds.
67
+ */
68
+ setWithTTL(key, value, ttlMs) {
69
+ const now = Date.now();
70
+ const expiresAt = now + ttlMs;
71
+ if (this.cache.has(key)) {
72
+ this.cache.delete(key);
73
+ }
74
+ this.cache.set(key, {
75
+ value,
76
+ expiresAt,
77
+ lastAccessed: now
78
+ });
79
+ if (this.maxSize && this.cache.size > this.maxSize) {
80
+ this.evictLRU();
81
+ }
82
+ }
83
+ /**
84
+ * Retrieves the value for a given key if it hasn't expired.
85
+ *
86
+ * @param key - The key to retrieve.
87
+ * @returns The cached value, or undefined if not found or expired.
88
+ */
89
+ get(key) {
90
+ const entry = this.cache.get(key);
91
+ if (!entry) return void 0;
92
+ const now = Date.now();
93
+ if (now > entry.expiresAt) {
94
+ this.cache.delete(key);
95
+ return void 0;
96
+ }
97
+ entry.lastAccessed = now;
98
+ this.cache.delete(key);
99
+ this.cache.set(key, entry);
100
+ return entry.value;
101
+ }
102
+ /**
103
+ * Retrieves or computes a value if it's not in the cache or has expired.
104
+ *
105
+ * @param key - The key to retrieve
106
+ * @param producer - Function to generate the value if not cached
107
+ * @param ttlMs - Optional custom TTL for the computed value
108
+ * @returns The cached or computed value
109
+ */
110
+ async getOrCompute(key, producer, ttlMs) {
111
+ const value = this.get(key);
112
+ if (value !== void 0) return value;
113
+ const newValue = await producer();
114
+ this.setWithTTL(key, newValue, ttlMs ?? this.ttlMs);
115
+ return newValue;
116
+ }
117
+ /**
118
+ * Checks if the key exists and hasn't expired.
119
+ *
120
+ * @param key - The key to check.
121
+ * @returns `true` if key exists and is valid, otherwise `false`.
122
+ */
123
+ has(key) {
124
+ return this.get(key) !== void 0;
125
+ }
126
+ /**
127
+ * Deletes a key from the cache.
128
+ *
129
+ * @param key - The key to delete.
130
+ * @returns `true` if the key existed and was removed, `false` otherwise.
131
+ */
132
+ delete(key) {
133
+ return this.cache.delete(key);
134
+ }
135
+ /**
136
+ * Clears all entries from the cache.
137
+ */
138
+ clear() {
139
+ this.cache.clear();
140
+ }
141
+ /**
142
+ * Returns the number of entries currently in the cache (includes expired entries until next access/cleanup).
143
+ *
144
+ * @returns Number of items in the cache (may include expired keys).
145
+ */
146
+ size() {
147
+ return this.cache.size;
148
+ }
149
+ /**
150
+ * Set multiple key-value pairs at once with the default TTL.
151
+ *
152
+ * @param entries - Array of [key, value] tuples to set
153
+ */
154
+ setMany(entries) {
155
+ for (const [key, value] of entries) {
156
+ this.set(key, value);
157
+ }
158
+ }
159
+ /**
160
+ * Get multiple values at once.
161
+ *
162
+ * @param keys - Array of keys to retrieve
163
+ * @returns Array of values (undefined for keys that don't exist or expired)
164
+ */
165
+ getMany(keys) {
166
+ return keys.map((key) => this.get(key));
167
+ }
168
+ /**
169
+ * Removes all expired entries from the cache.
170
+ *
171
+ * @returns Number of entries removed.
172
+ */
173
+ cleanup() {
174
+ let removed = 0;
175
+ const now = Date.now();
176
+ for (const [key, entry] of this.cache.entries()) {
177
+ if (now > entry.expiresAt) {
178
+ this.cache.delete(key);
179
+ removed++;
180
+ }
181
+ }
182
+ return removed;
183
+ }
184
+ /**
185
+ * Returns an iterator of all current valid [key, value] pairs.
186
+ *
187
+ * @returns IterableIterator<[K, V]>
188
+ */
189
+ *entries() {
190
+ for (const [key, entry] of this.cache.entries()) {
191
+ if (Date.now() <= entry.expiresAt) {
192
+ yield [
193
+ key,
194
+ entry.value
195
+ ];
196
+ }
197
+ }
198
+ }
199
+ /**
200
+ * Returns an iterator of all current valid keys.
201
+ *
202
+ * @returns IterableIterator<K>
203
+ */
204
+ *keys() {
205
+ for (const [key, entry] of this.cache.entries()) {
206
+ if (Date.now() <= entry.expiresAt) {
207
+ yield key;
208
+ }
209
+ }
210
+ }
211
+ /**
212
+ * Returns an iterator of all current valid values.
213
+ *
214
+ * @returns IterableIterator<V>
215
+ */
216
+ *values() {
217
+ for (const entry of this.cache.values()) {
218
+ if (Date.now() <= entry.expiresAt) {
219
+ yield entry.value;
220
+ }
221
+ }
222
+ }
223
+ /**
224
+ * Extends the expiration of a key by the specified time or default TTL.
225
+ *
226
+ * @param key - The key to refresh
227
+ * @param ttlMs - Optional new TTL in milliseconds (uses default if not specified)
228
+ * @returns true if the key was found and refreshed, false otherwise
229
+ */
230
+ refresh(key, ttlMs) {
231
+ const entry = this.cache.get(key);
232
+ if (!entry) return false;
233
+ const now = Date.now();
234
+ if (now > entry.expiresAt) {
235
+ this.cache.delete(key);
236
+ return false;
237
+ }
238
+ entry.expiresAt = now + (ttlMs ?? this.ttlMs);
239
+ entry.lastAccessed = now;
240
+ return true;
241
+ }
242
+ /**
243
+ * Returns a snapshot of cache stats.
244
+ *
245
+ * @returns Object containing cache statistics
246
+ */
247
+ stats() {
248
+ const now = Date.now();
249
+ let expired = 0;
250
+ let valid = 0;
251
+ for (const entry of this.cache.values()) {
252
+ if (now > entry.expiresAt) {
253
+ expired++;
254
+ } else {
255
+ valid++;
256
+ }
257
+ }
258
+ return {
259
+ size: this.cache.size,
260
+ validEntries: valid,
261
+ expiredEntries: expired,
262
+ maxSize: this.maxSize
263
+ };
264
+ }
265
+ /**
266
+ * Stop the auto-cleanup interval if it's running.
267
+ */
268
+ destroy() {
269
+ if (this.cleanupInterval) {
270
+ clearInterval(this.cleanupInterval);
271
+ this.cleanupInterval = void 0;
272
+ }
273
+ }
274
+ /**
275
+ * Evict the least recently used entry from the cache.
276
+ * @private
277
+ */
278
+ evictLRU() {
279
+ const now = Date.now();
280
+ for (const [key, entry] of Array.from(this.cache.entries())) {
281
+ if (now > entry.expiresAt) {
282
+ this.cache.delete(key);
283
+ }
284
+ }
285
+ while (this.maxSize && this.cache.size > this.maxSize) {
286
+ const oldestKey = this.cache.keys().next().value;
287
+ this.cache.delete(oldestKey);
288
+ }
289
+ }
290
+ };
291
+
292
+ exports.TTLCache = TTLCache;
@@ -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 './cache.utils';
@@ -0,0 +1,290 @@
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 { defaultCatbeeConfig } from '@catbee/utils/config';
26
+
27
+ var __defProp = Object.defineProperty;
28
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
29
+ var TTLCache = class {
30
+ static {
31
+ __name(this, "TTLCache");
32
+ }
33
+ cache = /* @__PURE__ */ new Map();
34
+ ttlMs;
35
+ maxSize;
36
+ cleanupInterval;
37
+ /**
38
+ * @param options - Configuration options for the cache
39
+ */
40
+ constructor(options = {}) {
41
+ this.ttlMs = options.ttlMs ?? defaultCatbeeConfig.cache.defaultTtl;
42
+ this.maxSize = options.maxSize;
43
+ const autoCleanupMs = options.autoCleanupMs;
44
+ if (autoCleanupMs && autoCleanupMs > 0) {
45
+ this.cleanupInterval = setInterval(() => {
46
+ this.cleanup();
47
+ }, autoCleanupMs);
48
+ }
49
+ }
50
+ /**
51
+ * Sets a key-value pair in the cache with the default TTL.
52
+ *
53
+ * @param key - The key to set.
54
+ * @param value - The value to associate with the key.
55
+ */
56
+ set(key, value) {
57
+ this.setWithTTL(key, value, this.ttlMs);
58
+ }
59
+ /**
60
+ * Sets a key-value pair in the cache with a custom TTL.
61
+ *
62
+ * @param key - The key to set.
63
+ * @param value - The value to associate with the key.
64
+ * @param ttlMs - Time-to-live in milliseconds.
65
+ */
66
+ setWithTTL(key, value, ttlMs) {
67
+ const now = Date.now();
68
+ const expiresAt = now + ttlMs;
69
+ if (this.cache.has(key)) {
70
+ this.cache.delete(key);
71
+ }
72
+ this.cache.set(key, {
73
+ value,
74
+ expiresAt,
75
+ lastAccessed: now
76
+ });
77
+ if (this.maxSize && this.cache.size > this.maxSize) {
78
+ this.evictLRU();
79
+ }
80
+ }
81
+ /**
82
+ * Retrieves the value for a given key if it hasn't expired.
83
+ *
84
+ * @param key - The key to retrieve.
85
+ * @returns The cached value, or undefined if not found or expired.
86
+ */
87
+ get(key) {
88
+ const entry = this.cache.get(key);
89
+ if (!entry) return void 0;
90
+ const now = Date.now();
91
+ if (now > entry.expiresAt) {
92
+ this.cache.delete(key);
93
+ return void 0;
94
+ }
95
+ entry.lastAccessed = now;
96
+ this.cache.delete(key);
97
+ this.cache.set(key, entry);
98
+ return entry.value;
99
+ }
100
+ /**
101
+ * Retrieves or computes a value if it's not in the cache or has expired.
102
+ *
103
+ * @param key - The key to retrieve
104
+ * @param producer - Function to generate the value if not cached
105
+ * @param ttlMs - Optional custom TTL for the computed value
106
+ * @returns The cached or computed value
107
+ */
108
+ async getOrCompute(key, producer, ttlMs) {
109
+ const value = this.get(key);
110
+ if (value !== void 0) return value;
111
+ const newValue = await producer();
112
+ this.setWithTTL(key, newValue, ttlMs ?? this.ttlMs);
113
+ return newValue;
114
+ }
115
+ /**
116
+ * Checks if the key exists and hasn't expired.
117
+ *
118
+ * @param key - The key to check.
119
+ * @returns `true` if key exists and is valid, otherwise `false`.
120
+ */
121
+ has(key) {
122
+ return this.get(key) !== void 0;
123
+ }
124
+ /**
125
+ * Deletes a key from the cache.
126
+ *
127
+ * @param key - The key to delete.
128
+ * @returns `true` if the key existed and was removed, `false` otherwise.
129
+ */
130
+ delete(key) {
131
+ return this.cache.delete(key);
132
+ }
133
+ /**
134
+ * Clears all entries from the cache.
135
+ */
136
+ clear() {
137
+ this.cache.clear();
138
+ }
139
+ /**
140
+ * Returns the number of entries currently in the cache (includes expired entries until next access/cleanup).
141
+ *
142
+ * @returns Number of items in the cache (may include expired keys).
143
+ */
144
+ size() {
145
+ return this.cache.size;
146
+ }
147
+ /**
148
+ * Set multiple key-value pairs at once with the default TTL.
149
+ *
150
+ * @param entries - Array of [key, value] tuples to set
151
+ */
152
+ setMany(entries) {
153
+ for (const [key, value] of entries) {
154
+ this.set(key, value);
155
+ }
156
+ }
157
+ /**
158
+ * Get multiple values at once.
159
+ *
160
+ * @param keys - Array of keys to retrieve
161
+ * @returns Array of values (undefined for keys that don't exist or expired)
162
+ */
163
+ getMany(keys) {
164
+ return keys.map((key) => this.get(key));
165
+ }
166
+ /**
167
+ * Removes all expired entries from the cache.
168
+ *
169
+ * @returns Number of entries removed.
170
+ */
171
+ cleanup() {
172
+ let removed = 0;
173
+ const now = Date.now();
174
+ for (const [key, entry] of this.cache.entries()) {
175
+ if (now > entry.expiresAt) {
176
+ this.cache.delete(key);
177
+ removed++;
178
+ }
179
+ }
180
+ return removed;
181
+ }
182
+ /**
183
+ * Returns an iterator of all current valid [key, value] pairs.
184
+ *
185
+ * @returns IterableIterator<[K, V]>
186
+ */
187
+ *entries() {
188
+ for (const [key, entry] of this.cache.entries()) {
189
+ if (Date.now() <= entry.expiresAt) {
190
+ yield [
191
+ key,
192
+ entry.value
193
+ ];
194
+ }
195
+ }
196
+ }
197
+ /**
198
+ * Returns an iterator of all current valid keys.
199
+ *
200
+ * @returns IterableIterator<K>
201
+ */
202
+ *keys() {
203
+ for (const [key, entry] of this.cache.entries()) {
204
+ if (Date.now() <= entry.expiresAt) {
205
+ yield key;
206
+ }
207
+ }
208
+ }
209
+ /**
210
+ * Returns an iterator of all current valid values.
211
+ *
212
+ * @returns IterableIterator<V>
213
+ */
214
+ *values() {
215
+ for (const entry of this.cache.values()) {
216
+ if (Date.now() <= entry.expiresAt) {
217
+ yield entry.value;
218
+ }
219
+ }
220
+ }
221
+ /**
222
+ * Extends the expiration of a key by the specified time or default TTL.
223
+ *
224
+ * @param key - The key to refresh
225
+ * @param ttlMs - Optional new TTL in milliseconds (uses default if not specified)
226
+ * @returns true if the key was found and refreshed, false otherwise
227
+ */
228
+ refresh(key, ttlMs) {
229
+ const entry = this.cache.get(key);
230
+ if (!entry) return false;
231
+ const now = Date.now();
232
+ if (now > entry.expiresAt) {
233
+ this.cache.delete(key);
234
+ return false;
235
+ }
236
+ entry.expiresAt = now + (ttlMs ?? this.ttlMs);
237
+ entry.lastAccessed = now;
238
+ return true;
239
+ }
240
+ /**
241
+ * Returns a snapshot of cache stats.
242
+ *
243
+ * @returns Object containing cache statistics
244
+ */
245
+ stats() {
246
+ const now = Date.now();
247
+ let expired = 0;
248
+ let valid = 0;
249
+ for (const entry of this.cache.values()) {
250
+ if (now > entry.expiresAt) {
251
+ expired++;
252
+ } else {
253
+ valid++;
254
+ }
255
+ }
256
+ return {
257
+ size: this.cache.size,
258
+ validEntries: valid,
259
+ expiredEntries: expired,
260
+ maxSize: this.maxSize
261
+ };
262
+ }
263
+ /**
264
+ * Stop the auto-cleanup interval if it's running.
265
+ */
266
+ destroy() {
267
+ if (this.cleanupInterval) {
268
+ clearInterval(this.cleanupInterval);
269
+ this.cleanupInterval = void 0;
270
+ }
271
+ }
272
+ /**
273
+ * Evict the least recently used entry from the cache.
274
+ * @private
275
+ */
276
+ evictLRU() {
277
+ const now = Date.now();
278
+ for (const [key, entry] of Array.from(this.cache.entries())) {
279
+ if (now > entry.expiresAt) {
280
+ this.cache.delete(key);
281
+ }
282
+ }
283
+ while (this.maxSize && this.cache.size > this.maxSize) {
284
+ const oldestKey = this.cache.keys().next().value;
285
+ this.cache.delete(oldestKey);
286
+ }
287
+ }
288
+ };
289
+
290
+ export { TTLCache };
@@ -0,0 +1,57 @@
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 type { CatbeeConfig, ServerConfig } from '@catbee/utils/types';
26
+ import type { Logger } from '@catbee/utils/logger';
27
+ /**
28
+ * Extends Express Request interface to add request ID tracking.
29
+ * Added by requestId middleware during request processing.
30
+ */
31
+ declare global {
32
+ namespace Express {
33
+ interface Request {
34
+ /** Unique request identifier (set by middleware) */
35
+ id?: string;
36
+ /** Logger instance for the request (set by middleware) */
37
+ logger?: Logger;
38
+ /** User information (set by authentication middleware) */
39
+ user?: any;
40
+ }
41
+ }
42
+ }
43
+ /**
44
+ * Application runtime configuration loaded from environment variables.
45
+ */
46
+ export declare let defaultCatbeeConfig: CatbeeConfig;
47
+ export declare const defaultServerConfig: ServerConfig;
48
+ /**
49
+ * Update the @catbee/utils configuration.
50
+ * @param value Partial configuration object.
51
+ */
52
+ export declare function setConfig(value: Partial<CatbeeConfig>): void;
53
+ /**
54
+ * Get the current @catbee/utils configuration.
55
+ * @returns The current configuration object.
56
+ */
57
+ export declare function getConfig(): CatbeeConfig;