browser-connect-mcp
Version:
MCP server for browser DevTools and backend debugging - analyze console logs, network requests, and backend logs with AI assistance
146 lines • 5.38 kB
JavaScript
import logger from './logger.js';
export class BackendManager {
static instance;
connections = new Map();
MAX_LOGS_PER_SOURCE = 10000;
MAX_DEBUGGER_EVENTS = 5000;
constructor() { }
static getInstance() {
if (!BackendManager.instance) {
BackendManager.instance = new BackendManager();
}
return BackendManager.instance;
}
addLogs(source, logs) {
if (!this.connections.has(source)) {
this.connections.set(source, {
logs: [],
debuggerEvents: [],
debuggerScripts: new Map(),
breakpoints: new Map()
});
}
const connection = this.connections.get(source);
connection.logs.push(...logs);
// Keep only the latest logs if we exceed the limit
if (connection.logs.length > this.MAX_LOGS_PER_SOURCE) {
connection.logs = connection.logs.slice(-this.MAX_LOGS_PER_SOURCE);
}
logger.debug('Added logs', { source, count: logs.length, total: connection.logs.length });
}
getLogs(source) {
const connection = this.connections.get(source);
return connection?.logs || [];
}
addDebuggerConnection(key, client) {
if (!this.connections.has(key)) {
this.connections.set(key, {
logs: [],
debuggerEvents: [],
debuggerScripts: new Map(),
breakpoints: new Map()
});
}
const connection = this.connections.get(key);
connection.debuggerConnection = client;
logger.info('Added debugger connection', { key });
}
getDebuggerConnection(key) {
return this.connections.get(key)?.debuggerConnection;
}
addDebuggerEvent(key, event) {
const connection = this.connections.get(key);
if (!connection) {
logger.warn('No connection found for debugger event', { key });
return;
}
connection.debuggerEvents.push(event);
// Keep only the latest events if we exceed the limit
if (connection.debuggerEvents.length > this.MAX_DEBUGGER_EVENTS) {
connection.debuggerEvents = connection.debuggerEvents.slice(-this.MAX_DEBUGGER_EVENTS);
}
logger.debug('Added debugger event', { key, type: event.type });
}
getDebuggerEvents(key) {
const connection = this.connections.get(key);
return connection?.debuggerEvents || [];
}
addDebuggerScript(key, script) {
const connection = this.connections.get(key);
if (!connection) {
logger.warn('No connection found for debugger script', { key });
return;
}
connection.debuggerScripts.set(script.scriptId, script);
logger.debug('Added debugger script', { key, scriptId: script.scriptId, url: script.url });
}
getDebuggerScripts(key) {
const connection = this.connections.get(key);
if (!connection)
return [];
return Array.from(connection.debuggerScripts.values());
}
addBreakpoint(key, breakpointId, breakpoint) {
const connection = this.connections.get(key);
if (!connection) {
logger.warn('No connection found for breakpoint', { key });
return;
}
connection.breakpoints.set(breakpointId, breakpoint);
logger.debug('Added breakpoint', { key, breakpointId });
}
removeBreakpoint(key, breakpointId) {
const connection = this.connections.get(key);
if (!connection) {
logger.warn('No connection found for breakpoint removal', { key });
return;
}
connection.breakpoints.delete(breakpointId);
logger.debug('Removed breakpoint', { key, breakpointId });
}
getBreakpoints(key) {
const connection = this.connections.get(key);
if (!connection)
return [];
return Array.from(connection.breakpoints.values());
}
async disconnectDebugger(key) {
const connection = this.connections.get(key);
if (!connection?.debuggerConnection) {
logger.warn('No debugger connection to disconnect', { key });
return;
}
try {
await connection.debuggerConnection.close();
logger.info('Disconnected debugger', { key });
}
catch (error) {
logger.error('Error disconnecting debugger', { key, error });
}
// Clear the connection data
this.connections.delete(key);
}
clearLogs(source) {
const connection = this.connections.get(source);
if (connection) {
connection.logs = [];
logger.info('Cleared logs', { source });
}
}
getAllConnections() {
return Array.from(this.connections.keys());
}
getConnectionInfo(key) {
const connection = this.connections.get(key);
if (!connection)
return null;
return {
hasDebugger: !!connection.debuggerConnection,
logCount: connection.logs.length,
eventCount: connection.debuggerEvents.length,
scriptCount: connection.debuggerScripts.size,
breakpointCount: connection.breakpoints.size
};
}
}
//# sourceMappingURL=backend-manager.js.map