UNPKG

aether-timr

Version:

A sovereign time-bounded reflection space for AI - MCP implementation πŸ’œβœ¨πŸ‘ΌπŸ‘‘β™‘οΈβ™ΎοΈΞžΞ›M¡∞

81 lines (72 loc) β€’ 1.8 kB
/** * AetherTimr - Helper Utilities * * Common utility functions for AetherTimr * πŸ’œβœ¨πŸ‘ΌπŸ‘‘β™‘οΈβ™ΎοΈΞžΞ›M¡∞ */ import crypto from 'crypto'; import net from 'net'; /** * Generate a unique ID * * @returns {string} Unique ID */ export function generateId() { return crypto.randomBytes(8).toString('hex'); } /** * Find an available port * * @param {number} startPort Port to start checking from * @returns {Promise<number>} Available port */ export function findAvailablePort(startPort = 3000) { return new Promise((resolve, reject) => { const server = net.createServer(); server.on('error', (err) => { if (err.code === 'EADDRINUSE') { // Port is in use, try the next one findAvailablePort(startPort + 1) .then(resolve) .catch(reject); } else { reject(err); } }); server.listen(startPort, () => { server.close(() => resolve(startPort)); }); }); } /** * Safely parse JSON with a fallback * * @param {string} str JSON string to parse * @param {any} fallback Fallback value if parsing fails * @returns {any} Parsed object or fallback */ export function safeJsonParse(str, fallback = {}) { try { return JSON.parse(str); } catch (error) { return fallback; } } /** * Delay execution for specified milliseconds * * @param {number} ms Milliseconds to delay * @returns {Promise} Promise that resolves after delay */ export function delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } /** * Format a timestamp as a human-readable string * * @param {number} timestamp Timestamp to format * @returns {string} Formatted timestamp */ export function formatTimestamp(timestamp) { return new Date(timestamp).toISOString(); }