@skrillex1224/playwright-toolkit 2.0.79 → 2.0.81

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
@@ -46,7 +46,9 @@ var Code = {
46
46
  UnknownError: -1,
47
47
  NotLogin: 30000001,
48
48
  Chaptcha: 30000002,
49
- Timeout: 30000003
49
+ Timeout: 30000003,
50
+ InitialTimeout: 30000004,
51
+ OverallTimeout: 30000005
50
52
  };
51
53
  var Status = {
52
54
  Success: "SUCCESS",
@@ -114,10 +116,7 @@ function createLogger(moduleName) {
114
116
  // src/errors.js
115
117
  var errors_exports = {};
116
118
  __export(errors_exports, {
117
- CrawlerError: () => CrawlerError,
118
- InitialTimeoutError: () => InitialTimeoutError,
119
- OverallTimeoutError: () => OverallTimeoutError,
120
- TimeoutError: () => TimeoutError
119
+ CrawlerError: () => CrawlerError
121
120
  });
122
121
  var import_serialize_error = require("serialize-error");
123
122
  var CrawlerError = class _CrawlerError extends Error {
@@ -170,51 +169,6 @@ var CrawlerError = class _CrawlerError extends Error {
170
169
  return crawlerError;
171
170
  }
172
171
  };
173
- var TimeoutError = class _TimeoutError extends CrawlerError {
174
- /**
175
- * @param {string} message - 错误信息
176
- * @param {number} timeout - 超时时间(毫秒)
177
- * @param {Object} [context={}] - 上下文信息
178
- */
179
- constructor(message, timeout, context = {}) {
180
- super({
181
- message,
182
- code: Code.Timeout,
183
- context: { ...context, timeout }
184
- });
185
- this.name = "TimeoutError";
186
- this.timeout = timeout;
187
- if (Error.captureStackTrace) {
188
- Error.captureStackTrace(this, _TimeoutError);
189
- }
190
- }
191
- };
192
- var InitialTimeoutError = class _InitialTimeoutError extends TimeoutError {
193
- /**
194
- * @param {number} timeout - 超时时间(毫秒)
195
- * @param {Object} [context={}] - 上下文信息
196
- */
197
- constructor(timeout, context = {}) {
198
- super(`\u521D\u59CB\u6570\u636E\u63A5\u6536\u8D85\u65F6 (${timeout}ms)`, timeout, context);
199
- this.name = "InitialTimeoutError";
200
- if (Error.captureStackTrace) {
201
- Error.captureStackTrace(this, _InitialTimeoutError);
202
- }
203
- }
204
- };
205
- var OverallTimeoutError = class _OverallTimeoutError extends TimeoutError {
206
- /**
207
- * @param {number} timeout - 超时时间(毫秒)
208
- * @param {Object} [context={}] - 上下文信息
209
- */
210
- constructor(timeout, context = {}) {
211
- super(`\u6574\u4F53\u8BF7\u6C42\u8D85\u65F6 (${timeout}ms)`, timeout, context);
212
- this.name = "OverallTimeoutError";
213
- if (Error.captureStackTrace) {
214
- Error.captureStackTrace(this, _OverallTimeoutError);
215
- }
216
- }
217
- };
218
172
 
219
173
  // src/apify-kit.js
220
174
  var import_serialize_error2 = require("serialize-error");
@@ -1086,7 +1040,7 @@ var Sse = {
1086
1040
  * @param {object} options
1087
1041
  * @param {function(string, function, string): void} [options.onData] - (textChunk, resolve, accumulatedText) => void
1088
1042
  * @param {function(string, function): void} [options.onEnd] - (fullText, resolve) => void
1089
- * @param {function(Error, function): void} [options.onTimeout] - (error, reject) => void,error 为 InitialTimeoutError 或 OverallTimeoutError
1043
+ * @param {function(Error, function): void} [options.onTimeout] - (error, reject) => void
1090
1044
  * @param {number} [options.initialTimeout=90000] - 初始数据接收超时 (ms),默认 90s
1091
1045
  * @param {number} [options.timeout=180000] - 整体请求超时时间 (ms),默认 180s
1092
1046
  * @returns {Promise<any>} - 返回 Promise,当流满足条件时 resolve
@@ -1108,93 +1062,91 @@ var Sse = {
1108
1062
  initialTimer = null;
1109
1063
  overallTimer = null;
1110
1064
  };
1111
- const workPromise = new Promise(async (resolve, reject) => {
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 reqOptions = {
1123
- hostname: urlObj.hostname,
1124
- port: 443,
1125
- path: urlObj.pathname + urlObj.search,
1126
- method: request.method(),
1127
- headers,
1128
- timeout: overallTimeout
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");
1065
+ const workPromise = new Promise((resolve, reject) => {
1066
+ page.route(urlPattern, async (route) => {
1067
+ const request = route.request();
1068
+ logger8.info(`[MITM] \u5DF2\u62E6\u622A\u8BF7\u6C42: ${request.url()}`);
1069
+ try {
1070
+ const headers = await request.allHeaders();
1071
+ const postData = request.postData();
1072
+ const urlObj = new import_url.URL(request.url());
1073
+ delete headers["accept-encoding"];
1074
+ delete headers["content-length"];
1075
+ const reqOptions = {
1076
+ hostname: urlObj.hostname,
1077
+ port: 443,
1078
+ path: urlObj.pathname + urlObj.search,
1079
+ method: request.method(),
1080
+ headers,
1081
+ timeout: overallTimeout
1082
+ };
1083
+ const req = import_https.default.request(reqOptions, (res) => {
1084
+ const chunks = [];
1085
+ let accumulatedText = "";
1086
+ res.on("data", (chunk) => {
1087
+ if (!hasReceivedInitialData) {
1088
+ hasReceivedInitialData = true;
1089
+ if (initialTimer) {
1090
+ clearTimeout(initialTimer);
1091
+ initialTimer = null;
1141
1092
  }
1142
- chunks.push(chunk);
1143
- const textChunk = chunk.toString("utf-8");
1144
- accumulatedText += textChunk;
1145
- if (onData) {
1146
- try {
1147
- onData(textChunk, resolve, accumulatedText);
1148
- } catch (e) {
1149
- logger8.fail(`onData \u9519\u8BEF`, e);
1150
- }
1093
+ logger8.debug("[Intercept] \u5DF2\u63A5\u6536\u521D\u59CB\u6570\u636E");
1094
+ }
1095
+ chunks.push(chunk);
1096
+ const textChunk = chunk.toString("utf-8");
1097
+ accumulatedText += textChunk;
1098
+ if (onData) {
1099
+ try {
1100
+ onData(textChunk, resolve, accumulatedText);
1101
+ } catch (e) {
1102
+ logger8.fail(`onData \u9519\u8BEF`, e);
1151
1103
  }
1152
- });
1153
- res.on("end", () => {
1154
- logger8.info("[MITM] \u4E0A\u6E38\u54CD\u5E94\u7ED3\u675F");
1155
- clearAllTimers();
1156
- if (onEnd) {
1157
- try {
1158
- onEnd(accumulatedText, resolve);
1159
- } catch (e) {
1160
- logger8.fail(`onEnd \u9519\u8BEF`, e);
1161
- }
1162
- } else if (!onData) {
1163
- resolve(accumulatedText);
1164
- }
1165
- route.fulfill({
1166
- status: res.statusCode,
1167
- headers: res.headers,
1168
- body: Buffer.concat(chunks)
1169
- }).catch(() => {
1170
- });
1171
- });
1104
+ }
1172
1105
  });
1173
- req.on("error", (e) => {
1106
+ res.on("end", () => {
1107
+ logger8.info("[MITM] \u4E0A\u6E38\u54CD\u5E94\u7ED3\u675F");
1174
1108
  clearAllTimers();
1175
- route.abort().catch(() => {
1109
+ if (onEnd) {
1110
+ try {
1111
+ onEnd(accumulatedText, resolve);
1112
+ } catch (e) {
1113
+ logger8.fail(`onEnd \u9519\u8BEF`, e);
1114
+ }
1115
+ } else if (!onData) {
1116
+ resolve(accumulatedText);
1117
+ }
1118
+ route.fulfill({
1119
+ status: res.statusCode,
1120
+ headers: res.headers,
1121
+ body: Buffer.concat(chunks)
1122
+ }).catch(() => {
1176
1123
  });
1177
- reject(e);
1178
1124
  });
1179
- if (postData) req.write(postData);
1180
- req.end();
1181
- } catch (e) {
1125
+ });
1126
+ req.on("error", (e) => {
1182
1127
  clearAllTimers();
1183
- route.continue().catch(() => {
1128
+ route.abort().catch(() => {
1184
1129
  });
1185
1130
  reject(e);
1186
- }
1187
- });
1188
- } catch (e) {
1189
- reject(e);
1190
- }
1191
- });
1192
- workPromise.catch(() => {
1131
+ });
1132
+ if (postData) req.write(postData);
1133
+ req.end();
1134
+ } catch (e) {
1135
+ clearAllTimers();
1136
+ route.continue().catch(() => {
1137
+ });
1138
+ reject(e);
1139
+ }
1140
+ }).catch(reject);
1193
1141
  });
1194
1142
  const timeoutPromise = new Promise((_, reject) => {
1195
1143
  initialTimer = setTimeout(() => {
1196
1144
  if (!hasReceivedInitialData) {
1197
- const error = new InitialTimeoutError(initialTimeout);
1145
+ const error = new CrawlerError({
1146
+ message: `\u521D\u59CB\u6570\u636E\u63A5\u6536\u8D85\u65F6 (${initialTimeout}ms)`,
1147
+ code: Code.InitialTimeout,
1148
+ context: { timeout: initialTimeout }
1149
+ });
1198
1150
  clearAllTimers();
1199
1151
  if (onTimeout) {
1200
1152
  try {
@@ -1208,7 +1160,11 @@ var Sse = {
1208
1160
  }
1209
1161
  }, initialTimeout);
1210
1162
  overallTimer = setTimeout(() => {
1211
- const error = new OverallTimeoutError(overallTimeout);
1163
+ const error = new CrawlerError({
1164
+ message: `\u6574\u4F53\u8BF7\u6C42\u8D85\u65F6 (${overallTimeout}ms)`,
1165
+ code: Code.OverallTimeout,
1166
+ context: { timeout: overallTimeout }
1167
+ });
1212
1168
  clearAllTimers();
1213
1169
  if (onTimeout) {
1214
1170
  try {
@@ -1221,11 +1177,12 @@ var Sse = {
1221
1177
  }
1222
1178
  }, overallTimeout);
1223
1179
  });
1180
+ workPromise.catch(() => {
1181
+ });
1224
1182
  timeoutPromise.catch(() => {
1225
1183
  });
1226
1184
  const racePromise = Promise.race([workPromise, timeoutPromise]);
1227
- racePromise.catch((e) => {
1228
- logger8.debug(`[Intercept] Race promise rejected (handled): ${e.message}`);
1185
+ racePromise.catch(() => {
1229
1186
  });
1230
1187
  return racePromise;
1231
1188
  }