@zaisma/cli 0.40.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.
Files changed (2) hide show
  1. package/chrome.mjs +47 -13
  2. package/package.json +1 -1
package/chrome.mjs CHANGED
@@ -60,12 +60,35 @@ export function chromeFlags({ isDrawingOnCpu = false } = {}) {
60
60
 
61
61
  /**
62
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().
63
+ * listen(sessionId, onEvent) and close(). A Chrome that doesn't answer is started again (startWithinTries), and then fails by name.
64
64
  */
65
- export async function openChrome(path, profilePrefix, { isDrawingOnCpu = false, ...others } = {}) {
65
+ export async function openChrome(path, profilePrefix, { isDrawingOnCpu = false, startLimitMs = CHROME_START_LIMIT_MS, ...others } = {}) {
66
66
  const unknownOptions = Object.keys(others);
67
- if (unknownOptions.length) throw new Error(`openChrome has no option ${unknownOptions.join(", ")}: its only option is isDrawingOnCpu, and every Chrome runs without the machine's GPU.`);
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.`);
68
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) {
69
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
70
93
  // that can't be removed yet keeps its owner for the next sweep.
71
94
  const folder = mkdtempSync(join(tmpdir(), profilePrefix));
@@ -89,8 +112,12 @@ export async function openChrome(path, profilePrefix, { isDrawingOnCpu = false,
89
112
  letGo(running);
90
113
  throw error;
91
114
  };
92
- const address = await new Promise((resolve, reject) => {
93
- let output = "";
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) => {
94
121
  child.stderr.on("data", (chunk) => {
95
122
  output += chunk;
96
123
  const match = output.match(/DevTools listening on (ws:\/\/\S+)/);
@@ -98,9 +125,9 @@ export async function openChrome(path, profilePrefix, { isDrawingOnCpu = false,
98
125
  });
99
126
  child.on("error", (error) => reject(error));
100
127
  child.on("exit", (code) => reject(new Error(`Chrome stopped (${code}): ${output.slice(-500)}`)));
101
- }).catch(giveUp);
128
+ })]).catch(giveUp);
102
129
  const socket = new WebSocket(address);
103
- 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);
104
131
  let nextMessageNumber = 1;
105
132
  const waiting = new Map();
106
133
  const listeners = new Set();
@@ -113,13 +140,20 @@ export async function openChrome(path, profilePrefix, { isDrawingOnCpu = false,
113
140
  else resolve(message.result);
114
141
  } else if (message.method) for (const listener of listeners) listener(message);
115
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);
116
155
  return {
117
- send: (method, parameters = {}, sessionId) =>
118
- new Promise((resolve, reject) => {
119
- const id = nextMessageNumber++;
120
- waiting.set(id, { resolve, reject });
121
- socket.send(JSON.stringify({ id, method, params: parameters, ...(sessionId ? { sessionId } : {}) }));
122
- }),
156
+ send,
123
157
  listen: (sessionId, onEvent) => {
124
158
  const listener = (message) => message.sessionId === sessionId && onEvent(message.method, message.params);
125
159
  listeners.add(listener);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zaisma/cli",
3
- "version": "0.40.0",
3
+ "version": "0.41.0",
4
4
  "description": "Upload and publish browser games on Zaisma from the command line.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://zaisma.com",