@skrillex1224/playwright-toolkit 3.0.1 → 3.0.2

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
@@ -5219,8 +5219,9 @@ var DefaultLaunch = {
5219
5219
  // src/internals/launch/cloak.js
5220
5220
  var import_node_child_process2 = require("node:child_process");
5221
5221
  var import_node_util = require("node:util");
5222
- var logger8 = createInternalLogger("Cloak");
5222
+ var logger8 = createInternalLogger("Launch");
5223
5223
  var execFileAsync = (0, import_node_util.promisify)(import_node_child_process2.execFile);
5224
+ var REQUEST_HOOK_FLAG2 = Symbol("playwright-toolkit-cloak-request-hook");
5224
5225
  var DEFAULT_CLOAK_CRAWLER_BASE_OPTIONS = Object.freeze({
5225
5226
  maxConcurrency: 1,
5226
5227
  maxRequestRetries: 0,
@@ -5281,6 +5282,86 @@ var resolveCloakProxy = (proxyConfiguration = {}) => {
5281
5282
  bypass: byPassDomains.join(",")
5282
5283
  };
5283
5284
  };
5285
+ var resolveProxyLaunchOptions2 = (proxyConfiguration = {}) => {
5286
+ const config = normalizeObject(proxyConfiguration);
5287
+ const proxyUrl = String(config.proxy_url || "").trim();
5288
+ const enableProxy = typeof config.enable_proxy === "boolean" ? config.enable_proxy : proxyUrl !== "";
5289
+ const byPassDomains = enableProxy && proxyUrl ? ByPass.normalizeByPassDomains(config.by_pass_domains) : [];
5290
+ return {
5291
+ byPassDomains,
5292
+ enableProxy,
5293
+ proxyUrl
5294
+ };
5295
+ };
5296
+ var buildMeteredProxy = ({ proxyConfiguration = {}, debugMode = false } = {}) => {
5297
+ const { byPassDomains, enableProxy, proxyUrl } = resolveProxyLaunchOptions2(proxyConfiguration);
5298
+ const byPassRules = ByPass.buildByPassDomainRules(byPassDomains);
5299
+ const proxyMeter = enableProxy && proxyUrl ? ProxyMeterRuntime.startProxyMeter({ proxyUrl, debugMode }) : null;
5300
+ const launchProxy = proxyMeter ? { server: proxyMeter.server } : null;
5301
+ if (launchProxy && byPassDomains.length > 0) {
5302
+ launchProxy.bypass = byPassDomains.join(",");
5303
+ }
5304
+ return {
5305
+ byPassDomains,
5306
+ byPassRules,
5307
+ enableProxy,
5308
+ proxyUrl,
5309
+ launchProxy
5310
+ };
5311
+ };
5312
+ var logProxyLaunchState = ({
5313
+ byPassDomains = [],
5314
+ debugMode = false,
5315
+ enableByPassLogger = false,
5316
+ enableProxy = false,
5317
+ launchProxy = null,
5318
+ proxyUrl = ""
5319
+ } = {}) => {
5320
+ if (!enableByPassLogger) return;
5321
+ if (launchProxy) {
5322
+ let upstreamLabel = "";
5323
+ try {
5324
+ const parsedProxyUrl = new URL(proxyUrl.includes("://") ? proxyUrl : `http://${proxyUrl}`);
5325
+ upstreamLabel = `${parsedProxyUrl.protocol}//${parsedProxyUrl.host}`;
5326
+ } catch {
5327
+ }
5328
+ logger8.info(
5329
+ `[\u4EE3\u7406\u5DF2\u542F\u7528] \u672C\u5730=${launchProxy.server} \u4E0A\u6E38=${upstreamLabel || "-"} \u76F4\u8FDE\u57DF\u540D=${byPassDomains.join(",")}`
5330
+ );
5331
+ logger8.info(`[\u6D41\u91CF\u89C2\u6D4B] \u9010\u8BF7\u6C42\u8C03\u8BD5=${Boolean(debugMode) ? "\u5F00\u542F" : "\u5173\u95ED"}\uFF08\u6C47\u603B\u59CB\u7EC8\u5F00\u542F\uFF09`);
5332
+ return;
5333
+ }
5334
+ if (enableProxy) {
5335
+ logger8.info("[\u4EE3\u7406\u672A\u542F\u7528] enable_proxy=true \u4F46 proxy_url \u4E3A\u7A7A");
5336
+ } else if (proxyUrl) {
5337
+ logger8.info("[\u4EE3\u7406\u672A\u542F\u7528] enable_proxy=false \u4E14 proxy_url \u5DF2\u914D\u7F6E");
5338
+ }
5339
+ logger8.info(`[\u6D41\u91CF\u89C2\u6D4B] \u9010\u8BF7\u6C42\u8C03\u8BD5=${Boolean(debugMode) ? "\u5F00\u542F" : "\u5173\u95ED"}\uFF08\u6C47\u603B\u59CB\u7EC8\u5F00\u542F\uFF09`);
5340
+ };
5341
+ var createProxyRequestHook = ({
5342
+ byPassDomains = [],
5343
+ byPassRules = [],
5344
+ enableByPassLogger = false,
5345
+ launchProxy = null
5346
+ } = {}) => {
5347
+ return (page) => {
5348
+ if (!page || typeof page.on !== "function" || page[REQUEST_HOOK_FLAG2]) {
5349
+ return;
5350
+ }
5351
+ page[REQUEST_HOOK_FLAG2] = true;
5352
+ page.on("request", (req) => {
5353
+ const requestUrl = req.url();
5354
+ const resourceType = req.resourceType();
5355
+ const matched = byPassDomains.length > 0 ? ByPass.findMatchedByPassRule(byPassRules, requestUrl) : null;
5356
+ if (launchProxy) {
5357
+ ProxyMeterRuntime.recordProxyMeterResourceType(requestUrl, resourceType);
5358
+ }
5359
+ if (!enableByPassLogger || byPassDomains.length === 0) return;
5360
+ if (!matched || !matched.rule) return;
5361
+ logger8.info(`[\u76F4\u8FDE\u547D\u4E2D] \u89C4\u5219=${matched.rule.pattern} \u57DF\u540D=${matched.hostname} \u8D44\u6E90\u7C7B\u578B=${resourceType} \u65B9\u6CD5=${req.method()} \u5730\u5740=${requestUrl}`);
5362
+ });
5363
+ };
5364
+ };
5284
5365
  var extractFingerprintArg = (launchOptions = {}) => {
5285
5366
  const args = Array.isArray(launchOptions?.args) ? launchOptions.args : [];
5286
5367
  return args.find((value) => String(value || "").startsWith("--fingerprint=")) || "";
@@ -5392,6 +5473,8 @@ var CloakLaunch = {
5392
5473
  const normalizedOptions = normalizeObject(options);
5393
5474
  const {
5394
5475
  proxyConfiguration = {},
5476
+ log: logOptions = null,
5477
+ debugMode = false,
5395
5478
  runInHeadfulMode = false,
5396
5479
  isRunningOnApify = false,
5397
5480
  launcher = null,
@@ -5409,8 +5492,15 @@ var CloakLaunch = {
5409
5492
  const patchedBrowsers = /* @__PURE__ */ new WeakSet();
5410
5493
  const defaultArgs = isRunningOnApify ? ["--no-sandbox", "--disable-setuid-sandbox"] : [];
5411
5494
  const extraArgs = normalizeStringArray(normalizedCloakOptions.args);
5412
- const proxy = hasOwn(normalizedCloakOptions, "proxy") ? normalizedCloakOptions.proxy : resolveCloakProxy(proxyConfiguration);
5495
+ const hasExplicitProxy = hasOwn(normalizedCloakOptions, "proxy");
5496
+ const proxyLaunchState = hasExplicitProxy ? {
5497
+ ...resolveProxyLaunchOptions2(proxyConfiguration),
5498
+ byPassRules: [],
5499
+ launchProxy: null
5500
+ } : buildMeteredProxy({ proxyConfiguration, debugMode });
5501
+ const proxy = hasExplicitProxy ? normalizedCloakOptions.proxy : proxyLaunchState.launchProxy;
5413
5502
  const headless = hasOwn(normalizedCloakOptions, "headless") ? normalizedCloakOptions.headless : !runInHeadfulMode || isRunningOnApify;
5503
+ const enableByPassLogger = Boolean(logOptions && logOptions.enable);
5414
5504
  const mergedCloakOptions = {
5415
5505
  ...normalizedCloakOptions,
5416
5506
  headless,
@@ -5420,8 +5510,23 @@ var CloakLaunch = {
5420
5510
  const launchOptions = await buildCloakLaunchOptions(mergedCloakOptions);
5421
5511
  const fingerprintArg = extractFingerprintArg(launchOptions);
5422
5512
  const internalPreNavigationHook = createStableGotoHook(recommendedGotoOptions);
5513
+ const proxyRequestHook = createProxyRequestHook({
5514
+ byPassDomains: proxyLaunchState.byPassDomains,
5515
+ byPassRules: proxyLaunchState.byPassRules,
5516
+ enableByPassLogger,
5517
+ launchProxy: proxyLaunchState.launchProxy
5518
+ });
5423
5519
  const normalizedPreNavigationHooks = Array.isArray(preNavigationHooks) ? preNavigationHooks : [];
5424
5520
  const normalizedPostNavigationHooks = Array.isArray(postNavigationHooks) ? postNavigationHooks : [];
5521
+ if (hasExplicitProxy && enableByPassLogger) {
5522
+ logger8.info("[\u4EE3\u7406\u5DF2\u542F\u7528] \u4F7F\u7528 cloakOptions.proxy\uFF0C\u8DF3\u8FC7 toolkit \u672C\u5730\u6D41\u91CF\u89C2\u6D4B");
5523
+ } else {
5524
+ logProxyLaunchState({
5525
+ ...proxyLaunchState,
5526
+ debugMode,
5527
+ enableByPassLogger
5528
+ });
5529
+ }
5425
5530
  const crawlerOptions = {
5426
5531
  ...DEFAULT_CLOAK_CRAWLER_BASE_OPTIONS,
5427
5532
  ...normalizeObject(crawlerBaseOptions),
@@ -5438,7 +5543,13 @@ var CloakLaunch = {
5438
5543
  patchedBrowsers,
5439
5544
  humanizeOptions
5440
5545
  }),
5441
- preNavigationHooks: [internalPreNavigationHook, ...normalizedPreNavigationHooks],
5546
+ preNavigationHooks: [
5547
+ async (crawlingContext, gotoOptions = {}) => {
5548
+ proxyRequestHook(crawlingContext?.page);
5549
+ await internalPreNavigationHook(crawlingContext, gotoOptions);
5550
+ },
5551
+ ...normalizedPreNavigationHooks
5552
+ ],
5442
5553
  ...normalizedPostNavigationHooks.length > 0 ? { postNavigationHooks: normalizedPostNavigationHooks } : {}
5443
5554
  };
5444
5555
  const closeActiveBrowsers = async () => {