UNPKG

@nodedaemon/core

Version:

Production-ready Node.js process manager with zero external dependencies

135 lines 4.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateId = generateId; exports.ensureDir = ensureDir; exports.ensureFileDir = ensureFileDir; exports.isFile = isFile; exports.isDirectory = isDirectory; exports.parseMemoryString = parseMemoryString; exports.formatMemory = formatMemory; exports.formatUptime = formatUptime; exports.calculateExponentialBackoff = calculateExponentialBackoff; exports.debounce = debounce; exports.throttle = throttle; exports.sanitizeProcessName = sanitizeProcessName; exports.validateProcessConfig = validateProcessConfig; const crypto_1 = require("crypto"); const fs_1 = require("fs"); const path_1 = require("path"); function generateId() { return (0, crypto_1.randomUUID)(); } function ensureDir(dirPath) { if (!(0, fs_1.existsSync)(dirPath)) { (0, fs_1.mkdirSync)(dirPath, { recursive: true }); } } function ensureFileDir(filePath) { ensureDir((0, path_1.dirname)(filePath)); } function isFile(path) { try { return (0, fs_1.existsSync)(path) && (0, fs_1.statSync)(path).isFile(); } catch { return false; } } function isDirectory(path) { try { return (0, fs_1.existsSync)(path) && (0, fs_1.statSync)(path).isDirectory(); } catch { return false; } } function parseMemoryString(memory) { const units = { 'B': 1, 'KB': 1024, 'MB': 1024 * 1024, 'GB': 1024 * 1024 * 1024 }; const match = memory.match(/^(\d+(?:\.\d+)?)\s*(B|KB|MB|GB)$/i); if (!match || !match[1] || !match[2]) { throw new Error(`Invalid memory format: ${memory}`); } const [, value, unit] = match; return Math.floor(parseFloat(value) * units[unit.toUpperCase()]); } function formatMemory(bytes) { if (bytes === 0) return '0 B'; const k = 1024; const sizes = ['B', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`; } function formatUptime(ms) { const seconds = Math.floor(ms / 1000) % 60; const minutes = Math.floor(ms / (1000 * 60)) % 60; const hours = Math.floor(ms / (1000 * 60 * 60)) % 24; const days = Math.floor(ms / (1000 * 60 * 60 * 24)); if (days > 0) { return `${days}d ${hours}h ${minutes}m ${seconds}s`; } if (hours > 0) { return `${hours}h ${minutes}m ${seconds}s`; } if (minutes > 0) { return `${minutes}m ${seconds}s`; } return `${seconds}s`; } function calculateExponentialBackoff(restartCount, baseDelay, maxDelay) { const delay = baseDelay * Math.pow(2, restartCount); return Math.min(delay, maxDelay); } function debounce(func, wait) { let timeout = null; return (...args) => { if (timeout) { clearTimeout(timeout); } timeout = setTimeout(() => { timeout = null; func.apply(null, args); }, wait); }; } function throttle(func, limit) { let inThrottle = false; return (...args) => { if (!inThrottle) { func.apply(null, args); inThrottle = true; setTimeout(() => inThrottle = false, limit); } }; } function sanitizeProcessName(name) { return name.replace(/[^a-zA-Z0-9-_]/g, '_'); } function validateProcessConfig(config) { if (!config || typeof config !== 'object') { throw new Error('Process config must be an object'); } if (!config.script || typeof config.script !== 'string') { throw new Error('Process config must have a script property'); } if (!isFile(config.script)) { throw new Error(`Script file does not exist: ${config.script}`); } if (config.instances !== undefined) { if (config.instances !== 'max' && (!Number.isInteger(config.instances) || config.instances < 1)) { throw new Error('instances must be a positive integer or "max"'); } } if (config.maxRestarts !== undefined) { if (!Number.isInteger(config.maxRestarts) || config.maxRestarts < 0) { throw new Error('maxRestarts must be a non-negative integer'); } } } //# sourceMappingURL=helpers.js.map