@catbee/utils 0.0.8-rc.3 → 1.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.
Files changed (129) hide show
  1. package/README.md +3 -2
  2. package/build/index.cjs +6912 -0
  3. package/build/index.d.ts +5284 -0
  4. package/build/index.mjs +6635 -0
  5. package/package.json +13 -292
  6. package/build/cjs/config.d.ts +0 -121
  7. package/build/cjs/config.js +0 -137
  8. package/build/cjs/index.d.ts +0 -26
  9. package/build/cjs/index.js +0 -68
  10. package/build/cjs/servers/server.builder.d.ts +0 -507
  11. package/build/cjs/servers/server.builder.js +0 -658
  12. package/build/cjs/servers/server.d.ts +0 -255
  13. package/build/cjs/servers/server.js +0 -970
  14. package/build/cjs/types/api-response.d.ts +0 -151
  15. package/build/cjs/types/api-response.js +0 -36
  16. package/build/cjs/types/index.d.ts +0 -124
  17. package/build/cjs/types/index.js +0 -25
  18. package/build/cjs/types/server.d.ts +0 -267
  19. package/build/cjs/types/server.js +0 -25
  20. package/build/cjs/utils/array.utils.d.ts +0 -167
  21. package/build/cjs/utils/array.utils.js +0 -363
  22. package/build/cjs/utils/async.utils.d.ts +0 -264
  23. package/build/cjs/utils/async.utils.js +0 -640
  24. package/build/cjs/utils/cache.utils.d.ts +0 -152
  25. package/build/cjs/utils/cache.utils.js +0 -298
  26. package/build/cjs/utils/context-store.utils.d.ts +0 -188
  27. package/build/cjs/utils/context-store.utils.js +0 -297
  28. package/build/cjs/utils/crypto.utils.d.ts +0 -159
  29. package/build/cjs/utils/crypto.utils.js +0 -295
  30. package/build/cjs/utils/date.utils.d.ts +0 -158
  31. package/build/cjs/utils/date.utils.js +0 -395
  32. package/build/cjs/utils/decorators.utils.d.ts +0 -511
  33. package/build/cjs/utils/decorators.utils.js +0 -1035
  34. package/build/cjs/utils/dir.utils.d.ts +0 -195
  35. package/build/cjs/utils/dir.utils.js +0 -500
  36. package/build/cjs/utils/env.utils.d.ts +0 -376
  37. package/build/cjs/utils/env.utils.js +0 -786
  38. package/build/cjs/utils/exception.utils.d.ts +0 -229
  39. package/build/cjs/utils/exception.utils.js +0 -408
  40. package/build/cjs/utils/fs.utils.d.ts +0 -163
  41. package/build/cjs/utils/fs.utils.js +0 -371
  42. package/build/cjs/utils/http-status-codes.d.ts +0 -265
  43. package/build/cjs/utils/http-status-codes.js +0 -297
  44. package/build/cjs/utils/id.utils.d.ts +0 -35
  45. package/build/cjs/utils/id.utils.js +0 -90
  46. package/build/cjs/utils/logger.utils.d.ts +0 -159
  47. package/build/cjs/utils/logger.utils.js +0 -350
  48. package/build/cjs/utils/middleware.utils.d.ts +0 -99
  49. package/build/cjs/utils/middleware.utils.js +0 -243
  50. package/build/cjs/utils/obj.utils.d.ts +0 -123
  51. package/build/cjs/utils/obj.utils.js +0 -428
  52. package/build/cjs/utils/performance.utils.d.ts +0 -135
  53. package/build/cjs/utils/performance.utils.js +0 -280
  54. package/build/cjs/utils/request.utils.d.ts +0 -85
  55. package/build/cjs/utils/request.utils.js +0 -199
  56. package/build/cjs/utils/response.utils.d.ts +0 -162
  57. package/build/cjs/utils/response.utils.js +0 -274
  58. package/build/cjs/utils/stream.utils.d.ts +0 -87
  59. package/build/cjs/utils/stream.utils.js +0 -217
  60. package/build/cjs/utils/string.utils.d.ts +0 -92
  61. package/build/cjs/utils/string.utils.js +0 -178
  62. package/build/cjs/utils/type.utils.d.ts +0 -89
  63. package/build/cjs/utils/type.utils.js +0 -195
  64. package/build/cjs/utils/url.utils.d.ts +0 -140
  65. package/build/cjs/utils/url.utils.js +0 -316
  66. package/build/cjs/utils/validate.utils.d.ts +0 -176
  67. package/build/cjs/utils/validate.utils.js +0 -344
  68. package/build/esm/config.d.ts +0 -121
  69. package/build/esm/config.js +0 -132
  70. package/build/esm/index.d.ts +0 -26
  71. package/build/esm/index.js +0 -49
  72. package/build/esm/servers/server.builder.d.ts +0 -507
  73. package/build/esm/servers/server.builder.js +0 -654
  74. package/build/esm/servers/server.d.ts +0 -255
  75. package/build/esm/servers/server.js +0 -963
  76. package/build/esm/types/api-response.d.ts +0 -151
  77. package/build/esm/types/api-response.js +0 -33
  78. package/build/esm/types/index.d.ts +0 -124
  79. package/build/esm/types/index.js +0 -24
  80. package/build/esm/types/server.d.ts +0 -267
  81. package/build/esm/types/server.js +0 -24
  82. package/build/esm/utils/array.utils.d.ts +0 -167
  83. package/build/esm/utils/array.utils.js +0 -344
  84. package/build/esm/utils/async.utils.d.ts +0 -264
  85. package/build/esm/utils/async.utils.js +0 -619
  86. package/build/esm/utils/cache.utils.d.ts +0 -152
  87. package/build/esm/utils/cache.utils.js +0 -294
  88. package/build/esm/utils/context-store.utils.d.ts +0 -188
  89. package/build/esm/utils/context-store.utils.js +0 -290
  90. package/build/esm/utils/crypto.utils.d.ts +0 -159
  91. package/build/esm/utils/crypto.utils.js +0 -278
  92. package/build/esm/utils/date.utils.d.ts +0 -158
  93. package/build/esm/utils/date.utils.js +0 -383
  94. package/build/esm/utils/decorators.utils.d.ts +0 -511
  95. package/build/esm/utils/decorators.utils.js +0 -1013
  96. package/build/esm/utils/dir.utils.d.ts +0 -195
  97. package/build/esm/utils/dir.utils.js +0 -476
  98. package/build/esm/utils/env.utils.d.ts +0 -376
  99. package/build/esm/utils/env.utils.js +0 -782
  100. package/build/esm/utils/exception.utils.d.ts +0 -229
  101. package/build/esm/utils/exception.utils.js +0 -382
  102. package/build/esm/utils/fs.utils.d.ts +0 -163
  103. package/build/esm/utils/fs.utils.js +0 -348
  104. package/build/esm/utils/http-status-codes.d.ts +0 -265
  105. package/build/esm/utils/http-status-codes.js +0 -294
  106. package/build/esm/utils/id.utils.d.ts +0 -35
  107. package/build/esm/utils/id.utils.js +0 -83
  108. package/build/esm/utils/logger.utils.d.ts +0 -159
  109. package/build/esm/utils/logger.utils.js +0 -304
  110. package/build/esm/utils/middleware.utils.d.ts +0 -99
  111. package/build/esm/utils/middleware.utils.js +0 -235
  112. package/build/esm/utils/obj.utils.d.ts +0 -123
  113. package/build/esm/utils/obj.utils.js +0 -412
  114. package/build/esm/utils/performance.utils.d.ts +0 -135
  115. package/build/esm/utils/performance.utils.js +0 -273
  116. package/build/esm/utils/request.utils.d.ts +0 -85
  117. package/build/esm/utils/request.utils.js +0 -190
  118. package/build/esm/utils/response.utils.d.ts +0 -162
  119. package/build/esm/utils/response.utils.js +0 -261
  120. package/build/esm/utils/stream.utils.d.ts +0 -87
  121. package/build/esm/utils/stream.utils.js +0 -209
  122. package/build/esm/utils/string.utils.d.ts +0 -92
  123. package/build/esm/utils/string.utils.js +0 -164
  124. package/build/esm/utils/type.utils.d.ts +0 -89
  125. package/build/esm/utils/type.utils.js +0 -186
  126. package/build/esm/utils/url.utils.d.ts +0 -140
  127. package/build/esm/utils/url.utils.js +0 -303
  128. package/build/esm/utils/validate.utils.d.ts +0 -176
  129. package/build/esm/utils/validate.utils.js +0 -319
@@ -1,297 +0,0 @@
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;
@@ -1,159 +0,0 @@
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;