@skrillex1224/playwright-toolkit 2.1.170 → 2.1.171

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
@@ -546,7 +546,7 @@ var CrawlerError = class _CrawlerError extends Error {
546
546
  // src/apify-kit.js
547
547
  import { serializeError as serializeError2 } from "serialize-error";
548
548
 
549
- // src/proxy-meter-runtime.js
549
+ // src/internals/proxy-meter-runtime.js
550
550
  import { spawn, spawnSync } from "child_process";
551
551
  import { existsSync, mkdirSync, readFileSync, rmSync } from "fs";
552
552
  import { tmpdir } from "os";
@@ -615,6 +615,10 @@ var resolveResourceTypeMeta = (domain) => {
615
615
  };
616
616
  var resolveScriptPath = () => {
617
617
  const baseDir = typeof __dirname !== "undefined" ? __dirname : path.dirname(fileURLToPath(import.meta.url));
618
+ const internalCandidate = path.join(baseDir, "internals", "proxy-meter.js");
619
+ if (existsSync(internalCandidate)) {
620
+ return internalCandidate;
621
+ }
618
622
  return path.join(baseDir, "proxy-meter.js");
619
623
  };
620
624
  var pickFreePort = () => {
@@ -898,6 +902,12 @@ var getProxyMeterSnapshot = async (options = {}) => {
898
902
  }
899
903
  return snapshot;
900
904
  };
905
+ var ProxyMeterRuntime = {
906
+ startProxyMeter,
907
+ recordProxyMeterResourceType,
908
+ stopProxyMeter,
909
+ getProxyMeterSnapshot
910
+ };
901
911
 
902
912
  // src/apify-kit.js
903
913
  var logger3 = createInternalLogger("ApifyKit");
@@ -1017,7 +1027,7 @@ async function createApifyKit() {
1017
1027
  * @param {Object} data - 要推送的数据对象
1018
1028
  */
1019
1029
  async pushSuccess(data) {
1020
- const traffic = await getProxyMeterSnapshot({ finalize: true });
1030
+ const traffic = await ProxyMeterRuntime.getProxyMeterSnapshot({ finalize: true });
1021
1031
  await Actor2.pushData({
1022
1032
  // 固定为0
1023
1033
  code: Code.Success,
@@ -1039,7 +1049,7 @@ async function createApifyKit() {
1039
1049
  const isCrawlerError = CrawlerError.isCrawlerError(error);
1040
1050
  const code = isCrawlerError ? error.code : Code.UnknownError;
1041
1051
  const context = isCrawlerError ? error.context : {};
1042
- const traffic = await getProxyMeterSnapshot({ finalize: true });
1052
+ const traffic = await ProxyMeterRuntime.getProxyMeterSnapshot({ finalize: true });
1043
1053
  await Actor2.pushData({
1044
1054
  // 如果是 CrawlerError,使用其 code,否则使用默认 Failed code
1045
1055
  code,
@@ -1670,6 +1680,7 @@ var normalizeByPassDomains = (domains) => {
1670
1680
  if (!Array.isArray(domains)) return [];
1671
1681
  return domains.map((item) => String(item || "").trim()).filter(Boolean);
1672
1682
  };
1683
+ var normalizeHostname = (value = "") => String(value || "").trim().toLowerCase();
1673
1684
  var buildByPassDomainRule = (rawPattern) => {
1674
1685
  const pattern = String(rawPattern || "").trim().toLowerCase();
1675
1686
  if (!pattern) return null;
@@ -1690,7 +1701,7 @@ var buildByPassDomainRules = (domains = []) => {
1690
1701
  var findMatchedByPassRule = (rules = [], requestUrl = "") => {
1691
1702
  let hostname = "";
1692
1703
  try {
1693
- hostname = new URL(String(requestUrl || "")).hostname.toLowerCase();
1704
+ hostname = normalizeHostname(new URL(String(requestUrl || "")).hostname);
1694
1705
  } catch {
1695
1706
  return null;
1696
1707
  }
@@ -1707,6 +1718,41 @@ var findMatchedByPassRule = (rules = [], requestUrl = "") => {
1707
1718
  hostname
1708
1719
  };
1709
1720
  };
1721
+ var isDomainCoveredByByPass = (domain = "", rules = []) => {
1722
+ const hostname = normalizeHostname(domain);
1723
+ if (!hostname) return false;
1724
+ for (const rule of rules || []) {
1725
+ if (!rule || typeof rule.test !== "function") continue;
1726
+ if (rule.test(hostname)) return true;
1727
+ }
1728
+ return false;
1729
+ };
1730
+ var resolveRouteByProxy = ({
1731
+ requestUrl = "",
1732
+ enableProxy = false,
1733
+ byPassRules = []
1734
+ }) => {
1735
+ if (!enableProxy) {
1736
+ return { route: "direct", matchedRule: null, hostname: "" };
1737
+ }
1738
+ const matched = findMatchedByPassRule(byPassRules, requestUrl);
1739
+ if (!matched) {
1740
+ return { route: "proxy", matchedRule: null, hostname: "" };
1741
+ }
1742
+ if (matched.rule) {
1743
+ return { route: "direct", matchedRule: matched.rule, hostname: matched.hostname };
1744
+ }
1745
+ return { route: "proxy", matchedRule: null, hostname: matched.hostname };
1746
+ };
1747
+ var ByPass = {
1748
+ normalizeByPassDomains,
1749
+ normalizeHostname,
1750
+ buildByPassDomainRule,
1751
+ buildByPassDomainRules,
1752
+ findMatchedByPassRule,
1753
+ isDomainCoveredByByPass,
1754
+ resolveRouteByProxy
1755
+ };
1710
1756
 
1711
1757
  // src/launch.js
1712
1758
  var logger7 = createInternalLogger("Launch");
@@ -1724,7 +1770,7 @@ var resolveProxyLaunchOptions = (proxyConfiguration = {}) => {
1724
1770
  if (!enableProxy || !proxyUrl) {
1725
1771
  return { byPassDomains: [], enableProxy, proxyUrl };
1726
1772
  }
1727
- const byPassDomains = normalizeByPassDomains(config.by_pass_domains);
1773
+ const byPassDomains = ByPass.normalizeByPassDomains(config.by_pass_domains);
1728
1774
  return { byPassDomains, enableProxy, proxyUrl };
1729
1775
  };
1730
1776
  var Launch = {
@@ -1742,8 +1788,8 @@ var Launch = {
1742
1788
  postNavigationHooks = []
1743
1789
  } = normalizedOptions;
1744
1790
  const { byPassDomains, enableProxy, proxyUrl } = resolveProxyLaunchOptions(proxyConfiguration);
1745
- const byPassRules = buildByPassDomainRules(byPassDomains);
1746
- const proxyMeter = enableProxy && proxyUrl ? startProxyMeter({ proxyUrl, debugMode }) : null;
1791
+ const byPassRules = ByPass.buildByPassDomainRules(byPassDomains);
1792
+ const proxyMeter = enableProxy && proxyUrl ? ProxyMeterRuntime.startProxyMeter({ proxyUrl, debugMode }) : null;
1747
1793
  const launchProxy = proxyMeter ? { server: proxyMeter.server } : null;
1748
1794
  if (launchProxy && byPassDomains.length > 0) {
1749
1795
  launchProxy.bypass = byPassDomains.join(",");
@@ -1797,9 +1843,9 @@ var Launch = {
1797
1843
  const requestHandler = (req) => {
1798
1844
  const requestUrl = req.url();
1799
1845
  const resourceType = req.resourceType();
1800
- const matched = byPassDomains.length > 0 ? findMatchedByPassRule(byPassRules, requestUrl) : null;
1801
- if (launchProxy && !matched) {
1802
- recordProxyMeterResourceType(requestUrl, resourceType);
1846
+ const matched = byPassDomains.length > 0 ? ByPass.findMatchedByPassRule(byPassRules, requestUrl) : null;
1847
+ if (launchProxy) {
1848
+ ProxyMeterRuntime.recordProxyMeterResourceType(requestUrl, resourceType);
1803
1849
  }
1804
1850
  if (!enableByPassLogger || byPassDomains.length === 0) return;
1805
1851
  if (!matched || !matched.rule) return;
@@ -4153,6 +4199,7 @@ var usePlaywrightToolKit = () => {
4153
4199
  Display,
4154
4200
  Logger,
4155
4201
  Share,
4202
+ ByPass,
4156
4203
  $Internals: { LOG_TEMPLATES, stripAnsi }
4157
4204
  };
4158
4205
  };