forked from protocolbuffers/protobuf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protobuf.js
6125 lines (5476 loc) · 179 KB
/
protobuf.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* protobuf.js v6.1.0 (c) 2016 Daniel Wirtz
* Compiled Wed, 07 Dec 2016 23:23:40 UTC
* Licensed under the Apache License, Version 2.0
* see: https://github.com/dcodeIO/protobuf.js for details
*/
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
// Copyright (c) 2008, Fair Oaks Labs, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Fair Oaks Labs, Inc. nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
//
// Modifications to writeIEEE754 to support negative zeroes made by Brian White
// ref: https://github.com/nodejs/node/blob/87286cc7371886d9856edf424785aaa890ba05a9/lib/buffer_ieee754.js
exports.read = function readIEEE754(buffer, offset, isBE, mLen, nBytes) {
var e, m,
eLen = nBytes * 8 - mLen - 1,
eMax = (1 << eLen) - 1,
eBias = eMax >> 1,
nBits = -7,
i = isBE ? 0 : (nBytes - 1),
d = isBE ? 1 : -1,
s = buffer[offset + i];
i += d;
e = s & ((1 << (-nBits)) - 1);
s >>= (-nBits);
nBits += eLen;
for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8);
m = e & ((1 << (-nBits)) - 1);
e >>= (-nBits);
nBits += mLen;
for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8);
if (e === 0) {
e = 1 - eBias;
} else if (e === eMax) {
return m ? NaN : ((s ? -1 : 1) * Infinity);
} else {
m = m + Math.pow(2, mLen);
e = e - eBias;
}
return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
};
exports.write = function writeIEEE754(buffer, value, offset, isBE, mLen, nBytes) {
var e, m, c,
eLen = nBytes * 8 - mLen - 1,
eMax = (1 << eLen) - 1,
eBias = eMax >> 1,
rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0),
i = isBE ? (nBytes - 1) : 0,
d = isBE ? -1 : 1,
s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
value = Math.abs(value);
if (isNaN(value) || value === Infinity) {
m = isNaN(value) ? 1 : 0;
e = eMax;
} else {
e = Math.floor(Math.log(value) / Math.LN2);
if (value * (c = Math.pow(2, -e)) < 1) {
e--;
c *= 2;
}
if (e + eBias >= 1) {
value += rt / c;
} else {
value += rt * Math.pow(2, 1 - eBias);
}
if (value * c >= 2) {
e++;
c /= 2;
}
if (e + eBias >= eMax) {
m = 0;
e = eMax;
} else if (e + eBias >= 1) {
m = (value * c - 1) * Math.pow(2, mLen);
e = e + eBias;
} else {
m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
e = 0;
}
}
for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8);
e = (e << mLen) | m;
eLen += mLen;
for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8);
buffer[offset + i - d] |= s * 128;
};
},{}],2:[function(require,module,exports){
"use strict";
module.exports = codegen;
var util = require(23);
var blockOpenRe = /[{[]$/,
blockCloseRe = /^[}\]]/,
casingRe = /:$/,
branchRe = /^\s*(?:if|else if|while|for)\b|\b(?:else)\s*$/,
breakRe = /\b(?:break|continue);?$|^\s*return\b/;
/**
* A closure for generating functions programmatically.
* @namespace
* @function
* @param {...string} params Function parameter names
* @returns {CodegenInstance} Codegen instance
* @property {boolean} supported Whether code generation is supported by the environment.
* @property {boolean} verbose=false When set to true, codegen will log generated code to console. Useful for debugging.
*/
function codegen() {
var args = Array.prototype.slice.call(arguments),
src = ['\t"use strict"'],
indent = 1,
inCase = false;
/**
* A codegen instance as returned by {@link codegen}, that also is a {@link util.sprintf|sprintf}-like appender function.
* @typedef CodegenInstance
* @type {function}
* @param {string} format Format string
* @param {...*} args Replacements
* @returns {CodegenInstance} Itself
* @property {function(string=):string} str Stringifies the so far generated function source.
* @property {function(string=, Object=):function} eof Ends generation and builds the function whilst applying a scope.
*/
/**/
function gen() {
var line = util.sprintf.apply(null, arguments);
var level = indent;
if (src.length) {
var prev = src[src.length - 1];
// block open or one time branch
if (blockOpenRe.test(prev))
level = ++indent; // keep
else if (branchRe.test(prev))
++level; // once
// casing
if (casingRe.test(prev) && !casingRe.test(line)) {
level = ++indent;
inCase = true;
} else if (inCase && breakRe.test(prev)) {
level = --indent;
inCase = false;
}
// block close
if (blockCloseRe.test(line))
level = --indent;
}
for (var index = 0; index < level; ++index)
line = "\t" + line;
src.push(line);
return gen;
}
/**
* Stringifies the so far generated function source.
* @param {string} [name] Function name, defaults to generate an anonymous function
* @returns {string} Function source using tabs for indentation
* @inner
*/
function str(name) {
return "function " + (name ? name.replace(/[^\w_$]/g, "_") : "") + "(" + args.join(", ") + ") {\n" + src.join("\n") + "\n}";
}
gen.str = str;
/**
* Ends generation and builds the function whilst applying a scope.
* @param {string} [name] Function name, defaults to generate an anonymous function
* @param {Object} [scope] Function scope
* @returns {function} The generated function, with scope applied if specified
* @inner
*/
function eof(name, scope) {
if (typeof name === 'object') {
scope = name;
name = undefined;
}
var source = gen.str(name);
if (codegen.verbose)
console.log("--- codegen ---\n" + source.replace(/^/mg, "> ").replace(/\t/g, " ")); // eslint-disable-line no-console
var keys = Object.keys(scope || (scope = {}));
return Function.apply(null, keys.concat("return " + source)).apply(null, keys.map(function(key) { return scope[key]; })); // eslint-disable-line no-new-func
// ^ Creates a wrapper function with the scoped variable names as its parameters,
// calls it with the respective scoped variable values ^
// and returns our brand-new properly scoped function.
//
// This works because "Invoking the Function constructor as a function (without using the
// new operator) has the same effect as invoking it as a constructor."
// https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Function
}
gen.eof = eof;
return gen;
}
codegen.supported = false; try { codegen.supported = codegen("a","b")("return a-b").eof()(2,1) === 1; } catch (e) {} // eslint-disable-line no-empty
codegen.verbose = false;
codegen.encode = require(4);
codegen.decode = require(3);
codegen.verify = require(5);
},{"23":23,"3":3,"4":4,"5":5}],3:[function(require,module,exports){
"use strict";
/**
* Wire format decoder using code generation on top of reflection that also provides a fallback.
* @exports codegen.decode
* @namespace
*/
var decode = exports;
var Enum = require(7),
Reader = require(17),
types = require(22),
util = require(23),
codegen = require(2);
/**
* Decodes a message of `this` message's type.
* @param {Reader|Uint8Array} readerOrBuffer Reader or buffer to decode from
* @param {number} [length] Length of the message, if known beforehand
* @returns {Prototype} Populated runtime message
* @this Type
*/
decode.fallback = function decode_fallback(readerOrBuffer, length) {
/* eslint-disable no-invalid-this, block-scoped-var, no-redeclare */
var fields = this.getFieldsById(),
reader = readerOrBuffer instanceof Reader ? readerOrBuffer : Reader.create(readerOrBuffer),
limit = length === undefined ? reader.len : reader.pos + length,
message = new (this.getCtor())();
while (reader.pos < limit) {
var tag = reader.tag(),
field = fields[tag.id].resolve(),
type = field.resolvedType instanceof Enum ? "uint32" : field.type;
// Known fields
if (field) {
// Map fields
if (field.map) {
var keyType = field.resolvedKeyType /* only valid is enum */ ? "uint32" : field.keyType,
length = reader.uint32();
var map = message[field.name] = {};
if (length) {
length += reader.pos;
var ks = [], vs = [];
while (reader.pos < length) {
if (reader.tag().id === 1)
ks[ks.length] = reader[keyType]();
else if (types.basic[type] !== undefined)
vs[vs.length] = reader[type]();
else
vs[vs.length] = field.resolvedType.decode(reader, reader.uint32());
}
for (var i = 0; i < ks.length; ++i)
map[typeof ks[i] === 'object' ? util.longToHash(ks[i]) : ks[i]] = vs[i];
}
// Repeated fields
} else if (field.repeated) {
var values = message[field.name] && message[field.name].length ? message[field.name] : message[field.name] = [];
// Packed
if (field.packed && types.packed[type] !== undefined && tag.wireType === 2) {
var plimit = reader.uint32() + reader.pos;
while (reader.pos < plimit)
values[values.length] = reader[type]();
// Non-packed
} else if (types.basic[type] !== undefined)
values[values.length] = reader[type]();
else
values[values.length] = field.resolvedType.decode(reader, reader.uint32());
// Non-repeated
} else if (types.basic[type] !== undefined)
message[field.name] = reader[type]();
else
message[field.name] = field.resolvedType.decode(reader, reader.uint32());
// Unknown fields
} else
reader.skipType(tag.wireType);
}
return message;
/* eslint-enable no-invalid-this, block-scoped-var, no-redeclare */
};
/**
* Generates a decoder specific to the specified message type, with an identical signature to {@link codegen.decode.fallback}.
* @param {Type} mtype Message type
* @returns {CodegenInstance} {@link codegen|Codegen} instance
*/
decode.generate = function decode_generate(mtype) {
/* eslint-disable no-unexpected-multiline */
var fields = mtype.getFieldsArray();
var gen = codegen("r", "l")
("r instanceof Reader||(r=Reader.create(r))")
("var c=l===undefined?r.len:r.pos+l,m=new(this.getCtor())")
("while(r.pos<c){")
("var t=r.tag()")
("switch(t.id){");
for (var i = 0; i < fields.length; ++i) {
var field = fields[i].resolve(),
type = field.resolvedType instanceof Enum ? "uint32" : field.type,
prop = util.safeProp(field.name);
gen
("case %d:", field.id);
if (field.map) {
var keyType = field.resolvedKeyType /* only valid is enum */ ? "uint32" : field.keyType;
gen
("var n=r.uint32(),o={}")
("if(n){")
("n+=r.pos")
("var k=[],v=[]")
("while(r.pos<n){")
("if(r.tag().id===1)")
("k[k.length]=r.%s()", keyType);
if (types.basic[type] !== undefined) gen
("else")
("v[v.length]=r.%s()", type);
else gen
("else")
("v[v.length]=types[%d].decode(r,r.uint32())", i, i);
gen
("}")
("for(var i=0;i<k.length;++i)")
("o[typeof(k[i])==='object'?util.longToHash(k[i]):k[i]]=v[i]")
("}")
("m%s=o", prop);
} else if (field.repeated) { gen
("m%s&&m%s.length?m%s:m%s=[]", prop, prop, prop, prop);
if (field.packed && types.packed[type] !== undefined) { gen
("if(t.wireType===2){")
("var e=r.uint32()+r.pos")
("while(r.pos<e)")
("m%s[m%s.length]=r.%s()", prop, prop, type)
("}else");
}
if (types.basic[type] !== undefined) gen
("m%s[m%s.length]=r.%s()", prop, prop, type);
else gen
("m%s[m%s.length]=types[%d].decode(r,r.uint32())", prop, prop, i, i);
} else if (types.basic[type] !== undefined) { gen
("m%s=r.%s()", prop, type);
} else { gen
("m%s=types[%d].decode(r,r.uint32())", prop, i, i);
} gen
("break");
} return gen
("default:")
("r.skipType(t.wireType)")
("break")
("}")
("}")
("return m");
/* eslint-enable no-unexpected-multiline */
};
},{"17":17,"2":2,"22":22,"23":23,"7":7}],4:[function(require,module,exports){
"use strict";
/**
* Wire format encoder using code generation on top of reflection that also provides a fallback.
* @exports codegen.encode
* @namespace
*/
var encode = exports;
var Enum = require(7),
Writer = require(27),
types = require(22),
util = require(23),
codegen = require(2);
/**
* Encodes a message of `this` message's type.
* @param {Prototype|Object} message Runtime message or plain object to encode
* @param {Writer} [writer] Writer to encode to
* @returns {Writer} writer
* @this Type
*/
encode.fallback = function encode_fallback(message, writer) {
/* eslint-disable block-scoped-var, no-redeclare */
if (!writer)
writer = Writer.create();
var fields = this.getFieldsArray(), fi = 0;
while (fi < fields.length) {
var field = fields[fi++].resolve(),
type = field.resolvedType instanceof Enum ? "uint32" : field.type,
wireType = types.basic[type];
// Map fields
if (field.map) {
var keyType = field.resolvedKeyType /* only valid is enum */ ? "uint32" : field.keyType;
var value, keys;
if ((value = message[field.name]) && (keys = Object.keys(value)).length) {
writer.fork();
for (var i = 0; i < keys.length; ++i) {
writer.tag(1, types.mapKey[keyType])[keyType](keys[i]);
if (wireType !== undefined)
writer.tag(2, wireType)[type](value[keys[i]]);
else
field.resolvedType.encode(value[keys[i]], writer.tag(2,2).fork()).ldelim();
}
writer.ldelim(field.id);
}
// Repeated fields
} else if (field.repeated) {
var values = message[field.name];
if (values && values.length) {
// Packed repeated
if (field.packed && types.packed[type] !== undefined) {
writer.fork();
var i = 0;
while (i < values.length)
writer[type](values[i++]);
writer.ldelim(field.id);
// Non-packed
} else {
var i = 0;
if (wireType !== undefined)
while (i < values.length)
writer.tag(field.id, wireType)[type](values[i++]);
else
while (i < values.length)
field.resolvedType.encode(values[i++], writer.tag(field.id,2).fork()).ldelim();
}
}
// Non-repeated
} else {
var value = message[field.name];
if (field.required || value !== undefined && field.long ? util.longNeq(value, field.defaultValue) : value !== field.defaultValue) {
if (wireType !== undefined)
writer.tag(field.id, wireType)[type](value);
else {
field.resolvedType.encode(value, writer.fork());
if (writer.len || field.required)
writer.ldelim(field.id);
else
writer.reset();
}
}
}
}
return writer;
/* eslint-enable block-scoped-var, no-redeclare */
};
/**
* Generates an encoder specific to the specified message type, with an identical signature to {@link codegen.encode.fallback}.
* @param {Type} mtype Message type
* @returns {CodegenInstance} {@link codegen|Codegen} instance
*/
encode.generate = function encode_generate(mtype) {
/* eslint-disable no-unexpected-multiline */
var fields = mtype.getFieldsArray();
var gen = codegen("m", "w")
("w||(w=Writer.create())");
for (var i = 0; i < fields.length; ++i) {
var field = fields[i].resolve(),
type = field.resolvedType instanceof Enum ? "uint32" : field.type,
wireType = types.basic[type],
prop = util.safeProp(field.name);
// Map fields
if (field.map) {
var keyType = field.resolvedKeyType /* only valid is enum */ ? "uint32" : field.keyType,
keyWireType = types.mapKey[keyType];
gen
("if(m%s){", prop)
("w.fork()")
("for(var i=0,ks=Object.keys(m%s);i<ks.length;++i){", prop)
("w.tag(1,%d).%s(ks[i])", keyWireType, keyType);
if (wireType !== undefined) gen
("w.tag(2,%d).%s(m%s[ks[i]])", wireType, type, prop);
else gen
("types[%d].encode(m%s[ks[i]],w.tag(2,2).fork()).ldelim()", i, prop);
gen
("}")
("w.len&&w.ldelim(%d)||w.reset()", field.id)
("}");
// Repeated fields
} else if (field.repeated) {
// Packed repeated
if (field.packed && types.packed[type] !== undefined) { gen
("if(m%s&&m%s.length){", prop, prop)
("w.fork()")
("for(var i=0;i<m%s.length;++i)", prop)
("w.%s(m%s[i])", type, prop)
("w.ldelim(%d)", field.id)
("}");
// Non-packed
} else { gen
("if(m%s)", prop)
("for(var i=0;i<m%s.length;++i)", prop);
if (wireType !== undefined) gen
("w.tag(%d,%d).%s(m%s[i])", field.id, wireType, type, prop);
else gen
("types[%d].encode(m%s[i],w.tag(%d,2).fork()).ldelim()", i, prop, field.id);
}
// Non-repeated
} else {
if (!field.required) {
if (field.long) gen
("if(m%s!==undefined&&util.longNeq(m%s,%j))", prop, prop, field.defaultValue);
else gen
("if(m%s!==undefined&&m%s!==%j)", prop, prop, field.defaultValue);
}
if (wireType !== undefined) gen
("w.tag(%d,%d).%s(m%s)", field.id, wireType, type, prop);
else if (field.required) gen
("types[%d].encode(m%s,w.tag(%d,2).fork()).ldelim()", i, prop, field.id);
else gen
("types[%d].encode(m%s,w.fork()).len&&w.ldelim(%d)||w.reset()", i, prop, field.id);
}
}
return gen
("return w");
/* eslint-enable no-unexpected-multiline */
};
},{"2":2,"22":22,"23":23,"27":27,"7":7}],5:[function(require,module,exports){
"use strict";
/**
* Runtime message verifier using code generation on top of reflection that also provides a fallback.
* @exports codegen.verify
* @namespace
*/
var verify = exports;
var Enum = require(7),
Type = require(21),
util = require(23),
codegen = require(2);
/**
* Verifies a runtime message of `this` message type.
* @param {Prototype|Object} message Runtime message or plain object to verify
* @returns {?string} `null` if valid, otherwise the reason why it is not
* @this {Type}
*/
verify.fallback = function verify_fallback(message) {
var fields = this.getFieldsArray(),
i = 0;
while (i < fields.length) {
var field = fields[i++].resolve(),
value = message[field.name];
if (value === undefined) {
if (field.required)
return "missing required field " + field.name + " in " + this.getFullName();
} else if (field.resolvedType instanceof Enum && field.resolvedType.getValuesById()[value] === undefined) {
return "invalid enum value " + field.name + " = " + value + " in " + this.getFullName();
} else if (field.resolvedType instanceof Type) {
if (!value && field.required)
return "missing required field " + field.name + " in " + this.getFullName();
var reason;
if ((reason = field.resolvedType.verify(value)) !== null)
return reason;
}
}
return null;
};
/**
* Generates a verifier specific to the specified message type, with an identical signature to {@link codegen.verify.fallback}.
* @param {Type} mtype Message type
* @returns {CodegenInstance} {@link codegen|Codegen} instance
*/
verify.generate = function verify_generate(mtype) {
/* eslint-disable no-unexpected-multiline */
var fields = mtype.getFieldsArray();
var gen = codegen("m");
var hasReasonVar = false;
for (var i = 0; i < fields.length; ++i) {
var field = fields[i].resolve(),
prop = util.safeProp(field.name);
if (field.required) { gen
("if(m%s===undefined)", prop)
("return 'missing required field %s in %s'", field.name, mtype.getFullName());
} else if (field.resolvedType instanceof Enum) {
var values = util.toArray(field.resolvedType.values); gen
("switch(m%s){", prop)
("default:")
("return 'invalid enum value %s = '+m%s+' in %s'", field.name, prop, mtype.getFullName());
for (var j = 0, l = values.length; j < l; ++j) gen
("case %d:", values[j]); gen
("}");
} else if (field.resolvedType instanceof Type) {
if (field.required) gen
("if(!m%s)", prop)
("return 'missing required field %s in %s'", field.name, mtype.getFullName());
if (!hasReasonVar) { gen("var r"); hasReasonVar = true; } gen
("if((r=types[%d].verify(m%s))!==null)", i, prop)
("return r");
}
}
return gen
("return null");
/* eslint-enable no-unexpected-multiline */
};
},{"2":2,"21":21,"23":23,"7":7}],6:[function(require,module,exports){
"use strict";
module.exports = common;
/**
* Provides common type definitions.
* Can also be used to provide additional google types or your own custom types.
* @param {string} name Short name as in `google/protobuf/[name].proto` or full file name
* @param {Object} json JSON definition within `google.protobuf` if a short name, otherwise the file's root definition
* @returns {undefined}
* @property {Object} google/protobuf/any.proto Any
* @property {Object} google/protobuf/duration.proto Duration
* @property {Object} google/protobuf/empty.proto Empty
* @property {Object} google/protobuf/struct.proto Struct, Value, NullValue and ListValue
* @property {Object} google/protobuf/timestamp.proto Timestamp
*/
function common(name, json) {
if (!/\/|\./.test(name)) {
name = "google/protobuf/" + name + ".proto";
json = { nested: { google: { nested: { protobuf: { nested: json } } } } };
}
common[name] = json;
}
// Not provided because of limited use (feel free to discuss or to provide yourself):
// - google/protobuf/descriptor.proto
// - google/protobuf/field_mask.proto
// - google/protobuf/source_context.proto
// - google/protobuf/type.proto
// - google/protobuf/wrappers.proto
common("any", {
Any: {
fields: {
type_url: {
type: "string",
id: 1
},
value: {
type: "bytes",
id: 2
}
}
}
});
var timeType;
common("duration", {
Duration: timeType = {
fields: {
seconds: {
type: "int64",
id: 1
},
nanos: {
type: "int32",
id: 2
}
}
}
});
common("timestamp", {
Timestamp: timeType
});
common("empty", {
Empty: {
fields: {}
}
});
common("struct", {
Struct: {
fields: {
fields: {
keyType: "string",
type: "Value",
id: 1
}
}
},
Value: {
oneofs: {
kind: {
oneof: [ "nullValue", "numberValue", "stringValue", "boolValue", "structValue", "listValue" ]
}
},
fields: {
nullValue: {
type: "NullValue",
id: 1
},
numberValue: {
type: "double",
id: 2
},
stringValue: {
type: "string",
id: 3
},
boolValue: {
type: "bool",
id: 4
},
structValue: {
type: "Struct",
id: 5
},
listValue: {
type: "ListValue",
id: 6
}
}
},
NullValue: {
values: {
NULL_VALUE: 0
}
},
ListValue: {
fields: {
values: {
rule: "repeated",
type: "Value",
id: 1
}
}
}
});
},{}],7:[function(require,module,exports){
"use strict";
module.exports = Enum;
var ReflectionObject = require(13);
/** @alias Enum.prototype */
var EnumPrototype = ReflectionObject.extend(Enum);
var util = require(23);
var _TypeError = util._TypeError;
/**
* Constructs a new enum.
* @classdesc Reflected enum.
* @extends ReflectionObject
* @constructor
* @param {string} name Unique name within its namespace
* @param {Object.<string,number>} [values] Enum values as an object, by name
* @param {Object} [options] Declared options
*/
function Enum(name, values, options) {
ReflectionObject.call(this, name, options);
/**
* Enum values by name.
* @type {Object.<string,number>}
*/
this.values = values || {}; // toJSON, marker
/**
* Cached values by id.
* @type {?Object.<number,string>}
* @private
*/
this._valuesById = null;
}
util.props(EnumPrototype, {
/**
* Enum values by id.
* @name Enum#valuesById
* @type {Object.<number,string>}
* @readonly
*/
valuesById: {
get: function getValuesById() {
if (!this._valuesById) {
this._valuesById = {};
Object.keys(this.values).forEach(function(name) {
var id = this.values[name];
if (this._valuesById[id])
throw Error("duplicate id " + id + " in " + this);
this._valuesById[id] = name;
}, this);
}
return this._valuesById;
}
}
/**
* Gets this enum's values by id. This is an alias of {@link Enum#valuesById}'s getter for use within non-ES5 environments.
* @name Enum#getValuesById
* @function
* @returns {Object.<number,string>}
*/
});
function clearCache(enm) {
enm._valuesById = null;
return enm;
}
/**
* Tests if the specified JSON object describes an enum.
* @param {*} json JSON object to test
* @returns {boolean} `true` if the object describes an enum
*/
Enum.testJSON = function testJSON(json) {
return Boolean(json && json.values);
};
/**
* Creates an enum from JSON.
* @param {string} name Enum name
* @param {Object.<string,*>} json JSON object
* @returns {Enum} Created enum
* @throws {TypeError} If arguments are invalid
*/
Enum.fromJSON = function fromJSON(name, json) {
return new Enum(name, json.values, json.options);
};
/**
* @override
*/
EnumPrototype.toJSON = function toJSON() {
return {
options : this.options,
values : this.values
};
};
/**
* Adds a value to this enum.
* @param {string} name Value name
* @param {number} id Value id
* @returns {Enum} `this`
* @throws {TypeError} If arguments are invalid
* @throws {Error} If there is already a value with this name or id
*/
EnumPrototype.add = function(name, id) {
if (!util.isString(name))
throw _TypeError("name");
if (!util.isInteger(id) || id < 0)
throw _TypeError("id", "a non-negative integer");
if (this.values[name] !== undefined)
throw Error('duplicate name "' + name + '" in ' + this);
if (this.getValuesById()[id] !== undefined)
throw Error("duplicate id " + id + " in " + this);
this.values[name] = id;
return clearCache(this);
};
/**
* Removes a value from this enum
* @param {string} name Value name
* @returns {Enum} `this`
* @throws {TypeError} If arguments are invalid
* @throws {Error} If `name` is not a name of this enum
*/
EnumPrototype.remove = function(name) {
if (!util.isString(name))
throw _TypeError("name");
if (this.values[name] === undefined)
throw Error('"' + name + '" is not a name of ' + this);
delete this.values[name];
return clearCache(this);
};
},{"13":13,"23":23}],8:[function(require,module,exports){
"use strict";
module.exports = Field;
var ReflectionObject = require(13);
/** @alias Field.prototype */
var FieldPrototype = ReflectionObject.extend(Field);
var Type = require(21),
Enum = require(7),
MapField = require(10),
types = require(22),
util = require(23);
var _TypeError = util._TypeError;
/**
* Constructs a new message field. Note that {@link MapField|map fields} have their own class.
* @classdesc Reflected message field.