@skrillex1224/playwright-toolkit 2.1.10 → 2.1.11
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 +29 -18
- package/dist/index.cjs.map +2 -2
- package/dist/index.js +29 -18
- package/dist/index.js.map +2 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1206,6 +1206,14 @@ import { gotScraping } from "got-scraping";
|
|
|
1206
1206
|
import { Agent as HttpAgent } from "http";
|
|
1207
1207
|
import { Agent as HttpsAgent } from "https";
|
|
1208
1208
|
var logger9 = createLogger("Interception");
|
|
1209
|
+
var SHARED_HTTP_AGENT = new HttpAgent({ keepAlive: false });
|
|
1210
|
+
var SHARED_HTTPS_AGENT = new HttpsAgent({ keepAlive: false, rejectUnauthorized: false });
|
|
1211
|
+
var DirectConfig = {
|
|
1212
|
+
/** 直连请求超时时间(秒) */
|
|
1213
|
+
directTimeout: 12,
|
|
1214
|
+
/** 静默扩展名:这些扩展名的直连成功日志用 debug 级别 */
|
|
1215
|
+
silentExtensions: [".js"]
|
|
1216
|
+
};
|
|
1209
1217
|
var ARCHIVE_EXTENSIONS = [".7z", ".zip", ".rar", ".gz", ".bz2", ".tar", ".zst"];
|
|
1210
1218
|
var EXECUTABLE_EXTENSIONS = [".exe", ".apk", ".bin", ".dmg", ".jar", ".class"];
|
|
1211
1219
|
var DOCUMENT_EXTENSIONS = [".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".csv"];
|
|
@@ -1330,14 +1338,17 @@ var Interception = {
|
|
|
1330
1338
|
if (mergedBlockingConfig.blockOther) enabledCategories.push("\u5176\u4ED6");
|
|
1331
1339
|
logger9.start("setup", hasDirectDomains ? `\u76F4\u8FDE\u57DF\u540D: [${directDomains.length} \u4E2A] | \u5C4F\u853D: [${enabledCategories.join(", ")}]` : `\u4EC5\u8D44\u6E90\u5C4F\u853D\u6A21\u5F0F | \u5C4F\u853D: [${enabledCategories.join(", ")}]`);
|
|
1332
1340
|
await page.route("**/*", async (route) => {
|
|
1341
|
+
let handled = false;
|
|
1333
1342
|
try {
|
|
1334
1343
|
const request = route.request();
|
|
1335
1344
|
const url = request.url();
|
|
1336
1345
|
const urlLower = url.toLowerCase();
|
|
1337
1346
|
const urlPath = urlLower.split("?")[0];
|
|
1347
|
+
const isSilent = DirectConfig.silentExtensions.some((ext) => urlPath.endsWith(ext));
|
|
1338
1348
|
const shouldBlock = blockedExtensions.some((ext) => urlPath.endsWith(ext));
|
|
1339
1349
|
if (shouldBlock) {
|
|
1340
1350
|
await route.abort();
|
|
1351
|
+
handled = true;
|
|
1341
1352
|
return;
|
|
1342
1353
|
}
|
|
1343
1354
|
if (hasDirectDomains && directDomains.some((domain) => url.includes(domain))) {
|
|
@@ -1358,18 +1369,13 @@ var Interception = {
|
|
|
1358
1369
|
// 强制获取 Buffer
|
|
1359
1370
|
// 模拟浏览器 TLS 指纹
|
|
1360
1371
|
headerGeneratorOptions: Stealth.getTlsFingerprintOptions(userAgent),
|
|
1361
|
-
//
|
|
1362
|
-
// 每一个拦截请求都是独立的,使用 keepAlive 会导致 Agent 池耗尽,
|
|
1363
|
-
// 从而导致后续请求一直 Pending。
|
|
1372
|
+
// 使用共享的 Agent 单例(keepAlive: false,不会池化连接)
|
|
1364
1373
|
agent: {
|
|
1365
|
-
http:
|
|
1366
|
-
https:
|
|
1374
|
+
http: SHARED_HTTP_AGENT,
|
|
1375
|
+
https: SHARED_HTTPS_AGENT
|
|
1367
1376
|
},
|
|
1368
|
-
//
|
|
1369
|
-
timeout: {
|
|
1370
|
-
request: 12 * 1e3
|
|
1371
|
-
// 12秒超时
|
|
1372
|
-
}
|
|
1377
|
+
// 超时时间
|
|
1378
|
+
timeout: { request: DirectConfig.directTimeout * 1e3 }
|
|
1373
1379
|
});
|
|
1374
1380
|
const resHeaders = {};
|
|
1375
1381
|
for (const [key, value] of Object.entries(response.headers)) {
|
|
@@ -1384,32 +1390,37 @@ var Interception = {
|
|
|
1384
1390
|
delete resHeaders["transfer-encoding"];
|
|
1385
1391
|
delete resHeaders["connection"];
|
|
1386
1392
|
delete resHeaders["keep-alive"];
|
|
1387
|
-
logger9.info(`\u76F4\u8FDE\u6210\u529F: ${url}`);
|
|
1393
|
+
isSilent ? logger9.debug(`\u76F4\u8FDE\u6210\u529F: ${url}`) : logger9.info(`\u76F4\u8FDE\u6210\u529F: ${url}`);
|
|
1388
1394
|
await safeFulfill(route, {
|
|
1389
1395
|
status: response.statusCode,
|
|
1390
1396
|
headers: resHeaders,
|
|
1391
1397
|
body: response.body
|
|
1392
1398
|
});
|
|
1399
|
+
handled = true;
|
|
1393
1400
|
return;
|
|
1394
1401
|
} catch (e) {
|
|
1402
|
+
const isTimeout = e.code === "ETIMEDOUT" || e.message.toLowerCase().includes("timeout");
|
|
1403
|
+
const action = fallbackToProxy ? "\u56DE\u9000\u4EE3\u7406" : "\u5DF2\u653E\u5F03";
|
|
1404
|
+
const reason = isTimeout ? `\u8D85\u65F6(${DirectConfig.directTimeout}s)` : `\u5F02\u5E38: ${e.message}`;
|
|
1405
|
+
logger9.warn(`\u76F4\u8FDE${reason}\uFF0C${action}: ${url}`);
|
|
1395
1406
|
if (fallbackToProxy) {
|
|
1396
|
-
logger9.warn(`\u76F4\u8FDE\u5F02\u5E38\uFF0C\u56DE\u9000\u4EE3\u7406: ${url} | Err: ${e.message}`);
|
|
1397
1407
|
await safeContinue(route);
|
|
1398
|
-
return;
|
|
1399
1408
|
} else {
|
|
1400
|
-
logger9.warn(`\u76F4\u8FDE\u5931\u8D25: ${url} | Err: ${e.message}`);
|
|
1401
1409
|
await route.abort();
|
|
1402
|
-
return;
|
|
1403
1410
|
}
|
|
1411
|
+
handled = true;
|
|
1412
|
+
return;
|
|
1404
1413
|
}
|
|
1405
1414
|
}
|
|
1406
1415
|
await safeContinue(route);
|
|
1416
|
+
handled = true;
|
|
1407
1417
|
} catch (err) {
|
|
1408
|
-
|
|
1409
|
-
|
|
1418
|
+
logger9.warn(`\u8DEF\u7531\u5904\u7406\u5F02\u5E38: ${err.message}`);
|
|
1419
|
+
if (!handled) {
|
|
1420
|
+
try {
|
|
1410
1421
|
await route.continue();
|
|
1422
|
+
} catch (_) {
|
|
1411
1423
|
}
|
|
1412
|
-
} catch (_) {
|
|
1413
1424
|
}
|
|
1414
1425
|
}
|
|
1415
1426
|
});
|