browserServerImpl.js 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.BrowserServerLauncherImpl = void 0;
  6. var _utilsBundle = require("./utilsBundle");
  7. var _clientHelper = require("./client/clientHelper");
  8. var _utils = require("./utils");
  9. var _instrumentation = require("./server/instrumentation");
  10. var _playwright = require("./server/playwright");
  11. var _playwrightServer = require("./remote/playwrightServer");
  12. var _helper = require("./server/helper");
  13. var _stackTrace = require("./utils/stackTrace");
  14. var _socksProxy = require("./common/socksProxy");
  15. /**
  16. * Copyright (c) Microsoft Corporation.
  17. *
  18. * Licensed under the Apache License, Version 2.0 (the 'License");
  19. * you may not use this file except in compliance with the License.
  20. * You may obtain a copy of the License at
  21. *
  22. * http://www.apache.org/licenses/LICENSE-2.0
  23. *
  24. * Unless required by applicable law or agreed to in writing, software
  25. * distributed under the License is distributed on an "AS IS" BASIS,
  26. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27. * See the License for the specific language governing permissions and
  28. * limitations under the License.
  29. */
  30. class BrowserServerLauncherImpl {
  31. constructor(browserName) {
  32. this._browserName = void 0;
  33. this._browserName = browserName;
  34. }
  35. async launchServer(options = {}) {
  36. const playwright = (0, _playwright.createPlaywright)({
  37. sdkLanguage: 'javascript',
  38. isServer: true
  39. });
  40. // TODO: enable socks proxy once ipv6 is supported.
  41. const socksProxy = false ? new _socksProxy.SocksProxy() : undefined;
  42. playwright.options.socksProxyPort = await (socksProxy === null || socksProxy === void 0 ? void 0 : socksProxy.listen(0));
  43. // 1. Pre-launch the browser
  44. const metadata = (0, _instrumentation.serverSideCallMetadata)();
  45. const browser = await playwright[this._browserName].launch(metadata, {
  46. ...options,
  47. ignoreDefaultArgs: Array.isArray(options.ignoreDefaultArgs) ? options.ignoreDefaultArgs : undefined,
  48. ignoreAllDefaultArgs: !!options.ignoreDefaultArgs && !Array.isArray(options.ignoreDefaultArgs),
  49. env: options.env ? (0, _clientHelper.envObjectToArray)(options.env) : undefined
  50. }, toProtocolLogger(options.logger)).catch(e => {
  51. const log = _helper.helper.formatBrowserLogs(metadata.log);
  52. (0, _stackTrace.rewriteErrorMessage)(e, `${e.message} Failed to launch browser.${log}`);
  53. throw e;
  54. });
  55. const path = options.wsPath ? options.wsPath.startsWith('/') ? options.wsPath : `/${options.wsPath}` : `/${(0, _utils.createGuid)()}`;
  56. // 2. Start the server
  57. const server = new _playwrightServer.PlaywrightServer({
  58. mode: 'launchServer',
  59. path,
  60. maxConnections: Infinity,
  61. preLaunchedBrowser: browser,
  62. preLaunchedSocksProxy: socksProxy
  63. });
  64. const wsEndpoint = await server.listen(options.port);
  65. // 3. Return the BrowserServer interface
  66. const browserServer = new _utilsBundle.ws.EventEmitter();
  67. browserServer.process = () => browser.options.browserProcess.process;
  68. browserServer.wsEndpoint = () => wsEndpoint;
  69. browserServer.close = () => browser.options.browserProcess.close();
  70. browserServer[Symbol.asyncDispose] = browserServer.close;
  71. browserServer.kill = () => browser.options.browserProcess.kill();
  72. browserServer._disconnectForTest = () => server.close();
  73. browserServer._userDataDirForTest = browser._userDataDirForTest;
  74. browser.options.browserProcess.onclose = (exitCode, signal) => {
  75. socksProxy === null || socksProxy === void 0 ? void 0 : socksProxy.close().catch(() => {});
  76. server.close();
  77. browserServer.emit('close', exitCode, signal);
  78. };
  79. return browserServer;
  80. }
  81. }
  82. exports.BrowserServerLauncherImpl = BrowserServerLauncherImpl;
  83. function toProtocolLogger(logger) {
  84. return logger ? (direction, message) => {
  85. if (logger.isEnabled('protocol', 'verbose')) logger.log('protocol', 'verbose', (direction === 'send' ? 'SEND ► ' : '◀ RECV ') + JSON.stringify(message), [], {});
  86. } : undefined;
  87. }