@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.js CHANGED
@@ -5191,8 +5191,9 @@ var DefaultLaunch = {
5191
5191
  // src/internals/launch/cloak.js
5192
5192
  import { execFile } from "node:child_process";
5193
5193
  import { promisify } from "node:util";
5194
- var logger8 = createInternalLogger("Cloak");
5194
+ var logger8 = createInternalLogger("Launch");
5195
5195
  var execFileAsync = promisify(execFile);
5196
+ var REQUEST_HOOK_FLAG2 = Symbol("playwright-toolkit-cloak-request-hook");
5196
5197
  var DEFAULT_CLOAK_CRAWLER_BASE_OPTIONS = Object.freeze({
5197
5198
  maxConcurrency: 1,
5198
5199
  maxRequestRetries: 0,
@@ -5253,6 +5254,86 @@ var resolveCloakProxy = (proxyConfiguration = {}) => {
5253
5254
  bypass: byPassDomains.join(",")
5254
5255
  };
5255
5256
  };
5257
+ var resolveProxyLaunchOptions2 = (proxyConfiguration = {}) => {
5258
+ const config = normalizeObject(proxyConfiguration);
5259
+ const proxyUrl = String(config.proxy_url || "").trim();
5260
+ const enableProxy = typeof config.enable_proxy === "boolean" ? config.enable_proxy : proxyUrl !== "";
5261
+ const byPassDomains = enableProxy && proxyUrl ? ByPass.normalizeByPassDomains(config.by_pass_domains) : [];
5262
+ return {
5263
+ byPassDomains,
5264
+ enableProxy,
5265
+ proxyUrl
5266
+ };
5267
+ };
5268
+ var buildMeteredProxy = ({ proxyConfiguration = {}, debugMode = false } = {}) => {
5269
+ const { byPassDomains, enableProxy, proxyUrl } = resolveProxyLaunchOptions2(proxyConfiguration);
5270
+ const byPassRules = ByPass.buildByPassDomainRules(byPassDomains);
5271
+ const proxyMeter = enableProxy && proxyUrl ? ProxyMeterRuntime.startProxyMeter({ proxyUrl, debugMode }) : null;
5272
+ const launchProxy = proxyMeter ? { server: proxyMeter.server } : null;
5273
+ if (launchProxy && byPassDomains.length > 0) {
5274
+ launchProxy.bypass = byPassDomains.join(",");
5275
+ }
5276
+ return {
5277
+ byPassDomains,
5278
+ byPassRules,
5279
+ enableProxy,
5280
+ proxyUrl,
5281
+ launchProxy
5282
+ };
5283
+ };
5284
+ var logProxyLaunchState = ({
5285
+ byPassDomains = [],
5286
+ debugMode = false,
5287
+ enableByPassLogger = false,
5288
+ enableProxy = false,
5289
+ launchProxy = null,
5290
+ proxyUrl = ""
5291
+ } = {}) => {
5292
+ if (!enableByPassLogger) return;
5293
+ if (launchProxy) {
5294
+ let upstreamLabel = "";
5295
+ try {
5296
+ const parsedProxyUrl = new URL(proxyUrl.includes("://") ? proxyUrl : `http://${proxyUrl}`);
5297
+ upstreamLabel = `${parsedProxyUrl.protocol}//${parsedProxyUrl.host}`;
5298
+ } catch {
5299
+ }
5300
+ logger8.info(
5301
+ `[\u4EE3\u7406\u5DF2\u542F\u7528] \u672C\u5730=${launchProxy.server} \u4E0A\u6E38=${upstreamLabel || "-"} \u76F4\u8FDE\u57DF\u540D=${byPassDomains.join(",")}`
5302
+ );
5303
+ 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`);
5304
+ return;
5305
+ }
5306
+ if (enableProxy) {
5307
+ logger8.info("[\u4EE3\u7406\u672A\u542F\u7528] enable_proxy=true \u4F46 proxy_url \u4E3A\u7A7A");
5308
+ } else if (proxyUrl) {
5309
+ logger8.info("[\u4EE3\u7406\u672A\u542F\u7528] enable_proxy=false \u4E14 proxy_url \u5DF2\u914D\u7F6E");
5310
+ }
5311
+ 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`);
5312
+ };
5313
+ var createProxyRequestHook = ({
5314
+ byPassDomains = [],
5315
+ byPassRules = [],
5316
+ enableByPassLogger = false,
5317
+ launchProxy = null
5318
+ } = {}) => {
5319
+ return (page) => {
5320
+ if (!page || typeof page.on !== "function" || page[REQUEST_HOOK_FLAG2]) {
5321
+ return;
5322
+ }
5323
+ page[REQUEST_HOOK_FLAG2] = true;
5324
+ page.on("request", (req) => {
5325
+ const requestUrl = req.url();
5326
+ const resourceType = req.resourceType();
5327
+ const matched = byPassDomains.length > 0 ? ByPass.findMatchedByPassRule(byPassRules, requestUrl) : null;
5328
+ if (launchProxy) {
5329
+ ProxyMeterRuntime.recordProxyMeterResourceType(requestUrl, resourceType);
5330
+ }
5331
+ if (!enableByPassLogger || byPassDomains.length === 0) return;
5332
+ if (!matched || !matched.rule) return;
5333
+ 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}`);
5334
+ });
5335
+ };
5336
+ };
5256
5337
  var extractFingerprintArg = (launchOptions = {}) => {
5257
5338
  const args = Array.isArray(launchOptions?.args) ? launchOptions.args : [];
5258
5339
  return args.find((value) => String(value || "").startsWith("--fingerprint=")) || "";
@@ -5364,6 +5445,8 @@ var CloakLaunch = {
5364
5445
  const normalizedOptions = normalizeObject(options);
5365
5446
  const {
5366
5447
  proxyConfiguration = {},
5448
+ log: logOptions = null,
5449
+ debugMode = false,
5367
5450
  runInHeadfulMode = false,
5368
5451
  isRunningOnApify = false,
5369
5452
  launcher = null,
@@ -5381,8 +5464,15 @@ var CloakLaunch = {
5381
5464
  const patchedBrowsers = /* @__PURE__ */ new WeakSet();
5382
5465
  const defaultArgs = isRunningOnApify ? ["--no-sandbox", "--disable-setuid-sandbox"] : [];
5383
5466
  const extraArgs = normalizeStringArray(normalizedCloakOptions.args);
5384
- const proxy = hasOwn(normalizedCloakOptions, "proxy") ? normalizedCloakOptions.proxy : resolveCloakProxy(proxyConfiguration);
5467
+ const hasExplicitProxy = hasOwn(normalizedCloakOptions, "proxy");
5468
+ const proxyLaunchState = hasExplicitProxy ? {
5469
+ ...resolveProxyLaunchOptions2(proxyConfiguration),
5470
+ byPassRules: [],
5471
+ launchProxy: null
5472
+ } : buildMeteredProxy({ proxyConfiguration, debugMode });
5473
+ const proxy = hasExplicitProxy ? normalizedCloakOptions.proxy : proxyLaunchState.launchProxy;
5385
5474
  const headless = hasOwn(normalizedCloakOptions, "headless") ? normalizedCloakOptions.headless : !runInHeadfulMode || isRunningOnApify;
5475
+ const enableByPassLogger = Boolean(logOptions && logOptions.enable);
5386
5476
  const mergedCloakOptions = {
5387
5477
  ...normalizedCloakOptions,
5388
5478
  headless,
@@ -5392,8 +5482,23 @@ var CloakLaunch = {
5392
5482
  const launchOptions = await buildCloakLaunchOptions(mergedCloakOptions);
5393
5483
  const fingerprintArg = extractFingerprintArg(launchOptions);
5394
5484
  const internalPreNavigationHook = createStableGotoHook(recommendedGotoOptions);
5485
+ const proxyRequestHook = createProxyRequestHook({
5486
+ byPassDomains: proxyLaunchState.byPassDomains,
5487
+ byPassRules: proxyLaunchState.byPassRules,
5488
+ enableByPassLogger,
5489
+ launchProxy: proxyLaunchState.launchProxy
5490
+ });
5395
5491
  const normalizedPreNavigationHooks = Array.isArray(preNavigationHooks) ? preNavigationHooks : [];
5396
5492
  const normalizedPostNavigationHooks = Array.isArray(postNavigationHooks) ? postNavigationHooks : [];
5493
+ if (hasExplicitProxy && enableByPassLogger) {
5494
+ logger8.info("[\u4EE3\u7406\u5DF2\u542F\u7528] \u4F7F\u7528 cloakOptions.proxy\uFF0C\u8DF3\u8FC7 toolkit \u672C\u5730\u6D41\u91CF\u89C2\u6D4B");
5495
+ } else {
5496
+ logProxyLaunchState({
5497
+ ...proxyLaunchState,
5498
+ debugMode,
5499
+ enableByPassLogger
5500
+ });
5501
+ }
5397
5502
  const crawlerOptions = {
5398
5503
  ...DEFAULT_CLOAK_CRAWLER_BASE_OPTIONS,
5399
5504
  ...normalizeObject(crawlerBaseOptions),
@@ -5410,7 +5515,13 @@ var CloakLaunch = {
5410
5515
  patchedBrowsers,
5411
5516
  humanizeOptions
5412
5517
  }),
5413
- preNavigationHooks: [internalPreNavigationHook, ...normalizedPreNavigationHooks],
5518
+ preNavigationHooks: [
5519
+ async (crawlingContext, gotoOptions = {}) => {
5520
+ proxyRequestHook(crawlingContext?.page);
5521
+ await internalPreNavigationHook(crawlingContext, gotoOptions);
5522
+ },
5523
+ ...normalizedPreNavigationHooks
5524
+ ],
5414
5525
  ...normalizedPostNavigationHooks.length > 0 ? { postNavigationHooks: normalizedPostNavigationHooks } : {}
5415
5526
  };
5416
5527
  const closeActiveBrowsers = async () => {