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