@catbee/utils 0.0.1 → 0.0.2
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/README.md +303 -107
- package/build/esm/config.d.ts +12 -0
- package/build/esm/config.js +12 -0
- package/build/esm/config.js.map +1 -1
- package/build/esm/index.d.ts +1 -0
- package/build/esm/index.js +1 -0
- package/build/esm/index.js.map +1 -1
- package/build/esm/types/api-response.d.ts +82 -0
- package/build/esm/types/api-response.js.map +1 -1
- package/build/esm/utils/array.utils.d.ts +64 -1
- package/build/esm/utils/array.utils.js +139 -2
- package/build/esm/utils/array.utils.js.map +1 -1
- package/build/esm/utils/async.utils.d.ts +60 -1
- package/build/esm/utils/async.utils.js +160 -2
- package/build/esm/utils/async.utils.js.map +1 -1
- package/build/esm/utils/cache.utils.d.ts +67 -4
- package/build/esm/utils/cache.utils.js +210 -28
- package/build/esm/utils/cache.utils.js.map +1 -1
- package/build/esm/utils/context-store.utils.d.ts +101 -0
- package/build/esm/utils/context-store.utils.js +182 -0
- package/build/esm/utils/context-store.utils.js.map +1 -1
- package/build/esm/utils/crypto.utils.d.ts +110 -8
- package/build/esm/utils/crypto.utils.js +298 -15
- package/build/esm/utils/crypto.utils.js.map +1 -1
- package/build/esm/utils/decorators.utils.d.ts +40 -0
- package/build/esm/utils/decorators.utils.js +312 -0
- package/build/esm/utils/decorators.utils.js.map +1 -0
- package/build/esm/utils/dir.utils.d.ts +121 -0
- package/build/esm/utils/dir.utils.js +577 -0
- package/build/esm/utils/dir.utils.js.map +1 -1
- package/build/esm/utils/env.utils.d.ts +83 -0
- package/build/esm/utils/env.utils.js +233 -0
- package/build/esm/utils/env.utils.js.map +1 -1
- package/build/esm/utils/exception.utils.d.ts +116 -0
- package/build/esm/utils/exception.utils.js +322 -0
- package/build/esm/utils/exception.utils.js.map +1 -1
- package/build/esm/utils/fs.utils.d.ts +132 -0
- package/build/esm/utils/fs.utils.js +421 -0
- package/build/esm/utils/fs.utils.js.map +1 -1
- package/build/esm/utils/http-status-codes.d.ts +87 -178
- package/build/esm/utils/http-status-codes.js +92 -178
- package/build/esm/utils/http-status-codes.js.map +1 -1
- package/build/esm/utils/id.utils.d.ts +11 -10
- package/build/esm/utils/id.utils.js +29 -20
- package/build/esm/utils/id.utils.js.map +1 -1
- package/build/esm/utils/logger.utils.d.ts +24 -0
- package/build/esm/utils/logger.utils.js +54 -0
- package/build/esm/utils/logger.utils.js.map +1 -1
- package/build/esm/utils/middleware.utils.d.ts +72 -0
- package/build/esm/utils/middleware.utils.js +158 -0
- package/build/esm/utils/middleware.utils.js.map +1 -0
- package/build/esm/utils/obj.utils.d.ts +65 -3
- package/build/esm/utils/obj.utils.js +205 -6
- package/build/esm/utils/obj.utils.js.map +1 -1
- package/build/esm/utils/response.utils.d.ts +101 -0
- package/build/esm/utils/response.utils.js +153 -0
- package/build/esm/utils/response.utils.js.map +1 -1
- package/build/esm/utils/string.utils.d.ts +61 -5
- package/build/esm/utils/string.utils.js +101 -11
- package/build/esm/utils/string.utils.js.map +1 -1
- package/build/esm/utils/url.utils.d.ts +117 -0
- package/build/esm/utils/url.utils.js +287 -0
- package/build/esm/utils/url.utils.js.map +1 -1
- package/build/esm/utils/validate.utils.d.ts +110 -0
- package/build/esm/utils/validate.utils.js +186 -0
- package/build/esm/utils/validate.utils.js.map +1 -1
- package/build/esnext/config.d.ts +12 -0
- package/build/esnext/config.js +12 -0
- package/build/esnext/config.js.map +1 -1
- package/build/esnext/index.d.ts +1 -0
- package/build/esnext/index.js +1 -0
- package/build/esnext/index.js.map +1 -1
- package/build/esnext/types/api-response.d.ts +82 -0
- package/build/esnext/types/api-response.js.map +1 -1
- package/build/esnext/utils/array.utils.d.ts +64 -1
- package/build/esnext/utils/array.utils.js +129 -2
- package/build/esnext/utils/array.utils.js.map +1 -1
- package/build/esnext/utils/async.utils.d.ts +60 -1
- package/build/esnext/utils/async.utils.js +117 -1
- package/build/esnext/utils/async.utils.js.map +1 -1
- package/build/esnext/utils/cache.utils.d.ts +67 -4
- package/build/esnext/utils/cache.utils.js +137 -9
- package/build/esnext/utils/cache.utils.js.map +1 -1
- package/build/esnext/utils/context-store.utils.d.ts +101 -0
- package/build/esnext/utils/context-store.utils.js +168 -0
- package/build/esnext/utils/context-store.utils.js.map +1 -1
- package/build/esnext/utils/crypto.utils.d.ts +110 -8
- package/build/esnext/utils/crypto.utils.js +205 -15
- package/build/esnext/utils/crypto.utils.js.map +1 -1
- package/build/esnext/utils/decorators.utils.d.ts +40 -0
- package/build/esnext/utils/decorators.utils.js +155 -0
- package/build/esnext/utils/decorators.utils.js.map +1 -0
- package/build/esnext/utils/dir.utils.d.ts +121 -0
- package/build/esnext/utils/dir.utils.js +306 -0
- package/build/esnext/utils/dir.utils.js.map +1 -1
- package/build/esnext/utils/env.utils.d.ts +83 -0
- package/build/esnext/utils/env.utils.js +185 -0
- package/build/esnext/utils/env.utils.js.map +1 -1
- package/build/esnext/utils/exception.utils.d.ts +116 -0
- package/build/esnext/utils/exception.utils.js +213 -0
- package/build/esnext/utils/exception.utils.js.map +1 -1
- package/build/esnext/utils/fs.utils.d.ts +132 -0
- package/build/esnext/utils/fs.utils.js +268 -0
- package/build/esnext/utils/fs.utils.js.map +1 -1
- package/build/esnext/utils/http-status-codes.d.ts +87 -178
- package/build/esnext/utils/http-status-codes.js +92 -178
- package/build/esnext/utils/http-status-codes.js.map +1 -1
- package/build/esnext/utils/id.utils.d.ts +11 -10
- package/build/esnext/utils/id.utils.js +27 -19
- package/build/esnext/utils/id.utils.js.map +1 -1
- package/build/esnext/utils/logger.utils.d.ts +24 -0
- package/build/esnext/utils/logger.utils.js +42 -0
- package/build/esnext/utils/logger.utils.js.map +1 -1
- package/build/esnext/utils/middleware.utils.d.ts +72 -0
- package/build/esnext/utils/middleware.utils.js +157 -0
- package/build/esnext/utils/middleware.utils.js.map +1 -0
- package/build/esnext/utils/obj.utils.d.ts +65 -3
- package/build/esnext/utils/obj.utils.js +167 -3
- package/build/esnext/utils/obj.utils.js.map +1 -1
- package/build/esnext/utils/response.utils.d.ts +101 -0
- package/build/esnext/utils/response.utils.js +138 -0
- package/build/esnext/utils/response.utils.js.map +1 -1
- package/build/esnext/utils/string.utils.d.ts +61 -5
- package/build/esnext/utils/string.utils.js +110 -14
- package/build/esnext/utils/string.utils.js.map +1 -1
- package/build/esnext/utils/url.utils.d.ts +117 -0
- package/build/esnext/utils/url.utils.js +248 -0
- package/build/esnext/utils/url.utils.js.map +1 -1
- package/build/esnext/utils/validate.utils.d.ts +110 -0
- package/build/esnext/utils/validate.utils.js +184 -0
- package/build/esnext/utils/validate.utils.js.map +1 -1
- package/build/src/config.d.ts +12 -0
- package/build/src/config.js +12 -0
- package/build/src/config.js.map +1 -1
- package/build/src/index.d.ts +1 -0
- package/build/src/index.js +1 -0
- package/build/src/index.js.map +1 -1
- package/build/src/types/api-response.d.ts +82 -0
- package/build/src/types/api-response.js.map +1 -1
- package/build/src/utils/array.utils.d.ts +64 -1
- package/build/src/utils/array.utils.js +137 -4
- package/build/src/utils/array.utils.js.map +1 -1
- package/build/src/utils/async.utils.d.ts +60 -1
- package/build/src/utils/async.utils.js +124 -4
- package/build/src/utils/async.utils.js.map +1 -1
- package/build/src/utils/cache.utils.d.ts +67 -4
- package/build/src/utils/cache.utils.js +137 -9
- package/build/src/utils/cache.utils.js.map +1 -1
- package/build/src/utils/context-store.utils.d.ts +101 -0
- package/build/src/utils/context-store.utils.js +171 -1
- package/build/src/utils/context-store.utils.js.map +1 -1
- package/build/src/utils/crypto.utils.d.ts +110 -8
- package/build/src/utils/crypto.utils.js +224 -27
- package/build/src/utils/crypto.utils.js.map +1 -1
- package/build/src/utils/decorators.utils.d.ts +40 -0
- package/build/src/utils/decorators.utils.js +165 -0
- package/build/src/utils/decorators.utils.js.map +1 -0
- package/build/src/utils/dir.utils.d.ts +121 -0
- package/build/src/utils/dir.utils.js +316 -0
- package/build/src/utils/dir.utils.js.map +1 -1
- package/build/src/utils/env.utils.d.ts +83 -0
- package/build/src/utils/env.utils.js +186 -1
- package/build/src/utils/env.utils.js.map +1 -1
- package/build/src/utils/exception.utils.d.ts +116 -0
- package/build/src/utils/exception.utils.js +226 -1
- package/build/src/utils/exception.utils.js.map +1 -1
- package/build/src/utils/fs.utils.d.ts +132 -0
- package/build/src/utils/fs.utils.js +282 -0
- package/build/src/utils/fs.utils.js.map +1 -1
- package/build/src/utils/http-status-codes.d.ts +87 -178
- package/build/src/utils/http-status-codes.js +92 -178
- package/build/src/utils/http-status-codes.js.map +1 -1
- package/build/src/utils/id.utils.d.ts +11 -10
- package/build/src/utils/id.utils.js +27 -19
- package/build/src/utils/id.utils.js.map +1 -1
- package/build/src/utils/logger.utils.d.ts +24 -0
- package/build/src/utils/logger.utils.js +45 -0
- package/build/src/utils/logger.utils.js.map +1 -1
- package/build/src/utils/middleware.utils.d.ts +72 -0
- package/build/src/utils/middleware.utils.js +163 -0
- package/build/src/utils/middleware.utils.js.map +1 -0
- package/build/src/utils/obj.utils.d.ts +65 -3
- package/build/src/utils/obj.utils.js +177 -7
- package/build/src/utils/obj.utils.js.map +1 -1
- package/build/src/utils/response.utils.d.ts +101 -0
- package/build/src/utils/response.utils.js +146 -1
- package/build/src/utils/response.utils.js.map +1 -1
- package/build/src/utils/string.utils.d.ts +61 -5
- package/build/src/utils/string.utils.js +122 -20
- package/build/src/utils/string.utils.js.map +1 -1
- package/build/src/utils/url.utils.d.ts +117 -0
- package/build/src/utils/url.utils.js +257 -0
- package/build/src/utils/url.utils.js.map +1 -1
- package/build/src/utils/validate.utils.d.ts +110 -0
- package/build/src/utils/validate.utils.js +198 -0
- package/build/src/utils/validate.utils.js.map +1 -1
- package/package.json +20 -12
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exception.utils.js","sourceRoot":"","sources":["../../../src/utils/exception.utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM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B,SAAQ,aAAa;IAClD;;OAEG;IACH,YAAY,UAAkB,UAAU;QACtC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,eAAe,CAAC,QAAQ,CAAC;IACzC,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,mBAAoB,SAAQ,aAAa;IACpD;;OAEG;IACH,YAAY,UAAkB,aAAa;QACzC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,eAAe,CAAC,WAAW,CAAC;IAC5C,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,wBAAyB,SAAQ,aAAa;IACzD;;OAEG;IACH,YAAY,UAAkB,mBAAmB;QAC/C,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,eAAe,CAAC,iBAAiB,CAAC;IAClD,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,2BAA4B,SAAQ,aAAa;IAC5D;;OAEG;IACH,YAAY,UAAkB,qBAAqB;QACjD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,eAAe,CAAC,mBAAmB,CAAC;IACpD,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,uBAAwB,SAAQ,aAAa;IACxD;;OAEG;IACH,YAAY,UAAkB,iBAAiB;QAC7C,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,eAAe,CAAC,eAAe,CAAC;IAChD,CAAC;CACF","sourcesContent":["import { HttpStatusCodes } from \"./http-status-codes\";\nimport { ErrorResponse } from \"./response.utils\";\n\n/**\n * Generic HTTP error class used for custom exceptions with any status code.\n * Inherit this when you need to throw errors dynamically at runtime.\n */\nexport class HttpError extends ErrorResponse {\n /**\n * Creates a new HTTP error instance.\n * @param status - A valid HTTP status code (e.g., 400, 500).\n * @param message - The error message to return to the client.\n */\n constructor(status: number, message: string) {\n super(message);\n this.status = status;\n }\n}\n\n/**\n * Represents a 500 Internal Server Error.\n * Use this for unexpected backend errors that are not caused by the client.\n */\nexport class InternalServerErrorException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Internal server error\".\n */\n constructor(message: string = \"Internal server error\") {\n super(message);\n this.status = HttpStatusCodes.INTERNAL_SERVER_ERROR;\n }\n}\n\n/**\n * Represents a 401 Unauthorized Error.\n * Indicates that authentication is required and has failed or not been provided.\n */\nexport class UnauthorizedException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Unauthorized\".\n */\n constructor(message: string = \"Unauthorized\") {\n super(message);\n this.status = HttpStatusCodes.UNAUTHORIZED;\n }\n}\n\n/**\n * Represents a 400 Bad Request Error.\n * Indicates that the client has sent invalid data (e.g., malformed request body).\n */\nexport class BadRequestException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Bad request\".\n */\n constructor(message: string = \"Bad request\") {\n super(message);\n this.status = HttpStatusCodes.BAD_REQUEST;\n }\n}\n\n/**\n * Represents a 404 Not Found Error.\n * Used when a requested resource (e.g., user, file) could not be located.\n */\nexport class NotFoundException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Resource not found\".\n */\n constructor(message: string = \"Resource not found\") {\n super(message);\n this.status = HttpStatusCodes.NOT_FOUND;\n }\n}\n\n/**\n * Represents a 403 Forbidden Error.\n * Indicates that the client is authenticated but not allowed to access the resource.\n */\nexport class ForbiddenException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Forbidden\".\n */\n constructor(message: string = \"Forbidden\") {\n super(message);\n this.status = HttpStatusCodes.FORBIDDEN;\n }\n}\n\n/**\n * Represents a 409 Conflict Error.\n * Commonly used when a resource already exists or a versioning conflict occurs.\n */\nexport class ConflictException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Conflict\".\n */\n constructor(message: string = \"Conflict\") {\n super(message);\n this.status = HttpStatusCodes.CONFLICT;\n }\n}\n\n/**\n * Represents a 502 Bad Gateway Error.\n * Used when the server receives an invalid response from an upstream server.\n */\nexport class BadGatewayException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Bad Gateway\".\n */\n constructor(message: string = \"Bad Gateway\") {\n super(message);\n this.status = HttpStatusCodes.BAD_GATEWAY;\n }\n}\n\n/**\n * Represents a 429 Too Many Requests Error.\n * Returned when the client has hit a rate limit.\n */\nexport class TooManyRequestsException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Too many requests\".\n */\n constructor(message: string = \"Too many requests\") {\n super(message);\n this.status = HttpStatusCodes.TOO_MANY_REQUESTS;\n }\n}\n\n/**\n * Represents a 503 Service Unavailable Error.\n * Indicates that the server is temporarily unavailable (e.g., for maintenance).\n */\nexport class ServiceUnavailableException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Service Unavailable\".\n */\n constructor(message: string = \"Service Unavailable\") {\n super(message);\n this.status = HttpStatusCodes.SERVICE_UNAVAILABLE;\n }\n}\n\n/**\n * Represents a 504 Gateway Timeout Error.\n * Returned when the server acting as a gateway times out waiting for a response.\n */\nexport class GatewayTimeoutException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Gateway Timeout\".\n */\n constructor(message: string = \"Gateway Timeout\") {\n super(message);\n this.status = HttpStatusCodes.GATEWAY_TIMEOUT;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"exception.utils.js","sourceRoot":"","sources":["../../../src/utils/exception.utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD;;;GAGG;AACH,MAAM,OAAO,SAAU,SAAQ,aAAa;IAC1C;;;;OAIG;IACH,YAAY,MAAc,EAAE,OAAe;QACzC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,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AC,KAAK,CAAC,EAAE,CAAC;gBACvB,MAAM,KAAK,CAAC;YACd,CAAC;YAED,0DAA0D;YAC1D,SAAS,EAAE,CAAC,KAAK,CAAC,0BAA0B,EAAE,KAAK,CAAC,CAAC;YAErD,sDAAsD;YACtD,IAAI,MAAM,GAAG,eAAe,CAAC,qBAAqB,CAAC;YACnD,IAAI,aAAa,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;gBAC7D,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;YACxB,CAAC;YAED,gCAAgC;YAChC,MAAM,eAAe,CAAC,MAAM,EAAE,eAAe,CAAC,KAAK,CAAC,CAAC,CAAC;QACxD,CAAC;IACH,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { HttpStatusCodes } from \"./http-status-codes\";\nimport { getLogger } from \"./logger.utils\";\nimport { ErrorResponse } from \"./response.utils\";\n\n/**\n * Generic HTTP error class used for custom exceptions with any status code.\n * Inherit this when you need to throw errors dynamically at runtime.\n */\nexport class HttpError extends ErrorResponse {\n /**\n * Creates a new HTTP error instance.\n * @param status - A valid HTTP status code (e.g., 400, 500).\n * @param message - The error message to return to the client.\n */\n constructor(status: number, message: string) {\n super(message);\n this.status = status;\n }\n}\n\n/**\n * Represents a 500 Internal Server Error.\n * Use this for unexpected backend errors that are not caused by the client.\n */\nexport class InternalServerErrorException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Internal server error\".\n */\n constructor(message: string = \"Internal server error\") {\n super(message);\n this.status = HttpStatusCodes.INTERNAL_SERVER_ERROR;\n }\n}\n\n/**\n * Represents a 401 Unauthorized Error.\n * Indicates that authentication is required and has failed or not been provided.\n */\nexport class UnauthorizedException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Unauthorized\".\n */\n constructor(message: string = \"Unauthorized\") {\n super(message);\n this.status = HttpStatusCodes.UNAUTHORIZED;\n }\n}\n\n/**\n * Represents a 400 Bad Request Error.\n * Indicates that the client has sent invalid data (e.g., malformed request body).\n */\nexport class BadRequestException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Bad request\".\n */\n constructor(message: string = \"Bad request\") {\n super(message);\n this.status = HttpStatusCodes.BAD_REQUEST;\n }\n}\n\n/**\n * Represents a 404 Not Found Error.\n * Used when a requested resource (e.g., user, file) could not be located.\n */\nexport class NotFoundException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Resource not found\".\n */\n constructor(message: string = \"Resource not found\") {\n super(message);\n this.status = HttpStatusCodes.NOT_FOUND;\n }\n}\n\n/**\n * Represents a 403 Forbidden Error.\n * Indicates that the client is authenticated but not allowed to access the resource.\n */\nexport class ForbiddenException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Forbidden\".\n */\n constructor(message: string = \"Forbidden\") {\n super(message);\n this.status = HttpStatusCodes.FORBIDDEN;\n }\n}\n\n/**\n * Represents a 409 Conflict Error.\n * Commonly used when a resource already exists or a versioning conflict occurs.\n */\nexport class ConflictException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Conflict\".\n */\n constructor(message: string = \"Conflict\") {\n super(message);\n this.status = HttpStatusCodes.CONFLICT;\n }\n}\n\n/**\n * Represents a 502 Bad Gateway Error.\n * Used when the server receives an invalid response from an upstream server.\n */\nexport class BadGatewayException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Bad Gateway\".\n */\n constructor(message: string = \"Bad Gateway\") {\n super(message);\n this.status = HttpStatusCodes.BAD_GATEWAY;\n }\n}\n\n/**\n * Represents a 429 Too Many Requests Error.\n * Returned when the client has hit a rate limit.\n */\nexport class TooManyRequestsException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Too many requests\".\n */\n constructor(message: string = \"Too many requests\") {\n super(message);\n this.status = HttpStatusCodes.TOO_MANY_REQUESTS;\n }\n}\n\n/**\n * Represents a 503 Service Unavailable Error.\n * Indicates that the server is temporarily unavailable (e.g., for maintenance).\n */\nexport class ServiceUnavailableException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Service Unavailable\".\n */\n constructor(message: string = \"Service Unavailable\") {\n super(message);\n this.status = HttpStatusCodes.SERVICE_UNAVAILABLE;\n }\n}\n\n/**\n * Represents a 504 Gateway Timeout Error.\n * Returned when the server acting as a gateway times out waiting for a response.\n */\nexport class GatewayTimeoutException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Gateway Timeout\".\n */\n constructor(message: string = \"Gateway Timeout\") {\n super(message);\n this.status = HttpStatusCodes.GATEWAY_TIMEOUT;\n }\n}\n\n/**\n * Represents a 422 Unprocessable Entity Error.\n * Used when the server understands the content type but cannot process the contained instructions.\n */\nexport class UnprocessableEntityException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Unprocessable Entity\".\n * @param details - Optional validation details or additional error context.\n */\n constructor(\n message: string = \"Unprocessable Entity\",\n public details?: Record<string, any>,\n ) {\n super(message);\n this.status = HttpStatusCodes.UNPROCESSABLE_ENTITY;\n }\n}\n\n/**\n * Represents a 405 Method Not Allowed Error.\n * Used when the request method is not supported for the requested resource.\n */\nexport class MethodNotAllowedException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Method Not Allowed\".\n * @param allowedMethods - Optional array of allowed HTTP methods.\n */\n constructor(\n message: string = \"Method Not Allowed\",\n public allowedMethods?: string[],\n ) {\n super(message);\n this.status = HttpStatusCodes.METHOD_NOT_ALLOWED;\n }\n}\n\n/**\n * Represents a 406 Not Acceptable Error.\n * Used when the server cannot produce a response matching the client's accepted content types.\n */\nexport class NotAcceptableException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Not Acceptable\".\n */\n constructor(message: string = \"Not Acceptable\") {\n super(message);\n this.status = HttpStatusCodes.NOT_ACCEPTABLE;\n }\n}\n\n/**\n * Represents a 408 Request Timeout Error.\n * Used when the server times out waiting for the client to send a request.\n */\nexport class RequestTimeoutException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Request Timeout\".\n */\n constructor(message: string = \"Request Timeout\") {\n super(message);\n this.status = HttpStatusCodes.REQUEST_TIMEOUT;\n }\n}\n\n/**\n * Represents a 415 Unsupported Media Type Error.\n * Used when the request's Content-Type is not supported.\n */\nexport class UnsupportedMediaTypeException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Unsupported Media Type\".\n */\n constructor(message: string = \"Unsupported Media Type\") {\n super(message);\n this.status = HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE;\n }\n}\n\n/**\n * Represents a 413 Payload Too Large Error.\n * Used when the request body exceeds server limits.\n */\nexport class PayloadTooLargeException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Payload Too Large\".\n */\n constructor(message: string = \"Payload Too Large\") {\n super(message);\n this.status = HttpStatusCodes.PAYLOAD_TOO_LARGE;\n }\n}\n\n/**\n * Represents a 507 Insufficient Storage Error.\n * Used when the server has insufficient storage to complete the request.\n */\nexport class InsufficientStorageException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Insufficient Storage\".\n */\n constructor(message: string = \"Insufficient Storage\") {\n super(message);\n this.status = HttpStatusCodes.INSUFFICIENT_STORAGE;\n }\n}\n\n/**\n * Checks if an error is an instance of HttpError or its subclasses.\n *\n * @param error - The error to check.\n * @returns True if the error is an HttpError, false otherwise.\n */\nexport function isHttpError(error: unknown): error is ErrorResponse {\n return error instanceof ErrorResponse;\n}\n\n/**\n * Creates an HTTP error with the specified status code and message.\n * Factory function for creating appropriate HTTP error instances.\n *\n * @param status - HTTP status code.\n * @param message - Optional custom error message.\n * @returns An instance of the appropriate HTTP error class.\n */\nexport function createHttpError(\n status: number,\n message?: string,\n): ErrorResponse {\n switch (status) {\n case HttpStatusCodes.BAD_REQUEST:\n return new BadRequestException(message);\n case HttpStatusCodes.UNAUTHORIZED:\n return new UnauthorizedException(message);\n case HttpStatusCodes.FORBIDDEN:\n return new ForbiddenException(message);\n case HttpStatusCodes.NOT_FOUND:\n return new NotFoundException(message);\n case HttpStatusCodes.METHOD_NOT_ALLOWED:\n return new MethodNotAllowedException(message);\n case HttpStatusCodes.NOT_ACCEPTABLE:\n return new NotAcceptableException(message);\n case HttpStatusCodes.REQUEST_TIMEOUT:\n return new RequestTimeoutException(message);\n case HttpStatusCodes.CONFLICT:\n return new ConflictException(message);\n case HttpStatusCodes.GONE:\n return new HttpError(HttpStatusCodes.GONE, message || \"Gone\");\n case HttpStatusCodes.PAYLOAD_TOO_LARGE:\n return new PayloadTooLargeException(message);\n case HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE:\n return new UnsupportedMediaTypeException(message);\n case HttpStatusCodes.UNPROCESSABLE_ENTITY:\n return new UnprocessableEntityException(message);\n case HttpStatusCodes.TOO_MANY_REQUESTS:\n return new TooManyRequestsException(message);\n case HttpStatusCodes.INTERNAL_SERVER_ERROR:\n return new InternalServerErrorException(message);\n case HttpStatusCodes.BAD_GATEWAY:\n return new BadGatewayException(message);\n case HttpStatusCodes.SERVICE_UNAVAILABLE:\n return new ServiceUnavailableException(message);\n case HttpStatusCodes.GATEWAY_TIMEOUT:\n return new GatewayTimeoutException(message);\n case HttpStatusCodes.INSUFFICIENT_STORAGE:\n return new InsufficientStorageException(message);\n default:\n return new HttpError(status, message || `HTTP Error ${status}`);\n }\n}\n\n/**\n * Type guard to check if an object has specific error properties.\n *\n * @param error - The object to check.\n * @returns True if object has error-like structure.\n */\nexport function hasErrorShape(\n error: unknown,\n): error is { message: string; status?: number; code?: string } {\n return (\n typeof error === \"object\" &&\n error !== null &&\n \"message\" in error &&\n typeof (error as any).message === \"string\"\n );\n}\n\n/**\n * Safely extracts message from any error type.\n *\n * @param error - Any error object or value.\n * @returns A string error message.\n */\nexport function getErrorMessage(error: unknown): string {\n if (typeof error === \"string\") {\n return error;\n }\n\n if (error instanceof Error) {\n return error.message;\n }\n\n if (hasErrorShape(error)) {\n return error.message;\n }\n\n return \"Unknown error occurred\";\n}\n\n/**\n * Wraps a function and transforms any errors into HTTP errors.\n * Useful for API handlers to ensure consistent error responses.\n *\n * @param handler - The async function to wrap.\n * @returns A function that always returns or throws an HTTP error.\n */\nexport function withErrorHandling<T extends (...args: any[]) => Promise<any>>(\n handler: T,\n): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>> {\n return async (...args: Parameters<T>) => {\n try {\n return await handler(...args);\n } catch (error) {\n if (isHttpError(error)) {\n throw error;\n }\n\n // Log the original error (consider using a proper logger)\n getLogger().error(\"Caught error in handler:\", error);\n\n // Extract status code if it exists, otherwise use 500\n let status = HttpStatusCodes.INTERNAL_SERVER_ERROR;\n if (hasErrorShape(error) && typeof error.status === \"number\") {\n status = error.status;\n }\n\n // Create appropriate HTTP error\n throw createHttpError(status, getErrorMessage(error));\n }\n };\n}\n"]}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { BufferEncoding } from "./crypto.utils";
|
|
2
|
+
import { Stats } from "fs";
|
|
1
3
|
/**
|
|
2
4
|
* Checks whether a file or directory exists at the given path.
|
|
3
5
|
*
|
|
@@ -29,4 +31,134 @@ export declare function writeJsonFile(path: string, data: any, space?: number):
|
|
|
29
31
|
* @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.
|
|
30
32
|
*/
|
|
31
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>;
|
|
32
164
|
//# sourceMappingURL=fs.utils.d.ts.map
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import fs from "fs/promises";
|
|
2
|
+
import { createReadStream, createWriteStream } from "fs";
|
|
3
|
+
import { tmpdir } from "os";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { pipeline } from "stream/promises";
|
|
6
|
+
import { randomUUID } from "crypto";
|
|
2
7
|
/**
|
|
3
8
|
* Checks whether a file or directory exists at the given path.
|
|
4
9
|
*
|
|
@@ -59,4 +64,267 @@ export async function deleteFileIfExists(path) {
|
|
|
59
64
|
return false;
|
|
60
65
|
}
|
|
61
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Reads a text file from the specified path.
|
|
69
|
+
*
|
|
70
|
+
* @param {string} path - The path to the text file.
|
|
71
|
+
* @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
|
|
72
|
+
* @returns {Promise<string | null>} The file contents, or `null` if reading fails.
|
|
73
|
+
*/
|
|
74
|
+
export async function readTextFile(path, encoding = "utf-8") {
|
|
75
|
+
try {
|
|
76
|
+
return await fs.readFile(path, encoding);
|
|
77
|
+
}
|
|
78
|
+
catch (_a) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Writes text content to a file.
|
|
84
|
+
*
|
|
85
|
+
* @param {string} path - The destination file path.
|
|
86
|
+
* @param {string} content - The text content to write.
|
|
87
|
+
* @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
|
|
88
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
89
|
+
*/
|
|
90
|
+
export async function writeTextFile(path, content, encoding = "utf-8") {
|
|
91
|
+
try {
|
|
92
|
+
await fs.writeFile(path, content, encoding);
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
catch (_a) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Appends text content to a file.
|
|
101
|
+
*
|
|
102
|
+
* @param {string} path - The file path to append to.
|
|
103
|
+
* @param {string} content - The text content to append.
|
|
104
|
+
* @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
|
|
105
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
106
|
+
*/
|
|
107
|
+
export async function appendTextFile(path, content, encoding = "utf-8") {
|
|
108
|
+
try {
|
|
109
|
+
await fs.appendFile(path, content, encoding);
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
catch (_a) {
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Copies a file from source to destination.
|
|
118
|
+
*
|
|
119
|
+
* @param {string} source - Source file path.
|
|
120
|
+
* @param {string} destination - Destination file path.
|
|
121
|
+
* @param {boolean} [overwrite=false] - Whether to overwrite if destination exists.
|
|
122
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
123
|
+
*/
|
|
124
|
+
export async function copyFile(source, destination, overwrite = false) {
|
|
125
|
+
try {
|
|
126
|
+
const flags = overwrite ? 0 : fs.constants.COPYFILE_EXCL;
|
|
127
|
+
await fs.copyFile(source, destination, flags);
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
catch (_a) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Renames/moves a file.
|
|
136
|
+
*
|
|
137
|
+
* @param {string} oldPath - Current file path.
|
|
138
|
+
* @param {string} newPath - New file path.
|
|
139
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
140
|
+
*/
|
|
141
|
+
export async function moveFile(oldPath, newPath) {
|
|
142
|
+
try {
|
|
143
|
+
await fs.rename(oldPath, newPath);
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
catch (_a) {
|
|
147
|
+
// If rename fails (e.g., across devices), try copy+delete
|
|
148
|
+
try {
|
|
149
|
+
await fs.copyFile(oldPath, newPath);
|
|
150
|
+
await fs.unlink(oldPath);
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
catch (_b) {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Gets file stats if the file exists.
|
|
160
|
+
*
|
|
161
|
+
* @param {string} path - Path to the file.
|
|
162
|
+
* @returns {Promise<fs.Stats | null>} File stats object or null if file doesn't exist.
|
|
163
|
+
*/
|
|
164
|
+
export async function getFileStats(path) {
|
|
165
|
+
try {
|
|
166
|
+
return await fs.stat(path);
|
|
167
|
+
}
|
|
168
|
+
catch (_a) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Creates a temporary file with optional content.
|
|
174
|
+
*
|
|
175
|
+
* @param {object} [options] - Options for creating the temp file.
|
|
176
|
+
* @param {string} [options.prefix="tmp-"] - Filename prefix.
|
|
177
|
+
* @param {string} [options.extension=""] - File extension.
|
|
178
|
+
* @param {string} [options.dir] - Directory to create the file in (defaults to OS temp dir).
|
|
179
|
+
* @param {string | Buffer} [options.content] - Optional content to write to the file.
|
|
180
|
+
* @returns {Promise<string>} Path to the created temporary file.
|
|
181
|
+
*/
|
|
182
|
+
export async function createTempFile({ prefix = "tmp-", extension = "", dir = tmpdir(), content, } = {}) {
|
|
183
|
+
const ext = extension
|
|
184
|
+
? extension.startsWith(".")
|
|
185
|
+
? extension
|
|
186
|
+
: `.${extension}`
|
|
187
|
+
: "";
|
|
188
|
+
const filename = `${prefix}${randomUUID()}${ext}`;
|
|
189
|
+
const filepath = path.join(dir, filename);
|
|
190
|
+
if (content) {
|
|
191
|
+
await fs.writeFile(filepath, content);
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
await fs.writeFile(filepath, "");
|
|
195
|
+
}
|
|
196
|
+
return filepath;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Streams a file from source to destination.
|
|
200
|
+
* Useful for large files to avoid loading the entire file into memory.
|
|
201
|
+
*
|
|
202
|
+
* @param {string} source - Source file path.
|
|
203
|
+
* @param {string} destination - Destination file path.
|
|
204
|
+
* @returns {Promise<void>} Resolves when streaming completes.
|
|
205
|
+
* @throws {Error} If streaming fails.
|
|
206
|
+
*/
|
|
207
|
+
export async function streamFile(source, destination) {
|
|
208
|
+
const readStream = createReadStream(source);
|
|
209
|
+
const writeStream = createWriteStream(destination);
|
|
210
|
+
try {
|
|
211
|
+
await pipeline(readStream, writeStream);
|
|
212
|
+
}
|
|
213
|
+
catch (error) {
|
|
214
|
+
// Ensure both streams are closed in case of error
|
|
215
|
+
readStream.destroy();
|
|
216
|
+
writeStream.destroy();
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Reads a directory and returns file names.
|
|
222
|
+
*
|
|
223
|
+
* @param {string} dirPath - Path to the directory.
|
|
224
|
+
* @param {object} [options] - Options for reading the directory.
|
|
225
|
+
* @param {boolean} [options.fullPaths=false] - Whether to return full paths.
|
|
226
|
+
* @param {RegExp} [options.filter] - Optional regex to filter files.
|
|
227
|
+
* @returns {Promise<string[]>} Array of file names or paths.
|
|
228
|
+
* @throws {Error} If directory cannot be read.
|
|
229
|
+
*/
|
|
230
|
+
export async function readDirectory(dirPath, options = {}) {
|
|
231
|
+
const files = await fs.readdir(dirPath);
|
|
232
|
+
let result = files;
|
|
233
|
+
if (options.filter) {
|
|
234
|
+
result = result.filter((file) => options.filter.test(file));
|
|
235
|
+
}
|
|
236
|
+
if (options.fullPaths) {
|
|
237
|
+
result = result.map((file) => path.join(dirPath, file));
|
|
238
|
+
}
|
|
239
|
+
return result;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Creates a directory if it doesn't exist.
|
|
243
|
+
*
|
|
244
|
+
* @param {string} dirPath - Path to the directory.
|
|
245
|
+
* @param {boolean} [recursive=true] - Whether to create parent directories.
|
|
246
|
+
* @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
|
|
247
|
+
*/
|
|
248
|
+
export async function createDirectory(dirPath, recursive = true) {
|
|
249
|
+
try {
|
|
250
|
+
await fs.mkdir(dirPath, { recursive });
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
catch (err) {
|
|
254
|
+
// Consider it success if directory already exists
|
|
255
|
+
return err.code === "EEXIST";
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Safely reads and parses a JSON file with error details.
|
|
260
|
+
*
|
|
261
|
+
* @typeParam T - The expected type of the parsed JSON object.
|
|
262
|
+
* @param {string} path - The path to the JSON file.
|
|
263
|
+
* @returns {Promise<{ data: T | null; error: Error | null }>} Object with data and error properties.
|
|
264
|
+
*/
|
|
265
|
+
export async function safeReadJsonFile(path) {
|
|
266
|
+
try {
|
|
267
|
+
const content = await fs.readFile(path, "utf-8");
|
|
268
|
+
try {
|
|
269
|
+
const data = JSON.parse(content);
|
|
270
|
+
return { data, error: null };
|
|
271
|
+
}
|
|
272
|
+
catch (error) {
|
|
273
|
+
return {
|
|
274
|
+
data: null,
|
|
275
|
+
error: new Error(`Invalid JSON in file ${path}: ${error.message}`),
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
catch (error) {
|
|
280
|
+
return {
|
|
281
|
+
data: null,
|
|
282
|
+
error: new Error(`Failed to read file ${path}: ${error.message}`),
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Checks if a path points to a file (not a directory).
|
|
288
|
+
*
|
|
289
|
+
* @param {string} path - Path to check.
|
|
290
|
+
* @returns {Promise<boolean>} True if the path is a file, false otherwise.
|
|
291
|
+
*/
|
|
292
|
+
export async function isFile(path) {
|
|
293
|
+
try {
|
|
294
|
+
const stat = await fs.stat(path);
|
|
295
|
+
return stat.isFile();
|
|
296
|
+
}
|
|
297
|
+
catch (_a) {
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Gets the size of a file in bytes.
|
|
303
|
+
*
|
|
304
|
+
* @param {string} path - Path to the file.
|
|
305
|
+
* @returns {Promise<number>} Size in bytes or -1 if file doesn't exist.
|
|
306
|
+
*/
|
|
307
|
+
export async function getFileSize(path) {
|
|
308
|
+
try {
|
|
309
|
+
const stat = await fs.stat(path);
|
|
310
|
+
return stat.size;
|
|
311
|
+
}
|
|
312
|
+
catch (_a) {
|
|
313
|
+
return -1;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Reads a file as a Buffer.
|
|
318
|
+
*
|
|
319
|
+
* @param {string} path - Path to the file.
|
|
320
|
+
* @returns {Promise<Buffer | null>} File contents as Buffer or null if reading fails.
|
|
321
|
+
*/
|
|
322
|
+
export async function readFileBuffer(path) {
|
|
323
|
+
try {
|
|
324
|
+
return await fs.readFile(path);
|
|
325
|
+
}
|
|
326
|
+
catch (_a) {
|
|
327
|
+
return null;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
62
330
|
//# sourceMappingURL=fs.utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fs.utils.js","sourceRoot":"","sources":["../../../src/utils/fs.utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,aAAa,CAAC;AAE7B;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,IAAY;IAC3C,IAAI,CAAC;QACH,MAAM,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,WAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAAU,IAAY;IACtD,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAC9C,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAM,CAAC;IAC/B,CAAC;IAAC,WAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,IAAY,EACZ,IAAS,EACT,KAAK,GAAG,CAAC;IAET,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;IAC/C,MAAM,EAAE,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;AAC1C,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CAAC,IAAY;IACnD,IAAI,CAAC;QACH,MAAM,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,OAAO,GAAQ,EAAE,CAAC;QAClB,IAAI,GAAG,CAAC,IAAI,KAAK,QAAQ;YAAE,OAAO,IAAI,CAAC;QACvC,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC","sourcesContent":["import fs from \"fs/promises\";\n\n/**\n * Checks whether a file or directory exists at the given path.\n *\n * @param {string} path - The file or directory path to check.\n * @returns {Promise<boolean>} Resolves to `true` if the path exists, `false` otherwise.\n */\nexport async function fileExists(path: string): Promise<boolean> {\n try {\n await fs.access(path);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Reads and parses a JSON file from the specified path.\n *\n * @typeParam T - The expected type of the parsed JSON object.\n * @param {string} path - The path to the JSON file.\n * @returns {Promise<T | null>} The parsed object, or `null` if reading or parsing fails.\n */\nexport async function readJsonFile<T = any>(path: string): Promise<T | null> {\n try {\n const data = await fs.readFile(path, \"utf-8\");\n return JSON.parse(data) as T;\n } catch {\n return null;\n }\n}\n\n/**\n * Writes a JavaScript object to a file as formatted JSON.\n *\n * @param {string} path - The destination file path.\n * @param {any} data - The object to serialize and write.\n * @param {number} [space=2] - Number of spaces for JSON formatting.\n * @returns {Promise<void>} Resolves when writing is complete.\n */\nexport async function writeJsonFile(\n path: string,\n data: any,\n space = 2,\n): Promise<void> {\n const json = JSON.stringify(data, null, space);\n await fs.writeFile(path, json, \"utf-8\");\n}\n\n/**\n * Deletes a file if it exists.\n *\n * @param {string} path - The file path to delete.\n * @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.\n */\nexport async function deleteFileIfExists(path: string): Promise<boolean> {\n try {\n await fs.unlink(path);\n return true;\n } catch (err: any) {\n if (err.code === \"ENOENT\") return true;\n return false;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"fs.utils.js","sourceRoot":"","sources":["../../../src/utils/fs.utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAS,MAAM,IAAI,CAAC;AAChE,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAC5B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAEpC;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,IAAY;IAC3C,IAAI,CAAC;QACH,MAAM,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACtB,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,WAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAAU,IAAY;IACtD,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAC9C,OAAO,IAAI,CAAC,KAAK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AI,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import { BufferEncoding } from \"./crypto.utils\";\nimport fs from \"fs/promises\";\nimport { createReadStream, createWriteStream, Stats } from \"fs\";\nimport { tmpdir } from \"os\";\nimport path from \"path\";\nimport { pipeline } from \"stream/promises\";\nimport { randomUUID } from \"crypto\";\n\n/**\n * Checks whether a file or directory exists at the given path.\n *\n * @param {string} path - The file or directory path to check.\n * @returns {Promise<boolean>} Resolves to `true` if the path exists, `false` otherwise.\n */\nexport async function fileExists(path: string): Promise<boolean> {\n try {\n await fs.access(path);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Reads and parses a JSON file from the specified path.\n *\n * @typeParam T - The expected type of the parsed JSON object.\n * @param {string} path - The path to the JSON file.\n * @returns {Promise<T | null>} The parsed object, or `null` if reading or parsing fails.\n */\nexport async function readJsonFile<T = any>(path: string): Promise<T | null> {\n try {\n const data = await fs.readFile(path, \"utf-8\");\n return JSON.parse(data) as T;\n } catch {\n return null;\n }\n}\n\n/**\n * Writes a JavaScript object to a file as formatted JSON.\n *\n * @param {string} path - The destination file path.\n * @param {any} data - The object to serialize and write.\n * @param {number} [space=2] - Number of spaces for JSON formatting.\n * @returns {Promise<void>} Resolves when writing is complete.\n */\nexport async function writeJsonFile(\n path: string,\n data: any,\n space = 2,\n): Promise<void> {\n const json = JSON.stringify(data, null, space);\n await fs.writeFile(path, json, \"utf-8\");\n}\n\n/**\n * Deletes a file if it exists.\n *\n * @param {string} path - The file path to delete.\n * @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.\n */\nexport async function deleteFileIfExists(path: string): Promise<boolean> {\n try {\n await fs.unlink(path);\n return true;\n } catch (err: any) {\n if (err.code === \"ENOENT\") return true;\n return false;\n }\n}\n\n/**\n * Reads a text file from the specified path.\n *\n * @param {string} path - The path to the text file.\n * @param {BufferEncoding} [encoding=\"utf-8\"] - The encoding to use.\n * @returns {Promise<string | null>} The file contents, or `null` if reading fails.\n */\nexport async function readTextFile(\n path: string,\n encoding: BufferEncoding = \"utf-8\",\n): Promise<string | null> {\n try {\n return await fs.readFile(path, encoding);\n } catch {\n return null;\n }\n}\n\n/**\n * Writes text content to a file.\n *\n * @param {string} path - The destination file path.\n * @param {string} content - The text content to write.\n * @param {BufferEncoding} [encoding=\"utf-8\"] - The encoding to use.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function writeTextFile(\n path: string,\n content: string,\n encoding: BufferEncoding = \"utf-8\",\n): Promise<boolean> {\n try {\n await fs.writeFile(path, content, encoding);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Appends text content to a file.\n *\n * @param {string} path - The file path to append to.\n * @param {string} content - The text content to append.\n * @param {BufferEncoding} [encoding=\"utf-8\"] - The encoding to use.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function appendTextFile(\n path: string,\n content: string,\n encoding: BufferEncoding = \"utf-8\",\n): Promise<boolean> {\n try {\n await fs.appendFile(path, content, encoding);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Copies a file from source to destination.\n *\n * @param {string} source - Source file path.\n * @param {string} destination - Destination file path.\n * @param {boolean} [overwrite=false] - Whether to overwrite if destination exists.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function copyFile(\n source: string,\n destination: string,\n overwrite = false,\n): Promise<boolean> {\n try {\n const flags = overwrite ? 0 : fs.constants.COPYFILE_EXCL;\n await fs.copyFile(source, destination, flags);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Renames/moves a file.\n *\n * @param {string} oldPath - Current file path.\n * @param {string} newPath - New file path.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function moveFile(\n oldPath: string,\n newPath: string,\n): Promise<boolean> {\n try {\n await fs.rename(oldPath, newPath);\n return true;\n } catch {\n // If rename fails (e.g., across devices), try copy+delete\n try {\n await fs.copyFile(oldPath, newPath);\n await fs.unlink(oldPath);\n return true;\n } catch {\n return false;\n }\n }\n}\n\n/**\n * Gets file stats if the file exists.\n *\n * @param {string} path - Path to the file.\n * @returns {Promise<fs.Stats | null>} File stats object or null if file doesn't exist.\n */\nexport async function getFileStats(path: string): Promise<Stats | null> {\n try {\n return await fs.stat(path);\n } catch {\n return null;\n }\n}\n\n/**\n * Creates a temporary file with optional content.\n *\n * @param {object} [options] - Options for creating the temp file.\n * @param {string} [options.prefix=\"tmp-\"] - Filename prefix.\n * @param {string} [options.extension=\"\"] - File extension.\n * @param {string} [options.dir] - Directory to create the file in (defaults to OS temp dir).\n * @param {string | Buffer} [options.content] - Optional content to write to the file.\n * @returns {Promise<string>} Path to the created temporary file.\n */\nexport async function createTempFile({\n prefix = \"tmp-\",\n extension = \"\",\n dir = tmpdir(),\n content,\n}: {\n prefix?: string;\n extension?: string;\n dir?: string;\n content?: string | Buffer;\n} = {}): Promise<string> {\n const ext = extension\n ? extension.startsWith(\".\")\n ? extension\n : `.${extension}`\n : \"\";\n const filename = `${prefix}${randomUUID()}${ext}`;\n const filepath = path.join(dir, filename);\n\n if (content) {\n await fs.writeFile(filepath, content);\n } else {\n await fs.writeFile(filepath, \"\");\n }\n\n return filepath;\n}\n\n/**\n * Streams a file from source to destination.\n * Useful for large files to avoid loading the entire file into memory.\n *\n * @param {string} source - Source file path.\n * @param {string} destination - Destination file path.\n * @returns {Promise<void>} Resolves when streaming completes.\n * @throws {Error} If streaming fails.\n */\nexport async function streamFile(\n source: string,\n destination: string,\n): Promise<void> {\n const readStream = createReadStream(source);\n const writeStream = createWriteStream(destination);\n\n try {\n await pipeline(readStream, writeStream);\n } catch (error) {\n // Ensure both streams are closed in case of error\n readStream.destroy();\n writeStream.destroy();\n throw error;\n }\n}\n\n/**\n * Reads a directory and returns file names.\n *\n * @param {string} dirPath - Path to the directory.\n * @param {object} [options] - Options for reading the directory.\n * @param {boolean} [options.fullPaths=false] - Whether to return full paths.\n * @param {RegExp} [options.filter] - Optional regex to filter files.\n * @returns {Promise<string[]>} Array of file names or paths.\n * @throws {Error} If directory cannot be read.\n */\nexport async function readDirectory(\n dirPath: string,\n options: { fullPaths?: boolean; filter?: RegExp } = {},\n): Promise<string[]> {\n const files = await fs.readdir(dirPath);\n\n let result = files;\n\n if (options.filter) {\n result = result.filter((file) => options.filter!.test(file));\n }\n\n if (options.fullPaths) {\n result = result.map((file) => path.join(dirPath, file));\n }\n\n return result;\n}\n\n/**\n * Creates a directory if it doesn't exist.\n *\n * @param {string} dirPath - Path to the directory.\n * @param {boolean} [recursive=true] - Whether to create parent directories.\n * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.\n */\nexport async function createDirectory(\n dirPath: string,\n recursive = true,\n): Promise<boolean> {\n try {\n await fs.mkdir(dirPath, { recursive });\n return true;\n } catch (err: any) {\n // Consider it success if directory already exists\n return err.code === \"EEXIST\";\n }\n}\n\n/**\n * Safely reads and parses a JSON file with error details.\n *\n * @typeParam T - The expected type of the parsed JSON object.\n * @param {string} path - The path to the JSON file.\n * @returns {Promise<{ data: T | null; error: Error | null }>} Object with data and error properties.\n */\nexport async function safeReadJsonFile<T = any>(\n path: string,\n): Promise<{ data: T | null; error: Error | null }> {\n try {\n const content = await fs.readFile(path, \"utf-8\");\n try {\n const data = JSON.parse(content) as T;\n return { data, error: null };\n } catch (error: any) {\n return {\n data: null,\n error: new Error(`Invalid JSON in file ${path}: ${error.message}`),\n };\n }\n } catch (error: any) {\n return {\n data: null,\n error: new Error(`Failed to read file ${path}: ${error.message}`),\n };\n }\n}\n\n/**\n * Checks if a path points to a file (not a directory).\n *\n * @param {string} path - Path to check.\n * @returns {Promise<boolean>} True if the path is a file, false otherwise.\n */\nexport async function isFile(path: string): Promise<boolean> {\n try {\n const stat = await fs.stat(path);\n return stat.isFile();\n } catch {\n return false;\n }\n}\n\n/**\n * Gets the size of a file in bytes.\n *\n * @param {string} path - Path to the file.\n * @returns {Promise<number>} Size in bytes or -1 if file doesn't exist.\n */\nexport async function getFileSize(path: string): Promise<number> {\n try {\n const stat = await fs.stat(path);\n return stat.size;\n } catch {\n return -1;\n }\n}\n\n/**\n * Reads a file as a Buffer.\n *\n * @param {string} path - Path to the file.\n * @returns {Promise<Buffer | null>} File contents as Buffer or null if reading fails.\n */\nexport async function readFileBuffer(path: string): Promise<Buffer | null> {\n try {\n return await fs.readFile(path);\n } catch {\n return null;\n }\n}\n"]}
|