@catbee/utils 0.0.8-rc.0 → 0.0.8-rc.2
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 +25 -1
- package/build/esm/config.d.ts +0 -1
- package/build/esm/config.js +6 -7
- package/build/esm/index.d.ts +0 -1
- package/build/esm/index.js +0 -1
- package/build/esm/servers/server.builder.d.ts +0 -1
- package/build/esm/servers/server.builder.js +99 -147
- package/build/esm/servers/server.d.ts +0 -1
- package/build/esm/servers/server.js +591 -876
- package/build/esm/types/api-response.d.ts +0 -1
- package/build/esm/types/api-response.js +0 -1
- package/build/esm/types/index.d.ts +0 -1
- package/build/esm/types/index.js +0 -1
- package/build/esm/types/server.d.ts +0 -1
- package/build/esm/types/server.js +0 -1
- package/build/esm/utils/array.utils.d.ts +0 -1
- package/build/esm/utils/array.utils.js +48 -86
- package/build/esm/utils/async.utils.d.ts +0 -1
- package/build/esm/utils/async.utils.js +247 -524
- package/build/esm/utils/cache.utils.d.ts +0 -1
- package/build/esm/utils/cache.utils.js +96 -301
- package/build/esm/utils/context-store.utils.d.ts +0 -1
- package/build/esm/utils/context-store.utils.js +62 -79
- package/build/esm/utils/crypto.utils.d.ts +0 -1
- package/build/esm/utils/crypto.utils.js +78 -175
- package/build/esm/utils/date.utils.d.ts +0 -1
- package/build/esm/utils/date.utils.js +47 -53
- package/build/esm/utils/decorators.utils.d.ts +201 -5
- package/build/esm/utils/decorators.utils.js +632 -285
- package/build/esm/utils/dir.utils.d.ts +0 -1
- package/build/esm/utils/dir.utils.js +264 -786
- package/build/esm/utils/env.utils.d.ts +0 -1
- package/build/esm/utils/env.utils.js +180 -271
- package/build/esm/utils/exception.utils.d.ts +0 -1
- package/build/esm/utils/exception.utils.js +112 -289
- package/build/esm/utils/fs.utils.d.ts +0 -1
- package/build/esm/utils/fs.utils.js +169 -401
- package/build/esm/utils/http-status-codes.d.ts +0 -1
- package/build/esm/utils/http-status-codes.js +0 -1
- package/build/esm/utils/id.utils.d.ts +0 -1
- package/build/esm/utils/id.utils.js +5 -9
- package/build/esm/utils/logger.utils.d.ts +0 -1
- package/build/esm/utils/logger.utils.js +34 -71
- package/build/esm/utils/middleware.utils.d.ts +0 -1
- package/build/esm/utils/middleware.utils.js +65 -155
- package/build/esm/utils/obj.utils.d.ts +0 -1
- package/build/esm/utils/obj.utils.js +69 -176
- package/build/esm/utils/performance.utils.d.ts +0 -1
- package/build/esm/utils/performance.utils.js +92 -195
- package/build/esm/utils/request.utils.d.ts +0 -1
- package/build/esm/utils/request.utils.js +41 -74
- package/build/esm/utils/response.utils.d.ts +0 -1
- package/build/esm/utils/response.utils.js +84 -107
- package/build/esm/utils/stream.utils.d.ts +0 -1
- package/build/esm/utils/stream.utils.js +38 -113
- package/build/esm/utils/string.utils.d.ts +0 -1
- package/build/esm/utils/string.utils.js +6 -11
- package/build/esm/utils/type.utils.d.ts +0 -1
- package/build/esm/utils/type.utils.js +8 -12
- package/build/esm/utils/url.utils.d.ts +0 -1
- package/build/esm/utils/url.utils.js +49 -127
- package/build/esm/utils/validate.utils.d.ts +0 -1
- package/build/esm/utils/validate.utils.js +23 -40
- package/package.json +3 -33
- package/build/esm/config.js.map +0 -1
- package/build/esm/index.js.map +0 -1
- package/build/esm/servers/server.builder.js.map +0 -1
- package/build/esm/servers/server.js.map +0 -1
- package/build/esm/types/api-response.js.map +0 -1
- package/build/esm/types/index.js.map +0 -1
- package/build/esm/types/server.js.map +0 -1
- package/build/esm/utils/array.utils.js.map +0 -1
- package/build/esm/utils/async.utils.js.map +0 -1
- package/build/esm/utils/cache.utils.js.map +0 -1
- package/build/esm/utils/context-store.utils.js.map +0 -1
- package/build/esm/utils/crypto.utils.js.map +0 -1
- package/build/esm/utils/date.utils.js.map +0 -1
- package/build/esm/utils/decorators.utils.js.map +0 -1
- package/build/esm/utils/dir.utils.js.map +0 -1
- package/build/esm/utils/env.utils.js.map +0 -1
- package/build/esm/utils/exception.utils.js.map +0 -1
- package/build/esm/utils/fs.utils.js.map +0 -1
- package/build/esm/utils/http-status-codes.js.map +0 -1
- package/build/esm/utils/id.utils.js.map +0 -1
- package/build/esm/utils/logger.utils.js.map +0 -1
- package/build/esm/utils/middleware.utils.js.map +0 -1
- package/build/esm/utils/obj.utils.js.map +0 -1
- package/build/esm/utils/performance.utils.js.map +0 -1
- package/build/esm/utils/request.utils.js.map +0 -1
- package/build/esm/utils/response.utils.js.map +0 -1
- package/build/esm/utils/stream.utils.js.map +0 -1
- package/build/esm/utils/string.utils.js.map +0 -1
- package/build/esm/utils/type.utils.js.map +0 -1
- package/build/esm/utils/url.utils.js.map +0 -1
- package/build/esm/utils/validate.utils.js.map +0 -1
- package/build/esnext/config.d.ts +0 -122
- package/build/esnext/config.js +0 -133
- package/build/esnext/config.js.map +0 -1
- package/build/esnext/index.d.ts +0 -27
- package/build/esnext/index.js +0 -50
- package/build/esnext/index.js.map +0 -1
- package/build/esnext/servers/server.builder.d.ts +0 -508
- package/build/esnext/servers/server.builder.js +0 -657
- package/build/esnext/servers/server.builder.js.map +0 -1
- package/build/esnext/servers/server.d.ts +0 -256
- package/build/esnext/servers/server.js +0 -955
- package/build/esnext/servers/server.js.map +0 -1
- package/build/esnext/types/api-response.d.ts +0 -152
- package/build/esnext/types/api-response.js +0 -34
- package/build/esnext/types/api-response.js.map +0 -1
- package/build/esnext/types/index.d.ts +0 -125
- package/build/esnext/types/index.js +0 -25
- package/build/esnext/types/index.js.map +0 -1
- package/build/esnext/types/server.d.ts +0 -268
- package/build/esnext/types/server.js +0 -25
- package/build/esnext/types/server.js.map +0 -1
- package/build/esnext/utils/array.utils.d.ts +0 -168
- package/build/esnext/utils/array.utils.js +0 -345
- package/build/esnext/utils/array.utils.js.map +0 -1
- package/build/esnext/utils/async.utils.d.ts +0 -265
- package/build/esnext/utils/async.utils.js +0 -620
- package/build/esnext/utils/async.utils.js.map +0 -1
- package/build/esnext/utils/cache.utils.d.ts +0 -153
- package/build/esnext/utils/cache.utils.js +0 -293
- package/build/esnext/utils/cache.utils.js.map +0 -1
- package/build/esnext/utils/context-store.utils.d.ts +0 -189
- package/build/esnext/utils/context-store.utils.js +0 -289
- package/build/esnext/utils/context-store.utils.js.map +0 -1
- package/build/esnext/utils/crypto.utils.d.ts +0 -160
- package/build/esnext/utils/crypto.utils.js +0 -276
- package/build/esnext/utils/crypto.utils.js.map +0 -1
- package/build/esnext/utils/date.utils.d.ts +0 -159
- package/build/esnext/utils/date.utils.js +0 -384
- package/build/esnext/utils/date.utils.js.map +0 -1
- package/build/esnext/utils/decorators.utils.d.ts +0 -315
- package/build/esnext/utils/decorators.utils.js +0 -504
- package/build/esnext/utils/decorators.utils.js.map +0 -1
- package/build/esnext/utils/dir.utils.d.ts +0 -196
- package/build/esnext/utils/dir.utils.js +0 -477
- package/build/esnext/utils/dir.utils.js.map +0 -1
- package/build/esnext/utils/env.utils.d.ts +0 -377
- package/build/esnext/utils/env.utils.js +0 -785
- package/build/esnext/utils/env.utils.js.map +0 -1
- package/build/esnext/utils/exception.utils.d.ts +0 -230
- package/build/esnext/utils/exception.utils.js +0 -381
- package/build/esnext/utils/exception.utils.js.map +0 -1
- package/build/esnext/utils/fs.utils.d.ts +0 -164
- package/build/esnext/utils/fs.utils.js +0 -349
- package/build/esnext/utils/fs.utils.js.map +0 -1
- package/build/esnext/utils/http-status-codes.d.ts +0 -266
- package/build/esnext/utils/http-status-codes.js +0 -295
- package/build/esnext/utils/http-status-codes.js.map +0 -1
- package/build/esnext/utils/id.utils.d.ts +0 -36
- package/build/esnext/utils/id.utils.js +0 -84
- package/build/esnext/utils/id.utils.js.map +0 -1
- package/build/esnext/utils/logger.utils.d.ts +0 -160
- package/build/esnext/utils/logger.utils.js +0 -300
- package/build/esnext/utils/logger.utils.js.map +0 -1
- package/build/esnext/utils/middleware.utils.d.ts +0 -100
- package/build/esnext/utils/middleware.utils.js +0 -236
- package/build/esnext/utils/middleware.utils.js.map +0 -1
- package/build/esnext/utils/obj.utils.d.ts +0 -124
- package/build/esnext/utils/obj.utils.js +0 -413
- package/build/esnext/utils/obj.utils.js.map +0 -1
- package/build/esnext/utils/performance.utils.d.ts +0 -136
- package/build/esnext/utils/performance.utils.js +0 -271
- package/build/esnext/utils/performance.utils.js.map +0 -1
- package/build/esnext/utils/request.utils.d.ts +0 -86
- package/build/esnext/utils/request.utils.js +0 -186
- package/build/esnext/utils/request.utils.js.map +0 -1
- package/build/esnext/utils/response.utils.d.ts +0 -163
- package/build/esnext/utils/response.utils.js +0 -247
- package/build/esnext/utils/response.utils.js.map +0 -1
- package/build/esnext/utils/stream.utils.d.ts +0 -88
- package/build/esnext/utils/stream.utils.js +0 -210
- package/build/esnext/utils/stream.utils.js.map +0 -1
- package/build/esnext/utils/string.utils.d.ts +0 -93
- package/build/esnext/utils/string.utils.js +0 -165
- package/build/esnext/utils/string.utils.js.map +0 -1
- package/build/esnext/utils/type.utils.d.ts +0 -90
- package/build/esnext/utils/type.utils.js +0 -187
- package/build/esnext/utils/type.utils.js.map +0 -1
- package/build/esnext/utils/url.utils.d.ts +0 -141
- package/build/esnext/utils/url.utils.js +0 -304
- package/build/esnext/utils/url.utils.js.map +0 -1
- package/build/esnext/utils/validate.utils.d.ts +0 -177
- package/build/esnext/utils/validate.utils.js +0 -320
- package/build/esnext/utils/validate.utils.js.map +0 -1
- package/build/src/config.d.ts +0 -122
- package/build/src/config.js +0 -138
- package/build/src/config.js.map +0 -1
- package/build/src/index.d.ts +0 -27
- package/build/src/index.js +0 -69
- package/build/src/index.js.map +0 -1
- package/build/src/servers/server.builder.d.ts +0 -508
- package/build/src/servers/server.builder.js +0 -661
- package/build/src/servers/server.builder.js.map +0 -1
- package/build/src/servers/server.d.ts +0 -256
- package/build/src/servers/server.js +0 -962
- package/build/src/servers/server.js.map +0 -1
- package/build/src/types/api-response.d.ts +0 -152
- package/build/src/types/api-response.js +0 -37
- package/build/src/types/api-response.js.map +0 -1
- package/build/src/types/index.d.ts +0 -125
- package/build/src/types/index.js +0 -26
- package/build/src/types/index.js.map +0 -1
- package/build/src/types/server.d.ts +0 -268
- package/build/src/types/server.js +0 -26
- package/build/src/types/server.js.map +0 -1
- package/build/src/utils/array.utils.d.ts +0 -168
- package/build/src/utils/array.utils.js +0 -364
- package/build/src/utils/array.utils.js.map +0 -1
- package/build/src/utils/async.utils.d.ts +0 -265
- package/build/src/utils/async.utils.js +0 -641
- package/build/src/utils/async.utils.js.map +0 -1
- package/build/src/utils/cache.utils.d.ts +0 -153
- package/build/src/utils/cache.utils.js +0 -297
- package/build/src/utils/cache.utils.js.map +0 -1
- package/build/src/utils/context-store.utils.d.ts +0 -189
- package/build/src/utils/context-store.utils.js +0 -296
- package/build/src/utils/context-store.utils.js.map +0 -1
- package/build/src/utils/crypto.utils.d.ts +0 -160
- package/build/src/utils/crypto.utils.js +0 -293
- package/build/src/utils/crypto.utils.js.map +0 -1
- package/build/src/utils/date.utils.d.ts +0 -159
- package/build/src/utils/date.utils.js +0 -396
- package/build/src/utils/date.utils.js.map +0 -1
- package/build/src/utils/decorators.utils.d.ts +0 -315
- package/build/src/utils/decorators.utils.js +0 -516
- package/build/src/utils/decorators.utils.js.map +0 -1
- package/build/src/utils/dir.utils.d.ts +0 -196
- package/build/src/utils/dir.utils.js +0 -501
- package/build/src/utils/dir.utils.js.map +0 -1
- package/build/src/utils/env.utils.d.ts +0 -377
- package/build/src/utils/env.utils.js +0 -789
- package/build/src/utils/env.utils.js.map +0 -1
- package/build/src/utils/exception.utils.d.ts +0 -230
- package/build/src/utils/exception.utils.js +0 -407
- package/build/src/utils/exception.utils.js.map +0 -1
- package/build/src/utils/fs.utils.d.ts +0 -164
- package/build/src/utils/fs.utils.js +0 -372
- package/build/src/utils/fs.utils.js.map +0 -1
- package/build/src/utils/http-status-codes.d.ts +0 -266
- package/build/src/utils/http-status-codes.js +0 -298
- package/build/src/utils/http-status-codes.js.map +0 -1
- package/build/src/utils/id.utils.d.ts +0 -36
- package/build/src/utils/id.utils.js +0 -91
- package/build/src/utils/id.utils.js.map +0 -1
- package/build/src/utils/logger.utils.d.ts +0 -160
- package/build/src/utils/logger.utils.js +0 -346
- package/build/src/utils/logger.utils.js.map +0 -1
- package/build/src/utils/middleware.utils.d.ts +0 -100
- package/build/src/utils/middleware.utils.js +0 -244
- package/build/src/utils/middleware.utils.js.map +0 -1
- package/build/src/utils/obj.utils.d.ts +0 -124
- package/build/src/utils/obj.utils.js +0 -429
- package/build/src/utils/obj.utils.js.map +0 -1
- package/build/src/utils/performance.utils.d.ts +0 -136
- package/build/src/utils/performance.utils.js +0 -278
- package/build/src/utils/performance.utils.js.map +0 -1
- package/build/src/utils/request.utils.d.ts +0 -86
- package/build/src/utils/request.utils.js +0 -195
- package/build/src/utils/request.utils.js.map +0 -1
- package/build/src/utils/response.utils.d.ts +0 -163
- package/build/src/utils/response.utils.js +0 -260
- package/build/src/utils/response.utils.js.map +0 -1
- package/build/src/utils/stream.utils.d.ts +0 -88
- package/build/src/utils/stream.utils.js +0 -218
- package/build/src/utils/stream.utils.js.map +0 -1
- package/build/src/utils/string.utils.d.ts +0 -93
- package/build/src/utils/string.utils.js +0 -179
- package/build/src/utils/string.utils.js.map +0 -1
- package/build/src/utils/type.utils.d.ts +0 -90
- package/build/src/utils/type.utils.js +0 -196
- package/build/src/utils/type.utils.js.map +0 -1
- package/build/src/utils/url.utils.d.ts +0 -141
- package/build/src/utils/url.utils.js +0 -317
- package/build/src/utils/url.utils.js.map +0 -1
- package/build/src/utils/validate.utils.d.ts +0 -177
- package/build/src/utils/validate.utils.js +0 -345
- package/build/src/utils/validate.utils.js.map +0 -1
|
@@ -21,69 +21,6 @@
|
|
|
21
21
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
22
|
* SOFTWARE.
|
|
23
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
24
|
import { config } from '../config';
|
|
88
25
|
/**
|
|
89
26
|
* An in-memory cache with time-to-live (TTL) support for each entry.
|
|
@@ -96,22 +33,22 @@ import { config } from '../config';
|
|
|
96
33
|
* cache.set("x", 123);
|
|
97
34
|
* const value = cache.get("x"); // 123
|
|
98
35
|
*/
|
|
99
|
-
|
|
36
|
+
export class TTLCache {
|
|
37
|
+
cache = new Map();
|
|
38
|
+
ttlMs;
|
|
39
|
+
maxSize;
|
|
40
|
+
cleanupInterval;
|
|
100
41
|
/**
|
|
101
42
|
* @param options - Configuration options for the cache
|
|
102
43
|
*/
|
|
103
|
-
|
|
104
|
-
|
|
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;
|
|
44
|
+
constructor(options = {}) {
|
|
45
|
+
this.ttlMs = options.ttlMs ?? config.cache.defaultTtl;
|
|
109
46
|
this.maxSize = options.maxSize;
|
|
110
47
|
// Setup auto-cleanup if enabled
|
|
111
|
-
|
|
48
|
+
const autoCleanupMs = options.autoCleanupMs;
|
|
112
49
|
if (autoCleanupMs && autoCleanupMs > 0) {
|
|
113
|
-
this.cleanupInterval = setInterval(
|
|
114
|
-
|
|
50
|
+
this.cleanupInterval = setInterval(() => {
|
|
51
|
+
this.cleanup();
|
|
115
52
|
}, autoCleanupMs);
|
|
116
53
|
}
|
|
117
54
|
}
|
|
@@ -121,9 +58,9 @@ var TTLCache = /** @class */ (function () {
|
|
|
121
58
|
* @param key - The key to set.
|
|
122
59
|
* @param value - The value to associate with the key.
|
|
123
60
|
*/
|
|
124
|
-
|
|
61
|
+
set(key, value) {
|
|
125
62
|
this.setWithTTL(key, value, this.ttlMs);
|
|
126
|
-
}
|
|
63
|
+
}
|
|
127
64
|
/**
|
|
128
65
|
* Sets a key-value pair in the cache with a custom TTL.
|
|
129
66
|
*
|
|
@@ -131,28 +68,28 @@ var TTLCache = /** @class */ (function () {
|
|
|
131
68
|
* @param value - The value to associate with the key.
|
|
132
69
|
* @param ttlMs - Time-to-live in milliseconds.
|
|
133
70
|
*/
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
71
|
+
setWithTTL(key, value, ttlMs) {
|
|
72
|
+
const now = Date.now();
|
|
73
|
+
const expiresAt = now + ttlMs;
|
|
137
74
|
if (this.cache.has(key)) {
|
|
138
75
|
this.cache.delete(key); // remove old entry to maintain LRU order
|
|
139
76
|
}
|
|
140
|
-
this.cache.set(key, { value
|
|
77
|
+
this.cache.set(key, { value, expiresAt, lastAccessed: now });
|
|
141
78
|
if (this.maxSize && this.cache.size > this.maxSize) {
|
|
142
79
|
this.evictLRU();
|
|
143
80
|
}
|
|
144
|
-
}
|
|
81
|
+
}
|
|
145
82
|
/**
|
|
146
83
|
* Retrieves the value for a given key if it hasn't expired.
|
|
147
84
|
*
|
|
148
85
|
* @param key - The key to retrieve.
|
|
149
86
|
* @returns The cached value, or undefined if not found or expired.
|
|
150
87
|
*/
|
|
151
|
-
|
|
152
|
-
|
|
88
|
+
get(key) {
|
|
89
|
+
const entry = this.cache.get(key);
|
|
153
90
|
if (!entry)
|
|
154
91
|
return undefined;
|
|
155
|
-
|
|
92
|
+
const now = Date.now();
|
|
156
93
|
if (now > entry.expiresAt) {
|
|
157
94
|
this.cache.delete(key);
|
|
158
95
|
return undefined;
|
|
@@ -163,7 +100,7 @@ var TTLCache = /** @class */ (function () {
|
|
|
163
100
|
this.cache.delete(key);
|
|
164
101
|
this.cache.set(key, entry);
|
|
165
102
|
return entry.value;
|
|
166
|
-
}
|
|
103
|
+
}
|
|
167
104
|
/**
|
|
168
105
|
* Retrieves or computes a value if it's not in the cache or has expired.
|
|
169
106
|
*
|
|
@@ -172,234 +109,117 @@ var TTLCache = /** @class */ (function () {
|
|
|
172
109
|
* @param ttlMs - Optional custom TTL for the computed value
|
|
173
110
|
* @returns The cached or computed value
|
|
174
111
|
*/
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
return
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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
|
-
};
|
|
112
|
+
async getOrCompute(key, producer, ttlMs) {
|
|
113
|
+
const value = this.get(key);
|
|
114
|
+
if (value !== undefined)
|
|
115
|
+
return value;
|
|
116
|
+
const newValue = await producer();
|
|
117
|
+
this.setWithTTL(key, newValue, ttlMs ?? this.ttlMs);
|
|
118
|
+
return newValue;
|
|
119
|
+
}
|
|
193
120
|
/**
|
|
194
121
|
* Checks if the key exists and hasn't expired.
|
|
195
122
|
*
|
|
196
123
|
* @param key - The key to check.
|
|
197
124
|
* @returns `true` if key exists and is valid, otherwise `false`.
|
|
198
125
|
*/
|
|
199
|
-
|
|
126
|
+
has(key) {
|
|
200
127
|
return this.get(key) !== undefined;
|
|
201
|
-
}
|
|
128
|
+
}
|
|
202
129
|
/**
|
|
203
130
|
* Deletes a key from the cache.
|
|
204
131
|
*
|
|
205
132
|
* @param key - The key to delete.
|
|
206
133
|
* @returns `true` if the key existed and was removed, `false` otherwise.
|
|
207
134
|
*/
|
|
208
|
-
|
|
135
|
+
delete(key) {
|
|
209
136
|
return this.cache.delete(key);
|
|
210
|
-
}
|
|
137
|
+
}
|
|
211
138
|
/**
|
|
212
139
|
* Clears all entries from the cache.
|
|
213
140
|
*/
|
|
214
|
-
|
|
141
|
+
clear() {
|
|
215
142
|
this.cache.clear();
|
|
216
|
-
}
|
|
143
|
+
}
|
|
217
144
|
/**
|
|
218
145
|
* Returns the number of entries currently in the cache (includes expired entries until next access/cleanup).
|
|
219
146
|
*
|
|
220
147
|
* @returns Number of items in the cache (may include expired keys).
|
|
221
148
|
*/
|
|
222
|
-
|
|
149
|
+
size() {
|
|
223
150
|
return this.cache.size;
|
|
224
|
-
}
|
|
151
|
+
}
|
|
225
152
|
/**
|
|
226
153
|
* Set multiple key-value pairs at once with the default TTL.
|
|
227
154
|
*
|
|
228
155
|
* @param entries - Array of [key, value] tuples to set
|
|
229
156
|
*/
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
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; }
|
|
157
|
+
setMany(entries) {
|
|
158
|
+
for (const [key, value] of entries) {
|
|
159
|
+
this.set(key, value);
|
|
244
160
|
}
|
|
245
|
-
}
|
|
161
|
+
}
|
|
246
162
|
/**
|
|
247
163
|
* Get multiple values at once.
|
|
248
164
|
*
|
|
249
165
|
* @param keys - Array of keys to retrieve
|
|
250
166
|
* @returns Array of values (undefined for keys that don't exist or expired)
|
|
251
167
|
*/
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
};
|
|
168
|
+
getMany(keys) {
|
|
169
|
+
return keys.map(key => this.get(key));
|
|
170
|
+
}
|
|
256
171
|
/**
|
|
257
172
|
* Removes all expired entries from the cache.
|
|
258
173
|
*
|
|
259
174
|
* @returns Number of entries removed.
|
|
260
175
|
*/
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
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);
|
|
176
|
+
cleanup() {
|
|
177
|
+
let removed = 0;
|
|
178
|
+
const now = Date.now();
|
|
179
|
+
for (const [key, entry] of this.cache.entries()) {
|
|
180
|
+
if (now > entry.expiresAt) {
|
|
181
|
+
this.cache.delete(key);
|
|
182
|
+
removed++;
|
|
278
183
|
}
|
|
279
|
-
finally { if (e_2) throw e_2.error; }
|
|
280
184
|
}
|
|
281
185
|
return removed;
|
|
282
|
-
}
|
|
186
|
+
}
|
|
283
187
|
/**
|
|
284
188
|
* Returns an iterator of all current valid [key, value] pairs.
|
|
285
189
|
*
|
|
286
190
|
* @returns IterableIterator<[K, V]>
|
|
287
191
|
*/
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
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*/];
|
|
192
|
+
*entries() {
|
|
193
|
+
for (const [key, entry] of this.cache.entries()) {
|
|
194
|
+
if (Date.now() <= entry.expiresAt) {
|
|
195
|
+
yield [key, entry.value];
|
|
320
196
|
}
|
|
321
|
-
}
|
|
322
|
-
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
323
199
|
/**
|
|
324
200
|
* Returns an iterator of all current valid keys.
|
|
325
201
|
*
|
|
326
202
|
* @returns IterableIterator<K>
|
|
327
203
|
*/
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
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*/];
|
|
204
|
+
*keys() {
|
|
205
|
+
for (const [key, entry] of this.cache.entries()) {
|
|
206
|
+
if (Date.now() <= entry.expiresAt) {
|
|
207
|
+
yield key;
|
|
360
208
|
}
|
|
361
|
-
}
|
|
362
|
-
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
363
211
|
/**
|
|
364
212
|
* Returns an iterator of all current valid values.
|
|
365
213
|
*
|
|
366
214
|
* @returns IterableIterator<V>
|
|
367
215
|
*/
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
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*/];
|
|
216
|
+
*values() {
|
|
217
|
+
for (const entry of this.cache.values()) {
|
|
218
|
+
if (Date.now() <= entry.expiresAt) {
|
|
219
|
+
yield entry.value;
|
|
400
220
|
}
|
|
401
|
-
}
|
|
402
|
-
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
403
223
|
/**
|
|
404
224
|
* Extends the expiration of a key by the specified time or default TTL.
|
|
405
225
|
*
|
|
@@ -407,46 +227,35 @@ var TTLCache = /** @class */ (function () {
|
|
|
407
227
|
* @param ttlMs - Optional new TTL in milliseconds (uses default if not specified)
|
|
408
228
|
* @returns true if the key was found and refreshed, false otherwise
|
|
409
229
|
*/
|
|
410
|
-
|
|
411
|
-
|
|
230
|
+
refresh(key, ttlMs) {
|
|
231
|
+
const entry = this.cache.get(key);
|
|
412
232
|
if (!entry)
|
|
413
233
|
return false;
|
|
414
|
-
|
|
234
|
+
const now = Date.now();
|
|
415
235
|
if (now > entry.expiresAt) {
|
|
416
236
|
this.cache.delete(key);
|
|
417
237
|
return false;
|
|
418
238
|
}
|
|
419
|
-
entry.expiresAt = now + (ttlMs
|
|
239
|
+
entry.expiresAt = now + (ttlMs ?? this.ttlMs);
|
|
420
240
|
entry.lastAccessed = now;
|
|
421
241
|
return true;
|
|
422
|
-
}
|
|
242
|
+
}
|
|
423
243
|
/**
|
|
424
244
|
* Returns a snapshot of cache stats.
|
|
425
245
|
*
|
|
426
246
|
* @returns Object containing cache statistics
|
|
427
247
|
*/
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
var entry = _c.value;
|
|
436
|
-
if (now > entry.expiresAt) {
|
|
437
|
-
expired++;
|
|
438
|
-
}
|
|
439
|
-
else {
|
|
440
|
-
valid++;
|
|
441
|
-
}
|
|
248
|
+
stats() {
|
|
249
|
+
const now = Date.now();
|
|
250
|
+
let expired = 0;
|
|
251
|
+
let valid = 0;
|
|
252
|
+
for (const entry of this.cache.values()) {
|
|
253
|
+
if (now > entry.expiresAt) {
|
|
254
|
+
expired++;
|
|
442
255
|
}
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
finally {
|
|
446
|
-
try {
|
|
447
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
256
|
+
else {
|
|
257
|
+
valid++;
|
|
448
258
|
}
|
|
449
|
-
finally { if (e_6) throw e_6.error; }
|
|
450
259
|
}
|
|
451
260
|
return {
|
|
452
261
|
size: this.cache.size,
|
|
@@ -454,46 +263,32 @@ var TTLCache = /** @class */ (function () {
|
|
|
454
263
|
expiredEntries: expired,
|
|
455
264
|
maxSize: this.maxSize
|
|
456
265
|
};
|
|
457
|
-
}
|
|
266
|
+
}
|
|
458
267
|
/**
|
|
459
268
|
* Stop the auto-cleanup interval if it's running.
|
|
460
269
|
*/
|
|
461
|
-
|
|
270
|
+
destroy() {
|
|
462
271
|
if (this.cleanupInterval) {
|
|
463
272
|
clearInterval(this.cleanupInterval);
|
|
464
273
|
this.cleanupInterval = undefined;
|
|
465
274
|
}
|
|
466
|
-
}
|
|
275
|
+
}
|
|
467
276
|
/**
|
|
468
277
|
* Evict the least recently used entry from the cache.
|
|
469
278
|
* @private
|
|
470
279
|
*/
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
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);
|
|
280
|
+
evictLRU() {
|
|
281
|
+
const now = Date.now();
|
|
282
|
+
// Remove all expired items first
|
|
283
|
+
for (const [key, entry] of Array.from(this.cache.entries())) {
|
|
284
|
+
if (now > entry.expiresAt) {
|
|
285
|
+
this.cache.delete(key);
|
|
487
286
|
}
|
|
488
|
-
finally { if (e_7) throw e_7.error; }
|
|
489
287
|
}
|
|
490
288
|
// Evict oldest entries until within max size
|
|
491
289
|
while (this.maxSize && this.cache.size > this.maxSize) {
|
|
492
|
-
|
|
290
|
+
const oldestKey = this.cache.keys().next().value;
|
|
493
291
|
this.cache.delete(oldestKey);
|
|
494
292
|
}
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
}());
|
|
498
|
-
export { TTLCache };
|
|
499
|
-
//# sourceMappingURL=cache.utils.js.map
|
|
293
|
+
}
|
|
294
|
+
}
|