UNPKG

9.95 kBJavaScriptView Raw
1var t={UInt8:1,UInt16LE:2,UInt16BE:2,UInt32LE:4,UInt32BE:4,Int8:1,Int16LE:2,Int16BE:2,Int32LE:4,Int32BE:4,FloatLE:4,FloatBE:4,DoubleLE:8,DoubleBE:8},e=Array.from(new Array(32),function(t,e){return e+1}),i={};Object.keys(Object.assign({},t,{Array:null,Skip:null,Nest:null,Bit:null,String:null})).forEach(function(t){i[t.toLowerCase()]=t});var r=Object.prototype.hasOwnProperty,s=function(){this.chain=[],this.bitChain=[],this.endian="be",this.initialize()};s.prototype.initialize=function(){this.addPrimitiveType(),this.addBitType()},s.prototype.addPrimitiveType=function(){var e=this;Object.keys(t).forEach(function(t){e[t.toLowerCase()]=function(i,r){return e.setNextParser(t.toLowerCase(),i,r)};var i=t.replace(/BE|LE/,"").toLowerCase();"function"!=typeof e[i]&&(e[i]=function(t,r){return e.setNextParser(i+e.endian,t,r)})})},s.prototype.addBitType=function(){var t=this;e.forEach(function(e){t["bit"+e]=function(i,r){return r=r||{},r=Object.assign({},r,{length:e}),t.setNextParser("bit",i,r)}})},s.prototype.skip=function(t,e){if(e&&e.assert)throw new Error("assert option on skip is not allowed.");return void 0===e&&(e={}),e.length=t,this.setNextParser("skip","",e)},s.prototype.string=function(t,e){if(!e.zeroTerminated&&!e.length&&!e.greedy)throw new Error("Neither length, zeroTerminated, nor greedy is defined for string.");if((e.zeroTerminated||e.length)&&e.greedy)throw new Error("Greedy is mutually exclusive with length and zeroTerminated for string.");if(e.stripNull&&!e.length&&!e.greedy)throw new Error("Length or greedy must be defined if stripNull is defined.");return e.encoding=e.encoding||"ascii",this.setNextParser("string",t,e)},s.prototype.array=function(e,s){if(!s.readUntil&&!s.length&&!s.lengthInBytes)throw new Error("Length option of array is not defined.");if(!s.type)throw new Error("Type option of array is not defined.");if("string"==typeof s.type&&!r.call(t,i[s.type])&&"string"!==s.type)throw new Error("Specified primitive type "+s.type+" is not supported.");return this.setNextParser("array",e,s)},s.prototype.nest=function(t,e){if(1===arguments.length&&"object"==typeof t&&(e=t,t=null),!e.type)throw new Error("Type option of nest is not defined.");if(!(e.type instanceof s)&&"function"!=typeof e.type)throw new Error("Type option of nest must be a Telegram object.");return this.setNextParser("nest",t,e)},s.prototype.endianess=function(t){switch(t.toLowerCase()){case"little":this.endian="le";break;case"big":this.endian="be";break;default:throw new Error("Invalid endianess: "+t)}return this},s.prototype.setNextParser=function(t,e,i){if("bit"===t){var r=this.chain[this.chain.length-1];r&&"bits"===r.type?r.bitChain.push({varName:e,options:i}):this.chain.push({type:"bits",bitChain:[{varName:e,options:i}]})}else this.chain.push({type:t,varName:e,options:i});return this},s.prototype.decompress=function(t,e){void 0===e&&(e=!1);var i=this.parse({buffer:t,offset:0,bitOffset:0},{}),r=i.result;return e?{result:r,length:i.buf.offset}:r},s.prototype.parse=function(t,e){for(var i=0,r=this.chain;i<r.length;i+=1){var s=r[i];new n[s.type]({endian:this.endian}).parse(t,e,s)}return{buf:t,result:e}};var n={},o=function(t){this.endian=t.endian,this.buf=null,this.result=null,this.ownResult=null,this.item=null};o.prototype.parse=function(t,e,i){this.buf=t,this.result=e,this.item=i,this.initialize(),this.realParse(),this.assert(),this.formatter(),this.store(),this.updateStatus()},o.prototype.initialize=function(){},o.prototype.realParse=function(t,e,i){},o.prototype.formatter=function(t){var e=void 0!==t?t.options:this.item.options;e&&"function"==typeof e.formatter&&(this.ownResult=e.formatter.call(this,this.ownResult))},o.prototype.assert=function(t){var e=void 0!==t?t.options:this.item.options;if(e&&e.assert){var i=!0;switch(typeof e.assert){case"function":i=e.assert.call(this,this.ownResult);break;case"number":case"string":i=this.ownResult===e.assert;break;default:throw new Error("Assert option supports only numbers and string and assert functions.")}if(!i)throw new Error("Assert error: "+this.item.varName+" is "+this.ownResult)}},o.prototype.store=function(){this.result[this.item.varName]=this.ownResult},o.prototype.updateStatus=function(){},o.prototype.getResult=function(){return{buf:this.buf,result:this.result}},o.prototype.calcBitOffset=function(t){var e=this.buf.bitOffset;this.buf.bitOffset=(e+t)%8;var i=Math.floor((e+t)/8);this.buf.offset+=i},o.prototype.generateLength=function(t){var e;return"number"==typeof t?e=t:"function"==typeof t?e=t.call(this,this.result):"string"==typeof t&&(e=this.result[t]),e},Object.keys(t).forEach(function(e){n[""+e.toLowerCase()]=function(i){function r(){i.apply(this,arguments)}return i&&(r.__proto__=i),(r.prototype=Object.create(i&&i.prototype)).constructor=r,r.prototype.realParse=function(){var t=this.buf;this.ownResult=t.buffer["read"+e](t.offset)},r.prototype.updateStatus=function(){this.buf.offset+=t[e]},r}(o)});var a=function(e){function r(t){e.call(this,t),this.ownItemResult=null,this.type=null,this.typeName=null}return e&&(r.__proto__=e),(r.prototype=Object.create(e&&e.prototype)).constructor=r,r.prototype.initialize=function(){this.ownResult=[],this.defineType()},r.prototype.defineType=function(){var e=this.item.options.type;"string"==typeof e?(this.typeName=Object.keys(t).includes(i[e])?"PRIMITIVE_TYPES":e,this.type=i[e]):e instanceof s&&(this.typeName="Telegram",this.type=e)},r.prototype.realParse=function(){var t=0,e=this.item.options,i=e.length,r=e.readUntil;if(i){var s=this.generateLength(i);for(t=0;t<s;t++)this.parseItem()}else if("eof"===r)for(;this.buf.offset<this.buf.buffer.length;)this.parseItem();else if("number"==typeof r)for(;this.buf.offset<this.buf.buffer.length-r;)this.parseItem()},r.prototype.parseItem=function(){this.realParseItem(),this.pushItem(),this.updateItemStatus()},r.prototype.realParseItem=function(){var t=this.buf,e=t.buffer,i=t.offset;switch(this.typeName){case"PRIMITIVE_TYPES":this.ownItemResult=e["read"+this.type](i);break;case"string":var r=this.item.options.subOptions;r.hasOwnProperty("length")&&(r.length=this.generateLength(r.length));var n=(new s).string("tmp",r).parse(this.buf,{});this.ownItemResult=n.result.tmp;break;case"Telegram":var o=this.type.parse(this.buf,{});this.ownItemResult=o.result}},r.prototype.pushItem=function(){this.ownResult.push(this.ownItemResult)},r.prototype.updateItemStatus=function(){"PRIMITIVE_TYPES"===this.typeName&&(this.buf.offset+=t[this.type])},r}(o),h=function(t){function e(e){t.call(this,e),this.bitChain=null,this.bitItem=null}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.swap=function(t){for(var e=Math.ceil(t/8),i=this.buf,r=i.buffer,s=i.offset,n=s+e-1,o=0,a=r.slice(s,s+e).toString("hex").match(/.{1,2}/g);o<a.length;o+=1){r.write(a[o],n--,1,"hex")}this.buf.buffer=r},e.prototype.initialize=function(){this.bitChain=this.item.bitChain;var t=this.bitChain.reduce(function(t,e){return t+e.options.length},0);"be"===this.endian||this.swap(t)},e.prototype.parse=function(t,e,i){this.buf=t,this.result=e,this.item=i,this.initialize(),this.realParse()},e.prototype.realParse=function(){for(var t=0,e=this.bitChain;t<e.length;t+=1){this.bitItem=e[t],this.parseBit()}},e.prototype.parseBit=function(){this.realParseBit(),this.formatter(this.bitItem),this.assert(this.bitItem),this.store(),this.updateStatus()},e.prototype.realParseBit=function(){var t=this.buf,e=t.buffer,i=t.offset,r=t.bitOffset,s=this.bitItem.options.length,n=Math.ceil((r+s)/8),o=0;switch(n){case 1:o=e.readUInt8(i);break;case 2:o=e.readUInt16BE(i);break;case 3:o=e.readUInt16BE(i)<<8|e.readUInt8(i+2);break;case 4:o=e.readUInt32BE(i);break;case 5:o=e.readUInt32BE(i)<<8|e.readUInt8(i+4)}this.ownResult=o>>((r+s)%8?8*n-(r+s):0)&(1<<s)-1},e.prototype.store=function(){this.result[this.bitItem.varName]=this.ownResult},e.prototype.updateStatus=function(){this.calcBitOffset(this.bitItem.options.length)},e}(o),p=function(t){function e(){t.apply(this,arguments)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.realParse=function(){var t=this.item.options.type;if(t instanceof s){var e=t.parse(this.buf,{});this.ownResult=e.result}else if("function"==typeof t){var i=t.call(this,this.result);if(!(i instanceof s))throw new Error("Type option of nest must be a Telegram object.");var r=i.parse(this.buf,{});this.ownResult=r.result}},e.prototype.store=function(){var t=this.item.varName;t?this.result[t]=this.ownResult:Object.assign(this.result,this.ownResult)},e}(o),u=function(t){function e(){t.apply(this,arguments)}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.store=function(){},e.prototype.updateStatus=function(){var t=this.item.options,e=t.type,i=this.generateLength(t.length);"bit"===e?this.calcBitOffset(i):this.buf.offset+=i},e}(o),f=function(t){function e(e){t.call(this,e),this.stringLength=null,this.isZeroTerminated=!1}return t&&(e.__proto__=t),(e.prototype=Object.create(t&&t.prototype)).constructor=e,e.prototype.realParse=function(){var t=this.item.options,e=t.length,i=t.encoding,r=t.zeroTerminated,s=t.greedy,n=t.stripNull,o=this.buf,a=o.buffer,h=o.offset,p=h;if(e&&r){for(var u=p,f=this.generateLength(e);u<p+f&&0!==a.readUInt8(u);u++);this.stringLength=u-p,this.isZeroTerminated=this.stringLength!==f}else if(e)this.stringLength=this.generateLength(e);else if(r){for(var l=p;l<a.length&&0!==a.readUInt8(l);l++);this.stringLength=l-p,this.isZeroTerminated=l!==a.length}else s&&(this.stringLength=a.length-p);this.ownResult=a.toString(i,h,h+this.stringLength),n&&(this.ownResult=this.ownResult.replace(/\0/g,""))},e.prototype.store=function(){this.result[this.item.varName]=this.ownResult},e.prototype.updateStatus=function(){this.buf.offset+=this.isZeroTerminated?this.stringLength+1:this.stringLength},e}(o);n.array=a,n.bits=h,n.nest=p,n.skip=u,n.string=f;export default s;
2//# sourceMappingURL=index.mjs.map