whistle.pastekitlab 1.9.2 → 1.9.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 +31 -5
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -41,10 +41,19 @@ function bufferToString(buffer) {
|
|
|
41
41
|
try {
|
|
42
42
|
if (!buffer) return null;
|
|
43
43
|
const result = buffer.toString('utf8');
|
|
44
|
-
if (/[\uFFFD]/.test(result))
|
|
44
|
+
if (/[\uFFFD]/.test(result)) {
|
|
45
|
+
// 包含替换字符,尝试 latin1 编码
|
|
46
|
+
log(`[bufferToString] UTF-8 包含替换字符,尝试 latin1 编码`);
|
|
47
|
+
return buffer.toString('latin1');
|
|
48
|
+
}
|
|
45
49
|
return result;
|
|
46
|
-
} catch {
|
|
47
|
-
|
|
50
|
+
} catch (e) {
|
|
51
|
+
log(`[bufferToString] 转换失败: ${e.message},尝试 latin1`);
|
|
52
|
+
try {
|
|
53
|
+
return buffer.toString('latin1');
|
|
54
|
+
} catch {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
48
57
|
}
|
|
49
58
|
}
|
|
50
59
|
|
|
@@ -404,13 +413,30 @@ module.exports = (server, options) => {
|
|
|
404
413
|
|
|
405
414
|
proxyRes.on('end', () => {
|
|
406
415
|
const duration = Date.now() - startTime;
|
|
416
|
+
|
|
407
417
|
// 构建响应数据
|
|
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
|
+
|
|
408
434
|
const responseData = {
|
|
409
435
|
...requestData,
|
|
410
436
|
statusCode: proxyRes.statusCode,
|
|
411
437
|
responseHeaders: {...proxyRes.headers},
|
|
412
|
-
responseBody: truncateString(
|
|
413
|
-
responseBodyBase64:
|
|
438
|
+
responseBody: truncateString(responseBodyStr),
|
|
439
|
+
responseBodyBase64: responseBodyBase64,
|
|
414
440
|
duration
|
|
415
441
|
};
|
|
416
442
|
|
package/package.json
CHANGED