修复再不同环境下buffer兼容性问题
This commit is contained in:
@@ -639,9 +639,13 @@ export class Scene implements IScene {
|
|||||||
incremental: IncrementalSnapshot | string | Buffer,
|
incremental: IncrementalSnapshot | string | Buffer,
|
||||||
componentRegistry?: Map<string, any>
|
componentRegistry?: Map<string, any>
|
||||||
): void {
|
): void {
|
||||||
const snapshot = (typeof incremental === 'string' || Buffer.isBuffer(incremental))
|
const isSerializedData = typeof incremental === 'string' ||
|
||||||
? IncrementalSerializer.deserializeIncremental(incremental)
|
(typeof Buffer !== 'undefined' && Buffer.isBuffer(incremental)) ||
|
||||||
: incremental;
|
incremental instanceof Uint8Array;
|
||||||
|
|
||||||
|
const snapshot = isSerializedData
|
||||||
|
? IncrementalSerializer.deserializeIncremental(incremental as string | Buffer)
|
||||||
|
: incremental as IncrementalSnapshot;
|
||||||
|
|
||||||
const registry = componentRegistry || ComponentRegistry.getAllComponentNames() as Map<string, any>;
|
const registry = componentRegistry || ComponentRegistry.getAllComponentNames() as Map<string, any>;
|
||||||
|
|
||||||
|
|||||||
@@ -687,7 +687,15 @@ export class IncrementalSerializer {
|
|||||||
|
|
||||||
if (typeof data === 'string') {
|
if (typeof data === 'string') {
|
||||||
// JSON格式:计算UTF-8编码后的字节数
|
// 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 {
|
} else {
|
||||||
// 二进制格式:直接返回Buffer长度
|
// 二进制格式:直接返回Buffer长度
|
||||||
return data.length;
|
return data.length;
|
||||||
|
|||||||
Reference in New Issue
Block a user