/
protobuf.js
5783 lines (5153 loc) · 167 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.0.0 (c) 2016 Daniel Wirtz
* Compiled Tue, 29 Nov 2016 21:14:25 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 = 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
}
}
}
});
},{}],3:[function(require,module,exports){
"use strict";
module.exports = Decoder;
var Enum = require(5),
Reader = require(15),
types = require(20),
util = require(21);
/**
* Constructs a new decoder for the specified message type.
* @classdesc Wire format decoder using code generation on top of reflection.
* @constructor
* @param {Type} type Message type
*/
function Decoder(type) {
/**
* Message type.
* @type {Type}
*/
this.type = type;
}
/** @alias Decoder.prototype */
var DecoderPrototype = Decoder.prototype;
// This is here to mimic Type so that fallback functions work without having to bind()
Object.defineProperties(DecoderPrototype, {
/**
* Fields of this decoder's message type by id for lookups.
* @name Decoder#fieldsById
* @type {Object.<number,Field>}
* @readonly
*/
fieldsById: {
get: DecoderPrototype.getFieldsById = function getFieldsById() {
return this.type.getFieldsById();
}
},
/**
* With this decoder's message type registered constructor, if any registered, otherwise a generic constructor.
* @name Decoder#ctor
* @type {Prototype}
*/
ctor: {
get: DecoderPrototype.getCtor = function getCtor() {
return this.type.getCtor();
}
}
});
/**
* Decodes a message of this decoder's message type.
* @param {Reader} reader Reader to decode from
* @param {number} [length] Length of the message, if known beforehand
* @returns {Prototype} Populated runtime message
*/
DecoderPrototype.decode = function decode_fallback(reader, length) { // codegen reference and fallback
/* eslint-disable no-invalid-this, block-scoped-var, no-redeclare */
var fields = this.getFieldsById(),
reader = reader instanceof Reader ? reader : Reader(reader),
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] = []);
// 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 this decoder's message type.
* @returns {function} Decoder function with an identical signature to {@link Decoder#decode}
*/
DecoderPrototype.generate = function generate() {
/* eslint-disable no-unexpected-multiline */
var fields = this.type.getFieldsArray();
var gen = util.codegen("r", "l")
("r instanceof Reader||(r=Reader(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=[])", 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");
} gen
("default:")
("r.skipType(t.wireType)")
("break")
("}")
("}")
("return m");
return gen
.eof(this.type.getFullName() + "$decode", {
Reader : Reader,
types : fields.map(function(fld) { return fld.resolvedType; }),
util : util.toHash
});
/* eslint-enable no-unexpected-multiline */
};
},{"15":15,"20":20,"21":21,"5":5}],4:[function(require,module,exports){
"use strict";
module.exports = Encoder;
var Enum = require(5),
Writer = require(25),
types = require(20),
util = require(21);
/**
* Constructs a new encoder for the specified message type.
* @classdesc Wire format encoder using code generation on top of reflection
* @constructor
* @param {Type} type Message type
*/
function Encoder(type) {
/**
* Message type.
* @type {Type}
*/
this.type = type;
}
/** @alias Encoder.prototype */
var EncoderPrototype = Encoder.prototype;
// This is here to mimic Type so that fallback functions work without having to bind()
Object.defineProperties(EncoderPrototype, {
/**
* Fields of this encoder's message type as an array for iteration.
* @name Encoder#fieldsArray
* @type {Field[]}
* @readonly
*/
fieldsArray: {
get: EncoderPrototype.getFieldsArray = function getFieldsArray() {
return this.type.getFieldsArray();
}
}
});
/**
* Encodes a message of this encoder's message type.
* @param {Prototype|Object} message Runtime message or plain object to encode
* @param {Writer} [writer] Writer to encode to
* @returns {Writer} writer
*/
EncoderPrototype.encode = function encode_fallback(message, writer) { // codegen reference and fallback
/* eslint-disable block-scoped-var, no-redeclare */
if (!writer)
writer = Writer();
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 this encoder's message type.
* @returns {function} Encoder function with an identical signature to {@link Encoder#encode}
*/
EncoderPrototype.generate = function generate() {
/* eslint-disable no-unexpected-multiline */
var fields = this.type.getFieldsArray();
var gen = util.codegen("m", "w")
("w||(w=Writer())");
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")
.eof(this.type.getFullName() + "$encode", {
Writer : Writer,
types : fields.map(function(fld) { return fld.resolvedType; }),
util : util
});
/* eslint-enable no-unexpected-multiline */
};
},{"20":20,"21":21,"25":25,"5":5}],5:[function(require,module,exports){
"use strict";
module.exports = Enum;
var ReflectionObject = require(11);
/** @alias Enum.prototype */
var EnumPrototype = ReflectionObject.extend(Enum);
var util = require(21);
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;
}
Object.defineProperties(EnumPrototype, {
/**
* Enum values by id.
* @name Enum#valuesById
* @type {Object.<number,string>}
* @readonly
*/
valuesById: {
get: EnumPrototype.getValuesById = 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;
}
}
});
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);
};
},{"11":11,"21":21}],6:[function(require,module,exports){
"use strict";
module.exports = Field;
var ReflectionObject = require(11);
/** @alias Field.prototype */
var FieldPrototype = ReflectionObject.extend(Field);
var Type = require(19),
Enum = require(5),
MapField = require(8),
types = require(20),
util = require(21);
var _TypeError = util._TypeError;
/**
* Constructs a new message field. Note that {@link MapField|map fields} have their own class.
* @classdesc Reflected message field.
* @extends ReflectionObject
* @constructor
* @param {string} name Unique name within its namespace
* @param {number} id Unique id within its namespace
* @param {string} type Value type
* @param {string} [rule=optional] Field rule
* @param {string} [extend] Extended type if different from parent
* @param {Object} [options] Declared options
*/
function Field(name, id, type, rule, extend, options) {
if (util.isObject(rule)) {
options = rule;
rule = extend = undefined;
} else if (util.isObject(extend)) {
options = extend;
extend = undefined;
}
ReflectionObject.call(this, name, options);
if (!util.isInteger(id) || id < 0)
throw _TypeError("id", "a non-negative integer");
if (!util.isString(type))
throw _TypeError("type");
if (extend !== undefined && !util.isString(extend))
throw _TypeError("extend");
if (rule !== undefined && !/^required|optional|repeated$/.test(rule = rule.toString().toLowerCase()))
throw _TypeError("rule", "a valid rule string");
/**
* Field rule, if any.
* @type {string|undefined}
*/
this.rule = rule && rule !== 'optional' ? rule : undefined; // toJSON
/**
* Field type.
* @type {string}
*/
this.type = type; // toJSON
/**
* Unique field id.
* @type {number}
*/
this.id = id; // toJSON, marker
/**
* Extended type if different from parent.
* @type {string|undefined}
*/
this.extend = extend || undefined; // toJSON
/**
* Whether this field is required.
* @type {boolean}
*/
this.required = rule === "required";
/**
* Whether this field is optional.
* @type {boolean}
*/
this.optional = !this.required;
/**
* Whether this field is repeated.
* @type {boolean}
*/
this.repeated = rule === "repeated";
/**
* Whether this field is a map or not.
* @type {boolean}
*/
this.map = false;
/**
* Message this field belongs to.
* @type {?Type}
*/
this.message = null;
/**
* OneOf this field belongs to, if any,
* @type {?OneOf}
*/
this.partOf = null;
/**
* The field's default value. Only relevant when working with proto2.
* @type {*}
*/
this.defaultValue = null;
/**
* Whether this field's value should be treated as a long.
* @type {boolean}
*/
this.long = util.Long ? types.long[type] !== undefined : false;
/**
* Resolved type if not a basic type.
* @type {?(Type|Enum)}
*/
this.resolvedType = null;
/**
* Sister-field within the extended type if a declaring extension field.
* @type {?Field}
*/
this.extensionField = null;
/**
* Sister-field within the declaring namespace if an extended field.
* @type {?Field}
*/
this.declaringField = null;
/**
* Internally remembers whether this field is packed.
* @type {?boolean}
* @private
*/
this._packed = null;
}
Object.defineProperties(FieldPrototype, {
/**
* Determines whether this field is packed. Only relevant when repeated and working with proto2.
* @name Field#packed
* @type {boolean}
* @readonly
*/
packed: {
get: FieldPrototype.isPacked = function() {
if (this._packed === null)
this._packed = this.getOption("packed") !== false;
return this._packed;
}
}
});
/**
* @override
*/