-
Notifications
You must be signed in to change notification settings - Fork 0
/
PbTranscoder.ts
154 lines (131 loc) · 4.75 KB
/
PbTranscoder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { Field, loadSync as pbLoadSync, Root, Type } from "protobufjs";
import { join } from "path";
const mainRoot = pbLoadSync(join(__dirname, "../protocol.proto"));
const mainRootObj = mainRoot.toJSON();
const mainType = mainRoot.lookupType("main");
export interface PbTranscoderOptions {
/**
* Path to the .proto file. Default: "./main.proto"
*/
protoPath?: string;
/**
* Name of the type to use for syncing. Must be present in the .proto file. Default: "obj"
*/
syncType?: string;
/**
* Name of the type to use for deleting. Must be present in the .proto file. Should be the same as the type used for syncing, but with all values being booleans. Default: "objBool"
*/
delType?: string;
/**
* Name of the enum to be used for the method name. Must be present in the .proto file. Default: "methods"
*/
methodEnumName?: string;
/**
* JSON Representation of ProtocolBuffers Types. When passed, this will be exclusively used to initialize PbTranscoder
*/
restoreState?: string;
}
/**
* Transcodes objects according to the protocol buffers spec.
*/
export class PbTranscoder {
private root: Root;
private type: Type;
readonly JSONRoot: Record<string, any>;
readonly syncType: string;
readonly delType: string;
readonly methodEnumName: string;
constructor({
protoPath,
syncType = "obj",
delType = "objBool",
methodEnumName = "methods",
restoreState,
}: PbTranscoderOptions) {
this.root = new Root();
if (restoreState) {
const parsedSavedState: {
JSONRoot: Record<string, any>;
syncType: string;
delType: string;
methodEnumName: string;
} = JSON.parse(restoreState);
Root.fromJSON(parsedSavedState.JSONRoot, this.root);
this.JSONRoot = parsedSavedState.JSONRoot;
this.syncType = parsedSavedState.syncType;
this.delType = parsedSavedState.delType;
this.methodEnumName = parsedSavedState.methodEnumName;
} else if (protoPath) {
this.root.loadSync(protoPath);
this.JSONRoot = this.root.toJSON();
this.syncType = syncType;
this.delType = delType;
this.methodEnumName = methodEnumName;
} else {
// error
}
Root.fromJSON(mainRootObj, this.root);
this.root.lookupType("rpcCall").add(new Field("method", 3, this.methodEnumName));
this.type = this.root.lookupType("main");
this.type.add(new Field("objAll", 20, this.syncType));
this.type.add(new Field("objSync", 21, this.syncType));
this.type.add(new Field("objDelete", 22, this.delType));
}
/**
* Returns the string representation of the transcoder. Used for saving and restoring.
* @returns The state string.
*/
getState(): string {
return JSON.stringify({
JSONRoot: this.JSONRoot,
syncType: this.syncType,
delType: this.delType,
methodEnumName: this.methodEnumName,
});
}
/**
* Decodes the buffer according to the provided type. Uses the general protocol main type by default. Does not decode service-specific information.
* @param buf Buffer to decode
* @param type Type used to decode. Defaults to main protocol type.
* @returns The decoded object
*/
static decode(buf: Buffer, type = mainType) {
const msg = type.decode(buf);
const obj = type.toObject(msg, {
enums: String,
});
return obj;
}
/**
* Decodes entire protocol object according to the given service spec.
* @param buf Buffer to decode
* @returns The decoded object
*/
decode(buf: Buffer) {
return PbTranscoder.decode(buf, this.type);
}
/**
* Encodes the object according to the provided type. Uses the general protocol main type by default. Does not encode service-specific information.
* @param obj Object to encode
* @param type Type used to encode. Defaults to main protocol type.
* @returns The encoded Buffer
*/
static encode(obj: Record<string, any>, type = mainType) {
// const err = this.mainType.verify(obj)
// if(err) {
// throw new Error(err)
// }
// console.log("testt", obj)
const msg = type.fromObject(obj);
const buf = Buffer.from(type.encode(msg).finish());
return buf;
}
/**
* Encodes entire protocol object according to the given service spec.
* @param obj Object to encode
* @returns Encoded Buffer.
*/
encode(obj: Record<string, any>) {
return PbTranscoder.encode(obj, this.type);
}
}