@catbee/utils 1.1.0 → 2.0.0-next.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.
Files changed (90) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +79 -43
  3. package/array/index.cjs +355 -0
  4. package/array/index.d.ts +317 -0
  5. package/array/index.mjs +327 -0
  6. package/async/index.cjs +484 -0
  7. package/async/index.d.ts +299 -0
  8. package/async/index.mjs +463 -0
  9. package/cache/index.cjs +292 -0
  10. package/cache/index.d.ts +179 -0
  11. package/cache/index.mjs +290 -0
  12. package/config/index.cjs +150 -0
  13. package/config/index.d.ts +88 -0
  14. package/config/index.mjs +143 -0
  15. package/context-store/index.cjs +267 -0
  16. package/context-store/index.d.ts +216 -0
  17. package/context-store/index.mjs +261 -0
  18. package/crypto/index.cjs +182 -0
  19. package/crypto/index.d.ts +187 -0
  20. package/crypto/index.mjs +166 -0
  21. package/date/index.cjs +340 -0
  22. package/date/index.d.ts +214 -0
  23. package/date/index.mjs +326 -0
  24. package/decorators/index.cjs +2051 -0
  25. package/decorators/index.d.ts +708 -0
  26. package/decorators/index.mjs +2010 -0
  27. package/dir/index.cjs +417 -0
  28. package/dir/index.d.ts +219 -0
  29. package/dir/index.mjs +390 -0
  30. package/env/index.cjs +745 -0
  31. package/env/index.d.ts +403 -0
  32. package/env/index.mjs +742 -0
  33. package/exception/index.cjs +362 -0
  34. package/exception/index.d.ts +256 -0
  35. package/exception/index.mjs +338 -0
  36. package/fs/index.cjs +287 -0
  37. package/fs/index.d.ts +229 -0
  38. package/fs/index.mjs +258 -0
  39. package/http-status-codes/index.cjs +96 -0
  40. package/http-status-codes/index.d.ts +291 -0
  41. package/http-status-codes/index.mjs +94 -0
  42. package/id/index.cjs +62 -0
  43. package/id/index.d.ts +61 -0
  44. package/id/index.mjs +56 -0
  45. package/index.cjs +218 -0
  46. package/index.d.ts +51 -0
  47. package/index.mjs +51 -0
  48. package/logger/index.cjs +334 -0
  49. package/logger/index.d.ts +213 -0
  50. package/logger/index.mjs +313 -0
  51. package/middleware/index.cjs +177 -0
  52. package/middleware/index.d.ts +127 -0
  53. package/middleware/index.mjs +170 -0
  54. package/obj/index.cjs +305 -0
  55. package/obj/index.d.ts +160 -0
  56. package/obj/index.mjs +289 -0
  57. package/package.json +172 -20
  58. package/performance/index.cjs +231 -0
  59. package/performance/index.d.ts +162 -0
  60. package/performance/index.mjs +225 -0
  61. package/request/index.cjs +202 -0
  62. package/request/index.d.ts +265 -0
  63. package/request/index.mjs +194 -0
  64. package/response/index.cjs +234 -0
  65. package/response/index.d.ts +342 -0
  66. package/response/index.mjs +222 -0
  67. package/server/index.cjs +1631 -0
  68. package/server/index.d.ts +809 -0
  69. package/server/index.mjs +1622 -0
  70. package/stream/index.cjs +151 -0
  71. package/stream/index.d.ts +114 -0
  72. package/stream/index.mjs +144 -0
  73. package/string/index.cjs +109 -0
  74. package/string/index.d.ts +126 -0
  75. package/string/index.mjs +95 -0
  76. package/type/index.cjs +129 -0
  77. package/type/index.d.ts +131 -0
  78. package/type/index.mjs +119 -0
  79. package/types/index.cjs +34 -0
  80. package/types/index.d.ts +798 -0
  81. package/types/index.mjs +32 -0
  82. package/url/index.cjs +199 -0
  83. package/url/index.d.ts +166 -0
  84. package/url/index.mjs +187 -0
  85. package/validation/index.cjs +259 -0
  86. package/validation/index.d.ts +209 -0
  87. package/validation/index.mjs +231 -0
  88. package/build/index.cjs +0 -7694
  89. package/build/index.d.ts +0 -5792
  90. 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.getCatbeeGlobalConfig().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,179 @@
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 configuring a TTLCache instance.
27
+ */
28
+ interface TTLCacheOptions {
29
+ /** Default time-to-live in milliseconds for cache entries */
30
+ ttlMs?: number;
31
+ /** Maximum number of entries to keep in cache (uses LRU eviction policy) */
32
+ maxSize?: number;
33
+ /** Auto-cleanup interval in milliseconds (disabled if 0 or negative) */
34
+ autoCleanupMs?: number;
35
+ }
36
+ /**
37
+ * An in-memory cache with time-to-live (TTL) support for each entry.
38
+ *
39
+ * @typeParam K - Type of the key.
40
+ * @typeParam V - Type of the value.
41
+ *
42
+ * @example
43
+ * const cache = new TTLCache<string, number>({ ttlMs: 1000 });
44
+ * cache.set("x", 123);
45
+ * const value = cache.get("x"); // 123
46
+ */
47
+ declare class TTLCache<K, V> {
48
+ private readonly cache;
49
+ private readonly ttlMs;
50
+ private readonly maxSize?;
51
+ private cleanupInterval?;
52
+ /**
53
+ * @param options - Configuration options for the cache
54
+ */
55
+ constructor(options?: TTLCacheOptions);
56
+ /**
57
+ * Sets a key-value pair in the cache with the default TTL.
58
+ *
59
+ * @param key - The key to set.
60
+ * @param value - The value to associate with the key.
61
+ */
62
+ set(key: K, value: V): void;
63
+ /**
64
+ * Sets a key-value pair in the cache with a custom TTL.
65
+ *
66
+ * @param key - The key to set.
67
+ * @param value - The value to associate with the key.
68
+ * @param ttlMs - Time-to-live in milliseconds.
69
+ */
70
+ setWithTTL(key: K, value: V, ttlMs: number): void;
71
+ /**
72
+ * Retrieves the value for a given key if it hasn't expired.
73
+ *
74
+ * @param key - The key to retrieve.
75
+ * @returns The cached value, or undefined if not found or expired.
76
+ */
77
+ get(key: K): V | undefined;
78
+ /**
79
+ * Retrieves or computes a value if it's not in the cache or has expired.
80
+ *
81
+ * @param key - The key to retrieve
82
+ * @param producer - Function to generate the value if not cached
83
+ * @param ttlMs - Optional custom TTL for the computed value
84
+ * @returns The cached or computed value
85
+ */
86
+ getOrCompute(key: K, producer: () => Promise<V>, ttlMs?: number): Promise<V>;
87
+ /**
88
+ * Checks if the key exists and hasn't expired.
89
+ *
90
+ * @param key - The key to check.
91
+ * @returns `true` if key exists and is valid, otherwise `false`.
92
+ */
93
+ has(key: K): boolean;
94
+ /**
95
+ * Deletes a key from the cache.
96
+ *
97
+ * @param key - The key to delete.
98
+ * @returns `true` if the key existed and was removed, `false` otherwise.
99
+ */
100
+ delete(key: K): boolean;
101
+ /**
102
+ * Clears all entries from the cache.
103
+ */
104
+ clear(): void;
105
+ /**
106
+ * Returns the number of entries currently in the cache (includes expired entries until next access/cleanup).
107
+ *
108
+ * @returns Number of items in the cache (may include expired keys).
109
+ */
110
+ size(): number;
111
+ /**
112
+ * Set multiple key-value pairs at once with the default TTL.
113
+ *
114
+ * @param entries - Array of [key, value] tuples to set
115
+ */
116
+ setMany(entries: [K, V][]): void;
117
+ /**
118
+ * Get multiple values at once.
119
+ *
120
+ * @param keys - Array of keys to retrieve
121
+ * @returns Array of values (undefined for keys that don't exist or expired)
122
+ */
123
+ getMany(keys: K[]): (V | undefined)[];
124
+ /**
125
+ * Removes all expired entries from the cache.
126
+ *
127
+ * @returns Number of entries removed.
128
+ */
129
+ cleanup(): number;
130
+ /**
131
+ * Returns an iterator of all current valid [key, value] pairs.
132
+ *
133
+ * @returns IterableIterator<[K, V]>
134
+ */
135
+ entries(): IterableIterator<[K, V]>;
136
+ /**
137
+ * Returns an iterator of all current valid keys.
138
+ *
139
+ * @returns IterableIterator<K>
140
+ */
141
+ keys(): IterableIterator<K>;
142
+ /**
143
+ * Returns an iterator of all current valid values.
144
+ *
145
+ * @returns IterableIterator<V>
146
+ */
147
+ values(): IterableIterator<V>;
148
+ /**
149
+ * Extends the expiration of a key by the specified time or default TTL.
150
+ *
151
+ * @param key - The key to refresh
152
+ * @param ttlMs - Optional new TTL in milliseconds (uses default if not specified)
153
+ * @returns true if the key was found and refreshed, false otherwise
154
+ */
155
+ refresh(key: K, ttlMs?: number): boolean;
156
+ /**
157
+ * Returns a snapshot of cache stats.
158
+ *
159
+ * @returns Object containing cache statistics
160
+ */
161
+ stats(): {
162
+ size: number;
163
+ validEntries: number;
164
+ expiredEntries: number;
165
+ maxSize: number | undefined;
166
+ };
167
+ /**
168
+ * Stop the auto-cleanup interval if it's running.
169
+ */
170
+ destroy(): void;
171
+ /**
172
+ * Evict the least recently used entry from the cache.
173
+ * @private
174
+ */
175
+ private evictLRU;
176
+ }
177
+
178
+ export { TTLCache };
179
+ export type { TTLCacheOptions };