dsh-web-fetch-playwright 0.2.13 → 0.2.15

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/lib/index.js CHANGED
@@ -27,6 +27,7 @@ const Config = z.object({
27
27
  playwrightPath: z.string().default("").volatile(),
28
28
  cdpEndpoint: z.string().default("").volatile(),
29
29
  shareBrowserContext: z.boolean().default(true).volatile(),
30
+ contextIsolationScope: z.union([z.const("session"), z.const("global")]).default("global").volatile(),
30
31
  denoise: z.boolean().default(true).volatile(),
31
32
  maxConcurrency: z.number().step(1).min(1).max(200).volatile(),
32
33
  challengeWaitMs: z.number().step(100).min(0).max(MAX_CHALLENGE_WAIT_MS).default(DEFAULT_CHALLENGE_WAIT_MS).volatile(),
@@ -46,6 +47,7 @@ function snapshotsOf(config) {
46
47
  playwrightPath: config.playwrightPath.get(),
47
48
  cdpEndpoint: config.cdpEndpoint.get(),
48
49
  shareBrowserContext: config.shareBrowserContext.get(),
50
+ contextIsolationScope: config.contextIsolationScope.get(),
49
51
  denoise: config.denoise.get(),
50
52
  maxConcurrency: config.maxConcurrency.get(),
51
53
  challengeWaitMs: config.challengeWaitMs.get(),
@@ -69,6 +71,7 @@ function resolveConfig(config) {
69
71
  playwrightPath: resolved.playwrightPath.get(),
70
72
  cdpEndpoint: resolved.cdpEndpoint.get(),
71
73
  shareBrowserContext: resolved.shareBrowserContext.get(),
74
+ contextIsolationScope: resolved.contextIsolationScope.get(),
72
75
  denoise: resolved.denoise.get(),
73
76
  maxConcurrency: resolved.maxConcurrency.get(),
74
77
  challengeWaitMs: resolved.challengeWaitMs.get(),
@@ -112,7 +115,7 @@ function effectiveChallengeRetries(config) {
112
115
  /**
113
116
  * The context mode a fetch actually runs with: profile (a tab in the remote
114
117
  * browser's real profile) requires the CDP backend — a local launch has no
115
- * meaningful shared profile — and the checkbox (which defaults on, so an
118
+ * meaningful shared profile — and the share option (which defaults on, so an
116
119
  * absent value reads as `true`, mirroring the schema default); everything
117
120
  * else collapses to isolated.
118
121
  *
@@ -424,154 +427,548 @@ const CHALLENGE_DOM_PROBE = `(() => {
424
427
  return false
425
428
  })()`;
426
429
  //#endregion
430
+ //#region src/session-window.ts
431
+ const ownership = /* @__PURE__ */ new WeakMap();
432
+ const unavailable = /* @__PURE__ */ new WeakSet();
433
+ const CLEANUP_GRACE_MS = 100;
434
+ function claim(browser, context, keys) {
435
+ let owners = ownership.get(browser);
436
+ if (!owners) {
437
+ owners = /* @__PURE__ */ new Map();
438
+ ownership.set(browser, owners);
439
+ }
440
+ for (const [key, reference] of owners) if (!reference.deref()) owners.delete(key);
441
+ for (const key of keys) {
442
+ const previous = owners.get(key)?.deref();
443
+ if (previous && previous !== context) throw new Error("session context or window identity is already owned by another session context");
444
+ }
445
+ const reference = new WeakRef(context);
446
+ const added = keys.filter((key) => !owners.has(key));
447
+ for (const key of added) owners.set(key, reference);
448
+ if (added.length) context.on?.("close", () => {
449
+ for (const key of added) if (owners.get(key) === reference) owners.delete(key);
450
+ });
451
+ }
452
+ /** Bound a promise without abandoning responsibility for resources arriving late. */
453
+ var SetupDeadline = class {
454
+ expired;
455
+ end;
456
+ stopped = false;
457
+ constructor(timeoutMs, expired) {
458
+ this.expired = expired;
459
+ this.end = Date.now() + Math.max(1, timeoutMs);
460
+ }
461
+ run(operation, late) {
462
+ if (this.stopped) return Promise.reject(/* @__PURE__ */ new Error("session window setup has finished"));
463
+ if (Date.now() >= this.end) {
464
+ this.expired();
465
+ return Promise.reject(/* @__PURE__ */ new Error("timed out creating the dedicated session window"));
466
+ }
467
+ return new Promise((resolve, reject) => {
468
+ let timedOut = false;
469
+ const timer = setTimeout(() => {
470
+ timedOut = true;
471
+ if (!this.stopped) this.expired();
472
+ reject(/* @__PURE__ */ new Error("timed out creating the dedicated session window"));
473
+ }, Math.max(1, this.end - Date.now()));
474
+ Promise.resolve().then(operation).then((value) => {
475
+ clearTimeout(timer);
476
+ if (timedOut) {
477
+ if (late) Promise.resolve().then(() => late(value)).catch(() => {});
478
+ return;
479
+ }
480
+ resolve(value);
481
+ }, (error) => {
482
+ clearTimeout(timer);
483
+ if (!timedOut) reject(error);
484
+ });
485
+ });
486
+ }
487
+ };
488
+ /** Cleanup must never hold a pool lock indefinitely, including detach itself. */
489
+ async function cleanup(operation) {
490
+ let timer;
491
+ await Promise.race([Promise.resolve().then(operation).catch(() => {}), new Promise((resolve) => {
492
+ timer = setTimeout(resolve, CLEANUP_GRACE_MS);
493
+ })]);
494
+ if (timer) clearTimeout(timer);
495
+ }
496
+ /**
497
+ * Caller serializes creation and release for this context. A bootstrap supplies
498
+ * its public CDP identity; the real first tab explicitly requests a NEW window.
499
+ * Subsequent tabs must match that window. Never falls back to the profile.
500
+ * On timeout only owned pages/targets are cleaned; existing leases stay alive.
501
+ */
502
+ async function createSessionPage(context, browser, timeoutMs) {
503
+ const ctx = context;
504
+ const owner = browser;
505
+ if (typeof owner.newBrowserCDPSession !== "function" || typeof ctx.newCDPSession !== "function" || typeof ctx.pages !== "function" || typeof ctx.on !== "function" || typeof ctx.removeListener !== "function") throw new Error("session window isolation requires Playwright Chromium CDP session and page event APIs");
506
+ if (browser.contexts?.()[0] === context) throw new Error("session window isolation must not use the default browser profile context");
507
+ if (unavailable.has(context)) throw new Error("session window context is unavailable after a setup timeout");
508
+ const deadline = new SetupDeadline(timeoutMs, () => unavailable.add(context));
509
+ const sessions = /* @__PURE__ */ new Set();
510
+ let root;
511
+ let bootstrap;
512
+ let bootstrapTargetId;
513
+ let created;
514
+ let targetId;
515
+ let listener;
516
+ let succeeded = false;
517
+ const detach = (session) => cleanup(() => session.detach());
518
+ const closePage = (page) => cleanup(() => page.close());
519
+ const newSession = async (operation) => {
520
+ const session = await deadline.run(operation, detach);
521
+ if (deadline.stopped) {
522
+ await detach(session);
523
+ throw new Error("session window setup has finished");
524
+ }
525
+ sessions.add(session);
526
+ return session;
527
+ };
528
+ const identify = async (page) => {
529
+ const session = await newSession(() => ctx.newCDPSession(page));
530
+ let identificationFailed = false;
531
+ try {
532
+ const { targetInfo } = await deadline.run(() => session.send("Target.getTargetInfo"));
533
+ if (!targetInfo?.targetId || !targetInfo.browserContextId) throw new Error("session window isolation requires a target and browser context identity");
534
+ return targetInfo;
535
+ } catch (error) {
536
+ identificationFailed = true;
537
+ throw error;
538
+ } finally {
539
+ try {
540
+ await deadline.run(() => session.detach());
541
+ sessions.delete(session);
542
+ } catch (error) {
543
+ if (!identificationFailed) throw error;
544
+ }
545
+ }
546
+ };
547
+ const windowOf = async (id) => {
548
+ const { windowId } = await deadline.run(() => root.send("Browser.getWindowForTarget", { targetId: id }));
549
+ if (!Number.isInteger(windowId)) throw new Error("session window isolation requires a browser window identity");
550
+ return windowId;
551
+ };
552
+ const closeLateTarget = async (value) => {
553
+ if (!value?.targetId) return;
554
+ const lateDeadline = new SetupDeadline(CLEANUP_GRACE_MS, () => {});
555
+ let session;
556
+ try {
557
+ session = await lateDeadline.run(() => owner.newBrowserCDPSession(), detach);
558
+ await lateDeadline.run(() => session.send("Target.closeTarget", { targetId: value.targetId }));
559
+ } finally {
560
+ if (session) await detach(session);
561
+ }
562
+ };
563
+ try {
564
+ root = await newSession(() => owner.newBrowserCDPSession());
565
+ const existing = ctx.pages();
566
+ if (existing.length) {
567
+ const identity = await identify(existing[0]);
568
+ const expectedWindow = await windowOf(identity.targetId);
569
+ for (const page of existing.slice(1)) {
570
+ const other = await identify(page);
571
+ if (other.browserContextId !== identity.browserContextId || await windowOf(other.targetId) !== expectedWindow) throw new Error("session context contains tabs in multiple windows; refusing unsafe reuse");
572
+ }
573
+ created = await deadline.run(() => ctx.newPage(), closePage);
574
+ const actual = await identify(created);
575
+ targetId = actual.targetId;
576
+ if (actual.browserContextId !== identity.browserContextId || await windowOf(actual.targetId) !== expectedWindow) throw new Error("Chromium did not place the session tab in its dedicated window");
577
+ claim(browser, context, [`context:${actual.browserContextId}`, `window:${expectedWindow}`]);
578
+ succeeded = true;
579
+ return created;
580
+ }
581
+ bootstrap = await deadline.run(() => ctx.newPage(), closePage);
582
+ const identity = await identify(bootstrap);
583
+ bootstrapTargetId = identity.targetId;
584
+ claim(browser, context, [`context:${identity.browserContextId}`]);
585
+ const bootstrapWindow = await windowOf(identity.targetId);
586
+ const candidates = /* @__PURE__ */ new Map();
587
+ let resolvePage;
588
+ let rejectPage;
589
+ const pending = new Promise((resolve, reject) => {
590
+ resolvePage = resolve;
591
+ rejectPage = reject;
592
+ });
593
+ pending.catch(() => {});
594
+ const match = () => {
595
+ const candidate = targetId ? candidates.get(targetId) : void 0;
596
+ if (!candidate) return;
597
+ if (candidate.contextId !== identity.browserContextId) rejectPage(/* @__PURE__ */ new Error("created target belongs to the wrong browser context"));
598
+ else resolvePage(candidate.page);
599
+ };
600
+ listener = (page) => {
601
+ identify(page).then((info) => {
602
+ candidates.set(info.targetId, {
603
+ page,
604
+ contextId: info.browserContextId
605
+ });
606
+ match();
607
+ }).catch(() => {});
608
+ };
609
+ ctx.on("page", listener);
610
+ const creator = await newSession(() => ctx.newCDPSession(bootstrap));
611
+ const result = await deadline.run(() => creator.send("Target.createTarget", {
612
+ url: "about:blank",
613
+ browserContextId: identity.browserContextId,
614
+ newWindow: true
615
+ }), closeLateTarget);
616
+ if (typeof result.targetId !== "string" || !result.targetId) throw new Error("CDP did not return a session window target id");
617
+ const explicitTargetId = result.targetId;
618
+ targetId = explicitTargetId;
619
+ match();
620
+ for (const page of ctx.pages()) listener(page);
621
+ created = await deadline.run(() => pending, closePage);
622
+ const explicitWindow = await windowOf(explicitTargetId);
623
+ if (explicitWindow === bootstrapWindow) throw new Error("Chromium ignored the dedicated session window request");
624
+ claim(browser, context, [`context:${identity.browserContextId}`, `window:${explicitWindow}`]);
625
+ await deadline.run(() => bootstrap.close());
626
+ bootstrap = void 0;
627
+ succeeded = true;
628
+ return created;
629
+ } finally {
630
+ deadline.stopped = true;
631
+ if (listener) ctx.removeListener("page", listener);
632
+ await Promise.all([
633
+ ...!succeeded && created ? [closePage(created)] : [],
634
+ ...!succeeded && targetId && root ? [cleanup(() => root.send("Target.closeTarget", { targetId }))] : [],
635
+ ...bootstrap ? [closePage(bootstrap)] : [],
636
+ ...bootstrap && bootstrapTargetId && root ? [cleanup(() => root.send("Target.closeTarget", { targetId: bootstrapTargetId }))] : []
637
+ ]);
638
+ await Promise.all([...sessions].map(detach));
639
+ }
640
+ }
641
+ //#endregion
427
642
  //#region src/cdp-pool.ts
428
- /** A reusable `connectOverCDP` session handing out per-fetch pages. */
643
+ const CLOSE_GRACE_MS$1 = 2e3;
644
+ /** Thrown when a policy/connection change retired the generation mid-acquisition.
645
+ * A dedicated class lets acquire() recognise it without matching message text.
646
+ */
647
+ var AcquisitionAbandonedError = class extends Error {
648
+ constructor() {
649
+ super("cdp acquisition abandoned after policy or connection change");
650
+ }
651
+ };
652
+ /** Chromium does not notify other CDP clients when a browser context is
653
+ * disposed externally. Only this explicit createTarget error proves that the
654
+ * context used by newPage no longer exists; generic "Target closed" errors do
655
+ * not justify discarding shared state. Kept private to the newPage path.
656
+ */
657
+ function isMissingBrowserContext(error) {
658
+ return error instanceof Error && /Protocol error \(Target\.createTarget\): Failed to find browser context with id [A-Za-z0-9_-]+(?:\r?\n|$)/.test(error.message);
659
+ }
429
660
  var CdpConnectionPool = class {
430
661
  connect;
431
- browser;
432
- endpoint = "";
433
- connecting;
434
- connectingEndpoint = "";
435
- /** Bumped by dispose/replace so a settling connect knows it was abandoned. */
436
- generation = 0;
437
- /**
438
- * @param connect - opens a connection to an endpoint (the provider's real
439
- * one resolves the bundled playwright-core; tests inject fakes).
440
- */
441
- constructor(connect) {
662
+ sessionPageFactory;
663
+ current;
664
+ sessionConnections = /* @__PURE__ */ new Map();
665
+ policy;
666
+ epoch = 0;
667
+ generations = /* @__PURE__ */ new Set();
668
+ closes = /* @__PURE__ */ new WeakMap();
669
+ constructor(connect, sessionPageFactory = createSessionPage) {
442
670
  this.connect = connect;
671
+ this.sessionPageFactory = sessionPageFactory;
443
672
  }
444
- /**
445
- * Lease a fetch's page on the shared connection, connecting (or
446
- * reconnecting) first if needed. Concurrent first fetches share one
447
- * connect attempt.
448
- *
449
- * @param endpoint - normalized CDP endpoint URL.
450
- * @param timeoutMs - connect timeout budget.
451
- * @param mode - `isolated` (default): a fresh throwaway context plus a page
452
- * in it; `profile`: a page in the remote browser's default context — the
453
- * real profile — which is never closed, only the page is.
454
- * @returns the shared browser, the page's context, and the page the caller
455
- * must release.
456
- * @throws whatever the connect function throws when no connection can be
457
- * established (the provider wraps it in a structured WebError), or a
458
- * diagnostic error when `profile` mode finds no default context.
673
+ /** Serialize create/reuse/new-tab and tab-close/empty-check/context-close as
674
+ * whole transactions. Different session keys never wait for each other.
675
+ * A failed operation cannot poison the queue, and the map only drops the
676
+ * exact tail it owns (a queued acquisition may already have replaced it).
459
677
  */
460
- async acquire(endpoint, timeoutMs, mode = "isolated") {
461
- const browser = await this.ensure(endpoint, timeoutMs);
678
+ async withLock(generation, key, operation) {
679
+ const previous = generation.locks.get(key) ?? Promise.resolve();
680
+ let unlock;
681
+ const tail = new Promise((resolve) => {
682
+ unlock = resolve;
683
+ });
684
+ generation.locks.set(key, tail);
685
+ await previous;
462
686
  try {
463
- return await this.openLease(browser, mode);
464
- } catch (error) {
465
- if (this.isLive(browser)) throw error;
466
- this.drop(browser);
467
- const fresh = await this.ensure(endpoint, timeoutMs);
468
- return await this.openLease(fresh, mode);
687
+ return await operation();
688
+ } finally {
689
+ unlock();
690
+ if (generation.locks.get(key) === tail) generation.locks.delete(key);
469
691
  }
470
692
  }
471
- /**
472
- * Open one lease's page on a live connection. The profile-mode handle is
473
- * taken fresh every call (never cached across connections): a reconnect
474
- * yields a new Browser object whose `contexts()[0]` must be re-read.
693
+ /** Call synchronously for every fetch, including local-backend fetches.
694
+ * Keys must encode backend, endpoint and effective context policy. A change
695
+ * prevents new leases on the old generation but preserves existing pages.
475
696
  */
476
- async openLease(browser, mode) {
477
- if (mode === "profile") {
478
- const context = browser.contexts?.()[0];
479
- if (context === void 0) throw new Error("the CDP endpoint exposed no default browser context (profile mode requires a real browser profile)");
480
- return {
481
- browser,
482
- context,
483
- page: await context.newPage(),
484
- persistent: true
485
- };
486
- }
487
- const context = await browser.newContext();
488
- try {
489
- return {
490
- browser,
491
- context,
492
- page: await context.newPage(),
493
- persistent: false
494
- };
495
- } catch (error) {
496
- await context.close().catch(() => {});
497
- throw error;
697
+ setPolicy(key) {
698
+ if (this.policy === key) return;
699
+ this.policy = key;
700
+ this.epoch++;
701
+ for (const generation of this.generations) this.retire(generation);
702
+ }
703
+ async acquire(endpoint, timeoutMs, mode = "isolated") {
704
+ for (let attempt = 0;; attempt++) {
705
+ const generation = this.ensure(endpoint, mode);
706
+ const epoch = this.epoch;
707
+ generation.active++;
708
+ try {
709
+ const browser = await this.attach(generation, timeoutMs);
710
+ this.assertCurrent(generation);
711
+ const open = async () => {
712
+ this.assertCurrent(generation);
713
+ return await this.openLease(generation, browser, mode, timeoutMs);
714
+ };
715
+ return typeof mode === "object" ? await this.withLock(generation, JSON.stringify([mode.scope, mode.name]), open) : await open();
716
+ } catch (error) {
717
+ generation.active--;
718
+ const idleSessionRetired = generation.sessionKey !== void 0 && generation.retired && epoch === this.epoch && error instanceof AcquisitionAbandonedError;
719
+ if (generation.sessionKey !== void 0 && generation.active === 0) this.retire(generation);
720
+ this.drain(generation);
721
+ if (attempt === 0 && idleSessionRetired) continue;
722
+ if (attempt > 0 || epoch !== this.epoch || !generation.browser || this.isLive(generation.browser)) throw error;
723
+ this.retire(generation);
724
+ }
498
725
  }
499
726
  }
500
- /**
501
- * Close a fetch-owned page, plus its context when the lease owns one. The
502
- * remote default context (persistent leases) and the shared connection
503
- * stay for the next fetch.
504
- * @param lease - the lease whose page (and, when isolated, context) goes away.
505
- */
506
- async release(lease) {
507
- await lease.page.close().catch(() => {});
508
- if (!lease.persistent) await lease.context.close().catch(() => {});
727
+ release(lease) {
728
+ return lease.release();
509
729
  }
510
- /**
511
- * Drop the shared connection (plugin teardown or endpoint change). Fetches
512
- * holding leases keep their contexts until they release them. A connect
513
- * still in flight is abandoned — its own continuation closes the stray
514
- * browser (nothing here waits on it, so teardown cannot deadlock).
730
+ /** Teardown is stronger than policy retirement: close owned contexts and
731
+ * disconnect even with active leases. Pending creations clean up on arrival.
732
+ * The remote default context is NEVER explicitly closed.
515
733
  */
516
734
  async dispose() {
517
- const browser = this.browser;
518
- this.browser = void 0;
519
- this.endpoint = "";
520
- this.connecting = void 0;
521
- this.connectingEndpoint = "";
522
- this.generation++;
523
- await browser?.close().catch(() => {});
735
+ const generations = [...this.generations];
736
+ this.epoch++;
737
+ this.current = void 0;
738
+ this.sessionConnections.clear();
739
+ for (const generation of generations) {
740
+ generation.retired = true;
741
+ generation.disposed = true;
742
+ }
743
+ await Promise.all(generations.map((generation) => this.drain(generation)));
524
744
  }
525
- /** The shared connection for `endpoint`, connecting or reconnecting as needed. */
526
- async ensure(endpoint, timeoutMs) {
527
- if (this.browser !== void 0 && this.endpoint === endpoint && this.isLive(this.browser)) return this.browser;
528
- if (this.connecting !== void 0 && this.connectingEndpoint === endpoint) return this.connecting;
529
- return await this.connectFresh(endpoint, timeoutMs);
745
+ ensure(endpoint, mode) {
746
+ if (typeof mode === "object" && mode.scope === "session") {
747
+ const key = JSON.stringify([endpoint, mode.name]);
748
+ let generation = this.sessionConnections.get(key);
749
+ if (generation?.browser && !this.isLive(generation.browser)) {
750
+ this.retire(generation);
751
+ generation = void 0;
752
+ }
753
+ if (!generation) {
754
+ generation = {
755
+ endpoint,
756
+ sessionKey: key,
757
+ retired: false,
758
+ disposed: false,
759
+ active: 0,
760
+ named: /* @__PURE__ */ new Map(),
761
+ owned: /* @__PURE__ */ new Set(),
762
+ locks: /* @__PURE__ */ new Map()
763
+ };
764
+ this.sessionConnections.set(key, generation);
765
+ this.generations.add(generation);
766
+ }
767
+ return generation;
768
+ }
769
+ if (this.current && (this.current.endpoint !== endpoint || this.current.browser && !this.isLive(this.current.browser))) {
770
+ if (this.current.endpoint !== endpoint) this.epoch++;
771
+ this.retire(this.current);
772
+ }
773
+ if (!this.current) {
774
+ this.current = {
775
+ endpoint,
776
+ retired: false,
777
+ disposed: false,
778
+ active: 0,
779
+ named: /* @__PURE__ */ new Map(),
780
+ owned: /* @__PURE__ */ new Set(),
781
+ locks: /* @__PURE__ */ new Map()
782
+ };
783
+ this.generations.add(this.current);
784
+ }
785
+ return this.current;
530
786
  }
531
- /** Start a connection to `endpoint`, superseding whatever was there. */
532
- connectFresh(endpoint, timeoutMs) {
533
- const stale = this.browser;
534
- this.browser = void 0;
535
- this.endpoint = "";
536
- const generation = ++this.generation;
537
- const attempt = (async () => {
538
- const browser = await this.connect(endpoint, timeoutMs);
539
- if (generation !== this.generation) {
540
- await browser.close().catch(() => {});
787
+ async attach(generation, timeoutMs) {
788
+ if (generation.browser) return generation.browser;
789
+ if (!generation.connecting) generation.connecting = (async () => {
790
+ const browser = await this.connect(generation.endpoint, timeoutMs);
791
+ generation.browser = browser;
792
+ if (generation.retired) {
793
+ await this.close(browser);
541
794
  throw new Error("cdp connection abandoned before it attached");
542
795
  }
543
- this.browser = browser;
544
- this.endpoint = endpoint;
545
- this.watch(browser);
796
+ try {
797
+ browser.on?.("disconnected", () => {
798
+ this.retire(generation);
799
+ });
800
+ } catch {}
546
801
  return browser;
547
802
  })();
548
- this.connecting = attempt;
549
- this.connectingEndpoint = endpoint;
550
- attempt.then(() => {
551
- if (this.connecting === attempt) this.connecting = void 0;
552
- }, () => {
553
- if (this.connecting === attempt) this.connecting = void 0;
554
- });
555
- stale?.close().catch(() => {});
556
- return attempt;
803
+ try {
804
+ return await generation.connecting;
805
+ } catch (error) {
806
+ generation.connecting = void 0;
807
+ throw error;
808
+ }
809
+ }
810
+ assertCurrent(generation) {
811
+ if (generation.retired) throw new AcquisitionAbandonedError();
557
812
  }
558
- /** Clear the reference when this exact connection reports it went away. */
559
- watch(browser) {
813
+ async createContext(generation, browser) {
814
+ const context = await browser.newContext();
815
+ if (browser.contexts?.()[0] === context || [...this.generations].some((item) => item.owned.has(context))) throw new Error("CDP backend did not create a distinct isolated browser context");
816
+ if (generation.retired) {
817
+ await this.close(context);
818
+ throw new Error("cdp context abandoned after policy or connection change");
819
+ }
820
+ const entry = {
821
+ context,
822
+ closed: false,
823
+ active: 0
824
+ };
825
+ generation.owned.add(context);
560
826
  try {
561
- browser.on?.("disconnected", () => {
562
- if (this.browser === browser) {
563
- this.browser = void 0;
564
- this.endpoint = "";
565
- }
827
+ context.on?.("close", () => {
828
+ entry.closed = true;
829
+ generation.owned.delete(context);
566
830
  });
567
831
  } catch {}
832
+ return entry;
833
+ }
834
+ async namedContext(generation, browser, key) {
835
+ let pending = generation.named.get(key);
836
+ if (!pending) {
837
+ pending = this.createContext(generation, browser);
838
+ generation.named.set(key, pending);
839
+ }
840
+ try {
841
+ const entry = await pending;
842
+ if (!entry.closed) {
843
+ entry.active++;
844
+ return entry;
845
+ }
846
+ if (generation.named.get(key) === pending) generation.named.delete(key);
847
+ this.assertCurrent(generation);
848
+ return await this.namedContext(generation, browser, key);
849
+ } catch (error) {
850
+ if (generation.named.get(key) === pending) generation.named.delete(key);
851
+ throw error;
852
+ }
853
+ }
854
+ /** Invalidate the exact entry, not its key: a concurrent request may already
855
+ * have installed a replacement. namedContext removes only the Promise it read.
856
+ * Best-effort close attempts to release Playwright's local handle after an
857
+ * external CDP deletion (some versions retain it until disconnect); it never
858
+ * touches a sibling context or the default profile.
859
+ */
860
+ invalidateContext(generation, entry) {
861
+ entry.closed = true;
862
+ generation.owned.delete(entry.context);
863
+ this.close(entry.context);
864
+ }
865
+ async releaseNamedContext(generation, entry, mode) {
866
+ entry.active--;
867
+ if (typeof mode !== "object" || mode.scope !== "session" || entry.active > 0 || entry.closed) return;
868
+ if (generation.active > 1) return;
869
+ if ((entry.context.pages?.().length ?? 0) > 0) return;
870
+ entry.closed = true;
871
+ generation.named.delete(JSON.stringify([mode.scope, mode.name]));
872
+ await this.close(entry.context);
873
+ generation.owned.delete(entry.context);
874
+ if (generation.sessionKey !== void 0) this.retire(generation);
875
+ }
876
+ async openLease(generation, browser, mode, timeoutMs) {
877
+ const namedKey = typeof mode === "object" ? JSON.stringify([mode.scope, mode.name]) : void 0;
878
+ let entry;
879
+ if (mode === "profile") {
880
+ const context = browser.contexts?.()[0];
881
+ if (!context) throw new Error("the CDP endpoint exposed no default browser context (profile mode requires a real browser profile)");
882
+ entry = {
883
+ context,
884
+ closed: false,
885
+ active: 0
886
+ };
887
+ } else entry = namedKey === void 0 ? await this.createContext(generation, browser) : await this.namedContext(generation, browser, namedKey);
888
+ let page;
889
+ try {
890
+ for (let attempt = 0;; attempt++) {
891
+ this.assertCurrent(generation);
892
+ try {
893
+ page = typeof mode === "object" && mode.scope === "session" ? await this.sessionPageFactory(entry.context, browser, timeoutMs) : await entry.context.newPage();
894
+ break;
895
+ } catch (error) {
896
+ if (!namedKey || !entry.closed && !isMissingBrowserContext(error)) throw error;
897
+ this.invalidateContext(generation, entry);
898
+ if (attempt >= 1 || !this.isLive(browser)) throw error;
899
+ this.assertCurrent(generation);
900
+ const replacement = await this.namedContext(generation, browser, namedKey);
901
+ entry.active--;
902
+ entry = replacement;
903
+ }
904
+ }
905
+ if (generation.retired) {
906
+ await this.close(page);
907
+ this.assertCurrent(generation);
908
+ }
909
+ } catch (error) {
910
+ if (mode === "isolated") {
911
+ await this.close(entry.context);
912
+ generation.owned.delete(entry.context);
913
+ }
914
+ if (namedKey !== void 0) await this.releaseNamedContext(generation, entry, mode);
915
+ throw error;
916
+ }
917
+ const context = entry.context;
918
+ let released;
919
+ const release = () => {
920
+ const cleanup = async () => {
921
+ await this.close(page);
922
+ if (mode === "isolated") {
923
+ await this.close(context);
924
+ generation.owned.delete(context);
925
+ }
926
+ if (namedKey !== void 0) await this.releaseNamedContext(generation, entry, mode);
927
+ generation.active--;
928
+ await this.drain(generation);
929
+ };
930
+ released ??= namedKey === void 0 || typeof mode === "object" && mode.scope === "global" ? cleanup() : this.withLock(generation, namedKey, cleanup);
931
+ return released;
932
+ };
933
+ return {
934
+ browser,
935
+ context,
936
+ page,
937
+ persistent: mode !== "isolated",
938
+ release
939
+ };
940
+ }
941
+ retire(generation) {
942
+ generation.retired = true;
943
+ if (this.current === generation) this.current = void 0;
944
+ if (generation.sessionKey !== void 0 && this.sessionConnections.get(generation.sessionKey) === generation) this.sessionConnections.delete(generation.sessionKey);
945
+ this.drain(generation);
568
946
  }
569
- /** Forget a connection known to be dead (absent probe = assume dead here). */
570
- drop(browser) {
571
- if (this.browser === browser) {
572
- this.browser = void 0;
573
- this.endpoint = "";
947
+ async drain(generation) {
948
+ if (!generation.retired || !generation.disposed && generation.active > 0) return;
949
+ generation.cleanup ??= (async () => {
950
+ await Promise.all([...generation.owned].map((context) => this.close(context)));
951
+ generation.owned.clear();
952
+ generation.named.clear();
953
+ if (generation.browser) await this.close(generation.browser);
954
+ this.generations.delete(generation);
955
+ })();
956
+ await generation.cleanup;
957
+ }
958
+ /** Bound cleanup and deduplicate close calls, including release/dispose races. */
959
+ close(value) {
960
+ let pending = this.closes.get(value);
961
+ if (!pending) {
962
+ pending = new Promise((resolve) => {
963
+ const timer = setTimeout(resolve, CLOSE_GRACE_MS$1);
964
+ Promise.resolve().then(() => value.close()).catch(() => {}).then(() => {
965
+ clearTimeout(timer);
966
+ resolve();
967
+ });
968
+ });
969
+ this.closes.set(value, pending);
574
970
  }
971
+ return pending;
575
972
  }
576
973
  isLive(browser) {
577
974
  return browser.isConnected?.() !== false;
@@ -1071,10 +1468,12 @@ function existsAsFile(path) {
1071
1468
  * ({@link recoverDownloadResponse}) — instead of a `WEB_PROVIDER_ERROR`.
1072
1469
  *
1073
1470
  * Lifecycle: the local backend launches a browser per fetch and closes it —
1074
- * nothing outlives the call. The CDP backend keeps ONE shared connection to
1075
- * the remote browser for the provider's lifetime; each fetch only opens a
1076
- * page (tab) inside it and closes that on completion — in a throwaway
1077
- * isolated context, or (the default, `shareBrowserContext`) in the remote
1471
+ * nothing outlives the call. CDP session isolation keeps one connection per
1472
+ * DSH session until its context becomes idle; this also isolates gateways that
1473
+ * map a connection's first context to a separate browser process. Global/profile
1474
+ * modes share a provider-lifetime connection. Each fetch owns only its page;
1475
+ * contexts are per-session until idle, global until teardown, or (the default,
1476
+ * `shareBrowserContext`) in the remote
1078
1477
  * browser's default context so its profile, cookies, and persistent logins
1079
1478
  * apply; that default context is never closed. The concurrency cap counts
1080
1479
  * tabs, not browsers. Either way, an aborted signal closes the fetch's page.
@@ -1229,12 +1628,8 @@ var Semaphore = class {
1229
1628
  });
1230
1629
  }
1231
1630
  release() {
1232
- const next = this.active <= this.limit ? this.queue.shift() : void 0;
1233
- if (next === void 0) {
1234
- this.active = Math.max(0, this.active - 1);
1235
- return;
1236
- }
1237
- next.start();
1631
+ this.active = Math.max(0, this.active - 1);
1632
+ this.drain();
1238
1633
  }
1239
1634
  drain() {
1240
1635
  while (this.active < this.limit) {
@@ -1281,6 +1676,7 @@ function translateError(error, deadline) {
1281
1676
  */
1282
1677
  var PlaywrightFetchProvider = class {
1283
1678
  configSource;
1679
+ sessionIdSource;
1284
1680
  id = PLAYWRIGHT_FETCH_PROVIDER_ID;
1285
1681
  semaphore = new Semaphore(4);
1286
1682
  /** Shared CDP connection; injectable so the suite can substitute a fake. */
@@ -1289,8 +1685,9 @@ var PlaywrightFetchProvider = class {
1289
1685
  * @param configSource - thunk returning the currently authoritative config.
1290
1686
  * @param cdpPool - optional pool over the CDP backend (tests inject fakes).
1291
1687
  */
1292
- constructor(configSource, cdpPool) {
1688
+ constructor(configSource, cdpPool, sessionIdSource = () => void 0) {
1293
1689
  this.configSource = configSource;
1690
+ this.sessionIdSource = sessionIdSource;
1294
1691
  this.cdpPool = cdpPool ?? new CdpConnectionPool(defaultCdpConnect);
1295
1692
  }
1296
1693
  /** Cheap and side-effect free; backend problems surface per fetch instead. */
@@ -1308,6 +1705,16 @@ var PlaywrightFetchProvider = class {
1308
1705
  async fetch(request, signal) {
1309
1706
  if (signal?.aborted) throw new WebError("web fetch aborted", "WEB_ABORTED");
1310
1707
  const config = this.configSource();
1708
+ const sessionScoped = config.backend === "cdp" && !config.shareBrowserContext && config.contextIsolationScope === "session";
1709
+ let sessionId;
1710
+ if (sessionScoped) {
1711
+ try {
1712
+ sessionId = this.sessionIdSource();
1713
+ } catch {
1714
+ sessionId = void 0;
1715
+ }
1716
+ if (!sessionId?.trim()) throw new WebError("per-session context isolation requires a DSH session identity", "WEB_FETCH_SESSION_REQUIRED");
1717
+ }
1311
1718
  const url = validateFetchUrl(request.url);
1312
1719
  const deadline = new Deadline(signal, DEFAULT_TIMEOUT_MS);
1313
1720
  let session;
@@ -1316,7 +1723,8 @@ var PlaywrightFetchProvider = class {
1316
1723
  this.semaphore.resize(effectiveMaxConcurrency(config));
1317
1724
  await this.semaphore.acquire(deadline.signal, QUEUE_TIMEOUT_MS);
1318
1725
  acquired = true;
1319
- session = await this.openSession(config, deadline);
1726
+ session = await this.openSession(config, deadline, sessionId);
1727
+ if (deadline.signal.aborted) throw new Error("web fetch aborted during session setup");
1320
1728
  const held = session;
1321
1729
  const onAbort = () => {
1322
1730
  closeSession(held);
@@ -1341,13 +1749,24 @@ var PlaywrightFetchProvider = class {
1341
1749
  * @param deadline - the fetch budget, applied to launch/connect timeouts.
1342
1750
  * @returns the browser session the fetch will use.
1343
1751
  */
1344
- async openSession(config, deadline) {
1752
+ async openSession(config, deadline, sessionId) {
1345
1753
  const timeout = Math.min(deadline.remainingMs(), 2e4);
1346
1754
  if (config.backend === "cdp") {
1347
1755
  const endpoint = normalizeCdpEndpoint(config.cdpEndpoint);
1756
+ const profile = effectiveContextMode(config) === "profile";
1757
+ if (!profile && config.contextIsolationScope === "session" && !sessionId?.trim()) throw new WebError("per-session context isolation requires a DSH session identity", "WEB_FETCH_SESSION_REQUIRED");
1758
+ const mode = profile ? "profile" : {
1759
+ scope: config.contextIsolationScope,
1760
+ name: config.contextIsolationScope === "session" ? sessionId : "global"
1761
+ };
1348
1762
  const { source } = await resolveCdpBackend();
1763
+ this.cdpPool.setPolicy(JSON.stringify([
1764
+ endpoint,
1765
+ config.shareBrowserContext,
1766
+ config.shareBrowserContext ? "" : config.contextIsolationScope
1767
+ ]));
1349
1768
  try {
1350
- const lease = await this.cdpPool.acquire(endpoint, timeout, effectiveContextMode(config));
1769
+ const lease = await this.cdpPool.acquire(endpoint, timeout, mode);
1351
1770
  await installResourceFilter(lease.page);
1352
1771
  guardPopups(lease.page);
1353
1772
  guardDownloads(lease.page);
@@ -1356,12 +1775,14 @@ var PlaywrightFetchProvider = class {
1356
1775
  context: lease.context,
1357
1776
  page: lease.page,
1358
1777
  sharedBrowser: true,
1359
- persistent: lease.persistent
1778
+ persistent: lease.persistent,
1779
+ release: () => lease.release()
1360
1780
  };
1361
1781
  } catch (error) {
1362
- throw new WebError(`cannot connect to the CDP endpoint ${endpoint} (${source}); is the browser started with --remote-debugging-port? ${String(error instanceof Error ? error.message : error)}`, "WEB_PROVIDER_ERROR", { cause: error });
1782
+ throw new WebError(`cannot open a fetch session via the CDP endpoint ${endpoint} (${source}): ${String(error instanceof Error ? error.message : error)}`, "WEB_PROVIDER_ERROR", { cause: error });
1363
1783
  }
1364
1784
  }
1785
+ this.cdpPool.setPolicy("local");
1365
1786
  const { chromium, executablePath, source } = await resolvePlaywrightBackend(config.playwrightPath);
1366
1787
  let browser;
1367
1788
  try {
@@ -1435,6 +1856,7 @@ var PlaywrightFetchProvider = class {
1435
1856
  content: finalResponse !== null ? await finalResponse.text() : await page.content()
1436
1857
  });
1437
1858
  await page.waitForLoadState("networkidle", { timeout: Math.min(SETTLE_MS, deadline.remainingMs()) }).catch(() => {});
1859
+ await stripScriptingElements(page);
1438
1860
  const html = await page.content();
1439
1861
  if (!config.denoise) return capResult(finalUrl, statusCode, {
1440
1862
  kind: "html",
@@ -1747,6 +2169,74 @@ async function probeStillOnChallenge(page) {
1747
2169
  return true;
1748
2170
  }
1749
2171
  }
2172
+ /**
2173
+ * In-page script that removes non-content scripting subtrees from the live
2174
+ * document before `page.content()` reads it as an HTML string.
2175
+ *
2176
+ * WHAT goes, and why each removal is output-neutral versus the old pipeline:
2177
+ *
2178
+ * - `script` — three consumers ever saw script text before, and none loses
2179
+ * it here: Readability's `parse()` deletes script/noscript nodes BEFORE
2180
+ * extraction and scoring (so script text never reached the article or
2181
+ * influenced the scorer); DOMPurify's default allowlist drops script; the
2182
+ * tool layer's renderer removes SCRIPT wholesale on the raw path. What the
2183
+ * removal buys is the reason this exists: a page's runtime JS may rewrite
2184
+ * a data script's text with a literal `<\/script>` embedded — measured on
2185
+ * techcommunity.microsoft.com (Khoros/Aurora), whose client writes its
2186
+ * Apollo cache back into `script#__NEXT_DATA__` via `JSON.stringify`,
2187
+ * turning the wire's 712 `\u003c` escapes back into raw `<` — and
2188
+ * browsers serialize script text verbatim, so the flattened string ends
2189
+ * that script at the FIRST embedded end tag and re-parses the remaining
2190
+ * ~270KB of JSON as markup. The phantom elements that survive (unknown
2191
+ * tags like `<id>`, anchors full of JSON text) are exactly what
2192
+ * Readability's scorer reads as article mass, so the extracted "article"
2193
+ * is CSS and i18n strings while the real body — still present in the DOM —
2194
+ * loses. No string-level patch can undo this after serialization (the
2195
+ * string is genuinely ambiguous); only the browser's own parse holds the
2196
+ * document's true structure.
2197
+ * - `script[type="application/ld+json"]` — KEPT, but defused: Readability
2198
+ * extracts metadata (title/byline/excerpt — e.g. the site-suffix-free
2199
+ * "Remote Desktop Protocol" instead of "… - Wikipedia") from JSON-LD
2200
+ * scripts before removing them, so deleting them would regress the title
2201
+ * on every JSON-LD-carrying page. Rewriting `</` as `<\/` keeps the text
2202
+ * valid JSON (`\/` is a legal escape, so the re-parsed text parses to the
2203
+ * same object) while making the serialization harmless: the tokenizer
2204
+ * only terminates on a literal `<\/script`.
2205
+ * - `noscript` — KEPT: Readability's `_unwrapNoscriptImages` salvages the
2206
+ * real image out of lazy-load `<noscript><img …>` wrappers before
2207
+ * `_removeScripts` deletes the noscript; pre-stripping it would silently
2208
+ * drop those images from the markdown.
2209
+ * - `template` — removed: its children parse into the inert `.content`
2210
+ * fragment (invisible to Readability's traversal), DOMPurify's
2211
+ * FORBID_TAGS drops it, and the tool layer's renderer removes it too.
2212
+ * - `style` stays — its text cannot terminate a re-parse except at its own
2213
+ * end tag, and the pipeline already drops it.
2214
+ */
2215
+ const STRIP_SCRIPTING_ELEMENTS = `(() => {
2216
+ for (const el of Array.from(document.querySelectorAll('script, template'))) {
2217
+ if (el.tagName === 'SCRIPT' && (el.getAttribute('type') || '').trim().toLowerCase() === 'application/ld+json') {
2218
+ // '<\\/' is still valid JSON, so the re-parsed textContent parses to
2219
+ // the same object — while the serialized form can no longer close the
2220
+ // script element early.
2221
+ el.textContent = el.textContent.replace(/<\\//g, '<\\\\/')
2222
+ continue
2223
+ }
2224
+ el.remove()
2225
+ }
2226
+ })()`;
2227
+ /**
2228
+ * Remove non-content scripting subtrees from the live document before
2229
+ * serialization. Best-effort, mirroring {@link probeStillOnChallenge}: a
2230
+ * backend without `evaluate` (fakes, minimal engines) simply keeps the
2231
+ * document as-is — the fetch never fails here, and the pipeline degrades to
2232
+ * whatever the serialized string itself says.
2233
+ */
2234
+ async function stripScriptingElements(page) {
2235
+ if (typeof page.evaluate !== "function") return;
2236
+ try {
2237
+ await page.evaluate(STRIP_SCRIPTING_ELEMENTS);
2238
+ } catch {}
2239
+ }
1750
2240
  /** Apply the decoded-body cap and flag the cut. */
1751
2241
  function capResult(url, statusCode, body) {
1752
2242
  const truncated = body.content.length > MAX_BODY_CHARS;
@@ -1770,6 +2260,10 @@ function capResult(url, statusCode, body) {
1770
2260
  */
1771
2261
  async function closeSession(session) {
1772
2262
  if (session === void 0) return;
2263
+ if (session.release !== void 0) {
2264
+ await closeWithGrace({ close: session.release });
2265
+ return;
2266
+ }
1773
2267
  await closeWithGrace(session.page);
1774
2268
  if (session.persistent !== true) await closeWithGrace(session.context);
1775
2269
  if (session.sharedBrowser !== true) await closeWithGrace(session.browser);
@@ -1865,10 +2359,10 @@ const WEB_FETCH_PLAYWRIGHT_SETTINGS_NAMESPACE = "web-fetch-playwright";
1865
2359
  * live references a settings commit updates in place.
1866
2360
  */
1867
2361
  function apply(ctx, config) {
1868
- const provider = new PlaywrightFetchProvider(() => resolveConfig(snapshotsOf(config)));
2362
+ const provider = new PlaywrightFetchProvider(() => resolveConfig(snapshotsOf(config)), void 0, () => ctx.get("agents")?.currentInitiator()?.session.id);
1869
2363
  ctx.effect(() => () => {
1870
2364
  provider.dispose();
1871
- }, "dsh-web-fetch-playwright: shared CDP connection");
2365
+ }, "dsh-web-fetch-playwright: CDP connections");
1872
2366
  ctx.web.registerFetchProvider(provider);
1873
2367
  }
1874
2368
  //#endregion