@skrillex1224/playwright-toolkit 2.0.70 → 2.0.72

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