browserclaw 0.5.2 → 0.5.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/dist/index.js CHANGED
@@ -929,33 +929,37 @@ async function findPageByTargetId(browser, targetId, cdpUrl) {
929
929
  }
930
930
  if (tid && tid === targetId) return page;
931
931
  }
932
- if (!resolvedViaCdp && pages.length === 1) {
933
- return pages[0];
934
- }
935
932
  if (cdpUrl) {
936
933
  try {
937
934
  const listUrl = `${normalizeCdpHttpBaseForJsonEndpoints(cdpUrl)}/json/list`;
938
935
  const headers = {};
939
936
  if (cached?.authToken) headers["Authorization"] = `Bearer ${cached.authToken}`;
940
- const response = await fetch(listUrl, { headers });
941
- if (response.ok) {
942
- const targets = await response.json();
943
- const target = targets.find((t) => t.id === targetId);
944
- if (target) {
945
- const urlMatch = pages.filter((p) => p.url() === target.url);
946
- if (urlMatch.length === 1) return urlMatch[0];
947
- if (urlMatch.length > 1) {
948
- const sameUrlTargets = targets.filter((t) => t.url === target.url);
949
- if (sameUrlTargets.length === urlMatch.length) {
950
- const idx = sameUrlTargets.findIndex((t) => t.id === targetId);
951
- if (idx >= 0 && idx < urlMatch.length) return urlMatch[idx];
937
+ const ctrl = new AbortController();
938
+ const t = setTimeout(() => ctrl.abort(), 2e3);
939
+ try {
940
+ const response = await fetch(listUrl, { headers, signal: ctrl.signal });
941
+ if (response.ok) {
942
+ const targets = await response.json();
943
+ const target = targets.find((entry) => entry.id === targetId);
944
+ if (target) {
945
+ const urlMatch = pages.filter((p) => p.url() === target.url);
946
+ if (urlMatch.length === 1) return urlMatch[0];
947
+ if (urlMatch.length > 1) {
948
+ const sameUrlTargets = targets.filter((entry) => entry.url === target.url);
949
+ if (sameUrlTargets.length === urlMatch.length) {
950
+ const idx = sameUrlTargets.findIndex((entry) => entry.id === targetId);
951
+ if (idx >= 0 && idx < urlMatch.length) return urlMatch[idx];
952
+ }
952
953
  }
953
954
  }
954
955
  }
956
+ } finally {
957
+ clearTimeout(t);
955
958
  }
956
959
  } catch {
957
960
  }
958
961
  }
962
+ if (!resolvedViaCdp && pages.length === 1) return pages[0];
959
963
  return null;
960
964
  }
961
965
  async function getPageForTargetId(opts) {