ScuttleBot

scuttlebot / tests / e2e / node_modules / playwright / lib / runner / processHost.js
Source Blame History 180 lines
f7eb47b… lmata 1 "use strict";
f7eb47b… lmata 2 var __create = Object.create;
f7eb47b… lmata 3 var __defProp = Object.defineProperty;
f7eb47b… lmata 4 var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
f7eb47b… lmata 5 var __getOwnPropNames = Object.getOwnPropertyNames;
f7eb47b… lmata 6 var __getProtoOf = Object.getPrototypeOf;
f7eb47b… lmata 7 var __hasOwnProp = Object.prototype.hasOwnProperty;
f7eb47b… lmata 8 var __export = (target, all) => {
f7eb47b… lmata 9 for (var name in all)
f7eb47b… lmata 10 __defProp(target, name, { get: all[name], enumerable: true });
f7eb47b… lmata 11 };
f7eb47b… lmata 12 var __copyProps = (to, from, except, desc) => {
f7eb47b… lmata 13 if (from && typeof from === "object" || typeof from === "function") {
f7eb47b… lmata 14 for (let key of __getOwnPropNames(from))
f7eb47b… lmata 15 if (!__hasOwnProp.call(to, key) && key !== except)
f7eb47b… lmata 16 __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
f7eb47b… lmata 17 }
f7eb47b… lmata 18 return to;
f7eb47b… lmata 19 };
f7eb47b… lmata 20 var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
f7eb47b… lmata 21 // If the importer is in node compatibility mode or this is not an ESM
f7eb47b… lmata 22 // file that has been converted to a CommonJS file using a Babel-
f7eb47b… lmata 23 // compatible transform (i.e. "__esModule" has not been set), then set
f7eb47b… lmata 24 // "default" to the CommonJS "module.exports" for node compatibility.
f7eb47b… lmata 25 isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
f7eb47b… lmata 26 mod
f7eb47b… lmata 27 ));
f7eb47b… lmata 28 var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
f7eb47b… lmata 29 var processHost_exports = {};
f7eb47b… lmata 30 __export(processHost_exports, {
f7eb47b… lmata 31 ProcessHost: () => ProcessHost
f7eb47b… lmata 32 });
f7eb47b… lmata 33 module.exports = __toCommonJS(processHost_exports);
f7eb47b… lmata 34 var import_child_process = __toESM(require("child_process"));
f7eb47b… lmata 35 var import_events = require("events");
f7eb47b… lmata 36 var import_utils = require("playwright-core/lib/utils");
f7eb47b… lmata 37 var import_utilsBundle = require("playwright-core/lib/utilsBundle");
f7eb47b… lmata 38 class ProcessHost extends import_events.EventEmitter {
f7eb47b… lmata 39 constructor(runnerScript, processName, env) {
f7eb47b… lmata 40 super();
f7eb47b… lmata 41 this._didSendStop = false;
f7eb47b… lmata 42 this._processDidExit = false;
f7eb47b… lmata 43 this._didExitAndRanOnExit = false;
f7eb47b… lmata 44 this._lastMessageId = 0;
f7eb47b… lmata 45 this._callbacks = /* @__PURE__ */ new Map();
f7eb47b… lmata 46 this._producedEnv = {};
f7eb47b… lmata 47 this._requestHandlers = /* @__PURE__ */ new Map();
f7eb47b… lmata 48 this._runnerScript = runnerScript;
f7eb47b… lmata 49 this._processName = processName;
f7eb47b… lmata 50 this._extraEnv = env;
f7eb47b… lmata 51 }
f7eb47b… lmata 52 async startRunner(runnerParams, options = {}) {
f7eb47b… lmata 53 (0, import_utils.assert)(!this.process, "Internal error: starting the same process twice");
f7eb47b… lmata 54 this.process = import_child_process.default.fork(require.resolve("../common/process"), {
f7eb47b… lmata 55 // Note: we pass detached:false, so that workers are in the same process group.
f7eb47b… lmata 56 // This way Ctrl+C or a kill command can shutdown all workers in case they misbehave.
f7eb47b… lmata 57 // Otherwise user can end up with a bunch of workers stuck in a busy loop without self-destructing.
f7eb47b… lmata 58 detached: false,
f7eb47b… lmata 59 env: {
f7eb47b… lmata 60 ...process.env,
f7eb47b… lmata 61 ...this._extraEnv
f7eb47b… lmata 62 },
f7eb47b… lmata 63 stdio: [
f7eb47b… lmata 64 "ignore",
f7eb47b… lmata 65 options.onStdOut ? "pipe" : "inherit",
f7eb47b… lmata 66 options.onStdErr && !process.env.PW_RUNNER_DEBUG ? "pipe" : "inherit",
f7eb47b… lmata 67 "ipc"
f7eb47b… lmata 68 ]
f7eb47b… lmata 69 });
f7eb47b… lmata 70 this.process.on("exit", async (code, signal) => {
f7eb47b… lmata 71 this._processDidExit = true;
f7eb47b… lmata 72 await this.onExit();
f7eb47b… lmata 73 this._didExitAndRanOnExit = true;
f7eb47b… lmata 74 this.emit("exit", { unexpectedly: !this._didSendStop, code, signal });
f7eb47b… lmata 75 });
f7eb47b… lmata 76 this.process.on("error", (e) => {
f7eb47b… lmata 77 });
f7eb47b… lmata 78 this.process.on("message", (message) => {
f7eb47b… lmata 79 if (import_utilsBundle.debug.enabled("pw:test:protocol"))
f7eb47b… lmata 80 (0, import_utilsBundle.debug)("pw:test:protocol")("\u25C0 RECV " + JSON.stringify(message));
f7eb47b… lmata 81 if (message.method === "__env_produced__") {
f7eb47b… lmata 82 const producedEnv = message.params;
f7eb47b… lmata 83 this._producedEnv = Object.fromEntries(producedEnv.map((e) => [e[0], e[1] ?? void 0]));
f7eb47b… lmata 84 } else if (message.method === "__dispatch__") {
f7eb47b… lmata 85 const { id, error: error2, method, params, result } = message.params;
f7eb47b… lmata 86 if (id && this._callbacks.has(id)) {
f7eb47b… lmata 87 const { resolve, reject } = this._callbacks.get(id);
f7eb47b… lmata 88 this._callbacks.delete(id);
f7eb47b… lmata 89 if (error2) {
f7eb47b… lmata 90 const errorObject = new Error(error2.message);
f7eb47b… lmata 91 errorObject.stack = error2.stack;
f7eb47b… lmata 92 reject(errorObject);
f7eb47b… lmata 93 } else {
f7eb47b… lmata 94 resolve(result);
f7eb47b… lmata 95 }
f7eb47b… lmata 96 } else {
f7eb47b… lmata 97 this.emit(method, params);
f7eb47b… lmata 98 }
f7eb47b… lmata 99 } else if (message.method === "__request__") {
f7eb47b… lmata 100 const { id, method, params } = message.params;
f7eb47b… lmata 101 const handler = this._requestHandlers.get(method);
f7eb47b… lmata 102 if (!handler) {
f7eb47b… lmata 103 this.send({ method: "__response__", params: { id, error: { message: "Unknown method" } } });
f7eb47b… lmata 104 } else {
f7eb47b… lmata 105 handler(params).then((result) => {
f7eb47b… lmata 106 this.send({ method: "__response__", params: { id, result } });
f7eb47b… lmata 107 }).catch((error2) => {
f7eb47b… lmata 108 this.send({ method: "__response__", params: { id, error: { message: error2.message } } });
f7eb47b… lmata 109 });
f7eb47b… lmata 110 }
f7eb47b… lmata 111 } else {
f7eb47b… lmata 112 this.emit(message.method, message.params);
f7eb47b… lmata 113 }
f7eb47b… lmata 114 });
f7eb47b… lmata 115 if (options.onStdOut)
f7eb47b… lmata 116 this.process.stdout?.on("data", options.onStdOut);
f7eb47b… lmata 117 if (options.onStdErr)
f7eb47b… lmata 118 this.process.stderr?.on("data", options.onStdErr);
f7eb47b… lmata 119 const error = await new Promise((resolve) => {
f7eb47b… lmata 120 this.process.once("exit", (code, signal) => resolve({ unexpectedly: true, code, signal }));
f7eb47b… lmata 121 this.once("ready", () => resolve(void 0));
f7eb47b… lmata 122 });
f7eb47b… lmata 123 if (error)
f7eb47b… lmata 124 return error;
f7eb47b… lmata 125 const processParams = {
f7eb47b… lmata 126 processName: this._processName,
f7eb47b… lmata 127 timeOrigin: (0, import_utils.timeOrigin)()
f7eb47b… lmata 128 };
f7eb47b… lmata 129 this.send({
f7eb47b… lmata 130 method: "__init__",
f7eb47b… lmata 131 params: {
f7eb47b… lmata 132 processParams,
f7eb47b… lmata 133 runnerScript: this._runnerScript,
f7eb47b… lmata 134 runnerParams
f7eb47b… lmata 135 }
f7eb47b… lmata 136 });
f7eb47b… lmata 137 }
f7eb47b… lmata 138 sendMessage(message) {
f7eb47b… lmata 139 const id = ++this._lastMessageId;
f7eb47b… lmata 140 this.send({
f7eb47b… lmata 141 method: "__dispatch__",
f7eb47b… lmata 142 params: { id, ...message }
f7eb47b… lmata 143 });
f7eb47b… lmata 144 return new Promise((resolve, reject) => {
f7eb47b… lmata 145 this._callbacks.set(id, { resolve, reject });
f7eb47b… lmata 146 });
f7eb47b… lmata 147 }
f7eb47b… lmata 148 sendMessageNoReply(message) {
f7eb47b… lmata 149 this.sendMessage(message).catch(() => {
f7eb47b… lmata 150 });
f7eb47b… lmata 151 }
f7eb47b… lmata 152 async onExit() {
f7eb47b… lmata 153 }
f7eb47b… lmata 154 onRequest(method, handler) {
f7eb47b… lmata 155 this._requestHandlers.set(method, handler);
f7eb47b… lmata 156 }
f7eb47b… lmata 157 async stop() {
f7eb47b… lmata 158 if (!this._processDidExit && !this._didSendStop) {
f7eb47b… lmata 159 this.send({ method: "__stop__" });
f7eb47b… lmata 160 this._didSendStop = true;
f7eb47b… lmata 161 }
f7eb47b… lmata 162 if (!this._didExitAndRanOnExit)
f7eb47b… lmata 163 await new Promise((f) => this.once("exit", f));
f7eb47b… lmata 164 }
f7eb47b… lmata 165 didSendStop() {
f7eb47b… lmata 166 return this._didSendStop;
f7eb47b… lmata 167 }
f7eb47b… lmata 168 producedEnv() {
f7eb47b… lmata 169 return this._producedEnv;
f7eb47b… lmata 170 }
f7eb47b… lmata 171 send(message) {
f7eb47b… lmata 172 if (import_utilsBundle.debug.enabled("pw:test:protocol"))
f7eb47b… lmata 173 (0, import_utilsBundle.debug)("pw:test:protocol")("SEND \u25BA " + JSON.stringify(message));
f7eb47b… lmata 174 this.process?.send(message);
f7eb47b… lmata 175 }
f7eb47b… lmata 176 }
f7eb47b… lmata 177 // Annotate the CommonJS export names for ESM import in node:
f7eb47b… lmata 178 0 && (module.exports = {
f7eb47b… lmata 179 ProcessHost
f7eb47b… lmata 180 });

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button