@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,163 +0,0 @@
1
- import { BufferEncoding } from './crypto.utils';
2
- import { Stats } from 'fs';
3
- /**
4
- * Checks whether a file or directory exists at the given path.
5
- *
6
- * @param {string} path - The file or directory path to check.
7
- * @returns {Promise<boolean>} Resolves to `true` if the path exists, `false` otherwise.
8
- */
9
- export declare function fileExists(path: string): Promise<boolean>;
10
- /**
11
- * Reads and parses a JSON file from the specified path.
12
- *
13
- * @typeParam T - The expected type of the parsed JSON object.
14
- * @param {string} path - The path to the JSON file.
15
- * @returns {Promise<T | null>} The parsed object, or `null` if reading or parsing fails.
16
- */
17
- export declare function readJsonFile<T = any>(path: string): Promise<T | null>;
18
- /**
19
- * Writes a JavaScript object to a file as formatted JSON.
20
- *
21
- * @param {string} path - The destination file path.
22
- * @param {any} data - The object to serialize and write.
23
- * @param {number} [space=2] - Number of spaces for JSON formatting.
24
- * @returns {Promise<void>} Resolves when writing is complete.
25
- */
26
- export declare function writeJsonFile(path: string, data: any, space?: number): Promise<void>;
27
- /**
28
- * Deletes a file if it exists.
29
- *
30
- * @param {string} path - The file path to delete.
31
- * @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.
32
- */
33
- export declare function deleteFileIfExists(path: string): Promise<boolean>;
34
- /**
35
- * Reads a text file from the specified path.
36
- *
37
- * @param {string} path - The path to the text file.
38
- * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
39
- * @returns {Promise<string | null>} The file contents, or `null` if reading fails.
40
- */
41
- export declare function readTextFile(path: string, encoding?: BufferEncoding): Promise<string | null>;
42
- /**
43
- * Writes text content to a file.
44
- *
45
- * @param {string} path - The destination file path.
46
- * @param {string} content - The text content to write.
47
- * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
48
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
49
- */
50
- export declare function writeTextFile(path: string, content: string, encoding?: BufferEncoding): Promise<boolean>;
51
- /**
52
- * Appends text content to a file.
53
- *
54
- * @param {string} path - The file path to append to.
55
- * @param {string} content - The text content to append.
56
- * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
57
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
58
- */
59
- export declare function appendTextFile(path: string, content: string, encoding?: BufferEncoding): Promise<boolean>;
60
- /**
61
- * Copies a file from source to destination.
62
- *
63
- * @param {string} source - Source file path.
64
- * @param {string} destination - Destination file path.
65
- * @param {boolean} [overwrite=false] - Whether to overwrite if destination exists.
66
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
67
- */
68
- export declare function copyFile(source: string, destination: string, overwrite?: boolean): Promise<boolean>;
69
- /**
70
- * Renames/moves a file.
71
- *
72
- * @param {string} oldPath - Current file path.
73
- * @param {string} newPath - New file path.
74
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
75
- */
76
- export declare function moveFile(oldPath: string, newPath: string): Promise<boolean>;
77
- /**
78
- * Gets file stats if the file exists.
79
- *
80
- * @param {string} path - Path to the file.
81
- * @returns {Promise<fs.Stats | null>} File stats object or null if file doesn't exist.
82
- */
83
- export declare function getFileStats(path: string): Promise<Stats | null>;
84
- /**
85
- * Creates a temporary file with optional content.
86
- *
87
- * @param {object} [options] - Options for creating the temp file.
88
- * @param {string} [options.prefix="tmp-"] - Filename prefix.
89
- * @param {string} [options.extension=""] - File extension.
90
- * @param {string} [options.dir] - Directory to create the file in (defaults to OS temp dir).
91
- * @param {string | Buffer} [options.content] - Optional content to write to the file.
92
- * @returns {Promise<string>} Path to the created temporary file.
93
- */
94
- export declare function createTempFile({ prefix, extension, dir, content }?: {
95
- prefix?: string;
96
- extension?: string;
97
- dir?: string;
98
- content?: string | Buffer;
99
- }): Promise<string>;
100
- /**
101
- * Streams a file from source to destination.
102
- * Useful for large files to avoid loading the entire file into memory.
103
- *
104
- * @param {string} source - Source file path.
105
- * @param {string} destination - Destination file path.
106
- * @returns {Promise<void>} Resolves when streaming completes.
107
- * @throws {Error} If streaming fails.
108
- */
109
- export declare function streamFile(source: string, destination: string): Promise<void>;
110
- /**
111
- * Reads a directory and returns file names.
112
- *
113
- * @param {string} dirPath - Path to the directory.
114
- * @param {object} [options] - Options for reading the directory.
115
- * @param {boolean} [options.fullPaths=false] - Whether to return full paths.
116
- * @param {RegExp} [options.filter] - Optional regex to filter files.
117
- * @returns {Promise<string[]>} Array of file names or paths.
118
- * @throws {Error} If directory cannot be read.
119
- */
120
- export declare function readDirectory(dirPath: string, options?: {
121
- fullPaths?: boolean;
122
- filter?: RegExp;
123
- }): Promise<string[]>;
124
- /**
125
- * Creates a directory if it doesn't exist.
126
- *
127
- * @param {string} dirPath - Path to the directory.
128
- * @param {boolean} [recursive=true] - Whether to create parent directories.
129
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
130
- */
131
- export declare function createDirectory(dirPath: string, recursive?: boolean): Promise<boolean>;
132
- /**
133
- * Safely reads and parses a JSON file with error details.
134
- *
135
- * @typeParam T - The expected type of the parsed JSON object.
136
- * @param {string} path - The path to the JSON file.
137
- * @returns {Promise<{ data: T | null; error: Error | null }>} Object with data and error properties.
138
- */
139
- export declare function safeReadJsonFile<T = any>(path: string): Promise<{
140
- data: T | null;
141
- error: Error | null;
142
- }>;
143
- /**
144
- * Checks if a path points to a file (not a directory).
145
- *
146
- * @param {string} path - Path to check.
147
- * @returns {Promise<boolean>} True if the path is a file, false otherwise.
148
- */
149
- export declare function isFile(path: string): Promise<boolean>;
150
- /**
151
- * Gets the size of a file in bytes.
152
- *
153
- * @param {string} path - Path to the file.
154
- * @returns {Promise<number>} Size in bytes or -1 if file doesn't exist.
155
- */
156
- export declare function getFileSize(path: string): Promise<number>;
157
- /**
158
- * Reads a file as a Buffer.
159
- *
160
- * @param {string} path - Path to the file.
161
- * @returns {Promise<Buffer | null>} File contents as Buffer or null if reading fails.
162
- */
163
- export declare function readFileBuffer(path: string): Promise<Buffer | null>;
@@ -1,371 +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
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.fileExists = fileExists;
30
- exports.readJsonFile = readJsonFile;
31
- exports.writeJsonFile = writeJsonFile;
32
- exports.deleteFileIfExists = deleteFileIfExists;
33
- exports.readTextFile = readTextFile;
34
- exports.writeTextFile = writeTextFile;
35
- exports.appendTextFile = appendTextFile;
36
- exports.copyFile = copyFile;
37
- exports.moveFile = moveFile;
38
- exports.getFileStats = getFileStats;
39
- exports.createTempFile = createTempFile;
40
- exports.streamFile = streamFile;
41
- exports.readDirectory = readDirectory;
42
- exports.createDirectory = createDirectory;
43
- exports.safeReadJsonFile = safeReadJsonFile;
44
- exports.isFile = isFile;
45
- exports.getFileSize = getFileSize;
46
- exports.readFileBuffer = readFileBuffer;
47
- const promises_1 = __importDefault(require("fs/promises"));
48
- const fs_1 = require("fs");
49
- const os_1 = require("os");
50
- const path_1 = __importDefault(require("path"));
51
- const promises_2 = require("stream/promises");
52
- const crypto_1 = require("crypto");
53
- /**
54
- * Checks whether a file or directory exists at the given path.
55
- *
56
- * @param {string} path - The file or directory path to check.
57
- * @returns {Promise<boolean>} Resolves to `true` if the path exists, `false` otherwise.
58
- */
59
- async function fileExists(path) {
60
- try {
61
- await promises_1.default.access(path);
62
- return true;
63
- }
64
- catch {
65
- return false;
66
- }
67
- }
68
- /**
69
- * Reads and parses a JSON file from the specified path.
70
- *
71
- * @typeParam T - The expected type of the parsed JSON object.
72
- * @param {string} path - The path to the JSON file.
73
- * @returns {Promise<T | null>} The parsed object, or `null` if reading or parsing fails.
74
- */
75
- async function readJsonFile(path) {
76
- try {
77
- const data = await promises_1.default.readFile(path, 'utf-8');
78
- return JSON.parse(data);
79
- }
80
- catch {
81
- return null;
82
- }
83
- }
84
- /**
85
- * Writes a JavaScript object to a file as formatted JSON.
86
- *
87
- * @param {string} path - The destination file path.
88
- * @param {any} data - The object to serialize and write.
89
- * @param {number} [space=2] - Number of spaces for JSON formatting.
90
- * @returns {Promise<void>} Resolves when writing is complete.
91
- */
92
- async function writeJsonFile(path, data, space = 2) {
93
- const json = JSON.stringify(data, null, space);
94
- await promises_1.default.writeFile(path, json, 'utf-8');
95
- }
96
- /**
97
- * Deletes a file if it exists.
98
- *
99
- * @param {string} path - The file path to delete.
100
- * @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.
101
- */
102
- async function deleteFileIfExists(path) {
103
- try {
104
- await promises_1.default.unlink(path);
105
- return true;
106
- }
107
- catch (err) {
108
- if (err.code === 'ENOENT')
109
- return true;
110
- return false;
111
- }
112
- }
113
- /**
114
- * Reads a text file from the specified path.
115
- *
116
- * @param {string} path - The path to the text file.
117
- * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
118
- * @returns {Promise<string | null>} The file contents, or `null` if reading fails.
119
- */
120
- async function readTextFile(path, encoding = 'utf-8') {
121
- try {
122
- return await promises_1.default.readFile(path, encoding);
123
- }
124
- catch {
125
- return null;
126
- }
127
- }
128
- /**
129
- * Writes text content to a file.
130
- *
131
- * @param {string} path - The destination file path.
132
- * @param {string} content - The text content to write.
133
- * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
134
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
135
- */
136
- async function writeTextFile(path, content, encoding = 'utf-8') {
137
- try {
138
- await promises_1.default.writeFile(path, content, encoding);
139
- return true;
140
- }
141
- catch {
142
- return false;
143
- }
144
- }
145
- /**
146
- * Appends text content to a file.
147
- *
148
- * @param {string} path - The file path to append to.
149
- * @param {string} content - The text content to append.
150
- * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
151
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
152
- */
153
- async function appendTextFile(path, content, encoding = 'utf-8') {
154
- try {
155
- await promises_1.default.appendFile(path, content, encoding);
156
- return true;
157
- }
158
- catch {
159
- return false;
160
- }
161
- }
162
- /**
163
- * Copies a file from source to destination.
164
- *
165
- * @param {string} source - Source file path.
166
- * @param {string} destination - Destination file path.
167
- * @param {boolean} [overwrite=false] - Whether to overwrite if destination exists.
168
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
169
- */
170
- async function copyFile(source, destination, overwrite = false) {
171
- try {
172
- const flags = overwrite ? 0 : promises_1.default.constants.COPYFILE_EXCL;
173
- await promises_1.default.copyFile(source, destination, flags);
174
- return true;
175
- }
176
- catch {
177
- return false;
178
- }
179
- }
180
- /**
181
- * Renames/moves a file.
182
- *
183
- * @param {string} oldPath - Current file path.
184
- * @param {string} newPath - New file path.
185
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
186
- */
187
- async function moveFile(oldPath, newPath) {
188
- try {
189
- await promises_1.default.rename(oldPath, newPath);
190
- return true;
191
- }
192
- catch {
193
- // If rename fails (e.g., across devices), try copy+delete
194
- try {
195
- await promises_1.default.copyFile(oldPath, newPath);
196
- await promises_1.default.unlink(oldPath);
197
- return true;
198
- }
199
- catch {
200
- return false;
201
- }
202
- }
203
- }
204
- /**
205
- * Gets file stats if the file exists.
206
- *
207
- * @param {string} path - Path to the file.
208
- * @returns {Promise<fs.Stats | null>} File stats object or null if file doesn't exist.
209
- */
210
- async function getFileStats(path) {
211
- try {
212
- return await promises_1.default.stat(path);
213
- }
214
- catch {
215
- return null;
216
- }
217
- }
218
- /**
219
- * Creates a temporary file with optional content.
220
- *
221
- * @param {object} [options] - Options for creating the temp file.
222
- * @param {string} [options.prefix="tmp-"] - Filename prefix.
223
- * @param {string} [options.extension=""] - File extension.
224
- * @param {string} [options.dir] - Directory to create the file in (defaults to OS temp dir).
225
- * @param {string | Buffer} [options.content] - Optional content to write to the file.
226
- * @returns {Promise<string>} Path to the created temporary file.
227
- */
228
- async function createTempFile({ prefix = 'tmp-', extension = '', dir = (0, os_1.tmpdir)(), content } = {}) {
229
- const ext = extension ? (extension.startsWith('.') ? extension : `.${extension}`) : '';
230
- const filename = `${prefix}${(0, crypto_1.randomUUID)()}${ext}`;
231
- const filepath = path_1.default.join(dir, filename);
232
- if (content) {
233
- await promises_1.default.writeFile(filepath, content);
234
- }
235
- else {
236
- await promises_1.default.writeFile(filepath, '');
237
- }
238
- return filepath;
239
- }
240
- /**
241
- * Streams a file from source to destination.
242
- * Useful for large files to avoid loading the entire file into memory.
243
- *
244
- * @param {string} source - Source file path.
245
- * @param {string} destination - Destination file path.
246
- * @returns {Promise<void>} Resolves when streaming completes.
247
- * @throws {Error} If streaming fails.
248
- */
249
- async function streamFile(source, destination) {
250
- const readStream = (0, fs_1.createReadStream)(source);
251
- const writeStream = (0, fs_1.createWriteStream)(destination);
252
- try {
253
- await (0, promises_2.pipeline)(readStream, writeStream);
254
- }
255
- catch (error) {
256
- // Ensure both streams are closed in case of error
257
- readStream.destroy();
258
- writeStream.destroy();
259
- throw error;
260
- }
261
- }
262
- /**
263
- * Reads a directory and returns file names.
264
- *
265
- * @param {string} dirPath - Path to the directory.
266
- * @param {object} [options] - Options for reading the directory.
267
- * @param {boolean} [options.fullPaths=false] - Whether to return full paths.
268
- * @param {RegExp} [options.filter] - Optional regex to filter files.
269
- * @returns {Promise<string[]>} Array of file names or paths.
270
- * @throws {Error} If directory cannot be read.
271
- */
272
- async function readDirectory(dirPath, options = {}) {
273
- const files = await promises_1.default.readdir(dirPath);
274
- let result = files;
275
- if (options.filter) {
276
- result = result.filter(file => options.filter.test(file));
277
- }
278
- if (options.fullPaths) {
279
- result = result.map(file => path_1.default.join(dirPath, file));
280
- }
281
- return result;
282
- }
283
- /**
284
- * Creates a directory if it doesn't exist.
285
- *
286
- * @param {string} dirPath - Path to the directory.
287
- * @param {boolean} [recursive=true] - Whether to create parent directories.
288
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
289
- */
290
- async function createDirectory(dirPath, recursive = true) {
291
- try {
292
- await promises_1.default.mkdir(dirPath, { recursive });
293
- return true;
294
- }
295
- catch (err) {
296
- // Consider it success if directory already exists
297
- return err.code === 'EEXIST';
298
- }
299
- }
300
- /**
301
- * Safely reads and parses a JSON file with error details.
302
- *
303
- * @typeParam T - The expected type of the parsed JSON object.
304
- * @param {string} path - The path to the JSON file.
305
- * @returns {Promise<{ data: T | null; error: Error | null }>} Object with data and error properties.
306
- */
307
- async function safeReadJsonFile(path) {
308
- try {
309
- const content = await promises_1.default.readFile(path, 'utf-8');
310
- try {
311
- const data = JSON.parse(content);
312
- return { data, error: null };
313
- }
314
- catch (error) {
315
- return {
316
- data: null,
317
- error: new Error(`Invalid JSON in file ${path}: ${error.message}`)
318
- };
319
- }
320
- }
321
- catch (error) {
322
- return {
323
- data: null,
324
- error: new Error(`Failed to read file ${path}: ${error.message}`)
325
- };
326
- }
327
- }
328
- /**
329
- * Checks if a path points to a file (not a directory).
330
- *
331
- * @param {string} path - Path to check.
332
- * @returns {Promise<boolean>} True if the path is a file, false otherwise.
333
- */
334
- async function isFile(path) {
335
- try {
336
- const stat = await promises_1.default.stat(path);
337
- return stat.isFile();
338
- }
339
- catch {
340
- return false;
341
- }
342
- }
343
- /**
344
- * Gets the size of a file in bytes.
345
- *
346
- * @param {string} path - Path to the file.
347
- * @returns {Promise<number>} Size in bytes or -1 if file doesn't exist.
348
- */
349
- async function getFileSize(path) {
350
- try {
351
- const stat = await promises_1.default.stat(path);
352
- return stat.size;
353
- }
354
- catch {
355
- return -1;
356
- }
357
- }
358
- /**
359
- * Reads a file as a Buffer.
360
- *
361
- * @param {string} path - Path to the file.
362
- * @returns {Promise<Buffer | null>} File contents as Buffer or null if reading fails.
363
- */
364
- async function readFileBuffer(path) {
365
- try {
366
- return await promises_1.default.readFile(path);
367
- }
368
- catch {
369
- return null;
370
- }
371
- }