@depup/apollo__protobufjs 1.2.7-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +39 -0
- package/README.md +32 -0
- package/bin/pbjs +6 -0
- package/bin/pbts +6 -0
- package/changes.json +14 -0
- package/cli/LICENSE +33 -0
- package/cli/README.md +11 -0
- package/cli/bin/pbjs +6 -0
- package/cli/bin/pbts +6 -0
- package/cli/index.d.ts +3 -0
- package/cli/index.js +3 -0
- package/cli/lib/tsd-jsdoc/LICENSE +21 -0
- package/cli/lib/tsd-jsdoc/README.md +23 -0
- package/cli/lib/tsd-jsdoc/plugin.js +21 -0
- package/cli/lib/tsd-jsdoc/publish.js +693 -0
- package/cli/lib/tsd-jsdoc.json +18 -0
- package/cli/package.json +7 -0
- package/cli/package.standalone.json +32 -0
- package/cli/pbjs.d.ts +9 -0
- package/cli/pbjs.js +331 -0
- package/cli/pbts.d.ts +9 -0
- package/cli/pbts.js +198 -0
- package/cli/targets/json-module.js +38 -0
- package/cli/targets/json.js +8 -0
- package/cli/targets/proto.js +326 -0
- package/cli/targets/proto2.js +10 -0
- package/cli/targets/proto3.js +10 -0
- package/cli/targets/static-module.js +29 -0
- package/cli/targets/static.js +709 -0
- package/cli/util.js +183 -0
- package/cli/wrappers/amd.js +7 -0
- package/cli/wrappers/closure.js +7 -0
- package/cli/wrappers/commonjs.js +7 -0
- package/cli/wrappers/default.js +15 -0
- package/cli/wrappers/es6.js +5 -0
- package/dist/README.md +31 -0
- package/dist/light/README.md +31 -0
- package/dist/light/protobuf.js +7198 -0
- package/dist/light/protobuf.js.map +1 -0
- package/dist/light/protobuf.min.js +7 -0
- package/dist/light/protobuf.min.js.map +1 -0
- package/dist/minimal/README.md +31 -0
- package/dist/minimal/protobuf.js +2675 -0
- package/dist/minimal/protobuf.js.map +1 -0
- package/dist/minimal/protobuf.min.js +7 -0
- package/dist/minimal/protobuf.min.js.map +1 -0
- package/dist/protobuf.js +8775 -0
- package/dist/protobuf.js.map +1 -0
- package/dist/protobuf.min.js +7 -0
- package/dist/protobuf.min.js.map +1 -0
- package/ext/debug/README.md +4 -0
- package/ext/debug/index.js +71 -0
- package/ext/descriptor/README.md +72 -0
- package/ext/descriptor/index.d.ts +191 -0
- package/ext/descriptor/index.js +1052 -0
- package/ext/descriptor/test.js +54 -0
- package/google/LICENSE +27 -0
- package/google/README.md +1 -0
- package/google/api/annotations.json +83 -0
- package/google/api/annotations.proto +11 -0
- package/google/api/http.json +86 -0
- package/google/api/http.proto +31 -0
- package/google/protobuf/api.json +118 -0
- package/google/protobuf/api.proto +34 -0
- package/google/protobuf/descriptor.json +739 -0
- package/google/protobuf/descriptor.proto +286 -0
- package/google/protobuf/source_context.json +20 -0
- package/google/protobuf/source_context.proto +7 -0
- package/google/protobuf/type.json +202 -0
- package/google/protobuf/type.proto +89 -0
- package/index.d.ts +2628 -0
- package/index.js +4 -0
- package/light.d.ts +2 -0
- package/light.js +4 -0
- package/minimal.d.ts +2 -0
- package/minimal.js +4 -0
- package/package.json +147 -0
- package/scripts/postinstall.js +35 -0
- package/src/common.js +399 -0
- package/src/converter.js +304 -0
- package/src/decoder.js +106 -0
- package/src/encoder.js +119 -0
- package/src/enum.js +181 -0
- package/src/field.js +379 -0
- package/src/index-light.js +104 -0
- package/src/index-minimal.js +36 -0
- package/src/index.js +12 -0
- package/src/mapfield.js +126 -0
- package/src/message.js +139 -0
- package/src/method.js +151 -0
- package/src/namespace.js +433 -0
- package/src/object.js +200 -0
- package/src/oneof.js +203 -0
- package/src/parse.js +761 -0
- package/src/reader.js +405 -0
- package/src/reader_buffer.js +44 -0
- package/src/root.js +353 -0
- package/src/roots.js +18 -0
- package/src/rpc/service.js +142 -0
- package/src/rpc.js +36 -0
- package/src/service.js +167 -0
- package/src/tokenize.js +397 -0
- package/src/type.js +589 -0
- package/src/types.js +196 -0
- package/src/typescript.jsdoc +22 -0
- package/src/util/longbits.js +200 -0
- package/src/util/minimal.js +406 -0
- package/src/util.js +178 -0
- package/src/verifier.js +191 -0
- package/src/wrappers.js +83 -0
- package/src/writer.js +459 -0
- package/src/writer_buffer.js +81 -0
- package/tsconfig.json +7 -0
package/src/converter.js
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Runtime message from/to plain object converters.
|
|
4
|
+
* @namespace
|
|
5
|
+
*/
|
|
6
|
+
var converter = exports;
|
|
7
|
+
|
|
8
|
+
var Enum = require("./enum"),
|
|
9
|
+
util = require("./util");
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Generates a partial value fromObject conveter.
|
|
13
|
+
* @param {Codegen} gen Codegen instance
|
|
14
|
+
* @param {Field} field Reflected field
|
|
15
|
+
* @param {number} fieldIndex Field index
|
|
16
|
+
* @param {string} prop Property reference
|
|
17
|
+
* @returns {Codegen} Codegen instance
|
|
18
|
+
* @ignore
|
|
19
|
+
*/
|
|
20
|
+
function genValuePartial_fromObject(gen, field, fieldIndex, prop, ref) {
|
|
21
|
+
/* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
|
|
22
|
+
if (ref === undefined) {
|
|
23
|
+
ref = "d" + prop;
|
|
24
|
+
}
|
|
25
|
+
if (field.resolvedType) {
|
|
26
|
+
if (field.resolvedType instanceof Enum) { gen
|
|
27
|
+
("switch(%s){", ref);
|
|
28
|
+
for (var values = field.resolvedType.values, keys = Object.keys(values), i = 0; i < keys.length; ++i) {
|
|
29
|
+
if (field.repeated && values[keys[i]] === field.typeDefault) gen
|
|
30
|
+
("default:");
|
|
31
|
+
gen
|
|
32
|
+
("case%j:", keys[i])
|
|
33
|
+
("case %i:", values[keys[i]])
|
|
34
|
+
("m%s=%j", prop, values[keys[i]])
|
|
35
|
+
("break");
|
|
36
|
+
} gen
|
|
37
|
+
("}");
|
|
38
|
+
} else gen
|
|
39
|
+
("if(typeof %s!==\"object\")", ref)
|
|
40
|
+
("throw TypeError(%j)", field.fullName + ": object expected")
|
|
41
|
+
("m%s=types[%i].fromObject(%s)", prop, fieldIndex, ref);
|
|
42
|
+
} else {
|
|
43
|
+
var isUnsigned = false;
|
|
44
|
+
switch (field.type) {
|
|
45
|
+
case "double":
|
|
46
|
+
case "float": gen
|
|
47
|
+
("m%s=Number(%s)", prop, ref); // also catches "NaN", "Infinity"
|
|
48
|
+
break;
|
|
49
|
+
case "uint32":
|
|
50
|
+
case "fixed32": gen
|
|
51
|
+
("m%s=%s>>>0", prop, ref);
|
|
52
|
+
break;
|
|
53
|
+
case "int32":
|
|
54
|
+
case "sint32":
|
|
55
|
+
case "sfixed32": gen
|
|
56
|
+
("m%s=%s|0", prop, ref);
|
|
57
|
+
break;
|
|
58
|
+
case "uint64":
|
|
59
|
+
isUnsigned = true;
|
|
60
|
+
// eslint-disable-line no-fallthrough
|
|
61
|
+
case "int64":
|
|
62
|
+
case "sint64":
|
|
63
|
+
case "fixed64":
|
|
64
|
+
case "sfixed64": gen
|
|
65
|
+
("if(util.Long)")
|
|
66
|
+
("(m%s=util.Long.fromValue(%s)).unsigned=%j", prop, ref, isUnsigned)
|
|
67
|
+
("else if(typeof %s===\"string\")", ref)
|
|
68
|
+
("m%s=parseInt(%s,10)", prop, ref)
|
|
69
|
+
("else if(typeof %s===\"number\")", ref)
|
|
70
|
+
("m%s=%s", prop, ref)
|
|
71
|
+
("else if(typeof %s===\"object\")", ref)
|
|
72
|
+
("m%s=new util.LongBits(%s.low>>>0,%s.high>>>0).toNumber(%s)", prop, ref, ref, isUnsigned ? "true" : "");
|
|
73
|
+
break;
|
|
74
|
+
case "bytes": gen
|
|
75
|
+
("if(typeof %s===\"string\")", ref)
|
|
76
|
+
("util.base64.decode(%s,m%s=util.newBuffer(util.base64.length(%s)),0)", ref, prop, ref)
|
|
77
|
+
("else if(%s.length)", ref)
|
|
78
|
+
("m%s=%s", prop, ref);
|
|
79
|
+
break;
|
|
80
|
+
case "string": gen
|
|
81
|
+
("m%s=String(%s)", prop, ref);
|
|
82
|
+
break;
|
|
83
|
+
case "bool": gen
|
|
84
|
+
("m%s=Boolean(%s)", prop, ref);
|
|
85
|
+
break;
|
|
86
|
+
/* default: gen
|
|
87
|
+
("m%s=%s", prop, ref);
|
|
88
|
+
break; */
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return gen;
|
|
92
|
+
/* eslint-enable no-unexpected-multiline, block-scoped-var, no-redeclare */
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Generates a plain object to runtime message converter specific to the specified message type.
|
|
97
|
+
* @param {Type} mtype Message type
|
|
98
|
+
* @returns {Codegen} Codegen instance
|
|
99
|
+
*/
|
|
100
|
+
converter.fromObject = function fromObject(mtype) {
|
|
101
|
+
/* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
|
|
102
|
+
var fields = mtype.fieldsArray;
|
|
103
|
+
var gen = util.codegen(["d"], mtype.name + "$fromObject")
|
|
104
|
+
("if(d instanceof this.ctor)")
|
|
105
|
+
("return d");
|
|
106
|
+
if (!fields.length) return gen
|
|
107
|
+
("return new this.ctor");
|
|
108
|
+
gen
|
|
109
|
+
("var m=new this.ctor");
|
|
110
|
+
for (var i = 0; i < fields.length; ++i) {
|
|
111
|
+
var field = fields[i].resolve(),
|
|
112
|
+
prop = util.safeProp(field.name);
|
|
113
|
+
|
|
114
|
+
// Map fields
|
|
115
|
+
if (field.map) { gen
|
|
116
|
+
("if(d%s){", prop)
|
|
117
|
+
("if(typeof d%s!==\"object\")", prop)
|
|
118
|
+
("throw TypeError(%j)", field.fullName + ": object expected")
|
|
119
|
+
("m%s={}", prop)
|
|
120
|
+
("for(var ks=Object.keys(d%s),i=0;i<ks.length;++i){", prop);
|
|
121
|
+
genValuePartial_fromObject(gen, field, /* not sorted */ i, prop + "[ks[i]]")
|
|
122
|
+
("}")
|
|
123
|
+
("}");
|
|
124
|
+
|
|
125
|
+
// Repeated fields
|
|
126
|
+
} else if (field.repeated) {
|
|
127
|
+
gen("if(d%s){", prop);
|
|
128
|
+
var arrayRef = "d" + prop;
|
|
129
|
+
if (field.useToArray()) {
|
|
130
|
+
arrayRef = "array" + field.id;
|
|
131
|
+
gen("var %s", arrayRef);
|
|
132
|
+
gen("if (d%s!=null&&d%s.toArray) { %s = d%s.toArray() } else { %s = d%s }",
|
|
133
|
+
prop, prop, arrayRef, prop, arrayRef, prop);
|
|
134
|
+
}
|
|
135
|
+
gen
|
|
136
|
+
("if(!Array.isArray(%s))", arrayRef)
|
|
137
|
+
("throw TypeError(%j)", field.fullName + ": array expected")
|
|
138
|
+
("m%s=[]", prop)
|
|
139
|
+
("for(var i=0;i<%s.length;++i){", arrayRef);
|
|
140
|
+
genValuePartial_fromObject(gen, field, /* not sorted */ i, prop + "[i]", arrayRef + "[i]")
|
|
141
|
+
("}")
|
|
142
|
+
("}");
|
|
143
|
+
|
|
144
|
+
// Non-repeated fields
|
|
145
|
+
} else {
|
|
146
|
+
if (!(field.resolvedType instanceof Enum)) gen // no need to test for null/undefined if an enum (uses switch)
|
|
147
|
+
("if(d%s!=null){", prop); // !== undefined && !== null
|
|
148
|
+
genValuePartial_fromObject(gen, field, /* not sorted */ i, prop);
|
|
149
|
+
if (!(field.resolvedType instanceof Enum)) gen
|
|
150
|
+
("}");
|
|
151
|
+
}
|
|
152
|
+
} return gen
|
|
153
|
+
("return m");
|
|
154
|
+
/* eslint-enable no-unexpected-multiline, block-scoped-var, no-redeclare */
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Generates a partial value toObject converter.
|
|
159
|
+
* @param {Codegen} gen Codegen instance
|
|
160
|
+
* @param {Field} field Reflected field
|
|
161
|
+
* @param {number} fieldIndex Field index
|
|
162
|
+
* @param {string} prop Property reference
|
|
163
|
+
* @returns {Codegen} Codegen instance
|
|
164
|
+
* @ignore
|
|
165
|
+
*/
|
|
166
|
+
function genValuePartial_toObject(gen, field, fieldIndex, prop) {
|
|
167
|
+
/* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
|
|
168
|
+
if (field.resolvedType) {
|
|
169
|
+
if (field.resolvedType instanceof Enum) gen
|
|
170
|
+
("d%s=o.enums===String?types[%i].values[m%s]:m%s", prop, fieldIndex, prop, prop);
|
|
171
|
+
else gen
|
|
172
|
+
("d%s=types[%i].toObject(m%s,o)", prop, fieldIndex, prop);
|
|
173
|
+
} else {
|
|
174
|
+
var isUnsigned = false;
|
|
175
|
+
switch (field.type) {
|
|
176
|
+
case "double":
|
|
177
|
+
case "float": gen
|
|
178
|
+
("d%s=o.json&&!isFinite(m%s)?String(m%s):m%s", prop, prop, prop, prop);
|
|
179
|
+
break;
|
|
180
|
+
case "uint64":
|
|
181
|
+
isUnsigned = true;
|
|
182
|
+
// eslint-disable-line no-fallthrough
|
|
183
|
+
case "int64":
|
|
184
|
+
case "sint64":
|
|
185
|
+
case "fixed64":
|
|
186
|
+
case "sfixed64": gen
|
|
187
|
+
("if(typeof m%s===\"number\")", prop)
|
|
188
|
+
("d%s=o.longs===String?String(m%s):m%s", prop, prop, prop)
|
|
189
|
+
("else") // Long-like
|
|
190
|
+
("d%s=o.longs===String?util.Long.prototype.toString.call(m%s):o.longs===Number?new util.LongBits(m%s.low>>>0,m%s.high>>>0).toNumber(%s):m%s", prop, prop, prop, prop, isUnsigned ? "true": "", prop);
|
|
191
|
+
break;
|
|
192
|
+
case "bytes": gen
|
|
193
|
+
("d%s=o.bytes===String?util.base64.encode(m%s,0,m%s.length):o.bytes===Array?Array.prototype.slice.call(m%s):m%s", prop, prop, prop, prop, prop);
|
|
194
|
+
break;
|
|
195
|
+
default: gen
|
|
196
|
+
("d%s=m%s", prop, prop);
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return gen;
|
|
201
|
+
/* eslint-enable no-unexpected-multiline, block-scoped-var, no-redeclare */
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Generates a runtime message to plain object converter specific to the specified message type.
|
|
206
|
+
* @param {Type} mtype Message type
|
|
207
|
+
* @returns {Codegen} Codegen instance
|
|
208
|
+
*/
|
|
209
|
+
converter.toObject = function toObject(mtype) {
|
|
210
|
+
/* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
|
|
211
|
+
var fields = mtype.fieldsArray.slice().sort(util.compareFieldsById);
|
|
212
|
+
if (!fields.length)
|
|
213
|
+
return util.codegen()("return {}");
|
|
214
|
+
var gen = util.codegen(["m", "o"], mtype.name + "$toObject")
|
|
215
|
+
("if(!o)")
|
|
216
|
+
("o={}")
|
|
217
|
+
("var d={}");
|
|
218
|
+
|
|
219
|
+
var repeatedFields = [],
|
|
220
|
+
mapFields = [],
|
|
221
|
+
normalFields = [],
|
|
222
|
+
i = 0;
|
|
223
|
+
for (; i < fields.length; ++i)
|
|
224
|
+
if (!fields[i].partOf)
|
|
225
|
+
( fields[i].resolve().repeated ? repeatedFields
|
|
226
|
+
: fields[i].map ? mapFields
|
|
227
|
+
: normalFields).push(fields[i]);
|
|
228
|
+
|
|
229
|
+
if (repeatedFields.length) { gen
|
|
230
|
+
("if(o.arrays||o.defaults){");
|
|
231
|
+
for (i = 0; i < repeatedFields.length; ++i) gen
|
|
232
|
+
("d%s=[]", util.safeProp(repeatedFields[i].name));
|
|
233
|
+
gen
|
|
234
|
+
("}");
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (mapFields.length) { gen
|
|
238
|
+
("if(o.objects||o.defaults){");
|
|
239
|
+
for (i = 0; i < mapFields.length; ++i) gen
|
|
240
|
+
("d%s={}", util.safeProp(mapFields[i].name));
|
|
241
|
+
gen
|
|
242
|
+
("}");
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (normalFields.length) { gen
|
|
246
|
+
("if(o.defaults){");
|
|
247
|
+
for (i = 0; i < normalFields.length; ++i) {
|
|
248
|
+
var field = normalFields[i],
|
|
249
|
+
prop = util.safeProp(field.name);
|
|
250
|
+
if (field.resolvedType instanceof Enum) gen
|
|
251
|
+
("d%s=o.enums===String?%j:%j", prop, field.resolvedType.valuesById[field.typeDefault], field.typeDefault);
|
|
252
|
+
else if (field.long) gen
|
|
253
|
+
("if(util.Long){")
|
|
254
|
+
("var n=new util.Long(%i,%i,%j)", field.typeDefault.low, field.typeDefault.high, field.typeDefault.unsigned)
|
|
255
|
+
("d%s=o.longs===String?n.toString():o.longs===Number?n.toNumber():n", prop)
|
|
256
|
+
("}else")
|
|
257
|
+
("d%s=o.longs===String?%j:%i", prop, field.typeDefault.toString(), field.typeDefault.toNumber());
|
|
258
|
+
else if (field.bytes) {
|
|
259
|
+
var arrayDefault = "[" + Array.prototype.slice.call(field.typeDefault).join(",") + "]";
|
|
260
|
+
gen
|
|
261
|
+
("if(o.bytes===String)d%s=%j", prop, String.fromCharCode.apply(String, field.typeDefault))
|
|
262
|
+
("else{")
|
|
263
|
+
("d%s=%s", prop, arrayDefault)
|
|
264
|
+
("if(o.bytes!==Array)d%s=util.newBuffer(d%s)", prop, prop)
|
|
265
|
+
("}");
|
|
266
|
+
} else gen
|
|
267
|
+
("d%s=%j", prop, field.typeDefault); // also messages (=null)
|
|
268
|
+
} gen
|
|
269
|
+
("}");
|
|
270
|
+
}
|
|
271
|
+
var hasKs2 = false;
|
|
272
|
+
for (i = 0; i < fields.length; ++i) {
|
|
273
|
+
var field = fields[i],
|
|
274
|
+
index = mtype._fieldsArray.indexOf(field),
|
|
275
|
+
prop = util.safeProp(field.name);
|
|
276
|
+
if (field.map) {
|
|
277
|
+
if (!hasKs2) { hasKs2 = true; gen
|
|
278
|
+
("var ks2");
|
|
279
|
+
} gen
|
|
280
|
+
("if(m%s&&(ks2=Object.keys(m%s)).length){", prop, prop)
|
|
281
|
+
("d%s={}", prop)
|
|
282
|
+
("for(var j=0;j<ks2.length;++j){");
|
|
283
|
+
genValuePartial_toObject(gen, field, /* sorted */ index, prop + "[ks2[j]]")
|
|
284
|
+
("}");
|
|
285
|
+
} else if (field.repeated) { gen
|
|
286
|
+
("if(m%s&&m%s.length){", prop, prop)
|
|
287
|
+
("d%s=[]", prop)
|
|
288
|
+
("for(var j=0;j<m%s.length;++j){", prop);
|
|
289
|
+
genValuePartial_toObject(gen, field, /* sorted */ index, prop + "[j]")
|
|
290
|
+
("}");
|
|
291
|
+
} else { gen
|
|
292
|
+
("if(m%s!=null&&m.hasOwnProperty(%j)){", prop, field.name); // !== undefined && !== null
|
|
293
|
+
genValuePartial_toObject(gen, field, /* sorted */ index, prop);
|
|
294
|
+
if (field.partOf) gen
|
|
295
|
+
("if(o.oneofs)")
|
|
296
|
+
("d%s=%j", util.safeProp(field.partOf.name), field.name);
|
|
297
|
+
}
|
|
298
|
+
gen
|
|
299
|
+
("}");
|
|
300
|
+
}
|
|
301
|
+
return gen
|
|
302
|
+
("return d");
|
|
303
|
+
/* eslint-enable no-unexpected-multiline, block-scoped-var, no-redeclare */
|
|
304
|
+
};
|
package/src/decoder.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = decoder;
|
|
3
|
+
|
|
4
|
+
var Enum = require("./enum"),
|
|
5
|
+
types = require("./types"),
|
|
6
|
+
util = require("./util");
|
|
7
|
+
|
|
8
|
+
function missing(field) {
|
|
9
|
+
return "missing required '" + field.name + "'";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Generates a decoder specific to the specified message type.
|
|
14
|
+
* @param {Type} mtype Message type
|
|
15
|
+
* @returns {Codegen} Codegen instance
|
|
16
|
+
*/
|
|
17
|
+
function decoder(mtype) {
|
|
18
|
+
/* eslint-disable no-unexpected-multiline */
|
|
19
|
+
var gen = util.codegen(["r", "l"], mtype.name + "$decode")
|
|
20
|
+
("if(!(r instanceof Reader))")
|
|
21
|
+
("r=Reader.create(r)")
|
|
22
|
+
("var c=l===undefined?r.len:r.pos+l,m=new this.ctor" + (mtype.fieldsArray.filter(function(field) { return field.map; }).length ? ",k" : ""))
|
|
23
|
+
("while(r.pos<c){")
|
|
24
|
+
("var t=r.uint32()");
|
|
25
|
+
if (mtype.group) gen
|
|
26
|
+
("if((t&7)===4)")
|
|
27
|
+
("break");
|
|
28
|
+
gen
|
|
29
|
+
("switch(t>>>3){");
|
|
30
|
+
|
|
31
|
+
var i = 0;
|
|
32
|
+
for (; i < /* initializes */ mtype.fieldsArray.length; ++i) {
|
|
33
|
+
var field = mtype._fieldsArray[i].resolve(),
|
|
34
|
+
type = field.resolvedType instanceof Enum ? "int32" : field.type,
|
|
35
|
+
ref = "m" + util.safeProp(field.name); gen
|
|
36
|
+
("case %i:", field.id);
|
|
37
|
+
|
|
38
|
+
// Map fields
|
|
39
|
+
if (field.map) { gen
|
|
40
|
+
("r.skip().pos++") // assumes id 1 + key wireType
|
|
41
|
+
("if(%s===util.emptyObject)", ref)
|
|
42
|
+
("%s={}", ref)
|
|
43
|
+
("k=r.%s()", field.keyType)
|
|
44
|
+
("r.pos++"); // assumes id 2 + value wireType
|
|
45
|
+
if (types.long[field.keyType] !== undefined) {
|
|
46
|
+
if (types.basic[type] === undefined) gen
|
|
47
|
+
("%s[typeof k===\"object\"?util.longToHash(k):k]=types[%i].decode(r,r.uint32())", ref, i); // can't be groups
|
|
48
|
+
else gen
|
|
49
|
+
("%s[typeof k===\"object\"?util.longToHash(k):k]=r.%s()", ref, type);
|
|
50
|
+
} else {
|
|
51
|
+
if (types.basic[type] === undefined) gen
|
|
52
|
+
("%s[k]=types[%i].decode(r,r.uint32())", ref, i); // can't be groups
|
|
53
|
+
else gen
|
|
54
|
+
("%s[k]=r.%s()", ref, type);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Repeated fields
|
|
58
|
+
} else if (field.repeated) { gen
|
|
59
|
+
|
|
60
|
+
("if(!(%s&&%s.length))", ref, ref)
|
|
61
|
+
("%s=[]", ref);
|
|
62
|
+
|
|
63
|
+
// Packable (always check for forward and backward compatiblity)
|
|
64
|
+
if (types.packed[type] !== undefined) gen
|
|
65
|
+
("if((t&7)===2){")
|
|
66
|
+
("var c2=r.uint32()+r.pos")
|
|
67
|
+
("while(r.pos<c2)")
|
|
68
|
+
("%s.push(r.%s())", ref, type)
|
|
69
|
+
("}else");
|
|
70
|
+
|
|
71
|
+
// Non-packed
|
|
72
|
+
if (types.basic[type] === undefined) gen(field.resolvedType.group
|
|
73
|
+
? "%s.push(types[%i].decode(r))"
|
|
74
|
+
: "%s.push(types[%i].decode(r,r.uint32()))", ref, i);
|
|
75
|
+
else gen
|
|
76
|
+
("%s.push(r.%s())", ref, type);
|
|
77
|
+
|
|
78
|
+
// Non-repeated
|
|
79
|
+
} else if (types.basic[type] === undefined) gen(field.resolvedType.group
|
|
80
|
+
? "%s=types[%i].decode(r)"
|
|
81
|
+
: "%s=types[%i].decode(r,r.uint32())", ref, i);
|
|
82
|
+
else gen
|
|
83
|
+
("%s=r.%s()", ref, type);
|
|
84
|
+
gen
|
|
85
|
+
("break");
|
|
86
|
+
// Unknown fields
|
|
87
|
+
} gen
|
|
88
|
+
("default:")
|
|
89
|
+
("r.skipType(t&7)")
|
|
90
|
+
("break")
|
|
91
|
+
|
|
92
|
+
("}")
|
|
93
|
+
("}");
|
|
94
|
+
|
|
95
|
+
// Field presence
|
|
96
|
+
for (i = 0; i < mtype._fieldsArray.length; ++i) {
|
|
97
|
+
var rfield = mtype._fieldsArray[i];
|
|
98
|
+
if (rfield.required) gen
|
|
99
|
+
("if(!m.hasOwnProperty(%j))", rfield.name)
|
|
100
|
+
("throw util.ProtocolError(%j,{instance:m})", missing(rfield));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return gen
|
|
104
|
+
("return m");
|
|
105
|
+
/* eslint-enable no-unexpected-multiline */
|
|
106
|
+
}
|
package/src/encoder.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
module.exports = encoder;
|
|
3
|
+
|
|
4
|
+
var Enum = require("./enum"),
|
|
5
|
+
types = require("./types"),
|
|
6
|
+
util = require("./util");
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Generates a partial message type encoder.
|
|
10
|
+
* @param {Codegen} gen Codegen instance
|
|
11
|
+
* @param {Field} field Reflected field
|
|
12
|
+
* @param {number} fieldIndex Field index
|
|
13
|
+
* @param {string} ref Variable reference
|
|
14
|
+
* @returns {Codegen} Codegen instance
|
|
15
|
+
* @ignore
|
|
16
|
+
*/
|
|
17
|
+
function genTypePartial(gen, field, fieldIndex, ref) {
|
|
18
|
+
/* eslint-disable no-unexpected-multiline */
|
|
19
|
+
if (field.resolvedType.group) {
|
|
20
|
+
gen("types[%i].encode(%s,w.uint32(%i)).uint32(%i)", fieldIndex, ref, (field.id << 3 | 3) >>> 0, (field.id << 3 | 4) >>> 0);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
var key = (field.id << 3 | 2) >>> 0;
|
|
24
|
+
if (field.preEncoded()) {
|
|
25
|
+
gen("if (%s instanceof Uint8Array) {", ref)
|
|
26
|
+
("w.uint32(%i)", key)
|
|
27
|
+
("w.bytes(%s)", ref)
|
|
28
|
+
("} else {");
|
|
29
|
+
}
|
|
30
|
+
gen("types[%i].encode(%s,w.uint32(%i).fork()).ldelim()", fieldIndex, ref, key);
|
|
31
|
+
if (field.preEncoded()) {
|
|
32
|
+
gen("}")
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Generates an encoder specific to the specified message type.
|
|
38
|
+
* @param {Type} mtype Message type
|
|
39
|
+
* @returns {Codegen} Codegen instance
|
|
40
|
+
*/
|
|
41
|
+
function encoder(mtype) {
|
|
42
|
+
/* eslint-disable no-unexpected-multiline, block-scoped-var, no-redeclare */
|
|
43
|
+
var gen = util.codegen(["m", "w"], mtype.name + "$encode")
|
|
44
|
+
("if(!w)")
|
|
45
|
+
("w=Writer.create()");
|
|
46
|
+
|
|
47
|
+
var i, ref;
|
|
48
|
+
|
|
49
|
+
// "when a message is serialized its known fields should be written sequentially by field number"
|
|
50
|
+
var fields = /* initializes */ mtype.fieldsArray.slice().sort(util.compareFieldsById);
|
|
51
|
+
|
|
52
|
+
for (var i = 0; i < fields.length; ++i) {
|
|
53
|
+
var field = fields[i].resolve(),
|
|
54
|
+
index = mtype._fieldsArray.indexOf(field),
|
|
55
|
+
type = field.resolvedType instanceof Enum ? "int32" : field.type,
|
|
56
|
+
wireType = types.basic[type];
|
|
57
|
+
ref = "m" + util.safeProp(field.name);
|
|
58
|
+
|
|
59
|
+
// Map fields
|
|
60
|
+
if (field.map) {
|
|
61
|
+
gen
|
|
62
|
+
("if(%s!=null&&Object.hasOwnProperty.call(m,%j)){", ref, field.name) // !== undefined && !== null
|
|
63
|
+
("for(var ks=Object.keys(%s),i=0;i<ks.length;++i){", ref)
|
|
64
|
+
("w.uint32(%i).fork().uint32(%i).%s(ks[i])", (field.id << 3 | 2) >>> 0, 8 | types.mapKey[field.keyType], field.keyType);
|
|
65
|
+
if (wireType === undefined) gen
|
|
66
|
+
("types[%i].encode(%s[ks[i]],w.uint32(18).fork()).ldelim().ldelim()", index, ref); // can't be groups
|
|
67
|
+
else gen
|
|
68
|
+
(".uint32(%i).%s(%s[ks[i]]).ldelim()", 16 | wireType, type, ref);
|
|
69
|
+
gen
|
|
70
|
+
("}")
|
|
71
|
+
("}");
|
|
72
|
+
|
|
73
|
+
// Repeated fields
|
|
74
|
+
} else if (field.repeated) {
|
|
75
|
+
var arrayRef = ref;
|
|
76
|
+
if (field.useToArray()) {
|
|
77
|
+
arrayRef = "array" + field.id;
|
|
78
|
+
gen("var %s", arrayRef);
|
|
79
|
+
gen("if (%s!=null&&%s.toArray) { %s = %s.toArray() } else { %s = %s }",
|
|
80
|
+
ref, ref, arrayRef, ref, arrayRef, ref);
|
|
81
|
+
}
|
|
82
|
+
gen("if(%s!=null&&%s.length){", arrayRef, arrayRef); // !== undefined && !== null
|
|
83
|
+
// Packed repeated
|
|
84
|
+
if (field.packed && types.packed[type] !== undefined) { gen
|
|
85
|
+
|
|
86
|
+
("w.uint32(%i).fork()", (field.id << 3 | 2) >>> 0)
|
|
87
|
+
("for(var i=0;i<%s.length;++i)", arrayRef)
|
|
88
|
+
("w.%s(%s[i])", type, arrayRef)
|
|
89
|
+
("w.ldelim()");
|
|
90
|
+
|
|
91
|
+
// Non-packed
|
|
92
|
+
} else { gen
|
|
93
|
+
|
|
94
|
+
("for(var i=0;i<%s.length;++i)", arrayRef);
|
|
95
|
+
if (wireType === undefined)
|
|
96
|
+
genTypePartial(gen, field, index, arrayRef + "[i]");
|
|
97
|
+
else gen
|
|
98
|
+
("w.uint32(%i).%s(%s[i])", (field.id << 3 | wireType) >>> 0, type, arrayRef);
|
|
99
|
+
|
|
100
|
+
} gen
|
|
101
|
+
("}");
|
|
102
|
+
|
|
103
|
+
// Non-repeated
|
|
104
|
+
} else {
|
|
105
|
+
if (field.optional) gen
|
|
106
|
+
("if(%s!=null&&Object.hasOwnProperty.call(m,%j))", ref, field.name); // !== undefined && !== null
|
|
107
|
+
|
|
108
|
+
if (wireType === undefined)
|
|
109
|
+
genTypePartial(gen, field, index, ref);
|
|
110
|
+
else gen
|
|
111
|
+
("w.uint32(%i).%s(%s)", (field.id << 3 | wireType) >>> 0, type, ref);
|
|
112
|
+
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return gen
|
|
117
|
+
("return w");
|
|
118
|
+
/* eslint-enable no-unexpected-multiline, block-scoped-var, no-redeclare */
|
|
119
|
+
}
|