UNPKG

ndn-js

Version:

A JavaScript client library for Named Data Networking

112 lines (108 loc) 48.9 kB
/* ProtoBuf.js (c) 2013 Daniel Wirtz <dcode@dcode.io> Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Released under the Apache License, Version 2.0 see: https://github.com/dcodeIO/ProtoBuf.js for details */ (function(p){function q(m){if(!m||!m.VERSION||3>m.VERSION.split(".")[0])throw Error("ProtoBuf.js requires ByteBuffer.js >=3");var g={VERSION:"3.0.0-pre",WIRE_TYPES:{}};g.WIRE_TYPES.VARINT=0;g.WIRE_TYPES.BITS64=1;g.WIRE_TYPES.LDELIM=2;g.WIRE_TYPES.STARTGROUP=3;g.WIRE_TYPES.ENDGROUP=4;g.WIRE_TYPES.BITS32=5;g.TYPES={int32:{name:"int32",wireType:g.WIRE_TYPES.VARINT},uint32:{name:"uint32",wireType:g.WIRE_TYPES.VARINT},sint32:{name:"sint32",wireType:g.WIRE_TYPES.VARINT},int64:{name:"int64",wireType:g.WIRE_TYPES.VARINT}, uint64:{name:"uint64",wireType:g.WIRE_TYPES.VARINT},sint64:{name:"sint64",wireType:g.WIRE_TYPES.VARINT},bool:{name:"bool",wireType:g.WIRE_TYPES.VARINT},"double":{name:"double",wireType:g.WIRE_TYPES.BITS64},string:{name:"string",wireType:g.WIRE_TYPES.LDELIM},bytes:{name:"bytes",wireType:g.WIRE_TYPES.LDELIM},fixed32:{name:"fixed32",wireType:g.WIRE_TYPES.BITS32},sfixed32:{name:"sfixed32",wireType:g.WIRE_TYPES.BITS32},fixed64:{name:"fixed64",wireType:g.WIRE_TYPES.BITS64},sfixed64:{name:"sfixed64",wireType:g.WIRE_TYPES.BITS64}, "float":{name:"float",wireType:g.WIRE_TYPES.BITS32},"enum":{name:"enum",wireType:g.WIRE_TYPES.VARINT},message:{name:"message",wireType:g.WIRE_TYPES.LDELIM},group:{name:"group",wireType:g.WIRE_TYPES.STARTGROUP}};g.Long=m.Long;g.convertFieldsToCamelCase=!1;g.Util=function(){Object.create||(Object.create=function(c){function f(){}if(1<arguments.length)throw Error("Object.create implementation only accepts the first parameter.");f.prototype=c;return new f});var d={IS_NODE:!1};try{d.IS_NODE="function"=== typeof require&&"function"===typeof require("fs").readFileSync&&"function"===typeof require("path").resolve}catch(c){}d.XHR=function(){for(var c=[function(){return new XMLHttpRequest},function(){return new ActiveXObject("Msxml2.XMLHTTP")},function(){return new ActiveXObject("Msxml3.XMLHTTP")},function(){return new ActiveXObject("Microsoft.XMLHTTP")}],f=null,a=0;a<c.length;a++){try{f=c[a]()}catch(b){continue}break}if(!f)throw Error("XMLHttpRequest is not supported");return f};d.fetch=function(c,f){f&& "function"!=typeof f&&(f=null);if(d.IS_NODE)if(f)require("fs").readFile(c,function(a,b){a?f(null):f(""+b)});else try{return require("fs").readFileSync(c)}catch(a){return null}else{var b=d.XHR();b.open("GET",c,f?!0:!1);b.setRequestHeader("Accept","text/plain");"function"===typeof b.overrideMimeType&&b.overrideMimeType("text/plain");if(f)b.onreadystatechange=function(){4==b.readyState&&(200==b.status||0==b.status&&"string"===typeof b.responseText?f(b.responseText):f(null))},4!=b.readyState&&b.send(null); else return b.send(null),200==b.status||0==b.status&&"string"===typeof b.responseText?b.responseText:null}};d.isArray=function(c){return c?c instanceof Array?!0:Array.isArray?Array.isArray(c):"[object Array]"===Object.prototype.toString.call(c):!1};return d}();g.Lang={OPEN:"{",CLOSE:"}",OPTOPEN:"[",OPTCLOSE:"]",OPTEND:",",EQUAL:"=",END:";",STRINGOPEN:'"',STRINGCLOSE:'"',STRINGOPEN_SQ:"'",STRINGCLOSE_SQ:"'",COPTOPEN:"(",COPTCLOSE:")",DELIM:/[\s\{\}=;\[\],'"\(\)]/g,KEYWORD:/^(?:package|option|import|message|enum|extend|service|syntax|extensions)$/, 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]*$/,OPTNAME:/^(?:[a-zA-Z][a-zA-Z_0-9]*|\([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]*)+$/,FQTYPEREF:/^(?:\.[a-zA-Z][a-zA-Z_0-9]*)+$/,NUMBER:/^-?(?:[1-9][0-9]*|0|0x[0-9a-fA-F]+|0[0-7]+|([0-9]*\.[0-9]+([Ee][+-]?[0-9]+)?))$/,NUMBER_DEC:/^(?:[1-9][0-9]*|0)$/,NUMBER_HEX:/^0x[0-9a-fA-F]+$/, NUMBER_OCT:/^0[0-7]+$/,NUMBER_FLT:/^[0-9]*\.[0-9]+([Ee][+-]?[0-9]+)?$/,ID:/^(?:[1-9][0-9]*|0|0x[0-9a-fA-F]+|0[0-7]+)$/,NEGID:/^\-?(?:[1-9][0-9]*|0|0x[0-9a-fA-F]+|0[0-7]+)$/,WHITESPACE:/\s/,STRING:/['"]([^'"\\]*(\\.[^"\\]*)*)['"]/g,BOOL:/^(?:true|false)$/i,ID_MIN:1,ID_MAX:536870911};g.DotProto={};g.DotProto.Tokenizer=function(d){var c=function(c){this.source=""+c;this.index=0;this.line=1;this.stack=[];this.readingString=!1;this.stringEndsWith=d.STRINGCLOSE};c.prototype._readString=function(){d.STRING.lastIndex= this.index-1;var c;if(null!==(c=d.STRING.exec(this.source)))return c=c[1],this.index=d.STRING.lastIndex,this.stack.push(this.stringEndsWith),c;throw Error("Illegal string value at line "+this.line+", index "+this.index);};c.prototype.next=function(){if(0<this.stack.length)return this.stack.shift();if(this.index>=this.source.length)return null;if(this.readingString)return this.readingString=!1,this._readString();var c,f;do{for(c=!1;d.WHITESPACE.test(f=this.source.charAt(this.index));)if(this.index++, "\n"===f&&this.line++,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++;this.line++;c=!0}else if("*"===this.source.charAt(this.index)){for(f="";"*/"!==f+(f=this.source.charAt(this.index));)if(this.index++,"\n"===f&&this.line++,this.index===this.source.length)return null;this.index++;c=!0}else throw Error("Invalid comment at line "+ this.line+": /"+this.source.charAt(this.index)+" ('/' or '*' expected)");}while(c);if(this.index===this.source.length)return null;c=this.index;d.DELIM.lastIndex=0;if(d.DELIM.test(this.source.charAt(c)))c++;else for(c++;c<this.source.length&&!d.DELIM.test(this.source.charAt(c));)c++;c=this.source.substring(this.index,this.index=c);c===d.STRINGOPEN?(this.readingString=!0,this.stringEndsWith=d.STRINGCLOSE):c===d.STRINGOPEN_SQ&&(this.readingString=!0,this.stringEndsWith=d.STRINGCLOSE_SQ);return c};c.prototype.peek= function(){if(0===this.stack.length){var c=this.next();if(null===c)return null;this.stack.push(c)}return this.stack[0]};c.prototype.toString=function(){return"Tokenizer("+this.index+"/"+this.source.length+" at line "+this.line+")"};return c}(g.Lang);g.DotProto.Parser=function(d,c,g){d=function(c){this.tn=new g(c)};d.prototype.parse=function(){var c={name:"[ROOT]","package":null,messages:[],enums:[],imports:[],options:{},services:[]},a,b=!0;do{a=this.tn.next();if(null==a)break;if("package"==a){if(!b)throw Error("Illegal package definition at line "+ this.tn.line+": Must be declared before the first message or enum");if(null!==c["package"])throw Error("Illegal package definition at line "+this.tn.line+": Package already declared");c["package"]=this._parsePackage(a)}else if("import"==a){if(!b)throw Error("Illegal import definition at line "+this.tn.line+": Must be declared before the first message or enum");c.imports.push(this._parseImport(a))}else if("message"===a)this._parseMessage(c,a),b=!1;else if("enum"===a)this._parseEnum(c,a),b=!1;else if("option"=== a){if(!b)throw Error("Illegal option definition at line "+this.tn.line+": Must be declared before the first message or enum");this._parseOption(c,a)}else if("service"===a)this._parseService(c,a);else if("extend"===a)this._parseExtend(c,a);else if("syntax"===a)this._parseIgnoredStatement(c,a);else throw Error("Illegal top level declaration at line "+this.tn.line+": "+a);}while(1);delete c.name;return c};d.prototype._parseNumber=function(f){var a=1;"-"==f.charAt(0)&&(a=-1,f=f.substring(1));if(c.NUMBER_DEC.test(f))return a* parseInt(f,10);if(c.NUMBER_HEX.test(f))return a*parseInt(f.substring(2),16);if(c.NUMBER_OCT.test(f))return a*parseInt(f.substring(1),8);if(c.NUMBER_FLT.test(f))return a*parseFloat(f);throw Error("Illegal number value at line "+this.tn.line+": "+(0>a?"-":"")+f);};d.prototype._parseId=function(f,a){var b=-1,d=1;"-"==f.charAt(0)&&(d=-1,f=f.substring(1));if(c.NUMBER_DEC.test(f))b=parseInt(f);else if(c.NUMBER_HEX.test(f))b=parseInt(f.substring(2),16);else if(c.NUMBER_OCT.test(f))b=parseInt(f.substring(1), 8);else throw Error("Illegal ID value at line "+this.tn.line+": "+(0>d?"-":"")+f);b=d*b|0;if(!a&&0>b)throw Error("Illegal ID range at line "+this.tn.line+": "+(0>d?"-":"")+f);return b};d.prototype._parsePackage=function(d){d=this.tn.next();if(!c.TYPEREF.test(d))throw Error("Illegal package name at line "+this.tn.line+": "+d);var a=d;d=this.tn.next();if(d!=c.END)throw Error("Illegal end of package definition at line "+this.tn.line+": "+d+" ('"+c.END+"' expected)");return a};d.prototype._parseImport= function(d){d=this.tn.next();"public"===d&&(d=this.tn.next());if(d!==c.STRINGOPEN&&d!==c.STRINGOPEN_SQ)throw Error("Illegal begin of import value at line "+this.tn.line+": "+d+" ('"+c.STRINGOPEN+"' or '"+c.STRINGOPEN_SQ+"' expected)");var a=this.tn.next();d=this.tn.next();if(d!==this.tn.stringEndsWith)throw Error("Illegal end of import value at line "+this.tn.line+": "+d+" ('"+this.tn.stringEndsWith+"' expected)");d=this.tn.next();if(d!==c.END)throw Error("Illegal end of import definition at line "+ this.tn.line+": "+d+" ('"+c.END+"' expected)");return a};d.prototype._parseOption=function(d,a){a=this.tn.next();var b=!1;a==c.COPTOPEN&&(b=!0,a=this.tn.next());if(!c.TYPEREF.test(a)&&!/google\.protobuf\./.test(a))throw Error("Illegal option name in message "+d.name+" at line "+this.tn.line+": "+a);var e=a;a=this.tn.next();if(b){if(a!==c.COPTCLOSE)throw Error("Illegal custom option name delimiter in message "+d.name+", option "+e+" at line "+this.tn.line+": "+a+" ('"+c.COPTCLOSE+"' expected)");e= "("+e+")";a=this.tn.next();c.FQTYPEREF.test(a)&&(e+=a,a=this.tn.next())}if(a!==c.EQUAL)throw Error("Illegal option operator in message "+d.name+", option "+e+" at line "+this.tn.line+": "+a+" ('"+c.EQUAL+"' expected)");a=this.tn.next();if(a===c.STRINGOPEN||a===c.STRINGOPEN_SQ){if(b=this.tn.next(),a=this.tn.next(),a!==this.tn.stringEndsWith)throw Error("Illegal end of option value in message "+d.name+", option "+e+" at line "+this.tn.line+": "+a+" ('"+this.tn.stringEndsWith+"' expected)");}else if(c.NUMBER.test(a))b= this._parseNumber(a,!0);else if(c.TYPEREF.test(a))b=a;else throw Error("Illegal option value in message "+d.name+", option "+e+" at line "+this.tn.line+": "+a);a=this.tn.next();if(a!==c.END)throw Error("Illegal end of option in message "+d.name+", option "+e+" at line "+this.tn.line+": "+a+" ('"+c.END+"' expected)");d.options[e]=b};d.prototype._parseIgnoredBlock=function(d,a){var b=this.tn.next();if(!c.TYPEREF.test(b))throw Error("Illegal "+a+" type in "+d.name+": "+b);var e=b,b=this.tn.next();if(b!== c.OPEN)throw Error("Illegal OPEN in "+d.name+" after "+a+" "+e+" at line "+this.tn.line+": "+b);var h=1;do{b=this.tn.next();if(null===b)throw Error("Unexpected EOF in "+d.name+", "+a+" (ignored) at line "+this.tn.line+": "+e);if(b===c.OPEN)h++;else if(b===c.CLOSE&&(b=this.tn.peek(),b===c.END&&this.tn.next(),h--,0===h))break}while(1)};d.prototype._parseIgnoredStatement=function(d,a){var b;do{b=this.tn.next();if(null===b)throw Error("Unexpected EOF in "+d.name+", "+a+" (ignored) at line "+this.tn.line); if(b===c.END)break}while(1)};d.prototype._parseService=function(d,a){var b=this.tn.next();if(!c.NAME.test(b))throw Error("Illegal service name at line "+this.tn.line+": "+b);var e=b,h={name:e,rpc:{},options:{}},b=this.tn.next();if(b!==c.OPEN)throw Error("Illegal OPEN after service "+e+" at line "+this.tn.line+": "+b+" ('"+c.OPEN+"' expected)");do if(b=this.tn.next(),"option"===b)this._parseOption(h,b);else if("rpc"===b)this._parseServiceRPC(h,b);else if(b!==c.CLOSE)throw Error("Illegal type for service "+ e+" at line "+this.tn.line+": "+b);while(b!==c.CLOSE);d.services.push(h)};d.prototype._parseServiceRPC=function(d,a){var b=a;a=this.tn.next();if(!c.NAME.test(a))throw Error("Illegal RPC method name in service "+d.name+" at line "+this.tn.line+": "+a);var e=a,h={request:null,response:null,options:{}};a=this.tn.next();if(a!==c.COPTOPEN)throw Error("Illegal start of request type in RPC service "+d.name+"#"+e+" at line "+this.tn.line+": "+a+" ('"+c.COPTOPEN+"' expected)");a=this.tn.next();if(!c.TYPEREF.test(a))throw Error("Illegal request type in RPC service "+ d.name+"#"+e+" at line "+this.tn.line+": "+a);h.request=a;a=this.tn.next();if(a!=c.COPTCLOSE)throw Error("Illegal end of request type in RPC service "+d.name+"#"+e+" at line "+this.tn.line+": "+a+" ('"+c.COPTCLOSE+"' expected)");a=this.tn.next();if("returns"!==a.toLowerCase())throw Error("Illegal request/response delimiter in RPC service "+d.name+"#"+e+" at line "+this.tn.line+": "+a+" ('returns' expected)");a=this.tn.next();if(a!=c.COPTOPEN)throw Error("Illegal start of response type in RPC service "+ d.name+"#"+e+" at line "+this.tn.line+": "+a+" ('"+c.COPTOPEN+"' expected)");a=this.tn.next();h.response=a;a=this.tn.next();if(a!==c.COPTCLOSE)throw Error("Illegal end of response type in RPC service "+d.name+"#"+e+" at line "+this.tn.line+": "+a+" ('"+c.COPTCLOSE+"' expected)");a=this.tn.next();if(a===c.OPEN){do if(a=this.tn.next(),"option"===a)this._parseOption(h,a);else if(a!==c.CLOSE)throw Error("Illegal start of option in RPC service "+d.name+"#"+e+" at line "+this.tn.line+": "+a+" ('option' expected)"); while(a!==c.CLOSE);this.tn.peek()===c.END&&this.tn.next()}else if(a!==c.END)throw Error("Illegal method delimiter in RPC service "+d.name+"#"+e+" at line "+this.tn.line+": "+a+" ('"+c.END+"' or '"+c.OPEN+"' expected)");"undefined"===typeof d[b]&&(d[b]={});d[b][e]=h};d.prototype._parseMessage=function(d,a){var b={},e="group"===a;a=this.tn.next();if(!c.NAME.test(a))throw Error("Illegal "+(e?"group":"message")+" name"+(d?" in message "+d.name:"")+" at line "+this.tn.line+": "+a);b.name=a;if(e){a=this.tn.next(); if(a!==c.EQUAL)throw Error("Illegal id assignment after group "+b.name+" at line "+this.tn.line+": "+a+" ('"+c.EQUAL+"' expected)");a=this.tn.next();if(!c.ID.test(a))throw Error("Illegal id value after group "+b.name+" at line "+this.tn.line+": "+a);b.groupId=this._parseId(a)}a=this.tn.next();if(a!=c.OPEN)throw Error("Illegal OPEN after "+(e?"group":"message")+" "+b.name+" at line "+this.tn.line+": "+a+" ('"+c.OPEN+"' expected)");b.fields=[];b.enums=[];b.messages=[];b.options={};do if(a=this.tn.next(), a===c.CLOSE){a=this.tn.peek();a===c.END&&this.tn.next();break}else if(c.RULE.test(a))this._parseMessageField(b,a);else if("enum"===a)this._parseEnum(b,a);else if("message"===a)this._parseMessage(b,a);else if("option"===a)this._parseOption(b,a);else if("extensions"===a)b.extensions=this._parseExtensions(b,a);else if("extend"===a)this._parseExtend(b,a);else throw Error("Illegal token in message "+b.name+" at line "+this.tn.line+": "+a+" (type or '"+c.CLOSE+"' expected)");while(1);d.messages.push(b); return b};d.prototype._parseMessageField=function(d,a){var b={},e=null;b.rule=a;a=this.tn.next();if("group"===a)e=this._parseMessage(d,a),b.type=e.name,b.name=e.name.toLowerCase(),b.id=e.groupId,b.options={},a=this.tn.peek(),a===c.END&&this.tn.next();else{if(!c.TYPE.test(a)&&!c.TYPEREF.test(a))throw Error("Illegal field type in message "+d.name+" at line "+this.tn.line+": "+a);b.type=a;a=this.tn.next();if(!c.NAME.test(a))throw Error("Illegal field name in message "+d.name+" at line "+this.tn.line+ ": "+a);b.name=a;a=this.tn.next();if(a!==c.EQUAL)throw Error("Illegal field id assignment in message "+d.name+"#"+b.name+" at line "+this.tn.line+": "+a+" ('"+c.EQUAL+"' expected)");a=this.tn.next();try{b.id=this._parseId(a)}catch(h){throw Error("Illegal field id value in message "+d.name+"#"+b.name+" at line "+this.tn.line+": "+a);}b.options={};a=this.tn.next();a===c.OPTOPEN&&(this._parseFieldOptions(d,b,a),a=this.tn.next());if(a!==c.END)throw Error("Illegal field delimiter in message "+d.name+"#"+ b.name+" at line "+this.tn.line+": "+a+" ('"+c.END+"' expected)");}d.fields.push(b)};d.prototype._parseFieldOptions=function(d,a,b){var e=!0;do{b=this.tn.next();if(b===c.OPTCLOSE)break;else if(b===c.OPTEND){if(e)throw Error("Illegal start of message field options in message "+d.name+"#"+a.name+" at line "+this.tn.line+": "+b);b=this.tn.next()}this._parseFieldOption(d,a,b);e=!1}while(1)};d.prototype._parseFieldOption=function(d,a,b){var e=!1;b===c.COPTOPEN&&(b=this.tn.next(),e=!0);if(!c.NAME.test(b))throw Error("Illegal field option in message "+ d.name+"#"+a.name+" at line "+this.tn.line+": "+b);var h=b;b=this.tn.next();if(e){if(b!==c.COPTCLOSE)throw Error("Illegal custom field option name delimiter in message "+d.name+"#"+a.name+" at line "+this.tn.line+": "+b+" (')' expected)");h="("+h+")";b=this.tn.next();c.FQTYPEREF.test(b)&&(h+=b,b=this.tn.next())}if(b!==c.EQUAL)throw Error("Illegal field option operation in message "+d.name+"#"+a.name+" at line "+this.tn.line+": "+b+" ('=' expected)");b=this.tn.next();if(b===c.STRINGOPEN||b===c.STRINGOPEN_SQ){if(e= this.tn.next(),b=this.tn.next(),b!=this.tn.stringEndsWith)throw Error("Illegal end of field value in message "+d.name+"#"+a.name+", option "+h+" at line "+this.tn.line+": "+b+" ('"+this.tn.stringEndsWith+"' expected)");}else if(c.NUMBER.test(b,!0))e=this._parseNumber(b,!0);else if(c.BOOL.test(b))e="true"===b.toLowerCase();else if(c.TYPEREF.test(b))e=b;else throw Error("Illegal field option value in message "+d.name+"#"+a.name+", option "+h+" at line "+this.tn.line+": "+b);a.options[h]=e};d.prototype._parseEnum= function(d,a){var b={};a=this.tn.next();if(!c.NAME.test(a))throw Error("Illegal enum name in message "+d.name+" at line "+this.tn.line+": "+a);b.name=a;a=this.tn.next();if(a!==c.OPEN)throw Error("Illegal OPEN after enum "+b.name+" at line "+this.tn.line+": "+a);b.values=[];b.options={};do{a=this.tn.next();if(a===c.CLOSE){a=this.tn.peek();a===c.END&&this.tn.next();break}if("option"==a)this._parseOption(b,a);else{if(!c.NAME.test(a))throw Error("Illegal enum value name in enum "+b.name+" at line "+this.tn.line+ ": "+a);this._parseEnumValue(b,a)}}while(1);d.enums.push(b)};d.prototype._parseEnumValue=function(d,a){var b={};b.name=a;a=this.tn.next();if(a!==c.EQUAL)throw Error("Illegal enum value operator in enum "+d.name+" at line "+this.tn.line+": "+a+" ('"+c.EQUAL+"' expected)");a=this.tn.next();try{b.id=this._parseId(a,!0)}catch(e){throw Error("Illegal enum value id in enum "+d.name+" at line "+this.tn.line+": "+a);}d.values.push(b);a=this.tn.next();a===c.OPTOPEN&&(this._parseFieldOptions(d,{options:{}}, a),a=this.tn.next());if(a!==c.END)throw Error("Illegal enum value delimiter in enum "+d.name+" at line "+this.tn.line+": "+a+" ('"+c.END+"' expected)");};d.prototype._parseExtensions=function(d,a){var b=[];a=this.tn.next();"min"===a?b.push(c.ID_MIN):"max"===a?b.push(c.ID_MAX):b.push(this._parseNumber(a));a=this.tn.next();if("to"!==a)throw"Illegal extensions delimiter in message "+d.name+" at line "+this.tn.line+" ('to' expected)";a=this.tn.next();"min"===a?b.push(c.ID_MIN):"max"===a?b.push(c.ID_MAX): b.push(this._parseNumber(a));a=this.tn.next();if(a!==c.END)throw Error("Illegal extension delimiter in message "+d.name+" at line "+this.tn.line+": "+a+" ('"+c.END+"' expected)");return b};d.prototype._parseExtend=function(d,a){a=this.tn.next();if(!c.TYPEREF.test(a))throw Error("Illegal extended message name at line "+this.tn.line+": "+a);var b={};b.ref=a;b.fields=[];a=this.tn.next();if(a!==c.OPEN)throw Error("Illegal OPEN in extend "+b.name+" at line "+this.tn.line+": "+a+" ('"+c.OPEN+"' expected)"); do if(a=this.tn.next(),a===c.CLOSE){a=this.tn.peek();a==c.END&&this.tn.next();break}else if(c.RULE.test(a))this._parseMessageField(b,a);else throw Error("Illegal token in extend "+b.name+" at line "+this.tn.line+": "+a+" (rule or '"+c.CLOSE+"' expected)");while(1);d.messages.push(b);return b};d.prototype.toString=function(){return"Parser"};return d}(g,g.Lang,g.DotProto.Tokenizer);g.Reflect=function(d){function c(a,b){if(a&&"number"===typeof a.low&&"number"===typeof a.high&&"boolean"===typeof a.unsigned)return new d.Long(a.low, a.high,"undefined"===typeof b?a.unsigned:b);if("string"===typeof a)return d.Long.fromString(a,b||!1,10);if("number"===typeof a)return d.Long.fromNumber(a,b||!1);throw Error("not convertible to Long");}var g={},f=function(a,d){this.parent=a;this.name=d};f.prototype.fqn=function(){var a=this.name,d=this;do{d=d.parent;if(null==d)break;a=d.name+"."+a}while(1);return a};f.prototype.toString=function(d){var c=this.fqn();d&&(this instanceof b?c="Message "+c:this instanceof b.Field?c="Message.Field "+c:this instanceof h?c="Enum "+c:this instanceof h.Value?c="Enum.Value "+c:this instanceof n?c="Service "+c:this instanceof n.Method?c=this instanceof n.RPCMethod?"Service.RPCMethod "+c:"Service.Method "+c:this instanceof a&&(c="Namespace "+c));return c};f.prototype.build=function(){throw Error(this.toString(!0)+" cannot be built directly");};g.T=f;var a=function(a,d,b){f.call(this,a,d);this.children=[];this.options=b||{}};a.prototype=Object.create(f.prototype);a.prototype.getChildren=function(a){a=a||null;if(null== a)return this.children.slice();for(var d=[],b=0;b<this.children.length;b++)this.children[b]instanceof a&&d.push(this.children[b]);return d};a.prototype.addChild=function(a){var d;if(d=this.getChild(a.name))if(d instanceof b.Field&&d.name!==d.originalName&&!this.hasChild(d.originalName))d.name=d.originalName;else if(a instanceof b.Field&&a.name!==a.originalName&&!this.hasChild(a.originalName))a.name=a.originalName;else throw Error("Duplicate name in namespace "+this.toString(!0)+": "+a.name);this.children.push(a)}; a.prototype.hasChild=function(a){var d;if("number"==typeof a)for(d=0;d<this.children.length;d++){if("undefined"!==typeof this.children[d].id&&this.children[d].id==a)return!0}else for(d=0;d<this.children.length;d++)if("undefined"!==typeof this.children[d].name&&this.children[d].name==a)return!0;return!1};a.prototype.getChild=function(a){var d;if("number"==typeof a)for(d=0;d<this.children.length;d++){if("undefined"!==typeof this.children[d].id&&this.children[d].id==a)return this.children[d]}else for(d= 0;d<this.children.length;d++)if("undefined"!==typeof this.children[d].name&&this.children[d].name==a)return this.children[d];return null};a.prototype.resolve=function(a,d){var b=a.split("."),c=this,e=0;if(""==b[e]){for(;null!=c.parent;)c=c.parent;e++}do{do{c=c.getChild(b[e]);if(!(c&&c instanceof g.T)||d&&c instanceof g.Message.Field){c=null;break}e++}while(e<b.length);if(null!=c)break;if(null!==this.parent)return this.parent.resolve(a,d)}while(null!=c);return c};a.prototype.build=function(){for(var d= {},b=this.getChildren(),c,e=0;e<b.length;e++)c=b[e],c instanceof a&&(d[c.name]=c.build());Object.defineProperty&&Object.defineProperty(d,"$options",{value:this.buildOpt(),enumerable:!1,configurable:!1,writable:!1});return d};a.prototype.buildOpt=function(){for(var a={},d=Object.keys(this.options),b=0;b<d.length;b++)a[d[b]]=this.options[d[b]];return a};a.prototype.getOption=function(a){return"undefined"==typeof a?this.options:"undefined"!=typeof this.options[a]?this.options[a]:null};g.Namespace=a; var b=function(b,c,e,f){a.call(this,b,c,e);this.extensions=[d.Lang.ID_MIN,d.Lang.ID_MAX];this.clazz=null;this.groupId=f};b.prototype=Object.create(a.prototype);b.prototype.build=function(a){if(this.clazz&&!a)return this.clazz;a=function(a,d){function b(a,d){var c={},e;for(e in a)a.hasOwnProperty(e)&&(null===a[e]||"object"!==typeof a[e]?c[e]=a[e]:a[e]instanceof m?d&&(c[e]=a.toBuffer()):c[e]=b(a[e],d));return c}var c=d.getChildren(g.Message.Field),e=function(d){a.Builder.Message.call(this);var b,e; for(b=0;b<c.length;b++)e=c[b],this[e.name]=e.repeated?[]:null;for(b=0;b<c.length;b++)if(e=c[b],"undefined"!=typeof e.options["default"])try{this.$set(e.name,e.options["default"])}catch(l){throw Error("[INTERNAL] "+l);}if(1!=arguments.length||"object"!=typeof d||"function"==typeof d.encode||a.Util.isArray(d)||d instanceof m||d instanceof ArrayBuffer||a.Long&&d instanceof a.Long)for(b=0;b<arguments.length;b++)b<c.length&&this.$set(c[b].name,arguments[b]);else for(e=Object.keys(d),b=0;b<e.length;b++)this.$set(e[b], d[e[b]])};e.prototype=Object.create(a.Builder.Message.prototype);e.prototype.add=function(b,c,e){var l=d.getChild(b);if(!l)throw Error(this+"#"+b+" is undefined");if(!(l instanceof a.Reflect.Message.Field))throw Error(this+"#"+b+" is not a field: "+l.toString(!0));if(!l.repeated)throw Error(this+"#"+b+" is not a repeated field");null===this[l.name]&&(this[l.name]=[]);this[l.name].push(e?c:l.verifyValue(c,!0))};e.prototype.$add=e.prototype.add;e.prototype.set=function(b,c,e){var l=d.getChild(b);if(!l)throw Error(this+ "#"+b+" is not a field: undefined");if(!(l instanceof a.Reflect.Message.Field))throw Error(this+"#"+b+" is not a field: "+l.toString(!0));this[l.name]=e?c:l.verifyValue(c)};e.prototype.$set=e.prototype.set;e.prototype.get=function(b){var c=d.getChild(b);if(!(c&&c instanceof a.Reflect.Message.Field))throw Error(this+"#"+b+" is not a field: undefined");if(!(c instanceof a.Reflect.Message.Field))throw Error(this+"#"+b+" is not a field: "+c.toString(!0));return this[c.name]};e.prototype.$get=e.prototype.get; for(var l=0;l<c.length;l++)(function(a){var b=a.originalName.replace(/(_[a-zA-Z])/g,function(a){return a.toUpperCase().replace("_","")}),b=b.substring(0,1).toUpperCase()+b.substring(1),c=a.originalName.replace(/([A-Z])/g,function(a){return"_"+a});d.hasChild("set"+b)||(e.prototype["set"+b]=function(d){this.$set(a.name,d)});d.hasChild("set_"+c)||(e.prototype["set_"+c]=function(d){this.$set(a.name,d)});d.hasChild("get"+b)||(e.prototype["get"+b]=function(){return this.$get(a.name)});d.hasChild("get_"+ c)||(e.prototype["get_"+c]=function(){return this.$get(a.name)})})(c[l]);e.prototype.encode=function(a){var b=!1;a||(a=new m,b=!0);var c=a.littleEndian;try{return d.encode(this,a.LE()),(b?a.flip():a).LE(c)}catch(e){throw a.LE(c),e;}};e.prototype.encodeDelimited=function(a){var b=!1;a||(a=new m,b=!0);try{var c=(new m).LE();d.encode(this,c).flip();a.writeVarint32(c.remaining());a.append(c);return b?a.flip():a}catch(e){throw e;}};e.prototype.encodeAB=function(){try{return this.encode().toArrayBuffer()}catch(a){throw a.encoded&& (a.encoded=a.encoded.toArrayBuffer()),a;}};e.prototype.toArrayBuffer=e.prototype.encodeAB;e.prototype.encodeNB=function(){try{return this.encode().toBuffer()}catch(a){throw a.encoded&&(a.encoded=a.encoded.toBuffer()),a;}};e.prototype.toBuffer=e.prototype.encodeNB;e.prototype.encode64=function(){try{return this.encode().toBase64()}catch(a){throw a.encoded&&(a.encoded=a.encoded.toBase64()),a;}};e.prototype.toBase64=e.prototype.encode64;e.prototype.encodeHex=function(){try{return this.encode().toHex()}catch(a){throw a.encoded&& (a.encoded=a.encoded.toHex()),a;}};e.prototype.toHex=e.prototype.encodeHex;e.prototype.toRaw=function(a){return b(this,!!a)};e.decode=function(a,b){if(null===a)throw Error("buffer must not be null");"string"===typeof a&&(a=m.wrap(a,b?b:"base64"));a=a instanceof m?a:m.wrap(a);var c=a.littleEndian;try{var e=d.decode(a.LE());a.LE(c);return e}catch(l){throw a.LE(c),l;}};e.decodeDelimited=function(a,b){if(null===a)throw Error("buffer must not be null");"string"===typeof a&&(a=m.wrap(a,b?b:"base64"));a= a instanceof m?a:m.wrap(a);var c=a.readVarint32(),e=d.decode(a.slice(a.offset,a.offset+c).LE());a.offset+=c;return e};e.decode64=function(a){return e.decode(a,"base64")};e.decodeHex=function(a){return e.decode(a,"hex")};e.prototype.toString=function(){return d.toString()};Object.defineProperty&&Object.defineProperty(e,"$options",{value:d.buildOpt(),enumerable:!1,configurable:!1,writable:!1});return e}(d,this);for(var c=this.getChildren(),e=0;e<c.length;e++)if(c[e]instanceof h)a[c[e].name]=c[e].build(); else if(c[e]instanceof b)a[c[e].name]=c[e].build();else if(!(c[e]instanceof b.Field))throw Error("Illegal reflect child of "+this.toString(!0)+": "+c[e].toString(!0));return this.clazz=a};b.prototype.encode=function(a,d){for(var c=this.getChildren(b.Field),e=null,f=0;f<c.length;f++){var h=a.$get(c[f].name);c[f].required&&null===h?null===e&&(e=c[f]):c[f].encode(h,d)}if(null!==e)throw c=Error("Missing at least one required field for "+this.toString(!0)+": "+e),c.encoded=d,c;return d};b.prototype.decode= function(a,b){b="number"===typeof b?b:-1;for(var c=a.offset,e=new this.clazz,f,h,g=-1;a.offset<c+b||-1==b&&0<a.remaining();){f=a.readVarint32();h=f&7;f>>=3;if(h===d.WIRE_TYPES.ENDGROUP&&"undefined"!==typeof this.groupId){g=f;break}var k=this.getChild(f);if(k)k.repeated&&!k.options.packed?e.$add(k.name,k.decode(h,a),!0):e.$set(k.name,k.decode(h,a),!0);else switch(h){case d.WIRE_TYPES.VARINT:a.readVarint32();break;case d.WIRE_TYPES.BITS32:a.offset+=4;break;case d.WIRE_TYPES.BITS64:a.offset+=8;break; case d.WIRE_TYPES.LDELIM:h=a.readVarint32();a.offset+=h;break;default:throw Error("Illegal wire type of unknown field "+f+" in "+this.toString(!0)+"#decode: "+h);}}if("undefined"!==typeof this.groupId&&g!==this.groupId)throw Error("Illegal end group indicator for "+this.toString(!0)+": "+g+" ("+this.groupId+" expected)");c=this.getChildren(d.Reflect.Field);for(g=0;g<c.length;g++)if(c[g].required&&null===e[c[g].name])throw c=Error("Missing at least one required field for "+this.toString(!0)+": "+c[g].name), c.decoded=e,c;return e};g.Message=b;var e=function(a,b,c,e,h,g){f.call(this,a,e);this.required="required"==b;this.repeated="repeated"==b;this.type=c;this.resolvedType=null;this.id=h;this.options=g||{};this.originalName=this.name;d.convertFieldsToCamelCase&&(this.name=this.name.replace(/_([a-zA-Z])/g,function(a,d){return d.toUpperCase()}))};e.prototype=Object.create(f.prototype);e.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 e;if(this.repeated&&!b){d.Util.isArray(a)||(a=[a]);var f=[];for(e=0;e<a.length;e++)f.push(this.verifyValue(a[e],!0));return f}if(!this.repeated&&d.Util.isArray(a))throw Error("Illegal value for "+this.toString(!0)+": "+a+" (no array expected)");if(this.type==d.TYPES.int32||this.type==d.TYPES.sint32||this.type==d.TYPES.sfixed32)return isNaN(e=parseInt(a,10))?e:e|0;if(this.type==d.TYPES.uint32||this.type==d.TYPES.fixed32)return isNaN(e=parseInt(a,10))?e:e>>>0;if(d.Long){if(this.type== d.TYPES.int64||this.type==d.TYPES.sint64||this.type==d.TYPES.sfixed64)try{return c(a,!1)}catch(g){throw Error("Illegal value for "+this.toString(!0)+": "+a+" ("+g.message+")");}if(this.type==d.TYPES.uint64||this.type==d.TYPES.fixed64)try{return c(a,!0)}catch(k){throw Error("Illegal value for "+this.toString(!0)+": "+a+" ("+k.message+")");}}if(this.type==d.TYPES.bool)return"string"===typeof a?"true"===a:!!a;if(this.type==d.TYPES["float"]||this.type==d.TYPES["double"])return parseFloat(a);if(this.type== d.TYPES.string)return""+a;if(this.type==d.TYPES.bytes)return a&&a instanceof m?a:m.wrap(a);if(this.type==d.TYPES["enum"]){f=this.resolvedType.getChildren(h.Value);for(e=0;e<f.length;e++)if(f[e].name==a||f[e].id==a)return f[e].id;throw Error("Illegal value for "+this.toString(!0)+": "+a+" (not a valid enum value)");}if(this.type==d.TYPES.message||this.type==d.TYPES.group){if("object"!==typeof a)throw Error("Illegal value for "+this.toString(!0)+": "+a+" (object expected)");return a instanceof this.resolvedType.clazz? a:new this.resolvedType.clazz(a)}throw Error("[INTERNAL] Illegal value for "+this.toString(!0)+": "+a+" (undefined type "+this.type+")");};e.prototype.encode=function(a,b){a=this.verifyValue(a);if(null==this.type||"object"!=typeof this.type)throw Error("[INTERNAL] Unresolved type in "+this.toString(!0)+": "+this.type);if(null===a||this.repeated&&0==a.length)return b;try{if(this.repeated){var c;if(this.options.packed){b.writeVarint32(this.id<<3|d.WIRE_TYPES.LDELIM);b.ensureCapacity(b.offset+=1);var e= b.offset;for(c=0;c<a.length;c++)this.encodeValue(a[c],b);var f=b.offset-e,h=m.calculateVarint32(f);if(1<h){var g=b.slice(e,b.offset),e=e+(h-1);b.offset=e;b.append(g)}b.writeVarint32(f,e-h)}else for(c=0;c<a.length;c++)b.writeVarint32(this.id<<3|this.type.wireType),this.encodeValue(a[c],b)}else b.writeVarint32(this.id<<3|this.type.wireType),this.encodeValue(a,b)}catch(k){throw Error("Illegal value for "+this.toString(!0)+": "+a+" ("+k+")");}return b};e.prototype.encodeValue=function(a,b){if(null!== a){if(this.type==d.TYPES.int32)0>a?b.writeVarint64(a):b.writeVarint32(a);else if(this.type==d.TYPES.uint32)b.writeVarint32(a);else if(this.type==d.TYPES.sint32)b.writeVarint32ZigZag(a);else if(this.type==d.TYPES.fixed32)b.writeUint32(a);else if(this.type==d.TYPES.sfixed32)b.writeInt32(a);else if(this.type==d.TYPES.int64||this.type==d.TYPES.uint64)b.writeVarint64(a);else if(this.type==d.TYPES.sint64)b.writeVarint64ZigZag(a);else if(this.type==d.TYPES.fixed64)b.writeUint64(a);else if(this.type==d.TYPES.sfixed64)b.writeInt64(a); else if(this.type==d.TYPES.bool)"string"===typeof a?b.writeVarint32("false"===a.toLowerCase()?0:!!a):b.writeVarint32(a?1:0);else if(this.type==d.TYPES["enum"])b.writeVarint32(a);else if(this.type==d.TYPES["float"])b.writeFloat32(a);else if(this.type==d.TYPES["double"])b.writeFloat64(a);else if(this.type==d.TYPES.string)b.writeVString(a);else if(this.type==d.TYPES.bytes){a.offset>a.length&&(b=b.clone().flip());var c=a.offset;b.writeVarint32(a.remaining());b.append(a);a.offset=c}else if(this.type== d.TYPES.message)c=(new m).LE(),this.resolvedType.encode(a,c),b.writeVarint32(c.offset),b.append(c.flip());else if(this.type==d.TYPES.group)this.resolvedType.encode(a,b),b.writeVarint32(this.id<<3|d.WIRE_TYPES.ENDGROUP);else throw Error("[INTERNAL] Illegal value to encode in "+this.toString(!0)+": "+a+" (unknown type)");return b}};e.prototype.decode=function(a,b,c){if(a!=this.type.wireType&&(c||a!=d.WIRE_TYPES.LDELIM||!this.repeated))throw Error("Illegal wire type for field "+this.toString(!0)+": "+ a+" ("+this.type.wireType+" expected)");if(a==d.WIRE_TYPES.LDELIM&&this.repeated&&this.options.packed&&!c){a=b.readVarint32();a=b.offset+a;for(c=[];b.offset<a;)c.push(this.decode(this.type.wireType,b,!0));return c}if(this.type==d.TYPES.int32)return b.readVarint32()|0;if(this.type==d.TYPES.uint32)return b.readVarint32()>>>0;if(this.type==d.TYPES.sint32)return b.readVarint32ZigZag()|0;if(this.type==d.TYPES.fixed32)return b.readUint32()>>>0;if(this.type==d.TYPES.sfixed32)return b.readInt32()|0;if(this.type== d.TYPES.int64)return b.readVarint64();if(this.type==d.TYPES.uint64)return b.readVarint64().toUnsigned();if(this.type==d.TYPES.sint64)return b.readVarint64ZigZag();if(this.type==d.TYPES.fixed64)return b.readUint64();if(this.type==d.TYPES.sfixed64)return b.readInt64();if(this.type==d.TYPES.bool)return!!b.readVarint32();if(this.type==d.TYPES["enum"])return b.readVarint32();if(this.type==d.TYPES["float"])return b.readFloat();if(this.type==d.TYPES["double"])return b.readDouble();if(this.type==d.TYPES.string)return b.readVString(); if(this.type==d.TYPES.bytes){a=b.readVarint32();if(b.remaining()<a)throw Error("Illegal number of bytes for "+this.toString(!0)+": "+a+" required but got only "+b.remaining());c=b.clone();c.length=c.offset+a;b.offset+=a;return c}if(this.type==d.TYPES.message)return a=b.readVarint32(),this.resolvedType.decode(b,a);if(this.type==d.TYPES.group)return this.resolvedType.decode(b,b.remaining());throw Error("[INTERNAL] Illegal wire type for "+this.toString(!0)+": "+a);};g.Message.Field=e;var h=function(b, c,d){a.call(this,b,c,d);this.object=null};h.prototype=Object.create(a.prototype);h.prototype.build=function(){for(var a={},b=this.getChildren(h.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.object=a};g.Enum=h;e=function(a,b,c){f.call(this,a,b);this.id=c};e.prototype=Object.create(f.prototype);g.Enum.Value=e;var n=function(b,c,d){a.call(this,b,c,d);this.clazz=null}; n.prototype=Object.create(a.prototype);n.prototype.build=function(a){return this.clazz&&!a?this.clazz:this.clazz=function(a,b){var c=function(b){a.Builder.Service.call(this);this.rpcImpl=b||function(a,b,c){setTimeout(c.bind(this,Error("Not implemented, see: https://github.com/dcodeIO/ProtoBuf.js/wiki/Services")),0)}};c.prototype=Object.create(a.Builder.Service.prototype);Object.defineProperty&&(Object.defineProperty(c,"$options",{value:b.buildOpt(),enumerable:!1,configurable:!1,writable:!1}),Object.defineProperty(c.prototype, "$options",{value:c.$options,enumerable:!1,configurable:!1,writable:!1}));for(var d=b.getChildren(g.Service.RPCMethod),e=0;e<d.length;e++)(function(a){c.prototype[a.name]=function(c,d){try{c&&c instanceof a.resolvedRequestType.clazz||setTimeout(d.bind(this,Error("Illegal request type provided to service method "+b.name+"#"+a.name))),this.rpcImpl(a.fqn(),c,function(c,e){if(c)d(c);else{try{e=a.resolvedResponseType.clazz.decode(e)}catch(f){}e&&e instanceof a.resolvedResponseType.clazz?d(null,e):d(Error("Illegal response type received in service method "+ b.name+"#"+a.name))}})}catch(e){setTimeout(d.bind(this,e),0)}};c[a.name]=function(b,d,e){(new c(b))[a.name](d,e)};Object.defineProperty&&(Object.defineProperty(c[a.name],"$options",{value:a.buildOpt(),enumerable:!1,configurable:!1,writable:!1}),Object.defineProperty(c.prototype[a.name],"$options",{value:c[a.name].$options,enumerable:!1,configurable:!1,writable:!1}))})(d[e]);return c}(d,this)};g.Service=n;var k=function(a,b,c){f.call(this,a,b);this.options=c||{}};k.prototype=Object.create(f.prototype); k.prototype.buildOpt=a.prototype.buildOpt;g.Service.Method=k;e=function(a,b,c,d,e){k.call(this,a,b,e);this.requestName=c;this.responseName=d;this.resolvedResponseType=this.resolvedRequestType=null};e.prototype=Object.create(k.prototype);g.Service.RPCMethod=e;return g}(g);g.Builder=function(d,c,g){var f=function(){this.ptr=this.ns=new g.Namespace(null,"");this.resolved=!1;this.result=null;this.files={};this.importRoot=null};f.prototype.reset=function(){this.ptr=this.ns};f.prototype.define=function(a, b){if("string"!==typeof a||!c.TYPEREF.test(a))throw Error("Illegal package name: "+a);var d=a.split("."),f;for(f=0;f<d.length;f++)if(!c.NAME.test(d[f]))throw Error("Illegal package name: "+d[f]);for(f=0;f<d.length;f++)this.ptr.hasChild(d[f])||this.ptr.addChild(new g.Namespace(this.ptr,d[f],b)),this.ptr=this.ptr.getChild(d[f]);return this};f.isValidMessage=function(a){if("string"!==typeof a.name||!c.NAME.test(a.name)||"undefined"!==typeof a.values||"undefined"!==typeof a.rpc)return!1;var b;if("undefined"!== typeof a.fields){if(!d.Util.isArray(a.fields))return!1;var e=[],g;for(b=0;b<a.fields.length;b++){if(!f.isValidMessageField(a.fields[b]))return!1;g=parseInt(a.fields[b].id,10);if(0<=e.indexOf(g))return!1;e.push(g)}}if("undefined"!==typeof a.enums){if(!d.Util.isArray(a.enums))return!1;for(b=0;b<a.enums.length;b++)if(!f.isValidEnum(a.enums[b]))return!1}if("undefined"!==typeof a.messages){if(!d.Util.isArray(a.messages))return!1;for(b=0;b<a.messages.length;b++)if(!f.isValidMessage(a.messages[b])&&!f.isValidExtend(a.messages[b]))return!1}return"undefined"=== typeof a.extensions||d.Util.isArray(a.extensions)&&2===a.extensions.length&&"number"===typeof a.extensions[0]&&"number"===typeof a.extensions[1]?!0:!1};f.isValidMessageField=function(a){if("string"!==typeof a.rule||"string"!==typeof a.name||"string"!==typeof a.type||"undefined"===typeof a.id||!(c.RULE.test(a.rule)&&c.NAME.test(a.name)&&c.TYPEREF.test(a.type)&&c.ID.test(""+a.id)))return!1;if("undefined"!=typeof a.options){if("object"!=typeof a.options)return!1;for(var b=Object.keys(a.options),d=0;d< b.length;d++)if(!c.OPTNAME.test(b[d])||"string"!==typeof a.options[b[d]]&&"number"!==typeof a.options[b[d]]&&"boolean"!==typeof a.options[b[d]])return!1}return!0};f.isValidEnum=function(a){if("string"!==typeof a.name||!c.NAME.test(a.name)||"undefined"===typeof a.values||!d.Util.isArray(a.values)||0==a.values.length)return!1;for(var b=0;b<a.values.length;b++)if("object"!=typeof a.values[b]||"string"!==typeof a.values[b].name||"undefined"===typeof a.values[b].id||!c.NAME.test(a.values[b].name)||!c.NEGID.test(""+ a.values[b].id))return!1;return!0};f.prototype.create=function(a){if(a&&(d.Util.isArray(a)||(a=[a]),0!=a.length)){var b=[],e,h,n,k,l;for(b.push(a);0<b.length;){a=b.pop();if(d.Util.isArray(a))for(;0<a.length;)if(e=a.shift(),f.isValidMessage(e)){h=new g.Message(this.ptr,e.name,e.options,e.groupId);if(e.fields&&0<e.fields.length)for(k=0;k<e.fields.length;k++){if(h.hasChild(e.fields[k].id))throw Error("Duplicate field id in message "+h.name+": "+e.fields[k].id);if(e.fields[k].options)for(n=Object.keys(e.fields[k].options), l=0;l<n.length;l++){if(!c.OPTNAME.test(n[l]))throw Error("Illegal field option name in message "+h.name+"#"+e.fields[k].name+": "+n[l]);if("string"!==typeof e.fields[k].options[n[l]]&&"number"!==typeof e.fields[k].options[n[l]]&&"boolean"!==typeof e.fields[k].options[n[l]])throw Error("Illegal field option value in message "+h.name+"#"+e.fields[k].name+"#"+n[l]+": "+e.fields[k].options[n[l]]);}h.addChild(new g.Message.Field(h,e.fields[k].rule,e.fields[k].type,e.fields[k].name,e.fields[k].id,e.fields[k].options))}n= [];if("undefined"!==typeof e.enums&&0<e.enums.length)for(k=0;k<e.enums.length;k++)n.push(e.enums[k]);if(e.messages&&0<e.messages.length)for(k=0;k<e.messages.length;k++)n.push(e.messages[k]);e.extensions&&(h.extensions=e.extensions,h.extensions[0]<d.Lang.ID_MIN&&(h.extensions[0]=d.Lang.ID_MIN),h.extensions[1]>d.Lang.ID_MAX&&(h.extensions[1]=d.Lang.ID_MAX));this.ptr.addChild(h);0<n.length&&(b.push(a),a=n,this.ptr=h)}else if(f.isValidEnum(e)){h=new g.Enum(this.ptr,e.name,e.options);for(k=0;k<e.values.length;k++)h.addChild(new g.Enum.Value(h, e.values[k].name,e.values[k].id));this.ptr.addChild(h)}else if(f.isValidService(e)){h=new g.Service(this.ptr,e.name,e.options);for(k in e.rpc)e.rpc.hasOwnProperty(k)&&h.addChild(new g.Service.RPCMethod(h,k,e.rpc[k].request,e.rpc[k].response,e.rpc[k].options));this.ptr.addChild(h)}else if(f.isValidExtend(e))if(h=this.ptr.resolve(e.ref))for(k=0;k<e.fields.length;k++){if(h.hasChild(e.fields[k].id))throw Error("Duplicate extended field id in message "+h.name+": "+e.fields[k].id);if(e.fields[k].id<h.extensions[0]|| e.fields[k].id>h.extensions[1])throw Error("Illegal extended field id in message "+h.name+": "+e.fields[k].id+" ("+h.extensions.join(" to ")+" expected)");h.addChild(new g.Message.Field(h,e.fields[k].rule,e.fields[k].type,e.fields[k].name,e.fields[k].id,e.fields[k].options))}else{if(!/\.?google\.protobuf\./.test(e.ref))throw Error("Extended message "+e.ref+" is not defined");}else throw Error("Not a valid message, enum, service or extend 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}};f.isValidImport=function(a){return!/google\/protobuf\//.test(a)};f.prototype["import"]=function(a,b){if("string"===typeof b){d.Util.IS_NODE&&(b=require("path").resolve(b));if(this.files[b])return this.reset(),this;this.files[b]=!0}if(a.imports&&0<a.imports.length){var c,g="/",n=!1;if("object"===typeof b){if(this.importRoot=b.root,n=!0,c=this.importRoot,b=b.file,0<=c.indexOf("\\")||0<=b.indexOf("\\"))g="\\"}else"string"=== typeof b?this.importRoot?c=this.importRoot:0<=b.indexOf("/")?(c=b.replace(/\/[^\/]*$/,""),""===c&&(c="/")):0<=b.indexOf("\\")?(c=b.replace(/\\[^\\]*$/,""),g="\\"):c=".":c=null;for(var k=0;k<a.imports.length;k++)if("string"===typeof a.imports[k]){if(!c)throw Error("Cannot determine import root: File name is unknown");var l=c+g+a.imports[k];if(f.isValidImport(l)){/\.proto$/i.test(l)&&!d.DotProto&&(l=l.replace(/\.proto$/,".json"));var m=d.Util.fetch(l);if(null===m)throw Error("Failed to import '"+l+ "' in '"+b+"': File not found");if(/\.json$/i.test(l))this["import"](JSON.parse(m+""),l);else this["import"]((new d.DotProto.Parser(m+"")).parse(),l)}}else if(b)if(/\.(\w+)$/.test(b))this["import"](a.imports[k],b.replace(/^(.+)\.(\w+)$/,function(a,b,c){return b+"_import"+k+"."+c}));else this["import"](a.imports[k],b+"_import"+k);else this["import"](a.imports[k]);n&&(this.importRoot=null)}a.messages&&(a["package"]&&this.define(a["package"],a.options),this.create(a.messages),this.reset());a.enums&& (a["package"]&&this.define(a["package"],a.options),this.create(a.enums),this.reset());a.services&&(a["package"]&&this.define(a["package"],a.options),this.create(a.services),this.reset());a["extends"]&&(a["package"]&&this.define(a["package"],a.options),this.create(a["extends"]),this.reset());return this};f.isValidService=function(a){return"string"===typeof a.name&&c.NAME.test(a.name)&&"object"===typeof a.rpc?!0:!1};f.isValidExtend=function(a){if("string"!==typeof a.ref||!c.TYPEREF.test(a.name))return!1; var b;if("undefined"!==typeof a.fields){if(!d.Util.isArray(a.fields))return!1;var e=[],g;for(b=0;b<a.fields.length;b++){if(!f.isValidMessageField(a.fields[b]))return!1;g=parseInt(a.id,10);if(0<=e.indexOf(g))return!1;e.push(g)}}return!0};f.prototype.resolveAll=function(){var a;if(null!=this.ptr&&"object"!==typeof this.ptr.type){if(this.ptr instanceof g.Namespace){a=this.ptr.getChildren();for(var b=0;b<a.length;b++)this.ptr=a[b],this.resolveAll()}else if(this.ptr instanceof g.Message.Field)if(c.TYPE.test(this.ptr.type))this.ptr.type= d.TYPES[this.ptr.type];else{if(!c.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,!0);if(!a)throw Error("Unresolvable type reference in "+this.ptr.toString(!0)+": "+this.ptr.type);this.ptr.resolvedType=a;if(a instanceof g.Enum)this.ptr.type=d.TYPES["enum"];else if(a instanceof g.Message)this.ptr.type="undefined"===typeof a.groupId?d.TYPES.message:d.TYPES.group;else throw Error("Illegal type reference in "+ this.ptr.toString(!0)+": "+this.ptr.type);}else if(!(this.ptr instanceof d.Reflect.Enum.Value))if(this.ptr instanceof d.Reflect.Service.Method)if(this.ptr instanceof d.Reflect.Service.RPCMethod){a=this.ptr.parent.resolve(this.ptr.requestName);if(!(a&&a instanceof d.Reflect.Message))throw Error("Illegal request type reference in "+this.ptr.toString(!0)+": "+this.ptr.requestName);this.ptr.resolvedRequestType=a;a=this.ptr.parent.resolve(this.ptr.responseName);if(!(a&&a instanceof d.Reflect.Message))throw Error("Illegal response type reference in "+ this.ptr.toString(!0)+": "+this.ptr.responseName);this.ptr.resolvedResponseType=a}else throw Error("Illegal service method type in "+this.ptr.toString(!0));else throw Error("Illegal object type in namespace: "+typeof this.ptr+":"+this.ptr);this.reset()}};f.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};f.prototype.lookup=function(a){return a?this.ns.resolve(a):this.ns};f.prototype.toString=function(){return"Builder"};f.Message=function(){};f.Service=function(){};return f}(g,g.Lang,g.Reflect);g.loadProto=function(d,c,m){if("string"==typeof c||c&&"string"===typeof c.file&&"string"===typeof c.root)m=c,c=null;return g.loadJson((new g.DotProto.Parser(d+"")).parse(),c,m)};g.protoFromString=g.loadProto;g.loadProtoFile=function(d,c,m){c&&"object"===typeof c?(m=c, c=null):c&&"function"===typeof c||(c=null);if(c)g.Util.fetch("object"===typeof d?d.root+"/"+d.file:d,function(a){c(g.loadProto(a,m,d))});else{var f=g.Util.fetch("object"===typeof d?d.root+"/"+d.file:d);return null!==f?g.protoFromString(f,m,d):null}};g.protoFromFile=g.loadProtoFile;g.newBuilder=function(d,c){var m=new g.Builder;"undefined"!==typeof d&&null!==d&&m.define(d,c);return m};g.loadJson=function(d,c,m){if("string"===typeof c||c&&"string"===typeof c.file&&"string"===typeof c.root)m=c,c=null; c&&"object"===typeof c||(c=g.newBuilder());"string"===typeof d&&(d=JSON.parse(d));c["import"](d,m);c.resolveAll();c.build();return c};g.loadJsonFile=function(d,c,m){c&&"object"===typeof c?(m=c,c=null):c&&"function"===typeof c||(c=null);if(c)g.Util.fetch("object"===typeof d?d.root+"/"+d.file:d,function(a){try{c(g.loadJson(JSON.parse(a),m,d))}catch(b){c(b)}});else{var f=g.Util.fetch("object"===typeof d?d.root+"/"+d.file:d);return null!==f?g.loadJson(JSON.parse(f),m,d):null}};return g}"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);