quick-struct
Version:
C like style to write struct layout, and it can convert variable length array or string automatically. It can be used in Node.js and Browser
2 lines (1 loc) • 5.29 kB
JavaScript
var x=Object.defineProperty,Y=Object.defineProperties,P=Object.getOwnPropertyDescriptor,V=Object.getOwnPropertyDescriptors,U=Object.getOwnPropertyNames,O=Object.getOwnPropertySymbols;var B=Object.prototype.hasOwnProperty,j=Object.prototype.propertyIsEnumerable;var A=(e,t,r)=>t in e?x(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,I=(e,t)=>{for(var r in t||(t={}))B.call(t,r)&&A(e,r,t[r]);if(O)for(var r of O(t))j.call(t,r)&&A(e,r,t[r]);return e},w=(e,t)=>Y(e,V(t));var J=(e,t)=>{for(var r in t)x(e,r,{get:t[r],enumerable:!0})},z=(e,t,r,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of U(t))!B.call(e,n)&&n!==r&&x(e,n,{get:()=>t[n],enumerable:!(s=P(t,n))||s.enumerable});return e};var Z=e=>z(x({},"__esModule",{value:!0}),e);var X={};J(X,{QStruct:()=>T,qs:()=>q});module.exports=Z(X);var D={u8:10,i8:11,u16:12,i16:13,u32:14,i32:15,u64:16,i64:17,float:18,double:19,char:20,uchar:21,string:22,struct:23};var E=[0,0,0,0,0,0,0,0,0,0,"Uint8","Int8","Uint16","Int16","Uint32","Int32","BigUint64","BigInt64","Float32","Float64","Uint8","Uint8","Uint8"],S=[0,0,0,0,0,0,0,0,0,0,1,1,2,2,4,4,8,8,4,8,1,1,1];var $=(e,t=0)=>{let r=/\bstruct(\w*)\{(\S*?)\}/g,s=e.trim().replace(/\s/g,""),n=[],c;for(;(c=r.exec(s))!==null;){let[u,_]=c.slice(1);u=u===""?"default":u,n.push([u,_])}return n},N=e=>{let t=/\<(\w+)(?::(?:\s*)(\S*))?\>/g,r={},s;for(;(s=t.exec(e))!==null;){let[n,c]=s.slice(1);r[n]=G(n,c)}return r},G=(e,t)=>{let r=e.toLocaleLowerCase();return r==="ver"?Number(t):r==="autoflush"?!0:r==="endian"?t:void 0},Q=(e,t,r)=>t!==void 0?t.startsWith("$")?e<20?6:e>=20&&e<=22?7:0:e<20?2:e>=20&&e<=22?3:0:0,M=(e,t)=>{if(e===void 0)return 0;if(e.startsWith("$")){let r=e.substring(1);return t.findIndex(n=>n===r)}else return Number(e)},W=e=>{let t=/(u8|i8|u16|i16|u32|i32|u64|i64|f32|f64|char|uchar|string)(\w+)(?:\[(\$\w*|\d*)\])?;??/g,r=[],s=[],n;for(;(n=t.exec(e))!==null;){let[c,u,_]=n.slice(1),a=D[c],b=Q(a,_),i=M(_,r);r.push(u),s.push([a,b,i])}return[r,s]},k=e=>$(e).map(r=>{let s=r[0],n=r[1],[c,u]=W(n);return[s,c,u]});var nt=BigInt(Number.MAX_SAFE_INTEGER);function L(e,t){let s=(arguments.length===1?this._structs[0]:this.findStruct(t))[2],n=0,c=0,u=0,_,a=this._decodeLittleEndian,b=s.length;for(let i=0;i<b;i+=1){let l=s[i],o=l[0],f=l[1],g=l[2],d=(f&2)!==0,p=(f&4)!==0?this._decodeFieldDataset[g]:d?g:1;u=S[o],c=n+u*p;let h=new DataView(e),y=`get${E[o]}`;switch(f){case 0:_=h[y](n,a);break;case 2:case 6:_=[...Array(p).keys()].map((H,R)=>h[y](R*u+n,a));break;case 3:case 7:_=new TextDecoder().decode(e.slice(n,c));break}n=c,this._decodeFieldDataset.push(_)}return this}function C(e,t){let r=this._decodeLittleEndian,s=arguments.length===1?this._structs[0]:this.findStruct(t),[n,c,u]=s,_=Object.keys(e),a={},b=0;for(let o of _){let f=e[o],g=c.findIndex(h=>h===o),d=u[g],m=Object.freeze(F(o,f,d,r));a[o]=m.buffer,b+=m.buffer.byteLength;let p;if((p=m.placeholderIndex)!==void 0){let h=c[p];d=u[p];let y=Object.freeze(F(h,m.buffer.byteLength,d));a[h]=y.buffer,b+=y.buffer.byteLength}}let i=new Uint8Array(b),l=0;for(let o of c){let f=a[o];i.set(new Uint8Array(f),l),l+=f.byteLength}return i.buffer}var F=(e,t,r,s=!1)=>{let n=r[0],c=r[1],u=r[2],_=(c&4)!==0,a=S[n],b=`set${E[n]}`,i={};if(_&&(i.placeholderIndex=u),n===16||c===17)if(typeof t=="object"&&Array.isArray(t)){let l=a*t.length,o=new ArrayBuffer(l),f=new DataView(o),g=t.length;for(let d=0;d<g;d+=1)f[b](d*a,BigInt(t[d]),s);return i.buffer=o,i}else{let l=a,o=new ArrayBuffer(l),f=new DataView(o),g=typeof t=="bigint"?t:typeof t=="number"?BigInt(t):void 0;return f[b](0,g,s),i.buffer=o,i}if(typeof t=="object"&&Array.isArray(t)){let l=a*t.length,o=new ArrayBuffer(l),f=new DataView(o),g=t.length;for(let d=0;d<g;d+=1)f[b](d*a,t[d],s);return i.buffer=o,i}if(typeof t=="string")return i.buffer=new TextEncoder().encode(t),i;if(typeof t=="number"){let l=a,o=new ArrayBuffer(l);return new DataView(o)[b](0,t,s),i.buffer=o,i}return i};var T=class{constructor(t){this._rawString="";this._fieldNames=[];this._decodeFieldDataset=[];this._structs=[];this._littleEndian=!0;this._decodeLittleEndian=!0;this._autoFlush=!1;this.decode=L;this.encode=C;this.toJson=()=>{let t=Object.freeze(this._fieldNames.reduce((r,s,n)=>w(I({},r),{[s]:this._decodeFieldDataset[n]}),{}));return this._autoFlush&&this.flush(),t};this.toJSON=()=>this.toJson();this.exportStructs=()=>btoa(JSON.stringify(this._structs));this.importStructs=t=>{this._structs=JSON.parse(atob(t)),this._fieldNames=this.findStruct()[1]};if(t!==void 0){this._rawString=t,this._structs=k(t),this._fieldNames=this.findStruct()[1];let s=N(this._rawString);this._autoFlush=s.autoflush??!1,s.endian&&(this._decodeLittleEndian=s.endian!=="big")}let r=new Uint8Array(new Uint16Array([1]).buffer);this._littleEndian=r[0]===1}findStruct(t="default"){return this._structs.find(r=>r[0]===t)}get isLittleEndian(){return this._littleEndian}get isBigEndian(){return!this._littleEndian}get endianness(){return this._littleEndian?"little":"big"}get description(){return JSON.stringify(this._structs)}setBigEndian(){return this._decodeLittleEndian=!1,this}setLittleEndian(){return this._decodeLittleEndian=!0,this}flush(){return this._decodeFieldDataset=[],this}autoFlush(t){return this._autoFlush=t??!0,this}};function q(e){return new T(e.raw[0])}0&&(module.exports={QStruct,qs});