whistle.pastekitlab 1.9.3 → 1.9.4
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 +9 -39
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -41,19 +41,11 @@ function bufferToString(buffer) {
|
|
|
41
41
|
try {
|
|
42
42
|
if (!buffer) return null;
|
|
43
43
|
const result = buffer.toString('utf8');
|
|
44
|
-
if (/[\uFFFD]/.test(result))
|
|
45
|
-
// 包含替换字符,尝试 latin1 编码
|
|
46
|
-
log(`[bufferToString] UTF-8 包含替换字符,尝试 latin1 编码`);
|
|
47
|
-
return buffer.toString('latin1');
|
|
48
|
-
}
|
|
44
|
+
if (/[\uFFFD]/.test(result)) return null; // 检查是否有替换字符
|
|
49
45
|
return result;
|
|
50
|
-
} catch
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return buffer.toString('latin1');
|
|
54
|
-
} catch {
|
|
55
|
-
return null;
|
|
56
|
-
}
|
|
46
|
+
} catch {
|
|
47
|
+
console.error('[pastekitlab]', 'bufferToString error:', e);
|
|
48
|
+
return null;
|
|
57
49
|
}
|
|
58
50
|
}
|
|
59
51
|
|
|
@@ -341,7 +333,7 @@ module.exports = (server, options) => {
|
|
|
341
333
|
url,
|
|
342
334
|
method: req.method,
|
|
343
335
|
requestHeaders: {...req.headers},
|
|
344
|
-
body:
|
|
336
|
+
body: bufferToString(Buffer.from(reqBody)),
|
|
345
337
|
bodyBase64: Buffer.from(reqBody).toString('base64'),
|
|
346
338
|
timestamp: Date.now()
|
|
347
339
|
};
|
|
@@ -399,44 +391,22 @@ module.exports = (server, options) => {
|
|
|
399
391
|
// 直接 pipe 流到响应
|
|
400
392
|
proxyRes.pipe(res);
|
|
401
393
|
|
|
402
|
-
// 收集响应数据用于 WebSocket
|
|
394
|
+
// 收集响应数据用于 WebSocket 通知
|
|
403
395
|
let responseDataBuffer = Buffer.alloc(0);
|
|
404
396
|
|
|
405
397
|
proxyRes.on('data', (chunk) => {
|
|
406
|
-
|
|
407
|
-
if (responseDataBuffer.length < 1024 * 1024) {
|
|
408
|
-
const remaining = 1024 * 1024 - responseDataBuffer.length;
|
|
409
|
-
const toCollect = chunk.slice(0, remaining);
|
|
410
|
-
responseDataBuffer = Buffer.concat([responseDataBuffer, toCollect]);
|
|
411
|
-
}
|
|
398
|
+
responseDataBuffer = Buffer.concat([responseDataBuffer, chunk]);
|
|
412
399
|
});
|
|
413
400
|
|
|
414
401
|
proxyRes.on('end', () => {
|
|
415
402
|
const duration = Date.now() - startTime;
|
|
416
|
-
|
|
417
403
|
// 构建响应数据
|
|
418
|
-
let responseBodyStr = bufferToString(responseDataBuffer);
|
|
419
|
-
const responseBodyBase64 = responseDataBuffer.toString('base64');
|
|
420
|
-
|
|
421
|
-
// 如果字符串转换失败但 base64 成功,记录日志
|
|
422
|
-
if (!responseBodyStr && responseBodyBase64) {
|
|
423
|
-
log(`[mainHandler] responseBody 为 null,但 responseBodyBase64 有数据 (${responseDataBuffer.length} bytes)`);
|
|
424
|
-
log(`[mainHandler] Content-Type: ${proxyRes.headers['content-type'] || 'N/A'}`);
|
|
425
|
-
// 尝试使用 latin1 编码作为备选
|
|
426
|
-
try {
|
|
427
|
-
responseBodyStr = responseDataBuffer.toString('latin1');
|
|
428
|
-
log(`[mainHandler] 使用 latin1 编码恢复 responseBody (${responseBodyStr.length} chars)`);
|
|
429
|
-
} catch (e) {
|
|
430
|
-
log(`[mainHandler] latin1 编码也失败: ${e.message}`);
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
|
|
434
404
|
const responseData = {
|
|
435
405
|
...requestData,
|
|
436
406
|
statusCode: proxyRes.statusCode,
|
|
437
407
|
responseHeaders: {...proxyRes.headers},
|
|
438
|
-
responseBody: truncateString(
|
|
439
|
-
responseBodyBase64:
|
|
408
|
+
responseBody: truncateString(bufferToString(responseDataBuffer)),
|
|
409
|
+
responseBodyBase64: responseDataBuffer.toString('base64'),
|
|
440
410
|
duration
|
|
441
411
|
};
|
|
442
412
|
|
package/package.json
CHANGED