@catbee/utils 1.1.0 → 2.0.0-next.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.
- package/LICENSE +1 -1
- package/README.md +79 -43
- package/array/index.cjs +355 -0
- package/array/index.d.ts +317 -0
- package/array/index.mjs +327 -0
- package/async/index.cjs +484 -0
- package/async/index.d.ts +299 -0
- package/async/index.mjs +463 -0
- package/cache/index.cjs +292 -0
- package/cache/index.d.ts +179 -0
- package/cache/index.mjs +290 -0
- package/config/index.cjs +150 -0
- package/config/index.d.ts +88 -0
- package/config/index.mjs +143 -0
- package/context-store/index.cjs +267 -0
- package/context-store/index.d.ts +216 -0
- package/context-store/index.mjs +261 -0
- package/crypto/index.cjs +182 -0
- package/crypto/index.d.ts +187 -0
- package/crypto/index.mjs +166 -0
- package/date/index.cjs +340 -0
- package/date/index.d.ts +214 -0
- package/date/index.mjs +326 -0
- package/decorators/index.cjs +2051 -0
- package/decorators/index.d.ts +708 -0
- package/decorators/index.mjs +2010 -0
- package/dir/index.cjs +417 -0
- package/dir/index.d.ts +219 -0
- package/dir/index.mjs +390 -0
- package/env/index.cjs +745 -0
- package/env/index.d.ts +403 -0
- package/env/index.mjs +742 -0
- package/exception/index.cjs +362 -0
- package/exception/index.d.ts +256 -0
- package/exception/index.mjs +338 -0
- package/fs/index.cjs +287 -0
- package/fs/index.d.ts +229 -0
- package/fs/index.mjs +258 -0
- package/http-status-codes/index.cjs +96 -0
- package/http-status-codes/index.d.ts +291 -0
- package/http-status-codes/index.mjs +94 -0
- package/id/index.cjs +62 -0
- package/id/index.d.ts +61 -0
- package/id/index.mjs +56 -0
- package/index.cjs +218 -0
- package/index.d.ts +51 -0
- package/index.mjs +51 -0
- package/logger/index.cjs +334 -0
- package/logger/index.d.ts +213 -0
- package/logger/index.mjs +313 -0
- package/middleware/index.cjs +177 -0
- package/middleware/index.d.ts +127 -0
- package/middleware/index.mjs +170 -0
- package/obj/index.cjs +305 -0
- package/obj/index.d.ts +160 -0
- package/obj/index.mjs +289 -0
- package/package.json +172 -20
- package/performance/index.cjs +231 -0
- package/performance/index.d.ts +162 -0
- package/performance/index.mjs +225 -0
- package/request/index.cjs +202 -0
- package/request/index.d.ts +265 -0
- package/request/index.mjs +194 -0
- package/response/index.cjs +234 -0
- package/response/index.d.ts +342 -0
- package/response/index.mjs +222 -0
- package/server/index.cjs +1631 -0
- package/server/index.d.ts +809 -0
- package/server/index.mjs +1622 -0
- package/stream/index.cjs +151 -0
- package/stream/index.d.ts +114 -0
- package/stream/index.mjs +144 -0
- package/string/index.cjs +109 -0
- package/string/index.d.ts +126 -0
- package/string/index.mjs +95 -0
- package/type/index.cjs +129 -0
- package/type/index.d.ts +131 -0
- package/type/index.mjs +119 -0
- package/types/index.cjs +34 -0
- package/types/index.d.ts +798 -0
- package/types/index.mjs +32 -0
- package/url/index.cjs +199 -0
- package/url/index.d.ts +166 -0
- package/url/index.mjs +187 -0
- package/validation/index.cjs +259 -0
- package/validation/index.d.ts +209 -0
- package/validation/index.mjs +231 -0
- package/build/index.cjs +0 -7694
- package/build/index.d.ts +0 -5792
- package/build/index.mjs +0 -7386
|
@@ -0,0 +1,338 @@
|
|
|
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 { HttpStatusCodes } from '@catbee/utils/http-status-codes';
|
|
26
|
+
import { getLogger } from '@catbee/utils/logger';
|
|
27
|
+
import { ErrorResponse } from '@catbee/utils/response';
|
|
28
|
+
|
|
29
|
+
var __defProp = Object.defineProperty;
|
|
30
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
31
|
+
var HttpError = class extends ErrorResponse {
|
|
32
|
+
static {
|
|
33
|
+
__name(this, "HttpError");
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Creates a new HTTP error instance.
|
|
37
|
+
* @param status - A valid HTTP status code (e.g., 400, 500).
|
|
38
|
+
* @param message - The error message to return to the client.
|
|
39
|
+
*/
|
|
40
|
+
constructor(status, message) {
|
|
41
|
+
super(message);
|
|
42
|
+
this.status = status;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var InternalServerErrorException = class extends ErrorResponse {
|
|
46
|
+
static {
|
|
47
|
+
__name(this, "InternalServerErrorException");
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* @param message - Optional custom message. Defaults to "Internal server error".
|
|
51
|
+
*/
|
|
52
|
+
constructor(message = "Internal server error") {
|
|
53
|
+
super(message);
|
|
54
|
+
this.status = HttpStatusCodes.INTERNAL_SERVER_ERROR;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
var UnauthorizedException = class extends ErrorResponse {
|
|
58
|
+
static {
|
|
59
|
+
__name(this, "UnauthorizedException");
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* @param message - Optional custom message. Defaults to "Unauthorized".
|
|
63
|
+
*/
|
|
64
|
+
constructor(message = "Unauthorized") {
|
|
65
|
+
super(message);
|
|
66
|
+
this.status = HttpStatusCodes.UNAUTHORIZED;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
var BadRequestException = class extends ErrorResponse {
|
|
70
|
+
static {
|
|
71
|
+
__name(this, "BadRequestException");
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* @param message - Optional custom message. Defaults to "Bad request".
|
|
75
|
+
*/
|
|
76
|
+
constructor(message = "Bad request") {
|
|
77
|
+
super(message);
|
|
78
|
+
this.status = HttpStatusCodes.BAD_REQUEST;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
var NotFoundException = class extends ErrorResponse {
|
|
82
|
+
static {
|
|
83
|
+
__name(this, "NotFoundException");
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* @param message - Optional custom message. Defaults to "Resource not found".
|
|
87
|
+
*/
|
|
88
|
+
constructor(message = "Resource not found") {
|
|
89
|
+
super(message);
|
|
90
|
+
this.status = HttpStatusCodes.NOT_FOUND;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
var ForbiddenException = class extends ErrorResponse {
|
|
94
|
+
static {
|
|
95
|
+
__name(this, "ForbiddenException");
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* @param message - Optional custom message. Defaults to "Forbidden".
|
|
99
|
+
*/
|
|
100
|
+
constructor(message = "Forbidden") {
|
|
101
|
+
super(message);
|
|
102
|
+
this.status = HttpStatusCodes.FORBIDDEN;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
var ConflictException = class extends ErrorResponse {
|
|
106
|
+
static {
|
|
107
|
+
__name(this, "ConflictException");
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* @param message - Optional custom message. Defaults to "Conflict".
|
|
111
|
+
*/
|
|
112
|
+
constructor(message = "Conflict") {
|
|
113
|
+
super(message);
|
|
114
|
+
this.status = HttpStatusCodes.CONFLICT;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
var BadGatewayException = class extends ErrorResponse {
|
|
118
|
+
static {
|
|
119
|
+
__name(this, "BadGatewayException");
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* @param message - Optional custom message. Defaults to "Bad Gateway".
|
|
123
|
+
*/
|
|
124
|
+
constructor(message = "Bad Gateway") {
|
|
125
|
+
super(message);
|
|
126
|
+
this.status = HttpStatusCodes.BAD_GATEWAY;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
var TooManyRequestsException = class extends ErrorResponse {
|
|
130
|
+
static {
|
|
131
|
+
__name(this, "TooManyRequestsException");
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* @param message - Optional custom message. Defaults to "Too many requests".
|
|
135
|
+
*/
|
|
136
|
+
constructor(message = "Too many requests") {
|
|
137
|
+
super(message);
|
|
138
|
+
this.status = HttpStatusCodes.TOO_MANY_REQUESTS;
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
var ServiceUnavailableException = class extends ErrorResponse {
|
|
142
|
+
static {
|
|
143
|
+
__name(this, "ServiceUnavailableException");
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* @param message - Optional custom message. Defaults to "Service Unavailable".
|
|
147
|
+
*/
|
|
148
|
+
constructor(message = "Service Unavailable") {
|
|
149
|
+
super(message);
|
|
150
|
+
this.status = HttpStatusCodes.SERVICE_UNAVAILABLE;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
var GatewayTimeoutException = class extends ErrorResponse {
|
|
154
|
+
static {
|
|
155
|
+
__name(this, "GatewayTimeoutException");
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* @param message - Optional custom message. Defaults to "Gateway Timeout".
|
|
159
|
+
*/
|
|
160
|
+
constructor(message = "Gateway Timeout") {
|
|
161
|
+
super(message);
|
|
162
|
+
this.status = HttpStatusCodes.GATEWAY_TIMEOUT;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
var UnprocessableEntityException = class extends ErrorResponse {
|
|
166
|
+
static {
|
|
167
|
+
__name(this, "UnprocessableEntityException");
|
|
168
|
+
}
|
|
169
|
+
details;
|
|
170
|
+
/**
|
|
171
|
+
* @param message - Optional custom message. Defaults to "Unprocessable Entity".
|
|
172
|
+
* @param details - Optional validation details or additional error context.
|
|
173
|
+
*/
|
|
174
|
+
constructor(message = "Unprocessable Entity", details) {
|
|
175
|
+
super(message), this.details = details;
|
|
176
|
+
this.status = HttpStatusCodes.UNPROCESSABLE_ENTITY;
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
var MethodNotAllowedException = class extends ErrorResponse {
|
|
180
|
+
static {
|
|
181
|
+
__name(this, "MethodNotAllowedException");
|
|
182
|
+
}
|
|
183
|
+
allowedMethods;
|
|
184
|
+
/**
|
|
185
|
+
* @param message - Optional custom message. Defaults to "Method Not Allowed".
|
|
186
|
+
* @param allowedMethods - Optional array of allowed HTTP methods.
|
|
187
|
+
*/
|
|
188
|
+
constructor(message = "Method Not Allowed", allowedMethods) {
|
|
189
|
+
super(message), this.allowedMethods = allowedMethods;
|
|
190
|
+
this.status = HttpStatusCodes.METHOD_NOT_ALLOWED;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
var NotAcceptableException = class extends ErrorResponse {
|
|
194
|
+
static {
|
|
195
|
+
__name(this, "NotAcceptableException");
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* @param message - Optional custom message. Defaults to "Not Acceptable".
|
|
199
|
+
*/
|
|
200
|
+
constructor(message = "Not Acceptable") {
|
|
201
|
+
super(message);
|
|
202
|
+
this.status = HttpStatusCodes.NOT_ACCEPTABLE;
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
var RequestTimeoutException = class extends ErrorResponse {
|
|
206
|
+
static {
|
|
207
|
+
__name(this, "RequestTimeoutException");
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* @param message - Optional custom message. Defaults to "Request Timeout".
|
|
211
|
+
*/
|
|
212
|
+
constructor(message = "Request Timeout") {
|
|
213
|
+
super(message);
|
|
214
|
+
this.status = HttpStatusCodes.REQUEST_TIMEOUT;
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
var UnsupportedMediaTypeException = class extends ErrorResponse {
|
|
218
|
+
static {
|
|
219
|
+
__name(this, "UnsupportedMediaTypeException");
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* @param message - Optional custom message. Defaults to "Unsupported Media Type".
|
|
223
|
+
*/
|
|
224
|
+
constructor(message = "Unsupported Media Type") {
|
|
225
|
+
super(message);
|
|
226
|
+
this.status = HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE;
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
var PayloadTooLargeException = class extends ErrorResponse {
|
|
230
|
+
static {
|
|
231
|
+
__name(this, "PayloadTooLargeException");
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* @param message - Optional custom message. Defaults to "Payload Too Large".
|
|
235
|
+
*/
|
|
236
|
+
constructor(message = "Payload Too Large") {
|
|
237
|
+
super(message);
|
|
238
|
+
this.status = HttpStatusCodes.PAYLOAD_TOO_LARGE;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
var InsufficientStorageException = class extends ErrorResponse {
|
|
242
|
+
static {
|
|
243
|
+
__name(this, "InsufficientStorageException");
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* @param message - Optional custom message. Defaults to "Insufficient Storage".
|
|
247
|
+
*/
|
|
248
|
+
constructor(message = "Insufficient Storage") {
|
|
249
|
+
super(message);
|
|
250
|
+
this.status = HttpStatusCodes.INSUFFICIENT_STORAGE;
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
function isHttpError(error) {
|
|
254
|
+
return error instanceof ErrorResponse;
|
|
255
|
+
}
|
|
256
|
+
__name(isHttpError, "isHttpError");
|
|
257
|
+
function createHttpError(status, message) {
|
|
258
|
+
switch (status) {
|
|
259
|
+
case HttpStatusCodes.BAD_REQUEST:
|
|
260
|
+
return new BadRequestException(message);
|
|
261
|
+
case HttpStatusCodes.UNAUTHORIZED:
|
|
262
|
+
return new UnauthorizedException(message);
|
|
263
|
+
case HttpStatusCodes.FORBIDDEN:
|
|
264
|
+
return new ForbiddenException(message);
|
|
265
|
+
case HttpStatusCodes.NOT_FOUND:
|
|
266
|
+
return new NotFoundException(message);
|
|
267
|
+
case HttpStatusCodes.METHOD_NOT_ALLOWED:
|
|
268
|
+
return new MethodNotAllowedException(message);
|
|
269
|
+
case HttpStatusCodes.NOT_ACCEPTABLE:
|
|
270
|
+
return new NotAcceptableException(message);
|
|
271
|
+
case HttpStatusCodes.REQUEST_TIMEOUT:
|
|
272
|
+
return new RequestTimeoutException(message);
|
|
273
|
+
case HttpStatusCodes.CONFLICT:
|
|
274
|
+
return new ConflictException(message);
|
|
275
|
+
case HttpStatusCodes.GONE:
|
|
276
|
+
return new HttpError(HttpStatusCodes.GONE, message || "Gone");
|
|
277
|
+
case HttpStatusCodes.PAYLOAD_TOO_LARGE:
|
|
278
|
+
return new PayloadTooLargeException(message);
|
|
279
|
+
case HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE:
|
|
280
|
+
return new UnsupportedMediaTypeException(message);
|
|
281
|
+
case HttpStatusCodes.UNPROCESSABLE_ENTITY:
|
|
282
|
+
return new UnprocessableEntityException(message);
|
|
283
|
+
case HttpStatusCodes.TOO_MANY_REQUESTS:
|
|
284
|
+
return new TooManyRequestsException(message);
|
|
285
|
+
case HttpStatusCodes.INTERNAL_SERVER_ERROR:
|
|
286
|
+
return new InternalServerErrorException(message);
|
|
287
|
+
case HttpStatusCodes.BAD_GATEWAY:
|
|
288
|
+
return new BadGatewayException(message);
|
|
289
|
+
case HttpStatusCodes.SERVICE_UNAVAILABLE:
|
|
290
|
+
return new ServiceUnavailableException(message);
|
|
291
|
+
case HttpStatusCodes.GATEWAY_TIMEOUT:
|
|
292
|
+
return new GatewayTimeoutException(message);
|
|
293
|
+
case HttpStatusCodes.INSUFFICIENT_STORAGE:
|
|
294
|
+
return new InsufficientStorageException(message);
|
|
295
|
+
default:
|
|
296
|
+
return new HttpError(status, message || `HTTP Error ${status}`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
__name(createHttpError, "createHttpError");
|
|
300
|
+
function hasErrorShape(error) {
|
|
301
|
+
return typeof error === "object" && error !== null && "message" in error && typeof error.message === "string";
|
|
302
|
+
}
|
|
303
|
+
__name(hasErrorShape, "hasErrorShape");
|
|
304
|
+
function getErrorMessage(error) {
|
|
305
|
+
if (typeof error === "string") {
|
|
306
|
+
return error;
|
|
307
|
+
}
|
|
308
|
+
if (error instanceof Error) {
|
|
309
|
+
return error.message;
|
|
310
|
+
}
|
|
311
|
+
if (hasErrorShape(error)) {
|
|
312
|
+
return error.message;
|
|
313
|
+
}
|
|
314
|
+
return "Unknown error occurred";
|
|
315
|
+
}
|
|
316
|
+
__name(getErrorMessage, "getErrorMessage");
|
|
317
|
+
function withErrorHandling(handler) {
|
|
318
|
+
return async (...args) => {
|
|
319
|
+
try {
|
|
320
|
+
return await handler(...args);
|
|
321
|
+
} catch (err) {
|
|
322
|
+
if (isHttpError(err)) {
|
|
323
|
+
throw err;
|
|
324
|
+
}
|
|
325
|
+
getLogger().error({
|
|
326
|
+
err
|
|
327
|
+
}, "Caught error in handler");
|
|
328
|
+
let status = HttpStatusCodes.INTERNAL_SERVER_ERROR;
|
|
329
|
+
if (hasErrorShape(err) && typeof err.status === "number") {
|
|
330
|
+
status = err.status;
|
|
331
|
+
}
|
|
332
|
+
throw createHttpError(status, getErrorMessage(err));
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
__name(withErrorHandling, "withErrorHandling");
|
|
337
|
+
|
|
338
|
+
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/fs/index.cjs
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
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
|
+
'use strict';
|
|
26
|
+
|
|
27
|
+
var fs = require('fs');
|
|
28
|
+
var fsp = require('fs/promises');
|
|
29
|
+
var os = require('os');
|
|
30
|
+
var path = require('path');
|
|
31
|
+
var promises = require('stream/promises');
|
|
32
|
+
var crypto = require('crypto');
|
|
33
|
+
|
|
34
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
35
|
+
|
|
36
|
+
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
37
|
+
var fsp__default = /*#__PURE__*/_interopDefault(fsp);
|
|
38
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
39
|
+
|
|
40
|
+
var __defProp = Object.defineProperty;
|
|
41
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
42
|
+
async function fileExists(path2) {
|
|
43
|
+
try {
|
|
44
|
+
await fsp__default.default.access(path2);
|
|
45
|
+
return true;
|
|
46
|
+
} catch {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
__name(fileExists, "fileExists");
|
|
51
|
+
async function readJsonFile(path2) {
|
|
52
|
+
try {
|
|
53
|
+
const data = await fsp__default.default.readFile(path2, "utf-8");
|
|
54
|
+
return JSON.parse(data);
|
|
55
|
+
} catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
__name(readJsonFile, "readJsonFile");
|
|
60
|
+
async function readJsonOrDefault(path2, defaultValue) {
|
|
61
|
+
const res = await safeReadJsonFile(path2);
|
|
62
|
+
return res.data === null ? defaultValue : res.data;
|
|
63
|
+
}
|
|
64
|
+
__name(readJsonOrDefault, "readJsonOrDefault");
|
|
65
|
+
async function writeJsonFile(path2, data, space = 2) {
|
|
66
|
+
const json = JSON.stringify(data, null, space);
|
|
67
|
+
await fsp__default.default.writeFile(path2, json, "utf-8");
|
|
68
|
+
}
|
|
69
|
+
__name(writeJsonFile, "writeJsonFile");
|
|
70
|
+
async function deleteFileIfExists(path2) {
|
|
71
|
+
try {
|
|
72
|
+
await fsp__default.default.unlink(path2);
|
|
73
|
+
return true;
|
|
74
|
+
} catch (err) {
|
|
75
|
+
if (err.code === "ENOENT") return true;
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
__name(deleteFileIfExists, "deleteFileIfExists");
|
|
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") {
|
|
89
|
+
try {
|
|
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
|
+
};
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
__name(safeReadFileSync, "safeReadFileSync");
|
|
118
|
+
async function writeFile(path2, content, encoding = "utf-8") {
|
|
119
|
+
try {
|
|
120
|
+
await fsp__default.default.writeFile(path2, content, encoding);
|
|
121
|
+
return true;
|
|
122
|
+
} catch {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
__name(writeFile, "writeFile");
|
|
127
|
+
async function appendFile(path2, content, encoding = "utf-8") {
|
|
128
|
+
try {
|
|
129
|
+
await fsp__default.default.appendFile(path2, content, encoding);
|
|
130
|
+
return true;
|
|
131
|
+
} catch {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
__name(appendFile, "appendFile");
|
|
136
|
+
async function copyFile(source, destination, overwrite = false) {
|
|
137
|
+
try {
|
|
138
|
+
const flags = overwrite ? 0 : fsp__default.default.constants.COPYFILE_EXCL;
|
|
139
|
+
await fsp__default.default.copyFile(source, destination, flags);
|
|
140
|
+
return true;
|
|
141
|
+
} catch {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
__name(copyFile, "copyFile");
|
|
146
|
+
async function moveFile(oldPath, newPath) {
|
|
147
|
+
try {
|
|
148
|
+
await fsp__default.default.rename(oldPath, newPath);
|
|
149
|
+
return true;
|
|
150
|
+
} catch {
|
|
151
|
+
try {
|
|
152
|
+
await fsp__default.default.copyFile(oldPath, newPath);
|
|
153
|
+
await fsp__default.default.unlink(oldPath);
|
|
154
|
+
return true;
|
|
155
|
+
} catch {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
__name(moveFile, "moveFile");
|
|
161
|
+
async function getFileStats(path2) {
|
|
162
|
+
try {
|
|
163
|
+
return await fsp__default.default.stat(path2);
|
|
164
|
+
} catch {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
__name(getFileStats, "getFileStats");
|
|
169
|
+
async function createTempFile({ prefix = "tmp-", extension = "", dir = os.tmpdir(), content } = {}) {
|
|
170
|
+
const ext = extension ? extension.startsWith(".") ? extension : `.${extension}` : "";
|
|
171
|
+
const filename = `${prefix}${crypto.randomUUID()}${ext}`;
|
|
172
|
+
const filepath = path__default.default.join(dir, filename);
|
|
173
|
+
if (content) {
|
|
174
|
+
await fsp__default.default.writeFile(filepath, content);
|
|
175
|
+
} else {
|
|
176
|
+
await fsp__default.default.writeFile(filepath, "");
|
|
177
|
+
}
|
|
178
|
+
return filepath;
|
|
179
|
+
}
|
|
180
|
+
__name(createTempFile, "createTempFile");
|
|
181
|
+
async function streamFile(source, destination) {
|
|
182
|
+
const readStream = fs__default.default.createReadStream(source);
|
|
183
|
+
const writeStream = fs__default.default.createWriteStream(destination);
|
|
184
|
+
try {
|
|
185
|
+
await promises.pipeline(readStream, writeStream);
|
|
186
|
+
} catch (error) {
|
|
187
|
+
readStream.destroy();
|
|
188
|
+
writeStream.destroy();
|
|
189
|
+
throw error;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
__name(streamFile, "streamFile");
|
|
193
|
+
async function readDirectory(dirPath, options = {}) {
|
|
194
|
+
const files = await fsp__default.default.readdir(dirPath);
|
|
195
|
+
let result = files;
|
|
196
|
+
if (options.filter) {
|
|
197
|
+
result = result.filter((file) => options.filter.test(file));
|
|
198
|
+
}
|
|
199
|
+
if (options.fullPaths) {
|
|
200
|
+
result = result.map((file) => path__default.default.join(dirPath, file));
|
|
201
|
+
}
|
|
202
|
+
return result;
|
|
203
|
+
}
|
|
204
|
+
__name(readDirectory, "readDirectory");
|
|
205
|
+
async function createDirectory(dirPath, recursive = true) {
|
|
206
|
+
try {
|
|
207
|
+
await fsp__default.default.mkdir(dirPath, {
|
|
208
|
+
recursive
|
|
209
|
+
});
|
|
210
|
+
return true;
|
|
211
|
+
} catch (err) {
|
|
212
|
+
return err.code === "EEXIST";
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
__name(createDirectory, "createDirectory");
|
|
216
|
+
async function safeReadJsonFile(path2) {
|
|
217
|
+
try {
|
|
218
|
+
const content = await fsp__default.default.readFile(path2, "utf-8");
|
|
219
|
+
try {
|
|
220
|
+
const data = JSON.parse(content);
|
|
221
|
+
return {
|
|
222
|
+
data,
|
|
223
|
+
error: null
|
|
224
|
+
};
|
|
225
|
+
} catch (error) {
|
|
226
|
+
return {
|
|
227
|
+
data: null,
|
|
228
|
+
error: new Error(`Invalid JSON in file ${path2}: ${error.message}`)
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
} catch (error) {
|
|
232
|
+
return {
|
|
233
|
+
data: null,
|
|
234
|
+
error: new Error(`Failed to read file ${path2}: ${error.message}`)
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
__name(safeReadJsonFile, "safeReadJsonFile");
|
|
239
|
+
async function isFile(path2) {
|
|
240
|
+
try {
|
|
241
|
+
const stat = await fsp__default.default.stat(path2);
|
|
242
|
+
return stat.isFile();
|
|
243
|
+
} catch {
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
__name(isFile, "isFile");
|
|
248
|
+
async function getFileSize(path2) {
|
|
249
|
+
try {
|
|
250
|
+
const stat = await fsp__default.default.stat(path2);
|
|
251
|
+
return stat.size;
|
|
252
|
+
} catch {
|
|
253
|
+
return -1;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
__name(getFileSize, "getFileSize");
|
|
257
|
+
async function readFileBuffer(path2) {
|
|
258
|
+
try {
|
|
259
|
+
return await fsp__default.default.readFile(path2);
|
|
260
|
+
} catch {
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
__name(readFileBuffer, "readFileBuffer");
|
|
265
|
+
|
|
266
|
+
exports.appendFile = appendFile;
|
|
267
|
+
exports.copyFile = copyFile;
|
|
268
|
+
exports.createDirectory = createDirectory;
|
|
269
|
+
exports.createTempFile = createTempFile;
|
|
270
|
+
exports.deleteFileIfExists = deleteFileIfExists;
|
|
271
|
+
exports.fileExists = fileExists;
|
|
272
|
+
exports.getFileSize = getFileSize;
|
|
273
|
+
exports.getFileStats = getFileStats;
|
|
274
|
+
exports.isFile = isFile;
|
|
275
|
+
exports.moveFile = moveFile;
|
|
276
|
+
exports.readDirectory = readDirectory;
|
|
277
|
+
exports.readFile = readFile;
|
|
278
|
+
exports.readFileBuffer = readFileBuffer;
|
|
279
|
+
exports.readFileSync = readFileSync;
|
|
280
|
+
exports.readJsonFile = readJsonFile;
|
|
281
|
+
exports.readJsonOrDefault = readJsonOrDefault;
|
|
282
|
+
exports.safeReadFile = safeReadFile;
|
|
283
|
+
exports.safeReadFileSync = safeReadFileSync;
|
|
284
|
+
exports.safeReadJsonFile = safeReadJsonFile;
|
|
285
|
+
exports.streamFile = streamFile;
|
|
286
|
+
exports.writeFile = writeFile;
|
|
287
|
+
exports.writeJsonFile = writeJsonFile;
|