@catbee/utils 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +303 -107
- package/build/esm/config.d.ts +13 -1
- package/build/esm/config.js +16 -4
- package/build/esm/config.js.map +1 -1
- package/build/esm/index.d.ts +20 -18
- package/build/esm/index.js +20 -18
- package/build/esm/index.js.map +1 -1
- package/build/esm/types/api-response.d.ts +83 -1
- package/build/esm/types/api-response.js.map +1 -1
- package/build/esm/utils/array.utils.d.ts +65 -2
- package/build/esm/utils/array.utils.js +147 -20
- package/build/esm/utils/array.utils.js.map +1 -1
- package/build/esm/utils/async.utils.d.ts +56 -1
- package/build/esm/utils/async.utils.js +166 -11
- package/build/esm/utils/async.utils.js.map +1 -1
- package/build/esm/utils/cache.utils.d.ts +67 -4
- package/build/esm/utils/cache.utils.js +210 -28
- package/build/esm/utils/cache.utils.js.map +1 -1
- package/build/esm/utils/context-store.utils.d.ts +102 -1
- package/build/esm/utils/context-store.utils.js +185 -3
- package/build/esm/utils/context-store.utils.js.map +1 -1
- package/build/esm/utils/crypto.utils.d.ts +110 -8
- package/build/esm/utils/crypto.utils.js +298 -23
- package/build/esm/utils/crypto.utils.js.map +1 -1
- package/build/esm/utils/decorators.utils.d.ts +40 -0
- package/build/esm/utils/decorators.utils.js +312 -0
- package/build/esm/utils/decorators.utils.js.map +1 -0
- package/build/esm/utils/dir.utils.d.ts +122 -1
- package/build/esm/utils/dir.utils.js +578 -3
- package/build/esm/utils/dir.utils.js.map +1 -1
- package/build/esm/utils/env.utils.d.ts +83 -0
- package/build/esm/utils/env.utils.js +235 -6
- package/build/esm/utils/env.utils.js.map +1 -1
- package/build/esm/utils/exception.utils.d.ts +117 -1
- package/build/esm/utils/exception.utils.js +331 -12
- package/build/esm/utils/exception.utils.js.map +1 -1
- package/build/esm/utils/fs.utils.d.ts +132 -0
- package/build/esm/utils/fs.utils.js +421 -4
- package/build/esm/utils/fs.utils.js.map +1 -1
- package/build/esm/utils/http-status-codes.d.ts +87 -178
- package/build/esm/utils/http-status-codes.js +92 -178
- package/build/esm/utils/http-status-codes.js.map +1 -1
- package/build/esm/utils/id.utils.d.ts +11 -10
- package/build/esm/utils/id.utils.js +30 -21
- package/build/esm/utils/id.utils.js.map +1 -1
- package/build/esm/utils/logger.utils.d.ts +35 -2
- package/build/esm/utils/logger.utils.js +68 -14
- package/build/esm/utils/logger.utils.js.map +1 -1
- package/build/esm/utils/middleware.utils.d.ts +73 -0
- package/build/esm/utils/middleware.utils.js +158 -0
- package/build/esm/utils/middleware.utils.js.map +1 -0
- package/build/esm/utils/obj.utils.d.ts +65 -3
- package/build/esm/utils/obj.utils.js +206 -18
- package/build/esm/utils/obj.utils.js.map +1 -1
- package/build/esm/utils/response.utils.d.ts +103 -2
- package/build/esm/utils/response.utils.js +156 -3
- package/build/esm/utils/response.utils.js.map +1 -1
- package/build/esm/utils/string.utils.d.ts +61 -5
- package/build/esm/utils/string.utils.js +109 -19
- package/build/esm/utils/string.utils.js.map +1 -1
- package/build/esm/utils/url.utils.d.ts +117 -0
- package/build/esm/utils/url.utils.js +287 -2
- package/build/esm/utils/url.utils.js.map +1 -1
- package/build/esm/utils/validate.utils.d.ts +110 -0
- package/build/esm/utils/validate.utils.js +190 -8
- package/build/esm/utils/validate.utils.js.map +1 -1
- package/build/esnext/config.d.ts +13 -1
- package/build/esnext/config.js +16 -4
- package/build/esnext/config.js.map +1 -1
- package/build/esnext/index.d.ts +20 -18
- package/build/esnext/index.js +20 -18
- package/build/esnext/index.js.map +1 -1
- package/build/esnext/types/api-response.d.ts +83 -1
- package/build/esnext/types/api-response.js.map +1 -1
- package/build/esnext/utils/array.utils.d.ts +65 -2
- package/build/esnext/utils/array.utils.js +140 -21
- package/build/esnext/utils/array.utils.js.map +1 -1
- package/build/esnext/utils/async.utils.d.ts +56 -1
- package/build/esnext/utils/async.utils.js +130 -14
- package/build/esnext/utils/async.utils.js.map +1 -1
- package/build/esnext/utils/cache.utils.d.ts +67 -4
- package/build/esnext/utils/cache.utils.js +137 -9
- package/build/esnext/utils/cache.utils.js.map +1 -1
- package/build/esnext/utils/context-store.utils.d.ts +102 -1
- package/build/esnext/utils/context-store.utils.js +171 -3
- package/build/esnext/utils/context-store.utils.js.map +1 -1
- package/build/esnext/utils/crypto.utils.d.ts +110 -8
- package/build/esnext/utils/crypto.utils.js +199 -19
- package/build/esnext/utils/crypto.utils.js.map +1 -1
- package/build/esnext/utils/decorators.utils.d.ts +40 -0
- package/build/esnext/utils/decorators.utils.js +155 -0
- package/build/esnext/utils/decorators.utils.js.map +1 -0
- package/build/esnext/utils/dir.utils.d.ts +122 -1
- package/build/esnext/utils/dir.utils.js +307 -3
- package/build/esnext/utils/dir.utils.js.map +1 -1
- package/build/esnext/utils/env.utils.d.ts +83 -0
- package/build/esnext/utils/env.utils.js +191 -8
- package/build/esnext/utils/env.utils.js.map +1 -1
- package/build/esnext/utils/exception.utils.d.ts +117 -1
- package/build/esnext/utils/exception.utils.js +222 -12
- package/build/esnext/utils/exception.utils.js.map +1 -1
- package/build/esnext/utils/fs.utils.d.ts +132 -0
- package/build/esnext/utils/fs.utils.js +268 -4
- package/build/esnext/utils/fs.utils.js.map +1 -1
- package/build/esnext/utils/http-status-codes.d.ts +87 -178
- package/build/esnext/utils/http-status-codes.js +92 -178
- package/build/esnext/utils/http-status-codes.js.map +1 -1
- package/build/esnext/utils/id.utils.d.ts +11 -10
- package/build/esnext/utils/id.utils.js +28 -20
- package/build/esnext/utils/id.utils.js.map +1 -1
- package/build/esnext/utils/logger.utils.d.ts +35 -2
- package/build/esnext/utils/logger.utils.js +56 -14
- package/build/esnext/utils/logger.utils.js.map +1 -1
- package/build/esnext/utils/middleware.utils.d.ts +73 -0
- package/build/esnext/utils/middleware.utils.js +157 -0
- package/build/esnext/utils/middleware.utils.js.map +1 -0
- package/build/esnext/utils/obj.utils.d.ts +65 -3
- package/build/esnext/utils/obj.utils.js +169 -14
- package/build/esnext/utils/obj.utils.js.map +1 -1
- package/build/esnext/utils/response.utils.d.ts +103 -2
- package/build/esnext/utils/response.utils.js +141 -3
- package/build/esnext/utils/response.utils.js.map +1 -1
- package/build/esnext/utils/string.utils.d.ts +61 -5
- package/build/esnext/utils/string.utils.js +110 -14
- package/build/esnext/utils/string.utils.js.map +1 -1
- package/build/esnext/utils/url.utils.d.ts +117 -0
- package/build/esnext/utils/url.utils.js +248 -2
- package/build/esnext/utils/url.utils.js.map +1 -1
- package/build/esnext/utils/validate.utils.d.ts +110 -0
- package/build/esnext/utils/validate.utils.js +190 -8
- package/build/esnext/utils/validate.utils.js.map +1 -1
- package/build/src/config.d.ts +13 -1
- package/build/src/config.js +15 -3
- package/build/src/config.js.map +1 -1
- package/build/src/index.d.ts +20 -18
- package/build/src/index.js +2 -0
- package/build/src/index.js.map +1 -1
- package/build/src/types/api-response.d.ts +83 -1
- package/build/src/types/api-response.js.map +1 -1
- package/build/src/utils/array.utils.d.ts +65 -2
- package/build/src/utils/array.utils.js +147 -22
- package/build/src/utils/array.utils.js.map +1 -1
- package/build/src/utils/async.utils.d.ts +56 -1
- package/build/src/utils/async.utils.js +137 -17
- package/build/src/utils/async.utils.js.map +1 -1
- package/build/src/utils/cache.utils.d.ts +67 -4
- package/build/src/utils/cache.utils.js +137 -9
- package/build/src/utils/cache.utils.js.map +1 -1
- package/build/src/utils/context-store.utils.d.ts +102 -1
- package/build/src/utils/context-store.utils.js +173 -3
- package/build/src/utils/context-store.utils.js.map +1 -1
- package/build/src/utils/crypto.utils.d.ts +110 -8
- package/build/src/utils/crypto.utils.js +214 -27
- package/build/src/utils/crypto.utils.js.map +1 -1
- package/build/src/utils/decorators.utils.d.ts +40 -0
- package/build/src/utils/decorators.utils.js +165 -0
- package/build/src/utils/decorators.utils.js.map +1 -0
- package/build/src/utils/dir.utils.d.ts +122 -1
- package/build/src/utils/dir.utils.js +314 -0
- package/build/src/utils/dir.utils.js.map +1 -1
- package/build/src/utils/env.utils.d.ts +83 -0
- package/build/src/utils/env.utils.js +192 -9
- package/build/src/utils/env.utils.js.map +1 -1
- package/build/src/utils/exception.utils.d.ts +117 -1
- package/build/src/utils/exception.utils.js +233 -11
- package/build/src/utils/exception.utils.js.map +1 -1
- package/build/src/utils/fs.utils.d.ts +132 -0
- package/build/src/utils/fs.utils.js +281 -3
- package/build/src/utils/fs.utils.js.map +1 -1
- package/build/src/utils/http-status-codes.d.ts +87 -178
- package/build/src/utils/http-status-codes.js +92 -178
- package/build/src/utils/http-status-codes.js.map +1 -1
- package/build/src/utils/id.utils.d.ts +11 -10
- package/build/src/utils/id.utils.js +28 -20
- package/build/src/utils/id.utils.js.map +1 -1
- package/build/src/utils/logger.utils.d.ts +35 -2
- package/build/src/utils/logger.utils.js +56 -11
- package/build/src/utils/logger.utils.js.map +1 -1
- package/build/src/utils/middleware.utils.d.ts +73 -0
- package/build/src/utils/middleware.utils.js +163 -0
- package/build/src/utils/middleware.utils.js.map +1 -0
- package/build/src/utils/obj.utils.d.ts +65 -3
- package/build/src/utils/obj.utils.js +179 -18
- package/build/src/utils/obj.utils.js.map +1 -1
- package/build/src/utils/response.utils.d.ts +103 -2
- package/build/src/utils/response.utils.js +147 -2
- package/build/src/utils/response.utils.js.map +1 -1
- package/build/src/utils/string.utils.d.ts +61 -5
- package/build/src/utils/string.utils.js +122 -20
- package/build/src/utils/string.utils.js.map +1 -1
- package/build/src/utils/url.utils.d.ts +117 -0
- package/build/src/utils/url.utils.js +256 -1
- package/build/src/utils/url.utils.js.map +1 -1
- package/build/src/utils/validate.utils.d.ts +110 -0
- package/build/src/utils/validate.utils.js +204 -8
- package/build/src/utils/validate.utils.js.map +1 -1
- package/package.json +20 -12
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
1
10
|
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
2
11
|
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);
|
|
3
12
|
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
@@ -52,6 +61,7 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
52
61
|
}
|
|
53
62
|
return ar;
|
|
54
63
|
};
|
|
64
|
+
import { Config } from '../config';
|
|
55
65
|
/**
|
|
56
66
|
* An in-memory cache with time-to-live (TTL) support for each entry.
|
|
57
67
|
*
|
|
@@ -59,18 +69,28 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
59
69
|
* @typeParam V - Type of the value.
|
|
60
70
|
*
|
|
61
71
|
* @example
|
|
62
|
-
* const cache = new TTLCache<string, number>(1000);
|
|
72
|
+
* const cache = new TTLCache<string, number>({ ttlMs: 1000 });
|
|
63
73
|
* cache.set("x", 123);
|
|
64
74
|
* const value = cache.get("x"); // 123
|
|
65
75
|
*/
|
|
66
76
|
var TTLCache = /** @class */ (function () {
|
|
67
77
|
/**
|
|
68
|
-
* @param
|
|
78
|
+
* @param options - Configuration options for the cache
|
|
69
79
|
*/
|
|
70
|
-
function TTLCache(
|
|
71
|
-
if (
|
|
72
|
-
|
|
80
|
+
function TTLCache(options) {
|
|
81
|
+
if (options === void 0) { options = {}; }
|
|
82
|
+
var _this = this;
|
|
83
|
+
var _a;
|
|
73
84
|
this.cache = new Map();
|
|
85
|
+
this.ttlMs = (_a = options.ttlMs) !== null && _a !== void 0 ? _a : Config.Cache.defaultTtl;
|
|
86
|
+
this.maxSize = options.maxSize;
|
|
87
|
+
// Setup auto-cleanup if enabled
|
|
88
|
+
var autoCleanupMs = options.autoCleanupMs;
|
|
89
|
+
if (autoCleanupMs && autoCleanupMs > 0) {
|
|
90
|
+
this.cleanupInterval = setInterval(function () {
|
|
91
|
+
_this.cleanup();
|
|
92
|
+
}, autoCleanupMs);
|
|
93
|
+
}
|
|
74
94
|
}
|
|
75
95
|
/**
|
|
76
96
|
* Sets a key-value pair in the cache with the default TTL.
|
|
@@ -79,8 +99,7 @@ var TTLCache = /** @class */ (function () {
|
|
|
79
99
|
* @param value - The value to associate with the key.
|
|
80
100
|
*/
|
|
81
101
|
TTLCache.prototype.set = function (key, value) {
|
|
82
|
-
|
|
83
|
-
this.cache.set(key, { value: value, expiresAt: expiresAt });
|
|
102
|
+
this.setWithTTL(key, value, this.ttlMs);
|
|
84
103
|
};
|
|
85
104
|
/**
|
|
86
105
|
* Sets a key-value pair in the cache with a custom TTL.
|
|
@@ -90,8 +109,13 @@ var TTLCache = /** @class */ (function () {
|
|
|
90
109
|
* @param ttlMs - Time-to-live in milliseconds.
|
|
91
110
|
*/
|
|
92
111
|
TTLCache.prototype.setWithTTL = function (key, value, ttlMs) {
|
|
93
|
-
var
|
|
94
|
-
|
|
112
|
+
var now = Date.now();
|
|
113
|
+
var expiresAt = now + ttlMs;
|
|
114
|
+
this.cache.set(key, { value: value, expiresAt: expiresAt, lastAccessed: now });
|
|
115
|
+
// Enforce max size limit if configured
|
|
116
|
+
if (this.maxSize && this.cache.size > this.maxSize) {
|
|
117
|
+
this.evictLRU();
|
|
118
|
+
}
|
|
95
119
|
};
|
|
96
120
|
/**
|
|
97
121
|
* Retrieves the value for a given key if it hasn't expired.
|
|
@@ -103,12 +127,41 @@ var TTLCache = /** @class */ (function () {
|
|
|
103
127
|
var entry = this.cache.get(key);
|
|
104
128
|
if (!entry)
|
|
105
129
|
return undefined;
|
|
106
|
-
|
|
130
|
+
var now = Date.now();
|
|
131
|
+
if (now > entry.expiresAt) {
|
|
107
132
|
this.cache.delete(key);
|
|
108
133
|
return undefined;
|
|
109
134
|
}
|
|
135
|
+
// Update last access time for LRU tracking
|
|
136
|
+
entry.lastAccessed = now;
|
|
110
137
|
return entry.value;
|
|
111
138
|
};
|
|
139
|
+
/**
|
|
140
|
+
* Retrieves or computes a value if it's not in the cache or has expired.
|
|
141
|
+
*
|
|
142
|
+
* @param key - The key to retrieve
|
|
143
|
+
* @param producer - Function to generate the value if not cached
|
|
144
|
+
* @param ttlMs - Optional custom TTL for the computed value
|
|
145
|
+
* @returns The cached or computed value
|
|
146
|
+
*/
|
|
147
|
+
TTLCache.prototype.getOrCompute = function (key, producer, ttlMs) {
|
|
148
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
149
|
+
var value, newValue;
|
|
150
|
+
return __generator(this, function (_a) {
|
|
151
|
+
switch (_a.label) {
|
|
152
|
+
case 0:
|
|
153
|
+
value = this.get(key);
|
|
154
|
+
if (value !== undefined)
|
|
155
|
+
return [2 /*return*/, value];
|
|
156
|
+
return [4 /*yield*/, producer()];
|
|
157
|
+
case 1:
|
|
158
|
+
newValue = _a.sent();
|
|
159
|
+
this.setWithTTL(key, newValue, ttlMs !== null && ttlMs !== void 0 ? ttlMs : this.ttlMs);
|
|
160
|
+
return [2 /*return*/, newValue];
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
};
|
|
112
165
|
/**
|
|
113
166
|
* Checks if the key exists and hasn't expired.
|
|
114
167
|
*
|
|
@@ -141,13 +194,44 @@ var TTLCache = /** @class */ (function () {
|
|
|
141
194
|
TTLCache.prototype.size = function () {
|
|
142
195
|
return this.cache.size;
|
|
143
196
|
};
|
|
197
|
+
/**
|
|
198
|
+
* Set multiple key-value pairs at once with the default TTL.
|
|
199
|
+
*
|
|
200
|
+
* @param entries - Array of [key, value] tuples to set
|
|
201
|
+
*/
|
|
202
|
+
TTLCache.prototype.setMany = function (entries) {
|
|
203
|
+
var e_1, _a;
|
|
204
|
+
try {
|
|
205
|
+
for (var entries_1 = __values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) {
|
|
206
|
+
var _b = __read(entries_1_1.value, 2), key = _b[0], value = _b[1];
|
|
207
|
+
this.set(key, value);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
211
|
+
finally {
|
|
212
|
+
try {
|
|
213
|
+
if (entries_1_1 && !entries_1_1.done && (_a = entries_1.return)) _a.call(entries_1);
|
|
214
|
+
}
|
|
215
|
+
finally { if (e_1) throw e_1.error; }
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
/**
|
|
219
|
+
* Get multiple values at once.
|
|
220
|
+
*
|
|
221
|
+
* @param keys - Array of keys to retrieve
|
|
222
|
+
* @returns Array of values (undefined for keys that don't exist or expired)
|
|
223
|
+
*/
|
|
224
|
+
TTLCache.prototype.getMany = function (keys) {
|
|
225
|
+
var _this = this;
|
|
226
|
+
return keys.map(function (key) { return _this.get(key); });
|
|
227
|
+
};
|
|
144
228
|
/**
|
|
145
229
|
* Removes all expired entries from the cache.
|
|
146
230
|
*
|
|
147
231
|
* @returns Number of entries removed.
|
|
148
232
|
*/
|
|
149
233
|
TTLCache.prototype.cleanup = function () {
|
|
150
|
-
var
|
|
234
|
+
var e_2, _a;
|
|
151
235
|
var removed = 0;
|
|
152
236
|
var now = Date.now();
|
|
153
237
|
try {
|
|
@@ -159,12 +243,12 @@ var TTLCache = /** @class */ (function () {
|
|
|
159
243
|
}
|
|
160
244
|
}
|
|
161
245
|
}
|
|
162
|
-
catch (
|
|
246
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
163
247
|
finally {
|
|
164
248
|
try {
|
|
165
249
|
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
166
250
|
}
|
|
167
|
-
finally { if (
|
|
251
|
+
finally { if (e_2) throw e_2.error; }
|
|
168
252
|
}
|
|
169
253
|
return removed;
|
|
170
254
|
};
|
|
@@ -174,8 +258,8 @@ var TTLCache = /** @class */ (function () {
|
|
|
174
258
|
* @returns IterableIterator<[K, V]>
|
|
175
259
|
*/
|
|
176
260
|
TTLCache.prototype.entries = function () {
|
|
177
|
-
var _a, _b, _c, key, entry,
|
|
178
|
-
var
|
|
261
|
+
var _a, _b, _c, key, entry, e_3_1;
|
|
262
|
+
var e_3, _d;
|
|
179
263
|
return __generator(this, function (_e) {
|
|
180
264
|
switch (_e.label) {
|
|
181
265
|
case 0:
|
|
@@ -195,14 +279,14 @@ var TTLCache = /** @class */ (function () {
|
|
|
195
279
|
return [3 /*break*/, 1];
|
|
196
280
|
case 4: return [3 /*break*/, 7];
|
|
197
281
|
case 5:
|
|
198
|
-
|
|
199
|
-
|
|
282
|
+
e_3_1 = _e.sent();
|
|
283
|
+
e_3 = { error: e_3_1 };
|
|
200
284
|
return [3 /*break*/, 7];
|
|
201
285
|
case 6:
|
|
202
286
|
try {
|
|
203
287
|
if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
|
|
204
288
|
}
|
|
205
|
-
finally { if (
|
|
289
|
+
finally { if (e_3) throw e_3.error; }
|
|
206
290
|
return [7 /*endfinally*/];
|
|
207
291
|
case 7: return [2 /*return*/];
|
|
208
292
|
}
|
|
@@ -214,8 +298,8 @@ var TTLCache = /** @class */ (function () {
|
|
|
214
298
|
* @returns IterableIterator<K>
|
|
215
299
|
*/
|
|
216
300
|
TTLCache.prototype.keys = function () {
|
|
217
|
-
var _a, _b, _c, key, entry,
|
|
218
|
-
var
|
|
301
|
+
var _a, _b, _c, key, entry, e_4_1;
|
|
302
|
+
var e_4, _d;
|
|
219
303
|
return __generator(this, function (_e) {
|
|
220
304
|
switch (_e.label) {
|
|
221
305
|
case 0:
|
|
@@ -235,14 +319,14 @@ var TTLCache = /** @class */ (function () {
|
|
|
235
319
|
return [3 /*break*/, 1];
|
|
236
320
|
case 4: return [3 /*break*/, 7];
|
|
237
321
|
case 5:
|
|
238
|
-
|
|
239
|
-
|
|
322
|
+
e_4_1 = _e.sent();
|
|
323
|
+
e_4 = { error: e_4_1 };
|
|
240
324
|
return [3 /*break*/, 7];
|
|
241
325
|
case 6:
|
|
242
326
|
try {
|
|
243
327
|
if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
|
|
244
328
|
}
|
|
245
|
-
finally { if (
|
|
329
|
+
finally { if (e_4) throw e_4.error; }
|
|
246
330
|
return [7 /*endfinally*/];
|
|
247
331
|
case 7: return [2 /*return*/];
|
|
248
332
|
}
|
|
@@ -254,8 +338,8 @@ var TTLCache = /** @class */ (function () {
|
|
|
254
338
|
* @returns IterableIterator<V>
|
|
255
339
|
*/
|
|
256
340
|
TTLCache.prototype.values = function () {
|
|
257
|
-
var _a, _b, entry,
|
|
258
|
-
var
|
|
341
|
+
var _a, _b, entry, e_5_1;
|
|
342
|
+
var e_5, _c;
|
|
259
343
|
return __generator(this, function (_d) {
|
|
260
344
|
switch (_d.label) {
|
|
261
345
|
case 0:
|
|
@@ -275,19 +359,117 @@ var TTLCache = /** @class */ (function () {
|
|
|
275
359
|
return [3 /*break*/, 1];
|
|
276
360
|
case 4: return [3 /*break*/, 7];
|
|
277
361
|
case 5:
|
|
278
|
-
|
|
279
|
-
|
|
362
|
+
e_5_1 = _d.sent();
|
|
363
|
+
e_5 = { error: e_5_1 };
|
|
280
364
|
return [3 /*break*/, 7];
|
|
281
365
|
case 6:
|
|
282
366
|
try {
|
|
283
367
|
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
284
368
|
}
|
|
285
|
-
finally { if (
|
|
369
|
+
finally { if (e_5) throw e_5.error; }
|
|
286
370
|
return [7 /*endfinally*/];
|
|
287
371
|
case 7: return [2 /*return*/];
|
|
288
372
|
}
|
|
289
373
|
});
|
|
290
374
|
};
|
|
375
|
+
/**
|
|
376
|
+
* Extends the expiration of a key by the specified time or default TTL.
|
|
377
|
+
*
|
|
378
|
+
* @param key - The key to refresh
|
|
379
|
+
* @param ttlMs - Optional new TTL in milliseconds (uses default if not specified)
|
|
380
|
+
* @returns true if the key was found and refreshed, false otherwise
|
|
381
|
+
*/
|
|
382
|
+
TTLCache.prototype.refresh = function (key, ttlMs) {
|
|
383
|
+
var entry = this.cache.get(key);
|
|
384
|
+
if (!entry)
|
|
385
|
+
return false;
|
|
386
|
+
var now = Date.now();
|
|
387
|
+
if (now > entry.expiresAt) {
|
|
388
|
+
this.cache.delete(key);
|
|
389
|
+
return false;
|
|
390
|
+
}
|
|
391
|
+
entry.expiresAt = now + (ttlMs !== null && ttlMs !== void 0 ? ttlMs : this.ttlMs);
|
|
392
|
+
entry.lastAccessed = now;
|
|
393
|
+
return true;
|
|
394
|
+
};
|
|
395
|
+
/**
|
|
396
|
+
* Returns a snapshot of cache stats.
|
|
397
|
+
*
|
|
398
|
+
* @returns Object containing cache statistics
|
|
399
|
+
*/
|
|
400
|
+
TTLCache.prototype.stats = function () {
|
|
401
|
+
var e_6, _a;
|
|
402
|
+
var now = Date.now();
|
|
403
|
+
var expired = 0;
|
|
404
|
+
var valid = 0;
|
|
405
|
+
try {
|
|
406
|
+
for (var _b = __values(this.cache.values()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
407
|
+
var entry = _c.value;
|
|
408
|
+
if (now > entry.expiresAt) {
|
|
409
|
+
expired++;
|
|
410
|
+
}
|
|
411
|
+
else {
|
|
412
|
+
valid++;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
catch (e_6_1) { e_6 = { error: e_6_1 }; }
|
|
417
|
+
finally {
|
|
418
|
+
try {
|
|
419
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
420
|
+
}
|
|
421
|
+
finally { if (e_6) throw e_6.error; }
|
|
422
|
+
}
|
|
423
|
+
return {
|
|
424
|
+
size: this.cache.size,
|
|
425
|
+
validEntries: valid,
|
|
426
|
+
expiredEntries: expired,
|
|
427
|
+
maxSize: this.maxSize
|
|
428
|
+
};
|
|
429
|
+
};
|
|
430
|
+
/**
|
|
431
|
+
* Stop the auto-cleanup interval if it's running.
|
|
432
|
+
*/
|
|
433
|
+
TTLCache.prototype.destroy = function () {
|
|
434
|
+
if (this.cleanupInterval) {
|
|
435
|
+
clearInterval(this.cleanupInterval);
|
|
436
|
+
this.cleanupInterval = undefined;
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
/**
|
|
440
|
+
* Evict the least recently used entry from the cache.
|
|
441
|
+
* @private
|
|
442
|
+
*/
|
|
443
|
+
TTLCache.prototype.evictLRU = function () {
|
|
444
|
+
var e_7, _a;
|
|
445
|
+
var oldestKey;
|
|
446
|
+
var oldestAccess = Infinity;
|
|
447
|
+
try {
|
|
448
|
+
for (var _b = __values(this.cache.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
449
|
+
var _d = __read(_c.value, 2), key = _d[0], entry = _d[1];
|
|
450
|
+
// Skip entries that are already expired
|
|
451
|
+
if (Date.now() > entry.expiresAt) {
|
|
452
|
+
this.cache.delete(key);
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
// Find the oldest accessed entry
|
|
456
|
+
if (entry.lastAccessed && entry.lastAccessed < oldestAccess) {
|
|
457
|
+
oldestAccess = entry.lastAccessed;
|
|
458
|
+
oldestKey = key;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
catch (e_7_1) { e_7 = { error: e_7_1 }; }
|
|
463
|
+
finally {
|
|
464
|
+
try {
|
|
465
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
466
|
+
}
|
|
467
|
+
finally { if (e_7) throw e_7.error; }
|
|
468
|
+
}
|
|
469
|
+
if (oldestKey) {
|
|
470
|
+
this.cache.delete(oldestKey);
|
|
471
|
+
}
|
|
472
|
+
};
|
|
291
473
|
return TTLCache;
|
|
292
474
|
}());
|
|
293
475
|
export { TTLCache };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.utils.js","sourceRoot":"","sources":["../../../src/utils/cache.utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAQA;;;;;;;;;;GAUG;AACH;IAGE;;OAEG;IACH,kBAAoB,KAA6B;QAA7B,sBAAA,EAAA,QAAgB,CAAC,GAAG,EAAE,GAAG,IAAI;QAA7B,UAAK,GAAL,KAAK,CAAwB;QALzC,UAAK,GAAG,IAAI,GAAG,EAAoB,CAAC;IAKQ,CAAC;IAErD;;;;;OAKG;IACH,sBAAG,GAAH,UAAI,GAAM,EAAE,KAAQ;QAClB,IAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;QAC1C,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,KAAK,OAAA,EAAE,SAAS,WAAA,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED;;;;;;OAMG;IACH,6BAAU,GAAV,UAAW,GAAM,EAAE,KAAQ,EAAE,KAAa;QACxC,IAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC;QACrC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,KAAK,OAAA,EAAE,SAAS,WAAA,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED;;;;;OAKG;IACH,sBAAG,GAAH,UAAI,GAAM;QACR,IAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAClC,IAAI,CAAC,KAAK;YAAE,OAAO,SAAS,CAAC;QAC7B,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,SAAS,EAAE,CAAC;YACjC,IAAI,CAAC,KAAK,CAAC,MAAM,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,KAAK,CAAC,SAAS,CAAA,EAA7B,wBAA6B;oBAC/B,qBAAM,CAAC,GAAG,EAAE,KAAK,CAAC,KAAK,CAAC,EAAA;;oBAAxB,SAAwB,CAAC;;;;;;;;;;;;;;;;;;;KAG9B;IAED;;;;OAIG;IACF,uBAAI,GAAL;;;;;;;oBAC6B,KAAA,SAAA,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAA;;;;oBAApC,KAAA,mBAAY,EAAX,GAAG,QAAA,EAAE,KAAK,QAAA;yBAChB,CAAA,IAAI,CAAC,GAAG,EAAE,IAAI,KAAK,CAAC,SAAS,CAAA,EAA7B,wBAA6B;oBAC/B,qBAAM,GAAG,EAAA;;oBAAT,SAAS,CAAC;;;;;;;;;;;;;;;;;;;KAGf;IAED;;;;OAIG;IACF,yBAAM,GAAP;;;;;;;oBACsB,KAAA,SAAA,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAA;;;;oBAA5B,KAAK;yBACV,CAAA,IAAI,CAAC,GAAG,EAAE,IAAI,KAAK,CAAC,SAAS,CAAA,EAA7B,wBAA6B;oBAC/B,qBAAM,KAAK,CAAC,KAAK,EAAA;;oBAAjB,SAAiB,CAAC;;;;;;;;;;;;;;;;;;;KAGvB;IACH,eAAC;AAAD,CAAC,AA1ID,IA0IC","sourcesContent":["/**\n * Represents a cached entry with a value and an expiration timestamp.\n */\ntype CacheEntry<T> = {\n value: T;\n expiresAt: 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>(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\n /**\n * @param ttlMs - Default time-to-live in milliseconds for cache entries.\n */\n constructor(private ttlMs: number = 5 * 60 * 1000) {}\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 const expiresAt = Date.now() + this.ttlMs;\n this.cache.set(key, { value, expiresAt });\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 expiresAt = Date.now() + ttlMs;\n this.cache.set(key, { value, expiresAt });\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 if (Date.now() > entry.expiresAt) {\n this.cache.delete(key);\n return undefined;\n }\n return entry.value;\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 * 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"]}
|
|
1
|
+
{"version":3,"file":"cache.utils.js","sourceRoot":"","sources":["../../../src/utils/cache.utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,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,OAAO,CAAC,OAAO,CAAC;QAE/B,gCAAgC;QAChC,IAAM,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;QAC5C,IAAI,aAAa,IAAI,aAAa,GAAG,CAAC,EAAE,CAAC;YACvC,IAAI,CAAC,eAAe,GAAG,WAAW,CAAC;gBACjC,KAAI,CAAC,OAAO,EAAE,CAAC;YACjB,CAAC,EAAE,aAAa,CAAC,CAAC;QACpB,CAAC;IACH,CAAC;IAED;;;;;OAKG;IACH,sBAAG,GAAH,UAAI,GAAM,EAAE,KAAQ;QAClB,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1C,CAAC;IAED;;;;;;OAMG;IACH,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ED;;OAEG;IACH,0BAAO,GAAP;QACE,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,2BAAQ,GAAhB;;QACE,IAAI,SAAwB,CAAC;QAC7B,IAAI,YAAY,GAAG,QAAQ,CAAC;;YAE5B,KAA2B,IAAA,KAAA,SAAA,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAA,gBAAA,4BAAE,CAAC;gBAAvC,IAAA,KAAA,mBAAY,EAAX,GAAG,QAAA,EAAE,KAAK,QAAA;gBACpB,wCAAwC;gBACxC,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,SAAS,EAAE,CAAC;oBACjC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;oBACvB,OAAO;gBACT,CAAC;gBAED,iCAAiC;gBACjC,IAAI,KAAK,CAAC,YAAY,IAAI,KAAK,CAAC,YAAY,GAAG,YAAY,EAAE,CAAC;oBAC5D,YAAY,GAAG,KAAK,CAAC,YAAY,CAAC;oBAClC,SAAS,GAAG,GAAG,CAAC;gBAClB,CAAC;YACH,CAAC;;;;;;;;;QAED,IAAI,SAAS,EAAE,CAAC;YACd,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QAC/B,CAAC;IACH,CAAC;IACH,eAAC;AAAD,CAAC,AA7RD,IA6RC","sourcesContent":["import { 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 this.cache.set(key, { value, expiresAt, lastAccessed: now });\n\n // Enforce max size limit if configured\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 last access time for LRU tracking\n entry.lastAccessed = now;\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 let oldestKey: K | undefined;\n let oldestAccess = Infinity;\n\n for (const [key, entry] of this.cache.entries()) {\n // Skip entries that are already expired\n if (Date.now() > entry.expiresAt) {\n this.cache.delete(key);\n return;\n }\n\n // Find the oldest accessed entry\n if (entry.lastAccessed && entry.lastAccessed < oldestAccess) {\n oldestAccess = entry.lastAccessed;\n oldestKey = key;\n }\n }\n\n if (oldestKey) {\n this.cache.delete(oldestKey);\n }\n }\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AsyncLocalStorage } from
|
|
1
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
2
2
|
/**
|
|
3
3
|
* Type representing the store object used in AsyncLocalStorage.
|
|
4
4
|
* Keys must be unique symbols to ensure isolation and prevent collisions.
|
|
@@ -13,6 +13,10 @@ export interface Store {
|
|
|
13
13
|
export declare const StoreKeys: {
|
|
14
14
|
LOGGER: symbol;
|
|
15
15
|
REQUEST_ID: symbol;
|
|
16
|
+
USER_ID: symbol;
|
|
17
|
+
TENANT_ID: symbol;
|
|
18
|
+
TRACE_ID: symbol;
|
|
19
|
+
CORRELATION_ID: symbol;
|
|
16
20
|
};
|
|
17
21
|
/**
|
|
18
22
|
* Retrieves the current request ID from the async context, if available.
|
|
@@ -20,6 +24,13 @@ export declare const StoreKeys: {
|
|
|
20
24
|
* @returns {string | undefined} The request ID string or undefined if not present in the current context.
|
|
21
25
|
*/
|
|
22
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;
|
|
23
34
|
/**
|
|
24
35
|
* ContextStore manages per-request scoped context using AsyncLocalStorage.
|
|
25
36
|
* It allows storing and retrieving data across async calls (e.g., request ID, logger).
|
|
@@ -81,5 +92,95 @@ export declare class ContextStore {
|
|
|
81
92
|
* @returns {T} The result of the callback function.
|
|
82
93
|
*/
|
|
83
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;
|
|
84
185
|
}
|
|
85
186
|
//# sourceMappingURL=context-store.utils.d.ts.map
|