@nasriya/hypercloud
Version:
Nasriya HyperCloud is a lightweight Node.js HTTP2 framework.
62 lines (61 loc) • 2.57 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const helpers_1 = __importDefault(require("../../../utils/helpers"));
const constants_1 = __importDefault(require("./constants"));
const uploadHelpers = {
isStorageSize: (value) => {
if (helpers_1.default.is.realObject(value)) {
if ('value' in value) {
if (typeof value.value !== 'number') {
throw new TypeError(`The limit value must be a number, instead got ${typeof value.value}`);
}
if (value.value < 0) {
throw new RangeError(`The limit value must be a non-negative number`);
}
}
if ('unit' in value) {
if (helpers_1.default.isNot.validString(value.unit)) {
throw new TypeError(`The limit unit must be a string, instead got ${typeof value.unit}`);
}
const units = [...constants_1.default.storageUnitNames, ...constants_1.default.storageUnitAbbreviations];
if (!units.includes(value.unit)) {
throw new Error(`The limit unit you entered (${value.unit}) is not a valid storage unit`);
}
}
return true;
}
return false;
},
isStorageUnitAbbreviation: (value) => {
return constants_1.default.storageUnitAbbreviations.includes(value);
},
isStorageUnitName: (value) => {
return constants_1.default.storageUnitNames.includes(value);
},
convertToBytes: (value, unit) => {
const unitLower = unit.toLowerCase();
const key = Object.keys(constants_1.default.storageUnitsToBytes).find(u => u.toLowerCase() === unitLower);
if (!key) {
throw new Error(`Unsupported unit: ${unit}`);
}
return value * constants_1.default.storageUnitsToBytes[key];
},
getLimit(value) {
if (this.isStorageSize(value)) {
return this.convertToBytes(value.value, value.unit);
}
else {
if (typeof value !== 'number') {
throw new TypeError(`The limit value must be a number, instead got ${value}`);
}
if (value < 0) {
throw new RangeError(`The limit value must be a non-negative number`);
}
return value;
}
}
};
exports.default = uploadHelpers;
;