outofprocess.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. var outofprocess_exports = {};
  30. __export(outofprocess_exports, {
  31. start: () => start
  32. });
  33. module.exports = __toCommonJS(outofprocess_exports);
  34. var childProcess = __toESM(require("child_process"));
  35. var import_path = __toESM(require("path"));
  36. var import_connection = require("./client/connection");
  37. var import_pipeTransport = require("./server/utils/pipeTransport");
  38. var import_manualPromise = require("./utils/isomorphic/manualPromise");
  39. var import_nodePlatform = require("./server/utils/nodePlatform");
  40. async function start(env = {}) {
  41. const client = new PlaywrightClient(env);
  42. const playwright = await client._playwright;
  43. playwright.driverProcess = client._driverProcess;
  44. return { playwright, stop: () => client.stop() };
  45. }
  46. class PlaywrightClient {
  47. constructor(env) {
  48. this._closePromise = new import_manualPromise.ManualPromise();
  49. this._driverProcess = childProcess.fork(import_path.default.join(__dirname, "..", "cli.js"), ["run-driver"], {
  50. stdio: "pipe",
  51. detached: true,
  52. env: {
  53. ...process.env,
  54. ...env
  55. }
  56. });
  57. this._driverProcess.unref();
  58. this._driverProcess.stderr.on("data", (data) => process.stderr.write(data));
  59. const connection = new import_connection.Connection(import_nodePlatform.nodePlatform);
  60. const transport = new import_pipeTransport.PipeTransport(this._driverProcess.stdin, this._driverProcess.stdout);
  61. connection.onmessage = (message) => transport.send(JSON.stringify(message));
  62. transport.onmessage = (message) => connection.dispatch(JSON.parse(message));
  63. transport.onclose = () => this._closePromise.resolve();
  64. this._playwright = connection.initializePlaywright();
  65. }
  66. async stop() {
  67. this._driverProcess.stdin.destroy();
  68. this._driverProcess.stdout.destroy();
  69. this._driverProcess.stderr.destroy();
  70. await this._closePromise;
  71. }
  72. }
  73. // Annotate the CommonJS export names for ESM import in node:
  74. 0 && (module.exports = {
  75. start
  76. });