@zaisma/cli 0.39.0 → 0.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/chrome.mjs +60 -17
- package/package.json +1 -1
package/chrome.mjs
CHANGED
|
@@ -49,22 +49,54 @@ function playwrightChromes() {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* sessionId), listen(sessionId, onEvent) and close().
|
|
52
|
+
* What Chrome starts with besides its profile. It never touches the machine's GPU: WebGL in software (SwiftShader, as on Zaisma's check and
|
|
53
|
+
* on machines without a GPU), or with isDrawingOnCpu --disable-gpu, for the layout check, which measures boxes, not pixels (no WebGL on a Mac, SwiftShader on Linux).
|
|
54
|
+
* test/node/chrome-no-gpu.test.ts holds this, and that nothing else starts a Chrome.
|
|
56
55
|
*/
|
|
57
|
-
export
|
|
56
|
+
export function chromeFlags({ isDrawingOnCpu = false } = {}) {
|
|
57
|
+
const drawing = isDrawingOnCpu ? ["--disable-gpu"] : ["--use-angle=swiftshader", "--enable-unsafe-swiftshader"];
|
|
58
|
+
return ["--headless=new", "--remote-debugging-port=0", "--no-first-run", "--no-default-browser-check", ...drawing, "--autoplay-policy=no-user-gesture-required", "--hide-scrollbars", "--disable-renderer-backgrounding", "--disable-background-timer-throttling", "--disable-backgrounding-occluded-windows"];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Headless Chrome at path, without the machine's GPU (chromeFlags), and a DevTools connection to it: send(method, parameters, sessionId),
|
|
63
|
+
* listen(sessionId, onEvent) and close(). A Chrome that doesn't answer is started again (startWithinTries), and then fails by name.
|
|
64
|
+
*/
|
|
65
|
+
export async function openChrome(path, profilePrefix, { isDrawingOnCpu = false, startLimitMs = CHROME_START_LIMIT_MS, ...others } = {}) {
|
|
66
|
+
const unknownOptions = Object.keys(others);
|
|
67
|
+
if (unknownOptions.length) throw new Error(`openChrome has no option ${unknownOptions.join(", ")}: its options are isDrawingOnCpu and startLimitMs, and every Chrome runs without the machine's GPU.`);
|
|
58
68
|
await sweepOrphans();
|
|
69
|
+
return startWithinTries(() => startChrome(path, profilePrefix, isDrawingOnCpu, startLimitMs));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** How long a Chrome may take to answer on its DevTools connection. It takes seconds; on GitHub's runners one in ten takes 40 or more, and one now and then never answers. */
|
|
73
|
+
export const CHROME_START_LIMIT_MS = 60_000;
|
|
74
|
+
const CHROME_START_TRY_COUNT = 3;
|
|
75
|
+
|
|
76
|
+
/** A Chrome that never answered, which says nothing about what it was started to look at. */
|
|
77
|
+
export class ChromeStartStalled extends Error {}
|
|
78
|
+
|
|
79
|
+
/** What startOnce answers, starting it again when it stalled, and saying so: the last try's stall, or any other failure, is thrown. */
|
|
80
|
+
export async function startWithinTries(startOnce, say = (line) => process.stderr.write(`${line}\n`)) {
|
|
81
|
+
for (let tryNumber = 1; ; tryNumber++) {
|
|
82
|
+
try {
|
|
83
|
+
return await startOnce();
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (!(error instanceof ChromeStartStalled) || tryNumber === CHROME_START_TRY_COUNT) throw error;
|
|
86
|
+
say(`${process.env.GITHUB_ACTIONS ? "::warning::" : ""}${error.message} It's started again.`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function startChrome(path, profilePrefix, isDrawingOnCpu, startLimitMs) {
|
|
59
92
|
// A folder for each Chrome: OWNER_FILE, which says whose it is, and Chrome's profile inside, removed before the folder so a profile
|
|
60
93
|
// that can't be removed yet keeps its owner for the next sweep.
|
|
61
94
|
const folder = mkdtempSync(join(tmpdir(), profilePrefix));
|
|
62
95
|
const profile = join(folder, "profile");
|
|
63
96
|
mkdirSync(profile);
|
|
64
|
-
const webgl = isDrawingOnCpu ? ["--disable-gpu"] : isGpu ? ["--enable-gpu", "--ignore-gpu-blocklist", ...(process.platform === "darwin" ? ["--use-angle=metal"] : [])] : ["--use-angle=swiftshader", "--enable-unsafe-swiftshader"];
|
|
65
97
|
let child;
|
|
66
98
|
try {
|
|
67
|
-
child = spawn(path, [
|
|
99
|
+
child = spawn(path, [...chromeFlags({ isDrawingOnCpu }), `--user-data-dir=${profile}`, "about:blank"], { stdio: ["ignore", "ignore", "pipe"] });
|
|
68
100
|
} catch (error) {
|
|
69
101
|
rmSync(folder, { recursive: true, force: true });
|
|
70
102
|
throw error;
|
|
@@ -80,8 +112,12 @@ export async function openChrome(path, profilePrefix, { isGpu = false, isDrawing
|
|
|
80
112
|
letGo(running);
|
|
81
113
|
throw error;
|
|
82
114
|
};
|
|
83
|
-
|
|
84
|
-
|
|
115
|
+
let output = "";
|
|
116
|
+
let limitTimer;
|
|
117
|
+
const stalled = new Promise((_, reject) => {
|
|
118
|
+
limitTimer = setTimeout(() => reject(new ChromeStartStalled(`Chrome didn't answer within ${startLimitMs / 1000} s. It said: ${output.slice(-500) || "nothing"}.`)), startLimitMs);
|
|
119
|
+
});
|
|
120
|
+
const address = await Promise.race([stalled, new Promise((resolve, reject) => {
|
|
85
121
|
child.stderr.on("data", (chunk) => {
|
|
86
122
|
output += chunk;
|
|
87
123
|
const match = output.match(/DevTools listening on (ws:\/\/\S+)/);
|
|
@@ -89,9 +125,9 @@ export async function openChrome(path, profilePrefix, { isGpu = false, isDrawing
|
|
|
89
125
|
});
|
|
90
126
|
child.on("error", (error) => reject(error));
|
|
91
127
|
child.on("exit", (code) => reject(new Error(`Chrome stopped (${code}): ${output.slice(-500)}`)));
|
|
92
|
-
}).catch(giveUp);
|
|
128
|
+
})]).catch(giveUp);
|
|
93
129
|
const socket = new WebSocket(address);
|
|
94
|
-
await new Promise((resolve, reject) => ((socket.onopen = resolve), (socket.onerror = reject))).catch(giveUp);
|
|
130
|
+
await Promise.race([stalled, new Promise((resolve, reject) => ((socket.onopen = resolve), (socket.onerror = reject)))]).catch(giveUp);
|
|
95
131
|
let nextMessageNumber = 1;
|
|
96
132
|
const waiting = new Map();
|
|
97
133
|
const listeners = new Set();
|
|
@@ -104,13 +140,20 @@ export async function openChrome(path, profilePrefix, { isGpu = false, isDrawing
|
|
|
104
140
|
else resolve(message.result);
|
|
105
141
|
} else if (message.method) for (const listener of listeners) listener(message);
|
|
106
142
|
};
|
|
143
|
+
const send = (method, parameters = {}, sessionId) =>
|
|
144
|
+
new Promise((resolve, reject) => {
|
|
145
|
+
const id = nextMessageNumber++;
|
|
146
|
+
waiting.set(id, { resolve, reject });
|
|
147
|
+
socket.send(JSON.stringify({ id, method, params: parameters, ...(sessionId ? { sessionId } : {}) }));
|
|
148
|
+
});
|
|
149
|
+
// Its first answer: only then is it up.
|
|
150
|
+
await Promise.race([stalled, send("Browser.getVersion")]).catch((error) => {
|
|
151
|
+
socket.close();
|
|
152
|
+
giveUp(error);
|
|
153
|
+
});
|
|
154
|
+
clearTimeout(limitTimer);
|
|
107
155
|
return {
|
|
108
|
-
send
|
|
109
|
-
new Promise((resolve, reject) => {
|
|
110
|
-
const id = nextMessageNumber++;
|
|
111
|
-
waiting.set(id, { resolve, reject });
|
|
112
|
-
socket.send(JSON.stringify({ id, method, params: parameters, ...(sessionId ? { sessionId } : {}) }));
|
|
113
|
-
}),
|
|
156
|
+
send,
|
|
114
157
|
listen: (sessionId, onEvent) => {
|
|
115
158
|
const listener = (message) => message.sessionId === sessionId && onEvent(message.method, message.params);
|
|
116
159
|
listeners.add(listener);
|