@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.
Files changed (183) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +295 -0
  3. package/build/esm/config.d.ts +22 -0
  4. package/build/esm/config.js +21 -0
  5. package/build/esm/config.js.map +1 -0
  6. package/build/esm/index.d.ts +19 -0
  7. package/build/esm/index.js +19 -0
  8. package/build/esm/index.js.map +1 -0
  9. package/build/esm/types/api-response.d.ts +44 -0
  10. package/build/esm/types/api-response.js +2 -0
  11. package/build/esm/types/api-response.js.map +1 -0
  12. package/build/esm/utils/array.utils.d.ts +105 -0
  13. package/build/esm/utils/array.utils.js +232 -0
  14. package/build/esm/utils/array.utils.js.map +1 -0
  15. package/build/esm/utils/async.utils.d.ts +119 -0
  16. package/build/esm/utils/async.utils.js +474 -0
  17. package/build/esm/utils/async.utils.js.map +1 -0
  18. package/build/esm/utils/cache.utils.d.ts +90 -0
  19. package/build/esm/utils/cache.utils.js +294 -0
  20. package/build/esm/utils/cache.utils.js.map +1 -0
  21. package/build/esm/utils/context-store.utils.d.ts +85 -0
  22. package/build/esm/utils/context-store.utils.js +99 -0
  23. package/build/esm/utils/context-store.utils.js.map +1 -0
  24. package/build/esm/utils/crypto.utils.d.ts +58 -0
  25. package/build/esm/utils/crypto.utils.js +76 -0
  26. package/build/esm/utils/crypto.utils.js.map +1 -0
  27. package/build/esm/utils/dir.utils.d.ts +75 -0
  28. package/build/esm/utils/dir.utils.js +400 -0
  29. package/build/esm/utils/dir.utils.js.map +1 -0
  30. package/build/esm/utils/env.utils.d.ts +131 -0
  31. package/build/esm/utils/env.utils.js +218 -0
  32. package/build/esm/utils/env.utils.js.map +1 -0
  33. package/build/esm/utils/exception.utils.d.ts +114 -0
  34. package/build/esm/utils/exception.utils.js +217 -0
  35. package/build/esm/utils/exception.utils.js.map +1 -0
  36. package/build/esm/utils/fs.utils.d.ts +32 -0
  37. package/build/esm/utils/fs.utils.js +140 -0
  38. package/build/esm/utils/fs.utils.js.map +1 -0
  39. package/build/esm/utils/http-status-codes.d.ts +357 -0
  40. package/build/esm/utils/http-status-codes.js +358 -0
  41. package/build/esm/utils/http-status-codes.js.map +1 -0
  42. package/build/esm/utils/id.utils.d.ts +35 -0
  43. package/build/esm/utils/id.utils.js +55 -0
  44. package/build/esm/utils/id.utils.js.map +1 -0
  45. package/build/esm/utils/logger.utils.d.ts +15 -0
  46. package/build/esm/utils/logger.utils.js +59 -0
  47. package/build/esm/utils/logger.utils.js.map +1 -0
  48. package/build/esm/utils/obj.utils.d.ts +55 -0
  49. package/build/esm/utils/obj.utils.js +121 -0
  50. package/build/esm/utils/obj.utils.js.map +1 -0
  51. package/build/esm/utils/response.utils.d.ts +48 -0
  52. package/build/esm/utils/response.utils.js +80 -0
  53. package/build/esm/utils/response.utils.js.map +1 -0
  54. package/build/esm/utils/string.utils.d.ts +37 -0
  55. package/build/esm/utils/string.utils.js +56 -0
  56. package/build/esm/utils/string.utils.js.map +1 -0
  57. package/build/esm/utils/url.utils.d.ts +24 -0
  58. package/build/esm/utils/url.utils.js +73 -0
  59. package/build/esm/utils/url.utils.js.map +1 -0
  60. package/build/esm/utils/validate.utils.d.ts +60 -0
  61. package/build/esm/utils/validate.utils.js +121 -0
  62. package/build/esm/utils/validate.utils.js.map +1 -0
  63. package/build/esnext/config.d.ts +22 -0
  64. package/build/esnext/config.js +21 -0
  65. package/build/esnext/config.js.map +1 -0
  66. package/build/esnext/index.d.ts +19 -0
  67. package/build/esnext/index.js +19 -0
  68. package/build/esnext/index.js.map +1 -0
  69. package/build/esnext/types/api-response.d.ts +44 -0
  70. package/build/esnext/types/api-response.js +2 -0
  71. package/build/esnext/types/api-response.js.map +1 -0
  72. package/build/esnext/utils/array.utils.d.ts +105 -0
  73. package/build/esnext/utils/array.utils.js +203 -0
  74. package/build/esnext/utils/array.utils.js.map +1 -0
  75. package/build/esnext/utils/async.utils.d.ts +119 -0
  76. package/build/esnext/utils/async.utils.js +291 -0
  77. package/build/esnext/utils/async.utils.js.map +1 -0
  78. package/build/esnext/utils/cache.utils.d.ts +90 -0
  79. package/build/esnext/utils/cache.utils.js +142 -0
  80. package/build/esnext/utils/cache.utils.js.map +1 -0
  81. package/build/esnext/utils/context-store.utils.d.ts +85 -0
  82. package/build/esnext/utils/context-store.utils.js +95 -0
  83. package/build/esnext/utils/context-store.utils.js.map +1 -0
  84. package/build/esnext/utils/crypto.utils.d.ts +58 -0
  85. package/build/esnext/utils/crypto.utils.js +72 -0
  86. package/build/esnext/utils/crypto.utils.js.map +1 -0
  87. package/build/esnext/utils/dir.utils.d.ts +75 -0
  88. package/build/esnext/utils/dir.utils.js +150 -0
  89. package/build/esnext/utils/dir.utils.js.map +1 -0
  90. package/build/esnext/utils/env.utils.d.ts +131 -0
  91. package/build/esnext/utils/env.utils.js +211 -0
  92. package/build/esnext/utils/env.utils.js.map +1 -0
  93. package/build/esnext/utils/exception.utils.d.ts +114 -0
  94. package/build/esnext/utils/exception.utils.js +148 -0
  95. package/build/esnext/utils/exception.utils.js.map +1 -0
  96. package/build/esnext/utils/fs.utils.d.ts +32 -0
  97. package/build/esnext/utils/fs.utils.js +62 -0
  98. package/build/esnext/utils/fs.utils.js.map +1 -0
  99. package/build/esnext/utils/http-status-codes.d.ts +357 -0
  100. package/build/esnext/utils/http-status-codes.js +358 -0
  101. package/build/esnext/utils/http-status-codes.js.map +1 -0
  102. package/build/esnext/utils/id.utils.d.ts +35 -0
  103. package/build/esnext/utils/id.utils.js +53 -0
  104. package/build/esnext/utils/id.utils.js.map +1 -0
  105. package/build/esnext/utils/logger.utils.d.ts +15 -0
  106. package/build/esnext/utils/logger.utils.js +59 -0
  107. package/build/esnext/utils/logger.utils.js.map +1 -0
  108. package/build/esnext/utils/obj.utils.d.ts +55 -0
  109. package/build/esnext/utils/obj.utils.js +94 -0
  110. package/build/esnext/utils/obj.utils.js.map +1 -0
  111. package/build/esnext/utils/response.utils.d.ts +48 -0
  112. package/build/esnext/utils/response.utils.js +58 -0
  113. package/build/esnext/utils/response.utils.js.map +1 -0
  114. package/build/esnext/utils/string.utils.d.ts +37 -0
  115. package/build/esnext/utils/string.utils.js +46 -0
  116. package/build/esnext/utils/string.utils.js.map +1 -0
  117. package/build/esnext/utils/url.utils.d.ts +24 -0
  118. package/build/esnext/utils/url.utils.js +35 -0
  119. package/build/esnext/utils/url.utils.js.map +1 -0
  120. package/build/esnext/utils/validate.utils.d.ts +60 -0
  121. package/build/esnext/utils/validate.utils.js +105 -0
  122. package/build/esnext/utils/validate.utils.js.map +1 -0
  123. package/build/src/config.d.ts +22 -0
  124. package/build/src/config.js +24 -0
  125. package/build/src/config.js.map +1 -0
  126. package/build/src/index.d.ts +19 -0
  127. package/build/src/index.js +35 -0
  128. package/build/src/index.js.map +1 -0
  129. package/build/src/types/api-response.d.ts +44 -0
  130. package/build/src/types/api-response.js +3 -0
  131. package/build/src/types/api-response.js.map +1 -0
  132. package/build/src/utils/array.utils.d.ts +105 -0
  133. package/build/src/utils/array.utils.js +216 -0
  134. package/build/src/utils/array.utils.js.map +1 -0
  135. package/build/src/utils/async.utils.d.ts +119 -0
  136. package/build/src/utils/async.utils.js +304 -0
  137. package/build/src/utils/async.utils.js.map +1 -0
  138. package/build/src/utils/cache.utils.d.ts +90 -0
  139. package/build/src/utils/cache.utils.js +146 -0
  140. package/build/src/utils/cache.utils.js.map +1 -0
  141. package/build/src/utils/context-store.utils.d.ts +85 -0
  142. package/build/src/utils/context-store.utils.js +100 -0
  143. package/build/src/utils/context-store.utils.js.map +1 -0
  144. package/build/src/utils/crypto.utils.d.ts +58 -0
  145. package/build/src/utils/crypto.utils.js +82 -0
  146. package/build/src/utils/crypto.utils.js.map +1 -0
  147. package/build/src/utils/dir.utils.d.ts +75 -0
  148. package/build/src/utils/dir.utils.js +164 -0
  149. package/build/src/utils/dir.utils.js.map +1 -0
  150. package/build/src/utils/env.utils.d.ts +131 -0
  151. package/build/src/utils/env.utils.js +215 -0
  152. package/build/src/utils/env.utils.js.map +1 -0
  153. package/build/src/utils/exception.utils.d.ts +114 -0
  154. package/build/src/utils/exception.utils.js +162 -0
  155. package/build/src/utils/exception.utils.js.map +1 -0
  156. package/build/src/utils/fs.utils.d.ts +32 -0
  157. package/build/src/utils/fs.utils.js +71 -0
  158. package/build/src/utils/fs.utils.js.map +1 -0
  159. package/build/src/utils/http-status-codes.d.ts +357 -0
  160. package/build/src/utils/http-status-codes.js +361 -0
  161. package/build/src/utils/http-status-codes.js.map +1 -0
  162. package/build/src/utils/id.utils.d.ts +35 -0
  163. package/build/src/utils/id.utils.js +60 -0
  164. package/build/src/utils/id.utils.js.map +1 -0
  165. package/build/src/utils/logger.utils.d.ts +15 -0
  166. package/build/src/utils/logger.utils.js +96 -0
  167. package/build/src/utils/logger.utils.js.map +1 -0
  168. package/build/src/utils/obj.utils.d.ts +55 -0
  169. package/build/src/utils/obj.utils.js +103 -0
  170. package/build/src/utils/obj.utils.js.map +1 -0
  171. package/build/src/utils/response.utils.d.ts +48 -0
  172. package/build/src/utils/response.utils.js +63 -0
  173. package/build/src/utils/response.utils.js.map +1 -0
  174. package/build/src/utils/string.utils.d.ts +37 -0
  175. package/build/src/utils/string.utils.js +54 -0
  176. package/build/src/utils/string.utils.js.map +1 -0
  177. package/build/src/utils/url.utils.d.ts +24 -0
  178. package/build/src/utils/url.utils.js +39 -0
  179. package/build/src/utils/url.utils.js.map +1 -0
  180. package/build/src/utils/validate.utils.d.ts +60 -0
  181. package/build/src/utils/validate.utils.js +115 -0
  182. package/build/src/utils/validate.utils.js.map +1 -0
  183. package/package.json +80 -0
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ContextStore = exports.StoreKeys = void 0;
4
+ exports.getRequestId = getRequestId;
5
+ const async_hooks_1 = require("async_hooks");
6
+ /**
7
+ * Predefined symbols used as keys in AsyncLocalStorage.
8
+ * Add new symbols here to avoid duplication.
9
+ */
10
+ exports.StoreKeys = {
11
+ LOGGER: Symbol("logger"),
12
+ REQUEST_ID: Symbol("requestId"),
13
+ };
14
+ /**
15
+ * Retrieves the current request ID from the async context, if available.
16
+ *
17
+ * @returns {string | undefined} The request ID string or undefined if not present in the current context.
18
+ */
19
+ function getRequestId() {
20
+ return ContextStore.get(exports.StoreKeys.REQUEST_ID);
21
+ }
22
+ /**
23
+ * ContextStore manages per-request scoped context using AsyncLocalStorage.
24
+ * It allows storing and retrieving data across async calls (e.g., request ID, logger).
25
+ *
26
+ * Example (Express middleware):
27
+ * ```
28
+ * // Middleware to initialize context and logger
29
+ * app.use((req, res, next) => {
30
+ * const requestId = req.headers["x-request-id"]?.toString() || crypto.randomUUID();
31
+ * ContextStore.run({ [StoreKeys.REQUEST_ID]: requestId }, () => {
32
+ * ContextStore.set(StoreKeys.LOGGER, getLogger().child({ reqId: requestId }));
33
+ * next();
34
+ * });
35
+ * });
36
+ * ```
37
+ */
38
+ class ContextStore {
39
+ /**
40
+ * Returns the raw AsyncLocalStorage instance for advanced access.
41
+ *
42
+ * @returns {AsyncLocalStorage<Store>} The AsyncLocalStorage instance.
43
+ */
44
+ static getInstance() {
45
+ return this.storage;
46
+ }
47
+ /**
48
+ * Retrieves a value from the async context store by symbol key.
49
+ *
50
+ * @typeParam T - The expected return type of the value.
51
+ * @param {symbol} key - Unique symbol used as the store key.
52
+ * @returns {T | undefined} The value found (typed) or undefined if not present.
53
+ */
54
+ static get(key) {
55
+ const store = this.storage.getStore();
56
+ return store === null || store === void 0 ? void 0 : store[key];
57
+ }
58
+ /**
59
+ * Sets a value in the current context store by symbol key.
60
+ *
61
+ * @typeParam T - The value type to set.
62
+ * @param {symbol} key - Unique symbol key.
63
+ * @param {T} value - Value to set in context.
64
+ * @throws {Error} If called outside an active context (not within a .run call or in the wrong async boundaries).
65
+ */
66
+ static set(key, value) {
67
+ const store = this.storage.getStore();
68
+ if (!store) {
69
+ throw new Error(`Failed to set ${String(key)}: AsyncLocalStorage store is not initialized.`);
70
+ }
71
+ store[key] = value;
72
+ }
73
+ /**
74
+ * Retrieves the entire context store object for the current async context.
75
+ *
76
+ * @returns {Store | undefined} The current store object or undefined if called outside a context.
77
+ */
78
+ static getAll() {
79
+ return this.storage.getStore();
80
+ }
81
+ /**
82
+ * Initializes a new async context and executes a callback within it.
83
+ * This must be called at the beginning of a request or logical async flow.
84
+ *
85
+ * @typeParam T - The callback's return type.
86
+ * @param {Store} store - The initial key-value store object.
87
+ * @param {() => T} callback - The function to run within the new context.
88
+ * @returns {T} The result of the callback function.
89
+ */
90
+ static run(store, callback) {
91
+ return this.storage.run(store, callback);
92
+ }
93
+ }
94
+ exports.ContextStore = ContextStore;
95
+ /**
96
+ * The underlying AsyncLocalStorage instance for context.
97
+ * @private
98
+ */
99
+ ContextStore.storage = new async_hooks_1.AsyncLocalStorage();
100
+ //# 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":";;;AAwBA,oCAEC;AA1BD,6CAAgD;AAUhD;;;GAGG;AACU,QAAA,SAAS,GAAG;IACvB,MAAM,EAAE,MAAM,CAAC,QAAQ,CAAC;IACxB,UAAU,EAAE,MAAM,CAAC,WAAW,CAAC;CAChC,CAAC;AAEF;;;;GAIG;AACH,SAAgB,YAAY;IAC1B,OAAO,YAAY,CAAC,GAAG,CAAS,iBAAS,CAAC,UAAU,CAAC,CAAC;AACxD,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAa,YAAY;IAOvB;;;;OAIG;IACH,MAAM,CAAC,WAAW;QAChB,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED;;;;;;OAMG;IACH,MAAM,CAAC,GAAG,CAAI,GAAW;QACvB,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;QACtC,OAAO,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAG,GAAG,CAAkB,CAAC;IACvC,CAAC;IAED;;;;;;;OAOG;IACH,MAAM,CAAC,GAAG,CAAI,GAAW,EAAE,KAAQ;QACjC,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;QACtC,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,MAAM,IAAI,KAAK,CACb,iBAAiB,MAAM,CAAC,GAAG,CAAC,+CAA+C,CAC5E,CAAC;QACJ,CAAC;QACD,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,MAAM;QACX,OAAO,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;IACjC,CAAC;IAED;;;;;;;;OAQG;IACH,MAAM,CAAC,GAAG,CAAI,KAAY,EAAE,QAAiB;QAC3C,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IAC3C,CAAC;;AAlEH,oCAmEC;AAlEC;;;GAGG;AACqB,oBAAO,GAAG,IAAI,+BAAiB,EAAS,CAAC","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,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.randomString = exports.md5 = exports.sha256 = exports.sha1 = exports.sha256Hmac = exports.hash = exports.hmac = void 0;
4
+ const crypto_1 = require("crypto");
5
+ /**
6
+ * Generates an HMAC digest using the specified algorithm and secret key.
7
+ *
8
+ * @param {string} algorithm - The hashing algorithm (e.g., 'sha256', 'sha1').
9
+ * @param {string} input - The string to hash.
10
+ * @param {string} secret - The secret key for HMAC.
11
+ * @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding ('hex', 'base64', etc).
12
+ * @returns {string} HMAC digest as a string.
13
+ */
14
+ const hmac = (algorithm, input, secret, encoding = "hex") => {
15
+ return (0, crypto_1.createHmac)(algorithm, secret).update(input).digest(encoding);
16
+ };
17
+ exports.hmac = hmac;
18
+ /**
19
+ * Generates a hash digest using the specified algorithm.
20
+ *
21
+ * @param {string} algorithm - The hashing algorithm (e.g., 'sha256', 'md5').
22
+ * @param {string} input - The string to hash.
23
+ * @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding ('hex', 'base64', etc).
24
+ * @returns {string} Hash digest as a string.
25
+ */
26
+ const hash = (algorithm, input, encoding = "hex") => {
27
+ return (0, crypto_1.createHash)(algorithm).update(input).digest(encoding);
28
+ };
29
+ exports.hash = hash;
30
+ /**
31
+ * Generates an HMAC-SHA256 digest.
32
+ *
33
+ * @param {string} input - The string to hash.
34
+ * @param {string} secret - The secret key.
35
+ * @returns {string} SHA-256 HMAC digest as a string.
36
+ */
37
+ const sha256Hmac = (input, secret) => {
38
+ return (0, exports.hmac)("sha256", input, secret);
39
+ };
40
+ exports.sha256Hmac = sha256Hmac;
41
+ /**
42
+ * Generates a SHA1 hash digest.
43
+ *
44
+ * @param {string} input - The string to hash.
45
+ * @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding.
46
+ * @returns {string} SHA-1 hash as a string.
47
+ */
48
+ const sha1 = (input, encoding = "hex") => {
49
+ return (0, exports.hash)("sha1", input, encoding);
50
+ };
51
+ exports.sha1 = sha1;
52
+ /**
53
+ * Generates a SHA256 hash digest.
54
+ *
55
+ * @param {string} input - The string to hash.
56
+ * @param {BinaryToTextEncoding} [encoding='hex'] - Output encoding.
57
+ * @returns {string} SHA-256 hash as a string.
58
+ */
59
+ const sha256 = (input, encoding = "hex") => {
60
+ return (0, exports.hash)("sha256", input, encoding);
61
+ };
62
+ exports.sha256 = sha256;
63
+ /**
64
+ * Generates an MD5 hash digest.
65
+ *
66
+ * @param {string} input - The string to hash.
67
+ * @returns {string} MD5 hash as a string.
68
+ */
69
+ const md5 = (input) => {
70
+ return (0, exports.hash)("md5", input);
71
+ };
72
+ exports.md5 = md5;
73
+ /**
74
+ * Generates a cryptographically strong random string by hashing a random UUID with SHA-256.
75
+ *
76
+ * @returns {string} Random string hashed with SHA-256 (hex encoding).
77
+ */
78
+ const randomString = () => {
79
+ return (0, exports.sha256)((0, crypto_1.randomUUID)());
80
+ };
81
+ exports.randomString = randomString;
82
+ //# 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,mCAKgB;AAEhB;;;;;;;;GAQG;AACI,MAAM,IAAI,GAAG,CAClB,SAAiB,EACjB,KAAa,EACb,MAAc,EACd,WAAiC,KAAK,EAC9B,EAAE;IACV,OAAO,IAAA,mBAAU,EAAC,SAAS,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AACtE,CAAC,CAAC;AAPW,QAAA,IAAI,QAOf;AAEF;;;;;;;GAOG;AACI,MAAM,IAAI,GAAG,CAClB,SAAiB,EACjB,KAAa,EACb,WAAiC,KAAK,EAC9B,EAAE;IACV,OAAO,IAAA,mBAAU,EAAC,SAAS,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAC9D,CAAC,CAAC;AANW,QAAA,IAAI,QAMf;AAEF;;;;;;GAMG;AACI,MAAM,UAAU,GAAG,CAAC,KAAa,EAAE,MAAc,EAAU,EAAE;IAClE,OAAO,IAAA,YAAI,EAAC,QAAQ,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;AACvC,CAAC,CAAC;AAFW,QAAA,UAAU,cAErB;AAEF;;;;;;GAMG;AACI,MAAM,IAAI,GAAG,CAClB,KAAa,EACb,WAAiC,KAAK,EAC9B,EAAE;IACV,OAAO,IAAA,YAAI,EAAC,MAAM,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC;AACvC,CAAC,CAAC;AALW,QAAA,IAAI,QAKf;AAEF;;;;;;GAMG;AACI,MAAM,MAAM,GAAG,CACpB,KAAa,EACb,WAAiC,KAAK,EAC9B,EAAE;IACV,OAAO,IAAA,YAAI,EAAC,QAAQ,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC;AACzC,CAAC,CAAC;AALW,QAAA,MAAM,UAKjB;AAEF;;;;;GAKG;AACI,MAAM,GAAG,GAAG,CAAC,KAAa,EAAU,EAAE;IAC3C,OAAO,IAAA,YAAI,EAAC,KAAK,EAAE,KAAK,CAAC,CAAC;AAC5B,CAAC,CAAC;AAFW,QAAA,GAAG,OAEd;AAEF;;;;GAIG;AACI,MAAM,YAAY,GAAG,GAAW,EAAE;IACvC,OAAO,IAAA,cAAM,EAAC,IAAA,mBAAU,GAAE,CAAC,CAAC;AAC9B,CAAC,CAAC;AAFW,QAAA,YAAY,gBAEvB","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"]}
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Ensures that a directory exists, creating parent directories if needed (like `mkdir -p`).
3
+ *
4
+ * @param {string} dirPath - The directory path to ensure.
5
+ * @returns {Promise<void>} Resolves when the directory exists.
6
+ * @throws {Error} If directory cannot be created.
7
+ */
8
+ export declare function ensureDir(dirPath: string): Promise<void>;
9
+ /**
10
+ * Recursively lists all files in a directory.
11
+ *
12
+ * @param {string} dirPath - The base directory.
13
+ * @param {boolean} [recursive=false] - Whether to recurse into subdirectories.
14
+ * @returns {Promise<string[]>} Array of absolute file paths.
15
+ * @throws {Error} If the directory cannot be read.
16
+ */
17
+ export declare function listFiles(dirPath: string, recursive?: boolean): Promise<string[]>;
18
+ /**
19
+ * Deletes a directory and all its contents recursively (like `rm -rf`).
20
+ *
21
+ * @param {string} dirPath - Directory to delete.
22
+ * @returns {Promise<void>} Resolves when deletion is complete.
23
+ * @throws {Error} If deletion fails.
24
+ */
25
+ export declare function deleteDirRecursive(dirPath: string): Promise<void>;
26
+ /**
27
+ * Checks whether a given path is a directory.
28
+ *
29
+ * @param {string} pathStr - Path to check.
30
+ * @returns {Promise<boolean>} True if the path is a directory, else false.
31
+ */
32
+ export declare function isDirectory(pathStr: string): Promise<boolean>;
33
+ /**
34
+ * Recursively copies a directory and all its contents to a destination.
35
+ *
36
+ * @param {string} src - Source directory path.
37
+ * @param {string} dest - Destination directory path.
38
+ * @returns {Promise<void>} Resolves when copy is complete.
39
+ * @throws {Error} If source does not exist or copy fails.
40
+ */
41
+ export declare function copyDir(src: string, dest: string): Promise<void>;
42
+ /**
43
+ * Moves a directory to a new location by copying and deleting the original.
44
+ *
45
+ * @param {string} src - Source directory path.
46
+ * @param {string} dest - Destination directory path.
47
+ * @returns {Promise<void>} Resolves when move is complete.
48
+ * @throws {Error} If copy or deletion fails.
49
+ */
50
+ export declare function moveDir(src: string, dest: string): Promise<void>;
51
+ /**
52
+ * Empties a directory by deleting all files and subdirectories inside it.
53
+ *
54
+ * @param {string} dirPath - Path to the directory to empty.
55
+ * @returns {Promise<void>} Resolves when the directory has been emptied.
56
+ * @throws {Error} If files or subdirectories cannot be removed.
57
+ */
58
+ export declare function emptyDir(dirPath: string): Promise<void>;
59
+ /**
60
+ * Calculates the total size (in bytes) of all files in a directory (recursive).
61
+ *
62
+ * @param {string} dirPath - Path to the directory.
63
+ * @returns {Promise<number>} Total size in bytes.
64
+ * @throws {Error} If any file stats cannot be read.
65
+ */
66
+ export declare function getDirSize(dirPath: string): Promise<number>;
67
+ /**
68
+ * Watches a directory for file changes and calls a callback on each event.
69
+ *
70
+ * @param {string} dirPath - Directory path to watch.
71
+ * @param {(eventType: "rename" | "change", filename: string | null) => void} callback - Callback for each change event.
72
+ * @returns {() => void} A function to stop watching the directory.
73
+ */
74
+ export declare function watchDir(dirPath: string, callback: (eventType: "rename" | "change", filename: string | null) => void): () => void;
75
+ //# sourceMappingURL=dir.utils.d.ts.map
@@ -0,0 +1,164 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ensureDir = ensureDir;
7
+ exports.listFiles = listFiles;
8
+ exports.deleteDirRecursive = deleteDirRecursive;
9
+ exports.isDirectory = isDirectory;
10
+ exports.copyDir = copyDir;
11
+ exports.moveDir = moveDir;
12
+ exports.emptyDir = emptyDir;
13
+ exports.getDirSize = getDirSize;
14
+ exports.watchDir = watchDir;
15
+ const fs_1 = __importDefault(require("fs"));
16
+ const promises_1 = __importDefault(require("fs/promises"));
17
+ const path_1 = __importDefault(require("path"));
18
+ /**
19
+ * Ensures that a directory exists, creating parent directories if needed (like `mkdir -p`).
20
+ *
21
+ * @param {string} dirPath - The directory path to ensure.
22
+ * @returns {Promise<void>} Resolves when the directory exists.
23
+ * @throws {Error} If directory cannot be created.
24
+ */
25
+ async function ensureDir(dirPath) {
26
+ await promises_1.default.mkdir(dirPath, { recursive: true });
27
+ }
28
+ /**
29
+ * Recursively lists all files in a directory.
30
+ *
31
+ * @param {string} dirPath - The base directory.
32
+ * @param {boolean} [recursive=false] - Whether to recurse into subdirectories.
33
+ * @returns {Promise<string[]>} Array of absolute file paths.
34
+ * @throws {Error} If the directory cannot be read.
35
+ */
36
+ async function listFiles(dirPath, recursive = false) {
37
+ const entries = await promises_1.default.readdir(dirPath, { withFileTypes: true });
38
+ const files = [];
39
+ for (const entry of entries) {
40
+ const fullPath = path_1.default.join(dirPath, entry.name);
41
+ if (entry.isFile()) {
42
+ files.push(fullPath);
43
+ }
44
+ else if (recursive && entry.isDirectory()) {
45
+ const nestedFiles = await listFiles(fullPath, true);
46
+ files.push(...nestedFiles);
47
+ }
48
+ }
49
+ return files;
50
+ }
51
+ /**
52
+ * Deletes a directory and all its contents recursively (like `rm -rf`).
53
+ *
54
+ * @param {string} dirPath - Directory to delete.
55
+ * @returns {Promise<void>} Resolves when deletion is complete.
56
+ * @throws {Error} If deletion fails.
57
+ */
58
+ async function deleteDirRecursive(dirPath) {
59
+ await promises_1.default.rm(dirPath, { recursive: true, force: true });
60
+ }
61
+ /**
62
+ * Checks whether a given path is a directory.
63
+ *
64
+ * @param {string} pathStr - Path to check.
65
+ * @returns {Promise<boolean>} True if the path is a directory, else false.
66
+ */
67
+ async function isDirectory(pathStr) {
68
+ try {
69
+ const stat = await promises_1.default.stat(pathStr);
70
+ return stat.isDirectory();
71
+ }
72
+ catch (_a) {
73
+ return false;
74
+ }
75
+ }
76
+ /**
77
+ * Recursively copies a directory and all its contents to a destination.
78
+ *
79
+ * @param {string} src - Source directory path.
80
+ * @param {string} dest - Destination directory path.
81
+ * @returns {Promise<void>} Resolves when copy is complete.
82
+ * @throws {Error} If source does not exist or copy fails.
83
+ */
84
+ async function copyDir(src, dest) {
85
+ await ensureDir(dest);
86
+ const entries = await promises_1.default.readdir(src, { withFileTypes: true });
87
+ for (const entry of entries) {
88
+ const srcPath = path_1.default.join(src, entry.name);
89
+ const destPath = path_1.default.join(dest, entry.name);
90
+ if (entry.isDirectory()) {
91
+ await copyDir(srcPath, destPath);
92
+ }
93
+ else {
94
+ await promises_1.default.copyFile(srcPath, destPath);
95
+ }
96
+ }
97
+ }
98
+ /**
99
+ * Moves a directory to a new location by copying and deleting the original.
100
+ *
101
+ * @param {string} src - Source directory path.
102
+ * @param {string} dest - Destination directory path.
103
+ * @returns {Promise<void>} Resolves when move is complete.
104
+ * @throws {Error} If copy or deletion fails.
105
+ */
106
+ async function moveDir(src, dest) {
107
+ await copyDir(src, dest);
108
+ await deleteDirRecursive(src);
109
+ }
110
+ /**
111
+ * Empties a directory by deleting all files and subdirectories inside it.
112
+ *
113
+ * @param {string} dirPath - Path to the directory to empty.
114
+ * @returns {Promise<void>} Resolves when the directory has been emptied.
115
+ * @throws {Error} If files or subdirectories cannot be removed.
116
+ */
117
+ async function emptyDir(dirPath) {
118
+ const entries = await promises_1.default.readdir(dirPath, { withFileTypes: true });
119
+ for (const entry of entries) {
120
+ const entryPath = path_1.default.join(dirPath, entry.name);
121
+ if (entry.isDirectory()) {
122
+ await promises_1.default.rm(entryPath, { recursive: true, force: true });
123
+ }
124
+ else {
125
+ await promises_1.default.unlink(entryPath);
126
+ }
127
+ }
128
+ }
129
+ /**
130
+ * Calculates the total size (in bytes) of all files in a directory (recursive).
131
+ *
132
+ * @param {string} dirPath - Path to the directory.
133
+ * @returns {Promise<number>} Total size in bytes.
134
+ * @throws {Error} If any file stats cannot be read.
135
+ */
136
+ async function getDirSize(dirPath) {
137
+ let total = 0;
138
+ const entries = await promises_1.default.readdir(dirPath, { withFileTypes: true });
139
+ for (const entry of entries) {
140
+ const entryPath = path_1.default.join(dirPath, entry.name);
141
+ if (entry.isDirectory()) {
142
+ total += await getDirSize(entryPath);
143
+ }
144
+ else {
145
+ const stat = await promises_1.default.stat(entryPath);
146
+ total += stat.size;
147
+ }
148
+ }
149
+ return total;
150
+ }
151
+ /**
152
+ * Watches a directory for file changes and calls a callback on each event.
153
+ *
154
+ * @param {string} dirPath - Directory path to watch.
155
+ * @param {(eventType: "rename" | "change", filename: string | null) => void} callback - Callback for each change event.
156
+ * @returns {() => void} A function to stop watching the directory.
157
+ */
158
+ function watchDir(dirPath, callback) {
159
+ const watcher = fs_1.default.watch(dirPath, (eventType, filename) => {
160
+ callback(eventType, filename);
161
+ });
162
+ return () => watcher.close();
163
+ }
164
+ //# sourceMappingURL=dir.utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dir.utils.js","sourceRoot":"","sources":["../../../src/utils/dir.utils.ts"],"names":[],"mappings":"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","sourcesContent":["import fs from \"fs\";\nimport fsp from \"fs/promises\";\nimport path from \"path\";\n\n/**\n * Ensures that a directory exists, creating parent directories if needed (like `mkdir -p`).\n *\n * @param {string} dirPath - The directory path to ensure.\n * @returns {Promise<void>} Resolves when the directory exists.\n * @throws {Error} If directory cannot be created.\n */\nexport async function ensureDir(dirPath: string): Promise<void> {\n await fsp.mkdir(dirPath, { recursive: true });\n}\n\n/**\n * Recursively lists all files in a directory.\n *\n * @param {string} dirPath - The base directory.\n * @param {boolean} [recursive=false] - Whether to recurse into subdirectories.\n * @returns {Promise<string[]>} Array of absolute file paths.\n * @throws {Error} If the directory cannot be read.\n */\nexport async function listFiles(\n dirPath: string,\n recursive = false,\n): Promise<string[]> {\n const entries = await fsp.readdir(dirPath, { withFileTypes: true });\n const files: string[] = [];\n\n for (const entry of entries) {\n const fullPath = path.join(dirPath, entry.name);\n\n if (entry.isFile()) {\n files.push(fullPath);\n } else if (recursive && entry.isDirectory()) {\n const nestedFiles = await listFiles(fullPath, true);\n files.push(...nestedFiles);\n }\n }\n\n return files;\n}\n\n/**\n * Deletes a directory and all its contents recursively (like `rm -rf`).\n *\n * @param {string} dirPath - Directory to delete.\n * @returns {Promise<void>} Resolves when deletion is complete.\n * @throws {Error} If deletion fails.\n */\nexport async function deleteDirRecursive(dirPath: string): Promise<void> {\n await fsp.rm(dirPath, { recursive: true, force: true });\n}\n\n/**\n * Checks whether a given path is a directory.\n *\n * @param {string} pathStr - Path to check.\n * @returns {Promise<boolean>} True if the path is a directory, else false.\n */\nexport async function isDirectory(pathStr: string): Promise<boolean> {\n try {\n const stat = await fsp.stat(pathStr);\n return stat.isDirectory();\n } catch {\n return false;\n }\n}\n\n/**\n * Recursively copies a directory and all its contents to a destination.\n *\n * @param {string} src - Source directory path.\n * @param {string} dest - Destination directory path.\n * @returns {Promise<void>} Resolves when copy is complete.\n * @throws {Error} If source does not exist or copy fails.\n */\nexport async function copyDir(src: string, dest: string): Promise<void> {\n await ensureDir(dest);\n const entries = await fsp.readdir(src, { withFileTypes: true });\n\n for (const entry of entries) {\n const srcPath = path.join(src, entry.name);\n const destPath = path.join(dest, entry.name);\n\n if (entry.isDirectory()) {\n await copyDir(srcPath, destPath);\n } else {\n await fsp.copyFile(srcPath, destPath);\n }\n }\n}\n\n/**\n * Moves a directory to a new location by copying and deleting the original.\n *\n * @param {string} src - Source directory path.\n * @param {string} dest - Destination directory path.\n * @returns {Promise<void>} Resolves when move is complete.\n * @throws {Error} If copy or deletion fails.\n */\nexport async function moveDir(src: string, dest: string): Promise<void> {\n await copyDir(src, dest);\n await deleteDirRecursive(src);\n}\n\n/**\n * Empties a directory by deleting all files and subdirectories inside it.\n *\n * @param {string} dirPath - Path to the directory to empty.\n * @returns {Promise<void>} Resolves when the directory has been emptied.\n * @throws {Error} If files or subdirectories cannot be removed.\n */\nexport async function emptyDir(dirPath: string): Promise<void> {\n const entries = await fsp.readdir(dirPath, { withFileTypes: true });\n\n for (const entry of entries) {\n const entryPath = path.join(dirPath, entry.name);\n\n if (entry.isDirectory()) {\n await fsp.rm(entryPath, { recursive: true, force: true });\n } else {\n await fsp.unlink(entryPath);\n }\n }\n}\n\n/**\n * Calculates the total size (in bytes) of all files in a directory (recursive).\n *\n * @param {string} dirPath - Path to the directory.\n * @returns {Promise<number>} Total size in bytes.\n * @throws {Error} If any file stats cannot be read.\n */\nexport async function getDirSize(dirPath: string): Promise<number> {\n let total = 0;\n const entries = await fsp.readdir(dirPath, { withFileTypes: true });\n\n for (const entry of entries) {\n const entryPath = path.join(dirPath, entry.name);\n if (entry.isDirectory()) {\n total += await getDirSize(entryPath);\n } else {\n const stat = await fsp.stat(entryPath);\n total += stat.size;\n }\n }\n\n return total;\n}\n\n/**\n * Watches a directory for file changes and calls a callback on each event.\n *\n * @param {string} dirPath - Directory path to watch.\n * @param {(eventType: \"rename\" | \"change\", filename: string | null) => void} callback - Callback for each change event.\n * @returns {() => void} A function to stop watching the directory.\n */\nexport function watchDir(\n dirPath: string,\n callback: (eventType: \"rename\" | \"change\", filename: string | null) => void,\n): () => void {\n const watcher = fs.watch(dirPath, (eventType, filename) => {\n callback(eventType, filename);\n });\n\n return () => watcher.close();\n}\n"]}
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Enum representing valid application environments.
3
+ */
4
+ export declare enum Environment {
5
+ PRODUCTION = "production",
6
+ DEVELOPMENT = "development",
7
+ STAGING = "staging",
8
+ TESTING = "testing"
9
+ }
10
+ /**
11
+ * Utility class for accessing and managing environment variables.
12
+ * Provides typed getters with fallback defaults and validation.
13
+ */
14
+ export declare class Env {
15
+ /**
16
+ * Checks if the current NODE_ENV is 'development'.
17
+ *
18
+ * @returns {boolean} `true` if NODE_ENV is 'development', else `false`.
19
+ */
20
+ static isDev(): boolean;
21
+ /**
22
+ * Sets an environment variable (only affects runtime memory).
23
+ *
24
+ * @param {string} key - The environment variable key.
25
+ * @param {string} value - The value to set.
26
+ */
27
+ static set(key: string, value: string): void;
28
+ /**
29
+ * Returns all environment variables as an object.
30
+ *
31
+ * @returns {object} The current `process.env` object.
32
+ */
33
+ static getAll(): object;
34
+ /**
35
+ * Retrieves a string environment variable with a fallback default.
36
+ *
37
+ * @param {string} key - The environment variable key.
38
+ * @param {string} [defaultValue] - Value to return if the key is missing.
39
+ * @returns {string | undefined} The env value or the fallback.
40
+ */
41
+ static get(key: string, defaultValue?: string): string | undefined;
42
+ /**
43
+ * Retrieves a string environment variable and throws if it's missing.
44
+ *
45
+ * @param {string} key - The environment variable key.
46
+ * @returns {string} The environment variable's value.
47
+ * @throws {Error} If the variable is not defined.
48
+ */
49
+ static getRequired(key: string): string;
50
+ /**
51
+ * Retrieves an environment variable as a number, or returns a default.
52
+ *
53
+ * @param {string} key - The environment variable key.
54
+ * @param {number} defaultValue - Fallback number if key is not present.
55
+ * @returns {number} Parsed numeric value or default.
56
+ * @throws {Error} If the value is not a valid number.
57
+ */
58
+ static getNumber(key: string, defaultValue: number): number;
59
+ /**
60
+ * Retrieves a required environment variable as a number.
61
+ *
62
+ * @param {string} key - The environment variable key.
63
+ * @returns {number} Parsed number.
64
+ * @throws {Error} If the value is missing or not a number.
65
+ */
66
+ static getNumberRequired(key: string): number;
67
+ /**
68
+ * Retrieves an environment variable as a boolean.
69
+ * Accepts `true`, `1`, `yes`, `on` as true; `false`, `0`, `no`, `off` as false.
70
+ *
71
+ * @param {string} key - The environment variable key.
72
+ * @param {boolean} [defaultValue=false] - Optional fallback value if key is missing.
73
+ * @returns {boolean} Parsed boolean.
74
+ * @throws {Error} If the value is not a recognized boolean string.
75
+ */
76
+ static getBoolean(key: string, defaultValue?: boolean): boolean;
77
+ /**
78
+ * Retrieves a required environment variable as a boolean.
79
+ *
80
+ * @param {string} key - The environment variable key.
81
+ * @returns {boolean} Parsed boolean value.
82
+ * @throws {Error} If missing or invalid.
83
+ */
84
+ static getBooleanRequired(key: string): boolean;
85
+ /**
86
+ * Parses a stringified JSON object from an environment variable.
87
+ *
88
+ * @typeParam T - The type to parse as (defaults to `object`).
89
+ * @param {string} key - The environment variable key.
90
+ * @param {T} defaultValue - Value to return if key is missing.
91
+ * @returns {T} Parsed object or default.
92
+ * @throws {Error} If the value is not valid JSON.
93
+ */
94
+ static getJSON<T extends object = object>(key: string, defaultValue: T): T;
95
+ /**
96
+ * Parses a comma-separated string as an array.
97
+ *
98
+ * @typeParam T - The item type (optional, defaults to string).
99
+ * @param {string} key - The environment variable key.
100
+ * @param {T[]} [defaultValue=[]] - Array to return if value is empty or missing.
101
+ * @param {string} [splitter=','] - Delimiter to split on.
102
+ * @returns {string[] | T[]} An array of strings.
103
+ */
104
+ static getArray<T = string>(key: string, defaultValue?: T[], splitter?: string): string[] | T[];
105
+ /**
106
+ * Retrieves an enum-like environment variable value, validating against allowed values.
107
+ *
108
+ * @typeParam T - The allowed value type (string literal types).
109
+ * @param {string} key - The environment variable key.
110
+ * @param {T[]} allowedValues - Array of accepted string values.
111
+ * @param {T} [defaultValue] - Optional fallback value.
112
+ * @returns {T} The validated environment value.
113
+ * @throws {Error} If missing or invalid.
114
+ */
115
+ static getEnum<T extends string>(key: string, allowedValues: T[], defaultValue?: T): T;
116
+ /**
117
+ * Checks whether the specified environment variable exists.
118
+ *
119
+ * @param {string} key - The environment variable key.
120
+ * @returns {boolean} `true` if the variable is defined, otherwise `false`.
121
+ */
122
+ static has(key: string): boolean;
123
+ /**
124
+ * Deletes the given environment variable (useful in tests).
125
+ *
126
+ * @param {string} key - The environment variable key to delete.
127
+ * @returns {void}
128
+ */
129
+ static delete(key: string): void;
130
+ }
131
+ //# sourceMappingURL=env.utils.d.ts.map