2025-05-29 20:40:33 -07:00
|
|
|
import { logger } from 'tools/dap/logger.js';
|
|
|
|
|
|
2025-12-30 00:57:37 -08:00
|
|
|
export class DapProtocolHandler
|
|
|
|
|
{
|
|
|
|
|
constructor(jsDebugger)
|
|
|
|
|
{
|
2025-05-29 20:40:33 -07:00
|
|
|
this.commands = {};
|
|
|
|
|
this.jsDebugger = jsDebugger;
|
|
|
|
|
this.logger = logger.getLogger("DAPProtocolHandler");
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 00:57:37 -08:00
|
|
|
registerCommand(name, fn)
|
|
|
|
|
{
|
2025-05-29 20:40:33 -07:00
|
|
|
this.logger.info(`Registering command: ${name}`);
|
|
|
|
|
this.commands[name] = fn;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 00:57:37 -08:00
|
|
|
handleRequest(req)
|
|
|
|
|
{
|
2025-05-29 20:40:33 -07:00
|
|
|
if (req.type !== 'request' || !req.command)
|
|
|
|
|
{
|
|
|
|
|
this.logger.error(`Invalid request: ${JSON.stringify(req)}`);
|
|
|
|
|
return this.errorResponse(req, 'Invalid request format');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handler = this.commands[req.command];
|
|
|
|
|
if (!handler)
|
|
|
|
|
return this.errorResponse(req, `Unknown command ${req.command}`);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
this.logger.info(`Handling command: ${req.command}`);
|
|
|
|
|
return handler(req);
|
|
|
|
|
}
|
2025-12-30 00:57:37 -08:00
|
|
|
catch(error)
|
2025-05-29 20:40:33 -07:00
|
|
|
{
|
|
|
|
|
this.logger.error(`Error handling command ${req.command}:`, error);
|
|
|
|
|
this.logger.error(uneval(error.stack));
|
|
|
|
|
return this.errorResponse(req, error.message || 'An error occurred while processing the request');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 00:57:37 -08:00
|
|
|
successResponse(req, result)
|
|
|
|
|
{
|
2025-05-29 20:40:33 -07:00
|
|
|
this.logger.info(`Response to ${req.command}`, result);
|
|
|
|
|
const response = {
|
|
|
|
|
'type': 'response',
|
|
|
|
|
'request_seq': req.seq,
|
|
|
|
|
'success': true,
|
|
|
|
|
'command': req.command,
|
|
|
|
|
'body': result
|
|
|
|
|
};
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 00:57:37 -08:00
|
|
|
errorResponse(req, error)
|
|
|
|
|
{
|
2025-05-29 20:40:33 -07:00
|
|
|
this.logger.error(`Error in ${req.command}: ${error}`);
|
|
|
|
|
const response = {
|
|
|
|
|
'type': 'response',
|
|
|
|
|
'request_seq': req.seq,
|
|
|
|
|
'success': false,
|
|
|
|
|
'command': req.command,
|
|
|
|
|
'body': {
|
|
|
|
|
'error': error
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
}
|