@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.cjs
CHANGED
|
@@ -115,7 +115,7 @@ function createLogger(moduleName) {
|
|
|
115
115
|
var errors_exports = {};
|
|
116
116
|
__export(errors_exports, {
|
|
117
117
|
CrawlerError: () => CrawlerError,
|
|
118
|
-
|
|
118
|
+
InitialTimeoutError: () => InitialTimeoutError,
|
|
119
119
|
OverallTimeoutError: () => OverallTimeoutError,
|
|
120
120
|
TimeoutError: () => TimeoutError
|
|
121
121
|
});
|
|
@@ -189,16 +189,16 @@ var TimeoutError = class _TimeoutError extends CrawlerError {
|
|
|
189
189
|
}
|
|
190
190
|
}
|
|
191
191
|
};
|
|
192
|
-
var
|
|
192
|
+
var InitialTimeoutError = class _InitialTimeoutError extends TimeoutError {
|
|
193
193
|
/**
|
|
194
194
|
* @param {number} timeout - 超时时间(毫秒)
|
|
195
195
|
* @param {Object} [context={}] - 上下文信息
|
|
196
196
|
*/
|
|
197
197
|
constructor(timeout, context = {}) {
|
|
198
|
-
super(`\
|
|
199
|
-
this.name = "
|
|
198
|
+
super(`\u521D\u59CB\u6570\u636E\u63A5\u6536\u8D85\u65F6 (${timeout}ms)`, timeout, context);
|
|
199
|
+
this.name = "InitialTimeoutError";
|
|
200
200
|
if (Error.captureStackTrace) {
|
|
201
|
-
Error.captureStackTrace(this,
|
|
201
|
+
Error.captureStackTrace(this, _InitialTimeoutError);
|
|
202
202
|
}
|
|
203
203
|
}
|
|
204
204
|
};
|
|
@@ -1086,8 +1086,8 @@ var Sse = {
|
|
|
1086
1086
|
* @param {object} options
|
|
1087
1087
|
* @param {function(string, function, string): void} [options.onData] - (textChunk, resolve, accumulatedText) => void
|
|
1088
1088
|
* @param {function(string, function): void} [options.onEnd] - (fullText, resolve) => void
|
|
1089
|
-
* @param {function(Error, function): void} [options.onTimeout] - (error, reject) => void,error 为
|
|
1090
|
-
* @param {number} [options.
|
|
1089
|
+
* @param {function(Error, function): void} [options.onTimeout] - (error, reject) => void,error 为 InitialTimeoutError 或 OverallTimeoutError
|
|
1090
|
+
* @param {number} [options.initialTimeout=90000] - 初始数据接收超时 (ms),默认 90s
|
|
1091
1091
|
* @param {number} [options.timeout=180000] - 整体请求超时时间 (ms),默认 180s
|
|
1092
1092
|
* @returns {Promise<any>} - 返回 Promise,当流满足条件时 resolve
|
|
1093
1093
|
*/
|
|
@@ -1096,140 +1096,124 @@ var Sse = {
|
|
|
1096
1096
|
onData,
|
|
1097
1097
|
onEnd,
|
|
1098
1098
|
onTimeout,
|
|
1099
|
-
|
|
1099
|
+
initialTimeout = 9e4,
|
|
1100
1100
|
timeout = 18e4
|
|
1101
1101
|
} = options;
|
|
1102
|
-
let
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
streamReject = reject;
|
|
1106
|
-
});
|
|
1107
|
-
capturePromise.catch(() => {
|
|
1108
|
-
});
|
|
1109
|
-
let firstDataTimer = null;
|
|
1110
|
-
overallTimer = null;
|
|
1111
|
-
let hasReceivedFirstData = false;
|
|
1102
|
+
let initialTimer = null;
|
|
1103
|
+
let overallTimer = null;
|
|
1104
|
+
let hasReceivedInitialData = false;
|
|
1112
1105
|
const clearAllTimers = () => {
|
|
1113
|
-
if (
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
if (overallTimer) {
|
|
1118
|
-
clearTimeout(overallTimer);
|
|
1119
|
-
overallTimer = null;
|
|
1120
|
-
}
|
|
1106
|
+
if (initialTimer) clearTimeout(initialTimer);
|
|
1107
|
+
if (overallTimer) clearTimeout(overallTimer);
|
|
1108
|
+
initialTimer = null;
|
|
1109
|
+
overallTimer = null;
|
|
1121
1110
|
};
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1111
|
+
const workPromise = new Promise(async (resolve, reject) => {
|
|
1112
|
+
await page.route(urlPattern, async (route) => {
|
|
1113
|
+
const request = route.request();
|
|
1114
|
+
const requestUrl = request.url();
|
|
1115
|
+
logger8.info(`[MITM] \u5DF2\u62E6\u622A\u8BF7\u6C42: ${requestUrl}`);
|
|
1116
|
+
try {
|
|
1117
|
+
const headers = await request.allHeaders();
|
|
1118
|
+
const postData = request.postData();
|
|
1119
|
+
const urlObj = new import_url.URL(requestUrl);
|
|
1120
|
+
delete headers["accept-encoding"];
|
|
1121
|
+
delete headers["content-length"];
|
|
1122
|
+
const reqOptions = {
|
|
1123
|
+
hostname: urlObj.hostname,
|
|
1124
|
+
port: 443,
|
|
1125
|
+
path: urlObj.pathname + urlObj.search,
|
|
1126
|
+
method: request.method(),
|
|
1127
|
+
headers,
|
|
1128
|
+
timeout
|
|
1129
|
+
};
|
|
1130
|
+
const req = import_https.default.request(reqOptions, (res) => {
|
|
1131
|
+
const chunks = [];
|
|
1132
|
+
let accumulatedText = "";
|
|
1133
|
+
res.on("data", (chunk) => {
|
|
1134
|
+
if (!hasReceivedInitialData) {
|
|
1135
|
+
hasReceivedInitialData = true;
|
|
1136
|
+
if (initialTimer) {
|
|
1137
|
+
clearTimeout(initialTimer);
|
|
1138
|
+
initialTimer = null;
|
|
1139
|
+
}
|
|
1140
|
+
logger8.debug("[Intercept] \u5DF2\u63A5\u6536\u521D\u59CB\u6570\u636E");
|
|
1141
|
+
}
|
|
1142
|
+
chunks.push(chunk);
|
|
1143
|
+
const textChunk = chunk.toString("utf-8");
|
|
1144
|
+
accumulatedText += textChunk;
|
|
1145
|
+
logger8.debug(`[CHUNK] ${textChunk.length} bytes`);
|
|
1146
|
+
if (onData) {
|
|
1147
|
+
try {
|
|
1148
|
+
onData(textChunk, resolve, accumulatedText);
|
|
1149
|
+
} catch (e) {
|
|
1150
|
+
logger8.fail(`onData callback error: ${e.message}`, e);
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
});
|
|
1154
|
+
res.on("end", () => {
|
|
1155
|
+
logger8.info("[MITM] \u4E0A\u6E38\u54CD\u5E94\u7ED3\u675F (Stream End)");
|
|
1156
|
+
clearAllTimers();
|
|
1157
|
+
const fullBody = Buffer.concat(chunks);
|
|
1158
|
+
if (onEnd) {
|
|
1159
|
+
try {
|
|
1160
|
+
onEnd(accumulatedText, resolve);
|
|
1161
|
+
} catch (e) {
|
|
1162
|
+
logger8.fail(`onEnd callback error: ${e.message}`, e);
|
|
1163
|
+
}
|
|
1164
|
+
} else if (!onData) {
|
|
1165
|
+
resolve(accumulatedText);
|
|
1166
|
+
}
|
|
1167
|
+
route.fulfill({
|
|
1168
|
+
status: res.statusCode,
|
|
1169
|
+
headers: res.headers,
|
|
1170
|
+
body: fullBody
|
|
1171
|
+
}).catch((e) => logger8.warn(`Route fulfill failed: ${e.message}`));
|
|
1172
|
+
});
|
|
1173
|
+
});
|
|
1174
|
+
req.on("error", (e) => {
|
|
1175
|
+
logger8.fail(`[MITM] Upstream request error: ${e.message}`, e);
|
|
1176
|
+
clearAllTimers();
|
|
1177
|
+
route.abort();
|
|
1178
|
+
reject(e);
|
|
1179
|
+
});
|
|
1180
|
+
if (postData) req.write(postData);
|
|
1181
|
+
req.end();
|
|
1182
|
+
} catch (e) {
|
|
1183
|
+
logger8.fail(`[MITM] Setup error: ${e.message}`, e);
|
|
1184
|
+
clearAllTimers();
|
|
1185
|
+
route.continue();
|
|
1186
|
+
}
|
|
1187
|
+
});
|
|
1188
|
+
});
|
|
1189
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
1190
|
+
initialTimer = setTimeout(() => {
|
|
1191
|
+
if (!hasReceivedInitialData) {
|
|
1192
|
+
const error = new InitialTimeoutError(initialTimeout);
|
|
1126
1193
|
clearAllTimers();
|
|
1127
1194
|
if (onTimeout) {
|
|
1128
|
-
onTimeout(error,
|
|
1195
|
+
onTimeout(error, reject);
|
|
1129
1196
|
} else {
|
|
1130
|
-
logger8.fail(`[Intercept] \
|
|
1131
|
-
|
|
1197
|
+
logger8.fail(`[Intercept] \u521D\u59CB\u6570\u636E\u63A5\u6536\u8D85\u65F6`, error);
|
|
1198
|
+
reject(error);
|
|
1132
1199
|
}
|
|
1133
1200
|
}
|
|
1134
|
-
}
|
|
1135
|
-
|
|
1136
|
-
streamReject(e);
|
|
1137
|
-
}
|
|
1138
|
-
}, firstDataTimeout);
|
|
1139
|
-
overallTimer = setTimeout(() => {
|
|
1140
|
-
try {
|
|
1201
|
+
}, initialTimeout);
|
|
1202
|
+
overallTimer = setTimeout(() => {
|
|
1141
1203
|
const error = new OverallTimeoutError(timeout);
|
|
1142
1204
|
clearAllTimers();
|
|
1143
1205
|
if (onTimeout) {
|
|
1144
|
-
onTimeout(error,
|
|
1206
|
+
onTimeout(error, reject);
|
|
1145
1207
|
} else {
|
|
1146
1208
|
logger8.fail(`[Intercept] \u6574\u4F53\u8BF7\u6C42\u8D85\u65F6`, error);
|
|
1147
|
-
|
|
1148
|
-
}
|
|
1149
|
-
} catch (e) {
|
|
1150
|
-
logger8.fail(`[Intercept] \u6574\u4F53\u8D85\u65F6\u5904\u7406\u5F02\u5E38`, e);
|
|
1151
|
-
streamReject(e);
|
|
1152
|
-
}
|
|
1153
|
-
}, timeout);
|
|
1154
|
-
await page.route(urlPattern, async (route) => {
|
|
1155
|
-
const request = route.request();
|
|
1156
|
-
const requestUrl = request.url();
|
|
1157
|
-
logger8.info(`[MITM] \u5DF2\u62E6\u622A\u8BF7\u6C42: ${requestUrl}`);
|
|
1158
|
-
try {
|
|
1159
|
-
const headers = await request.allHeaders();
|
|
1160
|
-
const postData = request.postData();
|
|
1161
|
-
const urlObj = new import_url.URL(requestUrl);
|
|
1162
|
-
delete headers["accept-encoding"];
|
|
1163
|
-
delete headers["content-length"];
|
|
1164
|
-
const reqOptions = {
|
|
1165
|
-
hostname: urlObj.hostname,
|
|
1166
|
-
port: 443,
|
|
1167
|
-
path: urlObj.pathname + urlObj.search,
|
|
1168
|
-
method: request.method(),
|
|
1169
|
-
headers,
|
|
1170
|
-
timeout
|
|
1171
|
-
};
|
|
1172
|
-
const req = import_https.default.request(reqOptions, (res) => {
|
|
1173
|
-
const chunks = [];
|
|
1174
|
-
let accumulatedText = "";
|
|
1175
|
-
res.on("data", (chunk) => {
|
|
1176
|
-
if (!hasReceivedFirstData) {
|
|
1177
|
-
hasReceivedFirstData = true;
|
|
1178
|
-
if (firstDataTimer) {
|
|
1179
|
-
clearTimeout(firstDataTimer);
|
|
1180
|
-
firstDataTimer = null;
|
|
1181
|
-
}
|
|
1182
|
-
logger8.debug("[Intercept] \u5DF2\u63A5\u6536\u9996\u6B21\u6570\u636E");
|
|
1183
|
-
}
|
|
1184
|
-
chunks.push(chunk);
|
|
1185
|
-
const textChunk = chunk.toString("utf-8");
|
|
1186
|
-
accumulatedText += textChunk;
|
|
1187
|
-
logger8.debug(`[CHUNK] ${textChunk.length} bytes`);
|
|
1188
|
-
if (onData) {
|
|
1189
|
-
try {
|
|
1190
|
-
onData(textChunk, streamResolve, accumulatedText);
|
|
1191
|
-
} catch (e) {
|
|
1192
|
-
logger8.fail(`onData callback error: ${e.message}`, e);
|
|
1193
|
-
}
|
|
1194
|
-
}
|
|
1195
|
-
});
|
|
1196
|
-
res.on("end", () => {
|
|
1197
|
-
logger8.info("[MITM] \u4E0A\u6E38\u54CD\u5E94\u7ED3\u675F (Stream End)");
|
|
1198
|
-
clearAllTimers();
|
|
1199
|
-
const fullBody = Buffer.concat(chunks);
|
|
1200
|
-
if (onEnd) {
|
|
1201
|
-
try {
|
|
1202
|
-
onEnd(accumulatedText, streamResolve);
|
|
1203
|
-
} catch (e) {
|
|
1204
|
-
logger8.fail(`onEnd callback error: ${e.message}`, e);
|
|
1205
|
-
}
|
|
1206
|
-
} else if (!onData) {
|
|
1207
|
-
streamResolve(accumulatedText);
|
|
1208
|
-
}
|
|
1209
|
-
route.fulfill({
|
|
1210
|
-
status: res.statusCode,
|
|
1211
|
-
headers: res.headers,
|
|
1212
|
-
body: fullBody
|
|
1213
|
-
}).catch((e) => logger8.warn(`Route fulfill failed: ${e.message}`));
|
|
1214
|
-
});
|
|
1215
|
-
});
|
|
1216
|
-
req.on("error", (e) => {
|
|
1217
|
-
logger8.fail(`[MITM] Upstream request error: ${e.message}`, e);
|
|
1218
|
-
clearAllTimers();
|
|
1219
|
-
route.abort();
|
|
1220
|
-
streamReject(e);
|
|
1221
|
-
});
|
|
1222
|
-
if (postData) {
|
|
1223
|
-
req.write(postData);
|
|
1209
|
+
reject(error);
|
|
1224
1210
|
}
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
route.continue();
|
|
1230
|
-
}
|
|
1211
|
+
}, timeout);
|
|
1212
|
+
});
|
|
1213
|
+
const racePromise = Promise.race([workPromise, timeoutPromise]);
|
|
1214
|
+
racePromise.catch(() => {
|
|
1231
1215
|
});
|
|
1232
|
-
return
|
|
1216
|
+
return racePromise;
|
|
1233
1217
|
}
|
|
1234
1218
|
};
|
|
1235
1219
|
|