Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tsbuffer

Package Overview
Dependencies
Maintainers
1
Versions
79
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsbuffer - npm Package Compare versions

Comparing version 0.3.0 to 1.0.0

tsbuffer/src/encoder/BufferPool.d.ts

2

index.js

@@ -1,1 +0,1 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),require("k8w-extend-native");var tslib_1=require("tslib"),tsbufferValidator=require("tsbuffer-validator"),Utf8Util=function(){function e(){}return e.measureLength=function(e){for(var t=0,r=0,i=0;i<e.length;++i)(r=e.charCodeAt(i))<128?t+=1:r<2048?t+=2:55296==(64512&r)&&56320==(64512&e.charCodeAt(i+1))?(++i,t+=4):t+=3;return t},e.encode=function(e,t,r){for(var i,n,a=r,s=0;s<e.length;++s)(i=e.charCodeAt(s))<128?t[r++]=i:(i<2048?t[r++]=i>>6|192:(55296==(64512&i)&&56320==(64512&(n=e.charCodeAt(s+1)))?(i=65536+((1023&i)<<10)+(1023&n),++s,t[r++]=i>>18|240,t[r++]=i>>12&63|128):t[r++]=i>>12|224,t[r++]=i>>6&63|128),t[r++]=63&i|128);return r-a},e.decode=function(e,t,r){if(r<1)return"";for(var i,n=void 0,a=[],s=0,o=t+r;t<o;)(i=e[t++])<128?a[s++]=i:191<i&&i<224?a[s++]=(31&i)<<6|63&e[t++]:239<i&&i<365?(i=((7&i)<<18|(63&e[t++])<<12|(63&e[t++])<<6|63&e[t++])-65536,a[s++]=55296+(i>>10),a[s++]=56320+(1023&i)):a[s++]=(15&i)<<12|(63&e[t++])<<6|63&e[t++],8191<s&&((n=n||[]).push(String.fromCharCode.apply(String,a)),s=0);return n?(s&&n.push(String.fromCharCode.apply(String,a.slice(0,s))),n.join("")):String.fromCharCode.apply(String,a.slice(0,s))},e}(),Varint64=function(){function s(e,t,r){this.uint32s=new Uint32Array([e,t]),void 0!==r&&(this._byteLength=r)}return s.from=function(e){if(0===e)return this.Zero;var t=e<0;t&&(e=-e);var r=e>>>0,i=(e-r)/4294967296>>>0;return t&&(i=~i>>>0,r=~r>>>0,4294967295<++r&&(r=0,4294967295<++i&&(i=0))),new s(i,r)},s.prototype.toNumber=function(e){if(!e&&this.uint32s[0]>>>31){var t=1+~this.uint32s[1]>>>0,r=~this.uint32s[0]>>>0;return t||(r=r+1>>>0),-(t+4294967296*r)}return this.uint32s[1]+4294967296*this.uint32s[0]},s.prototype.zzEncode=function(){var e=this.uint32s[0]>>31;return this.uint32s[0]=((this.uint32s[0]<<1|this.uint32s[1]>>>31)^e)>>>0,this.uint32s[1]=(this.uint32s[1]<<1^e)>>>0,this},s.prototype.zzDecode=function(){var e=-(1&this.uint32s[1]);return this.uint32s[1]=((this.uint32s[1]>>>1|this.uint32s[0]<<31)^e)>>>0,this.uint32s[0]=(this.uint32s[0]>>>1^e)>>>0,this},Object.defineProperty(s.prototype,"byteLength",{get:function(){if(void 0===this._byteLength){var e=this.uint32s[1],t=(this.uint32s[1]>>>28|this.uint32s[0]<<4)>>>0,r=this.uint32s[0]>>>24;this._byteLength=0==r?0==t?e<16384?e<128?1:2:e<2097152?3:4:t<16384?t<128?5:6:t<2097152?7:8:r<128?9:10}return this._byteLength},enumerable:!0,configurable:!0}),s.prototype.writeToBuffer=function(e,t){for(;this.uint32s[0];)e[t++]=127&this.uint32s[1]|128,this.uint32s[1]=(this.uint32s[1]>>>7|this.uint32s[0]<<25)>>>0,this.uint32s[0]>>>=7;for(;127<this.uint32s[1];)e[t++]=127&this.uint32s[1]|128,this.uint32s[1]=this.uint32s[1]>>>7;return e[t++]=this.uint32s[1],t},s.readFromBuffer=function(e,t){var r=t,i=0,n=0,a=0;if(!(4<e.byteLength-t)){for(;a<3;++a){if(t>=e.byteLength)throw new Error("Read varint error: index out of range");if(n=(n|(127&e[t])<<7*a)>>>0,e[t++]<128)return new s(i,n,t-r)}return new s(i,n=(n|(127&e[t++])<<7*a)>>>0,t-r)}for(;a<4;++a)if(n=(n|(127&e[t])<<7*a)>>>0,e[t++]<128)return new s(i,n,t-r);if(n=(n|(127&e[t])<<28)>>>0,i=(i|(127&e[t])>>4)>>>0,e[t++]<128)return new s(i,n,t-r);if(a=0,4<e.byteLength-t){for(;a<5;++a)if(i=(i|(127&e[t])<<7*a+3)>>>0,e[t++]<128)return new s(i,n,t-r)}else for(;a<5;++a){if(t>=e.byteLength)throw new Error("Read varint error: index out of range");if(i=(i|(127&e[t])<<7*a+3)>>>0,e[t++]<128)return new s(i,n,t-r)}throw Error("invalid varint encoding")},s.Zero=new s(0,0),s}(),BufferWriter=function(){function e(){this._ops=[]}return Object.defineProperty(e.prototype,"ops",{get:function(){return this._ops},enumerable:!0,configurable:!0}),e.prototype.clear=function(){this._ops=[]},e.prototype.push=function(e){return this._ops.push(this.req2op(e)),this},e.prototype.req2op=function(e){if("string"===e.type||"buffer"===e.type){var t=this.measureLength(e);return this.push({type:"varint",value:Varint64.from(t)}),tslib_1.__assign({},e,{length:t})}var r=this.measureLength(e);return tslib_1.__assign({},e,{length:r})},e.prototype.measureLength=function(e){switch(e.type){case"varint":return e.value.byteLength;case"string":return Utf8Util.measureLength(e.value);case"buffer":return e.value.byteLength;case"int32":case"uint32":case"float":return 4;case"double":return 8;case"boolean":return 1;default:return NaN}},e.prototype.finish=function(){for(var e=this._ops.sum(function(e){return e.length}),t=0,r=new Uint8Array(e),i=new DataView(r.buffer),n=0,a=this._ops;n<a.length;n++){var s=a[n];switch(s.type){case"varint":var o=s.value.writeToBuffer(r,t);if(o!==t+s.length)throw new Error("Error varint measuredLength "+s.length+", actual is "+(o-t)+", value is "+s.value.toNumber());break;case"int32":i.setInt32(r.byteOffset+t,s.value);break;case"uint32":i.setUint32(r.byteOffset+t,s.value);break;case"float":i.setFloat32(r.byteOffset+t,s.value);break;case"double":i.setFloat64(r.byteOffset+t,s.value);break;case"string":var u=Utf8Util.encode(s.value,r,t);if(u!==s.length)throw new Error("Expect "+s.length+" bytes but encoded "+u+" bytes");break;case"buffer":r.subarray(t,t+s.length).set(s.value);break;case"boolean":i.setUint8(r.byteOffset+t,s.value?255:0)}t+=s.length}return r},e}(),Config={interface:{maxExtendsNum:9}},TypedArrays={Int8Array:Int8Array,Int16Array:Int16Array,Int32Array:Int32Array,Uint8Array:Uint8Array,Uint16Array:Uint16Array,Uint32Array:Uint32Array,Float32Array:Float32Array,Float64Array:Float64Array},Encoder=function(){function e(e){this._writer=new BufferWriter,this._validator=e}return e.prototype.encode=function(e,t){return this._writer.clear(),this._write(e,t),this._writer.finish()},e.prototype._write=function(t,e,r){switch(e.type){case"Boolean":this._writer.push({type:"boolean",value:t});break;case"Number":this._writeNumber(t,e);break;case"String":this._writer.push({type:"string",value:t});break;case"Array":var i=t;this._writer.push({type:"varint",value:Varint64.from(i.length)});for(var n=0;n<i.length;++n)this._write(i[n],e.elementType);break;case"Tuple":if(64<e.elementTypes.length)throw new Error("Elements oversized, maximum supported tuple elements is 64, now get "+e.elementTypes.length);i=t;var a=[];for(n=0;n<i.length;++n)void 0!==i[n]&&a.push(n);for(var s=0,o=0,u=0,f=a;u<f.length;u++){var h=f[u];h<32?s|=1<<h:o|=1<<h-32}this._writer.push({type:"varint",value:new Varint64(o,s)});for(var p=0,c=a;p<c.length;p++){n=c[p];this._write(i[n],e.elementTypes[n])}break;case"Enum":var d=e.members.find(function(e){return e.value===t});if(!d)throw new Error("Unexpect enum value: "+t);this._writer.push({type:"varint",value:Varint64.from(d.id)});break;case"Any":void 0===t?this._writer.push({type:"string",value:"undefined"}):this._writer.push({type:"string",value:JSON.stringify(t)});break;case"NonPrimitive":this._writer.push({type:"string",value:JSON.stringify(t)});break;case"Literal":break;case"Interface":this._writeInterface(t,e,r);break;case"Buffer":this._writeBuffer(t,e);break;case"IndexedAccess":case"Reference":this._write(t,this._validator.protoHelper.parseReference(e),r);break;case"Pick":case"Partial":case"Omit":this._writeInterface(t,e.target,r);break;case"Overwrite":this._writeOverwrite(t,e,r);break;case"Union":this._writeUnion(t,e,r);break;case"Intersection":this._writeIntersection(t,e,r);break;default:throw new Error("Unrecognized schema type: "+e.type)}},e.prototype._writeNumber=function(e,t){var r=t.scalarType||"double";switch(r){case"int32":case"uint32":case"double":this._writer.push({type:r,value:e});break;case"int":this._writer.push({type:"varint",value:Varint64.from(e).zzEncode()});break;case"uint":this._writer.push({type:"varint",value:Varint64.from(e)});break;default:throw new Error("Scalar type not support : "+r)}},e.prototype._writeInterface=function(e,t,r,i){void 0===r&&(r={});var n=this._writer.ops.length,a=0,s=this._validator.protoHelper.parseReference(t);switch(s.type){case"Overwrite":return void this._writeOverwrite(e,s,r);case"Pick":case"Omit":case"Partial":return void this._writeInterface(e,s.target,r)}if(s.extends){if(s.extends.length>Config.interface.maxExtendsNum)throw new Error("Max support "+Config.interface.maxExtendsNum+" extends, actual: "+s.extends.length);for(var o=0,u=s.extends;o<u.length;o++){var f=u[o],h=f.id+1;this._writer.push({type:"varint",value:Varint64.from(h)});var p=this._writer.ops.length;this._writeInterface(e,f.type,r,!!s.indexSignature||i),this._writer.ops.length===p+1?this._writer.ops.splice(this._writer.ops.length-2,2):++a}}if(s.properties)for(var c=0,d=s.properties;c<d.length;c++){var l=d[c],y=this._validator.protoHelper.parseReference(l.type);if("Literal"!==y.type){if(void 0!==e[l.name]&&!r[l.name]){r[l.name]=1;h=l.id+Config.interface.maxExtendsNum+1;this._writer.push({type:"varint",value:Varint64.from(h)}),this._write(e[l.name],y),++a}}else r[l.name]=1}if(!i){var v=this._validator.protoHelper.getFlatInterfaceSchema(s);if(v.indexSignature)for(var _ in e)void 0!==e[_]&&(r[_]||(r[_]=1,this._writer.push({type:"varint",value:Varint64.from(0)}),this._writer.push({type:"string",value:_}),this._write(e[_],v.indexSignature.type),++a))}this._writer.ops.splice(n,0,this._writer.req2op({type:"varint",value:Varint64.from(a)}))},e.prototype._writeOverwrite=function(e,t,r){void 0===r&&(r={});var i,n,a=this._validator.protoHelper.parseReference(t.target),s=this._validator.protoHelper.parseReference(t.overwrite),o=this._validator.protoHelper.getFlatInterfaceSchema(a),u=this._validator.protoHelper.getFlatInterfaceSchema(s),f={},h={};if(u.properties)for(var p=0,c=u.properties;p<c.length;p++){void 0===e[(y=c[p]).name]||r[y.name]||(f[y.name]=e[y.name],r[y.name]=1)}if(o.properties)for(var d=0,l=o.properties;d<l.length;d++){var y;void 0===e[(y=l[d]).name]||r[y.name]||(h[y.name]=e[y.name],r[y.name]=1)}if(u.indexSignature?(n=u.indexSignature,i=f):o.indexSignature&&(n=o.indexSignature,i=h),n)for(var v in e)r[v]||(i[v]=e[v],r[v]=1);this._writeInterface(f,s),this._writeInterface(h,a)},e.prototype._writeUnion=function(e,t,r,i){void 0===r&&(r={}),i=i||(r?Object.keys(r):[]),this._validator.protoHelper.extendsUnionFields(i,t.members.map(function(e){return e.type}));for(var n=this._writer.ops.length,a=0,s=0,o=t.members;s<o.length;s++){var u=o[s];if((this._validator.protoHelper.isInterface(u.type)?this._validator.validateInterfaceReference(e,u.type,i):"Union"===u.type.type?this._validator.validateUnionType(e,u.type,i):"Intersection"===u.type.type?this._validator.validateIntersectionType(e,u.type,i):this._validator.validateBySchema(e,u.type)).isSucc&&(this._writer.push({type:"varint",value:Varint64.from(u.id)}),"Union"===u.type.type?this._writeUnion(e,u.type,r,i):this._write(e,u.type,r),a++,"object"!=typeof e))break}if(!(0<a))throw new Error("Non member is satisfied for union type");this._writer.ops.splice(n,0,this._writer.req2op({type:"varint",value:Varint64.from(a)}))},e.prototype._writeIntersection=function(e,t,r){void 0===r&&(r={}),this._writer.push({type:"varint",value:Varint64.from(t.members.length)});for(var i=0,n=t.members;i<n.length;i++){var a=n[i];this._writer.push({type:"varint",value:Varint64.from(a.id)}),this._write(e,a.type,r)}},e.prototype._writeBuffer=function(e,t){if(e instanceof ArrayBuffer)this._writer.push({type:"buffer",value:new Uint8Array(e)});else if(e instanceof Uint8Array)this._writer.push({type:"buffer",value:e});else{var r=e.constructor.name,i=TypedArrays[r],n=new Uint8Array(e.buffer,e.byteOffset,e.length*i.BYTES_PER_ELEMENT);this._writer.push({type:"buffer",value:n})}},e}(),BufferReader=function(){function e(){this._pos=0}return e.prototype.load=function(e,t){void 0===t&&(t=0),this._buf=e,this._pos=t,this._view=new DataView(e.buffer)},e.prototype.readVarint=function(){var e=Varint64.readFromBuffer(this._buf,this._pos);return this._pos+=e.byteLength,e},e.prototype.readUint=function(){return this.readVarint().toNumber(!0)},e.prototype.readInt=function(){return this.readVarint().zzDecode().toNumber()},e.prototype.readNumber=function(e){var t=this._pos;switch(e){case"int32":return this._pos+=4,this._view.getInt32(this._buf.byteOffset+t);case"uint32":return this._pos+=4,this._view.getUint32(this._buf.byteOffset+t);case"float":return this._pos+=4,this._view.getFloat32(this._buf.byteOffset+t);case"double":return this._pos+=8,this._view.getFloat64(this._buf.byteOffset+t);default:throw new Error("Error scalarType to read: "+e)}},e.prototype.readString=function(){var e=this.readUint(),t=Utf8Util.decode(this._buf,this._pos,e);return this._pos+=e,t},e.prototype.readBuffer=function(){var e=this.readUint(),t=this._buf.subarray(this._pos,this._pos+e);return this._pos+=e,t},e.prototype.readBoolean=function(){var e=this._view.getUint8(this._buf.byteOffset+this._pos++);if(255===e)return!0;if(0===e)return!1;throw new Error("Invalid boolean encoding ["+e+"] at pos "+(this._pos-1))},Object.defineProperty(e.prototype,"unreadByteLength",{get:function(){return this._buf.byteLength-this._pos},enumerable:!0,configurable:!0}),e}(),Decoder=function(){function e(e){this._reader=new BufferReader,this._validator=e}return e.prototype.decode=function(e,t){return this._reader.load(e),this._read(t)},e.prototype._read=function(e){switch(e.type){case"Boolean":return this._reader.readBoolean();case"Number":return this._readNumber(e);case"String":return this._reader.readString();case"Array":for(var t=[],r=this._reader.readUint(),i=0;i<r;++i){var n=this._read(e.elementType);t.push(n)}return t;case"Tuple":if(64<e.elementTypes.length)throw new Error("Elements oversized, maximum supported tuple elements is 64, now get "+e.elementTypes.length);t=[];var a=this._reader.readVarint(),s=[];for(i=0;i<32;++i)a.uint32s[1]&1<<i&&s.push(i);for(i=0;i<32;++i)a.uint32s[0]&1<<i&&s.push(i+32);if(!s.length)return[];for(var o=s.last(),u=(i=0,0),f=s[0];i<=o;++i)i===f?(t[i]=this._read(e.elementTypes[i]),f=s[++u]):t[i]=void 0;return t;case"Enum":var h=this._reader.readVarint().toNumber(),p=e.members.find(function(e){return e.id===h});if(!p)throw new Error("Invalid enum encoding: unexpected id "+h);return p.value;case"Any":case"NonPrimitive":var c=this._reader.readString();if("undefined"===c)return;return JSON.parse(c);case"Literal":return e.literal;case"Interface":return this._readInterface(e);case"Buffer":var d=this._reader.readBuffer();if(e.arrayType){if("BigInt64Array"===e.arrayType||"BigUint64Array"===e.arrayType)throw new Error("Unsupported arrayType: "+e.arrayType);if("Uint8Array"===e.arrayType)return d;var l=TypedArrays[e.arrayType];return d.byteOffset%l.BYTES_PER_ELEMENT==0?new l(d.buffer,d.byteOffset,d.byteLength/l.BYTES_PER_ELEMENT):new l(d.buffer.slice(d.byteOffset,d.byteOffset+d.byteLength))}return d.buffer.slice(d.byteOffset,d.byteOffset+d.byteLength);case"IndexedAccess":case"Reference":return this._read(this._validator.protoHelper.parseReference(e));case"Pick":case"Partial":case"Omit":return this._read(e.target);case"Overwrite":return this._readOverwrite(e);case"Union":case"Intersection":return this._readUnionOrIntersection(e);default:throw new Error("Unrecognized schema type: "+e.type)}},e.prototype._readNumber=function(e){var t=e.scalarType||"double";switch(t){case"int32":case"uint32":case"double":return this._reader.readNumber(t);case"int":return this._reader.readInt();case"uint":return this._reader.readUint();default:throw new Error("Scalar type not support : "+t)}},e.prototype._readInterface=function(u){for(var f={},h=this._validator.protoHelper.getFlatInterfaceSchema(u),e=this._reader.readUint(),t=function(e){var t=p._reader.readUint();if(0===t){var r=p._reader.readString();if(!h.indexSignature)throw new Error("Invalid interface encoding: unexpected indexSignature at block"+e);f[r]=p._read(h.indexSignature.type)}else if(t<=9){var i=t-1,n=u.extends&&u.extends.find(function(e){return e.id===i});if(!n)throw new Error("Invalid interface encoding: unexpected extendId "+i);var a=p._read(n.type);Object.assign(f,a)}else{var s=t-10,o=u.properties&&u.properties.find(function(e){return e.id===s});if(!o)throw new Error("Invalid interface encoding: unexpected propertyId "+s);f[o.name]=p._read(o.type)}},p=this,r=0;r<e;++r)t(r);for(var i=0,n=h.properties;i<n.length;i++){var a=n[i];if(!f.hasOwnProperty(a.name)){var s=this._validator.protoHelper.parseReference(a.type);"Literal"===s.type&&(f[a.name]=s.literal)}}return f},e.prototype._readOverwrite=function(e){var t=this._read(e.overwrite),r=this._read(e.target);return Object.assign({},r,t)},e.prototype._readUnionOrIntersection=function(n){for(var a,e=this._reader.readUint(),t=function(e){var t=s._reader.readUint(),r=n.members.find(function(e){return e.id===t});if(!r)throw new Error("Invalid "+n.type+" encoding: invalid member id "+t);var i=s._read(r.type);s._isObject(a)&&s._isObject(i)?Object.assign(a,i):a=i},s=this,r=0;r<e;++r)t();return a},e.prototype._isObject=function(e){return"object"==typeof e&&null!==e},e}(),TSBuffer=function(){function e(e){this._proto=e,this._validator=new tsbufferValidator.TSBufferValidator(e),this._encoder=new Encoder(this._validator),this._decoder=new Decoder(this._validator)}return e.prototype.encode=function(e,t,r){var i=this._proto[t];if(!i)throw new Error("Cannot find schema: "+t);if(!r||!r.skipValidate){var n=this._validator.validateBySchema(e,i);if(!n.isSucc)throw new Error(n.originalError.message)}return this._encoder.encode(e,i)},e.prototype.decode=function(e,t,r){var i,n=this._proto[t];if(!n)throw new Error("Cannot find schema: "+t);try{i=this._decoder.decode(e,n)}catch(e){var a=new Error("Invalid buffer encoding");throw a.encodingError=e,a}if(!r||!r.skipValidate){var s=this._validator.validateBySchema(i,n);if(!s.isSucc)throw new Error(s.originalError.message)}return i},e.prototype.validate=function(e,t){return this._validator.validate(e,t)},e}();exports.TSBuffer=TSBuffer;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),require("k8w-extend-native");var tslib_1=require("tslib"),tsbufferValidator=require("tsbuffer-validator"),Utf8Util=function(){function e(){}return e.measureLength=function(e){for(var t=0,r=0,i=0;i<e.length;++i)(r=e.charCodeAt(i))<128?t+=1:r<2048?t+=2:55296==(64512&r)&&56320==(64512&e.charCodeAt(i+1))?(++i,t+=4):t+=3;return t},e.encode=function(e,t,r){for(var i,n,a=r,s=0;s<e.length;++s)(i=e.charCodeAt(s))<128?t[r++]=i:(i<2048?t[r++]=i>>6|192:(55296==(64512&i)&&56320==(64512&(n=e.charCodeAt(s+1)))?(i=65536+((1023&i)<<10)+(1023&n),++s,t[r++]=i>>18|240,t[r++]=i>>12&63|128):t[r++]=i>>12|224,t[r++]=i>>6&63|128),t[r++]=63&i|128);return r-a},e.decode=function(e,t,r){if(r<1)return"";for(var i,n=void 0,a=[],s=0,o=t+r;t<o;)(i=e[t++])<128?a[s++]=i:191<i&&i<224?a[s++]=(31&i)<<6|63&e[t++]:239<i&&i<365?(i=((7&i)<<18|(63&e[t++])<<12|(63&e[t++])<<6|63&e[t++])-65536,a[s++]=55296+(i>>10),a[s++]=56320+(1023&i)):a[s++]=(15&i)<<12|(63&e[t++])<<6|63&e[t++],8191<s&&((n=n||[]).push(String.fromCharCode.apply(String,a)),s=0);return n?(s&&n.push(String.fromCharCode.apply(String,a.slice(0,s))),n.join("")):String.fromCharCode.apply(String,a.slice(0,s))},e}(),Varint64=function(){function s(e,t,r){this.uint32s=new Uint32Array([e,t]),void 0!==r&&(this._byteLength=r)}return s.from=function(e){if(0===e)return this.Zero;var t=e<0;t&&(e=-e);var r=e>>>0,i=(e-r)/4294967296>>>0;return t&&(i=~i>>>0,r=~r>>>0,4294967295<++r&&(r=0,4294967295<++i&&(i=0))),new s(i,r)},s.prototype.toNumber=function(e){if(!e&&this.uint32s[0]>>>31){var t=1+~this.uint32s[1]>>>0,r=~this.uint32s[0]>>>0;return t||(r=r+1>>>0),-(t+4294967296*r)}return this.uint32s[1]+4294967296*this.uint32s[0]},s.prototype.zzEncode=function(){var e=this.uint32s[0]>>31;return this.uint32s[0]=((this.uint32s[0]<<1|this.uint32s[1]>>>31)^e)>>>0,this.uint32s[1]=(this.uint32s[1]<<1^e)>>>0,this},s.prototype.zzDecode=function(){var e=-(1&this.uint32s[1]);return this.uint32s[1]=((this.uint32s[1]>>>1|this.uint32s[0]<<31)^e)>>>0,this.uint32s[0]=(this.uint32s[0]>>>1^e)>>>0,this},Object.defineProperty(s.prototype,"byteLength",{get:function(){if(void 0===this._byteLength){var e=this.uint32s[1],t=(this.uint32s[1]>>>28|this.uint32s[0]<<4)>>>0,r=this.uint32s[0]>>>24;this._byteLength=0==r?0==t?e<16384?e<128?1:2:e<2097152?3:4:t<16384?t<128?5:6:t<2097152?7:8:r<128?9:10}return this._byteLength},enumerable:!0,configurable:!0}),s.prototype.writeToBuffer=function(e,t){for(;this.uint32s[0];)e[t++]=127&this.uint32s[1]|128,this.uint32s[1]=(this.uint32s[1]>>>7|this.uint32s[0]<<25)>>>0,this.uint32s[0]>>>=7;for(;127<this.uint32s[1];)e[t++]=127&this.uint32s[1]|128,this.uint32s[1]=this.uint32s[1]>>>7;return e[t++]=this.uint32s[1],t},s.readFromBuffer=function(e,t){var r=t,i=0,n=0,a=0;if(!(4<e.byteLength-t)){for(;a<3;++a){if(t>=e.byteLength)throw new Error("Read varint error: index out of range");if(n=(n|(127&e[t])<<7*a)>>>0,e[t++]<128)return new s(i,n,t-r)}return new s(i,n=(n|(127&e[t++])<<7*a)>>>0,t-r)}for(;a<4;++a)if(n=(n|(127&e[t])<<7*a)>>>0,e[t++]<128)return new s(i,n,t-r);if(n=(n|(127&e[t])<<28)>>>0,i=(i|(127&e[t])>>4)>>>0,e[t++]<128)return new s(i,n,t-r);if(a=0,4<e.byteLength-t){for(;a<5;++a)if(i=(i|(127&e[t])<<7*a+3)>>>0,e[t++]<128)return new s(i,n,t-r)}else for(;a<5;++a){if(t>=e.byteLength)throw new Error("Read varint error: index out of range");if(i=(i|(127&e[t])<<7*a+3)>>>0,e[t++]<128)return new s(i,n,t-r)}throw Error("invalid varint encoding")},s.Zero=new s(0,0),s}(),BufferWriter=function(){function e(){this._ops=[]}return Object.defineProperty(e.prototype,"ops",{get:function(){return this._ops},enumerable:!0,configurable:!0}),e.prototype.clear=function(){this._ops=[]},e.prototype.push=function(e){return this._ops.push(this.req2op(e)),this},e.prototype.req2op=function(e){if("string"===e.type||"buffer"===e.type){var t=this.measureLength(e);return this.push({type:"varint",value:Varint64.from(t)}),tslib_1.__assign({},e,{length:t})}var r=this.measureLength(e);return tslib_1.__assign({},e,{length:r})},e.prototype.measureLength=function(e){switch(e.type){case"varint":return e.value.byteLength;case"string":return Utf8Util.measureLength(e.value);case"buffer":return e.value.byteLength;case"int32":case"uint32":case"float":return 4;case"double":return 8;case"boolean":return 1;default:return NaN}},e.prototype.finish=function(){for(var e=this._ops.sum(function(e){return e.length}),t=0,r=new Uint8Array(e),i=new DataView(r.buffer),n=0,a=this._ops;n<a.length;n++){var s=a[n];switch(s.type){case"varint":var o=s.value.writeToBuffer(r,t);if(o!==t+s.length)throw new Error("Error varint measuredLength "+s.length+", actual is "+(o-t)+", value is "+s.value.toNumber());break;case"int32":i.setInt32(r.byteOffset+t,s.value);break;case"uint32":i.setUint32(r.byteOffset+t,s.value);break;case"float":i.setFloat32(r.byteOffset+t,s.value);break;case"double":i.setFloat64(r.byteOffset+t,s.value);break;case"string":var u=Utf8Util.encode(s.value,r,t);if(u!==s.length)throw new Error("Expect "+s.length+" bytes but encoded "+u+" bytes");break;case"buffer":r.subarray(t,t+s.length).set(s.value);break;case"boolean":i.setUint8(r.byteOffset+t,s.value?255:0)}t+=s.length}return r},e}(),Config={interface:{maxExtendsNum:9}},TypedArrays={Int8Array:Int8Array,Int16Array:Int16Array,Int32Array:Int32Array,Uint8Array:Uint8Array,Uint16Array:Uint16Array,Uint32Array:Uint32Array,Float32Array:Float32Array,Float64Array:Float64Array},Encoder=function(){function e(e){this._writer=new BufferWriter,this._validator=e}return e.prototype.encode=function(e,t){return this._writer.clear(),this._write(e,t),this._writer.finish()},e.prototype._write=function(t,e,r){switch(e.type){case"Boolean":this._writer.push({type:"boolean",value:t});break;case"Number":this._writeNumber(t,e);break;case"String":this._writer.push({type:"string",value:t});break;case"Array":var i=t;this._writer.push({type:"varint",value:Varint64.from(i.length)});for(var n=0;n<i.length;++n)this._write(i[n],e.elementType);break;case"Tuple":if(64<e.elementTypes.length)throw new Error("Elements oversized, maximum supported tuple elements is 64, now get "+e.elementTypes.length);i=t;var a=[];for(n=0;n<i.length;++n)void 0!==i[n]&&a.push(n);for(var s=0,o=0,u=0,f=a;u<f.length;u++){var h=f[u];h<32?s|=1<<h:o|=1<<h-32}this._writer.push({type:"varint",value:new Varint64(o,s)});for(var p=0,c=a;p<c.length;p++){n=c[p];this._write(i[n],e.elementTypes[n])}break;case"Enum":var d=e.members.find(function(e){return e.value===t});if(!d)throw new Error("Unexpect enum value: "+t);this._writer.push({type:"varint",value:Varint64.from(d.id)});break;case"Any":void 0===t?this._writer.push({type:"string",value:"undefined"}):this._writer.push({type:"string",value:JSON.stringify(t)});break;case"NonPrimitive":this._writer.push({type:"string",value:JSON.stringify(t)});break;case"Literal":break;case"Interface":this._writeInterface(t,e,r);break;case"Buffer":this._writeBuffer(t,e);break;case"IndexedAccess":case"Reference":this._write(t,this._validator.protoHelper.parseReference(e),r);break;case"Pick":case"Partial":case"Omit":this._writeInterface(t,e.target,r);break;case"Overwrite":this._writeOverwrite(t,e,r);break;case"Union":this._writeUnion(t,e,r);break;case"Intersection":this._writeIntersection(t,e,r);break;default:throw new Error("Unrecognized schema type: "+e.type)}},e.prototype._writeNumber=function(e,t){var r=t.scalarType||"double";switch(r){case"int32":case"uint32":case"double":this._writer.push({type:r,value:e});break;case"int":this._writer.push({type:"varint",value:Varint64.from(e).zzEncode()});break;case"uint":this._writer.push({type:"varint",value:Varint64.from(e)});break;default:throw new Error("Scalar type not support : "+r)}},e.prototype._writeInterface=function(e,t,r,i){void 0===r&&(r={});var n=this._writer.ops.length,a=0,s=this._validator.protoHelper.parseReference(t);switch(s.type){case"Overwrite":return void this._writeOverwrite(e,s,r);case"Pick":case"Omit":case"Partial":return void this._writeInterface(e,s.target,r)}if(s.extends){if(s.extends.length>Config.interface.maxExtendsNum)throw new Error("Max support "+Config.interface.maxExtendsNum+" extends, actual: "+s.extends.length);for(var o=0,u=s.extends;o<u.length;o++){var f=u[o],h=f.id+1;this._writer.push({type:"varint",value:Varint64.from(h)});var p=this._writer.ops.length;this._writeInterface(e,f.type,r,!!s.indexSignature||i),this._writer.ops.length===p+1?this._writer.ops.splice(this._writer.ops.length-2,2):++a}}if(s.properties)for(var c=0,d=s.properties;c<d.length;c++){var l=d[c],y=this._validator.protoHelper.parseReference(l.type);if("Literal"!==y.type){if(void 0!==e[l.name]&&!r[l.name]){r[l.name]=1;h=l.id+Config.interface.maxExtendsNum+1;this._writer.push({type:"varint",value:Varint64.from(h)}),this._write(e[l.name],y),++a}}else r[l.name]=1}if(!i){var v=this._validator.protoHelper.getFlatInterfaceSchema(s);if(v.indexSignature)for(var _ in e)void 0!==e[_]&&(r[_]||(r[_]=1,this._writer.push({type:"varint",value:Varint64.from(0)}),this._writer.push({type:"string",value:_}),this._write(e[_],v.indexSignature.type),++a))}this._writer.ops.splice(n,0,this._writer.req2op({type:"varint",value:Varint64.from(a)}))},e.prototype._writeOverwrite=function(e,t,r){void 0===r&&(r={});var i,n,a=this._validator.protoHelper.parseReference(t.target),s=this._validator.protoHelper.parseReference(t.overwrite),o=this._validator.protoHelper.getFlatInterfaceSchema(a),u=this._validator.protoHelper.getFlatInterfaceSchema(s),f={},h={};if(u.properties)for(var p=0,c=u.properties;p<c.length;p++){void 0===e[(y=c[p]).name]||r[y.name]||(f[y.name]=e[y.name],r[y.name]=1)}if(o.properties)for(var d=0,l=o.properties;d<l.length;d++){var y;void 0===e[(y=l[d]).name]||r[y.name]||(h[y.name]=e[y.name],r[y.name]=1)}if(u.indexSignature?(n=u.indexSignature,i=f):o.indexSignature&&(n=o.indexSignature,i=h),n)for(var v in e)r[v]||(i[v]=e[v],r[v]=1);this._writeInterface(f,s),this._writeInterface(h,a)},e.prototype._writeUnion=function(e,t,r,i){void 0===r&&(r={}),i=i||(r?Object.keys(r):[]),this._validator.protoHelper.extendsUnionFields(i,t.members.map(function(e){return e.type}));for(var n=this._writer.ops.length,a=0,s=0,o=t.members;s<o.length;s++){var u=o[s];if((this._validator.protoHelper.isInterface(u.type)?this._validator.validateInterfaceReference(e,u.type,i):"Union"===u.type.type?this._validator.validateUnionType(e,u.type,i):"Intersection"===u.type.type?this._validator.validateIntersectionType(e,u.type,i):this._validator.validateBySchema(e,u.type)).isSucc&&(this._writer.push({type:"varint",value:Varint64.from(u.id)}),"Union"===u.type.type?this._writeUnion(e,u.type,r,i):this._write(e,u.type,r),a++,"object"!=typeof e))break}if(!(0<a))throw new Error("Non member is satisfied for union type");this._writer.ops.splice(n,0,this._writer.req2op({type:"varint",value:Varint64.from(a)}))},e.prototype._writeIntersection=function(e,t,r){void 0===r&&(r={}),this._writer.push({type:"varint",value:Varint64.from(t.members.length)});for(var i=0,n=t.members;i<n.length;i++){var a=n[i];this._writer.push({type:"varint",value:Varint64.from(a.id)}),this._write(e,a.type,r)}},e.prototype._writeBuffer=function(e,t){if(e instanceof ArrayBuffer)this._writer.push({type:"buffer",value:new Uint8Array(e)});else if(e instanceof Uint8Array)this._writer.push({type:"buffer",value:e});else{var r=e.constructor.name,i=TypedArrays[r],n=new Uint8Array(e.buffer,e.byteOffset,e.length*i.BYTES_PER_ELEMENT);this._writer.push({type:"buffer",value:n})}},e}(),BufferReader=function(){function e(){this._pos=0}return e.prototype.load=function(e,t){void 0===t&&(t=0),this._buf=e,this._pos=t,this._view=new DataView(e.buffer)},e.prototype.readVarint=function(){var e=Varint64.readFromBuffer(this._buf,this._pos);return this._pos+=e.byteLength,e},e.prototype.readUint=function(){return this.readVarint().toNumber(!0)},e.prototype.readInt=function(){return this.readVarint().zzDecode().toNumber()},e.prototype.readNumber=function(e){var t=this._pos;switch(e){case"int32":return this._pos+=4,this._view.getInt32(this._buf.byteOffset+t);case"uint32":return this._pos+=4,this._view.getUint32(this._buf.byteOffset+t);case"float":return this._pos+=4,this._view.getFloat32(this._buf.byteOffset+t);case"double":return this._pos+=8,this._view.getFloat64(this._buf.byteOffset+t);default:throw new Error("Error scalarType to read: "+e)}},e.prototype.readString=function(){var e=this.readUint(),t=Utf8Util.decode(this._buf,this._pos,e);return this._pos+=e,t},e.prototype.readBuffer=function(){var e=this.readUint(),t=this._buf.subarray(this._pos,this._pos+e);return this._pos+=e,t},e.prototype.readBoolean=function(){var e=this._view.getUint8(this._buf.byteOffset+this._pos++);if(255===e)return!0;if(0===e)return!1;throw new Error("Invalid boolean encoding ["+e+"] at pos "+(this._pos-1))},Object.defineProperty(e.prototype,"unreadByteLength",{get:function(){return this._buf.byteLength-this._pos},enumerable:!0,configurable:!0}),e}(),Decoder=function(){function e(e){this._reader=new BufferReader,this._validator=e}return e.prototype.decode=function(e,t){return this._reader.load(e),this._read(t)},e.prototype._read=function(e){switch(e.type){case"Boolean":return this._reader.readBoolean();case"Number":return this._readNumber(e);case"String":return this._reader.readString();case"Array":for(var t=[],r=this._reader.readUint(),i=0;i<r;++i){var n=this._read(e.elementType);t.push(n)}return t;case"Tuple":if(64<e.elementTypes.length)throw new Error("Elements oversized, maximum supported tuple elements is 64, now get "+e.elementTypes.length);t=[];var a=this._reader.readVarint(),s=[];for(i=0;i<32;++i)a.uint32s[1]&1<<i&&s.push(i);for(i=0;i<32;++i)a.uint32s[0]&1<<i&&s.push(i+32);if(!s.length)return[];for(var o=s.last(),u=(i=0,0),f=s[0];i<=o;++i)i===f?(t[i]=this._read(e.elementTypes[i]),f=s[++u]):t[i]=void 0;return t;case"Enum":var h=this._reader.readVarint().toNumber(),p=e.members.find(function(e){return e.id===h});if(!p)throw new Error("Invalid enum encoding: unexpected id "+h);return p.value;case"Any":case"NonPrimitive":var c=this._reader.readString();if("undefined"===c)return;return JSON.parse(c);case"Literal":return e.literal;case"Interface":return this._readInterface(e);case"Buffer":var d=this._reader.readBuffer();if(e.arrayType){if("BigInt64Array"===e.arrayType||"BigUint64Array"===e.arrayType)throw new Error("Unsupported arrayType: "+e.arrayType);if("Uint8Array"===e.arrayType)return d;var l=TypedArrays[e.arrayType];return d.byteOffset%l.BYTES_PER_ELEMENT==0?new l(d.buffer,d.byteOffset,d.byteLength/l.BYTES_PER_ELEMENT):new l(d.buffer.slice(d.byteOffset,d.byteOffset+d.byteLength))}return d.buffer.slice(d.byteOffset,d.byteOffset+d.byteLength);case"IndexedAccess":case"Reference":return this._read(this._validator.protoHelper.parseReference(e));case"Pick":case"Partial":case"Omit":return this._read(e.target);case"Overwrite":return this._readOverwrite(e);case"Union":case"Intersection":return this._readUnionOrIntersection(e);default:throw new Error("Unrecognized schema type: "+e.type)}},e.prototype._readNumber=function(e){var t=e.scalarType||"double";switch(t){case"int32":case"uint32":case"double":return this._reader.readNumber(t);case"int":return this._reader.readInt();case"uint":return this._reader.readUint();default:throw new Error("Scalar type not support : "+t)}},e.prototype._readInterface=function(u){for(var f={},h=this._validator.protoHelper.getFlatInterfaceSchema(u),e=this._reader.readUint(),t=function(e){var t=p._reader.readUint();if(0===t){var r=p._reader.readString();if(!h.indexSignature)throw new Error("Invalid interface encoding: unexpected indexSignature at block"+e);f[r]=p._read(h.indexSignature.type)}else if(t<=9){var i=t-1,n=u.extends&&u.extends.find(function(e){return e.id===i});if(!n)throw new Error("Invalid interface encoding: unexpected extendId "+i);var a=p._read(n.type);Object.assign(f,a)}else{var s=t-10,o=u.properties&&u.properties.find(function(e){return e.id===s});if(!o)throw new Error("Invalid interface encoding: unexpected propertyId "+s);f[o.name]=p._read(o.type)}},p=this,r=0;r<e;++r)t(r);for(var i=0,n=h.properties;i<n.length;i++){var a=n[i];if(!f.hasOwnProperty(a.name)){var s=this._validator.protoHelper.parseReference(a.type);"Literal"===s.type&&(f[a.name]=s.literal)}}return f},e.prototype._readOverwrite=function(e){var t=this._read(e.overwrite),r=this._read(e.target);return Object.assign({},r,t)},e.prototype._readUnionOrIntersection=function(n){for(var a,e=this._reader.readUint(),t=function(e){var t=s._reader.readUint(),r=n.members.find(function(e){return e.id===t});if(!r)throw new Error("Invalid "+n.type+" encoding: invalid member id "+t);var i=s._read(r.type);s._isObject(a)&&s._isObject(i)?Object.assign(a,i):a=i},s=this,r=0;r<e;++r)t();return a},e.prototype._isObject=function(e){return"object"==typeof e&&null!==e},e}(),TSBuffer=function(){function e(e){this._proto=e,this._validator=new tsbufferValidator.TSBufferValidator(e),this._encoder=new Encoder(this._validator),this._decoder=new Decoder(this._validator)}return e.prototype.encode=function(e,t,r){var i;if("string"==typeof t){if(!(i=this._proto[t]))throw new Error("Cannot find schema: "+t)}else i=t;if(!r||!r.skipValidate){var n=this._validator.validateBySchema(e,i);if(!n.isSucc)throw new Error(n.originalError.message)}return this._encoder.encode(e,i)},e.prototype.decode=function(e,t,r){var i,n;if("string"==typeof t){if(!(i=this._proto[t]))throw new Error("Cannot find schema: "+t)}else i=t;try{n=this._decoder.decode(e,i)}catch(e){var a=new Error("Invalid buffer encoding");throw a.encodingError=e,a}if(!r||!r.skipValidate){var s=this._validator.validateBySchema(n,i);if(!s.isSucc)throw new Error(s.originalError.message)}return n},e.prototype.validate=function(e,t){var r;if("string"==typeof t){if(!(r=this._proto[t]))throw new Error("Cannot find schema: "+t)}else r=t;return this._validator.validateBySchema(e,r)},e}();exports.TSBuffer=TSBuffer;
{
"name": "tsbuffer",
"version": "0.3.0",
"version": "1.0.0",
"description": "",

@@ -5,0 +5,0 @@ "main": "index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc