Files
18xchat/assets/Scripts/chat18x/network/codec/ProtoCodec.ts
T

14 lines
440 B
TypeScript
Raw Normal View History

2025-08-15 00:36:38 +08:00
import type { Type } from "protobufjs";
export const ProtoCodec = {
encode(reqType: Type, obj: any): Uint8Array {
const err = reqType.verify(obj);
if (err) throw Error(err);
return reqType.encode(reqType.create(obj)).finish();
},
decode<T = any>(resType: Type, buf: ArrayBuffer | Uint8Array): T {
const u8 = buf instanceof Uint8Array ? buf : new Uint8Array(buf);
return resType.decode(u8) as unknown as T;
}
};