outofprocess.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.start = start;
  6. var _connection = require("./client/connection");
  7. var _transport = require("./protocol/transport");
  8. var childProcess = _interopRequireWildcard(require("child_process"));
  9. var path = _interopRequireWildcard(require("path"));
  10. var _manualPromise = require("./utils/manualPromise");
  11. 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); }
  12. 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; }
  13. /**
  14. * Copyright (c) Microsoft Corporation.
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the "License");
  17. * you may not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS,
  24. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. async function start(env = {}) {
  29. const client = new PlaywrightClient(env);
  30. const playwright = await client._playwright;
  31. playwright.driverProcess = client._driverProcess;
  32. return {
  33. playwright,
  34. stop: () => client.stop()
  35. };
  36. }
  37. class PlaywrightClient {
  38. constructor(env) {
  39. this._playwright = void 0;
  40. this._driverProcess = void 0;
  41. this._closePromise = new _manualPromise.ManualPromise();
  42. this._driverProcess = childProcess.fork(path.join(__dirname, 'cli', 'cli.js'), ['run-driver'], {
  43. stdio: 'pipe',
  44. detached: true,
  45. env: {
  46. ...process.env,
  47. ...env
  48. }
  49. });
  50. this._driverProcess.unref();
  51. this._driverProcess.stderr.on('data', data => process.stderr.write(data));
  52. const connection = new _connection.Connection(undefined, undefined);
  53. const transport = new _transport.PipeTransport(this._driverProcess.stdin, this._driverProcess.stdout);
  54. connection.onmessage = message => transport.send(JSON.stringify(message));
  55. transport.onmessage = message => connection.dispatch(JSON.parse(message));
  56. transport.onclose = () => this._closePromise.resolve();
  57. this._playwright = connection.initializePlaywright();
  58. }
  59. async stop() {
  60. this._driverProcess.stdin.destroy();
  61. this._driverProcess.stdout.destroy();
  62. this._driverProcess.stderr.destroy();
  63. await this._closePromise;
  64. }
  65. }