@catbee/utils 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +295 -0
- package/build/esm/config.d.ts +22 -0
- package/build/esm/config.js +21 -0
- package/build/esm/config.js.map +1 -0
- package/build/esm/index.d.ts +19 -0
- package/build/esm/index.js +19 -0
- package/build/esm/index.js.map +1 -0
- package/build/esm/types/api-response.d.ts +44 -0
- package/build/esm/types/api-response.js +2 -0
- package/build/esm/types/api-response.js.map +1 -0
- package/build/esm/utils/array.utils.d.ts +105 -0
- package/build/esm/utils/array.utils.js +232 -0
- package/build/esm/utils/array.utils.js.map +1 -0
- package/build/esm/utils/async.utils.d.ts +119 -0
- package/build/esm/utils/async.utils.js +474 -0
- package/build/esm/utils/async.utils.js.map +1 -0
- package/build/esm/utils/cache.utils.d.ts +90 -0
- package/build/esm/utils/cache.utils.js +294 -0
- package/build/esm/utils/cache.utils.js.map +1 -0
- package/build/esm/utils/context-store.utils.d.ts +85 -0
- package/build/esm/utils/context-store.utils.js +99 -0
- package/build/esm/utils/context-store.utils.js.map +1 -0
- package/build/esm/utils/crypto.utils.d.ts +58 -0
- package/build/esm/utils/crypto.utils.js +76 -0
- package/build/esm/utils/crypto.utils.js.map +1 -0
- package/build/esm/utils/dir.utils.d.ts +75 -0
- package/build/esm/utils/dir.utils.js +400 -0
- package/build/esm/utils/dir.utils.js.map +1 -0
- package/build/esm/utils/env.utils.d.ts +131 -0
- package/build/esm/utils/env.utils.js +218 -0
- package/build/esm/utils/env.utils.js.map +1 -0
- package/build/esm/utils/exception.utils.d.ts +114 -0
- package/build/esm/utils/exception.utils.js +217 -0
- package/build/esm/utils/exception.utils.js.map +1 -0
- package/build/esm/utils/fs.utils.d.ts +32 -0
- package/build/esm/utils/fs.utils.js +140 -0
- package/build/esm/utils/fs.utils.js.map +1 -0
- package/build/esm/utils/http-status-codes.d.ts +357 -0
- package/build/esm/utils/http-status-codes.js +358 -0
- package/build/esm/utils/http-status-codes.js.map +1 -0
- package/build/esm/utils/id.utils.d.ts +35 -0
- package/build/esm/utils/id.utils.js +55 -0
- package/build/esm/utils/id.utils.js.map +1 -0
- package/build/esm/utils/logger.utils.d.ts +15 -0
- package/build/esm/utils/logger.utils.js +59 -0
- package/build/esm/utils/logger.utils.js.map +1 -0
- package/build/esm/utils/obj.utils.d.ts +55 -0
- package/build/esm/utils/obj.utils.js +121 -0
- package/build/esm/utils/obj.utils.js.map +1 -0
- package/build/esm/utils/response.utils.d.ts +48 -0
- package/build/esm/utils/response.utils.js +80 -0
- package/build/esm/utils/response.utils.js.map +1 -0
- package/build/esm/utils/string.utils.d.ts +37 -0
- package/build/esm/utils/string.utils.js +56 -0
- package/build/esm/utils/string.utils.js.map +1 -0
- package/build/esm/utils/url.utils.d.ts +24 -0
- package/build/esm/utils/url.utils.js +73 -0
- package/build/esm/utils/url.utils.js.map +1 -0
- package/build/esm/utils/validate.utils.d.ts +60 -0
- package/build/esm/utils/validate.utils.js +121 -0
- package/build/esm/utils/validate.utils.js.map +1 -0
- package/build/esnext/config.d.ts +22 -0
- package/build/esnext/config.js +21 -0
- package/build/esnext/config.js.map +1 -0
- package/build/esnext/index.d.ts +19 -0
- package/build/esnext/index.js +19 -0
- package/build/esnext/index.js.map +1 -0
- package/build/esnext/types/api-response.d.ts +44 -0
- package/build/esnext/types/api-response.js +2 -0
- package/build/esnext/types/api-response.js.map +1 -0
- package/build/esnext/utils/array.utils.d.ts +105 -0
- package/build/esnext/utils/array.utils.js +203 -0
- package/build/esnext/utils/array.utils.js.map +1 -0
- package/build/esnext/utils/async.utils.d.ts +119 -0
- package/build/esnext/utils/async.utils.js +291 -0
- package/build/esnext/utils/async.utils.js.map +1 -0
- package/build/esnext/utils/cache.utils.d.ts +90 -0
- package/build/esnext/utils/cache.utils.js +142 -0
- package/build/esnext/utils/cache.utils.js.map +1 -0
- package/build/esnext/utils/context-store.utils.d.ts +85 -0
- package/build/esnext/utils/context-store.utils.js +95 -0
- package/build/esnext/utils/context-store.utils.js.map +1 -0
- package/build/esnext/utils/crypto.utils.d.ts +58 -0
- package/build/esnext/utils/crypto.utils.js +72 -0
- package/build/esnext/utils/crypto.utils.js.map +1 -0
- package/build/esnext/utils/dir.utils.d.ts +75 -0
- package/build/esnext/utils/dir.utils.js +150 -0
- package/build/esnext/utils/dir.utils.js.map +1 -0
- package/build/esnext/utils/env.utils.d.ts +131 -0
- package/build/esnext/utils/env.utils.js +211 -0
- package/build/esnext/utils/env.utils.js.map +1 -0
- package/build/esnext/utils/exception.utils.d.ts +114 -0
- package/build/esnext/utils/exception.utils.js +148 -0
- package/build/esnext/utils/exception.utils.js.map +1 -0
- package/build/esnext/utils/fs.utils.d.ts +32 -0
- package/build/esnext/utils/fs.utils.js +62 -0
- package/build/esnext/utils/fs.utils.js.map +1 -0
- package/build/esnext/utils/http-status-codes.d.ts +357 -0
- package/build/esnext/utils/http-status-codes.js +358 -0
- package/build/esnext/utils/http-status-codes.js.map +1 -0
- package/build/esnext/utils/id.utils.d.ts +35 -0
- package/build/esnext/utils/id.utils.js +53 -0
- package/build/esnext/utils/id.utils.js.map +1 -0
- package/build/esnext/utils/logger.utils.d.ts +15 -0
- package/build/esnext/utils/logger.utils.js +59 -0
- package/build/esnext/utils/logger.utils.js.map +1 -0
- package/build/esnext/utils/obj.utils.d.ts +55 -0
- package/build/esnext/utils/obj.utils.js +94 -0
- package/build/esnext/utils/obj.utils.js.map +1 -0
- package/build/esnext/utils/response.utils.d.ts +48 -0
- package/build/esnext/utils/response.utils.js +58 -0
- package/build/esnext/utils/response.utils.js.map +1 -0
- package/build/esnext/utils/string.utils.d.ts +37 -0
- package/build/esnext/utils/string.utils.js +46 -0
- package/build/esnext/utils/string.utils.js.map +1 -0
- package/build/esnext/utils/url.utils.d.ts +24 -0
- package/build/esnext/utils/url.utils.js +35 -0
- package/build/esnext/utils/url.utils.js.map +1 -0
- package/build/esnext/utils/validate.utils.d.ts +60 -0
- package/build/esnext/utils/validate.utils.js +105 -0
- package/build/esnext/utils/validate.utils.js.map +1 -0
- package/build/src/config.d.ts +22 -0
- package/build/src/config.js +24 -0
- package/build/src/config.js.map +1 -0
- package/build/src/index.d.ts +19 -0
- package/build/src/index.js +35 -0
- package/build/src/index.js.map +1 -0
- package/build/src/types/api-response.d.ts +44 -0
- package/build/src/types/api-response.js +3 -0
- package/build/src/types/api-response.js.map +1 -0
- package/build/src/utils/array.utils.d.ts +105 -0
- package/build/src/utils/array.utils.js +216 -0
- package/build/src/utils/array.utils.js.map +1 -0
- package/build/src/utils/async.utils.d.ts +119 -0
- package/build/src/utils/async.utils.js +304 -0
- package/build/src/utils/async.utils.js.map +1 -0
- package/build/src/utils/cache.utils.d.ts +90 -0
- package/build/src/utils/cache.utils.js +146 -0
- package/build/src/utils/cache.utils.js.map +1 -0
- package/build/src/utils/context-store.utils.d.ts +85 -0
- package/build/src/utils/context-store.utils.js +100 -0
- package/build/src/utils/context-store.utils.js.map +1 -0
- package/build/src/utils/crypto.utils.d.ts +58 -0
- package/build/src/utils/crypto.utils.js +82 -0
- package/build/src/utils/crypto.utils.js.map +1 -0
- package/build/src/utils/dir.utils.d.ts +75 -0
- package/build/src/utils/dir.utils.js +164 -0
- package/build/src/utils/dir.utils.js.map +1 -0
- package/build/src/utils/env.utils.d.ts +131 -0
- package/build/src/utils/env.utils.js +215 -0
- package/build/src/utils/env.utils.js.map +1 -0
- package/build/src/utils/exception.utils.d.ts +114 -0
- package/build/src/utils/exception.utils.js +162 -0
- package/build/src/utils/exception.utils.js.map +1 -0
- package/build/src/utils/fs.utils.d.ts +32 -0
- package/build/src/utils/fs.utils.js +71 -0
- package/build/src/utils/fs.utils.js.map +1 -0
- package/build/src/utils/http-status-codes.d.ts +357 -0
- package/build/src/utils/http-status-codes.js +361 -0
- package/build/src/utils/http-status-codes.js.map +1 -0
- package/build/src/utils/id.utils.d.ts +35 -0
- package/build/src/utils/id.utils.js +60 -0
- package/build/src/utils/id.utils.js.map +1 -0
- package/build/src/utils/logger.utils.d.ts +15 -0
- package/build/src/utils/logger.utils.js +96 -0
- package/build/src/utils/logger.utils.js.map +1 -0
- package/build/src/utils/obj.utils.d.ts +55 -0
- package/build/src/utils/obj.utils.js +103 -0
- package/build/src/utils/obj.utils.js.map +1 -0
- package/build/src/utils/response.utils.d.ts +48 -0
- package/build/src/utils/response.utils.js +63 -0
- package/build/src/utils/response.utils.js.map +1 -0
- package/build/src/utils/string.utils.d.ts +37 -0
- package/build/src/utils/string.utils.js +54 -0
- package/build/src/utils/string.utils.js.map +1 -0
- package/build/src/utils/url.utils.d.ts +24 -0
- package/build/src/utils/url.utils.js +39 -0
- package/build/src/utils/url.utils.js.map +1 -0
- package/build/src/utils/validate.utils.d.ts +60 -0
- package/build/src/utils/validate.utils.js +115 -0
- package/build/src/utils/validate.utils.js.map +1 -0
- package/package.json +80 -0
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
2
|
+
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
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
4
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
5
|
+
function step(op) {
|
|
6
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
7
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
8
|
+
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;
|
|
9
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
10
|
+
switch (op[0]) {
|
|
11
|
+
case 0: case 1: t = op; break;
|
|
12
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
13
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
14
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
15
|
+
default:
|
|
16
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
17
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
18
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
19
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
20
|
+
if (t[2]) _.ops.pop();
|
|
21
|
+
_.trys.pop(); continue;
|
|
22
|
+
}
|
|
23
|
+
op = body.call(thisArg, _);
|
|
24
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
25
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
var __values = (this && this.__values) || function(o) {
|
|
29
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
30
|
+
if (m) return m.call(o);
|
|
31
|
+
if (o && typeof o.length === "number") return {
|
|
32
|
+
next: function () {
|
|
33
|
+
if (o && i >= o.length) o = void 0;
|
|
34
|
+
return { value: o && o[i++], done: !o };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
38
|
+
};
|
|
39
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
40
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
41
|
+
if (!m) return o;
|
|
42
|
+
var i = m.call(o), r, ar = [], e;
|
|
43
|
+
try {
|
|
44
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
45
|
+
}
|
|
46
|
+
catch (error) { e = { error: error }; }
|
|
47
|
+
finally {
|
|
48
|
+
try {
|
|
49
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
50
|
+
}
|
|
51
|
+
finally { if (e) throw e.error; }
|
|
52
|
+
}
|
|
53
|
+
return ar;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* An in-memory cache with time-to-live (TTL) support for each entry.
|
|
57
|
+
*
|
|
58
|
+
* @typeParam K - Type of the key.
|
|
59
|
+
* @typeParam V - Type of the value.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* const cache = new TTLCache<string, number>(1000);
|
|
63
|
+
* cache.set("x", 123);
|
|
64
|
+
* const value = cache.get("x"); // 123
|
|
65
|
+
*/
|
|
66
|
+
var TTLCache = /** @class */ (function () {
|
|
67
|
+
/**
|
|
68
|
+
* @param ttlMs - Default time-to-live in milliseconds for cache entries.
|
|
69
|
+
*/
|
|
70
|
+
function TTLCache(ttlMs) {
|
|
71
|
+
if (ttlMs === void 0) { ttlMs = 5 * 60 * 1000; }
|
|
72
|
+
this.ttlMs = ttlMs;
|
|
73
|
+
this.cache = new Map();
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Sets a key-value pair in the cache with the default TTL.
|
|
77
|
+
*
|
|
78
|
+
* @param key - The key to set.
|
|
79
|
+
* @param value - The value to associate with the key.
|
|
80
|
+
*/
|
|
81
|
+
TTLCache.prototype.set = function (key, value) {
|
|
82
|
+
var expiresAt = Date.now() + this.ttlMs;
|
|
83
|
+
this.cache.set(key, { value: value, expiresAt: expiresAt });
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Sets a key-value pair in the cache with a custom TTL.
|
|
87
|
+
*
|
|
88
|
+
* @param key - The key to set.
|
|
89
|
+
* @param value - The value to associate with the key.
|
|
90
|
+
* @param ttlMs - Time-to-live in milliseconds.
|
|
91
|
+
*/
|
|
92
|
+
TTLCache.prototype.setWithTTL = function (key, value, ttlMs) {
|
|
93
|
+
var expiresAt = Date.now() + ttlMs;
|
|
94
|
+
this.cache.set(key, { value: value, expiresAt: expiresAt });
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Retrieves the value for a given key if it hasn't expired.
|
|
98
|
+
*
|
|
99
|
+
* @param key - The key to retrieve.
|
|
100
|
+
* @returns The cached value, or undefined if not found or expired.
|
|
101
|
+
*/
|
|
102
|
+
TTLCache.prototype.get = function (key) {
|
|
103
|
+
var entry = this.cache.get(key);
|
|
104
|
+
if (!entry)
|
|
105
|
+
return undefined;
|
|
106
|
+
if (Date.now() > entry.expiresAt) {
|
|
107
|
+
this.cache.delete(key);
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
return entry.value;
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Checks if the key exists and hasn't expired.
|
|
114
|
+
*
|
|
115
|
+
* @param key - The key to check.
|
|
116
|
+
* @returns `true` if key exists and is valid, otherwise `false`.
|
|
117
|
+
*/
|
|
118
|
+
TTLCache.prototype.has = function (key) {
|
|
119
|
+
return this.get(key) !== undefined;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Deletes a key from the cache.
|
|
123
|
+
*
|
|
124
|
+
* @param key - The key to delete.
|
|
125
|
+
* @returns `true` if the key existed and was removed, `false` otherwise.
|
|
126
|
+
*/
|
|
127
|
+
TTLCache.prototype.delete = function (key) {
|
|
128
|
+
return this.cache.delete(key);
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* Clears all entries from the cache.
|
|
132
|
+
*/
|
|
133
|
+
TTLCache.prototype.clear = function () {
|
|
134
|
+
this.cache.clear();
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Returns the number of entries currently in the cache (includes expired entries until next access/cleanup).
|
|
138
|
+
*
|
|
139
|
+
* @returns Number of items in the cache (may include expired keys).
|
|
140
|
+
*/
|
|
141
|
+
TTLCache.prototype.size = function () {
|
|
142
|
+
return this.cache.size;
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* Removes all expired entries from the cache.
|
|
146
|
+
*
|
|
147
|
+
* @returns Number of entries removed.
|
|
148
|
+
*/
|
|
149
|
+
TTLCache.prototype.cleanup = function () {
|
|
150
|
+
var e_1, _a;
|
|
151
|
+
var removed = 0;
|
|
152
|
+
var now = Date.now();
|
|
153
|
+
try {
|
|
154
|
+
for (var _b = __values(this.cache.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
155
|
+
var _d = __read(_c.value, 2), key = _d[0], entry = _d[1];
|
|
156
|
+
if (now > entry.expiresAt) {
|
|
157
|
+
this.cache.delete(key);
|
|
158
|
+
removed++;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
163
|
+
finally {
|
|
164
|
+
try {
|
|
165
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
166
|
+
}
|
|
167
|
+
finally { if (e_1) throw e_1.error; }
|
|
168
|
+
}
|
|
169
|
+
return removed;
|
|
170
|
+
};
|
|
171
|
+
/**
|
|
172
|
+
* Returns an iterator of all current valid [key, value] pairs.
|
|
173
|
+
*
|
|
174
|
+
* @returns IterableIterator<[K, V]>
|
|
175
|
+
*/
|
|
176
|
+
TTLCache.prototype.entries = function () {
|
|
177
|
+
var _a, _b, _c, key, entry, e_2_1;
|
|
178
|
+
var e_2, _d;
|
|
179
|
+
return __generator(this, function (_e) {
|
|
180
|
+
switch (_e.label) {
|
|
181
|
+
case 0:
|
|
182
|
+
_e.trys.push([0, 5, 6, 7]);
|
|
183
|
+
_a = __values(this.cache.entries()), _b = _a.next();
|
|
184
|
+
_e.label = 1;
|
|
185
|
+
case 1:
|
|
186
|
+
if (!!_b.done) return [3 /*break*/, 4];
|
|
187
|
+
_c = __read(_b.value, 2), key = _c[0], entry = _c[1];
|
|
188
|
+
if (!(Date.now() <= entry.expiresAt)) return [3 /*break*/, 3];
|
|
189
|
+
return [4 /*yield*/, [key, entry.value]];
|
|
190
|
+
case 2:
|
|
191
|
+
_e.sent();
|
|
192
|
+
_e.label = 3;
|
|
193
|
+
case 3:
|
|
194
|
+
_b = _a.next();
|
|
195
|
+
return [3 /*break*/, 1];
|
|
196
|
+
case 4: return [3 /*break*/, 7];
|
|
197
|
+
case 5:
|
|
198
|
+
e_2_1 = _e.sent();
|
|
199
|
+
e_2 = { error: e_2_1 };
|
|
200
|
+
return [3 /*break*/, 7];
|
|
201
|
+
case 6:
|
|
202
|
+
try {
|
|
203
|
+
if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
|
|
204
|
+
}
|
|
205
|
+
finally { if (e_2) throw e_2.error; }
|
|
206
|
+
return [7 /*endfinally*/];
|
|
207
|
+
case 7: return [2 /*return*/];
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
};
|
|
211
|
+
/**
|
|
212
|
+
* Returns an iterator of all current valid keys.
|
|
213
|
+
*
|
|
214
|
+
* @returns IterableIterator<K>
|
|
215
|
+
*/
|
|
216
|
+
TTLCache.prototype.keys = function () {
|
|
217
|
+
var _a, _b, _c, key, entry, e_3_1;
|
|
218
|
+
var e_3, _d;
|
|
219
|
+
return __generator(this, function (_e) {
|
|
220
|
+
switch (_e.label) {
|
|
221
|
+
case 0:
|
|
222
|
+
_e.trys.push([0, 5, 6, 7]);
|
|
223
|
+
_a = __values(this.cache.entries()), _b = _a.next();
|
|
224
|
+
_e.label = 1;
|
|
225
|
+
case 1:
|
|
226
|
+
if (!!_b.done) return [3 /*break*/, 4];
|
|
227
|
+
_c = __read(_b.value, 2), key = _c[0], entry = _c[1];
|
|
228
|
+
if (!(Date.now() <= entry.expiresAt)) return [3 /*break*/, 3];
|
|
229
|
+
return [4 /*yield*/, key];
|
|
230
|
+
case 2:
|
|
231
|
+
_e.sent();
|
|
232
|
+
_e.label = 3;
|
|
233
|
+
case 3:
|
|
234
|
+
_b = _a.next();
|
|
235
|
+
return [3 /*break*/, 1];
|
|
236
|
+
case 4: return [3 /*break*/, 7];
|
|
237
|
+
case 5:
|
|
238
|
+
e_3_1 = _e.sent();
|
|
239
|
+
e_3 = { error: e_3_1 };
|
|
240
|
+
return [3 /*break*/, 7];
|
|
241
|
+
case 6:
|
|
242
|
+
try {
|
|
243
|
+
if (_b && !_b.done && (_d = _a.return)) _d.call(_a);
|
|
244
|
+
}
|
|
245
|
+
finally { if (e_3) throw e_3.error; }
|
|
246
|
+
return [7 /*endfinally*/];
|
|
247
|
+
case 7: return [2 /*return*/];
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
};
|
|
251
|
+
/**
|
|
252
|
+
* Returns an iterator of all current valid values.
|
|
253
|
+
*
|
|
254
|
+
* @returns IterableIterator<V>
|
|
255
|
+
*/
|
|
256
|
+
TTLCache.prototype.values = function () {
|
|
257
|
+
var _a, _b, entry, e_4_1;
|
|
258
|
+
var e_4, _c;
|
|
259
|
+
return __generator(this, function (_d) {
|
|
260
|
+
switch (_d.label) {
|
|
261
|
+
case 0:
|
|
262
|
+
_d.trys.push([0, 5, 6, 7]);
|
|
263
|
+
_a = __values(this.cache.values()), _b = _a.next();
|
|
264
|
+
_d.label = 1;
|
|
265
|
+
case 1:
|
|
266
|
+
if (!!_b.done) return [3 /*break*/, 4];
|
|
267
|
+
entry = _b.value;
|
|
268
|
+
if (!(Date.now() <= entry.expiresAt)) return [3 /*break*/, 3];
|
|
269
|
+
return [4 /*yield*/, entry.value];
|
|
270
|
+
case 2:
|
|
271
|
+
_d.sent();
|
|
272
|
+
_d.label = 3;
|
|
273
|
+
case 3:
|
|
274
|
+
_b = _a.next();
|
|
275
|
+
return [3 /*break*/, 1];
|
|
276
|
+
case 4: return [3 /*break*/, 7];
|
|
277
|
+
case 5:
|
|
278
|
+
e_4_1 = _d.sent();
|
|
279
|
+
e_4 = { error: e_4_1 };
|
|
280
|
+
return [3 /*break*/, 7];
|
|
281
|
+
case 6:
|
|
282
|
+
try {
|
|
283
|
+
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
|
|
284
|
+
}
|
|
285
|
+
finally { if (e_4) throw e_4.error; }
|
|
286
|
+
return [7 /*endfinally*/];
|
|
287
|
+
case 7: return [2 /*return*/];
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
};
|
|
291
|
+
return TTLCache;
|
|
292
|
+
}());
|
|
293
|
+
export { TTLCache };
|
|
294
|
+
//# sourceMappingURL=cache.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.utils.js","sourceRoot":"","sources":["../../../src/utils/cache.utils.ts"],"names":[],"mappings":"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","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"]}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
2
|
+
/**
|
|
3
|
+
* Type representing the store object used in AsyncLocalStorage.
|
|
4
|
+
* Keys must be unique symbols to ensure isolation and prevent collisions.
|
|
5
|
+
*/
|
|
6
|
+
export interface Store {
|
|
7
|
+
[key: symbol]: unknown;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Predefined symbols used as keys in AsyncLocalStorage.
|
|
11
|
+
* Add new symbols here to avoid duplication.
|
|
12
|
+
*/
|
|
13
|
+
export declare const StoreKeys: {
|
|
14
|
+
LOGGER: symbol;
|
|
15
|
+
REQUEST_ID: symbol;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Retrieves the current request ID from the async context, if available.
|
|
19
|
+
*
|
|
20
|
+
* @returns {string | undefined} The request ID string or undefined if not present in the current context.
|
|
21
|
+
*/
|
|
22
|
+
export declare function getRequestId(): string | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* ContextStore manages per-request scoped context using AsyncLocalStorage.
|
|
25
|
+
* It allows storing and retrieving data across async calls (e.g., request ID, logger).
|
|
26
|
+
*
|
|
27
|
+
* Example (Express middleware):
|
|
28
|
+
* ```
|
|
29
|
+
* // Middleware to initialize context and logger
|
|
30
|
+
* app.use((req, res, next) => {
|
|
31
|
+
* const requestId = req.headers["x-request-id"]?.toString() || crypto.randomUUID();
|
|
32
|
+
* ContextStore.run({ [StoreKeys.REQUEST_ID]: requestId }, () => {
|
|
33
|
+
* ContextStore.set(StoreKeys.LOGGER, getLogger().child({ reqId: requestId }));
|
|
34
|
+
* next();
|
|
35
|
+
* });
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare class ContextStore {
|
|
40
|
+
/**
|
|
41
|
+
* The underlying AsyncLocalStorage instance for context.
|
|
42
|
+
* @private
|
|
43
|
+
*/
|
|
44
|
+
private static readonly storage;
|
|
45
|
+
/**
|
|
46
|
+
* Returns the raw AsyncLocalStorage instance for advanced access.
|
|
47
|
+
*
|
|
48
|
+
* @returns {AsyncLocalStorage<Store>} The AsyncLocalStorage instance.
|
|
49
|
+
*/
|
|
50
|
+
static getInstance(): AsyncLocalStorage<Store>;
|
|
51
|
+
/**
|
|
52
|
+
* Retrieves a value from the async context store by symbol key.
|
|
53
|
+
*
|
|
54
|
+
* @typeParam T - The expected return type of the value.
|
|
55
|
+
* @param {symbol} key - Unique symbol used as the store key.
|
|
56
|
+
* @returns {T | undefined} The value found (typed) or undefined if not present.
|
|
57
|
+
*/
|
|
58
|
+
static get<T>(key: symbol): T | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Sets a value in the current context store by symbol key.
|
|
61
|
+
*
|
|
62
|
+
* @typeParam T - The value type to set.
|
|
63
|
+
* @param {symbol} key - Unique symbol key.
|
|
64
|
+
* @param {T} value - Value to set in context.
|
|
65
|
+
* @throws {Error} If called outside an active context (not within a .run call or in the wrong async boundaries).
|
|
66
|
+
*/
|
|
67
|
+
static set<T>(key: symbol, value: T): void;
|
|
68
|
+
/**
|
|
69
|
+
* Retrieves the entire context store object for the current async context.
|
|
70
|
+
*
|
|
71
|
+
* @returns {Store | undefined} The current store object or undefined if called outside a context.
|
|
72
|
+
*/
|
|
73
|
+
static getAll(): Store | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* Initializes a new async context and executes a callback within it.
|
|
76
|
+
* This must be called at the beginning of a request or logical async flow.
|
|
77
|
+
*
|
|
78
|
+
* @typeParam T - The callback's return type.
|
|
79
|
+
* @param {Store} store - The initial key-value store object.
|
|
80
|
+
* @param {() => T} callback - The function to run within the new context.
|
|
81
|
+
* @returns {T} The result of the callback function.
|
|
82
|
+
*/
|
|
83
|
+
static run<T>(store: Store, callback: () => T): T;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=context-store.utils.d.ts.map
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
2
|
+
/**
|
|
3
|
+
* Predefined symbols used as keys in AsyncLocalStorage.
|
|
4
|
+
* Add new symbols here to avoid duplication.
|
|
5
|
+
*/
|
|
6
|
+
export var StoreKeys = {
|
|
7
|
+
LOGGER: Symbol("logger"),
|
|
8
|
+
REQUEST_ID: Symbol("requestId"),
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Retrieves the current request ID from the async context, if available.
|
|
12
|
+
*
|
|
13
|
+
* @returns {string | undefined} The request ID string or undefined if not present in the current context.
|
|
14
|
+
*/
|
|
15
|
+
export function getRequestId() {
|
|
16
|
+
return ContextStore.get(StoreKeys.REQUEST_ID);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* ContextStore manages per-request scoped context using AsyncLocalStorage.
|
|
20
|
+
* It allows storing and retrieving data across async calls (e.g., request ID, logger).
|
|
21
|
+
*
|
|
22
|
+
* Example (Express middleware):
|
|
23
|
+
* ```
|
|
24
|
+
* // Middleware to initialize context and logger
|
|
25
|
+
* app.use((req, res, next) => {
|
|
26
|
+
* const requestId = req.headers["x-request-id"]?.toString() || crypto.randomUUID();
|
|
27
|
+
* ContextStore.run({ [StoreKeys.REQUEST_ID]: requestId }, () => {
|
|
28
|
+
* ContextStore.set(StoreKeys.LOGGER, getLogger().child({ reqId: requestId }));
|
|
29
|
+
* next();
|
|
30
|
+
* });
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
var ContextStore = /** @class */ (function () {
|
|
35
|
+
function ContextStore() {
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Returns the raw AsyncLocalStorage instance for advanced access.
|
|
39
|
+
*
|
|
40
|
+
* @returns {AsyncLocalStorage<Store>} The AsyncLocalStorage instance.
|
|
41
|
+
*/
|
|
42
|
+
ContextStore.getInstance = function () {
|
|
43
|
+
return this.storage;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Retrieves a value from the async context store by symbol key.
|
|
47
|
+
*
|
|
48
|
+
* @typeParam T - The expected return type of the value.
|
|
49
|
+
* @param {symbol} key - Unique symbol used as the store key.
|
|
50
|
+
* @returns {T | undefined} The value found (typed) or undefined if not present.
|
|
51
|
+
*/
|
|
52
|
+
ContextStore.get = function (key) {
|
|
53
|
+
var store = this.storage.getStore();
|
|
54
|
+
return store === null || store === void 0 ? void 0 : store[key];
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Sets a value in the current context store by symbol key.
|
|
58
|
+
*
|
|
59
|
+
* @typeParam T - The value type to set.
|
|
60
|
+
* @param {symbol} key - Unique symbol key.
|
|
61
|
+
* @param {T} value - Value to set in context.
|
|
62
|
+
* @throws {Error} If called outside an active context (not within a .run call or in the wrong async boundaries).
|
|
63
|
+
*/
|
|
64
|
+
ContextStore.set = function (key, value) {
|
|
65
|
+
var store = this.storage.getStore();
|
|
66
|
+
if (!store) {
|
|
67
|
+
throw new Error("Failed to set ".concat(String(key), ": AsyncLocalStorage store is not initialized."));
|
|
68
|
+
}
|
|
69
|
+
store[key] = value;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Retrieves the entire context store object for the current async context.
|
|
73
|
+
*
|
|
74
|
+
* @returns {Store | undefined} The current store object or undefined if called outside a context.
|
|
75
|
+
*/
|
|
76
|
+
ContextStore.getAll = function () {
|
|
77
|
+
return this.storage.getStore();
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Initializes a new async context and executes a callback within it.
|
|
81
|
+
* This must be called at the beginning of a request or logical async flow.
|
|
82
|
+
*
|
|
83
|
+
* @typeParam T - The callback's return type.
|
|
84
|
+
* @param {Store} store - The initial key-value store object.
|
|
85
|
+
* @param {() => T} callback - The function to run within the new context.
|
|
86
|
+
* @returns {T} The result of the callback function.
|
|
87
|
+
*/
|
|
88
|
+
ContextStore.run = function (store, callback) {
|
|
89
|
+
return this.storage.run(store, callback);
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* The underlying AsyncLocalStorage instance for context.
|
|
93
|
+
* @private
|
|
94
|
+
*/
|
|
95
|
+
ContextStore.storage = new AsyncLocalStorage();
|
|
96
|
+
return ContextStore;
|
|
97
|
+
}());
|
|
98
|
+
export { ContextStore };
|
|
99
|
+
//# sourceMappingURL=context-store.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-store.utils.js","sourceRoot":"","sources":["../../../src/utils/context-store.utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAUhD;;;GAGG;AACH,MAAM,CAAC,IAAM,SAAS,GAAG;IACvB,MAAM,EAAE,MAAM,CAAC,QAAQ,CAAC;IACxB,UAAU,EAAE,MAAM,CAAC,WAAW,CAAC;CAChC,CAAC;AAEF;;;;GAIG;AACH,MAAM,UAAU,YAAY;IAC1B,OAAO,YAAY,CAAC,GAAG,CAAS,SAAS,CAAC,UAAU,CAAC,CAAC;AACxD,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH;IAAA;IAmEA,CAAC;IA5DC;;;;OAIG;IACI,wBAAW,GAAlB;QACE,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED;;;;;;OAMG;IACI,gBAAG,GAAV,UAAc,GAAW;QACvB,IAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;QACtC,OAAO,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAG,GAAG,CAAkB,CAAC;IACvC,CAAC;IAED;;;;;;;OAOG;IACI,gBAAG,GAAV,UAAc,GAAW,EAAE,KAAQ;QACjC,IAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;QACtC,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,MAAM,IAAI,KAAK,CACb,wBAAiB,MAAM,CAAC,GAAG,CAAC,kDAA+C,CAC5E,CAAC;QACJ,CAAC;QACD,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACI,mBAAM,GAAb;QACE,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;IACjC,CAAC;IAED;;;;;;;;OAQG;IACI,gBAAG,GAAV,UAAc,KAAY,EAAE,QAAiB;QAC3C,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IAC3C,CAAC;IAjED;;;OAGG;IACqB,oBAAO,GAAG,IAAI,iBAAiB,EAAS,CAAC;IA8DnE,mBAAC;CAAA,AAnED,IAmEC;SAnEY,YAAY","sourcesContent":["import { AsyncLocalStorage } from \"async_hooks\";\n\n/**\n * Type representing the store object used in AsyncLocalStorage.\n * Keys must be unique symbols to ensure isolation and prevent collisions.\n */\nexport interface Store {\n [key: symbol]: unknown;\n}\n\n/**\n * Predefined symbols used as keys in AsyncLocalStorage.\n * Add new symbols here to avoid duplication.\n */\nexport const StoreKeys = {\n LOGGER: Symbol(\"logger\"),\n REQUEST_ID: Symbol(\"requestId\"),\n};\n\n/**\n * Retrieves the current request ID from the async context, if available.\n *\n * @returns {string | undefined} The request ID string or undefined if not present in the current context.\n */\nexport function getRequestId(): string | undefined {\n return ContextStore.get<string>(StoreKeys.REQUEST_ID);\n}\n\n/**\n * ContextStore manages per-request scoped context using AsyncLocalStorage.\n * It allows storing and retrieving data across async calls (e.g., request ID, logger).\n *\n * Example (Express middleware):\n * ```\n * // Middleware to initialize context and logger\n * app.use((req, res, next) => {\n * const requestId = req.headers[\"x-request-id\"]?.toString() || crypto.randomUUID();\n * ContextStore.run({ [StoreKeys.REQUEST_ID]: requestId }, () => {\n * ContextStore.set(StoreKeys.LOGGER, getLogger().child({ reqId: requestId }));\n * next();\n * });\n * });\n * ```\n */\nexport class ContextStore {\n /**\n * The underlying AsyncLocalStorage instance for context.\n * @private\n */\n private static readonly storage = new AsyncLocalStorage<Store>();\n\n /**\n * Returns the raw AsyncLocalStorage instance for advanced access.\n *\n * @returns {AsyncLocalStorage<Store>} The AsyncLocalStorage instance.\n */\n static getInstance(): AsyncLocalStorage<Store> {\n return this.storage;\n }\n\n /**\n * Retrieves a value from the async context store by symbol key.\n *\n * @typeParam T - The expected return type of the value.\n * @param {symbol} key - Unique symbol used as the store key.\n * @returns {T | undefined} The value found (typed) or undefined if not present.\n */\n static get<T>(key: symbol): T | undefined {\n const store = this.storage.getStore();\n return store?.[key] as T | undefined;\n }\n\n /**\n * Sets a value in the current context store by symbol key.\n *\n * @typeParam T - The value type to set.\n * @param {symbol} key - Unique symbol key.\n * @param {T} value - Value to set in context.\n * @throws {Error} If called outside an active context (not within a .run call or in the wrong async boundaries).\n */\n static set<T>(key: symbol, value: T): void {\n const store = this.storage.getStore();\n if (!store) {\n throw new Error(\n `Failed to set ${String(key)}: AsyncLocalStorage store is not initialized.`,\n );\n }\n store[key] = value;\n }\n\n /**\n * Retrieves the entire context store object for the current async context.\n *\n * @returns {Store | undefined} The current store object or undefined if called outside a context.\n */\n static getAll(): Store | undefined {\n return this.storage.getStore();\n }\n\n /**\n * Initializes a new async context and executes a callback within it.\n * This must be called at the beginning of a request or logical async flow.\n *\n * @typeParam T - The callback's return type.\n * @param {Store} store - The initial key-value store object.\n * @param {() => T} callback - The function to run within the new context.\n * @returns {T} The result of the callback function.\n */\n static run<T>(store: Store, callback: () => T): T {\n return this.storage.run(store, callback);\n }\n}\n"]}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { BinaryToTextEncoding } from "crypto";
|
|
2
|
+
/**
|
|
3
|
+
* Generates an HMAC digest using the specified algorithm and secret key.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} algorithm - The hashing algorithm (e.g., 'sha256', 'sha1').
|
|
6
|
+
* @param {string} input - The string to hash.
|
|
7
|
+
* @param {string} secret - The secret key for HMAC.
|
|
8
|
+
* @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding ('hex', 'base64', etc).
|
|
9
|
+
* @returns {string} HMAC digest as a string.
|
|
10
|
+
*/
|
|
11
|
+
export declare const hmac: (algorithm: string, input: string, secret: string, encoding?: BinaryToTextEncoding) => string;
|
|
12
|
+
/**
|
|
13
|
+
* Generates a hash digest using the specified algorithm.
|
|
14
|
+
*
|
|
15
|
+
* @param {string} algorithm - The hashing algorithm (e.g., 'sha256', 'md5').
|
|
16
|
+
* @param {string} input - The string to hash.
|
|
17
|
+
* @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding ('hex', 'base64', etc).
|
|
18
|
+
* @returns {string} Hash digest as a string.
|
|
19
|
+
*/
|
|
20
|
+
export declare const hash: (algorithm: string, input: string, encoding?: BinaryToTextEncoding) => string;
|
|
21
|
+
/**
|
|
22
|
+
* Generates an HMAC-SHA256 digest.
|
|
23
|
+
*
|
|
24
|
+
* @param {string} input - The string to hash.
|
|
25
|
+
* @param {string} secret - The secret key.
|
|
26
|
+
* @returns {string} SHA-256 HMAC digest as a string.
|
|
27
|
+
*/
|
|
28
|
+
export declare const sha256Hmac: (input: string, secret: string) => string;
|
|
29
|
+
/**
|
|
30
|
+
* Generates a SHA1 hash digest.
|
|
31
|
+
*
|
|
32
|
+
* @param {string} input - The string to hash.
|
|
33
|
+
* @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding.
|
|
34
|
+
* @returns {string} SHA-1 hash as a string.
|
|
35
|
+
*/
|
|
36
|
+
export declare const sha1: (input: string, encoding?: BinaryToTextEncoding) => string;
|
|
37
|
+
/**
|
|
38
|
+
* Generates a SHA256 hash digest.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} input - The string to hash.
|
|
41
|
+
* @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding.
|
|
42
|
+
* @returns {string} SHA-256 hash as a string.
|
|
43
|
+
*/
|
|
44
|
+
export declare const sha256: (input: string, encoding?: BinaryToTextEncoding) => string;
|
|
45
|
+
/**
|
|
46
|
+
* Generates an MD5 hash digest.
|
|
47
|
+
*
|
|
48
|
+
* @param {string} input - The string to hash.
|
|
49
|
+
* @returns {string} MD5 hash as a string.
|
|
50
|
+
*/
|
|
51
|
+
export declare const md5: (input: string) => string;
|
|
52
|
+
/**
|
|
53
|
+
* Generates a cryptographically strong random string by hashing a random UUID with SHA-256.
|
|
54
|
+
*
|
|
55
|
+
* @returns {string} Random string hashed with SHA-256 (hex encoding).
|
|
56
|
+
*/
|
|
57
|
+
export declare const randomString: () => string;
|
|
58
|
+
//# sourceMappingURL=crypto.utils.d.ts.map
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { createHmac, createHash, randomUUID, } from "crypto";
|
|
2
|
+
/**
|
|
3
|
+
* Generates an HMAC digest using the specified algorithm and secret key.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} algorithm - The hashing algorithm (e.g., 'sha256', 'sha1').
|
|
6
|
+
* @param {string} input - The string to hash.
|
|
7
|
+
* @param {string} secret - The secret key for HMAC.
|
|
8
|
+
* @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding ('hex', 'base64', etc).
|
|
9
|
+
* @returns {string} HMAC digest as a string.
|
|
10
|
+
*/
|
|
11
|
+
export var hmac = function (algorithm, input, secret, encoding) {
|
|
12
|
+
if (encoding === void 0) { encoding = "hex"; }
|
|
13
|
+
return createHmac(algorithm, secret).update(input).digest(encoding);
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Generates a hash digest using the specified algorithm.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} algorithm - The hashing algorithm (e.g., 'sha256', 'md5').
|
|
19
|
+
* @param {string} input - The string to hash.
|
|
20
|
+
* @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding ('hex', 'base64', etc).
|
|
21
|
+
* @returns {string} Hash digest as a string.
|
|
22
|
+
*/
|
|
23
|
+
export var hash = function (algorithm, input, encoding) {
|
|
24
|
+
if (encoding === void 0) { encoding = "hex"; }
|
|
25
|
+
return createHash(algorithm).update(input).digest(encoding);
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Generates an HMAC-SHA256 digest.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} input - The string to hash.
|
|
31
|
+
* @param {string} secret - The secret key.
|
|
32
|
+
* @returns {string} SHA-256 HMAC digest as a string.
|
|
33
|
+
*/
|
|
34
|
+
export var sha256Hmac = function (input, secret) {
|
|
35
|
+
return hmac("sha256", input, secret);
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Generates a SHA1 hash digest.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} input - The string to hash.
|
|
41
|
+
* @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding.
|
|
42
|
+
* @returns {string} SHA-1 hash as a string.
|
|
43
|
+
*/
|
|
44
|
+
export var sha1 = function (input, encoding) {
|
|
45
|
+
if (encoding === void 0) { encoding = "hex"; }
|
|
46
|
+
return hash("sha1", input, encoding);
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Generates a SHA256 hash digest.
|
|
50
|
+
*
|
|
51
|
+
* @param {string} input - The string to hash.
|
|
52
|
+
* @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding.
|
|
53
|
+
* @returns {string} SHA-256 hash as a string.
|
|
54
|
+
*/
|
|
55
|
+
export var sha256 = function (input, encoding) {
|
|
56
|
+
if (encoding === void 0) { encoding = "hex"; }
|
|
57
|
+
return hash("sha256", input, encoding);
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Generates an MD5 hash digest.
|
|
61
|
+
*
|
|
62
|
+
* @param {string} input - The string to hash.
|
|
63
|
+
* @returns {string} MD5 hash as a string.
|
|
64
|
+
*/
|
|
65
|
+
export var md5 = function (input) {
|
|
66
|
+
return hash("md5", input);
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Generates a cryptographically strong random string by hashing a random UUID with SHA-256.
|
|
70
|
+
*
|
|
71
|
+
* @returns {string} Random string hashed with SHA-256 (hex encoding).
|
|
72
|
+
*/
|
|
73
|
+
export var randomString = function () {
|
|
74
|
+
return sha256(randomUUID());
|
|
75
|
+
};
|
|
76
|
+
//# sourceMappingURL=crypto.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.utils.js","sourceRoot":"","sources":["../../../src/utils/crypto.utils.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,UAAU,EAEV,UAAU,GACX,MAAM,QAAQ,CAAC;AAEhB;;;;;;;;GAQG;AACH,MAAM,CAAC,IAAM,IAAI,GAAG,UAClB,SAAiB,EACjB,KAAa,EACb,MAAc,EACd,QAAsC;IAAtC,yBAAA,EAAA,gBAAsC;IAEtC,OAAO,UAAU,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AACtE,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,CAAC,IAAM,IAAI,GAAG,UAClB,SAAiB,EACjB,KAAa,EACb,QAAsC;IAAtC,yBAAA,EAAA,gBAAsC;IAEtC,OAAO,UAAU,CAAC,SAAS,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAC9D,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,IAAM,UAAU,GAAG,UAAC,KAAa,EAAE,MAAc;IACtD,OAAO,IAAI,CAAC,QAAQ,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;AACvC,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,IAAM,IAAI,GAAG,UAClB,KAAa,EACb,QAAsC;IAAtC,yBAAA,EAAA,gBAAsC;IAEtC,OAAO,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC;AACvC,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,IAAM,MAAM,GAAG,UACpB,KAAa,EACb,QAAsC;IAAtC,yBAAA,EAAA,gBAAsC;IAEtC,OAAO,IAAI,CAAC,QAAQ,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC;AACzC,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,IAAM,GAAG,GAAG,UAAC,KAAa;IAC/B,OAAO,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;AAC5B,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,IAAM,YAAY,GAAG;IAC1B,OAAO,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC;AAC9B,CAAC,CAAC","sourcesContent":["import {\n createHmac,\n createHash,\n BinaryToTextEncoding,\n randomUUID,\n} from \"crypto\";\n\n/**\n * Generates an HMAC digest using the specified algorithm and secret key.\n *\n * @param {string} algorithm - The hashing algorithm (e.g., 'sha256', 'sha1').\n * @param {string} input - The string to hash.\n * @param {string} secret - The secret key for HMAC.\n * @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding ('hex', 'base64', etc).\n * @returns {string} HMAC digest as a string.\n */\nexport const hmac = (\n algorithm: string,\n input: string,\n secret: string,\n encoding: BinaryToTextEncoding = \"hex\",\n): string => {\n return createHmac(algorithm, secret).update(input).digest(encoding);\n};\n\n/**\n * Generates a hash digest using the specified algorithm.\n *\n * @param {string} algorithm - The hashing algorithm (e.g., 'sha256', 'md5').\n * @param {string} input - The string to hash.\n * @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding ('hex', 'base64', etc).\n * @returns {string} Hash digest as a string.\n */\nexport const hash = (\n algorithm: string,\n input: string,\n encoding: BinaryToTextEncoding = \"hex\",\n): string => {\n return createHash(algorithm).update(input).digest(encoding);\n};\n\n/**\n * Generates an HMAC-SHA256 digest.\n *\n * @param {string} input - The string to hash.\n * @param {string} secret - The secret key.\n * @returns {string} SHA-256 HMAC digest as a string.\n */\nexport const sha256Hmac = (input: string, secret: string): string => {\n return hmac(\"sha256\", input, secret);\n};\n\n/**\n * Generates a SHA1 hash digest.\n *\n * @param {string} input - The string to hash.\n * @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding.\n * @returns {string} SHA-1 hash as a string.\n */\nexport const sha1 = (\n input: string,\n encoding: BinaryToTextEncoding = \"hex\",\n): string => {\n return hash(\"sha1\", input, encoding);\n};\n\n/**\n * Generates a SHA256 hash digest.\n *\n * @param {string} input - The string to hash.\n * @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding.\n * @returns {string} SHA-256 hash as a string.\n */\nexport const sha256 = (\n input: string,\n encoding: BinaryToTextEncoding = \"hex\",\n): string => {\n return hash(\"sha256\", input, encoding);\n};\n\n/**\n * Generates an MD5 hash digest.\n *\n * @param {string} input - The string to hash.\n * @returns {string} MD5 hash as a string.\n */\nexport const md5 = (input: string): string => {\n return hash(\"md5\", input);\n};\n\n/**\n * Generates a cryptographically strong random string by hashing a random UUID with SHA-256.\n *\n * @returns {string} Random string hashed with SHA-256 (hex encoding).\n */\nexport const randomString = (): string => {\n return sha256(randomUUID());\n};\n"]}
|