driver.js 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 driver_exports = {};
  30. __export(driver_exports, {
  31. launchBrowserServer: () => launchBrowserServer,
  32. printApiJson: () => printApiJson,
  33. runDriver: () => runDriver,
  34. runServer: () => runServer
  35. });
  36. module.exports = __toCommonJS(driver_exports);
  37. var import_fs = __toESM(require("fs"));
  38. var playwright = __toESM(require("../.."));
  39. var import_pipeTransport = require("../server/utils/pipeTransport");
  40. var import_playwrightServer = require("../remote/playwrightServer");
  41. var import_server = require("../server");
  42. var import_processLauncher = require("../server/utils/processLauncher");
  43. function printApiJson() {
  44. console.log(JSON.stringify(require("../../api.json")));
  45. }
  46. function runDriver() {
  47. const dispatcherConnection = new import_server.DispatcherConnection();
  48. new import_server.RootDispatcher(dispatcherConnection, async (rootScope, { sdkLanguage }) => {
  49. const playwright2 = (0, import_server.createPlaywright)({ sdkLanguage });
  50. return new import_server.PlaywrightDispatcher(rootScope, playwright2);
  51. });
  52. const transport = new import_pipeTransport.PipeTransport(process.stdout, process.stdin);
  53. transport.onmessage = (message) => dispatcherConnection.dispatch(JSON.parse(message));
  54. const isJavaScriptLanguageBinding = !process.env.PW_LANG_NAME || process.env.PW_LANG_NAME === "javascript";
  55. const replacer = !isJavaScriptLanguageBinding && String.prototype.toWellFormed ? (key, value) => {
  56. if (typeof value === "string")
  57. return value.toWellFormed();
  58. return value;
  59. } : void 0;
  60. dispatcherConnection.onmessage = (message) => transport.send(JSON.stringify(message, replacer));
  61. transport.onclose = () => {
  62. dispatcherConnection.onmessage = () => {
  63. };
  64. (0, import_processLauncher.gracefullyProcessExitDoNotHang)(0);
  65. };
  66. process.on("SIGINT", () => {
  67. });
  68. }
  69. async function runServer(options) {
  70. const {
  71. port,
  72. host,
  73. path = "/",
  74. maxConnections = Infinity,
  75. extension
  76. } = options;
  77. const server = new import_playwrightServer.PlaywrightServer({ mode: extension ? "extension" : "default", path, maxConnections });
  78. const wsEndpoint = await server.listen(port, host);
  79. process.on("exit", () => server.close().catch(console.error));
  80. console.log("Listening on " + wsEndpoint);
  81. process.stdin.on("close", () => (0, import_processLauncher.gracefullyProcessExitDoNotHang)(0));
  82. }
  83. async function launchBrowserServer(browserName, configFile) {
  84. let options = {};
  85. if (configFile)
  86. options = JSON.parse(import_fs.default.readFileSync(configFile).toString());
  87. const browserType = playwright[browserName];
  88. const server = await browserType.launchServer(options);
  89. console.log(server.wsEndpoint());
  90. }
  91. // Annotate the CommonJS export names for ESM import in node:
  92. 0 && (module.exports = {
  93. launchBrowserServer,
  94. printApiJson,
  95. runDriver,
  96. runServer
  97. });