UNPKG

andrade-soulseek-downloader

Version:

Simple, safe Soulseek download library with built-in rate limiting to prevent bans

95 lines 3.24 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Shared = void 0; const events_1 = require("events"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const matches_1 = require("./matches"); const debug = (msg) => { if (process.env.DEBUG?.includes('slsk:shared')) { console.log(`[slsk:shared] ${msg}`); } }; class Shared extends events_1.EventEmitter { files = []; constructor() { super(); this.on('file', (file) => { const filePath = file.path; this.files.push({ key: filePath.slice(Math.max(filePath.length - 2, 1)).join(path.sep), value: { file: filePath.join(path.sep), size: file.size } }); }); } scanFolder(folder) { fs.readdir(folder, (err, files) => { if (err) { debug(`Folder ${folder} does not exist`); } else { files.forEach(file => { this.scan([folder, file]); }); debug(`Scan folder ${folder} completed, ${this.files.length} shared`); this.emit('complete', folder); } }); } scan(filePath) { const file = filePath.join(path.sep); const stats = fs.statSync(file); if (stats.isFile()) { this.emit('file', { path: filePath, size: stats.size }); } else { fs.readdirSync(file).forEach(it => { this.scan(filePath.concat([it])); }); } } search(query) { return this.files.filter(it => (0, matches_1.matches)(it.key, query)); } } exports.Shared = Shared; //# sourceMappingURL=shared.js.map