webServerPlugin.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 webServerPlugin_exports = {};
  30. __export(webServerPlugin_exports, {
  31. WebServerPlugin: () => WebServerPlugin,
  32. webServer: () => webServer,
  33. webServerPluginsForConfig: () => webServerPluginsForConfig
  34. });
  35. module.exports = __toCommonJS(webServerPlugin_exports);
  36. var import_net = __toESM(require("net"));
  37. var import_path = __toESM(require("path"));
  38. var import_utils = require("playwright-core/lib/utils");
  39. var import_utils2 = require("playwright-core/lib/utils");
  40. var import_utilsBundle = require("playwright-core/lib/utilsBundle");
  41. const DEFAULT_ENVIRONMENT_VARIABLES = {
  42. "BROWSER": "none",
  43. // Disable that create-react-app will open the page in the browser
  44. "FORCE_COLOR": "1",
  45. "DEBUG_COLORS": "1"
  46. };
  47. const debugWebServer = (0, import_utilsBundle.debug)("pw:webserver");
  48. class WebServerPlugin {
  49. constructor(options, checkPortOnly) {
  50. this.name = "playwright:webserver";
  51. this._options = options;
  52. this._checkPortOnly = checkPortOnly;
  53. }
  54. async setup(config, configDir, reporter) {
  55. this._reporter = reporter;
  56. this._isAvailableCallback = this._options.url ? getIsAvailableFunction(this._options.url, this._checkPortOnly, !!this._options.ignoreHTTPSErrors, this._reporter.onStdErr?.bind(this._reporter)) : void 0;
  57. this._options.cwd = this._options.cwd ? import_path.default.resolve(configDir, this._options.cwd) : configDir;
  58. try {
  59. await this._startProcess();
  60. await this._waitForProcess();
  61. } catch (error) {
  62. await this.teardown();
  63. throw error;
  64. }
  65. }
  66. async teardown() {
  67. debugWebServer(`Terminating the WebServer`);
  68. await this._killProcess?.();
  69. debugWebServer(`Terminated the WebServer`);
  70. }
  71. async _startProcess() {
  72. let processExitedReject = (error) => {
  73. };
  74. this._processExitedPromise = new Promise((_, reject) => processExitedReject = reject);
  75. const isAlreadyAvailable = await this._isAvailableCallback?.();
  76. if (isAlreadyAvailable) {
  77. debugWebServer(`WebServer is already available`);
  78. if (this._options.reuseExistingServer)
  79. return;
  80. const port = new URL(this._options.url).port;
  81. throw new Error(`${this._options.url ?? `http://localhost${port ? ":" + port : ""}`} is already used, make sure that nothing is running on the port/url or set reuseExistingServer:true in config.webServer.`);
  82. }
  83. debugWebServer(`Starting WebServer process ${this._options.command}...`);
  84. const { launchedProcess, gracefullyClose } = await (0, import_utils.launchProcess)({
  85. command: this._options.command,
  86. env: {
  87. ...DEFAULT_ENVIRONMENT_VARIABLES,
  88. ...process.env,
  89. ...this._options.env
  90. },
  91. cwd: this._options.cwd,
  92. stdio: "stdin",
  93. shell: true,
  94. attemptToGracefullyClose: async () => {
  95. if (process.platform === "win32")
  96. throw new Error("Graceful shutdown is not supported on Windows");
  97. if (!this._options.gracefulShutdown)
  98. throw new Error("skip graceful shutdown");
  99. const { signal, timeout = 0 } = this._options.gracefulShutdown;
  100. process.kill(-launchedProcess.pid, signal);
  101. return new Promise((resolve, reject) => {
  102. const timer = timeout !== 0 ? setTimeout(() => reject(new Error(`process didn't close gracefully within timeout`)), timeout) : void 0;
  103. launchedProcess.once("close", (...args) => {
  104. clearTimeout(timer);
  105. resolve();
  106. });
  107. });
  108. },
  109. log: () => {
  110. },
  111. onExit: (code) => processExitedReject(new Error(code ? `Process from config.webServer was not able to start. Exit code: ${code}` : "Process from config.webServer exited early.")),
  112. tempDirectories: []
  113. });
  114. this._killProcess = gracefullyClose;
  115. debugWebServer(`Process started`);
  116. launchedProcess.stderr.on("data", (data) => {
  117. if (debugWebServer.enabled || (this._options.stderr === "pipe" || !this._options.stderr))
  118. this._reporter.onStdErr?.(prefixOutputLines(data.toString(), this._options.name));
  119. });
  120. launchedProcess.stdout.on("data", (data) => {
  121. if (debugWebServer.enabled || this._options.stdout === "pipe")
  122. this._reporter.onStdOut?.(prefixOutputLines(data.toString(), this._options.name));
  123. });
  124. }
  125. async _waitForProcess() {
  126. if (!this._isAvailableCallback) {
  127. this._processExitedPromise.catch(() => {
  128. });
  129. return;
  130. }
  131. debugWebServer(`Waiting for availability...`);
  132. const launchTimeout = this._options.timeout || 60 * 1e3;
  133. const cancellationToken = { canceled: false };
  134. const { timedOut } = await Promise.race([
  135. (0, import_utils.raceAgainstDeadline)(() => waitFor(this._isAvailableCallback, cancellationToken), (0, import_utils.monotonicTime)() + launchTimeout),
  136. this._processExitedPromise
  137. ]);
  138. cancellationToken.canceled = true;
  139. if (timedOut)
  140. throw new Error(`Timed out waiting ${launchTimeout}ms from config.webServer.`);
  141. debugWebServer(`WebServer available`);
  142. }
  143. }
  144. async function isPortUsed(port) {
  145. const innerIsPortUsed = (host) => new Promise((resolve) => {
  146. const conn = import_net.default.connect(port, host).on("error", () => {
  147. resolve(false);
  148. }).on("connect", () => {
  149. conn.end();
  150. resolve(true);
  151. });
  152. });
  153. return await innerIsPortUsed("127.0.0.1") || await innerIsPortUsed("::1");
  154. }
  155. async function waitFor(waitFn, cancellationToken) {
  156. const logScale = [100, 250, 500];
  157. while (!cancellationToken.canceled) {
  158. const connected = await waitFn();
  159. if (connected)
  160. return;
  161. const delay = logScale.shift() || 1e3;
  162. debugWebServer(`Waiting ${delay}ms`);
  163. await new Promise((x) => setTimeout(x, delay));
  164. }
  165. }
  166. function getIsAvailableFunction(url, checkPortOnly, ignoreHTTPSErrors, onStdErr) {
  167. const urlObject = new URL(url);
  168. if (!checkPortOnly)
  169. return () => (0, import_utils.isURLAvailable)(urlObject, ignoreHTTPSErrors, debugWebServer, onStdErr);
  170. const port = urlObject.port;
  171. return () => isPortUsed(+port);
  172. }
  173. const webServer = (options) => {
  174. return new WebServerPlugin(options, false);
  175. };
  176. const webServerPluginsForConfig = (config) => {
  177. const shouldSetBaseUrl = !!config.config.webServer;
  178. const webServerPlugins = [];
  179. for (const webServerConfig of config.webServers) {
  180. if (webServerConfig.port && webServerConfig.url)
  181. throw new Error(`Either 'port' or 'url' should be specified in config.webServer.`);
  182. let url;
  183. if (webServerConfig.port || webServerConfig.url) {
  184. url = webServerConfig.url || `http://localhost:${webServerConfig.port}`;
  185. if (shouldSetBaseUrl && !webServerConfig.url)
  186. process.env.PLAYWRIGHT_TEST_BASE_URL = url;
  187. }
  188. webServerPlugins.push(new WebServerPlugin({ ...webServerConfig, url }, webServerConfig.port !== void 0));
  189. }
  190. return webServerPlugins;
  191. };
  192. function prefixOutputLines(output, prefixName = "WebServer") {
  193. const lastIsNewLine = output[output.length - 1] === "\n";
  194. let lines = output.split("\n");
  195. if (lastIsNewLine)
  196. lines.pop();
  197. lines = lines.map((line) => import_utils2.colors.dim(`[${prefixName}] `) + line);
  198. if (lastIsNewLine)
  199. lines.push("");
  200. return lines.join("\n");
  201. }
  202. // Annotate the CommonJS export names for ESM import in node:
  203. 0 && (module.exports = {
  204. WebServerPlugin,
  205. webServer,
  206. webServerPluginsForConfig
  207. });