@skrillex1224/playwright-toolkit 2.0.71 → 2.0.73
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 +109 -125
- package/dist/index.cjs.map +2 -2
- package/dist/index.js +109 -125
- package/dist/index.js.map +2 -2
- package/index.d.ts +5 -5
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -86,7 +86,7 @@ function createLogger(moduleName) {
|
|
|
86
86
|
var errors_exports = {};
|
|
87
87
|
__export(errors_exports, {
|
|
88
88
|
CrawlerError: () => CrawlerError,
|
|
89
|
-
|
|
89
|
+
InitialTimeoutError: () => InitialTimeoutError,
|
|
90
90
|
OverallTimeoutError: () => OverallTimeoutError,
|
|
91
91
|
TimeoutError: () => TimeoutError
|
|
92
92
|
});
|
|
@@ -160,16 +160,16 @@ var TimeoutError = class _TimeoutError extends CrawlerError {
|
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
};
|
|
163
|
-
var
|
|
163
|
+
var InitialTimeoutError = class _InitialTimeoutError extends TimeoutError {
|
|
164
164
|
/**
|
|
165
165
|
* @param {number} timeout - 超时时间(毫秒)
|
|
166
166
|
* @param {Object} [context={}] - 上下文信息
|
|
167
167
|
*/
|
|
168
168
|
constructor(timeout, context = {}) {
|
|
169
|
-
super(`\
|
|
170
|
-
this.name = "
|
|
169
|
+
super(`\u521D\u59CB\u6570\u636E\u63A5\u6536\u8D85\u65F6 (${timeout}ms)`, timeout, context);
|
|
170
|
+
this.name = "InitialTimeoutError";
|
|
171
171
|
if (Error.captureStackTrace) {
|
|
172
|
-
Error.captureStackTrace(this,
|
|
172
|
+
Error.captureStackTrace(this, _InitialTimeoutError);
|
|
173
173
|
}
|
|
174
174
|
}
|
|
175
175
|
};
|
|
@@ -1057,8 +1057,8 @@ var Sse = {
|
|
|
1057
1057
|
* @param {object} options
|
|
1058
1058
|
* @param {function(string, function, string): void} [options.onData] - (textChunk, resolve, accumulatedText) => void
|
|
1059
1059
|
* @param {function(string, function): void} [options.onEnd] - (fullText, resolve) => void
|
|
1060
|
-
* @param {function(Error, function): void} [options.onTimeout] - (error, reject) => void,error 为
|
|
1061
|
-
* @param {number} [options.
|
|
1060
|
+
* @param {function(Error, function): void} [options.onTimeout] - (error, reject) => void,error 为 InitialTimeoutError 或 OverallTimeoutError
|
|
1061
|
+
* @param {number} [options.initialTimeout=90000] - 初始数据接收超时 (ms),默认 90s
|
|
1062
1062
|
* @param {number} [options.timeout=180000] - 整体请求超时时间 (ms),默认 180s
|
|
1063
1063
|
* @returns {Promise<any>} - 返回 Promise,当流满足条件时 resolve
|
|
1064
1064
|
*/
|
|
@@ -1067,140 +1067,124 @@ var Sse = {
|
|
|
1067
1067
|
onData,
|
|
1068
1068
|
onEnd,
|
|
1069
1069
|
onTimeout,
|
|
1070
|
-
|
|
1070
|
+
initialTimeout = 9e4,
|
|
1071
1071
|
timeout = 18e4
|
|
1072
1072
|
} = options;
|
|
1073
|
-
let
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
streamReject = reject;
|
|
1077
|
-
});
|
|
1078
|
-
capturePromise.catch(() => {
|
|
1079
|
-
});
|
|
1080
|
-
let firstDataTimer = null;
|
|
1081
|
-
overallTimer = null;
|
|
1082
|
-
let hasReceivedFirstData = false;
|
|
1073
|
+
let initialTimer = null;
|
|
1074
|
+
let overallTimer = null;
|
|
1075
|
+
let hasReceivedInitialData = false;
|
|
1083
1076
|
const clearAllTimers = () => {
|
|
1084
|
-
if (
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
if (overallTimer) {
|
|
1089
|
-
clearTimeout(overallTimer);
|
|
1090
|
-
overallTimer = null;
|
|
1091
|
-
}
|
|
1077
|
+
if (initialTimer) clearTimeout(initialTimer);
|
|
1078
|
+
if (overallTimer) clearTimeout(overallTimer);
|
|
1079
|
+
initialTimer = null;
|
|
1080
|
+
overallTimer = null;
|
|
1092
1081
|
};
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1082
|
+
const workPromise = new Promise(async (resolve, reject) => {
|
|
1083
|
+
await page.route(urlPattern, async (route) => {
|
|
1084
|
+
const request = route.request();
|
|
1085
|
+
const requestUrl = request.url();
|
|
1086
|
+
logger8.info(`[MITM] \u5DF2\u62E6\u622A\u8BF7\u6C42: ${requestUrl}`);
|
|
1087
|
+
try {
|
|
1088
|
+
const headers = await request.allHeaders();
|
|
1089
|
+
const postData = request.postData();
|
|
1090
|
+
const urlObj = new URL(requestUrl);
|
|
1091
|
+
delete headers["accept-encoding"];
|
|
1092
|
+
delete headers["content-length"];
|
|
1093
|
+
const reqOptions = {
|
|
1094
|
+
hostname: urlObj.hostname,
|
|
1095
|
+
port: 443,
|
|
1096
|
+
path: urlObj.pathname + urlObj.search,
|
|
1097
|
+
method: request.method(),
|
|
1098
|
+
headers,
|
|
1099
|
+
timeout
|
|
1100
|
+
};
|
|
1101
|
+
const req = https.request(reqOptions, (res) => {
|
|
1102
|
+
const chunks = [];
|
|
1103
|
+
let accumulatedText = "";
|
|
1104
|
+
res.on("data", (chunk) => {
|
|
1105
|
+
if (!hasReceivedInitialData) {
|
|
1106
|
+
hasReceivedInitialData = true;
|
|
1107
|
+
if (initialTimer) {
|
|
1108
|
+
clearTimeout(initialTimer);
|
|
1109
|
+
initialTimer = null;
|
|
1110
|
+
}
|
|
1111
|
+
logger8.debug("[Intercept] \u5DF2\u63A5\u6536\u521D\u59CB\u6570\u636E");
|
|
1112
|
+
}
|
|
1113
|
+
chunks.push(chunk);
|
|
1114
|
+
const textChunk = chunk.toString("utf-8");
|
|
1115
|
+
accumulatedText += textChunk;
|
|
1116
|
+
logger8.debug(`[CHUNK] ${textChunk.length} bytes`);
|
|
1117
|
+
if (onData) {
|
|
1118
|
+
try {
|
|
1119
|
+
onData(textChunk, resolve, accumulatedText);
|
|
1120
|
+
} catch (e) {
|
|
1121
|
+
logger8.fail(`onData callback error: ${e.message}`, e);
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
});
|
|
1125
|
+
res.on("end", () => {
|
|
1126
|
+
logger8.info("[MITM] \u4E0A\u6E38\u54CD\u5E94\u7ED3\u675F (Stream End)");
|
|
1127
|
+
clearAllTimers();
|
|
1128
|
+
const fullBody = Buffer.concat(chunks);
|
|
1129
|
+
if (onEnd) {
|
|
1130
|
+
try {
|
|
1131
|
+
onEnd(accumulatedText, resolve);
|
|
1132
|
+
} catch (e) {
|
|
1133
|
+
logger8.fail(`onEnd callback error: ${e.message}`, e);
|
|
1134
|
+
}
|
|
1135
|
+
} else if (!onData) {
|
|
1136
|
+
resolve(accumulatedText);
|
|
1137
|
+
}
|
|
1138
|
+
route.fulfill({
|
|
1139
|
+
status: res.statusCode,
|
|
1140
|
+
headers: res.headers,
|
|
1141
|
+
body: fullBody
|
|
1142
|
+
}).catch((e) => logger8.warn(`Route fulfill failed: ${e.message}`));
|
|
1143
|
+
});
|
|
1144
|
+
});
|
|
1145
|
+
req.on("error", (e) => {
|
|
1146
|
+
logger8.fail(`[MITM] Upstream request error: ${e.message}`, e);
|
|
1147
|
+
clearAllTimers();
|
|
1148
|
+
route.abort();
|
|
1149
|
+
reject(e);
|
|
1150
|
+
});
|
|
1151
|
+
if (postData) req.write(postData);
|
|
1152
|
+
req.end();
|
|
1153
|
+
} catch (e) {
|
|
1154
|
+
logger8.fail(`[MITM] Setup error: ${e.message}`, e);
|
|
1155
|
+
clearAllTimers();
|
|
1156
|
+
route.continue();
|
|
1157
|
+
}
|
|
1158
|
+
});
|
|
1159
|
+
});
|
|
1160
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
1161
|
+
initialTimer = setTimeout(() => {
|
|
1162
|
+
if (!hasReceivedInitialData) {
|
|
1163
|
+
const error = new InitialTimeoutError(initialTimeout);
|
|
1097
1164
|
clearAllTimers();
|
|
1098
1165
|
if (onTimeout) {
|
|
1099
|
-
onTimeout(error,
|
|
1166
|
+
onTimeout(error, reject);
|
|
1100
1167
|
} else {
|
|
1101
|
-
logger8.fail(`[Intercept] \
|
|
1102
|
-
|
|
1168
|
+
logger8.fail(`[Intercept] \u521D\u59CB\u6570\u636E\u63A5\u6536\u8D85\u65F6`, error);
|
|
1169
|
+
reject(error);
|
|
1103
1170
|
}
|
|
1104
1171
|
}
|
|
1105
|
-
}
|
|
1106
|
-
|
|
1107
|
-
streamReject(e);
|
|
1108
|
-
}
|
|
1109
|
-
}, firstDataTimeout);
|
|
1110
|
-
overallTimer = setTimeout(() => {
|
|
1111
|
-
try {
|
|
1172
|
+
}, initialTimeout);
|
|
1173
|
+
overallTimer = setTimeout(() => {
|
|
1112
1174
|
const error = new OverallTimeoutError(timeout);
|
|
1113
1175
|
clearAllTimers();
|
|
1114
1176
|
if (onTimeout) {
|
|
1115
|
-
onTimeout(error,
|
|
1177
|
+
onTimeout(error, reject);
|
|
1116
1178
|
} else {
|
|
1117
1179
|
logger8.fail(`[Intercept] \u6574\u4F53\u8BF7\u6C42\u8D85\u65F6`, error);
|
|
1118
|
-
|
|
1119
|
-
}
|
|
1120
|
-
} catch (e) {
|
|
1121
|
-
logger8.fail(`[Intercept] \u6574\u4F53\u8D85\u65F6\u5904\u7406\u5F02\u5E38`, e);
|
|
1122
|
-
streamReject(e);
|
|
1123
|
-
}
|
|
1124
|
-
}, timeout);
|
|
1125
|
-
await page.route(urlPattern, async (route) => {
|
|
1126
|
-
const request = route.request();
|
|
1127
|
-
const requestUrl = request.url();
|
|
1128
|
-
logger8.info(`[MITM] \u5DF2\u62E6\u622A\u8BF7\u6C42: ${requestUrl}`);
|
|
1129
|
-
try {
|
|
1130
|
-
const headers = await request.allHeaders();
|
|
1131
|
-
const postData = request.postData();
|
|
1132
|
-
const urlObj = new URL(requestUrl);
|
|
1133
|
-
delete headers["accept-encoding"];
|
|
1134
|
-
delete headers["content-length"];
|
|
1135
|
-
const reqOptions = {
|
|
1136
|
-
hostname: urlObj.hostname,
|
|
1137
|
-
port: 443,
|
|
1138
|
-
path: urlObj.pathname + urlObj.search,
|
|
1139
|
-
method: request.method(),
|
|
1140
|
-
headers,
|
|
1141
|
-
timeout
|
|
1142
|
-
};
|
|
1143
|
-
const req = https.request(reqOptions, (res) => {
|
|
1144
|
-
const chunks = [];
|
|
1145
|
-
let accumulatedText = "";
|
|
1146
|
-
res.on("data", (chunk) => {
|
|
1147
|
-
if (!hasReceivedFirstData) {
|
|
1148
|
-
hasReceivedFirstData = true;
|
|
1149
|
-
if (firstDataTimer) {
|
|
1150
|
-
clearTimeout(firstDataTimer);
|
|
1151
|
-
firstDataTimer = null;
|
|
1152
|
-
}
|
|
1153
|
-
logger8.debug("[Intercept] \u5DF2\u63A5\u6536\u9996\u6B21\u6570\u636E");
|
|
1154
|
-
}
|
|
1155
|
-
chunks.push(chunk);
|
|
1156
|
-
const textChunk = chunk.toString("utf-8");
|
|
1157
|
-
accumulatedText += textChunk;
|
|
1158
|
-
logger8.debug(`[CHUNK] ${textChunk.length} bytes`);
|
|
1159
|
-
if (onData) {
|
|
1160
|
-
try {
|
|
1161
|
-
onData(textChunk, streamResolve, accumulatedText);
|
|
1162
|
-
} catch (e) {
|
|
1163
|
-
logger8.fail(`onData callback error: ${e.message}`, e);
|
|
1164
|
-
}
|
|
1165
|
-
}
|
|
1166
|
-
});
|
|
1167
|
-
res.on("end", () => {
|
|
1168
|
-
logger8.info("[MITM] \u4E0A\u6E38\u54CD\u5E94\u7ED3\u675F (Stream End)");
|
|
1169
|
-
clearAllTimers();
|
|
1170
|
-
const fullBody = Buffer.concat(chunks);
|
|
1171
|
-
if (onEnd) {
|
|
1172
|
-
try {
|
|
1173
|
-
onEnd(accumulatedText, streamResolve);
|
|
1174
|
-
} catch (e) {
|
|
1175
|
-
logger8.fail(`onEnd callback error: ${e.message}`, e);
|
|
1176
|
-
}
|
|
1177
|
-
} else if (!onData) {
|
|
1178
|
-
streamResolve(accumulatedText);
|
|
1179
|
-
}
|
|
1180
|
-
route.fulfill({
|
|
1181
|
-
status: res.statusCode,
|
|
1182
|
-
headers: res.headers,
|
|
1183
|
-
body: fullBody
|
|
1184
|
-
}).catch((e) => logger8.warn(`Route fulfill failed: ${e.message}`));
|
|
1185
|
-
});
|
|
1186
|
-
});
|
|
1187
|
-
req.on("error", (e) => {
|
|
1188
|
-
logger8.fail(`[MITM] Upstream request error: ${e.message}`, e);
|
|
1189
|
-
clearAllTimers();
|
|
1190
|
-
route.abort();
|
|
1191
|
-
streamReject(e);
|
|
1192
|
-
});
|
|
1193
|
-
if (postData) {
|
|
1194
|
-
req.write(postData);
|
|
1180
|
+
reject(error);
|
|
1195
1181
|
}
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
route.continue();
|
|
1201
|
-
}
|
|
1182
|
+
}, timeout);
|
|
1183
|
+
});
|
|
1184
|
+
const racePromise = Promise.race([workPromise, timeoutPromise]);
|
|
1185
|
+
racePromise.catch(() => {
|
|
1202
1186
|
});
|
|
1203
|
-
return
|
|
1187
|
+
return racePromise;
|
|
1204
1188
|
}
|
|
1205
1189
|
};
|
|
1206
1190
|
|