@catbee/utils 1.0.5 → 2.0.0-next.0

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 (121) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +27 -27
  3. package/array/array.utils.d.ts +191 -0
  4. package/array/index.cjs +246 -0
  5. package/array/index.d.ts +25 -0
  6. package/array/index.mjs +228 -0
  7. package/async/async.utils.d.ts +296 -0
  8. package/async/index.cjs +428 -0
  9. package/async/index.d.ts +25 -0
  10. package/async/index.mjs +407 -0
  11. package/cache/cache.utils.d.ts +176 -0
  12. package/cache/index.cjs +292 -0
  13. package/cache/index.d.ts +25 -0
  14. package/cache/index.mjs +290 -0
  15. package/config/config.d.ts +57 -0
  16. package/config/index.cjs +136 -0
  17. package/config/index.d.ts +26 -0
  18. package/config/index.mjs +131 -0
  19. package/context-store/context-store.utils.d.ts +212 -0
  20. package/context-store/index.cjs +267 -0
  21. package/context-store/index.d.ts +25 -0
  22. package/context-store/index.mjs +261 -0
  23. package/crypto/crypto.utils.d.ts +183 -0
  24. package/crypto/index.cjs +182 -0
  25. package/crypto/index.d.ts +25 -0
  26. package/crypto/index.mjs +166 -0
  27. package/date/date.utils.d.ts +190 -0
  28. package/date/index.cjs +295 -0
  29. package/date/index.d.ts +25 -0
  30. package/date/index.mjs +283 -0
  31. package/decorators/decorators.utils.d.ts +705 -0
  32. package/decorators/index.cjs +913 -0
  33. package/decorators/index.d.ts +25 -0
  34. package/decorators/index.mjs +872 -0
  35. package/dir/dir.utils.d.ts +216 -0
  36. package/dir/index.cjs +416 -0
  37. package/dir/index.d.ts +25 -0
  38. package/dir/index.mjs +389 -0
  39. package/env/env.utils.d.ts +400 -0
  40. package/env/index.cjs +761 -0
  41. package/env/index.d.ts +25 -0
  42. package/env/index.mjs +758 -0
  43. package/exception/exception.utils.d.ts +253 -0
  44. package/exception/index.cjs +362 -0
  45. package/exception/index.d.ts +25 -0
  46. package/exception/index.mjs +338 -0
  47. package/fs/fs.utils.d.ts +196 -0
  48. package/fs/index.cjs +253 -0
  49. package/fs/index.d.ts +25 -0
  50. package/fs/index.mjs +228 -0
  51. package/http-status-codes/http-status-codes.d.ts +289 -0
  52. package/http-status-codes/index.cjs +96 -0
  53. package/http-status-codes/index.d.ts +25 -0
  54. package/http-status-codes/index.mjs +94 -0
  55. package/id/id.utils.d.ts +59 -0
  56. package/id/index.cjs +62 -0
  57. package/id/index.d.ts +25 -0
  58. package/id/index.mjs +56 -0
  59. package/index.cjs +218 -0
  60. package/index.d.ts +51 -0
  61. package/index.mjs +51 -0
  62. package/logger/index.cjs +334 -0
  63. package/logger/index.d.ts +25 -0
  64. package/logger/index.mjs +313 -0
  65. package/logger/logger.utils.d.ts +210 -0
  66. package/middleware/index.cjs +177 -0
  67. package/middleware/index.d.ts +25 -0
  68. package/middleware/index.mjs +170 -0
  69. package/middleware/middleware.utils.d.ts +123 -0
  70. package/obj/index.cjs +317 -0
  71. package/obj/index.d.ts +25 -0
  72. package/obj/index.mjs +301 -0
  73. package/obj/obj.utils.d.ts +156 -0
  74. package/package.json +172 -20
  75. package/performance/index.cjs +231 -0
  76. package/performance/index.d.ts +25 -0
  77. package/performance/index.mjs +225 -0
  78. package/performance/performance.utils.d.ts +159 -0
  79. package/request/index.cjs +202 -0
  80. package/request/index.d.ts +26 -0
  81. package/request/index.mjs +194 -0
  82. package/request/request.utils.d.ts +109 -0
  83. package/response/index.cjs +234 -0
  84. package/response/index.d.ts +26 -0
  85. package/response/index.mjs +222 -0
  86. package/response/response.utils.d.ts +186 -0
  87. package/server/index.cjs +1627 -0
  88. package/server/index.d.ts +28 -0
  89. package/server/index.mjs +1617 -0
  90. package/server/server.builder.d.ts +531 -0
  91. package/server/server.d.ts +303 -0
  92. package/stream/index.cjs +151 -0
  93. package/stream/index.d.ts +25 -0
  94. package/stream/index.mjs +144 -0
  95. package/stream/stream.utils.d.ts +111 -0
  96. package/string/index.cjs +109 -0
  97. package/string/index.d.ts +25 -0
  98. package/string/index.mjs +95 -0
  99. package/string/string.utils.d.ts +124 -0
  100. package/type/index.cjs +129 -0
  101. package/type/index.d.ts +25 -0
  102. package/type/index.mjs +119 -0
  103. package/type/type.utils.d.ts +129 -0
  104. package/types/api-response.d.ts +175 -0
  105. package/types/common.d.ts +148 -0
  106. package/types/config.d.ts +88 -0
  107. package/types/index.cjs +34 -0
  108. package/types/index.d.ts +28 -0
  109. package/types/index.mjs +32 -0
  110. package/types/server.d.ts +291 -0
  111. package/url/index.cjs +201 -0
  112. package/url/index.d.ts +25 -0
  113. package/url/index.mjs +189 -0
  114. package/url/url.utils.d.ts +164 -0
  115. package/validate/index.cjs +212 -0
  116. package/validate/index.d.ts +25 -0
  117. package/validate/index.mjs +188 -0
  118. package/validate/validate.utils.d.ts +200 -0
  119. package/build/index.cjs +0 -7579
  120. package/build/index.d.ts +0 -5774
  121. package/build/index.mjs +0 -7274
package/dir/index.mjs ADDED
@@ -0,0 +1,389 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import os from 'os';
26
+ import fs from 'fs';
27
+ import fsp from 'fs/promises';
28
+ import path from 'path';
29
+ import { randomBytes } from 'crypto';
30
+
31
+ var __defProp = Object.defineProperty;
32
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
33
+ async function ensureDir(dirPath) {
34
+ await fsp.mkdir(dirPath, {
35
+ recursive: true
36
+ });
37
+ }
38
+ __name(ensureDir, "ensureDir");
39
+ async function listFiles(dirPath, recursive = false) {
40
+ const entries = await fsp.readdir(dirPath, {
41
+ withFileTypes: true
42
+ });
43
+ const files = [];
44
+ for (const entry of entries) {
45
+ const fullPath = path.join(dirPath, entry.name);
46
+ if (entry.isFile()) {
47
+ files.push(fullPath);
48
+ } else if (recursive && entry.isDirectory()) {
49
+ const nestedFiles = await listFiles(fullPath, true);
50
+ files.push(...nestedFiles);
51
+ }
52
+ }
53
+ return files;
54
+ }
55
+ __name(listFiles, "listFiles");
56
+ async function deleteDirRecursive(dirPath) {
57
+ await fsp.rm(dirPath, {
58
+ recursive: true,
59
+ force: true
60
+ });
61
+ }
62
+ __name(deleteDirRecursive, "deleteDirRecursive");
63
+ async function isDirectory(pathStr) {
64
+ try {
65
+ const stat = await fsp.stat(pathStr);
66
+ return stat.isDirectory();
67
+ } catch {
68
+ return false;
69
+ }
70
+ }
71
+ __name(isDirectory, "isDirectory");
72
+ async function copyDir(src, dest) {
73
+ await ensureDir(dest);
74
+ const entries = await fsp.readdir(src, {
75
+ withFileTypes: true
76
+ });
77
+ for (const entry of entries) {
78
+ const srcPath = path.join(src, entry.name);
79
+ const destPath = path.join(dest, entry.name);
80
+ if (entry.isDirectory()) {
81
+ await copyDir(srcPath, destPath);
82
+ } else {
83
+ await fsp.copyFile(srcPath, destPath);
84
+ }
85
+ }
86
+ }
87
+ __name(copyDir, "copyDir");
88
+ async function moveDir(src, dest) {
89
+ await copyDir(src, dest);
90
+ await deleteDirRecursive(src);
91
+ }
92
+ __name(moveDir, "moveDir");
93
+ async function emptyDir(dirPath) {
94
+ const entries = await fsp.readdir(dirPath, {
95
+ withFileTypes: true
96
+ });
97
+ for (const entry of entries) {
98
+ const entryPath = path.join(dirPath, entry.name);
99
+ if (entry.isDirectory()) {
100
+ await fsp.rm(entryPath, {
101
+ recursive: true,
102
+ force: true
103
+ });
104
+ } else {
105
+ await fsp.unlink(entryPath);
106
+ }
107
+ }
108
+ }
109
+ __name(emptyDir, "emptyDir");
110
+ async function getDirSize(dirPath) {
111
+ let total = 0;
112
+ const entries = await fsp.readdir(dirPath, {
113
+ withFileTypes: true
114
+ });
115
+ for (const entry of entries) {
116
+ const entryPath = path.join(dirPath, entry.name);
117
+ if (entry.isDirectory()) {
118
+ total += await getDirSize(entryPath);
119
+ } else {
120
+ const stat = await fsp.stat(entryPath);
121
+ total += stat.size;
122
+ }
123
+ }
124
+ return total;
125
+ }
126
+ __name(getDirSize, "getDirSize");
127
+ function watchDir(dirPath, callback) {
128
+ const watcher = fs.watch(dirPath, (eventType, filename) => {
129
+ callback(eventType, filename);
130
+ });
131
+ return () => watcher.close();
132
+ }
133
+ __name(watchDir, "watchDir");
134
+ async function findFilesByPattern(pattern, options = {}) {
135
+ const matchPattern = /* @__PURE__ */ __name((name, pattern2) => {
136
+ let escaped = pattern2.replaceAll(/[.+^${}()|[\]\\]/g, "\\$&");
137
+ escaped = escaped.replaceAll("*", ".*").replaceAll("?", ".");
138
+ const regex = new RegExp(`^${escaped}$`);
139
+ return regex.test(name);
140
+ }, "matchPattern");
141
+ const { cwd = process.cwd(), dot = false } = options;
142
+ const segments = pattern.split("/");
143
+ async function walk(dir, segIndex) {
144
+ if (segIndex >= segments.length) return [];
145
+ const segment = segments[segIndex];
146
+ const entries = await fs.promises.readdir(dir, {
147
+ withFileTypes: true
148
+ });
149
+ let matchedFiles = [];
150
+ for (const entry of entries) {
151
+ const entryName = entry.name;
152
+ if (!dot && entryName.startsWith(".")) continue;
153
+ const fullPath = path.join(dir, entryName);
154
+ const isLastSegment = segIndex === segments.length - 1;
155
+ if (isLastSegment) {
156
+ if (matchPattern(entryName, segment) && entry.isFile()) {
157
+ matchedFiles.push(fullPath);
158
+ }
159
+ } else if (entry.isDirectory()) {
160
+ if (matchPattern(entryName, segment)) {
161
+ matchedFiles.push(...await walk(fullPath, segIndex + 1));
162
+ }
163
+ }
164
+ }
165
+ return matchedFiles;
166
+ }
167
+ __name(walk, "walk");
168
+ return walk(cwd, 0);
169
+ }
170
+ __name(findFilesByPattern, "findFilesByPattern");
171
+ async function getSubdirectories(dirPath, recursive = false) {
172
+ const entries = await fsp.readdir(dirPath, {
173
+ withFileTypes: true
174
+ });
175
+ const dirs = entries.filter((entry) => entry.isDirectory()).map((entry) => path.join(dirPath, entry.name));
176
+ if (recursive && dirs.length > 0) {
177
+ const nestedDirs = await Promise.all(dirs.map((dir) => getSubdirectories(dir, true)));
178
+ return [
179
+ ...dirs,
180
+ ...nestedDirs.flat()
181
+ ];
182
+ }
183
+ return dirs;
184
+ }
185
+ __name(getSubdirectories, "getSubdirectories");
186
+ async function ensureEmptyDir(dirPath) {
187
+ if (await isDirectory(dirPath)) {
188
+ await emptyDir(dirPath);
189
+ } else {
190
+ try {
191
+ await fsp.unlink(dirPath);
192
+ } catch (error) {
193
+ if (error.code !== "ENOENT") {
194
+ throw error;
195
+ }
196
+ }
197
+ await ensureDir(dirPath);
198
+ }
199
+ }
200
+ __name(ensureEmptyDir, "ensureEmptyDir");
201
+ async function createTempDir(options = {}) {
202
+ const prefix = options.prefix || "tmp-";
203
+ const parentDir = options.parentDir || os.tmpdir();
204
+ const dirName = `${prefix}${randomBytes(6).toString("hex")}`;
205
+ const tempDirPath = path.join(parentDir, dirName);
206
+ await ensureDir(tempDirPath);
207
+ const cleanup = /* @__PURE__ */ __name(async () => {
208
+ try {
209
+ await deleteDirRecursive(tempDirPath);
210
+ } catch {
211
+ }
212
+ }, "cleanup");
213
+ if (options.cleanup) {
214
+ process.once("exit", () => {
215
+ try {
216
+ fs.rmSync(tempDirPath, {
217
+ recursive: true,
218
+ force: true
219
+ });
220
+ } catch {
221
+ }
222
+ });
223
+ const signals = [
224
+ "SIGINT",
225
+ "SIGTERM",
226
+ "SIGUSR1",
227
+ "SIGUSR2"
228
+ ];
229
+ for (const signal of signals) {
230
+ process.once(signal, () => {
231
+ cleanup().then(() => process.exit());
232
+ });
233
+ }
234
+ }
235
+ return {
236
+ path: tempDirPath,
237
+ cleanup
238
+ };
239
+ }
240
+ __name(createTempDir, "createTempDir");
241
+ async function findNewestFile(dirPath, recursive = false) {
242
+ const files = await listFiles(dirPath, recursive);
243
+ if (files.length === 0) return null;
244
+ const stats = await Promise.all(files.map(async (file) => ({
245
+ path: file,
246
+ mtime: (await fsp.stat(file)).mtime
247
+ })));
248
+ return stats.sort((a, b) => b.mtime.getTime() - a.mtime.getTime())[0].path;
249
+ }
250
+ __name(findNewestFile, "findNewestFile");
251
+ async function findOldestFile(dirPath, recursive = false) {
252
+ const files = await listFiles(dirPath, recursive);
253
+ if (files.length === 0) return null;
254
+ const stats = await Promise.all(files.map(async (file) => ({
255
+ path: file,
256
+ mtime: (await fsp.stat(file)).mtime
257
+ })));
258
+ return stats.sort((a, b) => a.mtime.getTime() - b.mtime.getTime())[0].path;
259
+ }
260
+ __name(findOldestFile, "findOldestFile");
261
+ async function findInDir(dirPath, predicate, recursive = false) {
262
+ const entries = await fsp.readdir(dirPath, {
263
+ withFileTypes: true
264
+ });
265
+ const results = [];
266
+ for (const entry of entries) {
267
+ const entryPath = path.join(dirPath, entry.name);
268
+ const stat = await fsp.stat(entryPath);
269
+ if (await predicate(entryPath, stat)) {
270
+ results.push(entryPath);
271
+ }
272
+ if (recursive && entry.isDirectory()) {
273
+ const nestedResults = await findInDir(entryPath, predicate, true);
274
+ results.push(...nestedResults);
275
+ }
276
+ }
277
+ return results;
278
+ }
279
+ __name(findInDir, "findInDir");
280
+ async function watchDirRecursive(dirPath, callback, includeSubdirs = true) {
281
+ const normalizedBasePath = path.normalize(dirPath);
282
+ const watchers = [];
283
+ const addWatcher = /* @__PURE__ */ __name((dir) => {
284
+ try {
285
+ const watcher = fs.watch(dir, (eventType, filename) => {
286
+ if (!filename) return;
287
+ const fullPath = path.join(dir, filename);
288
+ const relativePath = path.relative(normalizedBasePath, fullPath);
289
+ callback(eventType, relativePath);
290
+ if (eventType === "rename" && includeSubdirs) {
291
+ setTimeout(async () => {
292
+ try {
293
+ if (await isDirectory(fullPath)) {
294
+ addWatcher(fullPath);
295
+ }
296
+ } catch {
297
+ }
298
+ }, 100);
299
+ }
300
+ });
301
+ watchers.push(watcher);
302
+ } catch {
303
+ }
304
+ }, "addWatcher");
305
+ addWatcher(normalizedBasePath);
306
+ if (includeSubdirs) {
307
+ const subdirs = await getSubdirectories(normalizedBasePath, true);
308
+ for (const subdir of subdirs) {
309
+ addWatcher(subdir);
310
+ }
311
+ }
312
+ return () => {
313
+ for (const watcher of watchers) {
314
+ watcher.close();
315
+ }
316
+ };
317
+ }
318
+ __name(watchDirRecursive, "watchDirRecursive");
319
+ async function getDirStats(dirPath) {
320
+ let fileCount = 0;
321
+ let dirCount = 0;
322
+ let totalSize = 0;
323
+ async function processDir(currentPath) {
324
+ const entries = await fsp.readdir(currentPath, {
325
+ withFileTypes: true
326
+ });
327
+ for (const entry of entries) {
328
+ const entryPath = path.join(currentPath, entry.name);
329
+ if (entry.isDirectory()) {
330
+ dirCount++;
331
+ await processDir(entryPath);
332
+ } else if (entry.isFile()) {
333
+ fileCount++;
334
+ const stat = await fsp.stat(entryPath);
335
+ totalSize += stat.size;
336
+ }
337
+ }
338
+ }
339
+ __name(processDir, "processDir");
340
+ await processDir(dirPath);
341
+ return {
342
+ fileCount,
343
+ dirCount,
344
+ totalSize
345
+ };
346
+ }
347
+ __name(getDirStats, "getDirStats");
348
+ async function walkDir(dirPath, options) {
349
+ const traversalOrder = options.traversalOrder || "pre";
350
+ const entries = await fsp.readdir(dirPath, {
351
+ withFileTypes: true
352
+ });
353
+ for (const entry of entries) {
354
+ const entryPath = path.join(dirPath, entry.name);
355
+ const stats = await fsp.stat(entryPath);
356
+ const entryInfo = {
357
+ path: entryPath,
358
+ name: entry.name,
359
+ isDirectory: entry.isDirectory(),
360
+ stats
361
+ };
362
+ if (entry.isDirectory()) {
363
+ if (traversalOrder === "pre") {
364
+ const shouldContinue = await options.visitorFn(entryInfo);
365
+ if (shouldContinue !== false) {
366
+ await walkDir(entryPath, options);
367
+ }
368
+ } else {
369
+ await walkDir(entryPath, options);
370
+ await options.visitorFn(entryInfo);
371
+ }
372
+ } else {
373
+ await options.visitorFn(entryInfo);
374
+ }
375
+ }
376
+ if (traversalOrder === "post") {
377
+ const stats = await fsp.stat(dirPath);
378
+ const entryInfo = {
379
+ path: dirPath,
380
+ name: path.basename(dirPath),
381
+ isDirectory: true,
382
+ stats
383
+ };
384
+ await options.visitorFn(entryInfo);
385
+ }
386
+ }
387
+ __name(walkDir, "walkDir");
388
+
389
+ export { copyDir, createTempDir, deleteDirRecursive, emptyDir, ensureDir, ensureEmptyDir, findFilesByPattern, findInDir, findNewestFile, findOldestFile, getDirSize, getDirStats, getSubdirectories, isDirectory, listFiles, moveDir, walkDir, watchDir, watchDirRecursive };