cdpConnection.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.CdpConnection = void 0;
  4. const log_js_1 = require("../utils/log.js");
  5. const cdpClient_js_1 = require("./cdpClient.js");
  6. /**
  7. * Represents a high-level CDP connection to the browser backend.
  8. * Manages a CdpClient instance for each active CDP session.
  9. */
  10. class CdpConnection {
  11. #transport;
  12. /** The CdpClient object attached to the root browser session. */
  13. #browserCdpClient;
  14. /** Map from session ID to CdpClient. */
  15. #sessionCdpClients = new Map();
  16. #commandCallbacks = new Map();
  17. #logger;
  18. #nextId = 0;
  19. constructor(transport, logger) {
  20. this.#transport = transport;
  21. this.#logger = logger;
  22. this.#transport.setOnMessage(this.#onMessage);
  23. this.#browserCdpClient = new cdpClient_js_1.CdpClient(this, undefined);
  24. }
  25. /** Closes the connection to the browser. */
  26. close() {
  27. this.#transport.close();
  28. for (const [, { reject, error }] of this.#commandCallbacks) {
  29. reject(error);
  30. }
  31. this.#commandCallbacks.clear();
  32. this.#sessionCdpClients.clear();
  33. }
  34. /** The CdpClient object attached to the root browser session. */
  35. browserClient() {
  36. return this.#browserCdpClient;
  37. }
  38. /**
  39. * Gets a CdpClient instance attached to the given session ID,
  40. * or null if the session is not attached.
  41. */
  42. getCdpClient(sessionId) {
  43. const cdpClient = this.#sessionCdpClients.get(sessionId);
  44. if (!cdpClient) {
  45. throw new Error('Unknown CDP session ID');
  46. }
  47. return cdpClient;
  48. }
  49. sendCommand(method, params, sessionId) {
  50. return new Promise((resolve, reject) => {
  51. const id = this.#nextId++;
  52. this.#commandCallbacks.set(id, {
  53. resolve,
  54. reject,
  55. error: new cdpClient_js_1.CloseError(`${method} ${JSON.stringify(params)} ${sessionId ?? ''} call rejected because the connection has been closed.`),
  56. });
  57. const cdpMessage = { id, method, params };
  58. if (sessionId) {
  59. cdpMessage.sessionId = sessionId;
  60. }
  61. const cdpMessageStr = JSON.stringify(cdpMessage);
  62. void this.#transport.sendMessage(cdpMessageStr)?.catch((error) => {
  63. this.#logger?.(`${log_js_1.LogType.cdp}:ERROR`, error);
  64. this.#transport.close();
  65. });
  66. this.#logger?.(`${log_js_1.LogType.cdp}:SEND ▸`, JSON.stringify(cdpMessage, null, 2));
  67. });
  68. }
  69. #onMessage = (message) => {
  70. const messageParsed = JSON.parse(message);
  71. const messagePretty = JSON.stringify(messageParsed, null, 2);
  72. this.#logger?.(`${log_js_1.LogType.cdp}:RECV ◂`, messagePretty);
  73. // Update client map if a session is attached or detached.
  74. // Listen for these events on every session.
  75. if (messageParsed.method === 'Target.attachedToTarget') {
  76. const { sessionId } = messageParsed.params;
  77. this.#sessionCdpClients.set(sessionId, new cdpClient_js_1.CdpClient(this, sessionId));
  78. }
  79. else if (messageParsed.method === 'Target.detachedFromTarget') {
  80. const { sessionId } = messageParsed.params;
  81. const client = this.#sessionCdpClients.get(sessionId);
  82. if (client) {
  83. this.#sessionCdpClients.delete(sessionId);
  84. }
  85. }
  86. if (messageParsed.id !== undefined) {
  87. // Handle command response.
  88. const callbacks = this.#commandCallbacks.get(messageParsed.id);
  89. this.#commandCallbacks.delete(messageParsed.id);
  90. if (callbacks) {
  91. if (messageParsed.result) {
  92. callbacks.resolve(messageParsed.result);
  93. }
  94. else if (messageParsed.error) {
  95. callbacks.reject(messageParsed.error);
  96. }
  97. }
  98. }
  99. else if (messageParsed.method) {
  100. const client = messageParsed.sessionId
  101. ? this.#sessionCdpClients.get(messageParsed.sessionId)
  102. : this.#browserCdpClient;
  103. client?.emit(messageParsed.method, messageParsed.params || {});
  104. }
  105. };
  106. }
  107. exports.CdpConnection = CdpConnection;
  108. //# sourceMappingURL=cdpConnection.js.map