dsh-web-fetch-playwright 0.2.13 → 0.2.14

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,155 +427,549 @@ 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;
568
833
  }
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 = "";
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;
574
852
  }
575
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);
946
+ }
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);
970
+ }
971
+ return pending;
972
+ }
576
973
  isLive(browser) {
577
974
  return browser.isConnected?.() !== false;
578
975
  }
@@ -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 {
@@ -1770,6 +2191,10 @@ function capResult(url, statusCode, body) {
1770
2191
  */
1771
2192
  async function closeSession(session) {
1772
2193
  if (session === void 0) return;
2194
+ if (session.release !== void 0) {
2195
+ await closeWithGrace({ close: session.release });
2196
+ return;
2197
+ }
1773
2198
  await closeWithGrace(session.page);
1774
2199
  if (session.persistent !== true) await closeWithGrace(session.context);
1775
2200
  if (session.sharedBrowser !== true) await closeWithGrace(session.browser);
@@ -1865,10 +2290,10 @@ const WEB_FETCH_PLAYWRIGHT_SETTINGS_NAMESPACE = "web-fetch-playwright";
1865
2290
  * live references a settings commit updates in place.
1866
2291
  */
1867
2292
  function apply(ctx, config) {
1868
- const provider = new PlaywrightFetchProvider(() => resolveConfig(snapshotsOf(config)));
2293
+ const provider = new PlaywrightFetchProvider(() => resolveConfig(snapshotsOf(config)), void 0, () => ctx.get("agents")?.currentInitiator()?.session.id);
1869
2294
  ctx.effect(() => () => {
1870
2295
  provider.dispose();
1871
- }, "dsh-web-fetch-playwright: shared CDP connection");
2296
+ }, "dsh-web-fetch-playwright: CDP connections");
1872
2297
  ctx.web.registerFetchProvider(provider);
1873
2298
  }
1874
2299
  //#endregion