@catbee/utils 0.0.8-rc.2 → 0.0.8-rc.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/build/cjs/config.d.ts +121 -0
- package/build/cjs/config.js +137 -0
- package/build/cjs/index.d.ts +26 -0
- package/build/cjs/index.js +68 -0
- package/build/cjs/servers/server.builder.d.ts +507 -0
- package/build/cjs/servers/server.builder.js +658 -0
- package/build/cjs/servers/server.d.ts +255 -0
- package/build/cjs/servers/server.js +970 -0
- package/build/cjs/types/api-response.d.ts +151 -0
- package/build/cjs/types/api-response.js +36 -0
- package/build/cjs/types/index.d.ts +124 -0
- package/build/cjs/types/index.js +25 -0
- package/build/cjs/types/server.d.ts +267 -0
- package/build/cjs/types/server.js +25 -0
- package/build/cjs/utils/array.utils.d.ts +167 -0
- package/build/cjs/utils/array.utils.js +363 -0
- package/build/cjs/utils/async.utils.d.ts +264 -0
- package/build/cjs/utils/async.utils.js +640 -0
- package/build/cjs/utils/cache.utils.d.ts +152 -0
- package/build/cjs/utils/cache.utils.js +298 -0
- package/build/cjs/utils/context-store.utils.d.ts +188 -0
- package/build/cjs/utils/context-store.utils.js +297 -0
- package/build/cjs/utils/crypto.utils.d.ts +159 -0
- package/build/cjs/utils/crypto.utils.js +295 -0
- package/build/cjs/utils/date.utils.d.ts +158 -0
- package/build/cjs/utils/date.utils.js +395 -0
- package/build/cjs/utils/decorators.utils.d.ts +511 -0
- package/build/cjs/utils/decorators.utils.js +1035 -0
- package/build/cjs/utils/dir.utils.d.ts +195 -0
- package/build/cjs/utils/dir.utils.js +500 -0
- package/build/cjs/utils/env.utils.d.ts +376 -0
- package/build/cjs/utils/env.utils.js +786 -0
- package/build/cjs/utils/exception.utils.d.ts +229 -0
- package/build/cjs/utils/exception.utils.js +408 -0
- package/build/cjs/utils/fs.utils.d.ts +163 -0
- package/build/cjs/utils/fs.utils.js +371 -0
- package/build/cjs/utils/http-status-codes.d.ts +265 -0
- package/build/cjs/utils/http-status-codes.js +297 -0
- package/build/cjs/utils/id.utils.d.ts +35 -0
- package/build/cjs/utils/id.utils.js +90 -0
- package/build/cjs/utils/logger.utils.d.ts +159 -0
- package/build/cjs/utils/logger.utils.js +350 -0
- package/build/cjs/utils/middleware.utils.d.ts +99 -0
- package/build/cjs/utils/middleware.utils.js +243 -0
- package/build/cjs/utils/obj.utils.d.ts +123 -0
- package/build/cjs/utils/obj.utils.js +428 -0
- package/build/cjs/utils/performance.utils.d.ts +135 -0
- package/build/cjs/utils/performance.utils.js +280 -0
- package/build/cjs/utils/request.utils.d.ts +85 -0
- package/build/cjs/utils/request.utils.js +199 -0
- package/build/cjs/utils/response.utils.d.ts +162 -0
- package/build/cjs/utils/response.utils.js +274 -0
- package/build/cjs/utils/stream.utils.d.ts +87 -0
- package/build/cjs/utils/stream.utils.js +217 -0
- package/build/cjs/utils/string.utils.d.ts +92 -0
- package/build/cjs/utils/string.utils.js +178 -0
- package/build/cjs/utils/type.utils.d.ts +89 -0
- package/build/cjs/utils/type.utils.js +195 -0
- package/build/cjs/utils/url.utils.d.ts +140 -0
- package/build/cjs/utils/url.utils.js +316 -0
- package/build/cjs/utils/validate.utils.d.ts +176 -0
- package/build/cjs/utils/validate.utils.js +344 -0
- package/build/esm/utils/context-store.utils.d.ts +1 -1
- package/build/esm/utils/context-store.utils.js +1 -1
- package/build/esm/utils/decorators.utils.js +1 -1
- package/package.json +287 -32
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* The MIT License
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license
|
|
6
|
+
*
|
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
* in the Software without restriction, including without limitation the rights
|
|
10
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
* furnished to do so, subject to the following conditions:
|
|
13
|
+
*
|
|
14
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
* copies or substantial portions of the Software.
|
|
16
|
+
*
|
|
17
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
* SOFTWARE.
|
|
24
|
+
*/
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.TypedContextKey = exports.ContextStore = exports.StoreKeys = void 0;
|
|
27
|
+
exports.getRequestId = getRequestId;
|
|
28
|
+
exports.getFromContext = getFromContext;
|
|
29
|
+
const async_hooks_1 = require("async_hooks");
|
|
30
|
+
/**
|
|
31
|
+
* Predefined symbols used as keys in AsyncLocalStorage.
|
|
32
|
+
* Add new symbols here to avoid duplication.
|
|
33
|
+
*/
|
|
34
|
+
exports.StoreKeys = {
|
|
35
|
+
LOGGER: Symbol('LOGGER'),
|
|
36
|
+
REQUEST_ID: Symbol('REQUEST_ID'),
|
|
37
|
+
USER: Symbol('USER'),
|
|
38
|
+
SESSION: Symbol('SESSION'),
|
|
39
|
+
TRANSACTION_ID: Symbol('TRANSACTION_ID'),
|
|
40
|
+
USER_ID: Symbol('USER_ID'),
|
|
41
|
+
TENANT_ID: Symbol('TENANT_ID'),
|
|
42
|
+
TRACE_ID: Symbol('TRACE_ID'),
|
|
43
|
+
CORRELATION_ID: Symbol('CORRELATION_ID')
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Retrieves the current request ID from the async context, if available.
|
|
47
|
+
*
|
|
48
|
+
* @returns {string | undefined} The request ID string or undefined if not present in the current context.
|
|
49
|
+
*/
|
|
50
|
+
function getRequestId() {
|
|
51
|
+
return ContextStore.get(exports.StoreKeys.REQUEST_ID);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Type-safe getter for common context values.
|
|
55
|
+
*
|
|
56
|
+
* @param key The store key symbol
|
|
57
|
+
* @returns The typed value from the store
|
|
58
|
+
*/
|
|
59
|
+
function getFromContext(key) {
|
|
60
|
+
return ContextStore.get(key);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* ContextStore manages per-request scoped context using AsyncLocalStorage.
|
|
64
|
+
* It allows storing and retrieving data across async calls (e.g., request ID, logger).
|
|
65
|
+
*
|
|
66
|
+
* Example (Express middleware):
|
|
67
|
+
* ```
|
|
68
|
+
* // Middleware to initialize context and logger
|
|
69
|
+
* app.use((req, res, next) => {
|
|
70
|
+
* const requestId = req.headers["x-request-id"]?.toString() || crypto.randomUUID();
|
|
71
|
+
* ContextStore.run({ [StoreKeys.REQUEST_ID]: requestId }, () => {
|
|
72
|
+
* ContextStore.set(StoreKeys.LOGGER, getLogger().child({ requestId }));
|
|
73
|
+
* next();
|
|
74
|
+
* });
|
|
75
|
+
* });
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
class ContextStore {
|
|
79
|
+
/**
|
|
80
|
+
* The underlying AsyncLocalStorage instance for context.
|
|
81
|
+
* @private
|
|
82
|
+
*/
|
|
83
|
+
static storage = new async_hooks_1.AsyncLocalStorage();
|
|
84
|
+
/**
|
|
85
|
+
* Returns the raw AsyncLocalStorage instance for advanced access.
|
|
86
|
+
*
|
|
87
|
+
* @returns {AsyncLocalStorage<Store>} The AsyncLocalStorage instance.
|
|
88
|
+
*/
|
|
89
|
+
static getInstance() {
|
|
90
|
+
return this.storage;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Retrieves a value from the async context store by symbol key.
|
|
94
|
+
*
|
|
95
|
+
* @typeParam T - The expected return type of the value.
|
|
96
|
+
* @param {symbol} key - Unique symbol used as the store key.
|
|
97
|
+
* @returns {T | undefined} The value found (typed) or undefined if not present.
|
|
98
|
+
*/
|
|
99
|
+
static get(key) {
|
|
100
|
+
const store = this.storage.getStore();
|
|
101
|
+
return store?.[key];
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Sets a value in the current context store by symbol key.
|
|
105
|
+
*
|
|
106
|
+
* @typeParam T - The value type to set.
|
|
107
|
+
* @param {symbol} key - Unique symbol key.
|
|
108
|
+
* @param {T} value - Value to set in context.
|
|
109
|
+
* @throws {Error} If called outside an active context (not within a .run call or in the wrong async boundaries).
|
|
110
|
+
*/
|
|
111
|
+
static set(key, value) {
|
|
112
|
+
const store = this.storage.getStore();
|
|
113
|
+
if (!store) {
|
|
114
|
+
throw new Error(`Failed to set ${String(key)}: AsyncLocalStorage store is not initialized.`);
|
|
115
|
+
}
|
|
116
|
+
store[key] = value;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Retrieves the entire context store object for the current async context.
|
|
120
|
+
*
|
|
121
|
+
* @returns {Store | undefined} The current store object or undefined if called outside a context.
|
|
122
|
+
*/
|
|
123
|
+
static getAll() {
|
|
124
|
+
return this.storage.getStore();
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Initializes a new async context and executes a callback within it.
|
|
128
|
+
* This must be called at the beginning of a request or logical async flow.
|
|
129
|
+
*
|
|
130
|
+
* @typeParam T - The callback's return type.
|
|
131
|
+
* @param {Store} store - The initial key-value store object.
|
|
132
|
+
* @param {() => T} callback - The function to run within the new context.
|
|
133
|
+
* @returns {T} The result of the callback function.
|
|
134
|
+
*/
|
|
135
|
+
static run(store, callback) {
|
|
136
|
+
return this.storage.run(store, callback);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Checks if a key exists in the current context store.
|
|
140
|
+
*
|
|
141
|
+
* @param {symbol} key - The symbol key to check.
|
|
142
|
+
* @returns {boolean} True if the key exists, false otherwise.
|
|
143
|
+
*/
|
|
144
|
+
static has(key) {
|
|
145
|
+
const store = this.storage.getStore();
|
|
146
|
+
return store !== undefined && key in store;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Removes a value from the current context store by symbol key.
|
|
150
|
+
*
|
|
151
|
+
* @param {symbol} key - The symbol key to delete.
|
|
152
|
+
* @returns {boolean} True if the key was deleted, false if the key wasn't found or no active context.
|
|
153
|
+
* @throws {Error} If called outside an active context.
|
|
154
|
+
*/
|
|
155
|
+
static delete(key) {
|
|
156
|
+
const store = this.storage.getStore();
|
|
157
|
+
if (!store) {
|
|
158
|
+
throw new Error(`Failed to delete ${String(key)}: AsyncLocalStorage store is not initialized.`);
|
|
159
|
+
}
|
|
160
|
+
return delete store[key];
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Updates multiple values in the current context store at once.
|
|
164
|
+
*
|
|
165
|
+
* @param {Partial<Record<symbol, unknown>>} values - Object containing symbol keys and values to update.
|
|
166
|
+
* @throws {Error} If called outside an active context.
|
|
167
|
+
*/
|
|
168
|
+
static patch(values) {
|
|
169
|
+
const store = this.storage.getStore();
|
|
170
|
+
if (!store) {
|
|
171
|
+
throw new Error('Failed to patch: AsyncLocalStorage store is not initialized.');
|
|
172
|
+
}
|
|
173
|
+
Object.getOwnPropertySymbols(values).forEach(key => {
|
|
174
|
+
store[key] = values[key];
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Executes a callback with a temporary store value that only exists during execution.
|
|
179
|
+
* Original store is restored after callback completes.
|
|
180
|
+
*
|
|
181
|
+
* @typeParam T - The callback's return type.
|
|
182
|
+
* @param {symbol} key - The symbol key to temporarily set.
|
|
183
|
+
* @param {unknown} value - The temporary value.
|
|
184
|
+
* @param {() => T} callback - The function to execute with the temporary value.
|
|
185
|
+
* @returns {T} The result of the callback function.
|
|
186
|
+
* @throws {Error} If called outside an active context.
|
|
187
|
+
*/
|
|
188
|
+
static withValue(key, value, callback) {
|
|
189
|
+
const store = this.storage.getStore();
|
|
190
|
+
if (!store) {
|
|
191
|
+
throw new Error(`Failed to set temporary value: AsyncLocalStorage store is not initialized.`);
|
|
192
|
+
}
|
|
193
|
+
// Save original value
|
|
194
|
+
const hasOriginal = key in store;
|
|
195
|
+
const originalValue = store[key];
|
|
196
|
+
// Set temporary value
|
|
197
|
+
store[key] = value;
|
|
198
|
+
try {
|
|
199
|
+
// Run callback
|
|
200
|
+
return callback();
|
|
201
|
+
}
|
|
202
|
+
finally {
|
|
203
|
+
// Restore original value
|
|
204
|
+
if (hasOriginal) {
|
|
205
|
+
store[key] = originalValue;
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
delete store[key];
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Creates a new context that inherits values from the current context.
|
|
214
|
+
*
|
|
215
|
+
* @typeParam T - The callback's return type.
|
|
216
|
+
* @param {Partial<Record<symbol, unknown>>} newValues - New values to add to or override in the context.
|
|
217
|
+
* @param {() => T} callback - The function to execute in the new context.
|
|
218
|
+
* @returns {T} The result of the callback function.
|
|
219
|
+
*/
|
|
220
|
+
static extend(newValues, callback) {
|
|
221
|
+
const currentStore = this.storage.getStore() || {};
|
|
222
|
+
// Create new store with current values plus new ones
|
|
223
|
+
const newStore = { ...currentStore };
|
|
224
|
+
// Add new values
|
|
225
|
+
Object.getOwnPropertySymbols(newValues).forEach(key => {
|
|
226
|
+
newStore[key] = newValues[key];
|
|
227
|
+
});
|
|
228
|
+
// Run with extended store
|
|
229
|
+
return this.storage.run(newStore, callback);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Creates Express middleware that initializes a context for each request.
|
|
233
|
+
*
|
|
234
|
+
* @param {(req: any) => Partial<Record<symbol, unknown>>} initialValuesFactory - Function that returns initial context values.
|
|
235
|
+
* @returns Express middleware function.
|
|
236
|
+
*/
|
|
237
|
+
static createExpressMiddleware(initialValuesFactory = () => ({})) {
|
|
238
|
+
return (req, _res, next) => {
|
|
239
|
+
const initialValues = initialValuesFactory(req);
|
|
240
|
+
ContextStore.run(initialValues, next);
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
exports.ContextStore = ContextStore;
|
|
245
|
+
/**
|
|
246
|
+
* Type-safe wrapper for accessing and modifying context values with specific types.
|
|
247
|
+
*
|
|
248
|
+
* @typeParam T - The value type.
|
|
249
|
+
*/
|
|
250
|
+
class TypedContextKey {
|
|
251
|
+
symbol;
|
|
252
|
+
defaultValue;
|
|
253
|
+
/**
|
|
254
|
+
* Creates a new typed context key.
|
|
255
|
+
*
|
|
256
|
+
* @param symbol - The unique symbol for this key
|
|
257
|
+
* @param defaultValue - Optional default value if key is not found
|
|
258
|
+
*/
|
|
259
|
+
constructor(symbol, defaultValue) {
|
|
260
|
+
this.symbol = symbol;
|
|
261
|
+
this.defaultValue = defaultValue;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Gets the current value for this key.
|
|
265
|
+
*
|
|
266
|
+
* @returns The value or defaultValue if not found
|
|
267
|
+
*/
|
|
268
|
+
get() {
|
|
269
|
+
const value = ContextStore.get(this.symbol);
|
|
270
|
+
return value !== undefined ? value : this.defaultValue;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Sets the value for this key.
|
|
274
|
+
*
|
|
275
|
+
* @param value The value to set
|
|
276
|
+
*/
|
|
277
|
+
set(value) {
|
|
278
|
+
ContextStore.set(this.symbol, value);
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Checks if this key exists in the context.
|
|
282
|
+
*
|
|
283
|
+
* @returns True if the key exists
|
|
284
|
+
*/
|
|
285
|
+
exists() {
|
|
286
|
+
return ContextStore.has(this.symbol);
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Deletes this key from the context.
|
|
290
|
+
*
|
|
291
|
+
* @returns True if the key was deleted
|
|
292
|
+
*/
|
|
293
|
+
delete() {
|
|
294
|
+
return ContextStore.delete(this.symbol);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
exports.TypedContextKey = TypedContextKey;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { BinaryToTextEncoding, CipherGCMTypes } from 'crypto';
|
|
2
|
+
export type BufferEncoding = 'ascii' | 'utf8' | 'utf-8' | 'utf16le' | 'utf-16le' | 'ucs2' | 'ucs-2' | 'base64' | 'base64url' | 'latin1' | 'binary' | 'hex';
|
|
3
|
+
/**
|
|
4
|
+
* Generates an HMAC digest using the specified algorithm and secret key.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} algorithm - The hashing algorithm (e.g., 'sha256', 'sha1').
|
|
7
|
+
* @param {string} input - The string to hash.
|
|
8
|
+
* @param {string} secret - The secret key for HMAC.
|
|
9
|
+
* @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding ('hex', 'base64', etc).
|
|
10
|
+
* @returns {string} HMAC digest as a string.
|
|
11
|
+
*/
|
|
12
|
+
export declare function hmac(algorithm: string, input: string, secret: string, encoding?: BinaryToTextEncoding): string;
|
|
13
|
+
/**
|
|
14
|
+
* Generates a hash digest using the specified algorithm.
|
|
15
|
+
*
|
|
16
|
+
* @param {string} algorithm - The hashing algorithm (e.g., 'sha256', 'md5').
|
|
17
|
+
* @param {string} input - The string to hash.
|
|
18
|
+
* @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding ('hex', 'base64', etc).
|
|
19
|
+
* @returns {string} Hash digest as a string.
|
|
20
|
+
*/
|
|
21
|
+
export declare function hash(algorithm: string, input: string, encoding?: BinaryToTextEncoding): string;
|
|
22
|
+
/**
|
|
23
|
+
* Generates an HMAC-SHA256 digest.
|
|
24
|
+
*
|
|
25
|
+
* @param {string} input - The string to hash.
|
|
26
|
+
* @param {string} secret - The secret key.
|
|
27
|
+
* @returns {string} SHA-256 HMAC digest as a string.
|
|
28
|
+
*/
|
|
29
|
+
export declare function sha256Hmac(input: string, secret: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* Generates a SHA1 hash digest.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} input - The string to hash.
|
|
34
|
+
* @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding.
|
|
35
|
+
* @returns {string} SHA-1 hash as a string.
|
|
36
|
+
*/
|
|
37
|
+
export declare function sha1(input: string, encoding?: BinaryToTextEncoding): string;
|
|
38
|
+
/**
|
|
39
|
+
* Generates a SHA256 hash digest.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} input - The string to hash.
|
|
42
|
+
* @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding.
|
|
43
|
+
* @returns {string} SHA-256 hash as a string.
|
|
44
|
+
*/
|
|
45
|
+
export declare function sha256(input: string, encoding?: BinaryToTextEncoding): string;
|
|
46
|
+
/**
|
|
47
|
+
* Generates an MD5 hash digest.
|
|
48
|
+
*
|
|
49
|
+
* @param {string} input - The string to hash.
|
|
50
|
+
* @returns {string} MD5 hash as a string.
|
|
51
|
+
*/
|
|
52
|
+
export declare function md5(input: string): string;
|
|
53
|
+
/**
|
|
54
|
+
* Generates a cryptographically strong random string by hashing a random UUID with SHA-256.
|
|
55
|
+
*
|
|
56
|
+
* @returns {string} Random string hashed with SHA-256 (hex encoding).
|
|
57
|
+
*/
|
|
58
|
+
export declare function randomString(): string;
|
|
59
|
+
/**
|
|
60
|
+
* Generates a secure random buffer of specified byte length.
|
|
61
|
+
*
|
|
62
|
+
* @param {number} [byteLength=32] - Number of random bytes to generate.
|
|
63
|
+
* @returns {Buffer} Buffer containing random bytes.
|
|
64
|
+
*/
|
|
65
|
+
export declare function generateRandomBytes(byteLength?: number): Buffer;
|
|
66
|
+
/**
|
|
67
|
+
* Generates a secure random string of specified byte length with specified encoding.
|
|
68
|
+
*
|
|
69
|
+
* @param {number} [byteLength=32] - Number of random bytes to generate.
|
|
70
|
+
* @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding.
|
|
71
|
+
* @returns {string} Random string in specified encoding.
|
|
72
|
+
*/
|
|
73
|
+
export declare function generateRandomBytesAsString(byteLength?: number, encoding?: BinaryToTextEncoding): string;
|
|
74
|
+
/**
|
|
75
|
+
* Generates a secure API key with a specified format.
|
|
76
|
+
*
|
|
77
|
+
* @param {string} [prefix=''] - Optional prefix for the key.
|
|
78
|
+
* @param {number} [byteLength=24] - Number of random bytes to generate.
|
|
79
|
+
* @returns {string} Formatted API key.
|
|
80
|
+
*/
|
|
81
|
+
export declare function generateApiKey(prefix?: string, byteLength?: number): string;
|
|
82
|
+
/**
|
|
83
|
+
* Compares two strings, arrays, or buffers in constant time to prevent timing attacks.
|
|
84
|
+
*
|
|
85
|
+
* @param {string | Buffer | Uint8Array} a - First value to compare
|
|
86
|
+
* @param {string | Buffer | Uint8Array} b - Second value to compare
|
|
87
|
+
* @returns {boolean} True if values are equal
|
|
88
|
+
*/
|
|
89
|
+
export declare function safeCompare(a: string | Buffer | Uint8Array, b: string | Buffer | Uint8Array): boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Interface for encryption options
|
|
92
|
+
*/
|
|
93
|
+
export interface EncryptionOptions {
|
|
94
|
+
/** Algorithm to use (default: aes-256-gcm) */
|
|
95
|
+
algorithm?: CipherGCMTypes;
|
|
96
|
+
/** Input encoding for plaintext if string (default: utf8) */
|
|
97
|
+
inputEncoding?: BufferEncoding;
|
|
98
|
+
/** Output encoding for ciphertext (default: hex) */
|
|
99
|
+
outputEncoding?: BinaryToTextEncoding;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Interface for decryption options
|
|
103
|
+
*/
|
|
104
|
+
export interface DecryptionOptions {
|
|
105
|
+
/** Algorithm to use (default: aes-256-gcm) */
|
|
106
|
+
algorithm?: CipherGCMTypes;
|
|
107
|
+
/** Input encoding for ciphertext if string (default: hex) */
|
|
108
|
+
inputEncoding?: BinaryToTextEncoding;
|
|
109
|
+
/** Output encoding for plaintext (default: utf8) */
|
|
110
|
+
outputEncoding?: BufferEncoding;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Result of encryption operation including all data needed for decryption
|
|
114
|
+
*/
|
|
115
|
+
export interface EncryptionResult {
|
|
116
|
+
/** Encrypted data (string or Buffer based on options) */
|
|
117
|
+
ciphertext: string | Buffer;
|
|
118
|
+
/** Initialization vector */
|
|
119
|
+
iv: Buffer;
|
|
120
|
+
/** Authentication tag (for GCM mode) */
|
|
121
|
+
authTag?: Buffer;
|
|
122
|
+
/** Algorithm used */
|
|
123
|
+
algorithm: string;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Encrypts data using a symmetric key with secure defaults (AES-256-GCM).
|
|
127
|
+
*
|
|
128
|
+
* @param {string | Buffer} data - Data to encrypt
|
|
129
|
+
* @param {string | Buffer} key - Encryption key or passphrase
|
|
130
|
+
* @param {EncryptionOptions} [options] - Encryption options
|
|
131
|
+
* @returns {Promise<EncryptionResult>} Encrypted data with metadata
|
|
132
|
+
*/
|
|
133
|
+
export declare function encrypt(data: string | Buffer, key: string | Buffer, options?: EncryptionOptions): Promise<EncryptionResult>;
|
|
134
|
+
/**
|
|
135
|
+
* Decrypts data that was encrypted with the encrypt function.
|
|
136
|
+
*
|
|
137
|
+
* @param {EncryptionResult} encryptedData - The encrypted data and metadata
|
|
138
|
+
* @param {string | Buffer} key - Decryption key or passphrase
|
|
139
|
+
* @param {DecryptionOptions} [options] - Decryption options
|
|
140
|
+
* @returns {Promise<string | Buffer>} Decrypted data
|
|
141
|
+
*/
|
|
142
|
+
export declare function decrypt(encryptedData: EncryptionResult, key: string | Buffer, options?: DecryptionOptions): Promise<string | Buffer>;
|
|
143
|
+
/**
|
|
144
|
+
* Creates a signed token with an expiration time and payload.
|
|
145
|
+
*
|
|
146
|
+
* @param {object} payload - Data to include in the token
|
|
147
|
+
* @param {string} secret - Secret key for signing
|
|
148
|
+
* @param {number} [expiresInSeconds=3600] - Token expiration in seconds
|
|
149
|
+
* @returns {string} Signed token string
|
|
150
|
+
*/
|
|
151
|
+
export declare function createSignedToken(payload: Record<string, any>, secret: string, expiresInSeconds?: number): string;
|
|
152
|
+
/**
|
|
153
|
+
* Verifies and decodes a signed token.
|
|
154
|
+
*
|
|
155
|
+
* @param {string} token - The token to verify
|
|
156
|
+
* @param {string} secret - Secret key for verification
|
|
157
|
+
* @returns {object | null} Decoded payload if valid, null if invalid
|
|
158
|
+
*/
|
|
159
|
+
export declare function verifySignedToken(token: string, secret: string): Record<string, any> | null;
|