@catbee/utils 2.0.0-next.0 → 2.0.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.
- package/LICENSE +1 -1
- package/README.md +102 -52
- package/array/index.cjs +215 -74
- package/array/index.d.ts +345 -2
- package/array/index.mjs +201 -74
- package/async/index.cjs +116 -39
- package/async/index.d.ts +292 -2
- package/async/index.mjs +116 -40
- package/cache/index.cjs +2 -2
- package/cache/index.d.ts +156 -2
- package/cache/index.mjs +3 -3
- package/config/index.cjs +80 -66
- package/config/index.d.ts +65 -3
- package/config/index.mjs +77 -65
- package/context-store/index.cjs +2 -3
- package/context-store/index.d.ts +193 -2
- package/context-store/index.mjs +2 -3
- package/crypto/index.cjs +55 -5
- package/crypto/index.d.ts +225 -2
- package/crypto/index.mjs +52 -7
- package/date/index.cjs +676 -2
- package/date/index.d.ts +676 -2
- package/date/index.mjs +665 -3
- package/decorator/index.cjs +2172 -0
- package/{decorators/decorators.utils.d.ts → decorator/index.d.ts} +58 -54
- package/decorator/index.mjs +2131 -0
- package/{dir → directory}/index.cjs +5 -4
- package/{dir/dir.utils.d.ts → directory/index.d.ts} +24 -21
- package/{dir → directory}/index.mjs +5 -4
- package/env/index.cjs +100 -68
- package/env/index.d.ts +391 -2
- package/env/index.mjs +100 -68
- package/exception/index.cjs +1 -1
- package/exception/index.d.ts +233 -2
- package/exception/index.mjs +1 -1
- package/fs/index.cjs +71 -37
- package/fs/index.d.ts +206 -2
- package/fs/index.mjs +65 -35
- package/http-status-codes/index.cjs +1 -1
- package/http-status-codes/index.d.ts +268 -2
- package/http-status-codes/index.mjs +1 -1
- package/id/index.cjs +1 -1
- package/id/index.d.ts +38 -2
- package/id/index.mjs +1 -1
- package/index.cjs +13 -13
- package/index.d.ts +5 -5
- package/index.mjs +5 -5
- package/logger/index.cjs +13 -15
- package/logger/index.d.ts +190 -2
- package/logger/index.mjs +14 -16
- package/middleware/index.cjs +1 -1
- package/middleware/index.d.ts +104 -2
- package/middleware/index.mjs +1 -1
- package/object/index.cjs +379 -0
- package/{obj/obj.utils.d.ts → object/index.d.ts} +73 -33
- package/object/index.mjs +360 -0
- package/package.json +41 -23
- package/performance/index.cjs +4 -4
- package/performance/index.d.ts +139 -2
- package/performance/index.mjs +4 -4
- package/request/index.cjs +37 -25
- package/request/index.d.ts +242 -3
- package/request/index.mjs +37 -25
- package/response/index.cjs +1 -1
- package/response/index.d.ts +319 -3
- package/response/index.mjs +1 -1
- package/server/index.cjs +249 -146
- package/server/index.d.ts +866 -5
- package/server/index.mjs +248 -144
- package/stream/index.cjs +1 -1
- package/stream/index.d.ts +91 -2
- package/stream/index.mjs +1 -1
- package/string/index.cjs +34 -1
- package/string/index.d.ts +146 -2
- package/string/index.mjs +31 -2
- package/type/index.cjs +19 -2
- package/type/index.d.ts +144 -2
- package/type/index.mjs +17 -3
- package/types/index.cjs +1 -1
- package/types/index.d.ts +775 -5
- package/types/index.mjs +1 -1
- package/url/index.cjs +63 -5
- package/url/index.d.ts +200 -2
- package/url/index.mjs +59 -6
- package/{validate → validation}/index.cjs +91 -44
- package/{validate/validate.utils.d.ts → validation/index.d.ts} +33 -24
- package/{validate → validation}/index.mjs +87 -44
- package/array/array.utils.d.ts +0 -191
- package/async/async.utils.d.ts +0 -296
- package/cache/cache.utils.d.ts +0 -176
- package/config/config.d.ts +0 -57
- package/context-store/context-store.utils.d.ts +0 -212
- package/crypto/crypto.utils.d.ts +0 -183
- package/date/date.utils.d.ts +0 -190
- package/decorators/index.cjs +0 -913
- package/decorators/index.d.ts +0 -25
- package/decorators/index.mjs +0 -872
- package/dir/index.d.ts +0 -25
- package/env/env.utils.d.ts +0 -400
- package/exception/exception.utils.d.ts +0 -253
- package/fs/fs.utils.d.ts +0 -196
- package/http-status-codes/http-status-codes.d.ts +0 -289
- package/id/id.utils.d.ts +0 -59
- package/logger/logger.utils.d.ts +0 -210
- package/middleware/middleware.utils.d.ts +0 -123
- package/obj/index.cjs +0 -317
- package/obj/index.d.ts +0 -25
- package/obj/index.mjs +0 -301
- package/performance/performance.utils.d.ts +0 -159
- package/request/request.utils.d.ts +0 -109
- package/response/response.utils.d.ts +0 -186
- package/server/server.builder.d.ts +0 -531
- package/server/server.d.ts +0 -303
- package/stream/stream.utils.d.ts +0 -111
- package/string/string.utils.d.ts +0 -124
- package/type/type.utils.d.ts +0 -129
- package/types/api-response.d.ts +0 -175
- package/types/common.d.ts +0 -148
- package/types/config.d.ts +0 -88
- package/types/server.d.ts +0 -291
- package/url/url.utils.d.ts +0 -164
- package/validate/index.d.ts +0 -25
package/exception/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -22,4 +22,235 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
import { ErrorResponse } from '@catbee/utils/response';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Generic HTTP error class used for custom exceptions with any status code.
|
|
29
|
+
* Inherit this when you need to throw errors dynamically at runtime.
|
|
30
|
+
*/
|
|
31
|
+
declare class HttpError extends ErrorResponse {
|
|
32
|
+
/**
|
|
33
|
+
* Creates a new HTTP error instance.
|
|
34
|
+
* @param status - A valid HTTP status code (e.g., 400, 500).
|
|
35
|
+
* @param message - The error message to return to the client.
|
|
36
|
+
*/
|
|
37
|
+
constructor(status: number, message: string);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Represents a 500 Internal Server Error.
|
|
41
|
+
* Use this for unexpected backend errors that are not caused by the client.
|
|
42
|
+
*/
|
|
43
|
+
declare class InternalServerErrorException extends ErrorResponse {
|
|
44
|
+
/**
|
|
45
|
+
* @param message - Optional custom message. Defaults to "Internal server error".
|
|
46
|
+
*/
|
|
47
|
+
constructor(message?: string);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Represents a 401 Unauthorized Error.
|
|
51
|
+
* Indicates that authentication is required and has failed or not been provided.
|
|
52
|
+
*/
|
|
53
|
+
declare class UnauthorizedException extends ErrorResponse {
|
|
54
|
+
/**
|
|
55
|
+
* @param message - Optional custom message. Defaults to "Unauthorized".
|
|
56
|
+
*/
|
|
57
|
+
constructor(message?: string);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Represents a 400 Bad Request Error.
|
|
61
|
+
* Indicates that the client has sent invalid data (e.g., malformed request body).
|
|
62
|
+
*/
|
|
63
|
+
declare class BadRequestException extends ErrorResponse {
|
|
64
|
+
/**
|
|
65
|
+
* @param message - Optional custom message. Defaults to "Bad request".
|
|
66
|
+
*/
|
|
67
|
+
constructor(message?: string);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Represents a 404 Not Found Error.
|
|
71
|
+
* Used when a requested resource (e.g., user, file) could not be located.
|
|
72
|
+
*/
|
|
73
|
+
declare class NotFoundException extends ErrorResponse {
|
|
74
|
+
/**
|
|
75
|
+
* @param message - Optional custom message. Defaults to "Resource not found".
|
|
76
|
+
*/
|
|
77
|
+
constructor(message?: string);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Represents a 403 Forbidden Error.
|
|
81
|
+
* Indicates that the client is authenticated but not allowed to access the resource.
|
|
82
|
+
*/
|
|
83
|
+
declare class ForbiddenException extends ErrorResponse {
|
|
84
|
+
/**
|
|
85
|
+
* @param message - Optional custom message. Defaults to "Forbidden".
|
|
86
|
+
*/
|
|
87
|
+
constructor(message?: string);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Represents a 409 Conflict Error.
|
|
91
|
+
* Commonly used when a resource already exists or a versioning conflict occurs.
|
|
92
|
+
*/
|
|
93
|
+
declare class ConflictException extends ErrorResponse {
|
|
94
|
+
/**
|
|
95
|
+
* @param message - Optional custom message. Defaults to "Conflict".
|
|
96
|
+
*/
|
|
97
|
+
constructor(message?: string);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Represents a 502 Bad Gateway Error.
|
|
101
|
+
* Used when the server receives an invalid response from an upstream server.
|
|
102
|
+
*/
|
|
103
|
+
declare class BadGatewayException extends ErrorResponse {
|
|
104
|
+
/**
|
|
105
|
+
* @param message - Optional custom message. Defaults to "Bad Gateway".
|
|
106
|
+
*/
|
|
107
|
+
constructor(message?: string);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Represents a 429 Too Many Requests Error.
|
|
111
|
+
* Returned when the client has hit a rate limit.
|
|
112
|
+
*/
|
|
113
|
+
declare class TooManyRequestsException extends ErrorResponse {
|
|
114
|
+
/**
|
|
115
|
+
* @param message - Optional custom message. Defaults to "Too many requests".
|
|
116
|
+
*/
|
|
117
|
+
constructor(message?: string);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Represents a 503 Service Unavailable Error.
|
|
121
|
+
* Indicates that the server is temporarily unavailable (e.g., for maintenance).
|
|
122
|
+
*/
|
|
123
|
+
declare class ServiceUnavailableException extends ErrorResponse {
|
|
124
|
+
/**
|
|
125
|
+
* @param message - Optional custom message. Defaults to "Service Unavailable".
|
|
126
|
+
*/
|
|
127
|
+
constructor(message?: string);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Represents a 504 Gateway Timeout Error.
|
|
131
|
+
* Returned when the server acting as a gateway times out waiting for a response.
|
|
132
|
+
*/
|
|
133
|
+
declare class GatewayTimeoutException extends ErrorResponse {
|
|
134
|
+
/**
|
|
135
|
+
* @param message - Optional custom message. Defaults to "Gateway Timeout".
|
|
136
|
+
*/
|
|
137
|
+
constructor(message?: string);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Represents a 422 Unprocessable Entity Error.
|
|
141
|
+
* Used when the server understands the content type but cannot process the contained instructions.
|
|
142
|
+
*/
|
|
143
|
+
declare class UnprocessableEntityException extends ErrorResponse {
|
|
144
|
+
details?: Record<string, any> | undefined;
|
|
145
|
+
/**
|
|
146
|
+
* @param message - Optional custom message. Defaults to "Unprocessable Entity".
|
|
147
|
+
* @param details - Optional validation details or additional error context.
|
|
148
|
+
*/
|
|
149
|
+
constructor(message?: string, details?: Record<string, any> | undefined);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Represents a 405 Method Not Allowed Error.
|
|
153
|
+
* Used when the request method is not supported for the requested resource.
|
|
154
|
+
*/
|
|
155
|
+
declare class MethodNotAllowedException extends ErrorResponse {
|
|
156
|
+
allowedMethods?: string[] | undefined;
|
|
157
|
+
/**
|
|
158
|
+
* @param message - Optional custom message. Defaults to "Method Not Allowed".
|
|
159
|
+
* @param allowedMethods - Optional array of allowed HTTP methods.
|
|
160
|
+
*/
|
|
161
|
+
constructor(message?: string, allowedMethods?: string[] | undefined);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Represents a 406 Not Acceptable Error.
|
|
165
|
+
* Used when the server cannot produce a response matching the client's accepted content types.
|
|
166
|
+
*/
|
|
167
|
+
declare class NotAcceptableException extends ErrorResponse {
|
|
168
|
+
/**
|
|
169
|
+
* @param message - Optional custom message. Defaults to "Not Acceptable".
|
|
170
|
+
*/
|
|
171
|
+
constructor(message?: string);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Represents a 408 Request Timeout Error.
|
|
175
|
+
* Used when the server times out waiting for the client to send a request.
|
|
176
|
+
*/
|
|
177
|
+
declare class RequestTimeoutException extends ErrorResponse {
|
|
178
|
+
/**
|
|
179
|
+
* @param message - Optional custom message. Defaults to "Request Timeout".
|
|
180
|
+
*/
|
|
181
|
+
constructor(message?: string);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Represents a 415 Unsupported Media Type Error.
|
|
185
|
+
* Used when the request's Content-Type is not supported.
|
|
186
|
+
*/
|
|
187
|
+
declare class UnsupportedMediaTypeException extends ErrorResponse {
|
|
188
|
+
/**
|
|
189
|
+
* @param message - Optional custom message. Defaults to "Unsupported Media Type".
|
|
190
|
+
*/
|
|
191
|
+
constructor(message?: string);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Represents a 413 Payload Too Large Error.
|
|
195
|
+
* Used when the request body exceeds server limits.
|
|
196
|
+
*/
|
|
197
|
+
declare class PayloadTooLargeException extends ErrorResponse {
|
|
198
|
+
/**
|
|
199
|
+
* @param message - Optional custom message. Defaults to "Payload Too Large".
|
|
200
|
+
*/
|
|
201
|
+
constructor(message?: string);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Represents a 507 Insufficient Storage Error.
|
|
205
|
+
* Used when the server has insufficient storage to complete the request.
|
|
206
|
+
*/
|
|
207
|
+
declare class InsufficientStorageException extends ErrorResponse {
|
|
208
|
+
/**
|
|
209
|
+
* @param message - Optional custom message. Defaults to "Insufficient Storage".
|
|
210
|
+
*/
|
|
211
|
+
constructor(message?: string);
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Checks if an error is an instance of HttpError or its subclasses.
|
|
215
|
+
*
|
|
216
|
+
* @param error - The error to check.
|
|
217
|
+
* @returns True if the error is an HttpError, false otherwise.
|
|
218
|
+
*/
|
|
219
|
+
declare function isHttpError(error: unknown): error is ErrorResponse;
|
|
220
|
+
/**
|
|
221
|
+
* Creates an HTTP error with the specified status code and message.
|
|
222
|
+
* Factory function for creating appropriate HTTP error instances.
|
|
223
|
+
*
|
|
224
|
+
* @param status - HTTP status code.
|
|
225
|
+
* @param message - Optional custom error message.
|
|
226
|
+
* @returns An instance of the appropriate HTTP error class.
|
|
227
|
+
*/
|
|
228
|
+
declare function createHttpError(status: number, message?: string): ErrorResponse;
|
|
229
|
+
/**
|
|
230
|
+
* Type guard to check if an object has specific error properties.
|
|
231
|
+
*
|
|
232
|
+
* @param error - The object to check.
|
|
233
|
+
* @returns True if object has error-like structure.
|
|
234
|
+
*/
|
|
235
|
+
declare function hasErrorShape(error: unknown): error is {
|
|
236
|
+
message: string;
|
|
237
|
+
status?: number;
|
|
238
|
+
code?: string;
|
|
239
|
+
};
|
|
240
|
+
/**
|
|
241
|
+
* Safely extracts message from any error type.
|
|
242
|
+
*
|
|
243
|
+
* @param error - Any error object or value.
|
|
244
|
+
* @returns A string error message.
|
|
245
|
+
*/
|
|
246
|
+
declare function getErrorMessage(error: unknown): string;
|
|
247
|
+
/**
|
|
248
|
+
* Wraps a function and transforms any errors into HTTP errors.
|
|
249
|
+
* Useful for API handlers to ensure consistent error responses.
|
|
250
|
+
*
|
|
251
|
+
* @param handler - The async function to wrap.
|
|
252
|
+
* @returns A function that always returns or throws an HTTP error.
|
|
253
|
+
*/
|
|
254
|
+
declare function withErrorHandling<T extends (...args: any[]) => Promise<any>>(handler: T): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>>;
|
|
255
|
+
|
|
256
|
+
export { BadGatewayException, BadRequestException, ConflictException, ForbiddenException, GatewayTimeoutException, HttpError, InsufficientStorageException, InternalServerErrorException, MethodNotAllowedException, NotAcceptableException, NotFoundException, PayloadTooLargeException, RequestTimeoutException, ServiceUnavailableException, TooManyRequestsException, UnauthorizedException, UnprocessableEntityException, UnsupportedMediaTypeException, createHttpError, getErrorMessage, hasErrorShape, isHttpError, withErrorHandling };
|
package/exception/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
package/fs/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
|
|
25
25
|
'use strict';
|
|
26
26
|
|
|
27
|
-
var fs
|
|
28
|
-
var
|
|
27
|
+
var fs = require('fs');
|
|
28
|
+
var fsp = require('fs/promises');
|
|
29
29
|
var os = require('os');
|
|
30
30
|
var path = require('path');
|
|
31
31
|
var promises = require('stream/promises');
|
|
@@ -34,13 +34,14 @@ var crypto = require('crypto');
|
|
|
34
34
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
35
35
|
|
|
36
36
|
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
37
|
+
var fsp__default = /*#__PURE__*/_interopDefault(fsp);
|
|
37
38
|
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
38
39
|
|
|
39
40
|
var __defProp = Object.defineProperty;
|
|
40
41
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
41
42
|
async function fileExists(path2) {
|
|
42
43
|
try {
|
|
43
|
-
await
|
|
44
|
+
await fsp__default.default.access(path2);
|
|
44
45
|
return true;
|
|
45
46
|
} catch {
|
|
46
47
|
return false;
|
|
@@ -49,7 +50,7 @@ async function fileExists(path2) {
|
|
|
49
50
|
__name(fileExists, "fileExists");
|
|
50
51
|
async function readJsonFile(path2) {
|
|
51
52
|
try {
|
|
52
|
-
const data = await
|
|
53
|
+
const data = await fsp__default.default.readFile(path2, "utf-8");
|
|
53
54
|
return JSON.parse(data);
|
|
54
55
|
} catch {
|
|
55
56
|
return null;
|
|
@@ -63,12 +64,12 @@ async function readJsonOrDefault(path2, defaultValue) {
|
|
|
63
64
|
__name(readJsonOrDefault, "readJsonOrDefault");
|
|
64
65
|
async function writeJsonFile(path2, data, space = 2) {
|
|
65
66
|
const json = JSON.stringify(data, null, space);
|
|
66
|
-
await
|
|
67
|
+
await fsp__default.default.writeFile(path2, json, "utf-8");
|
|
67
68
|
}
|
|
68
69
|
__name(writeJsonFile, "writeJsonFile");
|
|
69
70
|
async function deleteFileIfExists(path2) {
|
|
70
71
|
try {
|
|
71
|
-
await
|
|
72
|
+
await fsp__default.default.unlink(path2);
|
|
72
73
|
return true;
|
|
73
74
|
} catch (err) {
|
|
74
75
|
if (err.code === "ENOENT") return true;
|
|
@@ -76,36 +77,66 @@ async function deleteFileIfExists(path2) {
|
|
|
76
77
|
}
|
|
77
78
|
}
|
|
78
79
|
__name(deleteFileIfExists, "deleteFileIfExists");
|
|
79
|
-
async function
|
|
80
|
+
async function readFile(path2, encoding = "utf-8") {
|
|
81
|
+
return await fsp__default.default.readFile(path2, encoding);
|
|
82
|
+
}
|
|
83
|
+
__name(readFile, "readFile");
|
|
84
|
+
function readFileSync(path2, encoding = "utf-8") {
|
|
85
|
+
return fs__default.default.readFileSync(path2, encoding);
|
|
86
|
+
}
|
|
87
|
+
__name(readFileSync, "readFileSync");
|
|
88
|
+
async function safeReadFile(path2, encoding = "utf-8") {
|
|
80
89
|
try {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
90
|
+
const data = await fsp__default.default.readFile(path2, encoding);
|
|
91
|
+
return {
|
|
92
|
+
data,
|
|
93
|
+
error: null
|
|
94
|
+
};
|
|
95
|
+
} catch (error) {
|
|
96
|
+
return {
|
|
97
|
+
data: null,
|
|
98
|
+
error: new Error(`Failed to read file ${path2}: ${error.message}`)
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
__name(safeReadFile, "safeReadFile");
|
|
103
|
+
function safeReadFileSync(path2, encoding = "utf-8") {
|
|
104
|
+
try {
|
|
105
|
+
const data = fs__default.default.readFileSync(path2, encoding);
|
|
106
|
+
return {
|
|
107
|
+
data,
|
|
108
|
+
error: null
|
|
109
|
+
};
|
|
110
|
+
} catch (error) {
|
|
111
|
+
return {
|
|
112
|
+
data: null,
|
|
113
|
+
error: new Error(`Failed to read file ${path2}: ${error.message}`)
|
|
114
|
+
};
|
|
84
115
|
}
|
|
85
116
|
}
|
|
86
|
-
__name(
|
|
87
|
-
async function
|
|
117
|
+
__name(safeReadFileSync, "safeReadFileSync");
|
|
118
|
+
async function writeFile(path2, content, encoding = "utf-8") {
|
|
88
119
|
try {
|
|
89
|
-
await
|
|
120
|
+
await fsp__default.default.writeFile(path2, content, encoding);
|
|
90
121
|
return true;
|
|
91
122
|
} catch {
|
|
92
123
|
return false;
|
|
93
124
|
}
|
|
94
125
|
}
|
|
95
|
-
__name(
|
|
96
|
-
async function
|
|
126
|
+
__name(writeFile, "writeFile");
|
|
127
|
+
async function appendFile(path2, content, encoding = "utf-8") {
|
|
97
128
|
try {
|
|
98
|
-
await
|
|
129
|
+
await fsp__default.default.appendFile(path2, content, encoding);
|
|
99
130
|
return true;
|
|
100
131
|
} catch {
|
|
101
132
|
return false;
|
|
102
133
|
}
|
|
103
134
|
}
|
|
104
|
-
__name(
|
|
135
|
+
__name(appendFile, "appendFile");
|
|
105
136
|
async function copyFile(source, destination, overwrite = false) {
|
|
106
137
|
try {
|
|
107
|
-
const flags = overwrite ? 0 :
|
|
108
|
-
await
|
|
138
|
+
const flags = overwrite ? 0 : fsp__default.default.constants.COPYFILE_EXCL;
|
|
139
|
+
await fsp__default.default.copyFile(source, destination, flags);
|
|
109
140
|
return true;
|
|
110
141
|
} catch {
|
|
111
142
|
return false;
|
|
@@ -114,12 +145,12 @@ async function copyFile(source, destination, overwrite = false) {
|
|
|
114
145
|
__name(copyFile, "copyFile");
|
|
115
146
|
async function moveFile(oldPath, newPath) {
|
|
116
147
|
try {
|
|
117
|
-
await
|
|
148
|
+
await fsp__default.default.rename(oldPath, newPath);
|
|
118
149
|
return true;
|
|
119
150
|
} catch {
|
|
120
151
|
try {
|
|
121
|
-
await
|
|
122
|
-
await
|
|
152
|
+
await fsp__default.default.copyFile(oldPath, newPath);
|
|
153
|
+
await fsp__default.default.unlink(oldPath);
|
|
123
154
|
return true;
|
|
124
155
|
} catch {
|
|
125
156
|
return false;
|
|
@@ -129,7 +160,7 @@ async function moveFile(oldPath, newPath) {
|
|
|
129
160
|
__name(moveFile, "moveFile");
|
|
130
161
|
async function getFileStats(path2) {
|
|
131
162
|
try {
|
|
132
|
-
return await
|
|
163
|
+
return await fsp__default.default.stat(path2);
|
|
133
164
|
} catch {
|
|
134
165
|
return null;
|
|
135
166
|
}
|
|
@@ -140,16 +171,16 @@ async function createTempFile({ prefix = "tmp-", extension = "", dir = os.tmpdir
|
|
|
140
171
|
const filename = `${prefix}${crypto.randomUUID()}${ext}`;
|
|
141
172
|
const filepath = path__default.default.join(dir, filename);
|
|
142
173
|
if (content) {
|
|
143
|
-
await
|
|
174
|
+
await fsp__default.default.writeFile(filepath, content);
|
|
144
175
|
} else {
|
|
145
|
-
await
|
|
176
|
+
await fsp__default.default.writeFile(filepath, "");
|
|
146
177
|
}
|
|
147
178
|
return filepath;
|
|
148
179
|
}
|
|
149
180
|
__name(createTempFile, "createTempFile");
|
|
150
181
|
async function streamFile(source, destination) {
|
|
151
|
-
const readStream =
|
|
152
|
-
const writeStream =
|
|
182
|
+
const readStream = fs__default.default.createReadStream(source);
|
|
183
|
+
const writeStream = fs__default.default.createWriteStream(destination);
|
|
153
184
|
try {
|
|
154
185
|
await promises.pipeline(readStream, writeStream);
|
|
155
186
|
} catch (error) {
|
|
@@ -160,7 +191,7 @@ async function streamFile(source, destination) {
|
|
|
160
191
|
}
|
|
161
192
|
__name(streamFile, "streamFile");
|
|
162
193
|
async function readDirectory(dirPath, options = {}) {
|
|
163
|
-
const files = await
|
|
194
|
+
const files = await fsp__default.default.readdir(dirPath);
|
|
164
195
|
let result = files;
|
|
165
196
|
if (options.filter) {
|
|
166
197
|
result = result.filter((file) => options.filter.test(file));
|
|
@@ -173,7 +204,7 @@ async function readDirectory(dirPath, options = {}) {
|
|
|
173
204
|
__name(readDirectory, "readDirectory");
|
|
174
205
|
async function createDirectory(dirPath, recursive = true) {
|
|
175
206
|
try {
|
|
176
|
-
await
|
|
207
|
+
await fsp__default.default.mkdir(dirPath, {
|
|
177
208
|
recursive
|
|
178
209
|
});
|
|
179
210
|
return true;
|
|
@@ -184,7 +215,7 @@ async function createDirectory(dirPath, recursive = true) {
|
|
|
184
215
|
__name(createDirectory, "createDirectory");
|
|
185
216
|
async function safeReadJsonFile(path2) {
|
|
186
217
|
try {
|
|
187
|
-
const content = await
|
|
218
|
+
const content = await fsp__default.default.readFile(path2, "utf-8");
|
|
188
219
|
try {
|
|
189
220
|
const data = JSON.parse(content);
|
|
190
221
|
return {
|
|
@@ -207,7 +238,7 @@ async function safeReadJsonFile(path2) {
|
|
|
207
238
|
__name(safeReadJsonFile, "safeReadJsonFile");
|
|
208
239
|
async function isFile(path2) {
|
|
209
240
|
try {
|
|
210
|
-
const stat = await
|
|
241
|
+
const stat = await fsp__default.default.stat(path2);
|
|
211
242
|
return stat.isFile();
|
|
212
243
|
} catch {
|
|
213
244
|
return false;
|
|
@@ -216,7 +247,7 @@ async function isFile(path2) {
|
|
|
216
247
|
__name(isFile, "isFile");
|
|
217
248
|
async function getFileSize(path2) {
|
|
218
249
|
try {
|
|
219
|
-
const stat = await
|
|
250
|
+
const stat = await fsp__default.default.stat(path2);
|
|
220
251
|
return stat.size;
|
|
221
252
|
} catch {
|
|
222
253
|
return -1;
|
|
@@ -225,14 +256,14 @@ async function getFileSize(path2) {
|
|
|
225
256
|
__name(getFileSize, "getFileSize");
|
|
226
257
|
async function readFileBuffer(path2) {
|
|
227
258
|
try {
|
|
228
|
-
return await
|
|
259
|
+
return await fsp__default.default.readFile(path2);
|
|
229
260
|
} catch {
|
|
230
261
|
return null;
|
|
231
262
|
}
|
|
232
263
|
}
|
|
233
264
|
__name(readFileBuffer, "readFileBuffer");
|
|
234
265
|
|
|
235
|
-
exports.
|
|
266
|
+
exports.appendFile = appendFile;
|
|
236
267
|
exports.copyFile = copyFile;
|
|
237
268
|
exports.createDirectory = createDirectory;
|
|
238
269
|
exports.createTempFile = createTempFile;
|
|
@@ -243,11 +274,14 @@ exports.getFileStats = getFileStats;
|
|
|
243
274
|
exports.isFile = isFile;
|
|
244
275
|
exports.moveFile = moveFile;
|
|
245
276
|
exports.readDirectory = readDirectory;
|
|
277
|
+
exports.readFile = readFile;
|
|
246
278
|
exports.readFileBuffer = readFileBuffer;
|
|
279
|
+
exports.readFileSync = readFileSync;
|
|
247
280
|
exports.readJsonFile = readJsonFile;
|
|
248
281
|
exports.readJsonOrDefault = readJsonOrDefault;
|
|
249
|
-
exports.
|
|
282
|
+
exports.safeReadFile = safeReadFile;
|
|
283
|
+
exports.safeReadFileSync = safeReadFileSync;
|
|
250
284
|
exports.safeReadJsonFile = safeReadJsonFile;
|
|
251
285
|
exports.streamFile = streamFile;
|
|
286
|
+
exports.writeFile = writeFile;
|
|
252
287
|
exports.writeJsonFile = writeJsonFile;
|
|
253
|
-
exports.writeTextFile = writeTextFile;
|