-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProtoBuf.noparse.min.js
72 lines (72 loc) · 33.6 KB
/
ProtoBuf.noparse.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
65
66
67
68
69
70
71
72
/*
ProtoBuf.js (c) 2013 Daniel Wirtz <dcode@dcode.io>
Released under the Apache License, Version 2.0
see: https://github.com/dcodeIO/ProtoBuf.js for details
*/
(function(s){function t(h){var g={VERSION:"3.3.0",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.PACKABLE_WIRE_TYPES=[g.WIRE_TYPES.VARINT,g.WIRE_TYPES.BITS64,g.WIRE_TYPES.BITS32];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.ID_MIN=1;g.ID_MAX=536870911;g.ByteBuffer=h;g.Long=h.Long||null;g.convertFieldsToCamelCase=!1;g.populateAccessors=!0;g.Util=function(){Object.create||(Object.create=function(d){function m(){}if(1<arguments.length)throw Error("Object.create polyfill only accepts the first parameter.");m.prototype=
d;return new m});var d={IS_NODE:!1};try{d.IS_NODE="function"===typeof require&&"function"===typeof require("fs").readFileSync&&"function"===typeof require("path").resolve}catch(g){}d.XHR=function(){for(var d=[function(){return new XMLHttpRequest},function(){return new ActiveXObject("Msxml2.XMLHTTP")},function(){return new ActiveXObject("Msxml3.XMLHTTP")},function(){return new ActiveXObject("Microsoft.XMLHTTP")}],m=null,b=0;b<d.length;b++){try{m=d[b]()}catch(e){continue}break}if(!m)throw Error("XMLHttpRequest is not supported");
return m};d.fetch=function(g,m){m&&"function"!=typeof m&&(m=null);if(d.IS_NODE)if(m)require("fs").readFile(g,function(d,b){d?m(null):m(""+b)});else try{return require("fs").readFileSync(g)}catch(b){return null}else{var e=d.XHR();e.open("GET",g,m?!0:!1);e.setRequestHeader("Accept","text/plain");"function"===typeof e.overrideMimeType&&e.overrideMimeType("text/plain");if(m)e.onreadystatechange=function(){4==e.readyState&&(200==e.status||0==e.status&&"string"===typeof e.responseText?m(e.responseText):
m(null))},4!=e.readyState&&e.send(null);else return e.send(null),200==e.status||0==e.status&&"string"===typeof e.responseText?e.responseText:null}};d.isArray=Array.isArray||function(d){return"[object Array]"===Object.prototype.toString.call(d)};return d}();g.Lang={OPEN:"{",CLOSE:"}",OPTOPEN:"[",OPTCLOSE:"]",OPTEND:",",EQUAL:"=",END:";",STRINGOPEN:'"',STRINGCLOSE:'"',STRINGOPEN_SQ:"'",STRINGCLOSE_SQ:"'",COPTOPEN:"(",COPTCLOSE:")",DELIM:/[\s\{\}=;\[\],'"\(\)]/g,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]*)+$/,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};g.Reflect=function(d){function g(a,f){var b=f.readVarint32(),c=b&7,b=b>>3;switch(c){case d.WIRE_TYPES.VARINT:do b=f.readUint8();while(128===(b&128));break;case d.WIRE_TYPES.BITS64:f.offset+=8;break;case d.WIRE_TYPES.LDELIM:b=f.readVarint32();f.offset+=b;break;case d.WIRE_TYPES.STARTGROUP:g(b,f);break;case d.WIRE_TYPES.ENDGROUP:if(b===a)return!1;throw Error("Illegal GROUPEND after unknown group: "+
b+" ("+a+" expected)");case d.WIRE_TYPES.BITS32:f.offset+=4;break;default:throw Error("Illegal wire type in unknown group "+a+": "+c);}return!0}function p(a,f){if(a&&"number"===typeof a.low&&"number"===typeof a.high&&"boolean"===typeof a.unsigned&&a.low===a.low&&a.high===a.high)return new d.Long(a.low,a.high,"undefined"===typeof f?a.unsigned:f);if("string"===typeof a)return d.Long.fromString(a,f||!1,10);if("number"===typeof a)return d.Long.fromNumber(a,f||!1);throw Error("not convertible to Long");
}var m={},b=function(a,f){this.parent=a;this.name=f};b.prototype.fqn=function(){var a=this.name,f=this;do{f=f.parent;if(null==f)break;a=f.name+"."+a}while(1);return a};b.prototype.toString=function(a){return(a?this.className+" ":"")+this.fqn()};b.prototype.build=function(){throw Error(this.toString(!0)+" cannot be built directly");};m.T=b;var e=function(a,f,d){b.call(this,a,f);this.className="Namespace";this.children=[];this.options=d||{}};e.prototype=Object.create(b.prototype);e.prototype.getChildren=
function(a){a=a||null;if(null==a)return this.children.slice();for(var f=[],b=0,d=this.children.length;b<d;++b)this.children[b]instanceof a&&(a===c.Field&&this.children[b]instanceof c.ExtensionField||f.push(this.children[b]));return f};e.prototype.addChild=function(a){var f;if(f=this.getChild(a.name))if(f instanceof c.Field&&f.name!==f.originalName&&!this.hasChild(f.originalName))f.name=f.originalName;else if(a instanceof c.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)};e.prototype.hasChild=function(a){return-1<this._indexOf(a)};e.prototype.getChild=function(a){a=this._indexOf(a);return-1<a?this.children[a]:null};e.prototype._indexOf=function(a){for(var f="number"===typeof a?"id":"name",b=0;b<this.children.length;b++)if("undefined"!==typeof this.children[b][f]&&this.children[b][f]==a)return b;return-1};e.prototype.resolve=function(a,f){var b=a.split("."),d=this,
c=0;if(""==b[c]){for(;null!=d.parent;)d=d.parent;c++}do{do{d=d.getChild(b[c]);if(!(d&&d instanceof m.T)||f&&d instanceof m.Message.Field){d=null;break}c++}while(c<b.length);if(null!=d)break;if(null!==this.parent)return this.parent.resolve(a,f)}while(null!=d);return d};e.prototype.build=function(){for(var a={},f=this.getChildren(),b,d=0,c=f.length;d<c;++d)b=f[d],b instanceof e&&(a[b.name]=b.build());Object.defineProperty&&Object.defineProperty(a,"$options",{value:this.buildOpt()});return a};e.prototype.buildOpt=
function(){for(var a={},f=Object.keys(this.options),b=0;b<f.length;b++)a[f[b]]=this.options[f[b]];return a};e.prototype.getOption=function(a){return"undefined"===typeof a?this.options:"undefined"!==typeof this.options[a]?this.options[a]:null};m.Namespace=e;var c=function(a,f,b,c){e.call(this,a,f,b);this.className="Message";this.extensions=[d.ID_MIN,d.ID_MAX];this.clazz=null;this.isGroup=!!c};c.prototype=Object.create(e.prototype);c.prototype.build=function(a){if(this.clazz&&!a)return this.clazz;a=
function(a,b){function d(a,b){var f={},c;for(c in a)a.hasOwnProperty(c)&&(null===a[c]||"object"!==typeof a[c]?f[c]=a[c]:a[c]instanceof h?b&&(f[c]=a.toBuffer()):f[c]=d(a[c],b));return f}var f=b.getChildren(a.Reflect.Message.Field),c=function(d){a.Builder.Message.call(this);var c,e;Object.defineProperty&&Object.defineProperty(this,"$type",{get:function(){return b}});for(c=0;c<f.length;c++)e=f[c],this[e.name]=e.repeated?[]:null;for(c=0;c<f.length;c++)if(e=f[c],"undefined"!=typeof e.options["default"])try{this.$set(e.name,
e.options["default"])}catch(r){throw Error("[INTERNAL] "+r);}if(1!=arguments.length||"object"!=typeof d||"function"==typeof d.encode||a.Util.isArray(d)||d instanceof h||d instanceof ArrayBuffer||a.Long&&d instanceof a.Long)for(c=0;c<arguments.length;c++)c<f.length&&this.$set(f[c].name,arguments[c]);else for(e=Object.keys(d),c=0;c<e.length;c++)this.$set(e[c],d[e[c]])};c.prototype=Object.create(a.Builder.Message.prototype);c.prototype.add=function(f,d,c){var e=b.getChild(f);if(!e)throw Error(this+"#"+
f+" is undefined");if(!(e instanceof a.Reflect.Message.Field))throw Error(this+"#"+f+" is not a field: "+e.toString(!0));if(!e.repeated)throw Error(this+"#"+f+" is not a repeated field");null===this[e.name]&&(this[e.name]=[]);this[e.name].push(c?d:e.verifyValue(d,!0))};c.prototype.$add=c.prototype.add;c.prototype.set=function(f,d,c){var e=b.getChild(f);if(!e)throw Error(this+"#"+f+" is not a field: undefined");if(!(e instanceof a.Reflect.Message.Field))throw Error(this+"#"+f+" is not a field: "+e.toString(!0));
this[e.name]=c?d:e.verifyValue(d)};c.prototype.$set=c.prototype.set;c.prototype.get=function(f){var d=b.getChild(f);if(!(d&&d instanceof a.Reflect.Message.Field))throw Error(this+"#"+f+" is not a field: undefined");if(!(d instanceof a.Reflect.Message.Field))throw Error(this+"#"+f+" is not a field: "+d.toString(!0));return this[d.name]};c.prototype.$get=c.prototype.get;for(var e=0;e<f.length;e++){var r=f[e];a.populateAccessors&&function(a){var f=a.originalName.replace(/(_[a-zA-Z])/g,function(a){return a.toUpperCase().replace("_",
"")}),f=f.substring(0,1).toUpperCase()+f.substring(1),d=a.originalName.replace(/([A-Z])/g,function(a){return"_"+a});b.hasChild("set"+f)||(c.prototype["set"+f]=function(b){this.$set(a.name,b)});b.hasChild("set_"+d)||(c.prototype["set_"+d]=function(b){this.$set(a.name,b)});b.hasChild("get"+f)||(c.prototype["get"+f]=function(){return this.$get(a.name)});b.hasChild("get_"+d)||(c.prototype["get_"+d]=function(){return this.$get(a.name)})}(r)}c.prototype.encode=function(a){var f=!1;a||(a=new h,f=!0);var d=
a.littleEndian;try{return b.encode(this,a.LE()),(f?a.flip():a).LE(d)}catch(c){throw a.LE(d),c;}};c.prototype.calculate=function(){return b.calculate(this)};c.prototype.encodeDelimited=function(a){var f=!1;a||(a=new h,f=!0);var d=(new h).LE();b.encode(this,d).flip();a.writeVarint32(d.remaining());a.append(d);return f?a.flip():a};c.prototype.encodeAB=function(){try{return this.encode().toArrayBuffer()}catch(a){throw a.encoded&&(a.encoded=a.encoded.toArrayBuffer()),a;}};c.prototype.toArrayBuffer=c.prototype.encodeAB;
c.prototype.encodeNB=function(){try{return this.encode().toBuffer()}catch(a){throw a.encoded&&(a.encoded=a.encoded.toBuffer()),a;}};c.prototype.toBuffer=c.prototype.encodeNB;c.prototype.encode64=function(){try{return this.encode().toBase64()}catch(a){throw a.encoded&&(a.encoded=a.encoded.toBase64()),a;}};c.prototype.toBase64=c.prototype.encode64;c.prototype.encodeHex=function(){try{return this.encode().toHex()}catch(a){throw a.encoded&&(a.encoded=a.encoded.toHex()),a;}};c.prototype.toHex=c.prototype.encodeHex;
c.prototype.toRaw=function(a){return d(this,!!a)};c.decode=function(a,f){"string"===typeof a&&(a=h.wrap(a,f?f:"base64"));a=a instanceof h?a:h.wrap(a);var d=a.littleEndian;try{var c=b.decode(a.LE());a.LE(d);return c}catch(e){throw a.LE(d),e;}};c.decodeDelimited=function(a,f){"string"===typeof a&&(a=h.wrap(a,f?f:"base64"));a=a instanceof h?a:h.wrap(a);if(1>a.remaining())return null;var d=a.offset,c=a.readVarint32();if(a.remaining()<c)return a.offset=d,null;try{var e=b.decode(a.slice(a.offset,a.offset+
c).LE());a.offset+=c;return e}catch(r){throw a.offset+=c,r;}};c.decode64=function(a){return c.decode(a,"base64")};c.decodeHex=function(a){return c.decode(a,"hex")};c.prototype.toString=function(){return b.toString()};Object.defineProperty&&Object.defineProperty(c,"$options",{value:b.buildOpt()});return c}(d,this);for(var b=this.getChildren(),e=0;e<b.length;e++)if(b[e]instanceof l)a[b[e].name]=b[e].build();else if(b[e]instanceof c)a[b[e].name]=b[e].build();else if(!(b[e]instanceof c.Field))throw Error("Illegal reflect child of "+
this.toString(!0)+": "+b[e].toString(!0));return this.clazz=a};c.prototype.encode=function(a,f){for(var b=this.getChildren(c.Field),d=null,e=0,k;e<b.length;e++)k=a.$get(b[e].name),b[e].required&&null===k?null===d&&(d=b[e]):b[e].encode(k,f);if(null!==d)throw b=Error("Missing at least one required field for "+this.toString(!0)+": "+d),b.encoded=f,b;return f};c.prototype.calculate=function(a){for(var b=this.getChildren(c.Field),d=0,e=0,k;e<b.length;e++){k=a.$get(b[e].name);if(b[e].required&&null===k)throw Error("Missing at least one required field for "+
this.toString(!0)+": "+b[e]);d+=b[e].calculate(k)}return d};c.prototype.decode=function(a,b,c){b="number"===typeof b?b:-1;for(var e=a.offset,k=new this.clazz,l,m;a.offset<e+b||-1==b&&0<a.remaining();){l=a.readVarint32();m=l&7;l>>=3;if(m===d.WIRE_TYPES.ENDGROUP){if(l!==c)throw Error("Illegal group end indicator for "+this.toString(!0)+": "+l+" ("+(c?c+" expected":"not a group")+")");break}var h=this.getChild(l);if(h)h.repeated&&!h.options.packed?k.$add(h.name,h.decode(m,a),!0):k.$set(h.name,h.decode(m,
a),!0);else switch(m){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:m=a.readVarint32();a.offset+=m;break;case d.WIRE_TYPES.STARTGROUP:for(;g(l,a););break;default:throw Error("Illegal wire type for unknown field "+l+" in "+this.toString(!0)+"#decode: "+m);}}a=this.getChildren(d.Reflect.Field);for(b=0;b<a.length;b++)if(a[b].required&&null===k[a[b].name])throw a=Error("Missing at least one required field for "+
this.toString(!0)+": "+a[b].name),a.decoded=k,a;return k};m.Message=c;var k=function(a,f,c,e,k,l){b.call(this,a,e);this.className="Message.Field";this.required="required"==f;this.repeated="repeated"==f;this.type=c;this.resolvedType=null;this.id=k;this.options=l||{};this.originalName=this.name;d.convertFieldsToCamelCase&&(this.name=this.name.replace(/_([a-zA-Z])/g,function(a,b){return b.toUpperCase()}))};k.prototype=Object.create(b.prototype);k.prototype.verifyValue=function(a,b){b=b||!1;var c=function(a,
b){throw Error("Illegal value for "+this.toString(!0)+" of type "+this.type.name+": "+a+" ("+b+")");}.bind(this);if(null===a)return this.required&&c(typeof a,"required"),null;var e;if(this.repeated&&!b){d.Util.isArray(a)||(a=[a]);c=[];for(e=0;e<a.length;e++)c.push(this.verifyValue(a[e],!0));return c}!this.repeated&&d.Util.isArray(a)&&c(typeof a,"no array expected");switch(this.type){case d.TYPES.int32:case d.TYPES.sint32:case d.TYPES.sfixed32:return("number"!==typeof a||a===a&&0!==a%1)&&c(typeof a,
"not an integer"),4294967295<a?a|0:a;case d.TYPES.uint32:case d.TYPES.fixed32:return("number"!==typeof a||a===a&&0!==a%1)&&c(typeof a,"not an integer"),0>a?a>>>0:a;case d.TYPES.int64:case d.TYPES.sint64:case d.TYPES.sfixed64:if(d.Long)try{return p(a,!1)}catch(k){c(typeof a,k.message)}else c(typeof a,"requires Long.js");case d.TYPES.uint64:case d.TYPES.fixed64:if(d.Long)try{return p(a,!0)}catch(m){c(typeof a,m.message)}else c(typeof a,"requires Long.js");case d.TYPES.bool:return"boolean"!==typeof a&&
c(typeof a,"not a boolean"),a;case d.TYPES["float"]:case d.TYPES["double"]:return"number"!==typeof a&&c(typeof a,"not a number"),a;case d.TYPES.string:return"string"===typeof a||a&&a instanceof String||c(typeof a,"not a string"),""+a;case d.TYPES.bytes:return a&&a instanceof h?a:h.wrap(a);case d.TYPES["enum"]:var g=this.resolvedType.getChildren(l.Value);for(e=0;e<g.length;e++)if(g[e].name==a||g[e].id==a)return g[e].id;c(a,"not a valid enum value");case d.TYPES.group:case d.TYPES.message:return a&&
"object"===typeof a||c(typeof a,"object expected"),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+")");};k.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&&
0<=d.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType)){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 k=b.offset-e,l=h.calculateVarint32(k);if(1<l){var m=b.slice(e,b.offset),e=e+(l-1);b.offset=e;b.append(m)}b.writeVarint32(k,e-l)}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(g){throw Error("Illegal value for "+
this.toString(!0)+": "+a+" ("+g+")");}return b};k.prototype.encodeValue=function(a,b){if(null===a)return b;switch(this.type){case d.TYPES.int32:0>a?b.writeVarint64(a):b.writeVarint32(a);break;case d.TYPES.uint32:b.writeVarint32(a);break;case d.TYPES.sint32:b.writeVarint32ZigZag(a);break;case d.TYPES.fixed32:b.writeUint32(a);break;case d.TYPES.sfixed32:b.writeInt32(a);break;case d.TYPES.int64:case d.TYPES.uint64:b.writeVarint64(a);break;case d.TYPES.sint64:b.writeVarint64ZigZag(a);break;case d.TYPES.fixed64:b.writeUint64(a);
break;case d.TYPES.sfixed64:b.writeInt64(a);break;case d.TYPES.bool:"string"===typeof a?b.writeVarint32("false"===a.toLowerCase()?0:!!a):b.writeVarint32(a?1:0);break;case d.TYPES["enum"]:b.writeVarint32(a);break;case d.TYPES["float"]:b.writeFloat32(a);break;case d.TYPES["double"]:b.writeFloat64(a);break;case d.TYPES.string:b.writeVString(a);break;case d.TYPES.bytes:if(0>a.remaining())throw Error("Illegal value for "+this.toString(!0)+": "+a.remaining()+" bytes remaining");var c=a.offset;b.writeVarint32(a.remaining());
b.append(a);a.offset=c;break;case d.TYPES.message:c=(new h).LE();this.resolvedType.encode(a,c);b.writeVarint32(c.offset);b.append(c.flip());break;case d.TYPES.group:this.resolvedType.encode(a,b);b.writeVarint32(this.id<<3|d.WIRE_TYPES.ENDGROUP);break;default:throw Error("[INTERNAL] Illegal value to encode in "+this.toString(!0)+": "+a+" (unknown type)");}return b};k.prototype.calculate=function(a){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 0;var b=0;try{if(this.repeated){var c,e;if(this.options.packed&&0<=d.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType)){b+=h.calculateVarint32(this.id<<3|d.WIRE_TYPES.LDELIM);for(c=e=0;c<a.length;c++)e+=this.calculateValue(a[c]);b+=h.calculateVarint32(e);b+=e}else for(c=0;c<a.length;c++)b+=h.calculateVarint32(this.id<<3|this.type.wireType),b+=this.calculateValue(a[c])}else b+=h.calculateVarint32(this.id<<3|this.type.wireType),
b+=this.calculateValue(a)}catch(k){throw Error("Illegal value for "+this.toString(!0)+": "+a+" ("+k+")");}return b};k.prototype.calculateValue=function(a){if(null===a)return 0;switch(this.type){case d.TYPES.int32:return 0>a?h.calculateVarint64(a):h.calculateVarint32(a);case d.TYPES.uint32:return h.calculateVarint32(a);case d.TYPES.sint32:return h.calculateVarint32(h.zigZagEncode32(a));case d.TYPES.fixed32:case d.TYPES.sfixed32:case d.TYPES["float"]:return 4;case d.TYPES.int64:case d.TYPES.uint64:return h.calculateVarint64(a);
case d.TYPES.sint64:return h.calculateVarint64(h.zigZagEncode64(a));case d.TYPES.fixed64:case d.TYPES.sfixed64:return 8;case d.TYPES.bool:return 1;case d.TYPES["enum"]:return h.calculateVarint32(a);case d.TYPES["double"]:return 8;case d.TYPES.string:return a=h.calculateUTF8Bytes(a),h.calculateVarint32(a)+a;case d.TYPES.bytes:if(0>a.remaining())throw Error("Illegal value for "+this.toString(!0)+": "+a.remaining()+" bytes remaining");return h.calculateVarint32(a.remaining())+a.remaining();case d.TYPES.message:return a=
this.resolvedType.calculate(a),h.calculateVarint32(a)+a;case d.TYPES.group:return a=this.resolvedType.calculate(a),a+h.calculateVarint32(this.id<<3|d.WIRE_TYPES.ENDGROUP)}throw Error("[INTERNAL] Illegal value to encode in "+this.toString(!0)+": "+a+" (unknown type)");};k.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&&0<=d.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType)&&!c){a=b.readVarint32();a=b.offset+a;for(c=[];b.offset<a;)c.push(this.decode(this.type.wireType,b,!0));return c}switch(this.type){case d.TYPES.int32:return b.readVarint32()|0;case d.TYPES.uint32:return b.readVarint32()>>>0;case d.TYPES.sint32:return b.readVarint32ZigZag()|0;case d.TYPES.fixed32:return b.readUint32()>>>0;case d.TYPES.sfixed32:return b.readInt32()|0;case d.TYPES.int64:return b.readVarint64();case d.TYPES.uint64:return b.readVarint64().toUnsigned();
case d.TYPES.sint64:return b.readVarint64ZigZag();case d.TYPES.fixed64:return b.readUint64();case d.TYPES.sfixed64:return b.readInt64();case d.TYPES.bool:return!!b.readVarint32();case d.TYPES["enum"]:return b.readVarint32();case d.TYPES["float"]:return b.readFloat();case d.TYPES["double"]:return b.readDouble();case d.TYPES.string:return b.readVString();case 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.limit=c.offset+a;b.offset+=a;return c;case d.TYPES.message:return a=b.readVarint32(),this.resolvedType.decode(b,a);case d.TYPES.group:return this.resolvedType.decode(b,-1,this.id)}throw Error("[INTERNAL] Illegal wire type for "+this.toString(!0)+": "+a);};m.Message.Field=k;var q=function(a,b,c,d,e,l){k.call(this,a,b,c,d,e,l)};q.prototype=Object.create(k.prototype);m.Message.ExtensionField=q;var l=function(a,b,c){e.call(this,a,b,c);this.className="Enum";this.object=null};
l.prototype=Object.create(e.prototype);l.prototype.build=function(){for(var a={},b=this.getChildren(l.Value),c=0;c<b.length;c++)a[b[c].name]=b[c].id;Object.defineProperty&&Object.defineProperty(a,"$options",{value:this.buildOpt()});return this.object=a};m.Enum=l;q=function(a,c,d){b.call(this,a,c);this.className="Enum.Value";this.id=d};q.prototype=Object.create(b.prototype);m.Enum.Value=q;q=function(a,b,c){e.call(this,a,b,c);this.className="Service";this.clazz=null};q.prototype=Object.create(e.prototype);
q.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()}),Object.defineProperty(c.prototype,"$options",{value:c.$options}));for(var d=b.getChildren(a.Reflect.Service.RPCMethod),
e=0;e<d.length;e++)(function(a){c.prototype[a.name]=function(c,d){try{c&&c instanceof a.resolvedRequestType.clazz?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))}}):setTimeout(d.bind(this,Error("Illegal request type provided to service method "+b.name+"#"+a.name)),0)}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()}),Object.defineProperty(c.prototype[a.name],"$options",{value:c[a.name].$options}))})(d[e]);return c}(d,this)};m.Service=q;var n=function(a,c,d){b.call(this,a,c);this.className="Service.Method";this.options=d||{}};n.prototype=Object.create(b.prototype);n.prototype.buildOpt=e.prototype.buildOpt;m.Service.Method=n;q=function(a,b,c,d,e){n.call(this,a,b,e);this.className=
"Service.RPCMethod";this.requestName=c;this.responseName=d;this.resolvedResponseType=this.resolvedRequestType=null};q.prototype=Object.create(n.prototype);m.Service.RPCMethod=q;return m}(g);g.Builder=function(d,g,h){var m=function(){this.ptr=this.ns=new h.Namespace(null,"");this.resolved=!1;this.result=null;this.files={};this.importRoot=null};m.prototype.reset=function(){this.ptr=this.ns};m.prototype.define=function(b,d){if("string"!==typeof b||!g.TYPEREF.test(b))throw Error("Illegal package: "+b);
var c=b.split("."),k;for(k=0;k<c.length;k++)if(!g.NAME.test(c[k]))throw Error("Illegal package: "+c[k]);for(k=0;k<c.length;k++)this.ptr.hasChild(c[k])||this.ptr.addChild(new h.Namespace(this.ptr,c[k],d)),this.ptr=this.ptr.getChild(c[k]);return this};m.isValidMessage=function(b){if("string"!==typeof b.name||!g.NAME.test(b.name)||"undefined"!==typeof b.values||"undefined"!==typeof b.rpc)return!1;var e;if("undefined"!==typeof b.fields){if(!d.Util.isArray(b.fields))return!1;var c=[],k;for(e=0;e<b.fields.length;e++){if(!m.isValidMessageField(b.fields[e]))return!1;
k=parseInt(b.fields[e].id,10);if(0<=c.indexOf(k))return!1;c.push(k)}}if("undefined"!==typeof b.enums){if(!d.Util.isArray(b.enums))return!1;for(e=0;e<b.enums.length;e++)if(!m.isValidEnum(b.enums[e]))return!1}if("undefined"!==typeof b.messages){if(!d.Util.isArray(b.messages))return!1;for(e=0;e<b.messages.length;e++)if(!m.isValidMessage(b.messages[e])&&!m.isValidExtend(b.messages[e]))return!1}return"undefined"===typeof b.extensions||d.Util.isArray(b.extensions)&&2===b.extensions.length&&"number"===typeof b.extensions[0]&&
"number"===typeof b.extensions[1]?!0:!1};m.isValidMessageField=function(b){if("string"!==typeof b.rule||"string"!==typeof b.name||"string"!==typeof b.type||"undefined"===typeof b.id||!(g.RULE.test(b.rule)&&g.NAME.test(b.name)&&g.TYPEREF.test(b.type)&&g.ID.test(""+b.id)))return!1;if("undefined"!==typeof b.options){if("object"!==typeof b.options)return!1;for(var d=Object.keys(b.options),c=0,k;c<d.length;c++)if("string"!==typeof(k=d[c])||"string"!==typeof b.options[k]&&"number"!==typeof b.options[k]&&
"boolean"!==typeof b.options[k])return!1}return!0};m.isValidEnum=function(b){if("string"!==typeof b.name||!g.NAME.test(b.name)||"undefined"===typeof b.values||!d.Util.isArray(b.values)||0==b.values.length)return!1;for(var e=0;e<b.values.length;e++)if("object"!=typeof b.values[e]||"string"!==typeof b.values[e].name||"undefined"===typeof b.values[e].id||!g.NAME.test(b.values[e].name)||!g.NEGID.test(""+b.values[e].id))return!1;return!0};m.prototype.create=function(b){if(!b)return this;d.Util.isArray(b)||
(b=[b]);if(0==b.length)return this;var e=[],c,k,g,l,n;for(e.push(b);0<e.length;){b=e.pop();if(d.Util.isArray(b))for(;0<b.length;)if(c=b.shift(),m.isValidMessage(c)){k=new h.Message(this.ptr,c.name,c.options,c.isGroup);if(c.fields&&0<c.fields.length)for(l=0;l<c.fields.length;l++){if(k.hasChild(c.fields[l].id))throw Error("Duplicate field id in message "+k.name+": "+c.fields[l].id);if(c.fields[l].options)for(g=Object.keys(c.fields[l].options),n=0;n<g.length;n++){if("string"!==typeof g[n])throw Error("Illegal field option name in message "+
k.name+"#"+c.fields[l].name+": "+g[n]);if("string"!==typeof c.fields[l].options[g[n]]&&"number"!==typeof c.fields[l].options[g[n]]&&"boolean"!==typeof c.fields[l].options[g[n]])throw Error("Illegal field option value in message "+k.name+"#"+c.fields[l].name+"#"+g[n]+": "+c.fields[l].options[g[n]]);}k.addChild(new h.Message.Field(k,c.fields[l].rule,c.fields[l].type,c.fields[l].name,c.fields[l].id,c.fields[l].options))}g=[];if("undefined"!==typeof c.enums&&0<c.enums.length)for(l=0;l<c.enums.length;l++)g.push(c.enums[l]);
if(c.messages&&0<c.messages.length)for(l=0;l<c.messages.length;l++)g.push(c.messages[l]);c.extensions&&(k.extensions=c.extensions,k.extensions[0]<d.ID_MIN&&(k.extensions[0]=d.ID_MIN),k.extensions[1]>d.ID_MAX&&(k.extensions[1]=d.ID_MAX));this.ptr.addChild(k);0<g.length&&(e.push(b),b=g,this.ptr=k)}else if(m.isValidEnum(c)){k=new h.Enum(this.ptr,c.name,c.options);for(l=0;l<c.values.length;l++)k.addChild(new h.Enum.Value(k,c.values[l].name,c.values[l].id));this.ptr.addChild(k)}else if(m.isValidService(c)){k=
new h.Service(this.ptr,c.name,c.options);for(l in c.rpc)c.rpc.hasOwnProperty(l)&&k.addChild(new h.Service.RPCMethod(k,l,c.rpc[l].request,c.rpc[l].response,c.rpc[l].options));this.ptr.addChild(k)}else if(m.isValidExtend(c))if(k=this.ptr.resolve(c.ref))for(l=0;l<c.fields.length;l++){if(k.hasChild(c.fields[l].id))throw Error("Duplicate extended field id in message "+k.name+": "+c.fields[l].id);if(c.fields[l].id<k.extensions[0]||c.fields[l].id>k.extensions[1])throw Error("Illegal extended field id in message "+
k.name+": "+c.fields[l].id+" ("+k.extensions.join(" to ")+" expected)");k.addChild(new h.Message.Field(k,c.fields[l].rule,c.fields[l].type,c.fields[l].name,c.fields[l].id,c.fields[l].options))}else{if(!/\.?google\.protobuf\./.test(c.ref))throw Error("Extended message "+c.ref+" is not defined");}else throw Error("Not a valid definition: "+JSON.stringify(c));else throw Error("Not a valid namespace: "+JSON.stringify(b));this.ptr=this.ptr.parent}this.resolved=!1;this.result=null;return this};m.prototype["import"]=
function(b,e){if("string"===typeof e){d.Util.IS_NODE&&(e=require("path").resolve(e));if(!0===this.files[e])return this.reset(),this;this.files[e]=!0}if(b.imports&&0<b.imports.length){var c,g="/",m=!1;if("object"===typeof e){if(this.importRoot=e.root,m=!0,c=this.importRoot,e=e.file,0<=c.indexOf("\\")||0<=e.indexOf("\\"))g="\\"}else"string"===typeof e?this.importRoot?c=this.importRoot:0<=e.indexOf("/")?(c=e.replace(/\/[^\/]*$/,""),""===c&&(c="/")):0<=e.indexOf("\\")?(c=e.replace(/\\[^\\]*$/,""),g="\\"):
c=".":c=null;for(var l=0;l<b.imports.length;l++)if("string"===typeof b.imports[l]){if(!c)throw Error("Cannot determine import root: File name is unknown");var h=b.imports[l];if(!/^google\/protobuf\//.test(h)&&(h=c+g+h,!0!==this.files[h])){/\.proto$/i.test(h)&&!d.DotProto&&(h=h.replace(/\.proto$/,".json"));var a=d.Util.fetch(h);if(null===a)throw Error("Failed to import '"+h+"' in '"+e+"': File not found");if(/\.json$/i.test(h))this["import"](JSON.parse(a+""),h);else this["import"]((new d.DotProto.Parser(a+
"")).parse(),h)}}else if(e)if(/\.(\w+)$/.test(e))this["import"](b.imports[l],e.replace(/^(.+)\.(\w+)$/,function(a,b,c){return b+"_import"+l+"."+c}));else this["import"](b.imports[l],e+"_import"+l);else this["import"](b.imports[l]);m&&(this.importRoot=null)}b.messages&&(b["package"]&&this.define(b["package"],b.options),this.create(b.messages),this.reset());b.enums&&(b["package"]&&this.define(b["package"],b.options),this.create(b.enums),this.reset());b.services&&(b["package"]&&this.define(b["package"],
b.options),this.create(b.services),this.reset());b["extends"]&&(b["package"]&&this.define(b["package"],b.options),this.create(b["extends"]),this.reset());return this};m.isValidService=function(b){return!("string"!==typeof b.name||!g.NAME.test(b.name)||"object"!==typeof b.rpc)};m.isValidExtend=function(b){if("string"!==typeof b.ref||!g.TYPEREF.test(b.ref))return!1;var e;if("undefined"!==typeof b.fields){if(!d.Util.isArray(b.fields))return!1;var c=[],k;for(e=0;e<b.fields.length;e++){if(!m.isValidMessageField(b.fields[e]))return!1;
k=parseInt(b.id,10);if(0<=c.indexOf(k))return!1;c.push(k)}}return!0};m.prototype.resolveAll=function(){var b;if(null!=this.ptr&&"object"!==typeof this.ptr.type){if(this.ptr instanceof h.Namespace){b=this.ptr.getChildren();for(var e=0;e<b.length;e++)this.ptr=b[e],this.resolveAll()}else if(this.ptr instanceof h.Message.Field)if(g.TYPE.test(this.ptr.type))this.ptr.type=d.TYPES[this.ptr.type];else{if(!g.TYPEREF.test(this.ptr.type))throw Error("Illegal type reference in "+this.ptr.toString(!0)+": "+this.ptr.type);
b=this.ptr.parent.resolve(this.ptr.type,!0);if(!b)throw Error("Unresolvable type reference in "+this.ptr.toString(!0)+": "+this.ptr.type);this.ptr.resolvedType=b;if(b instanceof h.Enum)this.ptr.type=d.TYPES["enum"];else if(b instanceof h.Message)this.ptr.type=b.isGroup?d.TYPES.group:d.TYPES.message;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){b=this.ptr.parent.resolve(this.ptr.requestName);if(!(b&&b instanceof d.Reflect.Message))throw Error("Illegal type reference in "+this.ptr.toString(!0)+": "+this.ptr.requestName);this.ptr.resolvedRequestType=b;b=this.ptr.parent.resolve(this.ptr.responseName);if(!(b&&b instanceof d.Reflect.Message))throw Error("Illegal type reference in "+this.ptr.toString(!0)+": "+this.ptr.responseName);this.ptr.resolvedResponseType=b}else throw Error("Illegal service type in "+this.ptr.toString(!0));
else throw Error("Illegal object in namespace: "+typeof this.ptr+":"+this.ptr);this.reset()}};m.prototype.build=function(b){this.reset();this.resolved||(this.resolveAll(),this.resolved=!0,this.result=null);null==this.result&&(this.result=this.ns.build());if(b){b=b.split(".");for(var d=this.result,c=0;c<b.length;c++)if(d[b[c]])d=d[b[c]];else{d=null;break}return d}return this.result};m.prototype.lookup=function(b){return b?this.ns.resolve(b):this.ns};m.prototype.toString=function(){return"Builder"};
m.Message=function(){};m.Service=function(){};return m}(g,g.Lang,g.Reflect);g.newBuilder=function(d,h){var p=new g.Builder;"undefined"!==typeof d&&null!==d&&p.define(d,h);return p};g.loadJson=function(d,h,p){if("string"===typeof h||h&&"string"===typeof h.file&&"string"===typeof h.root)p=h,h=null;h&&"object"===typeof h||(h=g.newBuilder());"string"===typeof d&&(d=JSON.parse(d));h["import"](d,p);h.resolveAll();h.build();return h};g.loadJsonFile=function(d,h,p){h&&"object"===typeof h?(p=h,h=null):h&&
"function"===typeof h||(h=null);if(h)return g.Util.fetch("string"===typeof d?d:d.root+"/"+d.file,function(b){if(null===b)h(Error("Failed to fetch file"));else try{h(null,g.loadJson(JSON.parse(b),p,d))}catch(e){h(e)}});var m=g.Util.fetch("object"===typeof d?d.root+"/"+d.file:d);return null===m?null:g.loadJson(JSON.parse(m),p,d)};return g}"undefined"!==typeof module&&module.exports?module.exports=t(require("bytebuffer")):"function"===typeof define&&define.amd?define(["ByteBuffer"],t):(s.dcodeIO=s.dcodeIO||
{}).ProtoBuf=t(s.dcodeIO.ByteBuffer)})(this);