UNPKG

firewalla-mcp-server

Version:

Model Context Protocol (MCP) server for Firewalla MSP API - Provides real-time network monitoring, security analysis, and firewall management through 28 specialized tools compatible with any MCP client

111 lines 3.31 kB
/** * Simple utility functions replacing over-engineered Manager classes */ /** * Simple pagination - just limit results and track if there are more */ export function paginateResults(results, limit, cursor) { // For simplicity, treat cursor as offset const offset = cursor ? parseInt(cursor, 10) || 0 : 0; const startIndex = Math.max(0, offset); const endIndex = startIndex + limit; const data = results.slice(startIndex, endIndex); const hasMore = endIndex < results.length; const nextCursor = hasMore ? String(endIndex) : undefined; return { data, hasMore, nextCursor, count: data.length, }; } /** * Simple timeout wrapper for promises */ export async function withTimeout(promise, timeoutMs = 30000) { let timeoutId; const timeoutPromise = new Promise((_, reject) => { timeoutId = setTimeout(() => reject(new Error('Operation timed out')), timeoutMs); }); try { return await Promise.race([promise, timeoutPromise]); } finally { if (timeoutId) { clearTimeout(timeoutId); } } } /** * Simple retry mechanism */ export async function withRetry(operation, maxRetries = 3, delayMs = 1000) { let lastError = new Error('Unknown error'); for (let attempt = 0; attempt <= maxRetries; attempt++) { try { return await operation(); } catch (error) { lastError = error instanceof Error ? error : new Error(String(error)); if (attempt < maxRetries) { await new Promise(resolve => setTimeout(resolve, delayMs * (attempt + 1))); } } } throw lastError; } /** * Simple streaming helper - process items in chunks */ export async function processInChunks(items, processor, chunkSize = 100) { const results = []; for (let i = 0; i < items.length; i += chunkSize) { const chunk = items.slice(i, i + chunkSize); const chunkResults = await processor(chunk); results.push(...chunkResults); } return results; } /** * Simple bulk operation helper */ export async function executeBulkOperation(items, operation, batchSize = 10) { const successes = []; const failures = []; // Process in batches to avoid overwhelming the API for (let i = 0; i < items.length; i += batchSize) { const batch = items.slice(i, i + batchSize); const promises = batch.map(async (item) => { try { const result = await operation(item); successes.push(result); } catch (error) { failures.push({ item, error: error instanceof Error ? error : new Error(String(error)), }); } }); await Promise.all(promises); } return { successes, failures }; } /** * Simple response formatting */ export function formatResponse(data, meta) { return meta ? { data, meta } : { data }; } /** * Simple error formatting */ export function formatError(message, tool, details) { return { error: true, message, ...(tool && { tool }), ...(details && { validation_errors: details }), }; } //# sourceMappingURL=simple-utils.js.map