|
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 processHost_exports = {}; |
|
30
|
__export(processHost_exports, { |
|
31
|
ProcessHost: () => ProcessHost |
|
32
|
}); |
|
33
|
module.exports = __toCommonJS(processHost_exports); |
|
34
|
var import_child_process = __toESM(require("child_process")); |
|
35
|
var import_events = require("events"); |
|
36
|
var import_utils = require("playwright-core/lib/utils"); |
|
37
|
var import_utilsBundle = require("playwright-core/lib/utilsBundle"); |
|
38
|
class ProcessHost extends import_events.EventEmitter { |
|
39
|
constructor(runnerScript, processName, env) { |
|
40
|
super(); |
|
41
|
this._didSendStop = false; |
|
42
|
this._processDidExit = false; |
|
43
|
this._didExitAndRanOnExit = false; |
|
44
|
this._lastMessageId = 0; |
|
45
|
this._callbacks = /* @__PURE__ */ new Map(); |
|
46
|
this._producedEnv = {}; |
|
47
|
this._requestHandlers = /* @__PURE__ */ new Map(); |
|
48
|
this._runnerScript = runnerScript; |
|
49
|
this._processName = processName; |
|
50
|
this._extraEnv = env; |
|
51
|
} |
|
52
|
async startRunner(runnerParams, options = {}) { |
|
53
|
(0, import_utils.assert)(!this.process, "Internal error: starting the same process twice"); |
|
54
|
this.process = import_child_process.default.fork(require.resolve("../common/process"), { |
|
55
|
// Note: we pass detached:false, so that workers are in the same process group. |
|
56
|
// This way Ctrl+C or a kill command can shutdown all workers in case they misbehave. |
|
57
|
// Otherwise user can end up with a bunch of workers stuck in a busy loop without self-destructing. |
|
58
|
detached: false, |
|
59
|
env: { |
|
60
|
...process.env, |
|
61
|
...this._extraEnv |
|
62
|
}, |
|
63
|
stdio: [ |
|
64
|
"ignore", |
|
65
|
options.onStdOut ? "pipe" : "inherit", |
|
66
|
options.onStdErr && !process.env.PW_RUNNER_DEBUG ? "pipe" : "inherit", |
|
67
|
"ipc" |
|
68
|
] |
|
69
|
}); |
|
70
|
this.process.on("exit", async (code, signal) => { |
|
71
|
this._processDidExit = true; |
|
72
|
await this.onExit(); |
|
73
|
this._didExitAndRanOnExit = true; |
|
74
|
this.emit("exit", { unexpectedly: !this._didSendStop, code, signal }); |
|
75
|
}); |
|
76
|
this.process.on("error", (e) => { |
|
77
|
}); |
|
78
|
this.process.on("message", (message) => { |
|
79
|
if (import_utilsBundle.debug.enabled("pw:test:protocol")) |
|
80
|
(0, import_utilsBundle.debug)("pw:test:protocol")("\u25C0 RECV " + JSON.stringify(message)); |
|
81
|
if (message.method === "__env_produced__") { |
|
82
|
const producedEnv = message.params; |
|
83
|
this._producedEnv = Object.fromEntries(producedEnv.map((e) => [e[0], e[1] ?? void 0])); |
|
84
|
} else if (message.method === "__dispatch__") { |
|
85
|
const { id, error: error2, method, params, result } = message.params; |
|
86
|
if (id && this._callbacks.has(id)) { |
|
87
|
const { resolve, reject } = this._callbacks.get(id); |
|
88
|
this._callbacks.delete(id); |
|
89
|
if (error2) { |
|
90
|
const errorObject = new Error(error2.message); |
|
91
|
errorObject.stack = error2.stack; |
|
92
|
reject(errorObject); |
|
93
|
} else { |
|
94
|
resolve(result); |
|
95
|
} |
|
96
|
} else { |
|
97
|
this.emit(method, params); |
|
98
|
} |
|
99
|
} else if (message.method === "__request__") { |
|
100
|
const { id, method, params } = message.params; |
|
101
|
const handler = this._requestHandlers.get(method); |
|
102
|
if (!handler) { |
|
103
|
this.send({ method: "__response__", params: { id, error: { message: "Unknown method" } } }); |
|
104
|
} else { |
|
105
|
handler(params).then((result) => { |
|
106
|
this.send({ method: "__response__", params: { id, result } }); |
|
107
|
}).catch((error2) => { |
|
108
|
this.send({ method: "__response__", params: { id, error: { message: error2.message } } }); |
|
109
|
}); |
|
110
|
} |
|
111
|
} else { |
|
112
|
this.emit(message.method, message.params); |
|
113
|
} |
|
114
|
}); |
|
115
|
if (options.onStdOut) |
|
116
|
this.process.stdout?.on("data", options.onStdOut); |
|
117
|
if (options.onStdErr) |
|
118
|
this.process.stderr?.on("data", options.onStdErr); |
|
119
|
const error = await new Promise((resolve) => { |
|
120
|
this.process.once("exit", (code, signal) => resolve({ unexpectedly: true, code, signal })); |
|
121
|
this.once("ready", () => resolve(void 0)); |
|
122
|
}); |
|
123
|
if (error) |
|
124
|
return error; |
|
125
|
const processParams = { |
|
126
|
processName: this._processName, |
|
127
|
timeOrigin: (0, import_utils.timeOrigin)() |
|
128
|
}; |
|
129
|
this.send({ |
|
130
|
method: "__init__", |
|
131
|
params: { |
|
132
|
processParams, |
|
133
|
runnerScript: this._runnerScript, |
|
134
|
runnerParams |
|
135
|
} |
|
136
|
}); |
|
137
|
} |
|
138
|
sendMessage(message) { |
|
139
|
const id = ++this._lastMessageId; |
|
140
|
this.send({ |
|
141
|
method: "__dispatch__", |
|
142
|
params: { id, ...message } |
|
143
|
}); |
|
144
|
return new Promise((resolve, reject) => { |
|
145
|
this._callbacks.set(id, { resolve, reject }); |
|
146
|
}); |
|
147
|
} |
|
148
|
sendMessageNoReply(message) { |
|
149
|
this.sendMessage(message).catch(() => { |
|
150
|
}); |
|
151
|
} |
|
152
|
async onExit() { |
|
153
|
} |
|
154
|
onRequest(method, handler) { |
|
155
|
this._requestHandlers.set(method, handler); |
|
156
|
} |
|
157
|
async stop() { |
|
158
|
if (!this._processDidExit && !this._didSendStop) { |
|
159
|
this.send({ method: "__stop__" }); |
|
160
|
this._didSendStop = true; |
|
161
|
} |
|
162
|
if (!this._didExitAndRanOnExit) |
|
163
|
await new Promise((f) => this.once("exit", f)); |
|
164
|
} |
|
165
|
didSendStop() { |
|
166
|
return this._didSendStop; |
|
167
|
} |
|
168
|
producedEnv() { |
|
169
|
return this._producedEnv; |
|
170
|
} |
|
171
|
send(message) { |
|
172
|
if (import_utilsBundle.debug.enabled("pw:test:protocol")) |
|
173
|
(0, import_utilsBundle.debug)("pw:test:protocol")("SEND \u25BA " + JSON.stringify(message)); |
|
174
|
this.process?.send(message); |
|
175
|
} |
|
176
|
} |
|
177
|
// Annotate the CommonJS export names for ESM import in node: |
|
178
|
0 && (module.exports = { |
|
179
|
ProcessHost |
|
180
|
}); |
|
181
|
|