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.cjs CHANGED
@@ -957,33 +957,37 @@ async function findPageByTargetId(browser, targetId, cdpUrl) {
957
957
  }
958
958
  if (tid && tid === targetId) return page;
959
959
  }
960
- if (!resolvedViaCdp && pages.length === 1) {
961
- return pages[0];
962
- }
963
960
  if (cdpUrl) {
964
961
  try {
965
962
  const listUrl = `${normalizeCdpHttpBaseForJsonEndpoints(cdpUrl)}/json/list`;
966
963
  const headers = {};
967
964
  if (cached?.authToken) headers["Authorization"] = `Bearer ${cached.authToken}`;
968
- const response = await fetch(listUrl, { headers });
969
- if (response.ok) {
970
- const targets = await response.json();
971
- const target = targets.find((t) => t.id === targetId);
972
- if (target) {
973
- const urlMatch = pages.filter((p) => p.url() === target.url);
974
- if (urlMatch.length === 1) return urlMatch[0];
975
- if (urlMatch.length > 1) {
976
- const sameUrlTargets = targets.filter((t) => t.url === target.url);
977
- if (sameUrlTargets.length === urlMatch.length) {
978
- const idx = sameUrlTargets.findIndex((t) => t.id === targetId);
979
- if (idx >= 0 && idx < urlMatch.length) return urlMatch[idx];
965
+ const ctrl = new AbortController();
966
+ const t = setTimeout(() => ctrl.abort(), 2e3);
967
+ try {
968
+ const response = await fetch(listUrl, { headers, signal: ctrl.signal });
969
+ if (response.ok) {
970
+ const targets = await response.json();
971
+ const target = targets.find((entry) => entry.id === targetId);
972
+ if (target) {
973
+ const urlMatch = pages.filter((p) => p.url() === target.url);
974
+ if (urlMatch.length === 1) return urlMatch[0];
975
+ if (urlMatch.length > 1) {
976
+ const sameUrlTargets = targets.filter((entry) => entry.url === target.url);
977
+ if (sameUrlTargets.length === urlMatch.length) {
978
+ const idx = sameUrlTargets.findIndex((entry) => entry.id === targetId);
979
+ if (idx >= 0 && idx < urlMatch.length) return urlMatch[idx];
980
+ }
980
981
  }
981
982
  }
982
983
  }
984
+ } finally {
985
+ clearTimeout(t);
983
986
  }
984
987
  } catch {
985
988
  }
986
989
  }
990
+ if (!resolvedViaCdp && pages.length === 1) return pages[0];
987
991
  return null;
988
992
  }
989
993
  async function getPageForTargetId(opts) {