@spaik/mcp-server-roi
Version:
MCP server for AI ROI prediction and tracking with Monte Carlo simulations
64 lines • 2.9 kB
JavaScript
import { mcpDb } from './supabase.js';
import { rateLimiters } from '../utils/rate-limiter.js';
import { createLogger } from '../utils/logger.js';
const logger = createLogger({ module: 'rate-limited-db' });
/**
* Wraps Supabase client methods with rate limiting
*/
export function createRateLimitedClient(client) {
// Create a proxy to intercept method calls
return new Proxy(client, {
get(target, prop) {
const original = target[prop];
// Only wrap the 'from' and 'rpc' methods
if (prop === 'from') {
return function (...args) {
const query = original.apply(target, args);
// Wrap the query execution methods
const wrapMethod = (method) => {
const originalMethod = query[method];
if (typeof originalMethod === 'function') {
query[method] = function (...methodArgs) {
const result = originalMethod.apply(query, methodArgs);
// If it returns a promise-like object with 'then', wrap it
if (result && typeof result.then === 'function') {
const originalThen = result.then.bind(result);
result.then = function (onfulfilled, onrejected) {
return rateLimiters.supabase.executeWithRateLimit(() => originalThen(onfulfilled, onrejected), {
priority: method === 'insert' || method === 'update' ? 'high' : 'normal'
});
};
}
return result;
};
}
};
// Wrap common query methods
['select', 'insert', 'update', 'delete', 'upsert'].forEach(wrapMethod);
return query;
};
}
if (prop === 'rpc') {
return function (...args) {
return rateLimiters.supabase.executeWithRateLimit(() => original.apply(target, args), { priority: 'normal' });
};
}
// Return original for other properties
if (typeof original === 'function') {
return original.bind(target);
}
return original;
}
});
}
// Export rate-limited version of mcpDb
export const rateLimitedMcpDb = createRateLimitedClient(mcpDb);
// Log initialization
logger.info('Rate-limited database client initialized', {
limits: {
maxRequests: 500,
windowMs: 60000,
maxConcurrent: 20
}
});
//# sourceMappingURL=rate-limited-client.js.map