dsh-browser-cdp 0.18.18 → 0.18.21

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.
@@ -41,10 +41,14 @@ const defaultIo = () => ({
41
41
  process.kill(pid, "SIGKILL");
42
42
  } catch {}
43
43
  },
44
- spawn: (bin, args) => {
44
+ spawn: (bin, args, env) => {
45
45
  const child = spawn(bin, args, {
46
46
  detached: true,
47
- stdio: "ignore"
47
+ stdio: "ignore",
48
+ ...env === void 0 ? {} : { env: {
49
+ ...process.env,
50
+ ...env
51
+ } }
48
52
  });
49
53
  child.unref();
50
54
  return { pid: child.pid ?? 0 };
@@ -108,6 +112,81 @@ function buildLaunchArgs(options) {
108
112
  ...userArgs
109
113
  ];
110
114
  }
115
+ /** Flags the user must never smuggle into a managed `lightpanda serve`. */
116
+ const LP_BLOCKED_ARGS = [
117
+ "--host",
118
+ "--port",
119
+ "--address"
120
+ ];
121
+ /**
122
+ * Assemble `lightpanda serve --host 127.0.0.1 --port <port>` + user args.
123
+ * No profile/headless flags exist on the serve CLI — nothing to block there.
124
+ */
125
+ function buildServeArgs(options) {
126
+ const tokens = (options.lightpandaArgs ?? "").split(/\s+/).map((arg) => arg.trim()).filter((arg) => arg !== "");
127
+ const userArgs = [];
128
+ let skipNext = false;
129
+ for (const arg of tokens) {
130
+ if (skipNext) {
131
+ skipNext = false;
132
+ continue;
133
+ }
134
+ const blocked = LP_BLOCKED_ARGS.find((b) => arg === b || arg.startsWith(`${b}=`));
135
+ if (blocked !== void 0) {
136
+ if (arg === blocked) skipNext = true;
137
+ continue;
138
+ }
139
+ userArgs.push(arg);
140
+ }
141
+ return [
142
+ "serve",
143
+ "--host",
144
+ "127.0.0.1",
145
+ "--port",
146
+ String(options.port),
147
+ ...userArgs
148
+ ];
149
+ }
150
+ /**
151
+ * win32 sentinel returned by `discoverLightpandaBinary`: Lightpanda has no
152
+ * native Windows build, so the launch argv goes through `wsl.exe --` and the
153
+ * binary lives inside the WSL2 filesystem (host `exists()` cannot see it —
154
+ * the readiness gate is the real check).
155
+ */
156
+ const WSL_LAUNCH = "wsl";
157
+ /**
158
+ * Lightpanda discovery. unix: explicit → common install roots (the official
159
+ * installer targets `~/.local/bin`) → bare PATH name. win32: the WSL sentinel
160
+ * when `wsl.exe` exists (the serve runs inside the default distro).
161
+ */
162
+ function discoverLightpandaBinary(explicit, platform, exists) {
163
+ if (platform === "win32") return exists("C:/Windows/System32/wsl.exe") ? WSL_LAUNCH : "";
164
+ if (explicit !== "") return explicit;
165
+ const home = homedir();
166
+ for (const candidate of [
167
+ `/usr/local/bin/lightpanda`,
168
+ `/usr/bin/lightpanda`,
169
+ join(home, ".local", "bin", "lightpanda"),
170
+ join(home, ".dsh", "cache", "dsh-browser-cdp", "bin", "lightpanda")
171
+ ]) if (exists(candidate)) return candidate;
172
+ return "lightpanda";
173
+ }
174
+ /** Spawn argv for one engine on one platform. */
175
+ function buildEngineArgv(options) {
176
+ if (options.engine === "lightpanda" && options.platform === "win32" && options.bin === WSL_LAUNCH) return [
177
+ "wsl.exe",
178
+ "--",
179
+ "lightpanda",
180
+ ...options.serveArgs
181
+ ];
182
+ return [options.bin, ...options.serveArgs];
183
+ }
184
+ /** Env handed to a managed Lightpanda serve (telemetry opt-out; WSLENV carries it into WSL). */
185
+ function lightpandaSpawnEnv(platform) {
186
+ const env = { LIGHTPANDA_DISABLE_TELEMETRY: "true" };
187
+ if (platform === "win32") env.WSLENV = "LIGHTPANDA_DISABLE_TELEMETRY/u";
188
+ return env;
189
+ }
111
190
  function defaultLauncherStatePath() {
112
191
  return join(homedir(), ".dsh", "cache", "dsh-browser-cdp", "launcher.json");
113
192
  }
@@ -124,16 +203,22 @@ function readRecord(path, io) {
124
203
  port: parsed.port,
125
204
  userDataDir: typeof parsed.userDataDir === "string" ? parsed.userDataDir : "",
126
205
  endpoint: typeof parsed.endpoint === "string" ? parsed.endpoint : "",
127
- startedAt: typeof parsed.startedAt === "number" ? parsed.startedAt : 0
206
+ startedAt: typeof parsed.startedAt === "number" ? parsed.startedAt : 0,
207
+ engine: parsed.engine === "lightpanda" ? "lightpanda" : "chrome"
128
208
  };
129
209
  } catch {
130
210
  return null;
131
211
  }
132
212
  }
133
213
  /**
134
- * Launch (or reuse) the managed local browser. The readiness gate is the
135
- * three-step check from T2.12: process alive → port responds → `/json/version`
136
- * returns 200. `timeoutMs` bounds the readiness wait (default 20s).
214
+ * Launch (or reuse) the managed local browser for ONE engine. The readiness
215
+ * gate is the three-step check from T2.12: process alive → port responds →
216
+ * `/json/version` returns 200. `timeoutMs` bounds the readiness wait
217
+ * (default 20s).
218
+ *
219
+ * Engine exclusivity: the registry holds ONE managed instance — a record for
220
+ * the OTHER engine is stopped first, so Chrome and Lightpanda never fight
221
+ * over the singleton.
137
222
  */
138
223
  async function launchLocalBrowser(options, io) {
139
224
  const ioImpl = {
@@ -143,32 +228,67 @@ async function launchLocalBrowser(options, io) {
143
228
  const now = ioImpl.now;
144
229
  const sleep = ioImpl.sleep;
145
230
  const statePath = options.statePath ?? defaultLauncherStatePath();
231
+ const engine = options.engine === "lightpanda" ? "lightpanda" : "chrome";
232
+ const platform = options.platform ?? process.platform;
146
233
  const record = readRecord(statePath, ioImpl);
147
- if (record && ioImpl.isAlive(record.pid) && (await ioImpl.fetchVersion(`http://127.0.0.1:${record.port}/json/version`)).ok) return {
234
+ if (record && record.engine !== engine && ioImpl.isAlive(record.pid)) {
235
+ ioImpl.killTree(record.pid);
236
+ ioImpl.removeFile(statePath);
237
+ }
238
+ const live = readRecord(statePath, ioImpl);
239
+ if (live && live.engine === engine && ioImpl.isAlive(live.pid) && (await ioImpl.fetchVersion(`http://127.0.0.1:${live.port}/json/version`)).ok) return {
148
240
  ok: true,
149
241
  reused: true,
150
- endpoint: record.endpoint,
151
- pid: record.pid,
152
- port: record.port
153
- };
154
- const platform = options.platform ?? process.platform;
155
- const bin = discoverChromeBinary(options.chromePath ?? "", platform, ioImpl.exists);
156
- if (bin === "") return {
157
- ok: false,
158
- code: "chrome-not-found",
159
- message: "No Chrome/Chromium/Edge binary found. Set chromePath in the settings panel."
242
+ endpoint: live.endpoint,
243
+ pid: live.pid,
244
+ port: live.port
160
245
  };
246
+ let bin;
247
+ let spawnEnv;
248
+ if (engine === "lightpanda") {
249
+ bin = discoverLightpandaBinary(options.lightpandaPath ?? "", platform, ioImpl.exists);
250
+ if (bin === "") return {
251
+ ok: false,
252
+ code: "binary-not-found",
253
+ message: "wsl.exe not found — Lightpanda has no native Windows build. Install WSL2 (`wsl --install` as admin), then inside WSL: `curl -fsSL https://pkg.lightpanda.io/install.sh | bash`."
254
+ };
255
+ spawnEnv = lightpandaSpawnEnv(platform);
256
+ } else {
257
+ bin = discoverChromeBinary(options.chromePath ?? "", platform, ioImpl.exists);
258
+ if (bin === "") return {
259
+ ok: false,
260
+ code: "chrome-not-found",
261
+ message: "No Chrome/Chromium/Edge binary found. Set chromePath in the settings panel."
262
+ };
263
+ }
161
264
  const userDataDir = options.userDataDir && options.userDataDir !== "" ? options.userDataDir : defaultManagedProfileDir();
162
265
  const port = await ioImpl.makePort();
163
- const args = buildLaunchArgs({
164
- userDataDir,
165
- port,
166
- headless: options.localHeadless === true,
167
- chromeArgs: options.chromeArgs
168
- });
266
+ let spawnBin;
267
+ let args;
268
+ if (engine === "lightpanda") {
269
+ const full = buildEngineArgv({
270
+ engine,
271
+ platform,
272
+ bin,
273
+ serveArgs: buildServeArgs({
274
+ port,
275
+ lightpandaArgs: options.lightpandaArgs
276
+ })
277
+ });
278
+ spawnBin = full[0];
279
+ args = full.slice(1);
280
+ } else {
281
+ spawnBin = bin;
282
+ args = buildLaunchArgs({
283
+ userDataDir,
284
+ port,
285
+ headless: options.localHeadless === true,
286
+ chromeArgs: options.chromeArgs
287
+ });
288
+ }
169
289
  let pid = 0;
170
290
  try {
171
- pid = ioImpl.spawn(bin, args).pid;
291
+ pid = ioImpl.spawn(spawnBin, args, spawnEnv).pid;
172
292
  } catch (error) {
173
293
  return {
174
294
  ok: false,
@@ -182,12 +302,13 @@ async function launchLocalBrowser(options, io) {
182
302
  message: "spawn returned no pid"
183
303
  };
184
304
  const endpoint = `http://127.0.0.1:${port}`;
305
+ const engineName = engine === "lightpanda" ? "lightpanda" : "chrome";
185
306
  const deadline = now() + (options.timeoutMs ?? 2e4);
186
307
  for (;;) {
187
308
  if (!ioImpl.isAlive(pid)) return {
188
309
  ok: false,
189
310
  code: "launch-exited",
190
- message: `chrome exited before opening the debugging port (pid ${pid})`
311
+ message: `${engineName} exited before opening the debugging port (pid ${pid})${engine === "lightpanda" && platform === "win32" ? " — is lightpanda installed inside WSL? (`wsl` then `curl -fsSL https://pkg.lightpanda.io/install.sh | bash`)" : ""}`
191
312
  };
192
313
  let versionOk = false;
193
314
  try {
@@ -200,7 +321,7 @@ async function launchLocalBrowser(options, io) {
200
321
  return {
201
322
  ok: false,
202
323
  code: "not-ready",
203
- message: `chrome did not answer ${endpoint}/json/version within the readiness window`
324
+ message: `${engineName} did not answer ${endpoint}/json/version within the readiness window`
204
325
  };
205
326
  }
206
327
  await sleep(250);
@@ -211,7 +332,8 @@ async function launchLocalBrowser(options, io) {
211
332
  userDataDir,
212
333
  endpoint,
213
334
  startedAt: now(),
214
- headless: options.localHeadless === true
335
+ headless: options.localHeadless === true,
336
+ engine
215
337
  }, null, 2));
216
338
  return {
217
339
  ok: true,
@@ -224,6 +346,8 @@ async function launchLocalBrowser(options, io) {
224
346
  /**
225
347
  * Stop the managed instance: kill-tree, wait for the pid to disappear, drop
226
348
  * the registry entry. T2.14 — the reaper and plugin unmount both call this.
349
+ * Lightpanda on win32 rides `wsl.exe`, and taskkill /T does not reach the
350
+ * Linux-side process — add a targeted pkill of OUR serve argv as backstop.
227
351
  */
228
352
  async function stopLocalBrowser(options = {}, io) {
229
353
  const ioImpl = {
@@ -238,6 +362,14 @@ async function stopLocalBrowser(options = {}, io) {
238
362
  };
239
363
  if (ioImpl.isAlive(record.pid)) {
240
364
  ioImpl.killTree(record.pid);
365
+ if (record.engine === "lightpanda" && process.platform === "win32") try {
366
+ spawn("wsl.exe", [
367
+ "--",
368
+ "sh",
369
+ "-c",
370
+ `pkill -f 'lightpanda serve --host 127.0.0.1 --port ${record.port}' || true`
371
+ ], { stdio: "ignore" }).unref();
372
+ } catch {}
241
373
  const deadline = ioImpl.now() + (options.graceMs ?? 5e3);
242
374
  while (ioImpl.isAlive(record.pid) && ioImpl.now() < deadline) await ioImpl.sleep(100);
243
375
  if (ioImpl.isAlive(record.pid)) return {
@@ -266,4 +398,4 @@ function localBrowserInfo(statePath, io) {
266
398
  }
267
399
 
268
400
  //#endregion
269
- export { launchLocalBrowser as a, discoverChromeBinary as i, defaultLauncherStatePath as n, localBrowserInfo as o, defaultManagedProfileDir as r, stopLocalBrowser as s, buildLaunchArgs as t };
401
+ export { defaultLauncherStatePath as a, discoverLightpandaBinary as c, localBrowserInfo as d, stopLocalBrowser as f, buildServeArgs as i, launchLocalBrowser as l, buildEngineArgv as n, defaultManagedProfileDir as o, buildLaunchArgs as r, discoverChromeBinary as s, WSL_LAUNCH as t, lightpandaSpawnEnv as u };
@@ -0,0 +1,3 @@
1
+ import { a as defaultLauncherStatePath, c as discoverLightpandaBinary, d as localBrowserInfo, f as stopLocalBrowser, i as buildServeArgs, l as launchLocalBrowser, n as buildEngineArgv, o as defaultManagedProfileDir, r as buildLaunchArgs, s as discoverChromeBinary, t as WSL_LAUNCH, u as lightpandaSpawnEnv } from "./launcher-BadIUjAx.js";
2
+
3
+ export { WSL_LAUNCH, buildEngineArgv, buildLaunchArgs, buildServeArgs, defaultLauncherStatePath, defaultManagedProfileDir, discoverChromeBinary, discoverLightpandaBinary, launchLocalBrowser, lightpandaSpawnEnv, localBrowserInfo, stopLocalBrowser };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-browser-cdp",
3
- "version": "0.18.18",
3
+ "version": "0.18.21",
4
4
  "description": "dsh-browser-cdp: DSH browser plugin — attach to a running Chrome/Edge over CDP (no bundled browser needed); structured browser-automation tools (30+ bcdp_*) that drive the vendored ego runtime through ctx.subprocess, plus a realtime watch panel (live SSE screencast + direct mouse interaction + download capture + human-verification notice). Web shell: floating watch panel without dsh-better-sidebar, sidebar tab with it. Headless/TUI hosts: tools-only. [DSH 0.2.0-rc.1+ line: declarative settings via Config .volatile() fields; for DSH 0.1.2-rc.1 ~ 0.1.7 use the 0.17.x line]",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -1,3 +0,0 @@
1
- import { a as launchLocalBrowser, i as discoverChromeBinary, n as defaultLauncherStatePath, o as localBrowserInfo, r as defaultManagedProfileDir, s as stopLocalBrowser, t as buildLaunchArgs } from "./launcher-DBaxfIsF.js";
2
-
3
- export { buildLaunchArgs, defaultLauncherStatePath, defaultManagedProfileDir, discoverChromeBinary, launchLocalBrowser, localBrowserInfo, stopLocalBrowser };