ffConnection.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.kBrowserCloseMessageId = exports.FFSession = exports.FFConnection = exports.ConnectionEvents = void 0;
  6. var _events = require("events");
  7. var _debugLogger = require("../../common/debugLogger");
  8. var _helper = require("../helper");
  9. var _protocolError = require("../protocolError");
  10. /**
  11. * Copyright 2017 Google Inc. All rights reserved.
  12. * Modifications copyright (c) Microsoft Corporation.
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the 'License');
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an 'AS IS' BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. const ConnectionEvents = exports.ConnectionEvents = {
  27. Disconnected: Symbol('Disconnected')
  28. };
  29. // FFPlaywright uses this special id to issue Browser.close command which we
  30. // should ignore.
  31. const kBrowserCloseMessageId = exports.kBrowserCloseMessageId = -9999;
  32. class FFConnection extends _events.EventEmitter {
  33. constructor(transport, protocolLogger, browserLogsCollector) {
  34. super();
  35. this._lastId = void 0;
  36. this._transport = void 0;
  37. this._protocolLogger = void 0;
  38. this._browserLogsCollector = void 0;
  39. this._browserDisconnectedLogs = void 0;
  40. this.rootSession = void 0;
  41. this._sessions = void 0;
  42. this._closed = void 0;
  43. this.setMaxListeners(0);
  44. this._transport = transport;
  45. this._protocolLogger = protocolLogger;
  46. this._browserLogsCollector = browserLogsCollector;
  47. this._lastId = 0;
  48. this._sessions = new Map();
  49. this._closed = false;
  50. this.rootSession = new FFSession(this, '', message => this._rawSend(message));
  51. this._sessions.set('', this.rootSession);
  52. this._transport.onmessage = this._onMessage.bind(this);
  53. // onclose should be set last, since it can be immediately called.
  54. this._transport.onclose = this._onClose.bind(this);
  55. }
  56. nextMessageId() {
  57. return ++this._lastId;
  58. }
  59. _rawSend(message) {
  60. this._protocolLogger('send', message);
  61. this._transport.send(message);
  62. }
  63. async _onMessage(message) {
  64. this._protocolLogger('receive', message);
  65. if (message.id === kBrowserCloseMessageId) return;
  66. const session = this._sessions.get(message.sessionId || '');
  67. if (session) session.dispatchMessage(message);
  68. }
  69. _onClose() {
  70. this._closed = true;
  71. this._transport.onmessage = undefined;
  72. this._transport.onclose = undefined;
  73. this._browserDisconnectedLogs = _helper.helper.formatBrowserLogs(this._browserLogsCollector.recentLogs());
  74. this.rootSession.dispose();
  75. Promise.resolve().then(() => this.emit(ConnectionEvents.Disconnected));
  76. }
  77. close() {
  78. if (!this._closed) this._transport.close();
  79. }
  80. createSession(sessionId) {
  81. const session = new FFSession(this, sessionId, message => this._rawSend({
  82. ...message,
  83. sessionId
  84. }));
  85. this._sessions.set(sessionId, session);
  86. return session;
  87. }
  88. }
  89. exports.FFConnection = FFConnection;
  90. class FFSession extends _events.EventEmitter {
  91. constructor(connection, sessionId, rawSend) {
  92. super();
  93. this._connection = void 0;
  94. this._disposed = false;
  95. this._callbacks = void 0;
  96. this._sessionId = void 0;
  97. this._rawSend = void 0;
  98. this._crashed = false;
  99. this.on = void 0;
  100. this.addListener = void 0;
  101. this.off = void 0;
  102. this.removeListener = void 0;
  103. this.once = void 0;
  104. this.setMaxListeners(0);
  105. this._callbacks = new Map();
  106. this._connection = connection;
  107. this._sessionId = sessionId;
  108. this._rawSend = rawSend;
  109. this.on = super.on;
  110. this.addListener = super.addListener;
  111. this.off = super.removeListener;
  112. this.removeListener = super.removeListener;
  113. this.once = super.once;
  114. }
  115. markAsCrashed() {
  116. this._crashed = true;
  117. }
  118. async send(method, params) {
  119. if (this._crashed || this._disposed || this._connection._closed || this._connection._browserDisconnectedLogs) throw new _protocolError.ProtocolError(this._crashed ? 'crashed' : 'closed', undefined, this._connection._browserDisconnectedLogs);
  120. const id = this._connection.nextMessageId();
  121. this._rawSend({
  122. method,
  123. params,
  124. id
  125. });
  126. return new Promise((resolve, reject) => {
  127. this._callbacks.set(id, {
  128. resolve,
  129. reject,
  130. error: new _protocolError.ProtocolError('error', method)
  131. });
  132. });
  133. }
  134. sendMayFail(method, params) {
  135. return this.send(method, params).catch(error => _debugLogger.debugLogger.log('error', error));
  136. }
  137. dispatchMessage(object) {
  138. if (object.id) {
  139. const callback = this._callbacks.get(object.id);
  140. // Callbacks could be all rejected if someone has called `.dispose()`.
  141. if (callback) {
  142. this._callbacks.delete(object.id);
  143. if (object.error) {
  144. callback.error.setMessage(object.error.message);
  145. callback.reject(callback.error);
  146. } else {
  147. callback.resolve(object.result);
  148. }
  149. }
  150. } else {
  151. Promise.resolve().then(() => this.emit(object.method, object.params));
  152. }
  153. }
  154. dispose() {
  155. this._disposed = true;
  156. this._connection._sessions.delete(this._sessionId);
  157. for (const callback of this._callbacks.values()) {
  158. callback.error.type = this._crashed ? 'crashed' : 'closed';
  159. callback.error.logs = this._connection._browserDisconnectedLogs;
  160. callback.reject(callback.error);
  161. }
  162. this._callbacks.clear();
  163. }
  164. }
  165. exports.FFSession = FFSession;