rawproto
Version:
Guess structure of protobuf binary from raw data
3 lines (2 loc) • 10.9 kB
JavaScript
var t=require("flat");function e(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 i(t,i){for(var r=0;r<i.length;r++){var n=i[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,e(n.key),n)}}function r(t,e,r){return e&&i(t.prototype,e),r&&i(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function n(){return n=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},n.apply(this,arguments)}function s(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,f(t,e)}function f(t,e){return f=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},f(t,e)}function o(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 u(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 o(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)?o(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 a={VARINT:0,I64:1,LEN:2,SGROUP:3,EGROUP:4,I32:5},h=new TextDecoder,c={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 r(function(t,e,i,r){this.buffer=t,this.type=e,this.dataView=new DataView(this.buffer),this.path=i,this.renderType=r||c[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}}])}(),d=/*#__PURE__*/function(t){function e(e,i,r){return t.call(this,e,a.I64,i,r)||this}return s(e,t),r(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),y=/*#__PURE__*/function(t){function e(e,i,r){return t.call(this,e,a.I32,i,r)||this}return s(e,t),r(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),b=/*#__PURE__*/function(){return r(function(t,e,i,r){this.type=a.VARINT,this.buffer=t,this.value=this.uint=this.int=r,this.path=e,this.renderType=i||c[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}}])}(),l=/*#__PURE__*/function(){function t(t,e,i){void 0===e&&(e="0"),this.type=a.LEN,this.path=e,this.renderType=i||c[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!==a.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 v.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"),g(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"),_(this,t,e,i,r,n)},r(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===a.VARINT){var o=this.offset,u=parseInt(this.readVarInt()),h=new b(this.buffer.slice(o,this.offset-1),[this.path,f].join("."),"int32",u);this._sub[f].push(h),e=this.offset}if(s===a.LEN){var c=this.readVarInt(),p=new t(this.buffer.slice(this.offset,this.offset+c),[this.path,f].join("."));this.offset+=c,this._sub[f].push(p),e=this.offset}if(s===a.SGROUP){var l=new t(this.readBufferUntilGroupEnd(f),[this.path,f].join("."));this._sub[f].push(l),e=this.offset}if(s===a.I64){var v=new d(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===a.I32){var g=new y(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=h.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 v(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 a,h=s.replace(new RegExp("^"+e+"."),"").split("."),c=[t],p=u(h);!(a=p()).done;){for(var d,y=a.value,b=[],l=u(c);!(d=l()).done;){var v=d.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=u([].slice.call(arguments,2));!(i=s()).done;)n();return r}function g(e,i,r,s,f){void 0===r&&(r="f");var o={};if("object"==typeof i){s||(s={}),f||(f={});for(var h=0,c=Object.keys(i);h<c.length;h++){var p=c[h],d=i[p].split(":"),y=d[0],b=d[1],l=void 0===b?"raw":b;"0"!==y[0]&&(y="0."+y),s[y]=p,f[y]=l}}for(var v=0,w=Object.values(e.sub||{});v<w.length;v++)for(var _,I=u(w[v]);!(_=I()).done;){var V=_.value;try{var j,m=s[V.path]||k(r,V.path);(j=o)[m]||(j[m]=[]);var x=f[V.path]||V.renderType;V.type!==a.LEN||["string","bytes"].includes(x)?o[m].push(V[x]):V.couldHaveSub?o=n({},o,g(V,void 0,r,s,f)):o[m].push(V.likelyString?V.string:V.bytes)}catch(t){}}return t.unflatten(o)}var k=function(t,e){return e.split(".").map(function(e,i,r){return""+t+e}).join(".")},w=function(t,e,i){return void 0===i&&(i=" "),t.replace(/^/gm,i.repeat(e))};function _(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 h=0,c=Object.keys(e);h<c.length;h++){var p=c[h],d=e[p].split(":"),y=d[0],b=d[1],l=void 0===b?"raw":b;"0"!==y[0]&&(y="0."+y),r[y]=p,n[y]=l}}u.push("message "+s+" {");var v=Object.keys(t.sub||{});for(var g in v){var k,I,V=parseInt(g)+1,j=null==t?void 0:t.sub[v[g]];if(null!=(k=j[0])&&k.path){var m="",x="",O=n[null==(I=j[0])?void 0:I.path]||j[0].renderType,S=r[j[0].path]?r[j[0].path].split(".").pop():""+i+V;j.length>1&&(m="repeated "),"packedIntVar"===O&&(O="int32",m="repeated ",x=" [packed=true]"),"packedInt32"===O&&(O="fixed32",m="repeated ",x=" [packed=true]"),"packedInt64"===O&&(O="fixed64",m="repeated ",x=" [packed=true]"),"object"!=typeof j[0]||j[0].type!==a.LEN||["string","bytes"].includes(O)?u.push(w(""+m+O+" "+S+" = "+V+x+";",2)):j[0].couldHaveSub?(u.push(w("Message"+V+" "+S+" = "+V+";",2)),u.push(_(j[0],void 0,i,r,n,"Message"+V,f+1))):u.push(w("bytes "+S+" = "+V+";",2))}}return u.push("}"),w(u.join("\n"),2*f)}exports.ReaderFixed=p,exports.ReaderFixed32=y,exports.ReaderFixed64=d,exports.ReaderMessage=l,exports.ReaderVarInt=b,exports.default=l,exports.query=v,exports.toJS=g,exports.toProto=_,exports.wireMap=c,exports.wireTypes=a;
//# sourceMappingURL=rawproto.cjs.map