session.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. var events = require('events'),
  2. convert = require('./convert.js'),
  3. DebuggerClient = require('./DebuggerClient').DebuggerClient,
  4. ScriptManager = require('./ScriptManager').ScriptManager,
  5. FrontendClient = require('./FrontendClient').FrontendClient,
  6. FrontendCommandHandler = require('./FrontendCommandHandler').FrontendCommandHandler,
  7. BreakEventHandler = require('./BreakEventHandler').BreakEventHandler;
  8. ///////////////////////////////////////////////////////////
  9. // exports
  10. exports.create = function(debuggerPort, config) {
  11. var sessionInstance,
  12. scriptManager,
  13. frontendCommandHandler,
  14. frontendClient,
  15. debuggerClient,
  16. breakEventHandler;
  17. function onDebuggerClientClose(reason) {
  18. if (frontendClient.isConnected)
  19. frontendClient.sendInspectorDetached(reason);
  20. sessionInstance.close();
  21. }
  22. function onDebuggerClientError(e) {
  23. var err = e.toString();
  24. if (e.helpString) {
  25. err += '\n' + e.helpString;
  26. }
  27. frontendClient.sendLogToConsole('error', err);
  28. }
  29. sessionInstance = Object.create(events.EventEmitter.prototype, {
  30. close: {
  31. value: function()
  32. {
  33. debuggerClient.close();
  34. this.emit('close');
  35. }
  36. },
  37. join: {
  38. value: function(wsConnection) {
  39. frontendClient = new FrontendClient(wsConnection);
  40. debuggerClient = new DebuggerClient(debuggerPort);
  41. scriptManager = new ScriptManager(
  42. config.isScriptHidden,
  43. frontendClient,
  44. debuggerClient
  45. );
  46. breakEventHandler = new BreakEventHandler(
  47. config,
  48. frontendClient,
  49. debuggerClient,
  50. scriptManager
  51. );
  52. frontendCommandHandler = new FrontendCommandHandler(
  53. config,
  54. frontendClient,
  55. debuggerClient,
  56. breakEventHandler,
  57. scriptManager);
  58. frontendClient.on('close', this.close.bind(this));
  59. debuggerClient.on('close', onDebuggerClientClose);
  60. debuggerClient.on('error', onDebuggerClientError);
  61. }
  62. }
  63. });
  64. return sessionInstance;
  65. };