dsh-browser-cdp 1.2.2 → 1.2.3

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/dsh-plugin.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "$schema": "https://dsh.community/schemas/dsh-plugin-0.15.json",
3
3
  "id": "dsh-browser-cdp",
4
4
  "name": "dsh-browser-cdp",
5
- "version": "1.2.1",
5
+ "version": "1.2.3",
6
6
  "manifestVersion": "0.15",
7
7
  "facets": {
8
8
  "host": {
package/lib/client.js CHANGED
@@ -239,6 +239,18 @@ const FAMILY_CLAIM_DELAY_MS = FAMILY_CLAIM_BASE_MS + FAMILY_CLAIM_RANK * FAMILY_
239
239
  * 这样 owner 一旦上位就自然遮蔽本兜底行(宿主规则:同 cell 最低者渲染)。 */
240
240
  const FAMILY_HOST_PRIORITY = 50;
241
241
  const NO_TABS = [];
242
+ /**
243
+ * uSES 浅比较(issue #2 的 C 案加固):引用不同但行内容一致时视为未变。
244
+ * 主修法是按 getVersion 缓存快照(引用稳定),这里只兑底宿主绑定语义再变的情形。
245
+ */
246
+ function eqRows(a, b) {
247
+ if (a === b) return true;
248
+ if (a.length !== b.length) return false;
249
+ return a.every((row, i) => {
250
+ const other = b[i];
251
+ return other !== void 0 && row.id === other.id && row.order === other.order && row.label === other.label;
252
+ });
253
+ }
242
254
  /** 通用容器渲染:按 order 依次画出账本里每张贡献卡,不含任何"本插件特例"。 */
243
255
  function FamilyHostSection(props) {
244
256
  const raw = props.hooks?.tabs;
@@ -250,7 +262,7 @@ function FamilyHostSection(props) {
250
262
  return (0, react.createElement)("div", { style: {
251
263
  display: "grid",
252
264
  gap: "12px"
253
- } }, (props.useTabs !== void 0 ? props.useTabs((value) => value) : polled).map((row) => (0, react.createElement)("div", { key: row.id }, props.renderSlot ? props.renderSlot(FAMILY_CHILD_KEY, {}, {
265
+ } }, (props.useTabs !== void 0 ? props.useTabs((value) => value, eqRows) : polled).map((row) => (0, react.createElement)("div", { key: row.id }, props.renderSlot ? props.renderSlot(FAMILY_CHILD_KEY, {}, {
254
266
  only: row.id,
255
267
  fallback: null
256
268
  }) : props.renderContributor?.(row.id) ?? null)));
@@ -298,8 +310,17 @@ function claimFamilyHostIfOrphaned(slots, isHeld, label, log) {
298
310
  let claimed = false;
299
311
  const timer = setTimeout(() => {
300
312
  if (isHeld()) return;
313
+ let cachedVersion = -1;
314
+ let cachedRows = NO_TABS;
301
315
  const ledger = {
302
- getSnapshot: () => projectLedger(slots),
316
+ getSnapshot: () => {
317
+ const v = typeof slots.getVersion === "function" ? slots.getVersion(FAMILY_CHILD_KEY) : NaN;
318
+ if (v !== cachedVersion || Number.isNaN(v)) {
319
+ cachedRows = projectLedger(slots);
320
+ cachedVersion = v;
321
+ }
322
+ return cachedRows;
323
+ },
303
324
  subscribe: (listener) => slots.subscribe ? slots.subscribe(FAMILY_CHILD_KEY, listener) : () => void 0
304
325
  };
305
326
  /** ≤0.1.5 壳没有 renderSlot:从账本条目现挂组件,失败一律 null。 */
package/lib/index.js CHANGED
@@ -271,12 +271,25 @@ async function connectCdp(wsUrl, timeoutMs) {
271
271
  }
272
272
  };
273
273
  }
274
- async function browserWsUrl(port) {
275
- const r = await fetch(`http://127.0.0.1:${port}/json/version`, { signal: AbortSignal.timeout(3e3) });
276
- if (!r.ok) throw new Error(`/json/version HTTP ${r.status} on port ${port}`);
277
- const j$1 = await r.json();
278
- if (!j$1.webSocketDebuggerUrl) throw new Error("browser did not expose webSocketDebuggerUrl");
279
- return j$1.webSocketDebuggerUrl;
274
+ async function browserWsUrl(port, retries = 2) {
275
+ for (let attempt = 0;; attempt++) try {
276
+ const r = await fetch(`http://127.0.0.1:${port}/json/version`, { signal: AbortSignal.timeout(3e3) });
277
+ if (!r.ok) throw new Error(`/json/version HTTP ${r.status} on port ${port}`);
278
+ const j$1 = await r.json();
279
+ if (!j$1.webSocketDebuggerUrl) throw new Error("browser did not expose webSocketDebuggerUrl");
280
+ return j$1.webSocketDebuggerUrl;
281
+ } catch (err) {
282
+ const nm = err?.name;
283
+ const timedOut = nm === "TimeoutError" || nm === "AbortError";
284
+ const refused = err instanceof TypeError;
285
+ if ((timedOut || refused) && attempt < retries) {
286
+ await sleep(500);
287
+ continue;
288
+ }
289
+ if (timedOut) throw new Error(`/json/version on port ${port} timed out after ${retries + 1} attempts`);
290
+ if (refused) throw new Error(`DevTools HTTP endpoint unreachable on port ${port} after ${retries + 1} attempts (connection refused — the browser may have exited right after publishing the port)`);
291
+ throw err;
292
+ }
280
293
  }
281
294
  function sleep(ms) {
282
295
  return new Promise((resolve$1) => setTimeout(resolve$1, ms));
@@ -423,6 +436,7 @@ async function importLoginCookies(opts, deps) {
423
436
  }
424
437
  let closedSource = false;
425
438
  let srcHandle = null;
439
+ let exited = null;
426
440
  let tempCdp = null;
427
441
  let egoCdp = null;
428
442
  let linkDir = null;
@@ -483,7 +497,7 @@ async function importLoginCookies(opts, deps) {
483
497
  }
484
498
  }
485
499
  let port = null;
486
- for (let attempt = 0; attempt < 2 && port === null; attempt++) {
500
+ for (let attempt = 0; attempt < 2 && port === null && exited === null; attempt++) {
487
501
  if (attempt > 0) {
488
502
  await sleep(6e3);
489
503
  try {
@@ -518,8 +532,18 @@ async function importLoginCookies(opts, deps) {
518
532
  graceMs: 8e3
519
533
  });
520
534
  srcHandle.done.catch(() => null);
535
+ exited = null;
536
+ srcHandle.done.then((r) => {
537
+ exited = r;
538
+ }, () => {
539
+ exited = {
540
+ exitCode: null,
541
+ signal: null
542
+ };
543
+ });
521
544
  const deadline = Date.now() + timeoutMs;
522
- while (Date.now() < deadline) {
545
+ while (Date.now() < deadline && port === null) {
546
+ if (exited !== null) break;
523
547
  try {
524
548
  port = parseDevToolsActivePort(await readFile(portFile, "utf8"));
525
549
  if (port !== null) break;
@@ -527,6 +551,19 @@ async function importLoginCookies(opts, deps) {
527
551
  await sleep(250);
528
552
  }
529
553
  }
554
+ const earlyExit = exited;
555
+ if (port === null && earlyExit !== null) {
556
+ let tail = "";
557
+ try {
558
+ tail = srcHandle?.collected?.stderr?.readFrom(0).text ?? "";
559
+ } catch {}
560
+ tail = tail.slice(-500);
561
+ return {
562
+ ok: false,
563
+ closedSource,
564
+ error: `${browser.label} headless instance exited early (code=${earlyExit.exitCode ?? "null"}, signal=${earlyExit.signal ?? "none"})${tail ? `: ${tail}` : ""} — a lingering background instance may have swallowed the throwaway debug port`
565
+ };
566
+ }
530
567
  if (port === null) return {
531
568
  ok: false,
532
569
  closedSource,
@@ -8076,6 +8113,8 @@ function registerLoginImport(ctx, cfg, reg) {
8076
8113
  matched: { type: "integer" },
8077
8114
  written: { type: "integer" },
8078
8115
  domains: { type: "json" },
8116
+ closedSource: { type: "boolean" },
8117
+ restoredFromBackup: { type: "boolean" },
8079
8118
  error: { type: "string" }
8080
8119
  }
8081
8120
  },
@@ -8696,6 +8735,10 @@ function registerActionTools(ctx, cfg, reg) {
8696
8735
  type: "string",
8697
8736
  description: "'browser' (default) runs via the page context; 'server' uses Node-side fetch.server."
8698
8737
  },
8738
+ credentials: {
8739
+ type: "string",
8740
+ description: "Browser mode only: 'include' attaches the page profile's cookies even cross-origin (reuse login state on API endpoints); 'omit' / 'same-origin' also accepted. Omitted = engine default (cross-origin credentials not sent). Ignored in server mode."
8741
+ },
8699
8742
  space: spaceParam
8700
8743
  },
8701
8744
  buildScript: (args) => {
@@ -8706,6 +8749,8 @@ function registerActionTools(ctx, cfg, reg) {
8706
8749
  };
8707
8750
  if (str(args.body, "") !== "") opts.body = str(args.body, "");
8708
8751
  const mode = str(args.mode, "browser");
8752
+ const cred = str(args.credentials, "");
8753
+ if (mode !== "server" && (cred === "include" || cred === "omit" || cred === "same-origin")) opts.credentials = cred;
8709
8754
  return `${mode === "server" ? "" : `${useSpace(str(args.space, cfg.defaultSpace))}${ensureRealTab()}`}${SAFE_FN}const __r = await fetch.${mode === "server" ? "server" : "browser"}(${j(str(args.url, ""))}, ${j(opts)})\nconst __status = typeof __r.status !== "undefined" ? __r.status : 200\nlet __body = null\ntry { __body = typeof __r.text === "function" ? await __r.text() : (typeof __r === "string" ? __r : JSON.stringify(safe(__r))) } catch { __body = null }\nconsole.log('${SENTINEL}' + JSON.stringify({ ok: true, mode: ${j(mode)}, status: __status, body: __body, url: ${j(str(args.url, ""))} }))\n`;
8710
8755
  }
8711
8756
  }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-browser-cdp",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
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.1.7-rc.1 ~ 0.2.1-rc.1 line (exact rc enumeration): declarative settings via Config .volatile() fields; for DSH 0.1.2-rc.1 ~ 0.1.5-rc.3 use the 0.17.x line]",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",