forked from protobufjs/protobuf.js
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProtoBuf.min.js
64 lines (64 loc) · 29.6 KB
/
ProtoBuf.min.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
/*
ProtoBuf.js (c) 2013 Daniel Wirtz <[email protected]>
Released under the Apache License, Version 2.0
see: https://github.com/dcodeIO/ProtoBuf.js for details
*/
(function(p){function q(n){if(!n||!n.zigZagEncode32)throw Error("ProtoBuf.js requires ByteBuffer.js >=1.1.0: Get it at https://github.com/dcodeIO/ByteBuffer.js");var f={VERSION:"0.12.8",WIRE_TYPES:{}};f.WIRE_TYPES.VARINT=0;f.WIRE_TYPES.BITS64=1;f.WIRE_TYPES.LDELIM=2;f.WIRE_TYPES.STARTGROUP=3;f.WIRE_TYPES.ENDGROUP=4;f.WIRE_TYPES.BITS32=5;f.TYPES={int32:{name:"int32",wireType:f.WIRE_TYPES.VARINT},uint32:{name:"uint32",wireType:f.WIRE_TYPES.VARINT},sint32:{name:"sint32",wireType:f.WIRE_TYPES.VARINT},
int64:{name:"int64",wireType:f.WIRE_TYPES.VARINT},uint64:{name:"uint64",wireType:f.WIRE_TYPES.VARINT},sint64:{name:"sint64",wireType:f.WIRE_TYPES.VARINT},bool:{name:"bool",wireType:f.WIRE_TYPES.VARINT},"double":{name:"double",wireType:f.WIRE_TYPES.BITS64},string:{name:"string",wireType:f.WIRE_TYPES.LDELIM},bytes:{name:"bytes",wireType:f.WIRE_TYPES.LDELIM},fixed32:{name:"fixed32",wireType:f.WIRE_TYPES.BITS32},sfixed32:{name:"sfixed32",wireType:f.WIRE_TYPES.BITS32},fixed64:{name:"fixed64",wireType:f.WIRE_TYPES.BITS64},
sfixed64:{name:"sfixed64",wireType:f.WIRE_TYPES.BITS64},"float":{name:"float",wireType:f.WIRE_TYPES.BITS32},"enum":{name:"enum",wireType:f.WIRE_TYPES.VARINT},message:{name:"message",wireType:f.WIRE_TYPES.LDELIM}};f.Long=n.Long;f.Util=function(){Object.create||(Object.create=function(b){function c(){}if(1<arguments.length)throw Error("Object.create implementation only accepts the first parameter.");c.prototype=b;return new c});var c={};c.IS_NODE=("undefined"==typeof window||!window.window)&&"function"==
typeof require;c.XHR=function(){for(var b=[function(){return new XMLHttpRequest},function(){return new ActiveXObject("Msxml2.XMLHTTP")},function(){return new ActiveXObject("Msxml3.XMLHTTP")},function(){return new ActiveXObject("Microsoft.XMLHTTP")}],c=null,h=0;h<b.length;h++){try{c=b[h]()}catch(a){continue}break}if(!c)throw Error("XMLHttpRequest is not supported");return c};c.fetch=function(b,m){m&&"function"!=typeof m&&(m=null);if(c.IS_NODE)if(m)require("fs").readFile(b,function(a,b){a?m(null):m(""+
b)});else try{return require("fs").readFileSync(b)}catch(h){return null}else{var a=c.XHR();a.open("GET",b,m?!0:!1);a.setRequestHeader("User-Agent","XMLHTTP/1.0");a.setRequestHeader("Accept","text/plain");if(m)a.onreadystatechange=function(){4==a.readyState&&(200==a.status?m(a.responseText):m(null))},4!=a.readyState&&a.send(null);else return a.send(null),a.responseText}};return c}();f.Lang={OPEN:"{",CLOSE:"}",OPTOPEN:"[",OPTCLOSE:"]",OPTEND:",",EQUAL:"=",END:";",DELIM:/[\s\{\}=;\[\],"\(\)]/g,KEYWORD:/package|option|import|message|enum|extend|service/,
RULE:/required|optional|repeated/,TYPE:/double|float|int32|uint32|sint32|int64|uint64|sint64|fixed32|sfixed32|fixed64|sfixed64|bool|string|bytes/,NAME:/[a-zA-Z][a-zA-Z_0-9]*/,TYPEDEF:/[a-zA-Z](\.?[a-zA-Z_0-9])*/,TYPEREF:/\.?[a-zA-Z](\.?[a-zA-Z_0-9])*/,NUMBER:/^-?([1-9][0-9]*)|0$/,ID:/[0-9]+/,WHITESPACE:/\s/,STRING:/"([^"\\]*(\\.[^"\\]*)*)"/g,STRINGOPEN:'"',STRINGCLOSE:'"',COPTOPEN:"(",COPTCLOSE:")"};f.DotProto={};f.DotProto.Tokenizer=function(c){var b=function(b){this.source=""+b;this.index=0;this.stack=
[];this.readingString=!1};b.prototype._readString=function(){c.STRING.lastIndex=this.index-1;var b;if(null!==(b=c.STRING.exec(this.source)))return b=b[1],this.index=c.STRING.lastIndex,this.stack.push(c.STRINGCLOSE),b;throw Error("Illegal string value at index "+this.index);};b.prototype.next=function(){if(0<this.stack.length)return this.stack.pop();if(this.index>=this.source.length)return null;if(this.readingString)return this.readingString=!1,this._readString();var b;do{for(b=!1;c.WHITESPACE.test(this.source.charAt(this.index));)if(this.index++,
this.index==this.source.length)return null;if("/"==this.source.charAt(this.index))if("/"==this.source.charAt(++this.index)){for(;"\n"!=this.source.charAt(this.index);)if(this.index++,this.index==this.source.length)return null;this.index++;b=!0}else if("*"==this.source.charAt(this.index)){for(b="";"*/"!=b+(b=this.source.charAt(this.index));)if(this.index++,this.index==this.source.length)return null;this.index++;b=!0}else throw Error("Invalid comment: /"+this.source.charAt(this.index)+" ('/' or '*' expected)");
}while(b);if(this.index==this.source.length)return null;b=this.index;c.DELIM.lastIndex=0;if(c.DELIM.test(this.source.charAt(b)))b++;else for(b++;b<this.source.length&&!c.DELIM.test(this.source.charAt(b));)b++;b=this.source.substring(this.index,this.index=b);b==c.STRINGOPEN&&(this.readingString=!0);return b};b.prototype.toString=function(){return"Tokenizer("+this.index+"/"+this.source.length+")"};return b}(f.Lang);f.DotProto.Parser=function(c,b,f){c=function(b){this.tn=new f(b)};c.prototype.parse=
function(){var b={name:"[ROOT]","package":null,messages:[],enums:[],imports:[],options:{}},a,d=!0;do{a=this.tn.next();if(null==a)break;if("package"==a){if(!d)throw Error("Illegal package definition: Must be declared before the first message or enum");if(null!==b["package"])throw Error("Illegal package definition: Package already declared");b["package"]=this._parsePackage(a)}else if("import"==a){if(!d)throw Error("Illegal import definition: Must be declared before the first message or enum");b.imports.push(this._parseImport(a))}else if("message"==
a)this._parseMessage(b,a),d=!1;else if("enum"==a)this._parseEnum(b,a),d=!1;else if("option"==a){if(!d)throw Error("Illegal option definition: Must be declared before the first message or enum");this._parseOption(b,a)}else if("extend"==a||"service"==a)this._parseIgnored(b,a);else throw Error("Illegal top level declaration: "+a);}while(1);delete b.name;return b};c.prototype._parsePackage=function(c){c=this.tn.next();if(!b.TYPEDEF.test(c))throw Error("Illegal package name: "+c);var a=c;c=this.tn.next();
if(c!=b.END)throw Error("Illegal end of package definition: "+c+" ('"+b.END+"' expected)");return a};c.prototype._parseImport=function(c){c=this.tn.next();"public"==c&&(c=this.tn.next());if(c!=b.STRINGOPEN)throw Error("Illegal begin of import value: "+c+" ('"+b.STRINGOPEN+"' expected)");var a=this.tn.next();c=this.tn.next();if(c!=b.STRINGCLOSE)throw Error("Illegal end of import value: "+c+" ('"+b.STRINGCLOSE+"' expected)");c=this.tn.next();if(c!=b.END)throw Error("Illegal end of import definition: "+
c+" ('"+b.END+"' expected)");return a};c.prototype._parseOption=function(c,a){a=this.tn.next();var d=!1;a==b.COPTOPEN&&(d=!0,a=this.tn.next());if(!b.NAME.test(a))throw Error("Illegal option name in message "+c.name+": "+a);var e=a;a=this.tn.next();if(d){if(a!=b.COPTCLOSE)throw Error("Illegal custom option name delimiter in message "+c.name+", option "+e+": "+a+" ('"+b.COPTCLOSE+"' expected)");a=this.tn.next()}if(a!=b.EQUAL)throw Error("Illegal option operator in message "+c.name+", option "+e+": "+
a+" ('"+b.EQUAL+"' expected)");a=this.tn.next();if(a==b.STRINGOPEN){if(d=this.tn.next(),a=this.tn.next(),a!=b.STRINGCLOSE)throw Error("Illegal end of option value in message "+c.name+", option "+e+": "+a+" ('"+b.STRINGCLOSE+"' expected)");}else if(b.NUMBER.test(a))d=parseInt(a);else if(b.NAME.test(a))d=a;else throw Error("Illegal option value in message "+c.name+", option "+e+": "+a);a=this.tn.next();if(a!=b.END)throw Error("Illegal end of option in message "+c.name+", option "+e+": "+a+" ('"+b.END+
"' expected)");c.options[e]=d};c.prototype._parseIgnored=function(c,a){var d=this.tn.next();if(!b.TYPEREF.test(d))throw Error("Illegal "+a+" type in "+c.name+": "+d);var e=d,d=this.tn.next();if(d!=b.OPEN)throw Error("Illegal OPEN in "+c.name+" after "+a+" "+e+": "+d);var g=1;do{d=this.tn.next();if(null===d)throw Error("Illegal nesting in "+c.name+", "+a+" "+e+": EOF");if(d==b.OPEN)g++;else if(d==b.CLOSE&&(g--,0==g))break}while(1)};c.prototype._parseMessage=function(c,a){var d={};a=this.tn.next();
if(!b.NAME.test(a))throw Error("Illegal message name"+(c?" in message "+c.name:"")+": "+a);d.name=a;a=this.tn.next();if(a!=b.OPEN)throw Error("Illegal OPEN after message "+d.name+": "+a+" ('"+b.OPEN+"' expected)");d.fields=[];d.enums=[];d.messages=[];d.options={};do if(a=this.tn.next(),a==b.CLOSE)break;else if(b.RULE.test(a))this._parseMessageField(d,a);else if("enum"==a)this._parseEnum(d,a);else if("message"==a)this._parseMessage(d,a);else if("option"==a)this._parseOption(d,a);else throw Error("Illegal token in message "+
d.name+": "+a+" (type or '"+b.CLOSE+"' expected)");while(1);c.messages.push(d);return d};c.prototype._parseMessageField=function(c,a){var d={};d.rule=a;a=this.tn.next();if(!b.TYPE.test(a)&&!b.TYPEREF.test(a))throw Error("Illegal field type in message "+c.name+": "+a);d.type=a;a=this.tn.next();if(!b.NAME.test(a))throw Error("Illegal field name in message "+c.name+": "+a);d.name=a;a=this.tn.next();if(a!=b.EQUAL)throw Error("Illegal field number operator in message "+c.name+"#"+d.name+": "+a+" ('"+b.EQUAL+
"' expected)");a=this.tn.next();if(!b.ID.test(a))throw Error("Illegal field number in message "+c.name+"#"+d.name+": "+a);d.id=parseInt(a,10);d.options={};a=this.tn.next();a==b.OPTOPEN&&(this._parseFieldOptions(c,d,a),a=this.tn.next());if(a!=b.END)throw Error("Illegal field delimiter in message "+c.name+"#"+d.name+": "+a+" ('"+b.END+"' expected)");c.fields.push(d)};c.prototype._parseFieldOptions=function(c,a,d){var e=!0;do{d=this.tn.next();if(d==b.OPTCLOSE)break;else if(d==b.OPTEND){if(e)throw Error("Illegal start of message field options in message "+
c.name+"#"+a.name+": "+d);d=this.tn.next()}this._parseFieldOption(c,a,d);e=!1}while(1)};c.prototype._parseFieldOption=function(c,a,d){var e=!1;d==b.COPTOPEN&&(d=this.tn.next(),e=!0);if(!b.NAME.test(d))throw Error("Illegal field option in message "+c.name+"#"+a.name+": "+d);var g=d;d=this.tn.next();if(e){if(d!=b.COPTCLOSE)throw Error("Illegal custom field option name delimiter in message "+c.name+"#"+a.name+": "+d+" (')' expected)");d=this.tn.next()}if(d!=b.EQUAL)throw Error("Illegal field option operation in message "+
c.name+"#"+a.name+": "+d+" ('=' expected)");d=this.tn.next();if(d==b.STRINGOPEN){if(e=this.tn.next(),d=this.tn.next(),d!=b.STRINGCLOSE)throw Error("Illegal end of field value in message "+c.name+"#"+a.name+", option "+g+": "+d+" ('"+b.STRINGCLOSE+"' expected)");}else if(b.NUMBER.test(d))e=parseFloat(d);else if(b.TYPEREF.test(d))e=d;else throw Error("Illegal field option value in message "+c.name+"#"+a.name+", option "+g+": "+d);a.options[g]=e};c.prototype._parseEnum=function(c,a){var d={};a=this.tn.next();
if(!b.NAME.test(a))throw Error("Illegal enum name in message "+c.name+": "+a);d.name=a;a=this.tn.next();if(a!=b.OPEN)throw Error("Illegal OPEN after enum "+d.name+": "+a);d.values=[];d.options={};do{a=this.tn.next();if(a==b.CLOSE)break;if("option"==a)this._parseOption(d,a);else{if(!b.NAME.test(a))throw Error("Illegal enum value name in enum "+d.name+": "+a);this._parseEnumValue(d,a)}}while(1);c.enums.push(d)};c.prototype._parseEnumValue=function(c,a){var d={};d.name=a;a=this.tn.next();if(a!=b.EQUAL)throw Error("Illegal enum value operator in enum "+
c.name+": "+a+" ('"+b.EQUAL+"' expected)");a=this.tn.next();if(!b.ID.test(a))throw Error("Illegal enum value value in enum "+c.name+": "+a);d.id=parseInt(a,10);c.values.push(d);a=this.tn.next();a==b.OPTOPEN&&(this._parseFieldOptions(c,{options:{}},a),a=this.tn.next());if(a!=b.END)throw Error("Illegal enum value delimiter in enum "+c.name+": "+a+" ('"+b.END+"' expected)");};c.prototype.toString=function(){return"Parser"};return c}(f,f.Lang,f.DotProto.Tokenizer);f.Reflect=function(c){var b={},f=function(a,
c){this.parent=a;this.name=c;this.resolvedType=null};f.prototype.toString=function(c){var k=this.name,d=this;do{d=d.parent;if(null==d)break;k=d.name+"."+k}while(1);c&&(this instanceof b.Message?k="Message "+k:this instanceof a.Field?k="Message.Field "+k:this instanceof e?k="Enum "+k:this instanceof e.Value?k="Enum.Value "+k:this instanceof h&&(k="Namespace "+k));return k};f.prototype.build=function(){throw Error(this.toString(!0)+" cannot be built directly");};b.T=f;var h=function(a,c,b){f.call(this,
a,c);this.children=[];this.options=b||{}};h.prototype=Object.create(f.prototype);h.prototype.getChildren=function(a){a=a||null;if(null==a)return this.children.slice();for(var c=[],b=0;b<this.children.length;b++)this.children[b]instanceof a&&c.push(this.children[b]);return c};h.prototype.addChild=function(a){if(this.hasChild(a.name))throw Error("Duplicate name in namespace "+this.toString(!0)+": "+a.name);this.children.push(a)};h.prototype.hasChild=function(c){var b;if("number"==typeof c)for(b=0;b<
this.children.length;b++){if(this.children[b]instanceof a.Field&&this.children[b].id==c)return!0}else for(b=0;b<this.children.length;b++)if(this.children[b].name==c)return!0;return!1};h.prototype.getChild=function(c){var b;if("number"==typeof c)for(b=0;b<this.children.length;b++){if(this.children[b]instanceof a.Field&&this.children[b].id==c)return this.children[b]}else for(b=0;b<this.children.length;b++)if(this.children[b].name==c)return this.children[b];return null};h.prototype.resolve=function(a){var c=
a.split("."),d=this,e=0;if(""==c[e]){for(;null!=d.parent;)d=d.parent;e++}do{do{d=d.getChild(c[e]);if(!d||!(d instanceof b.T)){d=null;break}e++}while(e<c.length);if(null!=d)break;if(null!==this.parent)return this.parent.resolve(a)}while(null!=d);return d};h.prototype.build=function(){for(var a={},c=this.getChildren(),b,d=0;d<c.length;d++)b=c[d],b instanceof h&&(a[b.name]=b.build());Object.defineProperty&&Object.defineProperty(a,"$options",{value:this.buildOpt(),enumerable:!1,configurable:!1,writable:!1});
return a};h.prototype.buildOpt=function(){for(var a={},c=Object.keys(this.options),b=0;b<c.length;b++)a[c[b]]=this.options[c[b]];return a};h.prototype.getOption=function(a){return"undefined"==typeof a?this.options:"undefined"!=typeof this.options[a]?this.options[a]:null};b.Namespace=h;var a=function(a,c,b){h.call(this,a,c,b);this.built=null};a.prototype=Object.create(h.prototype);a.prototype.build=function(){for(var d=function(a,b){var d=b.getChildren(a.Message.Field),g=eval("(function "+b.name+"() { this.__construct.apply(this, arguments); })");
g.prototype.__construct=function(a){var b,g;for(b=0;b<d.length;b++)g=d[b],this[g.name]=g.repeated?[]:null;for(b=0;b<d.length;b++)if(g=d[b],"undefined"!=typeof g.options["default"])try{this.set(g.name,g.options["default"])}catch(k){throw Error("[INTERNAL ERROR] "+k);}if(1==arguments.length&&"object"==typeof a&&"function"!=typeof a.encode&&!(a instanceof Array)&&!(a instanceof n)&&!(a instanceof ArrayBuffer)&&!(c.Long&&a instanceof c.Long)){g=Object.keys(a);for(b=0;b<g.length;b++)this.set(g[b],a[g[b]])}else for(b=
0;b<arguments.length;b++)b<d.length&&this.set(d[b].name,arguments[b])};g.prototype.add=function(c,d){var g=b.getChild(c);if(!g)throw Error(this+"#"+c+" is undefined");if(!(g instanceof a.Message.Field))throw Error(this+"#"+c+" is not a field: "+g.toString(!0));if(!g.repeated)throw Error(this+"#"+c+" is not a repeated field");null===this[g.name]&&(this[g.name]=[]);this[g.name].push(g.verifyValue(d,!0))};g.prototype.set=function(c,d){var g=b.getChild(c);if(!g)throw Error(this+"#"+c+" is not a field: undefined");
if(!(g instanceof a.Message.Field))throw Error(this+"#"+c+" is not a field: "+g.toString(!0));this[g.name]=g.verifyValue(d)};g.prototype.get=function(c){var g=b.getChild(c);if(!g||!(g instanceof a.Message.Field))throw Error(this+"#"+c+" is not a field: undefined");if(!(g instanceof a.Message.Field))throw Error(this+"#"+c+" is not a field: "+g.toString(!0));return this[g.name]};for(var k=0;k<d.length;k++)(function(a){var c=a.name.replace(/(_[a-zA-Z])/g,function(a){return a.toUpperCase().replace("_",
"")}),c=c.substring(0,1).toUpperCase()+c.substring(1),d=a.name.replace(/([A-Z])/g,function(a){return"_"+a});b.hasChild("set"+c)||(g.prototype["set"+c]=function(b){this.set(a.name,b)});b.hasChild("set_"+d)||(g.prototype["set_"+d]=function(b){this.set(a.name,b)});b.hasChild("get"+c)||(g.prototype["get"+c]=function(){return this.get(a.name)});b.hasChild("get_"+d)||(g.prototype["get_"+d]=function(){return this.get(a.name)})})(d[k]);g.prototype.encode=function(a){a=a||new n;var c=a.littleEndian,g=b.encode(this,
a.LE()).flip();a.littleEndian=c;return g};g.prototype.toArrayBuffer=function(){return this.encode().toArrayBuffer()};g.decode=function(a){a=a?a instanceof n?a:n.wrap(a):new n;var c=a.littleEndian,g=b.decode(a.LE());a.littleEndian=c;return g};g.prototype.toString=function(){return b.toString()};Object.defineProperty&&Object.defineProperty(g,"$options",{value:b.buildOpt(),enumerable:!1,configurable:!1,writable:!1});return g}(b,this),k=this.getChildren(),l=0;l<k.length;l++)if(k[l]instanceof e)d[k[l].name]=
k[l].build();else if(k[l]instanceof a)d[k[l].name]=k[l].build();else if(!(k[l]instanceof a.Field))throw Error("Illegal reflect child of "+this.toString(!0)+": "+k[l].toString(!0));return this.built=d};a.prototype.encode=function(b,c){for(var d=this.getChildren(a.Field),e=0;e<d.length;e++)d[e].encode(b.get(d[e].name),c);return c};a.prototype.decode=function(a,b){b=b||-1;for(var c=a.offset,d=new this.built;a.offset<c+b||-1==b&&0<a.remaining();){var e=a.readVarint32(),f=e&7,e=e>>3,h=this.getChild(e);
if(!h)throw Error("Illegal field id in "+this.toString(!0)+"#decode: "+e);h.repeated&&!h.options.packed?d.add(h.name,h.decode(f,a)):d.set(h.name,h.decode(f,a))}return d};b.Message=a;var d=function(a,b,c,d,e,h){f.call(this,a,d);this.required="required"==b;this.repeated="repeated"==b;this.type=c;this.id=e;this.options=h||{}};d.prototype=Object.create(f.prototype);d.prototype.verifyValue=function(a,b){b=b||!1;if(null===a){if(this.required)throw Error("Illegal value for "+this.toString(!0)+": "+a+" (required)");
return null}var d,f;if(this.repeated&&!b){a instanceof Array||(a=[a]);f=[];for(d=0;d<a.length;d++)f.push(this.verifyValue(a[d],!0));return f}if(!this.repeated&&a instanceof Array)throw Error("Illegal value for "+this.toString(!0)+": "+a+" (no array expected)");if(this.type==c.TYPES.int32||this.type==c.TYPES.sint32||this.type==c.TYPES.sfixed32)return parseInt(a,10)|0;if(this.type==c.TYPES.uint32||this.type==c.TYPES.fixed32)return parseInt(a,10)>>>0;if(c.Long){if(this.type==c.TYPES.int64||this.type==
c.TYPES.sint64||this.type==c.TYPES.sfixed64)return!("object"==typeof a&&a instanceof c.Long)?c.Long.fromNumber(a,!1):a.unsigned?a.toSigned():a;if(this.type==c.TYPES.uint64||this.type==c.TYPES.fixed64)return!("object"==typeof a&&a instanceof c.Long)?c.Long.fromNumber(a,!0):a.unsigned?a:a.toUnsigned()}if(this.type==c.TYPES.bool)return!!a;if(this.type==c.TYPES["float"]||this.type==c.TYPES["double"])return parseFloat(a);if(this.type==c.TYPES.string)return""+a;if(this.type==c.TYPES.bytes)return"object"==
typeof a&&a instanceof n?a:n.wrap(a);if(this.type==c.TYPES["enum"]){f=this.resolvedType.getChildren(e.Value);for(d=0;d<f.length;d++)if(f[d].name==a||f[d].id==a)return f[d].id;throw Error("Illegal value for "+this.toString(!0)+": "+a+" (not a valid enum value)");}if(this.type==c.TYPES.message){if("object"!=typeof a)throw Error("Illegal value for "+this.toString(!0)+": "+a+" (object expected)");return a instanceof this.resolvedType.built?a:new this.resolvedType.built(a)}throw Error("[INTERNAL ERROR] Illegal value for "+
this.toString(!0)+": "+a+" (undefined type "+this.type+")");};d.prototype.encode=function(a,b){a=this.verifyValue(a);if(null==this.type||"object"!=typeof this.type)throw Error("[INTERNAL ERROR] Unresolved type in "+this.toString(!0)+": "+this.type);if(null===a||this.repeated&&0==a.length)return b;try{if(this.repeated){var d;if(this.options.packed){b.writeVarint32(this.id<<3|c.WIRE_TYPES.LDELIM);b.ensureCapacity(b.offset+=1);var e=b.offset;for(d=0;d<a.length;d++)this.encodeValue(a[d],b);var f=b.offset-
e,h=n.calculateVarint32(f);if(1<h){var m=b.slice(e,b.offset),e=e+(h-1);b.offset=e;b.append(m)}b.writeVarint32(f,e-h)}else for(d=0;d<a.length;d++)b.writeVarint32(this.id<<3|this.type.wireType),this.encodeValue(a[d],b)}else b.writeVarint32(this.id<<3|this.type.wireType),this.encodeValue(a,b)}catch(p){throw b.littleEndian=le,Error("Illegal value for "+this.toString(!0)+": "+a+" ("+p+")");}return b};d.prototype.encodeValue=function(a,b){if(null!==a){if(this.type==c.TYPES.int32||this.type==c.TYPES.uint32)b.writeVarint32(a);
else if(this.type==c.TYPES.sint32)b.writeZigZagVarint32(a);else if(this.type==c.TYPES.fixed32)b.writeUint32(a);else if(this.type==c.TYPES.sfixed32)b.writeInt32(a);else if(this.type==c.TYPES.int64||this.type==c.TYPES.uint64)b.writeVarint64(a);else if(this.type==c.TYPES.sint64)b.writeZigZagVarint64(a);else if(this.type==c.TYPES.fixed64)b.writeUint64(a);else if(this.type==c.TYPES.sfixed64)b.writeInt64(a);else if(this.type==c.TYPES.bool)b.writeVarint32(a?1:0);else if(this.type==c.TYPES["enum"])b.writeVarint32(a);
else if(this.type==c.TYPES["float"])b.writeFloat32(a);else if(this.type==c.TYPES["double"])b.writeFloat64(a);else if(this.type==c.TYPES.string)b.writeVString(a);else if(this.type==c.TYPES.bytes)a.offset>a.length&&(b=b.clone().flip()),b.writeVarint32(a.remaining()),b.append(a);else if(this.type==c.TYPES.message){var d=b.offset;b.writeUint8(0);this.resolvedType.encode(a,b);var e=b.offset-d-1,f=n.calculateVarint32(e);if(1<f){var h=b.clone();h.offset-=e;b.append(h.flip(),d-1+f)}b.writeVarint32(e,d)}else throw Error("[INTERNAL ERROR] Illegal value to encode in "+
this.toString(!0)+": "+a+" (unknown type)");return b}};d.prototype.decode=function(a,b,d){if(a!=this.type.wireType&&(d||a!=c.WIRE_TYPES.LDELIM||!this.repeated))throw Error("Illegal wire type for field "+this.toString(!0)+": "+a+" ("+this.type.wireType+" expected)");if(a==c.WIRE_TYPES.LDELIM&&(this.repeated&&this.options.packed)&&!d){a=b.readVarint32();a=b.offset+a;for(d=[];b.offset<a;)d.push(this.decode(this.type.wireType,b,!0));return d}if(this.type==c.TYPES.int32)return b.readVarint32()|0;if(this.type==
c.TYPES.uint32)return b.readVarint32()>>>0;if(this.type==c.TYPES.sint32)return b.readZigZagVarint32()|0;if(this.type==c.TYPES.fixed32)return b.readUint32()>>>0;if(this.type==c.TYPES.sfixed32)return b.readInt32()|0;if(this.type==c.TYPES.int64)return b.readVarint64();if(this.type==c.TYPES.uint64)return b.readVarint64().toUnsigned();if(this.type==c.TYPES.sint64)return b.readZigZagVarint64();if(this.type==c.TYPES.fixed64)return b.readUint64();if(this.type==c.TYPES.sfixed64)return b.readInt64();if(this.type==
c.TYPES.bool)return!!b.readVarint32();if(this.type==c.TYPES["enum"])return b.readVarint32();if(this.type==c.TYPES["float"])return b.readFloat();if(this.type==c.TYPES["double"])return b.readDouble();if(this.type==c.TYPES.string)return b.readVString();if(this.type==c.TYPES.bytes)return a=b.readVarint32(),d=b.clone(),d.length=d.offset+a,b.offset+=a,d;if(this.type==c.TYPES.message)return a=b.readVarint32(),this.resolvedType.decode(b,a);throw Error("[INTERNAL ERROR] Illegal wire type for "+this.toString(!0)+
": "+a);};b.Message.Field=d;var e=function(a,b,c){h.call(this,a,b,c);this.built=null};e.prototype=Object.create(h.prototype);e.prototype.build=function(){for(var a={},b=this.getChildren(e.Value),c=0;c<b.length;c++)a[b[c].name]=b[c].id;Object.defineProperty&&Object.defineProperty(a,"$options",{value:this.buildOpt(),enumerable:!1,configurable:!1,writable:!1});return this.built=a};b.Enum=e;d=function(a,b,c){f.call(this,a,b);this.id=c};d.prototype=Object.create(f.prototype);b.Enum.Value=d;return b}(f);
f.Builder=function(c,b,f){var h=function(){this.ptr=this.ns=new f.Namespace(null,"");this.resolved=!1;this.result=null};h.prototype.reset=function(){this.ptr=this.ns};h.prototype.define=function(a,c){if("string"!=typeof a||!b.TYPEDEF.test(a))throw Error("Illegal package name: "+a);var e=a.split("."),g;for(g=0;g<e.length;g++)if(!b.NAME.test(e[g]))throw Error("Illegal package name: "+e[g]);for(g=0;g<e.length;g++)this.ptr.hasChild(e[g])||this.ptr.addChild(new f.Namespace(this.ptr,e[g],c)),this.ptr=this.ptr.getChild(e[g]);
return this};h.isValidMessage=function(a){if("string"!=typeof a.name||!b.NAME.test(a.name)||"undefined"!=typeof a.values)return!1;var c;if("undefined"!=typeof a.fields){if(!(a.fields instanceof Array))return!1;var e=[],g;for(c=0;c<a.fields.length;c++){if(!h.isValidMessageField(a.fields[c]))return!1;g=parseInt(a.id,10);if(0<=e.indexOf(g))return!1;e.push(g)}}if("undefined"!=typeof a.enums){if(!(a.enums instanceof Array))return!1;for(c=0;c<a.enums.length;c++)if(!h.isValidEnum(a.enums[c]))return!1}if("undefined"!=
typeof a.messages){if(!(a.messages instanceof Array))return!1;for(c=0;c<a.messages.length;c++)if(!h.isValidMessage(a.messages[c]))return!1}return!0};h.isValidMessageField=function(a){if("string"!=typeof a.rule||"string"!=typeof a.name||"string"!=typeof a.type||"undefined"==typeof a.id||!b.RULE.test(a.rule)||!b.NAME.test(a.name)||!b.TYPEREF.test(a.type)||!b.ID.test(""+a.id))return!1;if("undefined"!=typeof a.options){if("object"!=typeof a.options)return!1;for(var c=Object.keys(a.options),e=0;e<c.length;e++)if(!b.NAME.test(c[e])||
!b.ID.test(""+a.options[c[e]])&&!b.TYPEREF.test(a.options[c[e]]))return!1}return!0};h.isValidEnum=function(a){if("string"!=typeof a.name||!b.NAME.test(a.name)||"undefined"==typeof a.values||!(a.values instanceof Array)||0==a.values.length)return!1;for(var c=0;c<a.values.length;c++)if("object"!=typeof a.values[c]||"string"!=typeof a.values[c].name||"undefined"==typeof a.values[c].id||!b.NAME.test(a.values[c].name)||!b.ID.test(""+a.values[c].id))return!1;return!0};h.prototype.create=function(a){if(a&&
(a instanceof Array||(a=[a]),0!=a.length)){var c=[],e,g,k,l,n;for(c.push(a);0<c.length;){a=c.pop();if(a instanceof Array)for(;0<a.length;)if(e=a.shift(),h.isValidMessage(e)){g=new f.Message(this.ptr,e.name,e.options);if(e.fields&&0<e.fields.length)for(l=0;l<e.fields.length;l++){if(!h.isValidMessageField(e.fields[l]))throw Error("Not a valid message field definition in message "+g.name+": "+JSON.stringify(e.fields[l]));if(g.hasChild(e.fields[l].id))throw Error("Duplicate field id in message "+g.name+
": "+e.fields[l].id);if(e.fields[l].options){k=Object.keys(e.fields[l].options);for(n=0;n<k.length;n++){if(!b.NAME.test(k[n]))throw Error("Illegal field option name in message "+g.name+"#"+e.fields[l].name+": "+k[n]);if(!b.ID.test(""+e.fields[l].options[k[n]])&&!b.TYPEREF.test(e.fields[l].options[k[n]]))throw Error("Illegal field option value in message "+g.name+"#"+e.fields[l].name+"#"+k[n]+": "+e.fields[l].options[k[n]]);}}g.addChild(new f.Message.Field(g,e.fields[l].rule,e.fields[l].type,e.fields[l].name,
e.fields[l].id,e.fields[l].options))}k=[];if("undefined"!=typeof e.enums&&0<e.enums.length)for(l=0;l<e.enums.length;l++)k.push(e.enums[l]);if(e.messages&&0<e.messages.length)for(l=0;l<e.messages.length;l++)k.push(e.messages[l]);this.ptr.addChild(g);0<k.length&&(c.push(a),a=k,this.ptr=g)}else if(h.isValidEnum(e)){g=new f.Enum(this.ptr,e.name,e.options);for(l=0;l<e.values.length;l++)g.addChild(new f.Enum.Value(g,e.values[l].name,e.values[l].id));this.ptr.addChild(g)}else throw Error("Not a valid message or enum definition: "+
JSON.stringify(e));else throw Error("Not a valid namespace definition: "+JSON.stringify(a));this.ptr=this.ptr.parent}this.resolved=!1;this.result=null;return this}};h.prototype["import"]=function(a,b){a["package"]&&this.define(a["package"],a.options);a.messages&&this.create(a.messages);this.reset();a["package"]&&this.define(a["package"],a.options);a.enums&&this.create(a.enums);this.reset();if(a.imports&&0<a.imports.length){if(!b)throw Error("Cannot determine import root: File name is unknown");for(var e=
b.replace(/[\/\\][^\/\\]*$/,""),g=0;g<a.imports.length;g++){var f=e+"/"+a.imports[g];if(/\.json$/i.test(f)){var h=c.Util.fetch(f);if(null===h)throw Error("Failed to import '"+f+"' in '"+b+"': File not found");this["import"](JSON.parse(h),f)}else if(!/google\/protobuf\//.test(f)){h=c.Util.fetch(f);if(null===h)throw Error("Failed to import '"+f+"' in '"+b+"': File not found");h=new c.DotProto.Parser(h+"");this["import"](h.parse(),f)}}}return this};h.prototype.resolveAll=function(){if(!(null==this.ptr||
"object"==typeof this.ptr.type)){if(this.ptr instanceof f.Namespace)for(var a=this.ptr.getChildren(),d=0;d<a.length;d++)this.ptr=a[d],this.resolveAll();else if(this.ptr instanceof f.Message.Field)if(b.TYPE.test(this.ptr.type))this.ptr.type=c.TYPES[this.ptr.type];else{if(!b.TYPEREF.test(this.ptr.type))throw Error("Illegal type reference in "+this.ptr.toString(!0)+": "+this.ptr.type);a=this.ptr.parent.resolve(this.ptr.type);if(!a)throw Error("Unresolvable type reference in "+this.ptr.toString(!0)+": "+
this.ptr.type);this.ptr.resolvedType=a;if(a instanceof f.Enum)this.ptr.type=c.TYPES["enum"];else if(a instanceof f.Message)this.ptr.type=c.TYPES.message;else throw Error("Illegal type reference in "+this.ptr.toString(!0)+": "+this.ptr.type);}else if(!(this.ptr instanceof c.Reflect.Enum.Value))throw Error("Illegal object type in namespace: "+typeof this.ptr+":"+this.ptr);this.reset()}};h.prototype.build=function(a){this.reset();this.resolved||(this.resolveAll(),this.resolved=!0,this.result=null);null==
this.result&&(this.result=this.ns.build());if(a){a=a.split(".");for(var b=this.result,c=0;c<a.length;c++)if(b[a[c]])b=b[a[c]];else{b=null;break}return b}return this.result};h.prototype.toString=function(){return"Builder"};return h}(f,f.Lang,f.Reflect);f.protoFromString=function(c,b,m){"string"==typeof b&&(m=b,b=null);c=(new f.DotProto.Parser(c+"")).parse();b="object"==typeof b?b:new f.Builder;null!==c["package"]&&b.define(c["package"],c.options);b.create(c.messages);b.reset();null!==c["package"]&&
b.define(c["package"],c.options);b.create(c.enums);b.reset();if(m&&0<c.imports.length)b["import"]({imports:c.imports},m);b.resolveAll();b.build();return b};f.protoFromFile=function(c,b,m){if(b&&"object"==typeof b)m=b,b=null;else if(!b||"function"!=typeof b)b=null;if(b)f.Util.fetch(c,function(a){b(f.protoFromString(a,m,c))});else{var h=f.Util.fetch(c);return null!==h?f.protoFromString(h,m,c):null}};f.newBuilder=function(c,b){var m=new f.Builder;"undefined"!=typeof c&&m.define(c,b);return m};return f}
"undefined"!=typeof module&&module.exports?module.exports=q(require("bytebuffer")):"undefined"!=typeof define&&define.amd?define("ProtoBuf",["ByteBuffer"],q):(p.dcodeIO||(p.dcodeIO={}),p.dcodeIO.ProtoBuf=q(p.dcodeIO.ByteBuffer))})(this);