driver.js 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.launchBrowserServer = launchBrowserServer;
  6. exports.printApiJson = printApiJson;
  7. exports.runDriver = runDriver;
  8. exports.runServer = runServer;
  9. var _fs = _interopRequireDefault(require("fs"));
  10. var playwright = _interopRequireWildcard(require("../.."));
  11. var _server = require("../server");
  12. var _transport = require("../protocol/transport");
  13. var _playwrightServer = require("../remote/playwrightServer");
  14. var _processLauncher = require("../utils/processLauncher");
  15. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  16. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  17. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  18. /**
  19. * Copyright (c) Microsoft Corporation.
  20. *
  21. * Licensed under the Apache License, Version 2.0 (the 'License");
  22. * you may not use this file except in compliance with the License.
  23. * You may obtain a copy of the License at
  24. *
  25. * http://www.apache.org/licenses/LICENSE-2.0
  26. *
  27. * Unless required by applicable law or agreed to in writing, software
  28. * distributed under the License is distributed on an "AS IS" BASIS,
  29. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  30. * See the License for the specific language governing permissions and
  31. * limitations under the License.
  32. */
  33. /* eslint-disable no-console */
  34. function printApiJson() {
  35. // Note: this file is generated by build-playwright-driver.sh
  36. console.log(JSON.stringify(require('../../api.json')));
  37. }
  38. function runDriver() {
  39. const dispatcherConnection = new _server.DispatcherConnection();
  40. new _server.RootDispatcher(dispatcherConnection, async (rootScope, {
  41. sdkLanguage
  42. }) => {
  43. const playwright = (0, _server.createPlaywright)({
  44. sdkLanguage
  45. });
  46. return new _server.PlaywrightDispatcher(rootScope, playwright);
  47. });
  48. const transport = new _transport.PipeTransport(process.stdout, process.stdin);
  49. transport.onmessage = message => dispatcherConnection.dispatch(JSON.parse(message));
  50. dispatcherConnection.onmessage = message => transport.send(JSON.stringify(message));
  51. transport.onclose = () => {
  52. // Drop any messages during shutdown on the floor.
  53. dispatcherConnection.onmessage = () => {};
  54. (0, _processLauncher.gracefullyProcessExitDoNotHang)(0);
  55. };
  56. // Ignore the SIGINT signal in the driver process so the parent can gracefully close the connection.
  57. // We still will destruct everything (close browsers and exit) when the transport pipe closes.
  58. process.on('SIGINT', () => {
  59. // Keep the process running.
  60. });
  61. }
  62. async function runServer(options) {
  63. const {
  64. port,
  65. host,
  66. path = '/',
  67. maxConnections = Infinity,
  68. extension
  69. } = options;
  70. const server = new _playwrightServer.PlaywrightServer({
  71. mode: extension ? 'extension' : 'default',
  72. path,
  73. maxConnections
  74. });
  75. const wsEndpoint = await server.listen(port, host);
  76. process.on('exit', () => server.close().catch(console.error));
  77. console.log('Listening on ' + wsEndpoint);
  78. process.stdin.on('close', () => (0, _processLauncher.gracefullyProcessExitDoNotHang)(0));
  79. }
  80. async function launchBrowserServer(browserName, configFile) {
  81. let options = {};
  82. if (configFile) options = JSON.parse(_fs.default.readFileSync(configFile).toString());
  83. const browserType = playwright[browserName];
  84. const server = await browserType.launchServer(options);
  85. console.log(server.wsEndpoint());
  86. }