process.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var process_exports = {};
  20. __export(process_exports, {
  21. ProcessRunner: () => ProcessRunner
  22. });
  23. module.exports = __toCommonJS(process_exports);
  24. var import_utils = require("playwright-core/lib/utils");
  25. var import_util = require("../util");
  26. class ProcessRunner {
  27. async gracefullyClose() {
  28. }
  29. dispatchEvent(method, params) {
  30. const response = { method, params };
  31. sendMessageToParent({ method: "__dispatch__", params: response });
  32. }
  33. }
  34. let gracefullyCloseCalled = false;
  35. let forceExitInitiated = false;
  36. sendMessageToParent({ method: "ready" });
  37. process.on("disconnect", () => gracefullyCloseAndExit(true));
  38. process.on("SIGINT", () => {
  39. });
  40. process.on("SIGTERM", () => {
  41. });
  42. let processRunner;
  43. let processName;
  44. const startingEnv = { ...process.env };
  45. process.on("message", async (message) => {
  46. if (message.method === "__init__") {
  47. const { processParams, runnerParams, runnerScript } = message.params;
  48. void (0, import_utils.startProfiling)();
  49. (0, import_utils.setTimeOrigin)(processParams.timeOrigin);
  50. const { create } = require(runnerScript);
  51. processRunner = create(runnerParams);
  52. processName = processParams.processName;
  53. return;
  54. }
  55. if (message.method === "__stop__") {
  56. const keys = /* @__PURE__ */ new Set([...Object.keys(process.env), ...Object.keys(startingEnv)]);
  57. const producedEnv = [...keys].filter((key) => startingEnv[key] !== process.env[key]).map((key) => [key, process.env[key] ?? null]);
  58. sendMessageToParent({ method: "__env_produced__", params: producedEnv });
  59. await gracefullyCloseAndExit(false);
  60. return;
  61. }
  62. if (message.method === "__dispatch__") {
  63. const { id, method, params } = message.params;
  64. try {
  65. const result = await processRunner[method](params);
  66. const response = { id, result };
  67. sendMessageToParent({ method: "__dispatch__", params: response });
  68. } catch (e) {
  69. const response = { id, error: (0, import_util.serializeError)(e) };
  70. sendMessageToParent({ method: "__dispatch__", params: response });
  71. }
  72. }
  73. });
  74. const kForceExitTimeout = +(process.env.PWTEST_FORCE_EXIT_TIMEOUT || 3e4);
  75. async function gracefullyCloseAndExit(forceExit) {
  76. if (forceExit && !forceExitInitiated) {
  77. forceExitInitiated = true;
  78. setTimeout(() => process.exit(0), kForceExitTimeout);
  79. }
  80. if (!gracefullyCloseCalled) {
  81. gracefullyCloseCalled = true;
  82. await processRunner?.gracefullyClose().catch(() => {
  83. });
  84. if (processName)
  85. await (0, import_utils.stopProfiling)(processName).catch(() => {
  86. });
  87. process.exit(0);
  88. }
  89. }
  90. function sendMessageToParent(message) {
  91. try {
  92. process.send(message);
  93. } catch (e) {
  94. try {
  95. JSON.stringify(message);
  96. } catch {
  97. throw e;
  98. }
  99. }
  100. }
  101. // Annotate the CommonJS export names for ESM import in node:
  102. 0 && (module.exports = {
  103. ProcessRunner
  104. });