@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.js CHANGED
@@ -17,7 +17,9 @@ var Code = {
17
17
  UnknownError: -1,
18
18
  NotLogin: 30000001,
19
19
  Chaptcha: 30000002,
20
- Timeout: 30000003
20
+ Timeout: 30000003,
21
+ InitialTimeout: 30000004,
22
+ OverallTimeout: 30000005
21
23
  };
22
24
  var Status = {
23
25
  Success: "SUCCESS",
@@ -85,10 +87,7 @@ function createLogger(moduleName) {
85
87
  // src/errors.js
86
88
  var errors_exports = {};
87
89
  __export(errors_exports, {
88
- CrawlerError: () => CrawlerError,
89
- InitialTimeoutError: () => InitialTimeoutError,
90
- OverallTimeoutError: () => OverallTimeoutError,
91
- TimeoutError: () => TimeoutError
90
+ CrawlerError: () => CrawlerError
92
91
  });
93
92
  import { serializeError } from "serialize-error";
94
93
  var CrawlerError = class _CrawlerError extends Error {
@@ -141,51 +140,6 @@ var CrawlerError = class _CrawlerError extends Error {
141
140
  return crawlerError;
142
141
  }
143
142
  };
144
- var TimeoutError = class _TimeoutError extends CrawlerError {
145
- /**
146
- * @param {string} message - 错误信息
147
- * @param {number} timeout - 超时时间(毫秒)
148
- * @param {Object} [context={}] - 上下文信息
149
- */
150
- constructor(message, timeout, context = {}) {
151
- super({
152
- message,
153
- code: Code.Timeout,
154
- context: { ...context, timeout }
155
- });
156
- this.name = "TimeoutError";
157
- this.timeout = timeout;
158
- if (Error.captureStackTrace) {
159
- Error.captureStackTrace(this, _TimeoutError);
160
- }
161
- }
162
- };
163
- var InitialTimeoutError = class _InitialTimeoutError extends TimeoutError {
164
- /**
165
- * @param {number} timeout - 超时时间(毫秒)
166
- * @param {Object} [context={}] - 上下文信息
167
- */
168
- constructor(timeout, context = {}) {
169
- super(`\u521D\u59CB\u6570\u636E\u63A5\u6536\u8D85\u65F6 (${timeout}ms)`, timeout, context);
170
- this.name = "InitialTimeoutError";
171
- if (Error.captureStackTrace) {
172
- Error.captureStackTrace(this, _InitialTimeoutError);
173
- }
174
- }
175
- };
176
- var OverallTimeoutError = class _OverallTimeoutError extends TimeoutError {
177
- /**
178
- * @param {number} timeout - 超时时间(毫秒)
179
- * @param {Object} [context={}] - 上下文信息
180
- */
181
- constructor(timeout, context = {}) {
182
- super(`\u6574\u4F53\u8BF7\u6C42\u8D85\u65F6 (${timeout}ms)`, timeout, context);
183
- this.name = "OverallTimeoutError";
184
- if (Error.captureStackTrace) {
185
- Error.captureStackTrace(this, _OverallTimeoutError);
186
- }
187
- }
188
- };
189
143
 
190
144
  // src/apify-kit.js
191
145
  import { serializeError as serializeError2 } from "serialize-error";
@@ -1057,7 +1011,7 @@ var Sse = {
1057
1011
  * @param {object} options
1058
1012
  * @param {function(string, function, string): void} [options.onData] - (textChunk, resolve, accumulatedText) => void
1059
1013
  * @param {function(string, function): void} [options.onEnd] - (fullText, resolve) => void
1060
- * @param {function(Error, function): void} [options.onTimeout] - (error, reject) => void,error 为 InitialTimeoutError 或 OverallTimeoutError
1014
+ * @param {function(Error, function): void} [options.onTimeout] - (error, reject) => void
1061
1015
  * @param {number} [options.initialTimeout=90000] - 初始数据接收超时 (ms),默认 90s
1062
1016
  * @param {number} [options.timeout=180000] - 整体请求超时时间 (ms),默认 180s
1063
1017
  * @returns {Promise<any>} - 返回 Promise,当流满足条件时 resolve
@@ -1079,93 +1033,91 @@ var Sse = {
1079
1033
  initialTimer = null;
1080
1034
  overallTimer = null;
1081
1035
  };
1082
- const workPromise = new Promise(async (resolve, reject) => {
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 reqOptions = {
1094
- hostname: urlObj.hostname,
1095
- port: 443,
1096
- path: urlObj.pathname + urlObj.search,
1097
- method: request.method(),
1098
- headers,
1099
- timeout: overallTimeout
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");
1036
+ const workPromise = new Promise((resolve, reject) => {
1037
+ page.route(urlPattern, async (route) => {
1038
+ const request = route.request();
1039
+ logger8.info(`[MITM] \u5DF2\u62E6\u622A\u8BF7\u6C42: ${request.url()}`);
1040
+ try {
1041
+ const headers = await request.allHeaders();
1042
+ const postData = request.postData();
1043
+ const urlObj = new URL(request.url());
1044
+ delete headers["accept-encoding"];
1045
+ delete headers["content-length"];
1046
+ const reqOptions = {
1047
+ hostname: urlObj.hostname,
1048
+ port: 443,
1049
+ path: urlObj.pathname + urlObj.search,
1050
+ method: request.method(),
1051
+ headers,
1052
+ timeout: overallTimeout
1053
+ };
1054
+ const req = https.request(reqOptions, (res) => {
1055
+ const chunks = [];
1056
+ let accumulatedText = "";
1057
+ res.on("data", (chunk) => {
1058
+ if (!hasReceivedInitialData) {
1059
+ hasReceivedInitialData = true;
1060
+ if (initialTimer) {
1061
+ clearTimeout(initialTimer);
1062
+ initialTimer = null;
1112
1063
  }
1113
- chunks.push(chunk);
1114
- const textChunk = chunk.toString("utf-8");
1115
- accumulatedText += textChunk;
1116
- if (onData) {
1117
- try {
1118
- onData(textChunk, resolve, accumulatedText);
1119
- } catch (e) {
1120
- logger8.fail(`onData \u9519\u8BEF`, e);
1121
- }
1064
+ logger8.debug("[Intercept] \u5DF2\u63A5\u6536\u521D\u59CB\u6570\u636E");
1065
+ }
1066
+ chunks.push(chunk);
1067
+ const textChunk = chunk.toString("utf-8");
1068
+ accumulatedText += textChunk;
1069
+ if (onData) {
1070
+ try {
1071
+ onData(textChunk, resolve, accumulatedText);
1072
+ } catch (e) {
1073
+ logger8.fail(`onData \u9519\u8BEF`, e);
1122
1074
  }
1123
- });
1124
- res.on("end", () => {
1125
- logger8.info("[MITM] \u4E0A\u6E38\u54CD\u5E94\u7ED3\u675F");
1126
- clearAllTimers();
1127
- if (onEnd) {
1128
- try {
1129
- onEnd(accumulatedText, resolve);
1130
- } catch (e) {
1131
- logger8.fail(`onEnd \u9519\u8BEF`, e);
1132
- }
1133
- } else if (!onData) {
1134
- resolve(accumulatedText);
1135
- }
1136
- route.fulfill({
1137
- status: res.statusCode,
1138
- headers: res.headers,
1139
- body: Buffer.concat(chunks)
1140
- }).catch(() => {
1141
- });
1142
- });
1075
+ }
1143
1076
  });
1144
- req.on("error", (e) => {
1077
+ res.on("end", () => {
1078
+ logger8.info("[MITM] \u4E0A\u6E38\u54CD\u5E94\u7ED3\u675F");
1145
1079
  clearAllTimers();
1146
- route.abort().catch(() => {
1080
+ if (onEnd) {
1081
+ try {
1082
+ onEnd(accumulatedText, resolve);
1083
+ } catch (e) {
1084
+ logger8.fail(`onEnd \u9519\u8BEF`, e);
1085
+ }
1086
+ } else if (!onData) {
1087
+ resolve(accumulatedText);
1088
+ }
1089
+ route.fulfill({
1090
+ status: res.statusCode,
1091
+ headers: res.headers,
1092
+ body: Buffer.concat(chunks)
1093
+ }).catch(() => {
1147
1094
  });
1148
- reject(e);
1149
1095
  });
1150
- if (postData) req.write(postData);
1151
- req.end();
1152
- } catch (e) {
1096
+ });
1097
+ req.on("error", (e) => {
1153
1098
  clearAllTimers();
1154
- route.continue().catch(() => {
1099
+ route.abort().catch(() => {
1155
1100
  });
1156
1101
  reject(e);
1157
- }
1158
- });
1159
- } catch (e) {
1160
- reject(e);
1161
- }
1162
- });
1163
- workPromise.catch(() => {
1102
+ });
1103
+ if (postData) req.write(postData);
1104
+ req.end();
1105
+ } catch (e) {
1106
+ clearAllTimers();
1107
+ route.continue().catch(() => {
1108
+ });
1109
+ reject(e);
1110
+ }
1111
+ }).catch(reject);
1164
1112
  });
1165
1113
  const timeoutPromise = new Promise((_, reject) => {
1166
1114
  initialTimer = setTimeout(() => {
1167
1115
  if (!hasReceivedInitialData) {
1168
- const error = new InitialTimeoutError(initialTimeout);
1116
+ const error = new CrawlerError({
1117
+ message: `\u521D\u59CB\u6570\u636E\u63A5\u6536\u8D85\u65F6 (${initialTimeout}ms)`,
1118
+ code: Code.InitialTimeout,
1119
+ context: { timeout: initialTimeout }
1120
+ });
1169
1121
  clearAllTimers();
1170
1122
  if (onTimeout) {
1171
1123
  try {
@@ -1179,7 +1131,11 @@ var Sse = {
1179
1131
  }
1180
1132
  }, initialTimeout);
1181
1133
  overallTimer = setTimeout(() => {
1182
- const error = new OverallTimeoutError(overallTimeout);
1134
+ const error = new CrawlerError({
1135
+ message: `\u6574\u4F53\u8BF7\u6C42\u8D85\u65F6 (${overallTimeout}ms)`,
1136
+ code: Code.OverallTimeout,
1137
+ context: { timeout: overallTimeout }
1138
+ });
1183
1139
  clearAllTimers();
1184
1140
  if (onTimeout) {
1185
1141
  try {
@@ -1192,11 +1148,12 @@ var Sse = {
1192
1148
  }
1193
1149
  }, overallTimeout);
1194
1150
  });
1151
+ workPromise.catch(() => {
1152
+ });
1195
1153
  timeoutPromise.catch(() => {
1196
1154
  });
1197
1155
  const racePromise = Promise.race([workPromise, timeoutPromise]);
1198
- racePromise.catch((e) => {
1199
- logger8.debug(`[Intercept] Race promise rejected (handled): ${e.message}`);
1156
+ racePromise.catch(() => {
1200
1157
  });
1201
1158
  return racePromise;
1202
1159
  }