vmind_dir_scan
Version:
A library to scan the file system based on a root folder. The library will scan recursively and send a callback for every file and directory it finds, including details such as age, name, path, and size.
124 lines (123 loc) • 6.8 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileSysScanHandler = void 0;
const promises_1 = require("fs/promises");
const events_1 = require("events");
const path_1 = __importDefault(require("path"));
const fsEvents_1 = require("./model/fsEvents");
class FileSysScanHandler extends events_1.EventEmitter {
constructor(rootDir) {
super();
this.rootDir = rootDir;
}
/**
* Iterative scan using a shared work queue and fixed worker concurrency.
*/
scanIterative() {
return __awaiter(this, arguments, void 0, function* (workerCount = 10, scan_params) {
// The queue holds directories that still need scanning.
const queue = [this.rootDir];
const isOlderThanThreshold = (fsEvent) => {
if (!scan_params)
return true;
return scan_params.mode === "days"
? fsEvent.ageInDays >= scan_params.age
: fsEvent.ageInSeconds >= scan_params.age;
};
// Worker function: each worker continuously grabs a directory from the queue.
const worker = () => __awaiter(this, void 0, void 0, function* () {
var _a, e_1, _b, _c;
while (true) {
// Get the next directory. Use shift() to remove from the queue.
const currentDir = queue.shift();
if (!currentDir) {
// If the queue is empty, exit the loop.
break;
}
try {
const dir = yield (0, promises_1.opendir)(currentDir);
try {
// Process each entry in the directory.
for (var _d = true, dir_1 = (e_1 = void 0, __asyncValues(dir)), dir_1_1; dir_1_1 = yield dir_1.next(), _a = dir_1_1.done, !_a; _d = true) {
_c = dir_1_1.value;
_d = false;
const dirent = _c;
const filePath = path_1.default.join(currentDir, dirent.name);
let fileStats;
try {
fileStats = yield (0, promises_1.stat)(filePath);
}
catch (statError) {
// Emit an error event for stat errors and skip this entry.
this.emit(fsEvents_1.FS_TYPE.ERROR, `stat error for ${filePath}: ${statError.message}`);
continue;
}
const ageInSeconds = Math.floor((Date.now() - fileStats.mtimeMs) / 1000);
const ageInDays = Math.floor(ageInSeconds / 86400);
const fsEvent = {
name: dirent.name,
path: filePath,
type: dirent.isDirectory() ? fsEvents_1.FS_TYPE.DIR : fsEvents_1.FS_TYPE.FILE,
size: fileStats.size || 0,
ageInDays,
ageInSeconds,
};
if (dirent.isDirectory()) {
// Emit an event for the directory and add it to the queue.
if (isOlderThanThreshold(fsEvent)) {
this.emit(fsEvents_1.FS_TYPE.DIR, fsEvent);
}
queue.push(filePath);
}
else {
// Emit an event for the file.
if (isOlderThanThreshold(fsEvent)) {
this.emit(fsEvents_1.FS_TYPE.FILE, fsEvent);
}
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = dir_1.return)) yield _b.call(dir_1);
}
finally { if (e_1) throw e_1.error; }
}
}
catch (opendirError) {
// Emit an error event if opening the directory fails.
this.emit(fsEvents_1.FS_TYPE.ERROR, `opendir error for ${currentDir}: ${opendirError.message}`);
}
}
});
// Create an array of worker promises.
const workers = [];
for (let i = 0; i < workerCount; i++) {
workers.push(worker());
}
// Wait until all workers complete.
yield Promise.all(workers);
});
}
}
exports.FileSysScanHandler = FileSysScanHandler;