@skrillex1224/playwright-toolkit 2.0.72 → 2.0.74

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
@@ -86,7 +86,7 @@ function createLogger(moduleName) {
86
86
  var errors_exports = {};
87
87
  __export(errors_exports, {
88
88
  CrawlerError: () => CrawlerError,
89
- FirstDataTimeoutError: () => FirstDataTimeoutError,
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 FirstDataTimeoutError = class _FirstDataTimeoutError extends TimeoutError {
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(`\u9996\u6B21\u6570\u636E\u63A5\u6536\u8D85\u65F6 (${timeout}ms)`, timeout, context);
170
- this.name = "FirstDataTimeoutError";
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, _FirstDataTimeoutError);
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 为 FirstDataTimeoutError 或 OverallTimeoutError
1061
- * @param {number} [options.firstDataTimeout=90000] - 首次数据接收超时 (ms),默认 90s
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,124 +1067,129 @@ var Sse = {
1067
1067
  onData,
1068
1068
  onEnd,
1069
1069
  onTimeout,
1070
- firstDataTimeout = 9e4,
1070
+ initialTimeout = 9e4,
1071
1071
  timeout = 18e4
1072
1072
  } = options;
1073
- let firstDataTimer = null;
1073
+ let initialTimer = null;
1074
1074
  let overallTimer = null;
1075
- let hasReceivedFirstData = false;
1075
+ let hasReceivedInitialData = false;
1076
1076
  const clearAllTimers = () => {
1077
- if (firstDataTimer) clearTimeout(firstDataTimer);
1077
+ if (initialTimer) clearTimeout(initialTimer);
1078
1078
  if (overallTimer) clearTimeout(overallTimer);
1079
- firstDataTimer = null;
1079
+ initialTimer = null;
1080
1080
  overallTimer = null;
1081
1081
  };
1082
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 (!hasReceivedFirstData) {
1106
- hasReceivedFirstData = true;
1107
- if (firstDataTimer) {
1108
- clearTimeout(firstDataTimer);
1109
- firstDataTimer = null;
1083
+ try {
1084
+ await page.route(urlPattern, async (route) => {
1085
+ const request = route.request();
1086
+ logger8.info(`[MITM] \u5DF2\u62E6\u622A\u8BF7\u6C42: ${request.url()}`);
1087
+ try {
1088
+ const headers = await request.allHeaders();
1089
+ const postData = request.postData();
1090
+ const urlObj = new URL(request.url());
1091
+ delete headers["accept-encoding"];
1092
+ delete headers["content-length"];
1093
+ const req = https.request({
1094
+ hostname: urlObj.hostname,
1095
+ port: 443,
1096
+ path: urlObj.pathname + urlObj.search,
1097
+ method: request.method(),
1098
+ headers,
1099
+ timeout
1100
+ }, (res) => {
1101
+ const chunks = [];
1102
+ let accumulatedText = "";
1103
+ res.on("data", (chunk) => {
1104
+ if (!hasReceivedInitialData) {
1105
+ hasReceivedInitialData = true;
1106
+ if (initialTimer) {
1107
+ clearTimeout(initialTimer);
1108
+ initialTimer = null;
1109
+ }
1110
+ logger8.debug("[Intercept] \u5DF2\u63A5\u6536\u521D\u59CB\u6570\u636E");
1111
+ }
1112
+ chunks.push(chunk);
1113
+ const textChunk = chunk.toString("utf-8");
1114
+ accumulatedText += textChunk;
1115
+ if (onData) {
1116
+ try {
1117
+ onData(textChunk, resolve, accumulatedText);
1118
+ } catch (e) {
1119
+ logger8.fail(`onData \u9519\u8BEF`, e);
1120
+ }
1110
1121
  }
1111
- logger8.debug("[Intercept] \u5DF2\u63A5\u6536\u9996\u6B21\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
+ res.on("end", () => {
1124
+ logger8.info("[MITM] \u4E0A\u6E38\u54CD\u5E94\u7ED3\u675F");
1125
+ clearAllTimers();
1126
+ if (onEnd) {
1127
+ try {
1128
+ onEnd(accumulatedText, resolve);
1129
+ } catch (e) {
1130
+ logger8.fail(`onEnd \u9519\u8BEF`, e);
1131
+ }
1132
+ } else if (!onData) {
1133
+ resolve(accumulatedText);
1122
1134
  }
1123
- }
1135
+ route.fulfill({
1136
+ status: res.statusCode,
1137
+ headers: res.headers,
1138
+ body: Buffer.concat(chunks)
1139
+ }).catch(() => {
1140
+ });
1141
+ });
1124
1142
  });
1125
- res.on("end", () => {
1126
- logger8.info("[MITM] \u4E0A\u6E38\u54CD\u5E94\u7ED3\u675F (Stream End)");
1143
+ req.on("error", (e) => {
1127
1144
  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}`));
1145
+ route.abort().catch(() => {
1146
+ });
1147
+ reject(e);
1143
1148
  });
1144
- });
1145
- req.on("error", (e) => {
1146
- logger8.fail(`[MITM] Upstream request error: ${e.message}`, e);
1149
+ if (postData) req.write(postData);
1150
+ req.end();
1151
+ } catch (e) {
1147
1152
  clearAllTimers();
1148
- route.abort();
1153
+ route.continue().catch(() => {
1154
+ });
1149
1155
  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
- });
1156
+ }
1157
+ });
1158
+ } catch (e) {
1159
+ reject(e);
1160
+ }
1159
1161
  });
1160
1162
  const timeoutPromise = new Promise((_, reject) => {
1161
- firstDataTimer = setTimeout(() => {
1162
- if (!hasReceivedFirstData) {
1163
- const error = new FirstDataTimeoutError(firstDataTimeout);
1163
+ initialTimer = setTimeout(() => {
1164
+ if (!hasReceivedInitialData) {
1165
+ const error = new InitialTimeoutError(initialTimeout);
1164
1166
  clearAllTimers();
1165
1167
  if (onTimeout) {
1166
- onTimeout(error, reject);
1168
+ try {
1169
+ onTimeout(error, reject);
1170
+ } catch (e) {
1171
+ reject(e);
1172
+ }
1167
1173
  } else {
1168
- logger8.fail(`[Intercept] \u9996\u6B21\u6570\u636E\u63A5\u6536\u8D85\u65F6`, error);
1169
1174
  reject(error);
1170
1175
  }
1171
1176
  }
1172
- }, firstDataTimeout);
1177
+ }, initialTimeout);
1173
1178
  overallTimer = setTimeout(() => {
1174
1179
  const error = new OverallTimeoutError(timeout);
1175
1180
  clearAllTimers();
1176
1181
  if (onTimeout) {
1177
- onTimeout(error, reject);
1182
+ try {
1183
+ onTimeout(error, reject);
1184
+ } catch (e) {
1185
+ reject(e);
1186
+ }
1178
1187
  } else {
1179
- logger8.fail(`[Intercept] \u6574\u4F53\u8BF7\u6C42\u8D85\u65F6`, error);
1180
1188
  reject(error);
1181
1189
  }
1182
1190
  }, timeout);
1183
1191
  });
1184
- const racePromise = Promise.race([workPromise, timeoutPromise]);
1185
- racePromise.catch(() => {
1186
- });
1187
- return racePromise;
1192
+ return Promise.race([workPromise, timeoutPromise]);
1188
1193
  }
1189
1194
  };
1190
1195