@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.cjs CHANGED
@@ -115,7 +115,7 @@ function createLogger(moduleName) {
115
115
  var errors_exports = {};
116
116
  __export(errors_exports, {
117
117
  CrawlerError: () => CrawlerError,
118
- FirstDataTimeoutError: () => FirstDataTimeoutError,
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 FirstDataTimeoutError = class _FirstDataTimeoutError extends TimeoutError {
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(`\u9996\u6B21\u6570\u636E\u63A5\u6536\u8D85\u65F6 (${timeout}ms)`, timeout, context);
199
- this.name = "FirstDataTimeoutError";
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, _FirstDataTimeoutError);
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 为 FirstDataTimeoutError 或 OverallTimeoutError
1090
- * @param {number} [options.firstDataTimeout=90000] - 首次数据接收超时 (ms),默认 90s
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,124 +1096,129 @@ var Sse = {
1096
1096
  onData,
1097
1097
  onEnd,
1098
1098
  onTimeout,
1099
- firstDataTimeout = 9e4,
1099
+ initialTimeout = 9e4,
1100
1100
  timeout = 18e4
1101
1101
  } = options;
1102
- let firstDataTimer = null;
1102
+ let initialTimer = null;
1103
1103
  let overallTimer = null;
1104
- let hasReceivedFirstData = false;
1104
+ let hasReceivedInitialData = false;
1105
1105
  const clearAllTimers = () => {
1106
- if (firstDataTimer) clearTimeout(firstDataTimer);
1106
+ if (initialTimer) clearTimeout(initialTimer);
1107
1107
  if (overallTimer) clearTimeout(overallTimer);
1108
- firstDataTimer = null;
1108
+ initialTimer = null;
1109
1109
  overallTimer = null;
1110
1110
  };
1111
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 (!hasReceivedFirstData) {
1135
- hasReceivedFirstData = true;
1136
- if (firstDataTimer) {
1137
- clearTimeout(firstDataTimer);
1138
- firstDataTimer = null;
1112
+ try {
1113
+ await page.route(urlPattern, async (route) => {
1114
+ const request = route.request();
1115
+ logger8.info(`[MITM] \u5DF2\u62E6\u622A\u8BF7\u6C42: ${request.url()}`);
1116
+ try {
1117
+ const headers = await request.allHeaders();
1118
+ const postData = request.postData();
1119
+ const urlObj = new import_url.URL(request.url());
1120
+ delete headers["accept-encoding"];
1121
+ delete headers["content-length"];
1122
+ const req = import_https.default.request({
1123
+ hostname: urlObj.hostname,
1124
+ port: 443,
1125
+ path: urlObj.pathname + urlObj.search,
1126
+ method: request.method(),
1127
+ headers,
1128
+ timeout
1129
+ }, (res) => {
1130
+ const chunks = [];
1131
+ let accumulatedText = "";
1132
+ res.on("data", (chunk) => {
1133
+ if (!hasReceivedInitialData) {
1134
+ hasReceivedInitialData = true;
1135
+ if (initialTimer) {
1136
+ clearTimeout(initialTimer);
1137
+ initialTimer = null;
1138
+ }
1139
+ logger8.debug("[Intercept] \u5DF2\u63A5\u6536\u521D\u59CB\u6570\u636E");
1140
+ }
1141
+ chunks.push(chunk);
1142
+ const textChunk = chunk.toString("utf-8");
1143
+ accumulatedText += textChunk;
1144
+ if (onData) {
1145
+ try {
1146
+ onData(textChunk, resolve, accumulatedText);
1147
+ } catch (e) {
1148
+ logger8.fail(`onData \u9519\u8BEF`, e);
1149
+ }
1139
1150
  }
1140
- logger8.debug("[Intercept] \u5DF2\u63A5\u6536\u9996\u6B21\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
+ res.on("end", () => {
1153
+ logger8.info("[MITM] \u4E0A\u6E38\u54CD\u5E94\u7ED3\u675F");
1154
+ clearAllTimers();
1155
+ if (onEnd) {
1156
+ try {
1157
+ onEnd(accumulatedText, resolve);
1158
+ } catch (e) {
1159
+ logger8.fail(`onEnd \u9519\u8BEF`, e);
1160
+ }
1161
+ } else if (!onData) {
1162
+ resolve(accumulatedText);
1151
1163
  }
1152
- }
1164
+ route.fulfill({
1165
+ status: res.statusCode,
1166
+ headers: res.headers,
1167
+ body: Buffer.concat(chunks)
1168
+ }).catch(() => {
1169
+ });
1170
+ });
1153
1171
  });
1154
- res.on("end", () => {
1155
- logger8.info("[MITM] \u4E0A\u6E38\u54CD\u5E94\u7ED3\u675F (Stream End)");
1172
+ req.on("error", (e) => {
1156
1173
  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}`));
1174
+ route.abort().catch(() => {
1175
+ });
1176
+ reject(e);
1172
1177
  });
1173
- });
1174
- req.on("error", (e) => {
1175
- logger8.fail(`[MITM] Upstream request error: ${e.message}`, e);
1178
+ if (postData) req.write(postData);
1179
+ req.end();
1180
+ } catch (e) {
1176
1181
  clearAllTimers();
1177
- route.abort();
1182
+ route.continue().catch(() => {
1183
+ });
1178
1184
  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
- });
1185
+ }
1186
+ });
1187
+ } catch (e) {
1188
+ reject(e);
1189
+ }
1188
1190
  });
1189
1191
  const timeoutPromise = new Promise((_, reject) => {
1190
- firstDataTimer = setTimeout(() => {
1191
- if (!hasReceivedFirstData) {
1192
- const error = new FirstDataTimeoutError(firstDataTimeout);
1192
+ initialTimer = setTimeout(() => {
1193
+ if (!hasReceivedInitialData) {
1194
+ const error = new InitialTimeoutError(initialTimeout);
1193
1195
  clearAllTimers();
1194
1196
  if (onTimeout) {
1195
- onTimeout(error, reject);
1197
+ try {
1198
+ onTimeout(error, reject);
1199
+ } catch (e) {
1200
+ reject(e);
1201
+ }
1196
1202
  } else {
1197
- logger8.fail(`[Intercept] \u9996\u6B21\u6570\u636E\u63A5\u6536\u8D85\u65F6`, error);
1198
1203
  reject(error);
1199
1204
  }
1200
1205
  }
1201
- }, firstDataTimeout);
1206
+ }, initialTimeout);
1202
1207
  overallTimer = setTimeout(() => {
1203
1208
  const error = new OverallTimeoutError(timeout);
1204
1209
  clearAllTimers();
1205
1210
  if (onTimeout) {
1206
- onTimeout(error, reject);
1211
+ try {
1212
+ onTimeout(error, reject);
1213
+ } catch (e) {
1214
+ reject(e);
1215
+ }
1207
1216
  } else {
1208
- logger8.fail(`[Intercept] \u6574\u4F53\u8BF7\u6C42\u8D85\u65F6`, error);
1209
1217
  reject(error);
1210
1218
  }
1211
1219
  }, timeout);
1212
1220
  });
1213
- const racePromise = Promise.race([workPromise, timeoutPromise]);
1214
- racePromise.catch(() => {
1215
- });
1216
- return racePromise;
1221
+ return Promise.race([workPromise, timeoutPromise]);
1217
1222
  }
1218
1223
  };
1219
1224