@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,499 +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
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
25
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
26
- return new (P || (P = Promise))(function (resolve, reject) {
27
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
28
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
29
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
30
- step((generator = generator.apply(thisArg, _arguments || [])).next());
31
- });
32
- };
33
- var __generator = (this && this.__generator) || function (thisArg, body) {
34
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
35
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
36
- function verb(n) { return function (v) { return step([n, v]); }; }
37
- function step(op) {
38
- if (f) throw new TypeError("Generator is already executing.");
39
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
40
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
41
- if (y = 0, t) op = [op[0] & 2, t.value];
42
- switch (op[0]) {
43
- case 0: case 1: t = op; break;
44
- case 4: _.label++; return { value: op[1], done: false };
45
- case 5: _.label++; y = op[1]; op = [0]; continue;
46
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
47
- default:
48
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
49
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
50
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
51
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
52
- if (t[2]) _.ops.pop();
53
- _.trys.pop(); continue;
54
- }
55
- op = body.call(thisArg, _);
56
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
57
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
58
- }
59
- };
60
- var __values = (this && this.__values) || function(o) {
61
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
62
- if (m) return m.call(o);
63
- if (o && typeof o.length === "number") return {
64
- next: function () {
65
- if (o && i >= o.length) o = void 0;
66
- return { value: o && o[i++], done: !o };
67
- }
68
- };
69
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
70
- };
71
- var __read = (this && this.__read) || function (o, n) {
72
- var m = typeof Symbol === "function" && o[Symbol.iterator];
73
- if (!m) return o;
74
- var i = m.call(o), r, ar = [], e;
75
- try {
76
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
77
- }
78
- catch (error) { e = { error: error }; }
79
- finally {
80
- try {
81
- if (r && !r.done && (m = i["return"])) m.call(i);
82
- }
83
- finally { if (e) throw e.error; }
84
- }
85
- return ar;
86
- };
87
- import { config } from '../config';
88
- /**
89
- * An in-memory cache with time-to-live (TTL) support for each entry.
90
- *
91
- * @typeParam K - Type of the key.
92
- * @typeParam V - Type of the value.
93
- *
94
- * @example
95
- * const cache = new TTLCache<string, number>({ ttlMs: 1000 });
96
- * cache.set("x", 123);
97
- * const value = cache.get("x"); // 123
98
- */
99
- var TTLCache = /** @class */ (function () {
100
- /**
101
- * @param options - Configuration options for the cache
102
- */
103
- function TTLCache(options) {
104
- if (options === void 0) { options = {}; }
105
- var _this = this;
106
- var _a;
107
- this.cache = new Map();
108
- this.ttlMs = (_a = options.ttlMs) !== null && _a !== void 0 ? _a : config.cache.defaultTtl;
109
- this.maxSize = options.maxSize;
110
- // Setup auto-cleanup if enabled
111
- var autoCleanupMs = options.autoCleanupMs;
112
- if (autoCleanupMs && autoCleanupMs > 0) {
113
- this.cleanupInterval = setInterval(function () {
114
- _this.cleanup();
115
- }, autoCleanupMs);
116
- }
117
- }
118
- /**
119
- * Sets a key-value pair in the cache with the default TTL.
120
- *
121
- * @param key - The key to set.
122
- * @param value - The value to associate with the key.
123
- */
124
- TTLCache.prototype.set = function (key, value) {
125
- this.setWithTTL(key, value, this.ttlMs);
126
- };
127
- /**
128
- * Sets a key-value pair in the cache with a custom TTL.
129
- *
130
- * @param key - The key to set.
131
- * @param value - The value to associate with the key.
132
- * @param ttlMs - Time-to-live in milliseconds.
133
- */
134
- TTLCache.prototype.setWithTTL = function (key, value, ttlMs) {
135
- var now = Date.now();
136
- var expiresAt = now + ttlMs;
137
- if (this.cache.has(key)) {
138
- this.cache.delete(key); // remove old entry to maintain LRU order
139
- }
140
- this.cache.set(key, { value: value, expiresAt: expiresAt, lastAccessed: now });
141
- if (this.maxSize && this.cache.size > this.maxSize) {
142
- this.evictLRU();
143
- }
144
- };
145
- /**
146
- * Retrieves the value for a given key if it hasn't expired.
147
- *
148
- * @param key - The key to retrieve.
149
- * @returns The cached value, or undefined if not found or expired.
150
- */
151
- TTLCache.prototype.get = function (key) {
152
- var entry = this.cache.get(key);
153
- if (!entry)
154
- return undefined;
155
- var now = Date.now();
156
- if (now > entry.expiresAt) {
157
- this.cache.delete(key);
158
- return undefined;
159
- }
160
- // Update lastAccessed and move key to the end (most recently used)
161
- entry.lastAccessed = now;
162
- // Re-insert to end of Map to maintain LRU order
163
- this.cache.delete(key);
164
- this.cache.set(key, entry);
165
- return entry.value;
166
- };
167
- /**
168
- * Retrieves or computes a value if it's not in the cache or has expired.
169
- *
170
- * @param key - The key to retrieve
171
- * @param producer - Function to generate the value if not cached
172
- * @param ttlMs - Optional custom TTL for the computed value
173
- * @returns The cached or computed value
174
- */
175
- TTLCache.prototype.getOrCompute = function (key, producer, ttlMs) {
176
- return __awaiter(this, void 0, void 0, function () {
177
- var value, newValue;
178
- return __generator(this, function (_a) {
179
- switch (_a.label) {
180
- case 0:
181
- value = this.get(key);
182
- if (value !== undefined)
183
- return [2 /*return*/, value];
184
- return [4 /*yield*/, producer()];
185
- case 1:
186
- newValue = _a.sent();
187
- this.setWithTTL(key, newValue, ttlMs !== null && ttlMs !== void 0 ? ttlMs : this.ttlMs);
188
- return [2 /*return*/, newValue];
189
- }
190
- });
191
- });
192
- };
193
- /**
194
- * Checks if the key exists and hasn't expired.
195
- *
196
- * @param key - The key to check.
197
- * @returns `true` if key exists and is valid, otherwise `false`.
198
- */
199
- TTLCache.prototype.has = function (key) {
200
- return this.get(key) !== undefined;
201
- };
202
- /**
203
- * Deletes a key from the cache.
204
- *
205
- * @param key - The key to delete.
206
- * @returns `true` if the key existed and was removed, `false` otherwise.
207
- */
208
- TTLCache.prototype.delete = function (key) {
209
- return this.cache.delete(key);
210
- };
211
- /**
212
- * Clears all entries from the cache.
213
- */
214
- TTLCache.prototype.clear = function () {
215
- this.cache.clear();
216
- };
217
- /**
218
- * Returns the number of entries currently in the cache (includes expired entries until next access/cleanup).
219
- *
220
- * @returns Number of items in the cache (may include expired keys).
221
- */
222
- TTLCache.prototype.size = function () {
223
- return this.cache.size;
224
- };
225
- /**
226
- * Set multiple key-value pairs at once with the default TTL.
227
- *
228
- * @param entries - Array of [key, value] tuples to set
229
- */
230
- TTLCache.prototype.setMany = function (entries) {
231
- var e_1, _a;
232
- try {
233
- for (var entries_1 = __values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) {
234
- var _b = __read(entries_1_1.value, 2), key = _b[0], value = _b[1];
235
- this.set(key, value);
236
- }
237
- }
238
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
239
- finally {
240
- try {
241
- if (entries_1_1 && !entries_1_1.done && (_a = entries_1.return)) _a.call(entries_1);
242
- }
243
- finally { if (e_1) throw e_1.error; }
244
- }
245
- };
246
- /**
247
- * Get multiple values at once.
248
- *
249
- * @param keys - Array of keys to retrieve
250
- * @returns Array of values (undefined for keys that don't exist or expired)
251
- */
252
- TTLCache.prototype.getMany = function (keys) {
253
- var _this = this;
254
- return keys.map(function (key) { return _this.get(key); });
255
- };
256
- /**
257
- * Removes all expired entries from the cache.
258
- *
259
- * @returns Number of entries removed.
260
- */
261
- TTLCache.prototype.cleanup = function () {
262
- var e_2, _a;
263
- var removed = 0;
264
- var now = Date.now();
265
- try {
266
- for (var _b = __values(this.cache.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
267
- var _d = __read(_c.value, 2), key = _d[0], entry = _d[1];
268
- if (now > entry.expiresAt) {
269
- this.cache.delete(key);
270
- removed++;
271
- }
272
- }
273
- }
274
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
275
- finally {
276
- try {
277
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
278
- }
279
- finally { if (e_2) throw e_2.error; }
280
- }
281
- return removed;
282
- };
283
- /**
284
- * Returns an iterator of all current valid [key, value] pairs.
285
- *
286
- * @returns IterableIterator<[K, V]>
287
- */
288
- TTLCache.prototype.entries = function () {
289
- var _a, _b, _c, key, entry, e_3_1;
290
- var e_3, _d;
291
- return __generator(this, function (_e) {
292
- switch (_e.label) {
293
- case 0:
294
- _e.trys.push([0, 5, 6, 7]);
295
- _a = __values(this.cache.entries()), _b = _a.next();
296
- _e.label = 1;
297
- case 1:
298
- if (!!_b.done) return [3 /*break*/, 4];
299
- _c = __read(_b.value, 2), key = _c[0], entry = _c[1];
300
- if (!(Date.now() <= entry.expiresAt)) return [3 /*break*/, 3];
301
- return [4 /*yield*/, [key, entry.value]];
302
- case 2:
303
- _e.sent();
304
- _e.label = 3;
305
- case 3:
306
- _b = _a.next();
307
- return [3 /*break*/, 1];
308
- case 4: return [3 /*break*/, 7];
309
- case 5:
310
- e_3_1 = _e.sent();
311
- e_3 = { error: e_3_1 };
312
- return [3 /*break*/, 7];
313
- case 6:
314
- try {
315
- if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
316
- }
317
- finally { if (e_3) throw e_3.error; }
318
- return [7 /*endfinally*/];
319
- case 7: return [2 /*return*/];
320
- }
321
- });
322
- };
323
- /**
324
- * Returns an iterator of all current valid keys.
325
- *
326
- * @returns IterableIterator<K>
327
- */
328
- TTLCache.prototype.keys = function () {
329
- var _a, _b, _c, key, entry, e_4_1;
330
- var e_4, _d;
331
- return __generator(this, function (_e) {
332
- switch (_e.label) {
333
- case 0:
334
- _e.trys.push([0, 5, 6, 7]);
335
- _a = __values(this.cache.entries()), _b = _a.next();
336
- _e.label = 1;
337
- case 1:
338
- if (!!_b.done) return [3 /*break*/, 4];
339
- _c = __read(_b.value, 2), key = _c[0], entry = _c[1];
340
- if (!(Date.now() <= entry.expiresAt)) return [3 /*break*/, 3];
341
- return [4 /*yield*/, key];
342
- case 2:
343
- _e.sent();
344
- _e.label = 3;
345
- case 3:
346
- _b = _a.next();
347
- return [3 /*break*/, 1];
348
- case 4: return [3 /*break*/, 7];
349
- case 5:
350
- e_4_1 = _e.sent();
351
- e_4 = { error: e_4_1 };
352
- return [3 /*break*/, 7];
353
- case 6:
354
- try {
355
- if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
356
- }
357
- finally { if (e_4) throw e_4.error; }
358
- return [7 /*endfinally*/];
359
- case 7: return [2 /*return*/];
360
- }
361
- });
362
- };
363
- /**
364
- * Returns an iterator of all current valid values.
365
- *
366
- * @returns IterableIterator<V>
367
- */
368
- TTLCache.prototype.values = function () {
369
- var _a, _b, entry, e_5_1;
370
- var e_5, _c;
371
- return __generator(this, function (_d) {
372
- switch (_d.label) {
373
- case 0:
374
- _d.trys.push([0, 5, 6, 7]);
375
- _a = __values(this.cache.values()), _b = _a.next();
376
- _d.label = 1;
377
- case 1:
378
- if (!!_b.done) return [3 /*break*/, 4];
379
- entry = _b.value;
380
- if (!(Date.now() <= entry.expiresAt)) return [3 /*break*/, 3];
381
- return [4 /*yield*/, entry.value];
382
- case 2:
383
- _d.sent();
384
- _d.label = 3;
385
- case 3:
386
- _b = _a.next();
387
- return [3 /*break*/, 1];
388
- case 4: return [3 /*break*/, 7];
389
- case 5:
390
- e_5_1 = _d.sent();
391
- e_5 = { error: e_5_1 };
392
- return [3 /*break*/, 7];
393
- case 6:
394
- try {
395
- if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
396
- }
397
- finally { if (e_5) throw e_5.error; }
398
- return [7 /*endfinally*/];
399
- case 7: return [2 /*return*/];
400
- }
401
- });
402
- };
403
- /**
404
- * Extends the expiration of a key by the specified time or default TTL.
405
- *
406
- * @param key - The key to refresh
407
- * @param ttlMs - Optional new TTL in milliseconds (uses default if not specified)
408
- * @returns true if the key was found and refreshed, false otherwise
409
- */
410
- TTLCache.prototype.refresh = function (key, ttlMs) {
411
- var entry = this.cache.get(key);
412
- if (!entry)
413
- return false;
414
- var now = Date.now();
415
- if (now > entry.expiresAt) {
416
- this.cache.delete(key);
417
- return false;
418
- }
419
- entry.expiresAt = now + (ttlMs !== null && ttlMs !== void 0 ? ttlMs : this.ttlMs);
420
- entry.lastAccessed = now;
421
- return true;
422
- };
423
- /**
424
- * Returns a snapshot of cache stats.
425
- *
426
- * @returns Object containing cache statistics
427
- */
428
- TTLCache.prototype.stats = function () {
429
- var e_6, _a;
430
- var now = Date.now();
431
- var expired = 0;
432
- var valid = 0;
433
- try {
434
- for (var _b = __values(this.cache.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
435
- var entry = _c.value;
436
- if (now > entry.expiresAt) {
437
- expired++;
438
- }
439
- else {
440
- valid++;
441
- }
442
- }
443
- }
444
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
445
- finally {
446
- try {
447
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
448
- }
449
- finally { if (e_6) throw e_6.error; }
450
- }
451
- return {
452
- size: this.cache.size,
453
- validEntries: valid,
454
- expiredEntries: expired,
455
- maxSize: this.maxSize
456
- };
457
- };
458
- /**
459
- * Stop the auto-cleanup interval if it's running.
460
- */
461
- TTLCache.prototype.destroy = function () {
462
- if (this.cleanupInterval) {
463
- clearInterval(this.cleanupInterval);
464
- this.cleanupInterval = undefined;
465
- }
466
- };
467
- /**
468
- * Evict the least recently used entry from the cache.
469
- * @private
470
- */
471
- TTLCache.prototype.evictLRU = function () {
472
- var e_7, _a;
473
- var now = Date.now();
474
- try {
475
- // Remove all expired items first
476
- for (var _b = __values(Array.from(this.cache.entries())), _c = _b.next(); !_c.done; _c = _b.next()) {
477
- var _d = __read(_c.value, 2), key = _d[0], entry = _d[1];
478
- if (now > entry.expiresAt) {
479
- this.cache.delete(key);
480
- }
481
- }
482
- }
483
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
484
- finally {
485
- try {
486
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
487
- }
488
- finally { if (e_7) throw e_7.error; }
489
- }
490
- // Evict oldest entries until within max size
491
- while (this.maxSize && this.cache.size > this.maxSize) {
492
- var oldestKey = this.cache.keys().next().value;
493
- this.cache.delete(oldestKey);
494
- }
495
- };
496
- return TTLCache;
497
- }());
498
- export { TTLCache };
499
- //# 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;IAME;;OAEG;IACH,kBAAY,OAA6B;QAA7B,wBAAA,EAAA,YAA6B;QAAzC,iBAWC;;QAnBO,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,OAA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,GAAG,GAAG,KAAK,CAAC,SAAS,EAAE,CAAC;oBAC1B,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;gBACzB,CAAC;YACH,CAAC;;;;;;;;;QAED,6CAA6C;QAC7C,OAAO,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC;YACtD,IAAM,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;IACH,eAAC;AAAD,CAAC,AA/RD,IA+RC","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"]}