修复再不同环境下buffer兼容性问题

This commit is contained in:
YHH
2025-10-09 17:44:15 +08:00
parent e724e5a1ba
commit 79f7c89e23
2 changed files with 16 additions and 4 deletions

View File

@@ -687,7 +687,15 @@ export class IncrementalSerializer {
if (typeof data === 'string') {
// JSON格式计算UTF-8编码后的字节数
return Buffer.byteLength(data, 'utf8');
// 使用 Blob 来计算浏览器和 Node.js 环境兼容的字节数
if (typeof Blob !== 'undefined') {
return new Blob([data]).size;
} else if (typeof Buffer !== 'undefined') {
return Buffer.byteLength(data, 'utf8');
} else {
// 回退方案:粗略估算(不精确,但可用)
return new TextEncoder().encode(data).length;
}
} else {
// 二进制格式直接返回Buffer长度
return data.length;