whistle.pastekitlab 1.8.2 → 1.8.3
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/index.js +47 -24
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -152,13 +152,13 @@ async function forwardRequest(req, res, url) {
|
|
|
152
152
|
reqBody += chunk;
|
|
153
153
|
}
|
|
154
154
|
|
|
155
|
-
// 通过 axios 转发请求(使用
|
|
155
|
+
// 通过 axios 转发请求(使用 stream)
|
|
156
156
|
const response = await axios({
|
|
157
157
|
method: req.method.toLowerCase(),
|
|
158
158
|
url: url,
|
|
159
159
|
headers: req.headers,
|
|
160
160
|
data: reqBody || undefined,
|
|
161
|
-
responseType: '
|
|
161
|
+
responseType: 'stream',
|
|
162
162
|
validateStatus: () => true
|
|
163
163
|
});
|
|
164
164
|
|
|
@@ -168,8 +168,9 @@ async function forwardRequest(req, res, url) {
|
|
|
168
168
|
res.setHeader(key, response.headers[key]);
|
|
169
169
|
});
|
|
170
170
|
|
|
171
|
-
//
|
|
172
|
-
|
|
171
|
+
// 直接 pipe 流到响应
|
|
172
|
+
response.data.pipe(res);
|
|
173
|
+
|
|
173
174
|
log(`直接转发: ${req.method} ${url} -> ${response.status}`);
|
|
174
175
|
|
|
175
176
|
} catch (error) {
|
|
@@ -217,15 +218,15 @@ module.exports = (server, options) => {
|
|
|
217
218
|
broadcast('REQUEST', requestData);
|
|
218
219
|
log(`REQ ${req.method} ${url}`);
|
|
219
220
|
|
|
220
|
-
// 通过 axios 转发请求(使用
|
|
221
|
+
// 通过 axios 转发请求(使用 stream 模式,支持大文件)
|
|
221
222
|
const startTime = Date.now();
|
|
222
223
|
const response = await axios({
|
|
223
224
|
method: req.method.toLowerCase(),
|
|
224
225
|
url: url,
|
|
225
226
|
headers: req.headers,
|
|
226
227
|
data: reqBody || undefined,
|
|
227
|
-
responseType: '
|
|
228
|
-
validateStatus: () => true
|
|
228
|
+
responseType: 'stream', // 使用流式传输,不占用大量内存
|
|
229
|
+
validateStatus: () => true
|
|
229
230
|
});
|
|
230
231
|
const duration = Date.now() - startTime;
|
|
231
232
|
|
|
@@ -235,25 +236,47 @@ module.exports = (server, options) => {
|
|
|
235
236
|
res.setHeader(key, response.headers[key]);
|
|
236
237
|
});
|
|
237
238
|
|
|
238
|
-
//
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
//
|
|
242
|
-
|
|
239
|
+
// 直接 pipe 流到响应(不会截断,支持任意大小)
|
|
240
|
+
response.data.pipe(res);
|
|
241
|
+
|
|
242
|
+
// 收集响应数据用于 WebSocket 通知(限制大小)
|
|
243
|
+
let responseDataBuffer = Buffer.alloc(0);
|
|
244
|
+
const originalPipe = response.data.pipe.bind(response.data);
|
|
245
|
+
|
|
246
|
+
response.data.on('data', (chunk) => {
|
|
247
|
+
// 只收集前 1MB 用于通知
|
|
248
|
+
if (responseDataBuffer.length < 1024 * 1024) {
|
|
249
|
+
const remaining = 1024 * 1024 - responseDataBuffer.length;
|
|
250
|
+
const toCollect = chunk.slice(0, remaining);
|
|
251
|
+
responseDataBuffer = Buffer.concat([responseDataBuffer, toCollect]);
|
|
252
|
+
}
|
|
253
|
+
});
|
|
243
254
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
255
|
+
response.data.on('end', () => {
|
|
256
|
+
// 构建响应数据
|
|
257
|
+
const responseData = {
|
|
258
|
+
...requestData,
|
|
259
|
+
statusCode: response.status,
|
|
260
|
+
responseHeaders: {...response.headers},
|
|
261
|
+
responseBody: truncateString(bufferToString(responseDataBuffer)),
|
|
262
|
+
responseBodyBase64: responseDataBuffer.toString('base64'),
|
|
263
|
+
duration
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
// 通知 Chrome 插件:响应完成
|
|
267
|
+
broadcast('RESPONSE', responseData);
|
|
268
|
+
log(`转发成功: ${req.method} ${url} -> ${response.status} (${duration}ms)`);
|
|
269
|
+
});
|
|
253
270
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
271
|
+
response.data.on('error', (error) => {
|
|
272
|
+
log(`流错误: ${error.message}`);
|
|
273
|
+
broadcast('ERROR', {
|
|
274
|
+
url,
|
|
275
|
+
method: req.method,
|
|
276
|
+
error: error.message,
|
|
277
|
+
timestamp: Date.now()
|
|
278
|
+
});
|
|
279
|
+
});
|
|
257
280
|
|
|
258
281
|
} catch (error) {
|
|
259
282
|
log(`转发失败: ${error.message}`);
|
package/package.json
CHANGED