@catbee/utils 0.0.6 → 0.0.8-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 (249) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +77 -1107
  3. package/build/index.cjs +6888 -0
  4. package/build/index.d.ts +5242 -0
  5. package/build/index.mjs +6615 -0
  6. package/package.json +26 -95
  7. package/build/esm/config.d.ts +0 -130
  8. package/build/esm/config.js +0 -145
  9. package/build/esm/config.js.map +0 -1
  10. package/build/esm/index.d.ts +0 -27
  11. package/build/esm/index.js +0 -50
  12. package/build/esm/index.js.map +0 -1
  13. package/build/esm/servers/server.builder.d.ts +0 -625
  14. package/build/esm/servers/server.builder.js +0 -722
  15. package/build/esm/servers/server.builder.js.map +0 -1
  16. package/build/esm/servers/server.d.ts +0 -256
  17. package/build/esm/servers/server.js +0 -1211
  18. package/build/esm/servers/server.js.map +0 -1
  19. package/build/esm/types/api-response.d.ts +0 -124
  20. package/build/esm/types/api-response.js +0 -25
  21. package/build/esm/types/api-response.js.map +0 -1
  22. package/build/esm/types/index.d.ts +0 -125
  23. package/build/esm/types/index.js +0 -25
  24. package/build/esm/types/index.js.map +0 -1
  25. package/build/esm/types/server.d.ts +0 -285
  26. package/build/esm/types/server.js +0 -25
  27. package/build/esm/types/server.js.map +0 -1
  28. package/build/esm/utils/array.utils.d.ts +0 -168
  29. package/build/esm/utils/array.utils.js +0 -382
  30. package/build/esm/utils/array.utils.js.map +0 -1
  31. package/build/esm/utils/async.utils.d.ts +0 -174
  32. package/build/esm/utils/async.utils.js +0 -652
  33. package/build/esm/utils/async.utils.js.map +0 -1
  34. package/build/esm/utils/cache.utils.d.ts +0 -153
  35. package/build/esm/utils/cache.utils.js +0 -499
  36. package/build/esm/utils/cache.utils.js.map +0 -1
  37. package/build/esm/utils/context-store.utils.d.ts +0 -186
  38. package/build/esm/utils/context-store.utils.js +0 -304
  39. package/build/esm/utils/context-store.utils.js.map +0 -1
  40. package/build/esm/utils/crypto.utils.d.ts +0 -160
  41. package/build/esm/utils/crypto.utils.js +0 -375
  42. package/build/esm/utils/crypto.utils.js.map +0 -1
  43. package/build/esm/utils/decorators.utils.d.ts +0 -315
  44. package/build/esm/utils/decorators.utils.js +0 -666
  45. package/build/esm/utils/decorators.utils.js.map +0 -1
  46. package/build/esm/utils/dir.utils.d.ts +0 -196
  47. package/build/esm/utils/dir.utils.js +0 -998
  48. package/build/esm/utils/dir.utils.js.map +0 -1
  49. package/build/esm/utils/env.utils.d.ts +0 -377
  50. package/build/esm/utils/env.utils.js +0 -873
  51. package/build/esm/utils/env.utils.js.map +0 -1
  52. package/build/esm/utils/exception.utils.d.ts +0 -230
  53. package/build/esm/utils/exception.utils.js +0 -559
  54. package/build/esm/utils/exception.utils.js.map +0 -1
  55. package/build/esm/utils/fs.utils.d.ts +0 -164
  56. package/build/esm/utils/fs.utils.js +0 -580
  57. package/build/esm/utils/fs.utils.js.map +0 -1
  58. package/build/esm/utils/http-status-codes.d.ts +0 -266
  59. package/build/esm/utils/http-status-codes.js +0 -295
  60. package/build/esm/utils/http-status-codes.js.map +0 -1
  61. package/build/esm/utils/id.utils.d.ts +0 -36
  62. package/build/esm/utils/id.utils.js +0 -87
  63. package/build/esm/utils/id.utils.js.map +0 -1
  64. package/build/esm/utils/logger.utils.d.ts +0 -166
  65. package/build/esm/utils/logger.utils.js +0 -347
  66. package/build/esm/utils/logger.utils.js.map +0 -1
  67. package/build/esm/utils/middleware.utils.d.ts +0 -83
  68. package/build/esm/utils/middleware.utils.js +0 -205
  69. package/build/esm/utils/middleware.utils.js.map +0 -1
  70. package/build/esm/utils/obj.utils.d.ts +0 -124
  71. package/build/esm/utils/obj.utils.js +0 -519
  72. package/build/esm/utils/obj.utils.js.map +0 -1
  73. package/build/esm/utils/request.utils.d.ts +0 -77
  74. package/build/esm/utils/request.utils.js +0 -196
  75. package/build/esm/utils/request.utils.js.map +0 -1
  76. package/build/esm/utils/response.utils.d.ts +0 -163
  77. package/build/esm/utils/response.utils.js +0 -284
  78. package/build/esm/utils/response.utils.js.map +0 -1
  79. package/build/esm/utils/string.utils.d.ts +0 -93
  80. package/build/esm/utils/string.utils.js +0 -169
  81. package/build/esm/utils/string.utils.js.map +0 -1
  82. package/build/esm/utils/url.utils.d.ts +0 -141
  83. package/build/esm/utils/url.utils.js +0 -381
  84. package/build/esm/utils/url.utils.js.map +0 -1
  85. package/build/esm/utils/validate.utils.d.ts +0 -177
  86. package/build/esm/utils/validate.utils.js +0 -336
  87. package/build/esm/utils/validate.utils.js.map +0 -1
  88. package/build/esnext/config.d.ts +0 -130
  89. package/build/esnext/config.js +0 -145
  90. package/build/esnext/config.js.map +0 -1
  91. package/build/esnext/index.d.ts +0 -27
  92. package/build/esnext/index.js +0 -50
  93. package/build/esnext/index.js.map +0 -1
  94. package/build/esnext/servers/server.builder.d.ts +0 -625
  95. package/build/esnext/servers/server.builder.js +0 -677
  96. package/build/esnext/servers/server.builder.js.map +0 -1
  97. package/build/esnext/servers/server.d.ts +0 -256
  98. package/build/esnext/servers/server.js +0 -918
  99. package/build/esnext/servers/server.js.map +0 -1
  100. package/build/esnext/types/api-response.d.ts +0 -124
  101. package/build/esnext/types/api-response.js +0 -25
  102. package/build/esnext/types/api-response.js.map +0 -1
  103. package/build/esnext/types/index.d.ts +0 -125
  104. package/build/esnext/types/index.js +0 -25
  105. package/build/esnext/types/index.js.map +0 -1
  106. package/build/esnext/types/server.d.ts +0 -285
  107. package/build/esnext/types/server.js +0 -25
  108. package/build/esnext/types/server.js.map +0 -1
  109. package/build/esnext/utils/array.utils.d.ts +0 -168
  110. package/build/esnext/utils/array.utils.js +0 -345
  111. package/build/esnext/utils/array.utils.js.map +0 -1
  112. package/build/esnext/utils/async.utils.d.ts +0 -174
  113. package/build/esnext/utils/async.utils.js +0 -430
  114. package/build/esnext/utils/async.utils.js.map +0 -1
  115. package/build/esnext/utils/cache.utils.d.ts +0 -153
  116. package/build/esnext/utils/cache.utils.js +0 -293
  117. package/build/esnext/utils/cache.utils.js.map +0 -1
  118. package/build/esnext/utils/context-store.utils.d.ts +0 -186
  119. package/build/esnext/utils/context-store.utils.js +0 -286
  120. package/build/esnext/utils/context-store.utils.js.map +0 -1
  121. package/build/esnext/utils/crypto.utils.d.ts +0 -160
  122. package/build/esnext/utils/crypto.utils.js +0 -276
  123. package/build/esnext/utils/crypto.utils.js.map +0 -1
  124. package/build/esnext/utils/decorators.utils.d.ts +0 -315
  125. package/build/esnext/utils/decorators.utils.js +0 -504
  126. package/build/esnext/utils/decorators.utils.js.map +0 -1
  127. package/build/esnext/utils/dir.utils.d.ts +0 -196
  128. package/build/esnext/utils/dir.utils.js +0 -477
  129. package/build/esnext/utils/dir.utils.js.map +0 -1
  130. package/build/esnext/utils/env.utils.d.ts +0 -377
  131. package/build/esnext/utils/env.utils.js +0 -785
  132. package/build/esnext/utils/env.utils.js.map +0 -1
  133. package/build/esnext/utils/exception.utils.d.ts +0 -230
  134. package/build/esnext/utils/exception.utils.js +0 -381
  135. package/build/esnext/utils/exception.utils.js.map +0 -1
  136. package/build/esnext/utils/fs.utils.d.ts +0 -164
  137. package/build/esnext/utils/fs.utils.js +0 -349
  138. package/build/esnext/utils/fs.utils.js.map +0 -1
  139. package/build/esnext/utils/http-status-codes.d.ts +0 -266
  140. package/build/esnext/utils/http-status-codes.js +0 -295
  141. package/build/esnext/utils/http-status-codes.js.map +0 -1
  142. package/build/esnext/utils/id.utils.d.ts +0 -36
  143. package/build/esnext/utils/id.utils.js +0 -84
  144. package/build/esnext/utils/id.utils.js.map +0 -1
  145. package/build/esnext/utils/logger.utils.d.ts +0 -166
  146. package/build/esnext/utils/logger.utils.js +0 -306
  147. package/build/esnext/utils/logger.utils.js.map +0 -1
  148. package/build/esnext/utils/middleware.utils.d.ts +0 -83
  149. package/build/esnext/utils/middleware.utils.js +0 -198
  150. package/build/esnext/utils/middleware.utils.js.map +0 -1
  151. package/build/esnext/utils/obj.utils.d.ts +0 -124
  152. package/build/esnext/utils/obj.utils.js +0 -413
  153. package/build/esnext/utils/obj.utils.js.map +0 -1
  154. package/build/esnext/utils/request.utils.d.ts +0 -77
  155. package/build/esnext/utils/request.utils.js +0 -168
  156. package/build/esnext/utils/request.utils.js.map +0 -1
  157. package/build/esnext/utils/response.utils.d.ts +0 -163
  158. package/build/esnext/utils/response.utils.js +0 -247
  159. package/build/esnext/utils/response.utils.js.map +0 -1
  160. package/build/esnext/utils/string.utils.d.ts +0 -93
  161. package/build/esnext/utils/string.utils.js +0 -165
  162. package/build/esnext/utils/string.utils.js.map +0 -1
  163. package/build/esnext/utils/url.utils.d.ts +0 -141
  164. package/build/esnext/utils/url.utils.js +0 -304
  165. package/build/esnext/utils/url.utils.js.map +0 -1
  166. package/build/esnext/utils/validate.utils.d.ts +0 -177
  167. package/build/esnext/utils/validate.utils.js +0 -320
  168. package/build/esnext/utils/validate.utils.js.map +0 -1
  169. package/build/src/config.d.ts +0 -130
  170. package/build/src/config.js +0 -150
  171. package/build/src/config.js.map +0 -1
  172. package/build/src/index.d.ts +0 -27
  173. package/build/src/index.js +0 -69
  174. package/build/src/index.js.map +0 -1
  175. package/build/src/servers/server.builder.d.ts +0 -625
  176. package/build/src/servers/server.builder.js +0 -681
  177. package/build/src/servers/server.builder.js.map +0 -1
  178. package/build/src/servers/server.d.ts +0 -256
  179. package/build/src/servers/server.js +0 -958
  180. package/build/src/servers/server.js.map +0 -1
  181. package/build/src/types/api-response.d.ts +0 -124
  182. package/build/src/types/api-response.js +0 -26
  183. package/build/src/types/api-response.js.map +0 -1
  184. package/build/src/types/index.d.ts +0 -125
  185. package/build/src/types/index.js +0 -26
  186. package/build/src/types/index.js.map +0 -1
  187. package/build/src/types/server.d.ts +0 -285
  188. package/build/src/types/server.js +0 -26
  189. package/build/src/types/server.js.map +0 -1
  190. package/build/src/utils/array.utils.d.ts +0 -168
  191. package/build/src/utils/array.utils.js +0 -364
  192. package/build/src/utils/array.utils.js.map +0 -1
  193. package/build/src/utils/async.utils.d.ts +0 -174
  194. package/build/src/utils/async.utils.js +0 -447
  195. package/build/src/utils/async.utils.js.map +0 -1
  196. package/build/src/utils/cache.utils.d.ts +0 -153
  197. package/build/src/utils/cache.utils.js +0 -297
  198. package/build/src/utils/cache.utils.js.map +0 -1
  199. package/build/src/utils/context-store.utils.d.ts +0 -186
  200. package/build/src/utils/context-store.utils.js +0 -293
  201. package/build/src/utils/context-store.utils.js.map +0 -1
  202. package/build/src/utils/crypto.utils.d.ts +0 -160
  203. package/build/src/utils/crypto.utils.js +0 -293
  204. package/build/src/utils/crypto.utils.js.map +0 -1
  205. package/build/src/utils/decorators.utils.d.ts +0 -315
  206. package/build/src/utils/decorators.utils.js +0 -516
  207. package/build/src/utils/decorators.utils.js.map +0 -1
  208. package/build/src/utils/dir.utils.d.ts +0 -196
  209. package/build/src/utils/dir.utils.js +0 -501
  210. package/build/src/utils/dir.utils.js.map +0 -1
  211. package/build/src/utils/env.utils.d.ts +0 -377
  212. package/build/src/utils/env.utils.js +0 -789
  213. package/build/src/utils/env.utils.js.map +0 -1
  214. package/build/src/utils/exception.utils.d.ts +0 -230
  215. package/build/src/utils/exception.utils.js +0 -407
  216. package/build/src/utils/exception.utils.js.map +0 -1
  217. package/build/src/utils/fs.utils.d.ts +0 -164
  218. package/build/src/utils/fs.utils.js +0 -372
  219. package/build/src/utils/fs.utils.js.map +0 -1
  220. package/build/src/utils/http-status-codes.d.ts +0 -266
  221. package/build/src/utils/http-status-codes.js +0 -298
  222. package/build/src/utils/http-status-codes.js.map +0 -1
  223. package/build/src/utils/id.utils.d.ts +0 -36
  224. package/build/src/utils/id.utils.js +0 -91
  225. package/build/src/utils/id.utils.js.map +0 -1
  226. package/build/src/utils/logger.utils.d.ts +0 -166
  227. package/build/src/utils/logger.utils.js +0 -352
  228. package/build/src/utils/logger.utils.js.map +0 -1
  229. package/build/src/utils/middleware.utils.d.ts +0 -83
  230. package/build/src/utils/middleware.utils.js +0 -205
  231. package/build/src/utils/middleware.utils.js.map +0 -1
  232. package/build/src/utils/obj.utils.d.ts +0 -124
  233. package/build/src/utils/obj.utils.js +0 -429
  234. package/build/src/utils/obj.utils.js.map +0 -1
  235. package/build/src/utils/request.utils.d.ts +0 -77
  236. package/build/src/utils/request.utils.js +0 -175
  237. package/build/src/utils/request.utils.js.map +0 -1
  238. package/build/src/utils/response.utils.d.ts +0 -163
  239. package/build/src/utils/response.utils.js +0 -260
  240. package/build/src/utils/response.utils.js.map +0 -1
  241. package/build/src/utils/string.utils.d.ts +0 -93
  242. package/build/src/utils/string.utils.js +0 -179
  243. package/build/src/utils/string.utils.js.map +0 -1
  244. package/build/src/utils/url.utils.d.ts +0 -141
  245. package/build/src/utils/url.utils.js +0 -317
  246. package/build/src/utils/url.utils.js.map +0 -1
  247. package/build/src/utils/validate.utils.d.ts +0 -177
  248. package/build/src/utils/validate.utils.js +0 -345
  249. package/build/src/utils/validate.utils.js.map +0 -1
@@ -1,293 +0,0 @@
1
- /*
2
- * The MIT License
3
- *
4
- * Copyright (c) 2025 Catbee Technologies
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
- import { config } from '../config';
25
- /**
26
- * An in-memory cache with time-to-live (TTL) support for each entry.
27
- *
28
- * @typeParam K - Type of the key.
29
- * @typeParam V - Type of the value.
30
- *
31
- * @example
32
- * const cache = new TTLCache<string, number>({ ttlMs: 1000 });
33
- * cache.set("x", 123);
34
- * const value = cache.get("x"); // 123
35
- */
36
- export class TTLCache {
37
- /**
38
- * @param options - Configuration options for the cache
39
- */
40
- constructor(options = {}) {
41
- var _a;
42
- this.cache = new Map();
43
- this.ttlMs = (_a = options.ttlMs) !== null && _a !== void 0 ? _a : config.cache.defaultTtl;
44
- this.maxSize = options.maxSize;
45
- // Setup auto-cleanup if enabled
46
- const autoCleanupMs = options.autoCleanupMs;
47
- if (autoCleanupMs && autoCleanupMs > 0) {
48
- this.cleanupInterval = setInterval(() => {
49
- this.cleanup();
50
- }, autoCleanupMs);
51
- }
52
- }
53
- /**
54
- * Sets a key-value pair in the cache with the default TTL.
55
- *
56
- * @param key - The key to set.
57
- * @param value - The value to associate with the key.
58
- */
59
- set(key, value) {
60
- this.setWithTTL(key, value, this.ttlMs);
61
- }
62
- /**
63
- * Sets a key-value pair in the cache with a custom TTL.
64
- *
65
- * @param key - The key to set.
66
- * @param value - The value to associate with the key.
67
- * @param ttlMs - Time-to-live in milliseconds.
68
- */
69
- setWithTTL(key, value, ttlMs) {
70
- const now = Date.now();
71
- const expiresAt = now + ttlMs;
72
- if (this.cache.has(key)) {
73
- this.cache.delete(key); // remove old entry to maintain LRU order
74
- }
75
- this.cache.set(key, { value, expiresAt, lastAccessed: now });
76
- if (this.maxSize && this.cache.size > this.maxSize) {
77
- this.evictLRU();
78
- }
79
- }
80
- /**
81
- * Retrieves the value for a given key if it hasn't expired.
82
- *
83
- * @param key - The key to retrieve.
84
- * @returns The cached value, or undefined if not found or expired.
85
- */
86
- get(key) {
87
- const entry = this.cache.get(key);
88
- if (!entry)
89
- return undefined;
90
- const now = Date.now();
91
- if (now > entry.expiresAt) {
92
- this.cache.delete(key);
93
- return undefined;
94
- }
95
- // Update lastAccessed and move key to the end (most recently used)
96
- entry.lastAccessed = now;
97
- // Re-insert to end of Map to maintain LRU order
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 !== undefined)
113
- return value;
114
- const newValue = await producer();
115
- this.setWithTTL(key, newValue, ttlMs !== null && ttlMs !== void 0 ? ttlMs : this.ttlMs);
116
- return newValue;
117
- }
118
- /**
119
- * Checks if the key exists and hasn't expired.
120
- *
121
- * @param key - The key to check.
122
- * @returns `true` if key exists and is valid, otherwise `false`.
123
- */
124
- has(key) {
125
- return this.get(key) !== undefined;
126
- }
127
- /**
128
- * Deletes a key from the cache.
129
- *
130
- * @param key - The key to delete.
131
- * @returns `true` if the key existed and was removed, `false` otherwise.
132
- */
133
- delete(key) {
134
- return this.cache.delete(key);
135
- }
136
- /**
137
- * Clears all entries from the cache.
138
- */
139
- clear() {
140
- this.cache.clear();
141
- }
142
- /**
143
- * Returns the number of entries currently in the cache (includes expired entries until next access/cleanup).
144
- *
145
- * @returns Number of items in the cache (may include expired keys).
146
- */
147
- size() {
148
- return this.cache.size;
149
- }
150
- /**
151
- * Set multiple key-value pairs at once with the default TTL.
152
- *
153
- * @param entries - Array of [key, value] tuples to set
154
- */
155
- setMany(entries) {
156
- for (const [key, value] of entries) {
157
- this.set(key, value);
158
- }
159
- }
160
- /**
161
- * Get multiple values at once.
162
- *
163
- * @param keys - Array of keys to retrieve
164
- * @returns Array of values (undefined for keys that don't exist or expired)
165
- */
166
- getMany(keys) {
167
- return keys.map(key => this.get(key));
168
- }
169
- /**
170
- * Removes all expired entries from the cache.
171
- *
172
- * @returns Number of entries removed.
173
- */
174
- cleanup() {
175
- let removed = 0;
176
- const now = Date.now();
177
- for (const [key, entry] of this.cache.entries()) {
178
- if (now > entry.expiresAt) {
179
- this.cache.delete(key);
180
- removed++;
181
- }
182
- }
183
- return removed;
184
- }
185
- /**
186
- * Returns an iterator of all current valid [key, value] pairs.
187
- *
188
- * @returns IterableIterator<[K, V]>
189
- */
190
- *entries() {
191
- for (const [key, entry] of this.cache.entries()) {
192
- if (Date.now() <= entry.expiresAt) {
193
- yield [key, entry.value];
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)
231
- return false;
232
- const now = Date.now();
233
- if (now > entry.expiresAt) {
234
- this.cache.delete(key);
235
- return false;
236
- }
237
- entry.expiresAt = now + (ttlMs !== null && ttlMs !== void 0 ? ttlMs : this.ttlMs);
238
- entry.lastAccessed = now;
239
- return true;
240
- }
241
- /**
242
- * Returns a snapshot of cache stats.
243
- *
244
- * @returns Object containing cache statistics
245
- */
246
- stats() {
247
- const now = Date.now();
248
- let expired = 0;
249
- let valid = 0;
250
- for (const entry of this.cache.values()) {
251
- if (now > entry.expiresAt) {
252
- expired++;
253
- }
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 = undefined;
272
- }
273
- }
274
- /**
275
- * Evict the least recently used entry from the cache.
276
- * @private
277
- */
278
- evictLRU() {
279
- const now = Date.now();
280
- // Remove all expired items first
281
- for (const [key, entry] of Array.from(this.cache.entries())) {
282
- if (now > entry.expiresAt) {
283
- this.cache.delete(key);
284
- }
285
- }
286
- // Evict oldest entries until within max size
287
- while (this.maxSize && this.cache.size > this.maxSize) {
288
- const oldestKey = this.cache.keys().next().value;
289
- this.cache.delete(oldestKey);
290
- }
291
- }
292
- }
293
- //# sourceMappingURL=cache.utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"cache.utils.js","sourceRoot":"","sources":["../../../src/utils/cache.utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAuBnC;;;;;;;;;;GAUG;AACH,MAAM,OAAO,QAAQ;IAMnB;;OAEG;IACH,YAAY,UAA2B,EAAE;;QARjC,UAAK,GAAG,IAAI,GAAG,EAAoB,CAAC;QAS1C,IAAI,CAAC,KAAK,GAAG,MAAA,OAAO,CAAC,KAAK,mCAAI,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC;QACtD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;QAE/B,gCAAgC;QAChC,MAAM,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;QAC5C,IAAI,aAAa,IAAI,aAAa,GAAG,CAAC,EAAE,CAAC;YACvC,IAAI,CAAC,eAAe,GAAG,WAAW,CAAC,GAAG,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E,OAAO;YACvB,OAAO,EAAE,IAAI,CAAC,OAAO;SACtB,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,OAAO;QACL,IAAI,IAAI,CAAC,eAAe,EAAE,CAAC;YACzB,aAAa,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;YACpC,IAAI,CAAC,eAAe,GAAG,SAAS,CAAC;QACnC,CAAC;IACH,CAAC;IAED;;;OAGG;IACK,QAAQ;QACd,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAEvB,iCAAiC;QACjC,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,EAAE,CAAC;YAC5D,IAAI,GAAG,GAAG,KAAK,CAAC,SAAS,EAAE,CAAC;gBAC1B,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YACzB,CAAC;QACH,CAAC;QAED,6CAA6C;QAC7C,OAAO,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;YACtD,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;YACjD,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,SAAc,CAAC,CAAC;QACpC,CAAC;IACH,CAAC;CACF","sourcesContent":["/*\n * The MIT License\n *\n * Copyright (c) 2025 Catbee Technologies\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 { config } from '../config';\n\n/**\n * Represents a cached entry with a value and an expiration timestamp.\n */\ntype CacheEntry<T> = {\n value: T;\n expiresAt: number;\n lastAccessed?: number; // Track last access time for LRU functionality\n};\n\n/**\n * Options for configuring a TTLCache instance.\n */\nexport interface TTLCacheOptions {\n /** Default time-to-live in milliseconds for cache entries */\n ttlMs?: number;\n /** Maximum number of entries to keep in cache (uses LRU eviction policy) */\n maxSize?: number;\n /** Auto-cleanup interval in milliseconds (disabled if 0 or negative) */\n autoCleanupMs?: number;\n}\n\n/**\n * An in-memory cache with time-to-live (TTL) support for each entry.\n *\n * @typeParam K - Type of the key.\n * @typeParam V - Type of the value.\n *\n * @example\n * const cache = new TTLCache<string, number>({ ttlMs: 1000 });\n * cache.set(\"x\", 123);\n * const value = cache.get(\"x\"); // 123\n */\nexport class TTLCache<K, V> {\n private cache = new Map<K, CacheEntry<V>>();\n private ttlMs: number;\n private maxSize?: number;\n private cleanupInterval?: NodeJS.Timeout;\n\n /**\n * @param options - Configuration options for the cache\n */\n constructor(options: TTLCacheOptions = {}) {\n this.ttlMs = options.ttlMs ?? config.cache.defaultTtl;\n this.maxSize = options.maxSize;\n\n // Setup auto-cleanup if enabled\n const autoCleanupMs = options.autoCleanupMs;\n if (autoCleanupMs && autoCleanupMs > 0) {\n this.cleanupInterval = setInterval(() => {\n this.cleanup();\n }, autoCleanupMs);\n }\n }\n\n /**\n * Sets a key-value pair in the cache with the default TTL.\n *\n * @param key - The key to set.\n * @param value - The value to associate with the key.\n */\n set(key: K, value: V): void {\n this.setWithTTL(key, value, this.ttlMs);\n }\n\n /**\n * Sets a key-value pair in the cache with a custom TTL.\n *\n * @param key - The key to set.\n * @param value - The value to associate with the key.\n * @param ttlMs - Time-to-live in milliseconds.\n */\n setWithTTL(key: K, value: V, ttlMs: number): void {\n const now = Date.now();\n const expiresAt = now + ttlMs;\n\n if (this.cache.has(key)) {\n this.cache.delete(key); // remove old entry to maintain LRU order\n }\n\n this.cache.set(key, { value, expiresAt, lastAccessed: now });\n\n if (this.maxSize && this.cache.size > this.maxSize) {\n this.evictLRU();\n }\n }\n\n /**\n * Retrieves the value for a given key if it hasn't expired.\n *\n * @param key - The key to retrieve.\n * @returns The cached value, or undefined if not found or expired.\n */\n get(key: K): V | undefined {\n const entry = this.cache.get(key);\n if (!entry) return undefined;\n\n const now = Date.now();\n if (now > entry.expiresAt) {\n this.cache.delete(key);\n return undefined;\n }\n\n // Update lastAccessed and move key to the end (most recently used)\n entry.lastAccessed = now;\n // Re-insert to end of Map to maintain LRU order\n this.cache.delete(key);\n this.cache.set(key, entry);\n\n return entry.value;\n }\n\n /**\n * Retrieves or computes a value if it's not in the cache or has expired.\n *\n * @param key - The key to retrieve\n * @param producer - Function to generate the value if not cached\n * @param ttlMs - Optional custom TTL for the computed value\n * @returns The cached or computed value\n */\n async getOrCompute(key: K, producer: () => Promise<V>, ttlMs?: number): Promise<V> {\n const value = this.get(key);\n if (value !== undefined) return value;\n\n const newValue = await producer();\n this.setWithTTL(key, newValue, ttlMs ?? this.ttlMs);\n return newValue;\n }\n\n /**\n * Checks if the key exists and hasn't expired.\n *\n * @param key - The key to check.\n * @returns `true` if key exists and is valid, otherwise `false`.\n */\n has(key: K): boolean {\n return this.get(key) !== undefined;\n }\n\n /**\n * Deletes a key from the cache.\n *\n * @param key - The key to delete.\n * @returns `true` if the key existed and was removed, `false` otherwise.\n */\n delete(key: K): boolean {\n return this.cache.delete(key);\n }\n\n /**\n * Clears all entries from the cache.\n */\n clear(): void {\n this.cache.clear();\n }\n\n /**\n * Returns the number of entries currently in the cache (includes expired entries until next access/cleanup).\n *\n * @returns Number of items in the cache (may include expired keys).\n */\n size(): number {\n return this.cache.size;\n }\n\n /**\n * Set multiple key-value pairs at once with the default TTL.\n *\n * @param entries - Array of [key, value] tuples to set\n */\n setMany(entries: [K, V][]): void {\n for (const [key, value] of entries) {\n this.set(key, value);\n }\n }\n\n /**\n * Get multiple values at once.\n *\n * @param keys - Array of keys to retrieve\n * @returns Array of values (undefined for keys that don't exist or expired)\n */\n getMany(keys: K[]): (V | undefined)[] {\n return keys.map(key => this.get(key));\n }\n\n /**\n * Removes all expired entries from the cache.\n *\n * @returns Number of entries removed.\n */\n cleanup(): number {\n let removed = 0;\n const now = Date.now();\n for (const [key, entry] of this.cache.entries()) {\n if (now > entry.expiresAt) {\n this.cache.delete(key);\n removed++;\n }\n }\n return removed;\n }\n\n /**\n * Returns an iterator of all current valid [key, value] pairs.\n *\n * @returns IterableIterator<[K, V]>\n */\n *entries(): IterableIterator<[K, V]> {\n for (const [key, entry] of this.cache.entries()) {\n if (Date.now() <= entry.expiresAt) {\n yield [key, entry.value];\n }\n }\n }\n\n /**\n * Returns an iterator of all current valid keys.\n *\n * @returns IterableIterator<K>\n */\n *keys(): IterableIterator<K> {\n for (const [key, entry] of this.cache.entries()) {\n if (Date.now() <= entry.expiresAt) {\n yield key;\n }\n }\n }\n\n /**\n * Returns an iterator of all current valid values.\n *\n * @returns IterableIterator<V>\n */\n *values(): IterableIterator<V> {\n for (const entry of this.cache.values()) {\n if (Date.now() <= entry.expiresAt) {\n yield entry.value;\n }\n }\n }\n\n /**\n * Extends the expiration of a key by the specified time or default TTL.\n *\n * @param key - The key to refresh\n * @param ttlMs - Optional new TTL in milliseconds (uses default if not specified)\n * @returns true if the key was found and refreshed, false otherwise\n */\n refresh(key: K, ttlMs?: number): boolean {\n const entry = this.cache.get(key);\n if (!entry) return false;\n\n const now = Date.now();\n if (now > entry.expiresAt) {\n this.cache.delete(key);\n return false;\n }\n\n entry.expiresAt = now + (ttlMs ?? this.ttlMs);\n entry.lastAccessed = now;\n return true;\n }\n\n /**\n * Returns a snapshot of cache stats.\n *\n * @returns Object containing cache statistics\n */\n stats() {\n const now = Date.now();\n let expired = 0;\n let valid = 0;\n\n for (const entry of this.cache.values()) {\n if (now > entry.expiresAt) {\n expired++;\n } else {\n valid++;\n }\n }\n\n return {\n size: this.cache.size,\n validEntries: valid,\n expiredEntries: expired,\n maxSize: this.maxSize\n };\n }\n\n /**\n * Stop the auto-cleanup interval if it's running.\n */\n destroy() {\n if (this.cleanupInterval) {\n clearInterval(this.cleanupInterval);\n this.cleanupInterval = undefined;\n }\n }\n\n /**\n * Evict the least recently used entry from the cache.\n * @private\n */\n private evictLRU(): void {\n const now = Date.now();\n\n // Remove all expired items first\n for (const [key, entry] of Array.from(this.cache.entries())) {\n if (now > entry.expiresAt) {\n this.cache.delete(key);\n }\n }\n\n // Evict oldest entries until within max size\n while (this.maxSize && this.cache.size > this.maxSize) {\n const oldestKey = this.cache.keys().next().value;\n this.cache.delete(oldestKey as K);\n }\n }\n}\n"]}
@@ -1,186 +0,0 @@
1
- import { AsyncLocalStorage } from 'async_hooks';
2
- /**
3
- * Type representing the store object used in AsyncLocalStorage.
4
- * Keys must be unique symbols to ensure isolation and prevent collisions.
5
- */
6
- export interface Store {
7
- [key: symbol]: unknown;
8
- }
9
- /**
10
- * Predefined symbols used as keys in AsyncLocalStorage.
11
- * Add new symbols here to avoid duplication.
12
- */
13
- export declare const StoreKeys: {
14
- LOGGER: symbol;
15
- REQUEST_ID: symbol;
16
- USER_ID: symbol;
17
- TENANT_ID: symbol;
18
- TRACE_ID: symbol;
19
- CORRELATION_ID: symbol;
20
- };
21
- /**
22
- * Retrieves the current request ID from the async context, if available.
23
- *
24
- * @returns {string | undefined} The request ID string or undefined if not present in the current context.
25
- */
26
- export declare function getRequestId(): string | undefined;
27
- /**
28
- * Type-safe getter for common context values.
29
- *
30
- * @param key The store key symbol
31
- * @returns The typed value from the store
32
- */
33
- export declare function getFromContext<T>(key: symbol): T | undefined;
34
- /**
35
- * ContextStore manages per-request scoped context using AsyncLocalStorage.
36
- * It allows storing and retrieving data across async calls (e.g., request ID, logger).
37
- *
38
- * Example (Express middleware):
39
- * ```
40
- * // Middleware to initialize context and logger
41
- * app.use((req, res, next) => {
42
- * const requestId = req.headers["x-request-id"]?.toString() || crypto.randomUUID();
43
- * ContextStore.run({ [StoreKeys.REQUEST_ID]: requestId }, () => {
44
- * ContextStore.set(StoreKeys.LOGGER, getLogger().child({ requestId }));
45
- * next();
46
- * });
47
- * });
48
- * ```
49
- */
50
- export declare class ContextStore {
51
- /**
52
- * The underlying AsyncLocalStorage instance for context.
53
- * @private
54
- */
55
- private static readonly storage;
56
- /**
57
- * Returns the raw AsyncLocalStorage instance for advanced access.
58
- *
59
- * @returns {AsyncLocalStorage<Store>} The AsyncLocalStorage instance.
60
- */
61
- static getInstance(): AsyncLocalStorage<Store>;
62
- /**
63
- * Retrieves a value from the async context store by symbol key.
64
- *
65
- * @typeParam T - The expected return type of the value.
66
- * @param {symbol} key - Unique symbol used as the store key.
67
- * @returns {T | undefined} The value found (typed) or undefined if not present.
68
- */
69
- static get<T>(key: symbol): T | undefined;
70
- /**
71
- * Sets a value in the current context store by symbol key.
72
- *
73
- * @typeParam T - The value type to set.
74
- * @param {symbol} key - Unique symbol key.
75
- * @param {T} value - Value to set in context.
76
- * @throws {Error} If called outside an active context (not within a .run call or in the wrong async boundaries).
77
- */
78
- static set<T>(key: symbol, value: T): void;
79
- /**
80
- * Retrieves the entire context store object for the current async context.
81
- *
82
- * @returns {Store | undefined} The current store object or undefined if called outside a context.
83
- */
84
- static getAll(): Store | undefined;
85
- /**
86
- * Initializes a new async context and executes a callback within it.
87
- * This must be called at the beginning of a request or logical async flow.
88
- *
89
- * @typeParam T - The callback's return type.
90
- * @param {Store} store - The initial key-value store object.
91
- * @param {() => T} callback - The function to run within the new context.
92
- * @returns {T} The result of the callback function.
93
- */
94
- static run<T>(store: Store, callback: () => T): T;
95
- /**
96
- * Checks if a key exists in the current context store.
97
- *
98
- * @param {symbol} key - The symbol key to check.
99
- * @returns {boolean} True if the key exists, false otherwise.
100
- */
101
- static has(key: symbol): boolean;
102
- /**
103
- * Removes a value from the current context store by symbol key.
104
- *
105
- * @param {symbol} key - The symbol key to delete.
106
- * @returns {boolean} True if the key was deleted, false if the key wasn't found or no active context.
107
- * @throws {Error} If called outside an active context.
108
- */
109
- static delete(key: symbol): boolean;
110
- /**
111
- * Updates multiple values in the current context store at once.
112
- *
113
- * @param {Partial<Record<symbol, unknown>>} values - Object containing symbol keys and values to update.
114
- * @throws {Error} If called outside an active context.
115
- */
116
- static patch(values: Partial<Record<symbol, unknown>>): void;
117
- /**
118
- * Executes a callback with a temporary store value that only exists during execution.
119
- * Original store is restored after callback completes.
120
- *
121
- * @typeParam T - The callback's return type.
122
- * @param {symbol} key - The symbol key to temporarily set.
123
- * @param {unknown} value - The temporary value.
124
- * @param {() => T} callback - The function to execute with the temporary value.
125
- * @returns {T} The result of the callback function.
126
- * @throws {Error} If called outside an active context.
127
- */
128
- static withValue<T>(key: symbol, value: unknown, callback: () => T): T;
129
- /**
130
- * Creates a new context that inherits values from the current context.
131
- *
132
- * @typeParam T - The callback's return type.
133
- * @param {Partial<Record<symbol, unknown>>} newValues - New values to add to or override in the context.
134
- * @param {() => T} callback - The function to execute in the new context.
135
- * @returns {T} The result of the callback function.
136
- */
137
- static extend<T>(newValues: Partial<Record<symbol, unknown>>, callback: () => T): T;
138
- /**
139
- * Creates Express middleware that initializes a context for each request.
140
- *
141
- * @param {(req: any) => Partial<Record<symbol, unknown>>} initialValuesFactory - Function that returns initial context values.
142
- * @returns Express middleware function.
143
- */
144
- static createExpressMiddleware(initialValuesFactory?: (req: any) => Partial<Record<symbol, unknown>>): (req: any, res: any, next: any) => void;
145
- }
146
- /**
147
- * Type-safe wrapper for accessing and modifying context values with specific types.
148
- *
149
- * @typeParam T - The value type.
150
- */
151
- export declare class TypedContextKey<T> {
152
- private symbol;
153
- private defaultValue?;
154
- /**
155
- * Creates a new typed context key.
156
- *
157
- * @param symbol - The unique symbol for this key
158
- * @param defaultValue - Optional default value if key is not found
159
- */
160
- constructor(symbol: symbol, defaultValue?: T | undefined);
161
- /**
162
- * Gets the current value for this key.
163
- *
164
- * @returns The value or defaultValue if not found
165
- */
166
- get(): T | undefined;
167
- /**
168
- * Sets the value for this key.
169
- *
170
- * @param value The value to set
171
- */
172
- set(value: T): void;
173
- /**
174
- * Checks if this key exists in the context.
175
- *
176
- * @returns True if the key exists
177
- */
178
- exists(): boolean;
179
- /**
180
- * Deletes this key from the context.
181
- *
182
- * @returns True if the key was deleted
183
- */
184
- delete(): boolean;
185
- }
186
- //# sourceMappingURL=context-store.utils.d.ts.map