BidiServer.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. /**
  3. * Copyright 2021 Google LLC.
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.BidiServer = void 0;
  20. const EventEmitter_js_1 = require("../utils/EventEmitter.js");
  21. const log_js_1 = require("../utils/log.js");
  22. const processingQueue_js_1 = require("../utils/processingQueue.js");
  23. const CommandProcessor_js_1 = require("./CommandProcessor.js");
  24. const browsingContextStorage_js_1 = require("./domains/context/browsingContextStorage.js");
  25. const EventManager_js_1 = require("./domains/events/EventManager.js");
  26. const realmStorage_js_1 = require("./domains/script/realmStorage.js");
  27. class BidiServer extends EventEmitter_js_1.EventEmitter {
  28. #messageQueue;
  29. #transport;
  30. #commandProcessor;
  31. #browsingContextStorage = new browsingContextStorage_js_1.BrowsingContextStorage();
  32. #realmStorage = new realmStorage_js_1.RealmStorage();
  33. #logger;
  34. #handleIncomingMessage = (message) => {
  35. void this.#commandProcessor.processCommand(message).catch((error) => {
  36. this.#logger?.(log_js_1.LogType.system, error);
  37. });
  38. };
  39. #processOutgoingMessage = async (messageEntry) => {
  40. const message = messageEntry.message;
  41. if (messageEntry.channel !== null) {
  42. message['channel'] = messageEntry.channel;
  43. }
  44. await this.#transport.sendMessage(message);
  45. };
  46. constructor(bidiTransport, cdpConnection, selfTargetId, parser, logger) {
  47. super();
  48. this.#logger = logger;
  49. this.#messageQueue = new processingQueue_js_1.ProcessingQueue(this.#processOutgoingMessage, this.#logger);
  50. this.#transport = bidiTransport;
  51. this.#transport.setOnMessage(this.#handleIncomingMessage);
  52. this.#commandProcessor = new CommandProcessor_js_1.CommandProcessor(cdpConnection, new EventManager_js_1.EventManager(this), selfTargetId, parser, this.#browsingContextStorage, this.#realmStorage, this.#logger);
  53. this.#commandProcessor.on('response', (response) => {
  54. this.emitOutgoingMessage(response);
  55. });
  56. }
  57. static async createAndStart(bidiTransport, cdpConnection, selfTargetId, parser, logger) {
  58. const server = new BidiServer(bidiTransport, cdpConnection, selfTargetId, parser, logger);
  59. const cdpClient = cdpConnection.browserClient();
  60. // Needed to get events about new targets.
  61. await cdpClient.sendCommand('Target.setDiscoverTargets', { discover: true });
  62. // Needed to automatically attach to new targets.
  63. await cdpClient.sendCommand('Target.setAutoAttach', {
  64. autoAttach: true,
  65. waitForDebuggerOnStart: true,
  66. flatten: true,
  67. });
  68. await server.topLevelContextsLoaded();
  69. return server;
  70. }
  71. async topLevelContextsLoaded() {
  72. await Promise.all(this.#browsingContextStorage
  73. .getTopLevelContexts()
  74. .map((c) => c.awaitLoaded()));
  75. }
  76. /**
  77. * Sends BiDi message.
  78. */
  79. emitOutgoingMessage(messageEntry) {
  80. this.#messageQueue.add(messageEntry);
  81. }
  82. close() {
  83. this.#transport.close();
  84. }
  85. getBrowsingContextStorage() {
  86. return this.#browsingContextStorage;
  87. }
  88. }
  89. exports.BidiServer = BidiServer;
  90. //# sourceMappingURL=BidiServer.js.map