audiobookshelf/server/libs/watcher/utils.js

88 lines
6.4 KiB
JavaScript

"use strict";
/* IMPORT */
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const are_shallow_equal_1 = __importDefault(require("./are-shallow-equal"));
const debounce_1 = __importDefault(require("./debounce"));
const path_1 = __importDefault(require("path"));
const ripstat_1 = __importDefault(require("./ripstat"));
const tiny_readdir_1 = __importDefault(require("./tiny-readdir"));
const constants_1 = require("./constants");
/* UTILS */
const Utils = {
lang: {
areShallowEqual: //TODO: Import all these utilities from "nanodash" instead
are_shallow_equal_1.default,
debounce: debounce_1.default,
attempt: (fn) => {
try {
return fn();
}
catch (error) {
return Utils.lang.castError(error);
}
},
castArray: (x) => {
return Utils.lang.isArray(x) ? x : [x];
},
castError(exception) {
if (Utils.lang.isError(exception))
return exception;
if (Utils.lang.isString(exception))
return new Error(exception);
return new Error('Unknown error');
},
defer: (callback) => {
return setTimeout(callback, 0);
},
isArray: (x) => {
return Array.isArray(x);
},
isError(x) {
return x instanceof Error;
},
isFunction: (x) => {
return typeof x === 'function';
},
isNumber: (x) => {
return typeof x === 'number';
},
isString: (x) => {
return typeof x === 'string';
},
isUndefined: (x) => {
return x === undefined;
},
noop: () => {
return;
},
uniq: (arr) => {
if (arr.length < 2)
return arr;
return Array.from(new Set(arr));
}
},
fs: {
isSubPath: (targetPath, subPath) => {
return (subPath.startsWith(targetPath) && subPath[targetPath.length] === path_1.default.sep && (subPath.length - targetPath.length) > path_1.default.sep.length);
},
poll: (targetPath, timeout = constants_1.POLLING_TIMEOUT) => {
return ripstat_1.default(targetPath, timeout).catch(Utils.lang.noop);
},
readdir: async (rootPath, ignore, depth = Infinity, signal, readdirMap) => {
if (readdirMap && depth === 1 && rootPath in readdirMap) { // Reusing cached data
const result = readdirMap[rootPath];
return [result.directories, result.files];
}
else { // Retrieving fresh data
const result = await tiny_readdir_1.default(rootPath, { depth, ignore, signal });
return [result.directories, result.files];
}
}
}
};
/* EXPORT */
exports.default = Utils;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvdXRpbHMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUNBLFlBQVk7Ozs7O0FBRVosMEVBQWdEO0FBQ2hELHdEQUFnQztBQUNoQyxnREFBd0I7QUFDeEIsc0RBQThCO0FBQzlCLGdFQUFtQztBQUNuQywyQ0FBNEM7QUFHNUMsV0FBVztBQUVYLE1BQU0sS0FBSyxHQUFHO0lBRVosSUFBSSxFQUFFO1FBRUosZUFBZSxFQUZULDBEQUEwRDtRQUVoRSwyQkFBZTtRQUVmLFFBQVEsRUFBUixrQkFBUTtRQUVSLE9BQU8sRUFBRSxDQUFNLEVBQVcsRUFBYyxFQUFFO1lBRXhDLElBQUk7Z0JBRUYsT0FBTyxFQUFFLEVBQUcsQ0FBQzthQUVkO1lBQUMsT0FBUSxLQUFjLEVBQUc7Z0JBRXpCLE9BQU8sS0FBSyxDQUFDLElBQUksQ0FBQyxTQUFTLENBQUcsS0FBSyxDQUFFLENBQUM7YUFFdkM7UUFFSCxDQUFDO1FBRUQsU0FBUyxFQUFFLENBQU0sQ0FBVSxFQUFRLEVBQUU7WUFFbkMsT0FBTyxLQUFLLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBRyxDQUFDLENBQUUsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDO1FBRTVDLENBQUM7UUFFRCxTQUFTLENBQUcsU0FBa0I7WUFFNUIsSUFBSyxLQUFLLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBRyxTQUFTLENBQUU7Z0JBQUcsT0FBTyxTQUFTLENBQUM7WUFFekQsSUFBSyxLQUFLLENBQUMsSUFBSSxDQUFDLFFB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