14 lines
440 B
TypeScript
14 lines
440 B
TypeScript
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;
|
|
}
|
|
}; |