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