@zenera/browser 1.3.40 → 1.3.41

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/README.md CHANGED
@@ -29,7 +29,7 @@ tools will run on.
29
29
  `ProcessSandbox`. No port: an in-process server on a private Unix socket.
30
30
  - **`PageSession`** is one tab: in a remote browser when `ZEN_BROWSER_URL` names one (a Playwright
31
31
  `run-server` at `ws://…`, or a DevTools endpoint at `http://…`), else in a local headless
32
- Chromium. It routes the page's requests (its own files, three CDNs, nothing else), waits for
32
+ Chromium; a remote one that is full or down is tried twice more, then the local one is used. It routes the page's requests (its own files, three CDNs, nothing else), waits for
33
33
  the page to settle, collects what went wrong, and takes snapshots with refs and screenshots.
34
34
  - **`checkPage`** loads a page once and reports errors, failed function calls and sideways
35
35
  scrolling, with screenshots at the chat panel's width, a wide one and dark.
package/dist/session.d.ts CHANGED
@@ -45,7 +45,9 @@ export declare const PREVIEW: {
45
45
  export declare class BrowserMissing extends Error {
46
46
  }
47
47
  /** The browser ZEN_BROWSER_URL names, else a local headless Chromium. */
48
- export declare function openBrowser(): Promise<Browser>;
48
+ export declare function openBrowser(opts?: {
49
+ warn?: (message: string) => void;
50
+ }): Promise<Browser>;
49
51
  export declare class PageSession {
50
52
  #private;
51
53
  readonly browser: Browser;
package/dist/session.js CHANGED
@@ -55,12 +55,69 @@ const IN_PAGE = {
55
55
  })()`,
56
56
  };
57
57
  /** The browser ZEN_BROWSER_URL names, else a local headless Chromium. */
58
- export async function openBrowser() {
58
+ export async function openBrowser(opts = {}) {
59
59
  const { chromium } = await import('playwright-core');
60
60
  const remote = process.env.ZEN_BROWSER_URL;
61
- if (remote) {
62
- return /^wss?:/i.test(remote) ? chromium.connect(remote) : chromium.connectOverCDP(remote);
61
+ if (!remote)
62
+ return launchLocal(chromium);
63
+ let why;
64
+ try {
65
+ return await connectRemote(chromium, remote);
66
+ }
67
+ catch (err) {
68
+ why = remoteFailure(err);
69
+ }
70
+ // A pool that is full, down or of another Playwright version: this machine's own, if it has one.
71
+ try {
72
+ const local = await launchLocal(chromium);
73
+ (opts.warn ?? ((m) => process.stderr.write(`${m}\n`)))(`zen page: the remote browser (ZEN_BROWSER_URL) failed: ${why}; using a local one`);
74
+ return local;
63
75
  }
76
+ catch (err) {
77
+ if (err instanceof BrowserMissing) {
78
+ throw new Error(`the remote browser (ZEN_BROWSER_URL) failed: ${why}; and no browser is installed here`, { cause: err });
79
+ }
80
+ throw err;
81
+ }
82
+ }
83
+ const CONNECT_TIMEOUT_MS = 15_000;
84
+ /** waits before each try again: a full pool takes its pods out of the Service, so another answers */
85
+ const CONNECT_RETRIES_MS = [1000, 2000];
86
+ async function connectRemote(chromium, url) {
87
+ const connect = () => /^wss?:/i.test(url)
88
+ ? chromium.connect(url, { timeout: CONNECT_TIMEOUT_MS })
89
+ : chromium.connectOverCDP(url, { timeout: CONNECT_TIMEOUT_MS });
90
+ for (const wait of CONNECT_RETRIES_MS) {
91
+ try {
92
+ return await connect();
93
+ }
94
+ catch (err) {
95
+ // Refused for who or what this is (token, version): asking again changes nothing.
96
+ if (!busy(err))
97
+ throw err;
98
+ await new Promise((r) => setTimeout(r, wait));
99
+ }
100
+ }
101
+ return connect();
102
+ }
103
+ const statusOf = (err) => Number(/ (\d{3}) [A-Z][\w ]*/.exec(String(err?.message ?? err))?.[1] ?? 0);
104
+ /** full (429, 503), or the connection itself failed: worth another try */
105
+ function busy(err) {
106
+ const status = statusOf(err);
107
+ return status === 0 || status === 429 || status === 502 || status === 503;
108
+ }
109
+ /** One line on why: the status and the pool's own sentence (it never holds the token). */
110
+ function remoteFailure(err) {
111
+ const message = String(err?.message ?? err)
112
+ .replace(/^.*?WebSocket error: /s, '')
113
+ .replace(/\s*(Call log:|=+ logs =+)[\s\S]*$/, '');
114
+ const lines = message
115
+ .split('\n')
116
+ .map((l) => l.replace(/[║╔╗╚╝═]/g, '').trim())
117
+ .filter(Boolean);
118
+ return lines.slice(0, 3).join(' ').slice(0, 300) || 'no answer';
119
+ }
120
+ async function launchLocal(chromium) {
64
121
  try {
65
122
  return await chromium.launch({
66
123
  headless: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenera/browser",
3
- "version": "1.3.40",
3
+ "version": "1.3.41",
4
4
  "description": "An html page in a real browser, served as a zen chat serves it: checked for errors, driven, photographed.",
5
5
  "keywords": [
6
6
  "playwright",
@@ -41,8 +41,8 @@
41
41
  "prepack": "tsc -b"
42
42
  },
43
43
  "dependencies": {
44
- "@zenera/engine": "^1.3.40",
45
- "@zenera/sandbox": "^1.3.40",
44
+ "@zenera/engine": "^1.3.41",
45
+ "@zenera/sandbox": "^1.3.41",
46
46
  "playwright-core": "1.65.1"
47
47
  }
48
48
  }