container.ts
Version:
Modular application framework
93 lines • 3.42 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
var path = require("path");
var error_1 = require("../error");
var validate_1 = require("../validate");
/** NodeValidate error codes. */
var ENodeValidateError;
(function (ENodeValidateError) {
ENodeValidateError[ENodeValidateError["InvalidBuffer"] = 0] = "InvalidBuffer";
ENodeValidateError[ENodeValidateError["InvalidFile"] = 1] = "InvalidFile";
ENodeValidateError[ENodeValidateError["InvalidDirectory"] = 2] = "InvalidDirectory";
})(ENodeValidateError = exports.ENodeValidateError || (exports.ENodeValidateError = {}));
/** NodeValidate error chain class. */
var NodeValidateError = /** @class */ (function (_super) {
__extends(NodeValidateError, _super);
function NodeValidateError(code, value, cause) {
return _super.call(this, { name: ENodeValidateError[code], value: value }, cause) || this;
}
return NodeValidateError;
}(error_1.ErrorChain));
exports.NodeValidateError = NodeValidateError;
function isBuffer(value, options) {
if (value === void 0) { value = ""; }
if (options === void 0) { options = {}; }
var buf = null;
try {
buf = Buffer.from(value, options.encoding);
}
catch (error) {
throw new NodeValidateError(ENodeValidateError.InvalidBuffer, value, error);
}
if (buf == null) {
throw new NodeValidateError(ENodeValidateError.InvalidBuffer, value);
}
return buf;
}
exports.isBuffer = isBuffer;
function isFile(value) {
if (value === void 0) { value = ""; }
var isValid = false;
try {
value = path.resolve(value);
isValid = fs.lstatSync(value).isFile();
}
catch (error) {
throw new NodeValidateError(ENodeValidateError.InvalidFile, value, error);
}
if (!isValid) {
throw new NodeValidateError(ENodeValidateError.InvalidFile, value);
}
return value;
}
exports.isFile = isFile;
function isDirectory(value) {
if (value === void 0) { value = ""; }
var isValid = false;
try {
value = path.resolve(value);
isValid = fs.lstatSync(value).isDirectory();
}
catch (error) {
throw new NodeValidateError(ENodeValidateError.InvalidDirectory, value, error);
}
if (!isValid) {
throw new NodeValidateError(ENodeValidateError.InvalidDirectory, value);
}
return value;
}
exports.isDirectory = isDirectory;
/** Static validate methods container. */
var NodeValidate = /** @class */ (function (_super) {
__extends(NodeValidate, _super);
function NodeValidate() {
return _super !== null && _super.apply(this, arguments) || this;
}
NodeValidate.isBuffer = isBuffer;
NodeValidate.isFile = isFile;
NodeValidate.isDirectory = isDirectory;
return NodeValidate;
}(validate_1.Validate));
exports.NodeValidate = NodeValidate;
//# sourceMappingURL=NodeValidate.js.map