UNPKG

rawproto

Version:

Guess structure of protobuf binary from raw data

3 lines (2 loc) 7.84 kB
import{unflatten as t}from"flat";function e(){return e=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var s=arguments[e];for(var i in s)Object.prototype.hasOwnProperty.call(s,i)&&(t[i]=s[i])}return t},e.apply(this,arguments)}const s={VARINT:0,I64:1,LEN:2,SGROUP:3,EGROUP:4,I32:5},i=new TextDecoder,r={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"]};class n{constructor(t,e,s,i){this.buffer=t,this.type=e,this.dataView=new DataView(this.buffer),this.path=s,this.renderType=i||r[this.type][0]}get raw(){return this}get string(){return this.int+""}get bytes(){return this._bytes||(this._bytes=new Uint8Array(this.buffer)),this._bytes}}class h extends n{constructor(t,e,i){super(t,s.I64,e,i)}get uint(){const t=this.dataView.getBigUint64(0,!0);try{return Number(t)}catch(e){return t}}get int(){const t=this.dataView.getBigInt64(0,!0);try{return Number(t)}catch(e){return t}}get float(){const t=this.dataView.getFloat64(0,!0);try{return Number(t)}catch(e){return t}}get double(){return this.float}get fixed64(){return this.uint}get sfixed64(){return this.int}}class f extends n{constructor(t,e,i){super(t,s.I32,e,i)}get uint(){return this.dataView.getUint32(0,!0)}get int(){return this.dataView.getInt32(0,!0)}get float(){return this.dataView.getFloat32(0,!0)}get fixed32(){return this.uint}get sfixed32(){return this.int}}class a{constructor(t,e,i,n){this.type=s.VARINT,this.buffer=t,this.value=this.uint=this.int=n,this.path=e,this.renderType=i||r[this.type][0]}get raw(){return this}get bytes(){return this._bytes||(this._bytes=new Uint8Array(this.buffer)),this._bytes}get string(){return this.uint.toString()}get bool(){return!!this.uint}get int32(){return this.int}get int64(){return this.int}get sint32(){return this.int}get sint64(){return this.int}get uint32(){return this.uint}get uint64(){return this.uint}}class u{constructor(t,e="0",i){this.type=s.LEN,this.path=e,this.renderType=i||r[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}readBufferUntilGroupEnd(t){const e=this.offset;let i=parseInt(this.readVarInt()),r=7&i;for(;r!==s.EGROUP;)i=parseInt(this.readVarInt()),r=7&i;return this.buffer.slice(e,this.offset)}readVarInt(){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.");let t,e=0,s=0;do{if(this.offset>=this.buffer.byteLength)throw new Error(`Buffer overflow while reading varint: ${this.offset}/${this.buffer.byteLength}`);t=this.bytes[this.offset++],e|=(127&t)<<s,s+=7}while(t>=128);return e}get couldHaveSub(){return void 0===this._couldHaveSub&&(this._couldHaveSub=Object.keys(this.sub).length>0),this._couldHaveSub}get likelyString(){return this._likelyString||(this._likelyString=void 0===this.bytes.find(t=>t<32)),this._likelyString}get fields(){return this._fields}get sub(){if(this._sub)return this._sub;this.offset=0,this._fields={},this._sub={};let t=this.offset;try{for(;this.offset<this.buffer.byteLength;){var e,i;const r=parseInt(this.readVarInt()),n=7&r,o=r>>3;if((e=this._fields)[o]||(e[o]=0),this._fields[o]++,(i=this._sub)[o]||(i[o]=[]),n===s.VARINT){const e=this.offset,s=parseInt(this.readVarInt()),i=new a(this.buffer.slice(e,this.offset-1),[this.path,o].join("."),"int32",s);this._sub[o].push(i),t=this.offset}if(n===s.LEN){const e=this.readVarInt(),s=new u(this.buffer.slice(this.offset,this.offset+e),[this.path,o].join("."));this.offset+=e,this._sub[o].push(s),t=this.offset}if(n===s.SGROUP){const e=new u(this.readBufferUntilGroupEnd(o),[this.path,o].join("."));this._sub[o].push(e),t=this.offset}if(n===s.I64){const e=new h(this.buffer.slice(this.offset,this.offset+8),[this.path,o].join("."));this.offset+=8,this._sub[o].push(e),t=this.offset}if(n===s.I32){const e=new f(this.buffer.slice(this.offset,this.offset+4),[this.path,o].join("."));this.offset+=4,this._sub[o].push(e),t=this.offset}}return this._sub}catch(e){return this.remainder=this.buffer.slice(t),{}}}get raw(){return this}get string(){return this._string||(this._string=i.decode(this.bytes)),this._string}get packedIntVar(){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}get packedInt32(){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}get packedInt64(){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}query(...t){return o(this,this.path,...t)}toJS(t={},e="f",s,i){return c(this,t,e,s,i)}toProto(t={},e="f",s,i,r="MessageRoot"){return b(this,t,e,s,i,r)}}function o(t,e="0",...s){const i=[];for(const r of s){let[s,n="raw"]=r.split(":");s.substr(0,e.length)!==e&&(s=`${e}.${s}`);const h=s.replace(new RegExp(`^${e}.`),"").split(".");let f=[t];for(const t of h){const e=[];for(const s of f)s.sub[t]&&e.push(...s.sub[t]);f=e}i.push(...f.filter(t=>t.path===s).map(t=>t[n]))}return i}function c(i,r,n="f",h,f){let a={};if("object"==typeof r){h||(h={}),f||(f={});for(const t of Object.keys(r)){let[e,s="raw"]=r[t].split(":");"0"!==e[0]&&(e=`0.${e}`),h[e]=t,f[e]=s}}for(const t of Object.values(i.sub||{}))for(const i of t)try{var u;const t=h[i.path]||p(n,i.path);(u=a)[t]||(u[t]=[]);const r=f[i.path]||i.renderType;i.type!==s.LEN||["string","bytes"].includes(r)?a[t].push(i[r]):i.couldHaveSub?a=e({},a,c(i,void 0,n,h,f)):a[t].push(i.likelyString?i.string:i.bytes)}catch(t){}return t(a)}const p=(t,e)=>e.split(".").map((e,s,i)=>`${t}${e}`).join("."),d=(t,e,s=" ")=>t.replace(/^/gm,s.repeat(e));function b(t,e,i="f",r,n,h="MessageRoot",f=0,a=!1){const u=[];if("object"==typeof e){r||(r={}),n||(n={});for(const t of Object.keys(e)){let[s,i="raw"]=e[t].split(":");"0"!==s[0]&&(s=`0.${s}`),r[s]=t,n[s]=i}}u.push(`message ${h} {`);const o=Object.keys(t.sub||{});for(const e in o){var c,p;const h=parseInt(e)+1,a=null==t?void 0:t.sub[o[e]];if(null==(c=a[0])||!c.path)continue;let l="",g="",y=n[null==(p=a[0])?void 0:p.path]||a[0].renderType;const w=r[a[0].path]?r[a[0].path].split(".").pop():`${i}${h}`;a.length>1&&(l="repeated "),"packedIntVar"===y&&(y="int32",l="repeated ",g=" [packed=true]"),"packedInt32"===y&&(y="fixed32",l="repeated ",g=" [packed=true]"),"packedInt64"===y&&(y="fixed64",l="repeated ",g=" [packed=true]"),"object"!=typeof a[0]||a[0].type!==s.LEN||["string","bytes"].includes(y)?u.push(d(`${l}${y} ${w} = ${h}${g};`,2)):a[0].couldHaveSub?(u.push(d(`Message${h} ${w} = ${h};`,2)),u.push(b(a[0],void 0,i,r,n,`Message${h}`,f+1,!0))):u.push(d(`bytes ${w} = ${h};`,2))}return u.push("}"),d(u.join("\n"),2*f)}export{n as ReaderFixed,f as ReaderFixed32,h as ReaderFixed64,u as ReaderMessage,a as ReaderVarInt,u as default,o as query,c as toJS,b as toProto,r as wireMap,s as wireTypes}; //# sourceMappingURL=rawproto.modern.js.map