@nasriya/atomix
Version:
Composable helper functions for building reliable systems
57 lines (56 loc) • 2.62 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAccessMode = getAccessMode;
exports.getAccessOptions = getAccessOptions;
const records_utils_1 = __importDefault(require("../../data-types/record/records-utils"));
const strings_utils_1 = __importDefault(require("../../data-types/string/strings-utils"));
const fs_1 = __importDefault(require("fs"));
function getAccessMode(permission) {
switch (permission) {
case 'Read': return fs_1.default.constants.R_OK;
case 'Write': return fs_1.default.constants.W_OK;
case 'Execute': return fs_1.default.constants.X_OK;
}
}
function getAccessOptions(options) {
const result = {
throwError: false,
mode: fs_1.default.constants.F_OK,
};
if (options !== undefined) {
if (!records_utils_1.default.guard.isRecord(options)) {
throw new Error(`The "options" parameter (when provided) should be a record, instead got ${typeof options}`);
}
if (records_utils_1.default.hasOwnProperty(options, "throwError")) {
if (typeof options.throwError !== "boolean") {
throw new TypeError(`The "throwError" property should be a boolean, instead got ${typeof options.throwError}`);
}
result.throwError = options.throwError;
}
if (records_utils_1.default.hasOwnProperty(options, "permissions")) {
const accepted = ['Read', 'Write', 'Execute'];
const valid = [];
if (strings_utils_1.default.guard.isString(options.permissions)) {
if (!accepted.includes(options.permissions)) {
throw new Error(`The "permissions" property should be one of the following: ${accepted.join(', ')}, instead got ${options.permissions}`);
}
valid.push(options.permissions);
}
if (Array.isArray(options.permissions)) {
for (const permission of options.permissions) {
if (!accepted.includes(permission)) {
throw new Error(`The "permissions" property should be one of the following: ${accepted.join(', ')}, instead got ${permission}`);
}
valid.push(permission);
}
}
for (const permission of valid) {
result.mode = result.mode | getAccessMode(permission);
}
}
}
return result;
}