网络层
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user