UNPKG

rawproto

Version:

Guess structure of protobuf binary from raw data

3 lines (2 loc) 11 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("flat")):"function"==typeof define&&define.amd?define(["exports","flat"],e):e((t||self).rawproto={},t.flat)}(this,function(t,e){function i(t){var e=function(t,e){if("object"!=typeof t||!t)return t;var i=t[Symbol.toPrimitive];if(void 0!==i){var r=i.call(t,"string");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==typeof e?e:e+""}function r(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,i(n.key),n)}}function n(t,e,i){return e&&r(t.prototype,e),i&&r(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}function s(){return s=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var r in i)Object.prototype.hasOwnProperty.call(i,r)&&(t[r]=i[r])}return t},s.apply(this,arguments)}function f(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,o(t,e)}function o(t,e){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},o(t,e)}function u(t,e){(null==e||e>t.length)&&(e=t.length);for(var i=0,r=new Array(e);i<e;i++)r[i]=t[i];return r}function a(t,e){var i="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(i)return(i=i.call(t)).next.bind(i);if(Array.isArray(t)||(i=function(t,e){if(t){if("string"==typeof t)return u(t,e);var i=Object.prototype.toString.call(t).slice(8,-1);return"Object"===i&&t.constructor&&(i=t.constructor.name),"Map"===i||"Set"===i?Array.from(t):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?u(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){i&&(t=i);var r=0;return function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var h={VARINT:0,I64:1,LEN:2,SGROUP:3,EGROUP:4,I32:5},c=new TextDecoder,d={0:["uint32","int32","int64","uint64","sint32","sint64","bool","raw","bytes"],1:["uint32","int32","bytes","fixed64","sfixed64","double"],2:["raw","bytes","string","sub","packedIntVar","packedInt32","packedInt64"],5:["uint32","int32","bytes","fixed32","sfixed32","float","raw"]},p=/*#__PURE__*/function(){return n(function(t,e,i,r){this.buffer=t,this.type=e,this.dataView=new DataView(this.buffer),this.path=i,this.renderType=r||d[this.type][0]},[{key:"raw",get:function(){return this}},{key:"string",get:function(){return this.int+""}},{key:"bytes",get:function(){return this._bytes||(this._bytes=new Uint8Array(this.buffer)),this._bytes}}])}(),y=/*#__PURE__*/function(t){function e(e,i,r){return t.call(this,e,h.I64,i,r)||this}return f(e,t),n(e,[{key:"uint",get:function(){var t=this.dataView.getBigUint64(0,!0);try{return Number(t)}catch(e){return t}}},{key:"int",get:function(){var t=this.dataView.getBigInt64(0,!0);try{return Number(t)}catch(e){return t}}},{key:"float",get:function(){var t=this.dataView.getFloat64(0,!0);try{return Number(t)}catch(e){return t}}},{key:"double",get:function(){return this.float}},{key:"fixed64",get:function(){return this.uint}},{key:"sfixed64",get:function(){return this.int}}])}(p),b=/*#__PURE__*/function(t){function e(e,i,r){return t.call(this,e,h.I32,i,r)||this}return f(e,t),n(e,[{key:"uint",get:function(){return this.dataView.getUint32(0,!0)}},{key:"int",get:function(){return this.dataView.getInt32(0,!0)}},{key:"float",get:function(){return this.dataView.getFloat32(0,!0)}},{key:"fixed32",get:function(){return this.uint}},{key:"sfixed32",get:function(){return this.int}}])}(p),l=/*#__PURE__*/function(){return n(function(t,e,i,r){this.type=h.VARINT,this.buffer=t,this.value=this.uint=this.int=r,this.path=e,this.renderType=i||d[this.type][0]},[{key:"raw",get:function(){return this}},{key:"bytes",get:function(){return this._bytes||(this._bytes=new Uint8Array(this.buffer)),this._bytes}},{key:"string",get:function(){return this.uint.toString()}},{key:"bool",get:function(){return!!this.uint}},{key:"int32",get:function(){return this.int}},{key:"int64",get:function(){return this.int}},{key:"sint32",get:function(){return this.int}},{key:"sint64",get:function(){return this.int}},{key:"uint32",get:function(){return this.uint}},{key:"uint64",get:function(){return this.uint}}])}(),v=/*#__PURE__*/function(){function t(t,e,i){void 0===e&&(e="0"),this.type=h.LEN,this.path=e,this.renderType=i||d[this.type][0],"undefined"!=typeof Buffer&&t instanceof Buffer?(this.bytes=new Uint8Array(t),this.buffer=this.bytes.buffer):t instanceof ArrayBuffer?(this.buffer=t,this.bytes=new Uint8Array(this.buffer)):t instanceof Uint8Array?(this.buffer=t.buffer,this.bytes=new Uint8Array(this.buffer)):(this.bytes=new Uint8Array(t),this.buffer=this.bytes.buffer),this.offset=0}var e=t.prototype;return e.readBufferUntilGroupEnd=function(t){for(var e=this.offset,i=parseInt(this.readVarInt()),r=7&i;r!==h.EGROUP;)r=7&(i=parseInt(this.readVarInt()));return this.buffer.slice(e,this.offset)},e.readVarInt=function(){if(void 0===this.offset)throw new Error("Offset must be defined to use readVarInt. If you really want to do this, try setting it to 0.");var t,e=0,i=0;do{if(this.offset>=this.buffer.byteLength)throw new Error("Buffer overflow while reading varint: "+this.offset+"/"+this.buffer.byteLength);e|=(127&(t=this.bytes[this.offset++]))<<i,i+=7}while(t>=128);return e},e.query=function(){return g.apply(void 0,[this,this.path].concat([].slice.call(arguments)))},e.toJS=function(t,e,i,r){return void 0===t&&(t={}),void 0===e&&(e="f"),k(this,t,e,i,r)},e.toProto=function(t,e,i,r,n){return void 0===t&&(t={}),void 0===e&&(e="f"),void 0===n&&(n="MessageRoot"),I(this,t,e,i,r,n)},n(t,[{key:"couldHaveSub",get:function(){return void 0===this._couldHaveSub&&(this._couldHaveSub=Object.keys(this.sub).length>0),this._couldHaveSub}},{key:"likelyString",get:function(){return this._likelyString||(this._likelyString=void 0===this.bytes.find(function(t){return t<32})),this._likelyString}},{key:"fields",get:function(){return this._fields}},{key:"sub",get:function(){if(this._sub)return this._sub;this.offset=0,this._fields={},this._sub={};var e=this.offset;try{for(;this.offset<this.buffer.byteLength;){var i,r,n=parseInt(this.readVarInt()),s=7&n,f=n>>3;if((i=this._fields)[f]||(i[f]=0),this._fields[f]++,(r=this._sub)[f]||(r[f]=[]),s===h.VARINT){var o=this.offset,u=parseInt(this.readVarInt()),a=new l(this.buffer.slice(o,this.offset-1),[this.path,f].join("."),"int32",u);this._sub[f].push(a),e=this.offset}if(s===h.LEN){var c=this.readVarInt(),d=new t(this.buffer.slice(this.offset,this.offset+c),[this.path,f].join("."));this.offset+=c,this._sub[f].push(d),e=this.offset}if(s===h.SGROUP){var p=new t(this.readBufferUntilGroupEnd(f),[this.path,f].join("."));this._sub[f].push(p),e=this.offset}if(s===h.I64){var v=new y(this.buffer.slice(this.offset,this.offset+8),[this.path,f].join("."));this.offset+=8,this._sub[f].push(v),e=this.offset}if(s===h.I32){var g=new b(this.buffer.slice(this.offset,this.offset+4),[this.path,f].join("."));this.offset+=4,this._sub[f].push(g),e=this.offset}}return this._sub}catch(t){return this.remainder=this.buffer.slice(e),{}}}},{key:"raw",get:function(){return this}},{key:"string",get:function(){return this._string||(this._string=c.decode(this.bytes)),this._string}},{key:"packedIntVar",get:function(){if(void 0!==this._packedintvar)return this._packedintvar;for(this._packedintvar=[],this.offset=0;this.offset<this.buffer.byteLength;)this._packedintvar.push(this.readVarInt());return this._packedintvar}},{key:"packedInt32",get:function(){if(void 0!==this._packedint32)return this._packedint32;for(this.dataView||(this.dataView=new DataView(this.buffer)),this._packedint32=[],this.offset=0;this.offset<this.buffer.byteLength;)this._packedint32.push(this.dataView.getInt32(this.offset,!0)),this.offset+=4;return this._packedint32}},{key:"packedInt64",get:function(){if(void 0!==this._packedint64)return this._packedint64;for(this.dataView||(this.dataView=new DataView(this.buffer)),this._packedint64=[],this.offset=0;this.offset<this.buffer.byteLength;){try{this._packedint64.push(parseInt(this.dataView.getBigInt64(this.offset,!0)))}catch(t){this._packedint64.push(this.dataView.getBigInt64(this.offset,!0))}this.offset+=8}return this._packedint64}}])}();function g(t,e){void 0===e&&(e="0");for(var i,r=[],n=function(){var n=i.value.split(":"),s=n[0],f=n[1],o=void 0===f?"raw":f;s.substr(0,e.length)!==e&&(s=e+"."+s);for(var u,h=s.replace(new RegExp("^"+e+"."),"").split("."),c=[t],d=a(h);!(u=d()).done;){for(var p,y=u.value,b=[],l=a(c);!(p=l()).done;){var v=p.value;v.sub[y]&&b.push.apply(b,v.sub[y])}c=b}r.push.apply(r,c.filter(function(t){return t.path===s}).map(function(t){return t[o]}))},s=a([].slice.call(arguments,2));!(i=s()).done;)n();return r}function k(t,i,r,n,f){void 0===r&&(r="f");var o={};if("object"==typeof i){n||(n={}),f||(f={});for(var u=0,c=Object.keys(i);u<c.length;u++){var d=c[u],p=i[d].split(":"),y=p[0],b=p[1],l=void 0===b?"raw":b;"0"!==y[0]&&(y="0."+y),n[y]=d,f[y]=l}}for(var v=0,g=Object.values(t.sub||{});v<g.length;v++)for(var _,I=a(g[v]);!(_=I()).done;){var V=_.value;try{var m,j=n[V.path]||w(r,V.path);(m=o)[j]||(m[j]=[]);var O=f[V.path]||V.renderType;V.type!==h.LEN||["string","bytes"].includes(O)?o[j].push(V[O]):V.couldHaveSub?o=s({},o,k(V,void 0,r,n,f)):o[j].push(V.likelyString?V.string:V.bytes)}catch(t){}}return e.unflatten(o)}var w=function(t,e){return e.split(".").map(function(e,i,r){return""+t+e}).join(".")},_=function(t,e,i){return void 0===i&&(i=" "),t.replace(/^/gm,i.repeat(e))};function I(t,e,i,r,n,s,f,o){void 0===i&&(i="f"),void 0===s&&(s="MessageRoot"),void 0===f&&(f=0);var u=[];if("object"==typeof e){r||(r={}),n||(n={});for(var a=0,c=Object.keys(e);a<c.length;a++){var d=c[a],p=e[d].split(":"),y=p[0],b=p[1],l=void 0===b?"raw":b;"0"!==y[0]&&(y="0."+y),r[y]=d,n[y]=l}}u.push("message "+s+" {");var v=Object.keys(t.sub||{});for(var g in v){var k,w,V=parseInt(g)+1,m=null==t?void 0:t.sub[v[g]];if(null!=(k=m[0])&&k.path){var j="",O="",S=n[null==(w=m[0])?void 0:w.path]||m[0].renderType,x=r[m[0].path]?r[m[0].path].split(".").pop():""+i+V;m.length>1&&(j="repeated "),"packedIntVar"===S&&(S="int32",j="repeated ",O=" [packed=true]"),"packedInt32"===S&&(S="fixed32",j="repeated ",O=" [packed=true]"),"packedInt64"===S&&(S="fixed64",j="repeated ",O=" [packed=true]"),"object"!=typeof m[0]||m[0].type!==h.LEN||["string","bytes"].includes(S)?u.push(_(""+j+S+" "+x+" = "+V+O+";",2)):m[0].couldHaveSub?(u.push(_("Message"+V+" "+x+" = "+V+";",2)),u.push(I(m[0],void 0,i,r,n,"Message"+V,f+1))):u.push(_("bytes "+x+" = "+V+";",2))}}return u.push("}"),_(u.join("\n"),2*f)}t.ReaderFixed=p,t.ReaderFixed32=b,t.ReaderFixed64=y,t.ReaderMessage=v,t.ReaderVarInt=l,t.default=v,t.query=g,t.toJS=k,t.toProto=I,t.wireMap=d,t.wireTypes=h}); //# sourceMappingURL=rawproto.umd.js.map