|
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
|
async sendRequest(method, params) { |
|
34
|
return await sendRequestToParent(method, params); |
|
35
|
} |
|
36
|
async sendMessageNoReply(method, params) { |
|
37
|
void sendRequestToParent(method, params).catch(() => { |
|
38
|
}); |
|
39
|
} |
|
40
|
} |
|
41
|
let gracefullyCloseCalled = false; |
|
42
|
let forceExitInitiated = false; |
|
43
|
sendMessageToParent({ method: "ready" }); |
|
44
|
process.on("disconnect", () => gracefullyCloseAndExit(true)); |
|
45
|
process.on("SIGINT", () => { |
|
46
|
}); |
|
47
|
process.on("SIGTERM", () => { |
|
48
|
}); |
|
49
|
let processRunner; |
|
50
|
let processName; |
|
51
|
const startingEnv = { ...process.env }; |
|
52
|
process.on("message", async (message) => { |
|
53
|
if (message.method === "__init__") { |
|
54
|
const { processParams, runnerParams, runnerScript } = message.params; |
|
55
|
void (0, import_utils.startProfiling)(); |
|
56
|
(0, import_utils.setTimeOrigin)(processParams.timeOrigin); |
|
57
|
const { create } = require(runnerScript); |
|
58
|
processRunner = create(runnerParams); |
|
59
|
processName = processParams.processName; |
|
60
|
return; |
|
61
|
} |
|
62
|
if (message.method === "__stop__") { |
|
63
|
const keys = /* @__PURE__ */ new Set([...Object.keys(process.env), ...Object.keys(startingEnv)]); |
|
64
|
const producedEnv = [...keys].filter((key) => startingEnv[key] !== process.env[key]).map((key) => [key, process.env[key] ?? null]); |
|
65
|
sendMessageToParent({ method: "__env_produced__", params: producedEnv }); |
|
66
|
await gracefullyCloseAndExit(false); |
|
67
|
return; |
|
68
|
} |
|
69
|
if (message.method === "__dispatch__") { |
|
70
|
const { id, method, params } = message.params; |
|
71
|
try { |
|
72
|
const result = await processRunner[method](params); |
|
73
|
const response = { id, result }; |
|
74
|
sendMessageToParent({ method: "__dispatch__", params: response }); |
|
75
|
} catch (e) { |
|
76
|
const response = { id, error: (0, import_util.serializeError)(e) }; |
|
77
|
sendMessageToParent({ method: "__dispatch__", params: response }); |
|
78
|
} |
|
79
|
} |
|
80
|
if (message.method === "__response__") |
|
81
|
handleResponseFromParent(message.params); |
|
82
|
}); |
|
83
|
const kForceExitTimeout = +(process.env.PWTEST_FORCE_EXIT_TIMEOUT || 3e4); |
|
84
|
async function gracefullyCloseAndExit(forceExit) { |
|
85
|
if (forceExit && !forceExitInitiated) { |
|
86
|
forceExitInitiated = true; |
|
87
|
setTimeout(() => process.exit(0), kForceExitTimeout); |
|
88
|
} |
|
89
|
if (!gracefullyCloseCalled) { |
|
90
|
gracefullyCloseCalled = true; |
|
91
|
await processRunner?.gracefullyClose().catch(() => { |
|
92
|
}); |
|
93
|
if (processName) |
|
94
|
await (0, import_utils.stopProfiling)(processName).catch(() => { |
|
95
|
}); |
|
96
|
process.exit(0); |
|
97
|
} |
|
98
|
} |
|
99
|
function sendMessageToParent(message) { |
|
100
|
try { |
|
101
|
process.send(message); |
|
102
|
} catch (e) { |
|
103
|
try { |
|
104
|
JSON.stringify(message); |
|
105
|
} catch { |
|
106
|
throw e; |
|
107
|
} |
|
108
|
} |
|
109
|
} |
|
110
|
let lastId = 0; |
|
111
|
const requestCallbacks = /* @__PURE__ */ new Map(); |
|
112
|
async function sendRequestToParent(method, params) { |
|
113
|
const id = ++lastId; |
|
114
|
sendMessageToParent({ method: "__request__", params: { id, method, params } }); |
|
115
|
const promise = new import_utils.ManualPromise(); |
|
116
|
requestCallbacks.set(id, promise); |
|
117
|
return promise; |
|
118
|
} |
|
119
|
function handleResponseFromParent(response) { |
|
120
|
const promise = requestCallbacks.get(response.id); |
|
121
|
if (!promise) |
|
122
|
return; |
|
123
|
requestCallbacks.delete(response.id); |
|
124
|
if (response.error) |
|
125
|
promise.reject(new Error(response.error.message)); |
|
126
|
else |
|
127
|
promise.resolve(response.result); |
|
128
|
} |
|
129
|
// Annotate the CommonJS export names for ESM import in node: |
|
130
|
0 && (module.exports = { |
|
131
|
ProcessRunner |
|
132
|
}); |
|
133
|
|