@msgpack/msgpack
Advanced tools
Comparing version 2.8.0 to 3.0.0-beta1
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.MessagePack=t():e.MessagePack=t()}(this,(function(){return function(){"use strict";var e={d:function(t,n){for(var r in n)e.o(n,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:n[r]})},o:function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r:function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{DataViewIndexOutOfBoundsError:function(){return K},DecodeError:function(){return m},Decoder:function(){return G},EXT_TIMESTAMP:function(){return U},Encoder:function(){return z},ExtData:function(){return b},ExtensionCodec:function(){return A},decode:function(){return q},decodeArrayStream:function(){return ne},decodeAsync:function(){return te},decodeMulti:function(){return J},decodeMultiStream:function(){return re},decodeStream:function(){return ie},decodeTimestampExtension:function(){return L},decodeTimestampToTimeSpec:function(){return T},encode:function(){return C},encodeDateToTimeSpec:function(){return E},encodeTimeSpecToTimestamp:function(){return S},encodeTimestampExtension:function(){return B}});var n=4294967295;function r(e,t,n){var r=Math.floor(n/4294967296),i=n;e.setUint32(t,r),e.setUint32(t+4,i)}function i(e,t){return 4294967296*e.getInt32(t)+e.getUint32(t+4)}var o,s,a,c=function(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,i,o=n.call(e),s=[];try{for(;(void 0===t||t-- >0)&&!(r=o.next()).done;)s.push(r.value)}catch(e){i={error:e}}finally{try{r&&!r.done&&(n=o.return)&&n.call(o)}finally{if(i)throw i.error}}return s},h=function(e,t,n){if(n||2===arguments.length)for(var r,i=0,o=t.length;i<o;i++)!r&&i in t||(r||(r=Array.prototype.slice.call(t,0,i)),r[i]=t[i]);return e.concat(r||Array.prototype.slice.call(t))},u=("undefined"==typeof process||"never"!==(null===(o=null===process||void 0===process?void 0:process.env)||void 0===o?void 0:o.TEXT_ENCODING))&&"undefined"!=typeof TextEncoder&&"undefined"!=typeof TextDecoder;function f(e){for(var t=e.length,n=0,r=0;r<t;){var i=e.charCodeAt(r++);if(0!=(4294967168&i))if(0==(4294965248&i))n+=2;else{if(i>=55296&&i<=56319&&r<t){var o=e.charCodeAt(r);56320==(64512&o)&&(++r,i=((1023&i)<<10)+(1023&o)+65536)}n+=0==(4294901760&i)?3:4}else n++}return n}var l=u?new TextEncoder:void 0,p=u?"undefined"!=typeof process&&"force"!==(null===(s=null===process||void 0===process?void 0:process.env)||void 0===s?void 0:s.TEXT_ENCODING)?200:0:n,d=(null==l?void 0:l.encodeInto)?function(e,t,n){l.encodeInto(e,t.subarray(n))}:function(e,t,n){t.set(l.encode(e),n)};function y(e,t,n){for(var r=t,i=r+n,o=[],s="";r<i;){var a=e[r++];if(0==(128&a))o.push(a);else if(192==(224&a)){var u=63&e[r++];o.push((31&a)<<6|u)}else if(224==(240&a)){u=63&e[r++];var f=63&e[r++];o.push((31&a)<<12|u<<6|f)}else if(240==(248&a)){var l=(7&a)<<18|(u=63&e[r++])<<12|(f=63&e[r++])<<6|63&e[r++];l>65535&&(l-=65536,o.push(l>>>10&1023|55296),l=56320|1023&l),o.push(l)}else o.push(a);o.length>=4096&&(s+=String.fromCharCode.apply(String,h([],c(o),!1)),o.length=0)}return o.length>0&&(s+=String.fromCharCode.apply(String,h([],c(o),!1))),s}var w,v=u?new TextDecoder:null,g=u?"undefined"!=typeof process&&"force"!==(null===(a=null===process||void 0===process?void 0:process.env)||void 0===a?void 0:a.TEXT_DECODER)?200:0:n,b=function(e,t){this.type=e,this.data=t},x=(w=function(e,t){return w=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])},w(e,t)},function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}w(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}),m=function(e){function t(n){var r=e.call(this,n)||this,i=Object.create(t.prototype);return Object.setPrototypeOf(r,i),Object.defineProperty(r,"name",{configurable:!0,enumerable:!1,value:t.name}),r}return x(t,e),t}(Error),U=-1;function S(e){var t,n=e.sec,i=e.nsec;if(n>=0&&i>=0&&n<=17179869183){if(0===i&&n<=4294967295){var o=new Uint8Array(4);return(t=new DataView(o.buffer)).setUint32(0,n),o}var s=n/4294967296,a=4294967295&n;return o=new Uint8Array(8),(t=new DataView(o.buffer)).setUint32(0,i<<2|3&s),t.setUint32(4,a),o}return o=new Uint8Array(12),(t=new DataView(o.buffer)).setUint32(0,i),r(t,4,n),o}function E(e){var t=e.getTime(),n=Math.floor(t/1e3),r=1e6*(t-1e3*n),i=Math.floor(r/1e9);return{sec:n+i,nsec:r-1e9*i}}function B(e){return e instanceof Date?S(E(e)):null}function T(e){var t=new DataView(e.buffer,e.byteOffset,e.byteLength);switch(e.byteLength){case 4:return{sec:t.getUint32(0),nsec:0};case 8:var n=t.getUint32(0);return{sec:4294967296*(3&n)+t.getUint32(4),nsec:n>>>2};case 12:return{sec:i(t,4),nsec:t.getUint32(0)};default:throw new m("Unrecognized data size for timestamp (expected 4, 8, or 12): ".concat(e.length))}}function L(e){var t=T(e);return new Date(1e3*t.sec+t.nsec/1e6)}var I={type:U,encode:B,decode:L},A=function(){function e(){this.builtInEncoders=[],this.builtInDecoders=[],this.encoders=[],this.decoders=[],this.register(I)}return e.prototype.register=function(e){var t=e.type,n=e.encode,r=e.decode;if(t>=0)this.encoders[t]=n,this.decoders[t]=r;else{var i=1+t;this.builtInEncoders[i]=n,this.builtInDecoders[i]=r}},e.prototype.tryToEncode=function(e,t){for(var n=0;n<this.builtInEncoders.length;n++)if(null!=(r=this.builtInEncoders[n])&&null!=(i=r(e,t)))return new b(-1-n,i);for(n=0;n<this.encoders.length;n++){var r,i;if(null!=(r=this.encoders[n])&&null!=(i=r(e,t)))return new b(n,i)}return e instanceof b?e:null},e.prototype.decode=function(e,t,n){var r=t<0?this.builtInDecoders[-1-t]:this.decoders[t];return r?r(e,t,n):new b(t,e)},e.defaultCodec=new e,e}();function k(e){return e instanceof Uint8Array?e:ArrayBuffer.isView(e)?new Uint8Array(e.buffer,e.byteOffset,e.byteLength):e instanceof ArrayBuffer?new Uint8Array(e):Uint8Array.from(e)}var M=function(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],r=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")},z=function(){function e(e,t,n,r,i,o,s,a){void 0===e&&(e=A.defaultCodec),void 0===t&&(t=void 0),void 0===n&&(n=100),void 0===r&&(r=2048),void 0===i&&(i=!1),void 0===o&&(o=!1),void 0===s&&(s=!1),void 0===a&&(a=!1),this.extensionCodec=e,this.context=t,this.maxDepth=n,this.initialBufferSize=r,this.sortKeys=i,this.forceFloat32=o,this.ignoreUndefined=s,this.forceIntegerToFloat=a,this.pos=0,this.view=new DataView(new ArrayBuffer(this.initialBufferSize)),this.bytes=new Uint8Array(this.view.buffer)}return e.prototype.reinitializeState=function(){this.pos=0},e.prototype.encodeSharedRef=function(e){return this.reinitializeState(),this.doEncode(e,1),this.bytes.subarray(0,this.pos)},e.prototype.encode=function(e){return this.reinitializeState(),this.doEncode(e,1),this.bytes.slice(0,this.pos)},e.prototype.doEncode=function(e,t){if(t>this.maxDepth)throw new Error("Too deep objects in depth ".concat(t));null==e?this.encodeNil():"boolean"==typeof e?this.encodeBoolean(e):"number"==typeof e?this.encodeNumber(e):"string"==typeof e?this.encodeString(e):this.encodeObject(e,t)},e.prototype.ensureBufferSizeToWrite=function(e){var t=this.pos+e;this.view.byteLength<t&&this.resizeBuffer(2*t)},e.prototype.resizeBuffer=function(e){var t=new ArrayBuffer(e),n=new Uint8Array(t),r=new DataView(t);n.set(this.bytes),this.view=r,this.bytes=n},e.prototype.encodeNil=function(){this.writeU8(192)},e.prototype.encodeBoolean=function(e){!1===e?this.writeU8(194):this.writeU8(195)},e.prototype.encodeNumber=function(e){Number.isSafeInteger(e)&&!this.forceIntegerToFloat?e>=0?e<128?this.writeU8(e):e<256?(this.writeU8(204),this.writeU8(e)):e<65536?(this.writeU8(205),this.writeU16(e)):e<4294967296?(this.writeU8(206),this.writeU32(e)):(this.writeU8(207),this.writeU64(e)):e>=-32?this.writeU8(224|e+32):e>=-128?(this.writeU8(208),this.writeI8(e)):e>=-32768?(this.writeU8(209),this.writeI16(e)):e>=-2147483648?(this.writeU8(210),this.writeI32(e)):(this.writeU8(211),this.writeI64(e)):this.forceFloat32?(this.writeU8(202),this.writeF32(e)):(this.writeU8(203),this.writeF64(e))},e.prototype.writeStringHeader=function(e){if(e<32)this.writeU8(160+e);else if(e<256)this.writeU8(217),this.writeU8(e);else if(e<65536)this.writeU8(218),this.writeU16(e);else{if(!(e<4294967296))throw new Error("Too long string: ".concat(e," bytes in UTF-8"));this.writeU8(219),this.writeU32(e)}},e.prototype.encodeString=function(e){if(e.length>p){var t=f(e);this.ensureBufferSizeToWrite(5+t),this.writeStringHeader(t),d(e,this.bytes,this.pos),this.pos+=t}else t=f(e),this.ensureBufferSizeToWrite(5+t),this.writeStringHeader(t),function(e,t,n){for(var r=e.length,i=n,o=0;o<r;){var s=e.charCodeAt(o++);if(0!=(4294967168&s)){if(0==(4294965248&s))t[i++]=s>>6&31|192;else{if(s>=55296&&s<=56319&&o<r){var a=e.charCodeAt(o);56320==(64512&a)&&(++o,s=((1023&s)<<10)+(1023&a)+65536)}0==(4294901760&s)?(t[i++]=s>>12&15|224,t[i++]=s>>6&63|128):(t[i++]=s>>18&7|240,t[i++]=s>>12&63|128,t[i++]=s>>6&63|128)}t[i++]=63&s|128}else t[i++]=s}}(e,this.bytes,this.pos),this.pos+=t},e.prototype.encodeObject=function(e,t){var n=this.extensionCodec.tryToEncode(e,this.context);if(null!=n)this.encodeExtension(n);else if(Array.isArray(e))this.encodeArray(e,t);else if(ArrayBuffer.isView(e))this.encodeBinary(e);else{if("object"!=typeof e)throw new Error("Unrecognized object: ".concat(Object.prototype.toString.apply(e)));this.encodeMap(e,t)}},e.prototype.encodeBinary=function(e){var t=e.byteLength;if(t<256)this.writeU8(196),this.writeU8(t);else if(t<65536)this.writeU8(197),this.writeU16(t);else{if(!(t<4294967296))throw new Error("Too large binary: ".concat(t));this.writeU8(198),this.writeU32(t)}var n=k(e);this.writeU8a(n)},e.prototype.encodeArray=function(e,t){var n,r,i=e.length;if(i<16)this.writeU8(144+i);else if(i<65536)this.writeU8(220),this.writeU16(i);else{if(!(i<4294967296))throw new Error("Too large array: ".concat(i));this.writeU8(221),this.writeU32(i)}try{for(var o=M(e),s=o.next();!s.done;s=o.next()){var a=s.value;this.doEncode(a,t+1)}}catch(e){n={error:e}}finally{try{s&&!s.done&&(r=o.return)&&r.call(o)}finally{if(n)throw n.error}}},e.prototype.countWithoutUndefined=function(e,t){var n,r,i=0;try{for(var o=M(t),s=o.next();!s.done;s=o.next())void 0!==e[s.value]&&i++}catch(e){n={error:e}}finally{try{s&&!s.done&&(r=o.return)&&r.call(o)}finally{if(n)throw n.error}}return i},e.prototype.encodeMap=function(e,t){var n,r,i=Object.keys(e);this.sortKeys&&i.sort();var o=this.ignoreUndefined?this.countWithoutUndefined(e,i):i.length;if(o<16)this.writeU8(128+o);else if(o<65536)this.writeU8(222),this.writeU16(o);else{if(!(o<4294967296))throw new Error("Too large map object: ".concat(o));this.writeU8(223),this.writeU32(o)}try{for(var s=M(i),a=s.next();!a.done;a=s.next()){var c=a.value,h=e[c];this.ignoreUndefined&&void 0===h||(this.encodeString(c),this.doEncode(h,t+1))}}catch(e){n={error:e}}finally{try{a&&!a.done&&(r=s.return)&&r.call(s)}finally{if(n)throw n.error}}},e.prototype.encodeExtension=function(e){var t=e.data.length;if(1===t)this.writeU8(212);else if(2===t)this.writeU8(213);else if(4===t)this.writeU8(214);else if(8===t)this.writeU8(215);else if(16===t)this.writeU8(216);else if(t<256)this.writeU8(199),this.writeU8(t);else if(t<65536)this.writeU8(200),this.writeU16(t);else{if(!(t<4294967296))throw new Error("Too large extension object: ".concat(t));this.writeU8(201),this.writeU32(t)}this.writeI8(e.type),this.writeU8a(e.data)},e.prototype.writeU8=function(e){this.ensureBufferSizeToWrite(1),this.view.setUint8(this.pos,e),this.pos++},e.prototype.writeU8a=function(e){var t=e.length;this.ensureBufferSizeToWrite(t),this.bytes.set(e,this.pos),this.pos+=t},e.prototype.writeI8=function(e){this.ensureBufferSizeToWrite(1),this.view.setInt8(this.pos,e),this.pos++},e.prototype.writeU16=function(e){this.ensureBufferSizeToWrite(2),this.view.setUint16(this.pos,e),this.pos+=2},e.prototype.writeI16=function(e){this.ensureBufferSizeToWrite(2),this.view.setInt16(this.pos,e),this.pos+=2},e.prototype.writeU32=function(e){this.ensureBufferSizeToWrite(4),this.view.setUint32(this.pos,e),this.pos+=4},e.prototype.writeI32=function(e){this.ensureBufferSizeToWrite(4),this.view.setInt32(this.pos,e),this.pos+=4},e.prototype.writeF32=function(e){this.ensureBufferSizeToWrite(4),this.view.setFloat32(this.pos,e),this.pos+=4},e.prototype.writeF64=function(e){this.ensureBufferSizeToWrite(8),this.view.setFloat64(this.pos,e),this.pos+=8},e.prototype.writeU64=function(e){this.ensureBufferSizeToWrite(8),function(e,t,n){var r=n/4294967296,i=n;e.setUint32(t,r),e.setUint32(t+4,i)}(this.view,this.pos,e),this.pos+=8},e.prototype.writeI64=function(e){this.ensureBufferSizeToWrite(8),r(this.view,this.pos,e),this.pos+=8},e}(),D={};function C(e,t){return void 0===t&&(t=D),new z(t.extensionCodec,t.context,t.maxDepth,t.initialBufferSize,t.sortKeys,t.forceFloat32,t.ignoreUndefined,t.forceIntegerToFloat).encodeSharedRef(e)}function P(e){return"".concat(e<0?"-":"","0x").concat(Math.abs(e).toString(16).padStart(2,"0"))}var O=function(){function e(e,t){void 0===e&&(e=16),void 0===t&&(t=16),this.maxKeyLength=e,this.maxLengthPerKey=t,this.hit=0,this.miss=0,this.caches=[];for(var n=0;n<this.maxKeyLength;n++)this.caches.push([])}return e.prototype.canBeCached=function(e){return e>0&&e<=this.maxKeyLength},e.prototype.find=function(e,t,n){var r,i,o=this.caches[n-1];try{e:for(var s=function(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],r=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}(o),a=s.next();!a.done;a=s.next()){for(var c=a.value,h=c.bytes,u=0;u<n;u++)if(h[u]!==e[t+u])continue e;return c.str}}catch(e){r={error:e}}finally{try{a&&!a.done&&(i=s.return)&&i.call(s)}finally{if(r)throw r.error}}return null},e.prototype.store=function(e,t){var n=this.caches[e.length-1],r={bytes:e,str:t};n.length>=this.maxLengthPerKey?n[Math.random()*n.length|0]=r:n.push(r)},e.prototype.decode=function(e,t,n){var r=this.find(e,t,n);if(null!=r)return this.hit++,r;this.miss++;var i=y(e,t,n),o=Uint8Array.prototype.slice.call(e,t,t+n);return this.store(o,i),i},e}(),_=function(e,t){var n,r,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;s;)try{if(n=1,r&&(i=2&o[0]?r.return:o[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,o[1])).done)return i;switch(r=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,r=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!((i=(i=s.trys).length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){s.label=o[1];break}if(6===o[0]&&s.label<i[1]){s.label=i[1],i=o;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(o);break}i[2]&&s.ops.pop(),s.trys.pop();continue}o=t.call(e,s)}catch(e){o=[6,e],r=0}finally{n=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,a])}}},j=function(e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var t,n=e[Symbol.asyncIterator];return n?n.call(e):(e="function"==typeof __values?__values(e):e[Symbol.iterator](),t={},r("next"),r("throw"),r("return"),t[Symbol.asyncIterator]=function(){return this},t);function r(n){t[n]=e[n]&&function(t){return new Promise((function(r,i){!function(e,t,n,r){Promise.resolve(r).then((function(t){e({value:t,done:n})}),t)}(r,i,(t=e[n](t)).done,t.value)}))}}},F=function(e){return this instanceof F?(this.v=e,this):new F(e)},W=function(e,t,n){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var r,i=n.apply(e,t||[]),o=[];return r={},s("next"),s("throw"),s("return"),r[Symbol.asyncIterator]=function(){return this},r;function s(e){i[e]&&(r[e]=function(t){return new Promise((function(n,r){o.push([e,t,n,r])>1||a(e,t)}))})}function a(e,t){try{(n=i[e](t)).value instanceof F?Promise.resolve(n.value.v).then(c,h):u(o[0][2],n)}catch(e){u(o[0][3],e)}var n}function c(e){a("next",e)}function h(e){a("throw",e)}function u(e,t){e(t),o.shift(),o.length&&a(o[0][0],o[0][1])}},R=new DataView(new ArrayBuffer(0)),V=new Uint8Array(R.buffer),K=function(){try{R.getInt8(0)}catch(e){return e.constructor}throw new Error("never reached")}(),N=new K("Insufficient data"),H=new O,G=function(){function e(e,t,r,i,o,s,a,c){void 0===e&&(e=A.defaultCodec),void 0===t&&(t=void 0),void 0===r&&(r=n),void 0===i&&(i=n),void 0===o&&(o=n),void 0===s&&(s=n),void 0===a&&(a=n),void 0===c&&(c=H),this.extensionCodec=e,this.context=t,this.maxStrLength=r,this.maxBinLength=i,this.maxArrayLength=o,this.maxMapLength=s,this.maxExtLength=a,this.keyDecoder=c,this.totalPos=0,this.pos=0,this.view=R,this.bytes=V,this.headByte=-1,this.stack=[]}return e.prototype.reinitializeState=function(){this.totalPos=0,this.headByte=-1,this.stack.length=0},e.prototype.setBuffer=function(e){this.bytes=k(e),this.view=function(e){if(e instanceof ArrayBuffer)return new DataView(e);var t=k(e);return new DataView(t.buffer,t.byteOffset,t.byteLength)}(this.bytes),this.pos=0},e.prototype.appendBuffer=function(e){if(-1!==this.headByte||this.hasRemaining(1)){var t=this.bytes.subarray(this.pos),n=k(e),r=new Uint8Array(t.length+n.length);r.set(t),r.set(n,t.length),this.setBuffer(r)}else this.setBuffer(e)},e.prototype.hasRemaining=function(e){return this.view.byteLength-this.pos>=e},e.prototype.createExtraByteError=function(e){var t=this.view,n=this.pos;return new RangeError("Extra ".concat(t.byteLength-n," of ").concat(t.byteLength," byte(s) found at buffer[").concat(e,"]"))},e.prototype.decode=function(e){this.reinitializeState(),this.setBuffer(e);var t=this.doDecodeSync();if(this.hasRemaining(1))throw this.createExtraByteError(this.pos);return t},e.prototype.decodeMulti=function(e){return _(this,(function(t){switch(t.label){case 0:this.reinitializeState(),this.setBuffer(e),t.label=1;case 1:return this.hasRemaining(1)?[4,this.doDecodeSync()]:[3,3];case 2:return t.sent(),[3,1];case 3:return[2]}}))},e.prototype.decodeAsync=function(e){var t,n,r,i,o,s,a,c;return o=this,s=void 0,c=function(){var o,s,a,c,h,u,f,l;return _(this,(function(p){switch(p.label){case 0:o=!1,p.label=1;case 1:p.trys.push([1,6,7,12]),t=j(e),p.label=2;case 2:return[4,t.next()];case 3:if((n=p.sent()).done)return[3,5];if(a=n.value,o)throw this.createExtraByteError(this.totalPos);this.appendBuffer(a);try{s=this.doDecodeSync(),o=!0}catch(e){if(!(e instanceof K))throw e}this.totalPos+=this.pos,p.label=4;case 4:return[3,2];case 5:return[3,12];case 6:return c=p.sent(),r={error:c},[3,12];case 7:return p.trys.push([7,,10,11]),n&&!n.done&&(i=t.return)?[4,i.call(t)]:[3,9];case 8:p.sent(),p.label=9;case 9:return[3,11];case 10:if(r)throw r.error;return[7];case 11:return[7];case 12:if(o){if(this.hasRemaining(1))throw this.createExtraByteError(this.totalPos);return[2,s]}throw u=(h=this).headByte,f=h.pos,l=h.totalPos,new RangeError("Insufficient data in parsing ".concat(P(u)," at ").concat(l," (").concat(f," in the current buffer)"))}}))},new((a=void 0)||(a=Promise))((function(e,t){function n(e){try{i(c.next(e))}catch(e){t(e)}}function r(e){try{i(c.throw(e))}catch(e){t(e)}}function i(t){var i;t.done?e(t.value):(i=t.value,i instanceof a?i:new a((function(e){e(i)}))).then(n,r)}i((c=c.apply(o,s||[])).next())}))},e.prototype.decodeArrayStream=function(e){return this.decodeMultiAsync(e,!0)},e.prototype.decodeStream=function(e){return this.decodeMultiAsync(e,!1)},e.prototype.decodeMultiAsync=function(e,t){return W(this,arguments,(function(){var n,r,i,o,s,a,c,h,u;return _(this,(function(f){switch(f.label){case 0:n=t,r=-1,f.label=1;case 1:f.trys.push([1,13,14,19]),i=j(e),f.label=2;case 2:return[4,F(i.next())];case 3:if((o=f.sent()).done)return[3,12];if(s=o.value,t&&0===r)throw this.createExtraByteError(this.totalPos);this.appendBuffer(s),n&&(r=this.readArraySize(),n=!1,this.complete()),f.label=4;case 4:f.trys.push([4,9,,10]),f.label=5;case 5:return[4,F(this.doDecodeSync())];case 6:return[4,f.sent()];case 7:return f.sent(),0==--r?[3,8]:[3,5];case 8:return[3,10];case 9:if(!((a=f.sent())instanceof K))throw a;return[3,10];case 10:this.totalPos+=this.pos,f.label=11;case 11:return[3,2];case 12:return[3,19];case 13:return c=f.sent(),h={error:c},[3,19];case 14:return f.trys.push([14,,17,18]),o&&!o.done&&(u=i.return)?[4,F(u.call(i))]:[3,16];case 15:f.sent(),f.label=16;case 16:return[3,18];case 17:if(h)throw h.error;return[7];case 18:return[7];case 19:return[2]}}))}))},e.prototype.doDecodeSync=function(){e:for(;;){var e=this.readHeadByte(),t=void 0;if(e>=224)t=e-256;else if(e<192)if(e<128)t=e;else if(e<144){if(0!=(r=e-128)){this.pushMapState(r),this.complete();continue e}t={}}else if(e<160){if(0!=(r=e-144)){this.pushArrayState(r),this.complete();continue e}t=[]}else{var n=e-160;t=this.decodeUtf8String(n,0)}else if(192===e)t=null;else if(194===e)t=!1;else if(195===e)t=!0;else if(202===e)t=this.readF32();else if(203===e)t=this.readF64();else if(204===e)t=this.readU8();else if(205===e)t=this.readU16();else if(206===e)t=this.readU32();else if(207===e)t=this.readU64();else if(208===e)t=this.readI8();else if(209===e)t=this.readI16();else if(210===e)t=this.readI32();else if(211===e)t=this.readI64();else if(217===e)n=this.lookU8(),t=this.decodeUtf8String(n,1);else if(218===e)n=this.lookU16(),t=this.decodeUtf8String(n,2);else if(219===e)n=this.lookU32(),t=this.decodeUtf8String(n,4);else if(220===e){if(0!==(r=this.readU16())){this.pushArrayState(r),this.complete();continue e}t=[]}else if(221===e){if(0!==(r=this.readU32())){this.pushArrayState(r),this.complete();continue e}t=[]}else if(222===e){if(0!==(r=this.readU16())){this.pushMapState(r),this.complete();continue e}t={}}else if(223===e){if(0!==(r=this.readU32())){this.pushMapState(r),this.complete();continue e}t={}}else if(196===e){var r=this.lookU8();t=this.decodeBinary(r,1)}else if(197===e)r=this.lookU16(),t=this.decodeBinary(r,2);else if(198===e)r=this.lookU32(),t=this.decodeBinary(r,4);else if(212===e)t=this.decodeExtension(1,0);else if(213===e)t=this.decodeExtension(2,0);else if(214===e)t=this.decodeExtension(4,0);else if(215===e)t=this.decodeExtension(8,0);else if(216===e)t=this.decodeExtension(16,0);else if(199===e)r=this.lookU8(),t=this.decodeExtension(r,1);else if(200===e)r=this.lookU16(),t=this.decodeExtension(r,2);else{if(201!==e)throw new m("Unrecognized type byte: ".concat(P(e)));r=this.lookU32(),t=this.decodeExtension(r,4)}this.complete();for(var i=this.stack;i.length>0;){var o=i[i.length-1];if(0===o.type){if(o.array[o.position]=t,o.position++,o.position!==o.size)continue e;i.pop(),t=o.array}else{if(1===o.type){if(void 0,"string"!=(s=typeof t)&&"number"!==s)throw new m("The type of key must be string or number but "+typeof t);if("__proto__"===t)throw new m("The key __proto__ is not allowed");o.key=t,o.type=2;continue e}if(o.map[o.key]=t,o.readCount++,o.readCount!==o.size){o.key=null,o.type=1;continue e}i.pop(),t=o.map}}return t}var s},e.prototype.readHeadByte=function(){return-1===this.headByte&&(this.headByte=this.readU8()),this.headByte},e.prototype.complete=function(){this.headByte=-1},e.prototype.readArraySize=function(){var e=this.readHeadByte();switch(e){case 220:return this.readU16();case 221:return this.readU32();default:if(e<160)return e-144;throw new m("Unrecognized array type byte: ".concat(P(e)))}},e.prototype.pushMapState=function(e){if(e>this.maxMapLength)throw new m("Max length exceeded: map length (".concat(e,") > maxMapLengthLength (").concat(this.maxMapLength,")"));this.stack.push({type:1,size:e,key:null,readCount:0,map:{}})},e.prototype.pushArrayState=function(e){if(e>this.maxArrayLength)throw new m("Max length exceeded: array length (".concat(e,") > maxArrayLength (").concat(this.maxArrayLength,")"));this.stack.push({type:0,size:e,array:new Array(e),position:0})},e.prototype.decodeUtf8String=function(e,t){var n;if(e>this.maxStrLength)throw new m("Max length exceeded: UTF-8 byte length (".concat(e,") > maxStrLength (").concat(this.maxStrLength,")"));if(this.bytes.byteLength<this.pos+t+e)throw N;var r,i=this.pos+t;return r=this.stateIsMapKey()&&(null===(n=this.keyDecoder)||void 0===n?void 0:n.canBeCached(e))?this.keyDecoder.decode(this.bytes,i,e):e>g?function(e,t,n){var r=e.subarray(t,t+n);return v.decode(r)}(this.bytes,i,e):y(this.bytes,i,e),this.pos+=t+e,r},e.prototype.stateIsMapKey=function(){return this.stack.length>0&&1===this.stack[this.stack.length-1].type},e.prototype.decodeBinary=function(e,t){if(e>this.maxBinLength)throw new m("Max length exceeded: bin length (".concat(e,") > maxBinLength (").concat(this.maxBinLength,")"));if(!this.hasRemaining(e+t))throw N;var n=this.pos+t,r=this.bytes.subarray(n,n+e);return this.pos+=t+e,r},e.prototype.decodeExtension=function(e,t){if(e>this.maxExtLength)throw new m("Max length exceeded: ext length (".concat(e,") > maxExtLength (").concat(this.maxExtLength,")"));var n=this.view.getInt8(this.pos+t),r=this.decodeBinary(e,t+1);return this.extensionCodec.decode(r,n,this.context)},e.prototype.lookU8=function(){return this.view.getUint8(this.pos)},e.prototype.lookU16=function(){return this.view.getUint16(this.pos)},e.prototype.lookU32=function(){return this.view.getUint32(this.pos)},e.prototype.readU8=function(){var e=this.view.getUint8(this.pos);return this.pos++,e},e.prototype.readI8=function(){var e=this.view.getInt8(this.pos);return this.pos++,e},e.prototype.readU16=function(){var e=this.view.getUint16(this.pos);return this.pos+=2,e},e.prototype.readI16=function(){var e=this.view.getInt16(this.pos);return this.pos+=2,e},e.prototype.readU32=function(){var e=this.view.getUint32(this.pos);return this.pos+=4,e},e.prototype.readI32=function(){var e=this.view.getInt32(this.pos);return this.pos+=4,e},e.prototype.readU64=function(){var e,t,n=(e=this.view,t=this.pos,4294967296*e.getUint32(t)+e.getUint32(t+4));return this.pos+=8,n},e.prototype.readI64=function(){var e=i(this.view,this.pos);return this.pos+=8,e},e.prototype.readF32=function(){var e=this.view.getFloat32(this.pos);return this.pos+=4,e},e.prototype.readF64=function(){var e=this.view.getFloat64(this.pos);return this.pos+=8,e},e}(),X={};function q(e,t){return void 0===t&&(t=X),new G(t.extensionCodec,t.context,t.maxStrLength,t.maxBinLength,t.maxArrayLength,t.maxMapLength,t.maxExtLength).decode(e)}function J(e,t){return void 0===t&&(t=X),new G(t.extensionCodec,t.context,t.maxStrLength,t.maxBinLength,t.maxArrayLength,t.maxMapLength,t.maxExtLength).decodeMulti(e)}var Q=function(e,t){var n,r,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;s;)try{if(n=1,r&&(i=2&o[0]?r.return:o[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,o[1])).done)return i;switch(r=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,r=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!((i=(i=s.trys).length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){s.label=o[1];break}if(6===o[0]&&s.label<i[1]){s.label=i[1],i=o;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(o);break}i[2]&&s.ops.pop(),s.trys.pop();continue}o=t.call(e,s)}catch(e){o=[6,e],r=0}finally{n=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,a])}}},Y=function(e){return this instanceof Y?(this.v=e,this):new Y(e)},Z=function(e,t,n){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var r,i=n.apply(e,t||[]),o=[];return r={},s("next"),s("throw"),s("return"),r[Symbol.asyncIterator]=function(){return this},r;function s(e){i[e]&&(r[e]=function(t){return new Promise((function(n,r){o.push([e,t,n,r])>1||a(e,t)}))})}function a(e,t){try{(n=i[e](t)).value instanceof Y?Promise.resolve(n.value.v).then(c,h):u(o[0][2],n)}catch(e){u(o[0][3],e)}var n}function c(e){a("next",e)}function h(e){a("throw",e)}function u(e,t){e(t),o.shift(),o.length&&a(o[0][0],o[0][1])}};function $(e){if(null==e)throw new Error("Assertion Failure: value must not be null nor undefined")}function ee(e){return null!=e[Symbol.asyncIterator]?e:function(e){return Z(this,arguments,(function(){var t,n,r,i;return Q(this,(function(o){switch(o.label){case 0:t=e.getReader(),o.label=1;case 1:o.trys.push([1,,9,10]),o.label=2;case 2:return[4,Y(t.read())];case 3:return n=o.sent(),r=n.done,i=n.value,r?[4,Y(void 0)]:[3,5];case 4:return[2,o.sent()];case 5:return $(i),[4,Y(i)];case 6:return[4,o.sent()];case 7:return o.sent(),[3,2];case 8:return[3,10];case 9:return t.releaseLock(),[7];case 10:return[2]}}))}))}(e)}function te(e,t){return void 0===t&&(t=X),n=this,r=void 0,o=function(){var n;return function(e,t){var n,r,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(o){return function(a){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;s;)try{if(n=1,r&&(i=2&o[0]?r.return:o[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,o[1])).done)return i;switch(r=0,i&&(o=[2&o[0],i.value]),o[0]){case 0:case 1:i=o;break;case 4:return s.label++,{value:o[1],done:!1};case 5:s.label++,r=o[1],o=[0];continue;case 7:o=s.ops.pop(),s.trys.pop();continue;default:if(!((i=(i=s.trys).length>0&&i[i.length-1])||6!==o[0]&&2!==o[0])){s=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]<i[3])){s.label=o[1];break}if(6===o[0]&&s.label<i[1]){s.label=i[1],i=o;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(o);break}i[2]&&s.ops.pop(),s.trys.pop();continue}o=t.call(e,s)}catch(e){o=[6,e],r=0}finally{n=i=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,a])}}}(this,(function(r){return n=ee(e),[2,new G(t.extensionCodec,t.context,t.maxStrLength,t.maxBinLength,t.maxArrayLength,t.maxMapLength,t.maxExtLength).decodeAsync(n)]}))},new((i=void 0)||(i=Promise))((function(e,t){function s(e){try{c(o.next(e))}catch(e){t(e)}}function a(e){try{c(o.throw(e))}catch(e){t(e)}}function c(t){var n;t.done?e(t.value):(n=t.value,n instanceof i?n:new i((function(e){e(n)}))).then(s,a)}c((o=o.apply(n,r||[])).next())}));var n,r,i,o}function ne(e,t){void 0===t&&(t=X);var n=ee(e);return new G(t.extensionCodec,t.context,t.maxStrLength,t.maxBinLength,t.maxArrayLength,t.maxMapLength,t.maxExtLength).decodeArrayStream(n)}function re(e,t){void 0===t&&(t=X);var n=ee(e);return new G(t.extensionCodec,t.context,t.maxStrLength,t.maxBinLength,t.maxArrayLength,t.maxMapLength,t.maxExtLength).decodeStream(n)}function ie(e,t){return void 0===t&&(t=X),re(e,t)}return t}()})); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.MessagePack=e():t.MessagePack=e()}(this,(function(){return function(){"use strict";var t={d:function(e,n){for(var r in n)t.o(n,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:n[r]})},o:function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r:function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{DataViewIndexOutOfBoundsError:function(){return W},DecodeError:function(){return p},Decoder:function(){return K},EXT_TIMESTAMP:function(){return v},Encoder:function(){return k},ExtData:function(){return f},ExtensionCodec:function(){return E},decode:function(){return N},decodeArrayStream:function(){return $},decodeAsync:function(){return Z},decodeMulti:function(){return H},decodeMultiStream:function(){return tt},decodeStream:function(){return et},decodeTimestampExtension:function(){return B},decodeTimestampToTimeSpec:function(){return S},encode:function(){return L},encodeDateToTimeSpec:function(){return m},encodeTimeSpecToTimestamp:function(){return U},encodeTimestampExtension:function(){return x}});var n=function(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,i,o=n.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(r=o.next()).done;)s.push(r.value)}catch(t){i={error:t}}finally{try{r&&!r.done&&(n=o.return)&&n.call(o)}finally{if(i)throw i.error}}return s},r=function(t,e,n){if(n||2===arguments.length)for(var r,i=0,o=e.length;i<o;i++)!r&&i in e||(r||(r=Array.prototype.slice.call(e,0,i)),r[i]=e[i]);return t.concat(r||Array.prototype.slice.call(e))},i=new TextEncoder,o=50;var s=4096;function a(t,e,i){for(var o=e,a=o+i,c=[],u="";o<a;){var h=t[o++];if(0==(128&h))c.push(h);else if(192==(224&h)){var f=63&t[o++];c.push((31&h)<<6|f)}else if(224==(240&h)){f=63&t[o++];var l=63&t[o++];c.push((31&h)<<12|f<<6|l)}else if(240==(248&h)){var p=(7&h)<<18|(f=63&t[o++])<<12|(l=63&t[o++])<<6|63&t[o++];p>65535&&(p-=65536,c.push(p>>>10&1023|55296),p=56320|1023&p),c.push(p)}else c.push(h);c.length>=s&&(u+=String.fromCharCode.apply(String,r([],n(c),!1)),c.length=0)}return c.length>0&&(u+=String.fromCharCode.apply(String,r([],n(c),!1))),u}var c=new TextDecoder,u=200;var h,f=function(t,e){this.type=t,this.data=e},l=(h=function(t,e){return h=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},h(t,e)},function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}h(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}),p=function(t){function e(n){var r=t.call(this,n)||this,i=Object.create(e.prototype);return Object.setPrototypeOf(r,i),Object.defineProperty(r,"name",{configurable:!0,enumerable:!1,value:e.name}),r}return l(e,t),e}(Error),d=4294967295;function y(t,e,n){var r=Math.floor(n/4294967296),i=n;t.setUint32(e,r),t.setUint32(e+4,i)}function w(t,e){return 4294967296*t.getInt32(e)+t.getUint32(e+4)}var v=-1,g=4294967295,b=17179869183;function U(t){var e,n=t.sec,r=t.nsec;if(n>=0&&r>=0&&n<=b){if(0===r&&n<=g){var i=new Uint8Array(4);return(e=new DataView(i.buffer)).setUint32(0,n),i}var o=n/4294967296,s=4294967295&n;return i=new Uint8Array(8),(e=new DataView(i.buffer)).setUint32(0,r<<2|3&o),e.setUint32(4,s),i}return i=new Uint8Array(12),(e=new DataView(i.buffer)).setUint32(0,r),y(e,4,n),i}function m(t){var e=t.getTime(),n=Math.floor(e/1e3),r=1e6*(e-1e3*n),i=Math.floor(r/1e9);return{sec:n+i,nsec:r-1e9*i}}function x(t){return t instanceof Date?U(m(t)):null}function S(t){var e=new DataView(t.buffer,t.byteOffset,t.byteLength);switch(t.byteLength){case 4:return{sec:e.getUint32(0),nsec:0};case 8:var n=e.getUint32(0);return{sec:4294967296*(3&n)+e.getUint32(4),nsec:n>>>2};case 12:return{sec:w(e,4),nsec:e.getUint32(0)};default:throw new p("Unrecognized data size for timestamp (expected 4, 8, or 12): ".concat(t.length))}}function B(t){var e=S(t);return new Date(1e3*e.sec+e.nsec/1e6)}var I={type:v,encode:x,decode:B},E=function(){function t(){this.builtInEncoders=[],this.builtInDecoders=[],this.encoders=[],this.decoders=[],this.register(I)}return t.prototype.register=function(t){var e=t.type,n=t.encode,r=t.decode;if(e>=0)this.encoders[e]=n,this.decoders[e]=r;else{var i=1+e;this.builtInEncoders[i]=n,this.builtInDecoders[i]=r}},t.prototype.tryToEncode=function(t,e){for(var n=0;n<this.builtInEncoders.length;n++)if(null!=(r=this.builtInEncoders[n])&&null!=(i=r(t,e)))return new f(-1-n,i);for(n=0;n<this.encoders.length;n++){var r,i;if(null!=(r=this.encoders[n])&&null!=(i=r(t,e)))return new f(n,i)}return t instanceof f?t:null},t.prototype.decode=function(t,e,n){var r=e<0?this.builtInDecoders[-1-e]:this.decoders[e];return r?r(t,e,n):new f(e,t)},t.defaultCodec=new t,t}();function A(t){return t instanceof Uint8Array?t:ArrayBuffer.isView(t)?new Uint8Array(t.buffer,t.byteOffset,t.byteLength):t instanceof ArrayBuffer?new Uint8Array(t):Uint8Array.from(t)}var T=function(t){var e="function"==typeof Symbol&&Symbol.iterator,n=e&&t[e],r=0;if(n)return n.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")},k=function(){function t(t){var e,n,r,i,o,s,a,c;this.extensionCodec=null!==(e=null==t?void 0:t.extensionCodec)&&void 0!==e?e:E.defaultCodec,this.context=null==t?void 0:t.context,this.useBigInt64=null!==(n=null==t?void 0:t.useBigInt64)&&void 0!==n&&n,this.maxDepth=null!==(r=null==t?void 0:t.maxDepth)&&void 0!==r?r:100,this.initialBufferSize=null!==(i=null==t?void 0:t.initialBufferSize)&&void 0!==i?i:2048,this.sortKeys=null!==(o=null==t?void 0:t.sortKeys)&&void 0!==o&&o,this.forceFloat32=null!==(s=null==t?void 0:t.forceFloat32)&&void 0!==s&&s,this.ignoreUndefined=null!==(a=null==t?void 0:t.ignoreUndefined)&&void 0!==a&&a,this.forceIntegerToFloat=null!==(c=null==t?void 0:t.forceIntegerToFloat)&&void 0!==c&&c,this.pos=0,this.view=new DataView(new ArrayBuffer(this.initialBufferSize)),this.bytes=new Uint8Array(this.view.buffer)}return t.prototype.reinitializeState=function(){this.pos=0},t.prototype.encodeSharedRef=function(t){return this.reinitializeState(),this.doEncode(t,1),this.bytes.subarray(0,this.pos)},t.prototype.encode=function(t){return this.reinitializeState(),this.doEncode(t,1),this.bytes.slice(0,this.pos)},t.prototype.doEncode=function(t,e){if(e>this.maxDepth)throw new Error("Too deep objects in depth ".concat(e));null==t?this.encodeNil():"boolean"==typeof t?this.encodeBoolean(t):"number"==typeof t?this.forceIntegerToFloat?this.encodeNumberAsFloat(t):this.encodeNumber(t):"string"==typeof t?this.encodeString(t):this.useBigInt64&&"bigint"==typeof t?this.encodeBigInt64(t):this.encodeObject(t,e)},t.prototype.ensureBufferSizeToWrite=function(t){var e=this.pos+t;this.view.byteLength<e&&this.resizeBuffer(2*e)},t.prototype.resizeBuffer=function(t){var e=new ArrayBuffer(t),n=new Uint8Array(e),r=new DataView(e);n.set(this.bytes),this.view=r,this.bytes=n},t.prototype.encodeNil=function(){this.writeU8(192)},t.prototype.encodeBoolean=function(t){!1===t?this.writeU8(194):this.writeU8(195)},t.prototype.encodeNumber=function(t){!this.forceIntegerToFloat&&Number.isSafeInteger(t)?t>=0?t<128?this.writeU8(t):t<256?(this.writeU8(204),this.writeU8(t)):t<65536?(this.writeU8(205),this.writeU16(t)):t<4294967296?(this.writeU8(206),this.writeU32(t)):this.useBigInt64?this.encodeNumberAsFloat(t):(this.writeU8(207),this.writeU64(t)):t>=-32?this.writeU8(224|t+32):t>=-128?(this.writeU8(208),this.writeI8(t)):t>=-32768?(this.writeU8(209),this.writeI16(t)):t>=-2147483648?(this.writeU8(210),this.writeI32(t)):this.useBigInt64?this.encodeNumberAsFloat(t):(this.writeU8(211),this.writeI64(t)):this.encodeNumberAsFloat(t)},t.prototype.encodeNumberAsFloat=function(t){this.forceFloat32?(this.writeU8(202),this.writeF32(t)):(this.writeU8(203),this.writeF64(t))},t.prototype.encodeBigInt64=function(t){t>=BigInt(0)?(this.writeU8(207),this.writeBigUint64(t)):(this.writeU8(211),this.writeBigInt64(t))},t.prototype.writeStringHeader=function(t){if(t<32)this.writeU8(160+t);else if(t<256)this.writeU8(217),this.writeU8(t);else if(t<65536)this.writeU8(218),this.writeU16(t);else{if(!(t<4294967296))throw new Error("Too long string: ".concat(t," bytes in UTF-8"));this.writeU8(219),this.writeU32(t)}},t.prototype.encodeString=function(t){var e,n,r,s=function(t){for(var e=t.length,n=0,r=0;r<e;){var i=t.charCodeAt(r++);if(0!=(4294967168&i))if(0==(4294965248&i))n+=2;else{if(i>=55296&&i<=56319&&r<e){var o=t.charCodeAt(r);56320==(64512&o)&&(++r,i=((1023&i)<<10)+(1023&o)+65536)}n+=0==(4294901760&i)?3:4}else n++}return n}(t);this.ensureBufferSizeToWrite(5+s),this.writeStringHeader(s),e=t,n=this.bytes,r=this.pos,e.length>o?function(t,e,n){i.encodeInto(t,e.subarray(n))}(e,n,r):function(t,e,n){for(var r=t.length,i=n,o=0;o<r;){var s=t.charCodeAt(o++);if(0!=(4294967168&s)){if(0==(4294965248&s))e[i++]=s>>6&31|192;else{if(s>=55296&&s<=56319&&o<r){var a=t.charCodeAt(o);56320==(64512&a)&&(++o,s=((1023&s)<<10)+(1023&a)+65536)}0==(4294901760&s)?(e[i++]=s>>12&15|224,e[i++]=s>>6&63|128):(e[i++]=s>>18&7|240,e[i++]=s>>12&63|128,e[i++]=s>>6&63|128)}e[i++]=63&s|128}else e[i++]=s}}(e,n,r),this.pos+=s},t.prototype.encodeObject=function(t,e){var n=this.extensionCodec.tryToEncode(t,this.context);if(null!=n)this.encodeExtension(n);else if(Array.isArray(t))this.encodeArray(t,e);else if(ArrayBuffer.isView(t))this.encodeBinary(t);else{if("object"!=typeof t)throw new Error("Unrecognized object: ".concat(Object.prototype.toString.apply(t)));this.encodeMap(t,e)}},t.prototype.encodeBinary=function(t){var e=t.byteLength;if(e<256)this.writeU8(196),this.writeU8(e);else if(e<65536)this.writeU8(197),this.writeU16(e);else{if(!(e<4294967296))throw new Error("Too large binary: ".concat(e));this.writeU8(198),this.writeU32(e)}var n=A(t);this.writeU8a(n)},t.prototype.encodeArray=function(t,e){var n,r,i=t.length;if(i<16)this.writeU8(144+i);else if(i<65536)this.writeU8(220),this.writeU16(i);else{if(!(i<4294967296))throw new Error("Too large array: ".concat(i));this.writeU8(221),this.writeU32(i)}try{for(var o=T(t),s=o.next();!s.done;s=o.next()){var a=s.value;this.doEncode(a,e+1)}}catch(t){n={error:t}}finally{try{s&&!s.done&&(r=o.return)&&r.call(o)}finally{if(n)throw n.error}}},t.prototype.countWithoutUndefined=function(t,e){var n,r,i=0;try{for(var o=T(e),s=o.next();!s.done;s=o.next())void 0!==t[s.value]&&i++}catch(t){n={error:t}}finally{try{s&&!s.done&&(r=o.return)&&r.call(o)}finally{if(n)throw n.error}}return i},t.prototype.encodeMap=function(t,e){var n,r,i=Object.keys(t);this.sortKeys&&i.sort();var o=this.ignoreUndefined?this.countWithoutUndefined(t,i):i.length;if(o<16)this.writeU8(128+o);else if(o<65536)this.writeU8(222),this.writeU16(o);else{if(!(o<4294967296))throw new Error("Too large map object: ".concat(o));this.writeU8(223),this.writeU32(o)}try{for(var s=T(i),a=s.next();!a.done;a=s.next()){var c=a.value,u=t[c];this.ignoreUndefined&&void 0===u||(this.encodeString(c),this.doEncode(u,e+1))}}catch(t){n={error:t}}finally{try{a&&!a.done&&(r=s.return)&&r.call(s)}finally{if(n)throw n.error}}},t.prototype.encodeExtension=function(t){var e=t.data.length;if(1===e)this.writeU8(212);else if(2===e)this.writeU8(213);else if(4===e)this.writeU8(214);else if(8===e)this.writeU8(215);else if(16===e)this.writeU8(216);else if(e<256)this.writeU8(199),this.writeU8(e);else if(e<65536)this.writeU8(200),this.writeU16(e);else{if(!(e<4294967296))throw new Error("Too large extension object: ".concat(e));this.writeU8(201),this.writeU32(e)}this.writeI8(t.type),this.writeU8a(t.data)},t.prototype.writeU8=function(t){this.ensureBufferSizeToWrite(1),this.view.setUint8(this.pos,t),this.pos++},t.prototype.writeU8a=function(t){var e=t.length;this.ensureBufferSizeToWrite(e),this.bytes.set(t,this.pos),this.pos+=e},t.prototype.writeI8=function(t){this.ensureBufferSizeToWrite(1),this.view.setInt8(this.pos,t),this.pos++},t.prototype.writeU16=function(t){this.ensureBufferSizeToWrite(2),this.view.setUint16(this.pos,t),this.pos+=2},t.prototype.writeI16=function(t){this.ensureBufferSizeToWrite(2),this.view.setInt16(this.pos,t),this.pos+=2},t.prototype.writeU32=function(t){this.ensureBufferSizeToWrite(4),this.view.setUint32(this.pos,t),this.pos+=4},t.prototype.writeI32=function(t){this.ensureBufferSizeToWrite(4),this.view.setInt32(this.pos,t),this.pos+=4},t.prototype.writeF32=function(t){this.ensureBufferSizeToWrite(4),this.view.setFloat32(this.pos,t),this.pos+=4},t.prototype.writeF64=function(t){this.ensureBufferSizeToWrite(8),this.view.setFloat64(this.pos,t),this.pos+=8},t.prototype.writeU64=function(t){this.ensureBufferSizeToWrite(8),function(t,e,n){var r=n/4294967296,i=n;t.setUint32(e,r),t.setUint32(e+4,i)}(this.view,this.pos,t),this.pos+=8},t.prototype.writeI64=function(t){this.ensureBufferSizeToWrite(8),y(this.view,this.pos,t),this.pos+=8},t.prototype.writeBigUint64=function(t){this.ensureBufferSizeToWrite(8),this.view.setBigUint64(this.pos,t),this.pos+=8},t.prototype.writeBigInt64=function(t){this.ensureBufferSizeToWrite(8),this.view.setBigInt64(this.pos,t),this.pos+=8},t}();function L(t,e){return new k(e).encodeSharedRef(t)}function z(t){return"".concat(t<0?"-":"","0x").concat(Math.abs(t).toString(16).padStart(2,"0"))}var M=function(){function t(t,e){void 0===t&&(t=16),void 0===e&&(e=16),this.maxKeyLength=t,this.maxLengthPerKey=e,this.hit=0,this.miss=0,this.caches=[];for(var n=0;n<this.maxKeyLength;n++)this.caches.push([])}return t.prototype.canBeCached=function(t){return t>0&&t<=this.maxKeyLength},t.prototype.find=function(t,e,n){var r,i,o=this.caches[n-1];try{t:for(var s=function(t){var e="function"==typeof Symbol&&Symbol.iterator,n=e&&t[e],r=0;if(n)return n.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}(o),a=s.next();!a.done;a=s.next()){for(var c=a.value,u=c.bytes,h=0;h<n;h++)if(u[h]!==t[e+h])continue t;return c.str}}catch(t){r={error:t}}finally{try{a&&!a.done&&(i=s.return)&&i.call(s)}finally{if(r)throw r.error}}return null},t.prototype.store=function(t,e){var n=this.caches[t.length-1],r={bytes:t,str:e};n.length>=this.maxLengthPerKey?n[Math.random()*n.length|0]=r:n.push(r)},t.prototype.decode=function(t,e,n){var r=this.find(t,e,n);if(null!=r)return this.hit++,r;this.miss++;var i=a(t,e,n),o=Uint8Array.prototype.slice.call(t,e,e+n);return this.store(o,i),i},t}(),D=function(t,e){var n,r,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(a){return function(c){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;o&&(o=0,a[0]&&(s=0)),s;)try{if(n=1,r&&(i=2&a[0]?r.return:a[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,a[1])).done)return i;switch(r=0,i&&(a=[2&a[0],i.value]),a[0]){case 0:case 1:i=a;break;case 4:return s.label++,{value:a[1],done:!1};case 5:s.label++,r=a[1],a=[0];continue;case 7:a=s.ops.pop(),s.trys.pop();continue;default:if(!((i=(i=s.trys).length>0&&i[i.length-1])||6!==a[0]&&2!==a[0])){s=0;continue}if(3===a[0]&&(!i||a[1]>i[0]&&a[1]<i[3])){s.label=a[1];break}if(6===a[0]&&s.label<i[1]){s.label=i[1],i=a;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(a);break}i[2]&&s.ops.pop(),s.trys.pop();continue}a=e.call(t,s)}catch(t){a=[6,t],r=0}finally{n=i=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}},P=function(t){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var e,n=t[Symbol.asyncIterator];return n?n.call(t):(t="function"==typeof __values?__values(t):t[Symbol.iterator](),e={},r("next"),r("throw"),r("return"),e[Symbol.asyncIterator]=function(){return this},e);function r(n){e[n]=t[n]&&function(e){return new Promise((function(r,i){!function(t,e,n,r){Promise.resolve(r).then((function(e){t({value:e,done:n})}),e)}(r,i,(e=t[n](e)).done,e.value)}))}}},F=function(t){return this instanceof F?(this.v=t,this):new F(t)},O="array",_="map_key",j=new DataView(new ArrayBuffer(0)),C=new Uint8Array(j.buffer);try{j.getInt8(0)}catch(t){if(!(t instanceof RangeError))throw new Error("This module is not supported in the current JavaScript engine because DataView does not throw RangeError on out-of-bounds access")}var W=RangeError,R=new W("Insufficient data"),V=new M,K=function(){function t(t){var e,n,r,i,o,s,a;this.totalPos=0,this.pos=0,this.view=j,this.bytes=C,this.headByte=-1,this.stack=[],this.extensionCodec=null!==(e=null==t?void 0:t.extensionCodec)&&void 0!==e?e:E.defaultCodec,this.context=null==t?void 0:t.context,this.useBigInt64=null!==(n=null==t?void 0:t.useBigInt64)&&void 0!==n&&n,this.maxStrLength=null!==(r=null==t?void 0:t.maxStrLength)&&void 0!==r?r:d,this.maxBinLength=null!==(i=null==t?void 0:t.maxBinLength)&&void 0!==i?i:d,this.maxArrayLength=null!==(o=null==t?void 0:t.maxArrayLength)&&void 0!==o?o:d,this.maxMapLength=null!==(s=null==t?void 0:t.maxMapLength)&&void 0!==s?s:d,this.maxExtLength=null!==(a=null==t?void 0:t.maxExtLength)&&void 0!==a?a:d,this.keyDecoder=void 0!==(null==t?void 0:t.keyDecoder)?t.keyDecoder:V}return t.prototype.reinitializeState=function(){this.totalPos=0,this.headByte=-1,this.stack.length=0},t.prototype.setBuffer=function(t){this.bytes=A(t),this.view=function(t){if(t instanceof ArrayBuffer)return new DataView(t);var e=A(t);return new DataView(e.buffer,e.byteOffset,e.byteLength)}(this.bytes),this.pos=0},t.prototype.appendBuffer=function(t){if(-1!==this.headByte||this.hasRemaining(1)){var e=this.bytes.subarray(this.pos),n=A(t),r=new Uint8Array(e.length+n.length);r.set(e),r.set(n,e.length),this.setBuffer(r)}else this.setBuffer(t)},t.prototype.hasRemaining=function(t){return this.view.byteLength-this.pos>=t},t.prototype.createExtraByteError=function(t){var e=this.view,n=this.pos;return new RangeError("Extra ".concat(e.byteLength-n," of ").concat(e.byteLength," byte(s) found at buffer[").concat(t,"]"))},t.prototype.decode=function(t){this.reinitializeState(),this.setBuffer(t);var e=this.doDecodeSync();if(this.hasRemaining(1))throw this.createExtraByteError(this.pos);return e},t.prototype.decodeMulti=function(t){return D(this,(function(e){switch(e.label){case 0:this.reinitializeState(),this.setBuffer(t),e.label=1;case 1:return this.hasRemaining(1)?[4,this.doDecodeSync()]:[3,3];case 2:return e.sent(),[3,1];case 3:return[2]}}))},t.prototype.decodeAsync=function(t){var e,n,r,i,o,s,a,c,u,h,f;return c=this,u=void 0,f=function(){var c,u,h,f,l,p,d,y;return D(this,(function(w){switch(w.label){case 0:c=!1,w.label=1;case 1:w.trys.push([1,6,7,12]),e=!0,n=P(t),w.label=2;case 2:return[4,n.next()];case 3:if(r=w.sent(),i=r.done)return[3,5];a=r.value,e=!1;try{if(h=a,c)throw this.createExtraByteError(this.totalPos);this.appendBuffer(h);try{u=this.doDecodeSync(),c=!0}catch(t){if(!(t instanceof W))throw t}this.totalPos+=this.pos}finally{e=!0}w.label=4;case 4:return[3,2];case 5:return[3,12];case 6:return f=w.sent(),o={error:f},[3,12];case 7:return w.trys.push([7,,10,11]),e||i||!(s=n.return)?[3,9]:[4,s.call(n)];case 8:w.sent(),w.label=9;case 9:return[3,11];case 10:if(o)throw o.error;return[7];case 11:return[7];case 12:if(c){if(this.hasRemaining(1))throw this.createExtraByteError(this.totalPos);return[2,u]}throw p=(l=this).headByte,d=l.pos,y=l.totalPos,new RangeError("Insufficient data in parsing ".concat(z(p)," at ").concat(y," (").concat(d," in the current buffer)"))}}))},new((h=void 0)||(h=Promise))((function(t,e){function n(t){try{i(f.next(t))}catch(t){e(t)}}function r(t){try{i(f.throw(t))}catch(t){e(t)}}function i(e){var i;e.done?t(e.value):(i=e.value,i instanceof h?i:new h((function(t){t(i)}))).then(n,r)}i((f=f.apply(c,u||[])).next())}))},t.prototype.decodeArrayStream=function(t){return this.decodeMultiAsync(t,!0)},t.prototype.decodeStream=function(t){return this.decodeMultiAsync(t,!1)},t.prototype.decodeMultiAsync=function(t,e){return function(t,e,n){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var r,i=n.apply(t,e||[]),o=[];return r={},s("next"),s("throw"),s("return"),r[Symbol.asyncIterator]=function(){return this},r;function s(t){i[t]&&(r[t]=function(e){return new Promise((function(n,r){o.push([t,e,n,r])>1||a(t,e)}))})}function a(t,e){try{(n=i[t](e)).value instanceof F?Promise.resolve(n.value.v).then(c,u):h(o[0][2],n)}catch(t){h(o[0][3],t)}var n}function c(t){a("next",t)}function u(t){a("throw",t)}function h(t,e){t(e),o.shift(),o.length&&a(o[0][0],o[0][1])}}(this,arguments,(function(){var n,r,i,o,s,a,c,u,h,f,l,p;return D(this,(function(d){switch(d.label){case 0:n=e,r=-1,d.label=1;case 1:d.trys.push([1,15,16,21]),i=!0,o=P(t),d.label=2;case 2:return[4,F(o.next())];case 3:if(s=d.sent(),h=s.done)return[3,14];p=s.value,i=!1,d.label=4;case 4:if(d.trys.push([4,,12,13]),a=p,e&&0===r)throw this.createExtraByteError(this.totalPos);this.appendBuffer(a),n&&(r=this.readArraySize(),n=!1,this.complete()),d.label=5;case 5:d.trys.push([5,10,,11]),d.label=6;case 6:return[4,F(this.doDecodeSync())];case 7:return[4,d.sent()];case 8:return d.sent(),0==--r?[3,9]:[3,6];case 9:return[3,11];case 10:if(!((c=d.sent())instanceof W))throw c;return[3,11];case 11:return this.totalPos+=this.pos,[3,13];case 12:return i=!0,[7];case 13:return[3,2];case 14:return[3,21];case 15:return u=d.sent(),f={error:u},[3,21];case 16:return d.trys.push([16,,19,20]),i||h||!(l=o.return)?[3,18]:[4,F(l.call(o))];case 17:d.sent(),d.label=18;case 18:return[3,20];case 19:if(f)throw f.error;return[7];case 20:return[7];case 21:return[2]}}))}))},t.prototype.doDecodeSync=function(){t:for(;;){var t=this.readHeadByte(),e=void 0;if(t>=224)e=t-256;else if(t<192)if(t<128)e=t;else if(t<144){if(0!=(r=t-128)){this.pushMapState(r),this.complete();continue t}e={}}else if(t<160){if(0!=(r=t-144)){this.pushArrayState(r),this.complete();continue t}e=[]}else{var n=t-160;e=this.decodeUtf8String(n,0)}else if(192===t)e=null;else if(194===t)e=!1;else if(195===t)e=!0;else if(202===t)e=this.readF32();else if(203===t)e=this.readF64();else if(204===t)e=this.readU8();else if(205===t)e=this.readU16();else if(206===t)e=this.readU32();else if(207===t)e=this.useBigInt64?this.readU64AsBigInt():this.readU64();else if(208===t)e=this.readI8();else if(209===t)e=this.readI16();else if(210===t)e=this.readI32();else if(211===t)e=this.useBigInt64?this.readI64AsBigInt():this.readI64();else if(217===t)n=this.lookU8(),e=this.decodeUtf8String(n,1);else if(218===t)n=this.lookU16(),e=this.decodeUtf8String(n,2);else if(219===t)n=this.lookU32(),e=this.decodeUtf8String(n,4);else if(220===t){if(0!==(r=this.readU16())){this.pushArrayState(r),this.complete();continue t}e=[]}else if(221===t){if(0!==(r=this.readU32())){this.pushArrayState(r),this.complete();continue t}e=[]}else if(222===t){if(0!==(r=this.readU16())){this.pushMapState(r),this.complete();continue t}e={}}else if(223===t){if(0!==(r=this.readU32())){this.pushMapState(r),this.complete();continue t}e={}}else if(196===t){var r=this.lookU8();e=this.decodeBinary(r,1)}else if(197===t)r=this.lookU16(),e=this.decodeBinary(r,2);else if(198===t)r=this.lookU32(),e=this.decodeBinary(r,4);else if(212===t)e=this.decodeExtension(1,0);else if(213===t)e=this.decodeExtension(2,0);else if(214===t)e=this.decodeExtension(4,0);else if(215===t)e=this.decodeExtension(8,0);else if(216===t)e=this.decodeExtension(16,0);else if(199===t)r=this.lookU8(),e=this.decodeExtension(r,1);else if(200===t)r=this.lookU16(),e=this.decodeExtension(r,2);else{if(201!==t)throw new p("Unrecognized type byte: ".concat(z(t)));r=this.lookU32(),e=this.decodeExtension(r,4)}this.complete();for(var i=this.stack;i.length>0;){var o=i[i.length-1];if(o.type===O){if(o.array[o.position]=e,o.position++,o.position!==o.size)continue t;i.pop(),e=o.array}else{if(o.type===_){if("string"!=typeof(s=e)&&"number"!=typeof s)throw new p("The type of key must be string or number but "+typeof e);if("__proto__"===e)throw new p("The key __proto__ is not allowed");o.key=e,o.type="map_value";continue t}if(o.map[o.key]=e,o.readCount++,o.readCount!==o.size){o.key=null,o.type=_;continue t}i.pop(),e=o.map}}return e}var s},t.prototype.readHeadByte=function(){return-1===this.headByte&&(this.headByte=this.readU8()),this.headByte},t.prototype.complete=function(){this.headByte=-1},t.prototype.readArraySize=function(){var t=this.readHeadByte();switch(t){case 220:return this.readU16();case 221:return this.readU32();default:if(t<160)return t-144;throw new p("Unrecognized array type byte: ".concat(z(t)))}},t.prototype.pushMapState=function(t){if(t>this.maxMapLength)throw new p("Max length exceeded: map length (".concat(t,") > maxMapLengthLength (").concat(this.maxMapLength,")"));this.stack.push({type:_,size:t,key:null,readCount:0,map:{}})},t.prototype.pushArrayState=function(t){if(t>this.maxArrayLength)throw new p("Max length exceeded: array length (".concat(t,") > maxArrayLength (").concat(this.maxArrayLength,")"));this.stack.push({type:O,size:t,array:new Array(t),position:0})},t.prototype.decodeUtf8String=function(t,e){var n;if(t>this.maxStrLength)throw new p("Max length exceeded: UTF-8 byte length (".concat(t,") > maxStrLength (").concat(this.maxStrLength,")"));if(this.bytes.byteLength<this.pos+e+t)throw R;var r,i=this.pos+e;return r=this.stateIsMapKey()&&(null===(n=this.keyDecoder)||void 0===n?void 0:n.canBeCached(t))?this.keyDecoder.decode(this.bytes,i,t):function(t,e,n){return n>u?function(t,e,n){var r=t.subarray(e,e+n);return c.decode(r)}(t,e,n):a(t,e,n)}(this.bytes,i,t),this.pos+=e+t,r},t.prototype.stateIsMapKey=function(){return this.stack.length>0&&this.stack[this.stack.length-1].type===_},t.prototype.decodeBinary=function(t,e){if(t>this.maxBinLength)throw new p("Max length exceeded: bin length (".concat(t,") > maxBinLength (").concat(this.maxBinLength,")"));if(!this.hasRemaining(t+e))throw R;var n=this.pos+e,r=this.bytes.subarray(n,n+t);return this.pos+=e+t,r},t.prototype.decodeExtension=function(t,e){if(t>this.maxExtLength)throw new p("Max length exceeded: ext length (".concat(t,") > maxExtLength (").concat(this.maxExtLength,")"));var n=this.view.getInt8(this.pos+e),r=this.decodeBinary(t,e+1);return this.extensionCodec.decode(r,n,this.context)},t.prototype.lookU8=function(){return this.view.getUint8(this.pos)},t.prototype.lookU16=function(){return this.view.getUint16(this.pos)},t.prototype.lookU32=function(){return this.view.getUint32(this.pos)},t.prototype.readU8=function(){var t=this.view.getUint8(this.pos);return this.pos++,t},t.prototype.readI8=function(){var t=this.view.getInt8(this.pos);return this.pos++,t},t.prototype.readU16=function(){var t=this.view.getUint16(this.pos);return this.pos+=2,t},t.prototype.readI16=function(){var t=this.view.getInt16(this.pos);return this.pos+=2,t},t.prototype.readU32=function(){var t=this.view.getUint32(this.pos);return this.pos+=4,t},t.prototype.readI32=function(){var t=this.view.getInt32(this.pos);return this.pos+=4,t},t.prototype.readU64=function(){var t,e,n=(t=this.view,e=this.pos,4294967296*t.getUint32(e)+t.getUint32(e+4));return this.pos+=8,n},t.prototype.readI64=function(){var t=w(this.view,this.pos);return this.pos+=8,t},t.prototype.readU64AsBigInt=function(){var t=this.view.getBigUint64(this.pos);return this.pos+=8,t},t.prototype.readI64AsBigInt=function(){var t=this.view.getBigInt64(this.pos);return this.pos+=8,t},t.prototype.readF32=function(){var t=this.view.getFloat32(this.pos);return this.pos+=4,t},t.prototype.readF64=function(){var t=this.view.getFloat64(this.pos);return this.pos+=8,t},t}();function N(t,e){return new K(e).decode(t)}function H(t,e){return new K(e).decodeMulti(t)}var G=function(t,e){var n,r,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(a){return function(c){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;o&&(o=0,a[0]&&(s=0)),s;)try{if(n=1,r&&(i=2&a[0]?r.return:a[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,a[1])).done)return i;switch(r=0,i&&(a=[2&a[0],i.value]),a[0]){case 0:case 1:i=a;break;case 4:return s.label++,{value:a[1],done:!1};case 5:s.label++,r=a[1],a=[0];continue;case 7:a=s.ops.pop(),s.trys.pop();continue;default:if(!((i=(i=s.trys).length>0&&i[i.length-1])||6!==a[0]&&2!==a[0])){s=0;continue}if(3===a[0]&&(!i||a[1]>i[0]&&a[1]<i[3])){s.label=a[1];break}if(6===a[0]&&s.label<i[1]){s.label=i[1],i=a;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(a);break}i[2]&&s.ops.pop(),s.trys.pop();continue}a=e.call(t,s)}catch(t){a=[6,t],r=0}finally{n=i=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}},J=function(t){return this instanceof J?(this.v=t,this):new J(t)},X=function(t,e,n){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var r,i=n.apply(t,e||[]),o=[];return r={},s("next"),s("throw"),s("return"),r[Symbol.asyncIterator]=function(){return this},r;function s(t){i[t]&&(r[t]=function(e){return new Promise((function(n,r){o.push([t,e,n,r])>1||a(t,e)}))})}function a(t,e){try{(n=i[t](e)).value instanceof J?Promise.resolve(n.value.v).then(c,u):h(o[0][2],n)}catch(t){h(o[0][3],t)}var n}function c(t){a("next",t)}function u(t){a("throw",t)}function h(t,e){t(e),o.shift(),o.length&&a(o[0][0],o[0][1])}};function q(t){return null!=t[Symbol.asyncIterator]?t:function(t){return X(this,arguments,(function(){var e,n,r,i;return G(this,(function(o){switch(o.label){case 0:e=t.getReader(),o.label=1;case 1:o.trys.push([1,,9,10]),o.label=2;case 2:return[4,J(e.read())];case 3:return n=o.sent(),r=n.done,i=n.value,r?[4,J(void 0)]:[3,5];case 4:return[2,o.sent()];case 5:return function(t){if(null==t)throw new Error("Assertion Failure: value must not be null nor undefined")}(i),[4,J(i)];case 6:return[4,o.sent()];case 7:return o.sent(),[3,2];case 8:return[3,10];case 9:return e.releaseLock(),[7];case 10:return[2]}}))}))}(t)}var Q=function(t,e,n,r){return new(n||(n=Promise))((function(i,o){function s(t){try{c(r.next(t))}catch(t){o(t)}}function a(t){try{c(r.throw(t))}catch(t){o(t)}}function c(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,a)}c((r=r.apply(t,e||[])).next())}))},Y=function(t,e){var n,r,i,o,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return o={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function a(a){return function(c){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;o&&(o=0,a[0]&&(s=0)),s;)try{if(n=1,r&&(i=2&a[0]?r.return:a[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,a[1])).done)return i;switch(r=0,i&&(a=[2&a[0],i.value]),a[0]){case 0:case 1:i=a;break;case 4:return s.label++,{value:a[1],done:!1};case 5:s.label++,r=a[1],a=[0];continue;case 7:a=s.ops.pop(),s.trys.pop();continue;default:if(!((i=(i=s.trys).length>0&&i[i.length-1])||6!==a[0]&&2!==a[0])){s=0;continue}if(3===a[0]&&(!i||a[1]>i[0]&&a[1]<i[3])){s.label=a[1];break}if(6===a[0]&&s.label<i[1]){s.label=i[1],i=a;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(a);break}i[2]&&s.ops.pop(),s.trys.pop();continue}a=e.call(t,s)}catch(t){a=[6,t],r=0}finally{n=i=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}};function Z(t,e){return Q(this,void 0,void 0,(function(){var n;return Y(this,(function(r){return n=q(t),[2,new K(e).decodeAsync(n)]}))}))}function $(t,e){var n=q(t);return new K(e).decodeArrayStream(n)}function tt(t,e){var n=q(t);return new K(e).decodeStream(n)}var et=void 0;return e}()})); | ||
//# sourceMappingURL=msgpack.min.js.map |
@@ -1,4 +0,4 @@ | ||
export declare type SplitTypes<T, U> = U extends T ? (Exclude<T, U> extends never ? T : Exclude<T, U>) : T; | ||
export declare type SplitUndefined<T> = SplitTypes<T, undefined>; | ||
export declare type ContextOf<ContextType> = ContextType extends undefined ? {} : { | ||
export type SplitTypes<T, U> = U extends T ? (Exclude<T, U> extends never ? T : Exclude<T, U>) : T; | ||
export type SplitUndefined<T> = SplitTypes<T, undefined>; | ||
export type ContextOf<ContextType> = ContextType extends undefined ? {} : { | ||
/** | ||
@@ -5,0 +5,0 @@ * Custom user-defined data, read/writable |
@@ -1,38 +0,12 @@ | ||
import type { ExtensionCodecType } from "./ExtensionCodec"; | ||
import type { ContextOf, SplitUndefined } from "./context"; | ||
export declare type DecodeOptions<ContextType = undefined> = Readonly<Partial<{ | ||
extensionCodec: ExtensionCodecType<ContextType>; | ||
/** | ||
* Maximum string length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxStrLength: number; | ||
/** | ||
* Maximum binary length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxBinLength: number; | ||
/** | ||
* Maximum array length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxArrayLength: number; | ||
/** | ||
* Maximum map length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxMapLength: number; | ||
/** | ||
* Maximum extension length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxExtLength: number; | ||
}>> & ContextOf<ContextType>; | ||
export declare const defaultDecodeOptions: DecodeOptions; | ||
import type { DecoderOptions } from "./Decoder"; | ||
import type { SplitUndefined } from "./context"; | ||
/** | ||
* @deprecated Use {@link DecoderOptions} instead. | ||
*/ | ||
export type DecodeOptions = never; | ||
/** | ||
* @deprecated No longer supported. | ||
*/ | ||
export declare const defaultDecodeOptions: never; | ||
/** | ||
* It decodes a single MessagePack object in a buffer. | ||
@@ -46,3 +20,3 @@ * | ||
*/ | ||
export declare function decode<ContextType = undefined>(buffer: ArrayLike<number> | BufferSource, options?: DecodeOptions<SplitUndefined<ContextType>>): unknown; | ||
export declare function decode<ContextType = undefined>(buffer: ArrayLike<number> | BufferSource, options?: DecoderOptions<SplitUndefined<ContextType>>): unknown; | ||
/** | ||
@@ -55,2 +29,2 @@ * It decodes multiple MessagePack objects in a buffer. | ||
*/ | ||
export declare function decodeMulti<ContextType = undefined>(buffer: ArrayLike<number> | BufferSource, options?: DecodeOptions<SplitUndefined<ContextType>>): Generator<unknown, void, unknown>; | ||
export declare function decodeMulti<ContextType = undefined>(buffer: ArrayLike<number> | BufferSource, options?: DecoderOptions<SplitUndefined<ContextType>>): Generator<unknown, void, unknown>; |
@@ -5,4 +5,7 @@ "use strict"; | ||
const Decoder_1 = require("./Decoder"); | ||
exports.defaultDecodeOptions = {}; | ||
/** | ||
* @deprecated No longer supported. | ||
*/ | ||
exports.defaultDecodeOptions = undefined; | ||
/** | ||
* It decodes a single MessagePack object in a buffer. | ||
@@ -16,4 +19,4 @@ * | ||
*/ | ||
function decode(buffer, options = exports.defaultDecodeOptions) { | ||
const decoder = new Decoder_1.Decoder(options.extensionCodec, options.context, options.maxStrLength, options.maxBinLength, options.maxArrayLength, options.maxMapLength, options.maxExtLength); | ||
function decode(buffer, options) { | ||
const decoder = new Decoder_1.Decoder(options); | ||
return decoder.decode(buffer); | ||
@@ -29,4 +32,4 @@ } | ||
*/ | ||
function decodeMulti(buffer, options = exports.defaultDecodeOptions) { | ||
const decoder = new Decoder_1.Decoder(options.extensionCodec, options.context, options.maxStrLength, options.maxBinLength, options.maxArrayLength, options.maxMapLength, options.maxExtLength); | ||
function decodeMulti(buffer, options) { | ||
const decoder = new Decoder_1.Decoder(options); | ||
return decoder.decodeMulti(buffer); | ||
@@ -33,0 +36,0 @@ } |
@@ -0,3 +1,3 @@ | ||
import type { DecoderOptions } from "./Decoder"; | ||
import type { ReadableStreamLike } from "./utils/stream"; | ||
import type { DecodeOptions } from "./decode"; | ||
import type { SplitUndefined } from "./context"; | ||
@@ -8,3 +8,3 @@ /** | ||
*/ | ||
export declare function decodeAsync<ContextType>(streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeOptions<SplitUndefined<ContextType>>): Promise<unknown>; | ||
export declare function decodeAsync<ContextType = undefined>(streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecoderOptions<SplitUndefined<ContextType>>): Promise<unknown>; | ||
/** | ||
@@ -14,3 +14,3 @@ * @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty. | ||
*/ | ||
export declare function decodeArrayStream<ContextType>(streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeOptions<SplitUndefined<ContextType>>): AsyncGenerator<unknown, void, unknown>; | ||
export declare function decodeArrayStream<ContextType>(streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecoderOptions<SplitUndefined<ContextType>>): AsyncGenerator<unknown, void, unknown>; | ||
/** | ||
@@ -20,6 +20,6 @@ * @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty. | ||
*/ | ||
export declare function decodeMultiStream<ContextType>(streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeOptions<SplitUndefined<ContextType>>): AsyncGenerator<unknown, void, unknown>; | ||
export declare function decodeMultiStream<ContextType>(streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecoderOptions<SplitUndefined<ContextType>>): AsyncGenerator<unknown, void, unknown>; | ||
/** | ||
* @deprecated Use {@link decodeMultiStream()} instead. | ||
*/ | ||
export declare function decodeStream<ContextType>(streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeOptions<SplitUndefined<ContextType>>): AsyncGenerator<unknown, void, unknown>; | ||
export declare const decodeStream: never; |
@@ -6,3 +6,2 @@ "use strict"; | ||
const stream_1 = require("./utils/stream"); | ||
const decode_1 = require("./decode"); | ||
/** | ||
@@ -12,5 +11,5 @@ * @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty. | ||
*/ | ||
async function decodeAsync(streamLike, options = decode_1.defaultDecodeOptions) { | ||
async function decodeAsync(streamLike, options) { | ||
const stream = (0, stream_1.ensureAsyncIterable)(streamLike); | ||
const decoder = new Decoder_1.Decoder(options.extensionCodec, options.context, options.maxStrLength, options.maxBinLength, options.maxArrayLength, options.maxMapLength, options.maxExtLength); | ||
const decoder = new Decoder_1.Decoder(options); | ||
return decoder.decodeAsync(stream); | ||
@@ -23,5 +22,5 @@ } | ||
*/ | ||
function decodeArrayStream(streamLike, options = decode_1.defaultDecodeOptions) { | ||
function decodeArrayStream(streamLike, options) { | ||
const stream = (0, stream_1.ensureAsyncIterable)(streamLike); | ||
const decoder = new Decoder_1.Decoder(options.extensionCodec, options.context, options.maxStrLength, options.maxBinLength, options.maxArrayLength, options.maxMapLength, options.maxExtLength); | ||
const decoder = new Decoder_1.Decoder(options); | ||
return decoder.decodeArrayStream(stream); | ||
@@ -34,5 +33,5 @@ } | ||
*/ | ||
function decodeMultiStream(streamLike, options = decode_1.defaultDecodeOptions) { | ||
function decodeMultiStream(streamLike, options) { | ||
const stream = (0, stream_1.ensureAsyncIterable)(streamLike); | ||
const decoder = new Decoder_1.Decoder(options.extensionCodec, options.context, options.maxStrLength, options.maxBinLength, options.maxArrayLength, options.maxMapLength, options.maxExtLength); | ||
const decoder = new Decoder_1.Decoder(options); | ||
return decoder.decodeStream(stream); | ||
@@ -44,6 +43,3 @@ } | ||
*/ | ||
function decodeStream(streamLike, options = decode_1.defaultDecodeOptions) { | ||
return decodeMultiStream(streamLike, options); | ||
} | ||
exports.decodeStream = decodeStream; | ||
exports.decodeStream = undefined; | ||
//# sourceMappingURL=decodeAsync.js.map |
import { ExtensionCodecType } from "./ExtensionCodec"; | ||
import { KeyDecoder } from "./CachedKeyDecoder"; | ||
export declare const DataViewIndexOutOfBoundsError: typeof Error; | ||
import type { ContextOf } from "./context"; | ||
export type DecoderOptions<ContextType = undefined> = Readonly<Partial<{ | ||
extensionCodec: ExtensionCodecType<ContextType>; | ||
/** | ||
* Decodes Int64 and Uint64 as bigint if it's set to true. | ||
* Depends on ES2020's {@link DataView#getBigInt64} and | ||
* {@link DataView#getBigUint64}. | ||
* | ||
* Defaults to false. | ||
*/ | ||
useBigInt64: boolean; | ||
/** | ||
* Maximum string length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxStrLength: number; | ||
/** | ||
* Maximum binary length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxBinLength: number; | ||
/** | ||
* Maximum array length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxArrayLength: number; | ||
/** | ||
* Maximum map length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxMapLength: number; | ||
/** | ||
* Maximum extension length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxExtLength: number; | ||
/** | ||
* An object key decoder. Defaults to the shared instance of {@link CachedKeyDecoder}. | ||
* `null` is a special value to disable the use of the key decoder at all. | ||
*/ | ||
keyDecoder: KeyDecoder | null; | ||
}>> & ContextOf<ContextType>; | ||
export declare const DataViewIndexOutOfBoundsError: RangeErrorConstructor; | ||
export declare class Decoder<ContextType = undefined> { | ||
private readonly extensionCodec; | ||
private readonly context; | ||
private readonly useBigInt64; | ||
private readonly maxStrLength; | ||
@@ -19,3 +67,3 @@ private readonly maxBinLength; | ||
private readonly stack; | ||
constructor(extensionCodec?: ExtensionCodecType<ContextType>, context?: ContextType, maxStrLength?: number, maxBinLength?: number, maxArrayLength?: number, maxMapLength?: number, maxExtLength?: number, keyDecoder?: KeyDecoder | null); | ||
constructor(options?: DecoderOptions<ContextType>); | ||
private reinitializeState; | ||
@@ -57,4 +105,6 @@ private setBuffer; | ||
private readI64; | ||
private readU64AsBigInt; | ||
private readI64AsBigInt; | ||
private readF32; | ||
private readF64; | ||
} |
@@ -11,5 +11,7 @@ "use strict"; | ||
const DecodeError_1 = require("./DecodeError"); | ||
const STATE_ARRAY = "array"; | ||
const STATE_MAP_KEY = "map_key"; | ||
const STATE_MAP_VALUE = "map_value"; | ||
const isValidMapKeyType = (key) => { | ||
const keyType = typeof key; | ||
return keyType === "string" || keyType === "number"; | ||
return typeof key === "string" || typeof key === "number"; | ||
}; | ||
@@ -19,27 +21,18 @@ const HEAD_BYTE_REQUIRED = -1; | ||
const EMPTY_BYTES = new Uint8Array(EMPTY_VIEW.buffer); | ||
// IE11: Hack to support IE11. | ||
// IE11: Drop this hack and just use RangeError when IE11 is obsolete. | ||
exports.DataViewIndexOutOfBoundsError = (() => { | ||
try { | ||
// IE11: The spec says it should throw RangeError, | ||
// IE11: but in IE11 it throws TypeError. | ||
EMPTY_VIEW.getInt8(0); | ||
try { | ||
// IE11: The spec says it should throw RangeError, | ||
// IE11: but in IE11 it throws TypeError. | ||
EMPTY_VIEW.getInt8(0); | ||
} | ||
catch (e) { | ||
if (!(e instanceof RangeError)) { | ||
throw new Error("This module is not supported in the current JavaScript engine because DataView does not throw RangeError on out-of-bounds access"); | ||
} | ||
catch (e) { | ||
return e.constructor; | ||
} | ||
throw new Error("never reached"); | ||
})(); | ||
} | ||
exports.DataViewIndexOutOfBoundsError = RangeError; | ||
const MORE_DATA = new exports.DataViewIndexOutOfBoundsError("Insufficient data"); | ||
const sharedCachedKeyDecoder = new CachedKeyDecoder_1.CachedKeyDecoder(); | ||
class Decoder { | ||
constructor(extensionCodec = ExtensionCodec_1.ExtensionCodec.defaultCodec, context = undefined, maxStrLength = int_1.UINT32_MAX, maxBinLength = int_1.UINT32_MAX, maxArrayLength = int_1.UINT32_MAX, maxMapLength = int_1.UINT32_MAX, maxExtLength = int_1.UINT32_MAX, keyDecoder = sharedCachedKeyDecoder) { | ||
this.extensionCodec = extensionCodec; | ||
this.context = context; | ||
this.maxStrLength = maxStrLength; | ||
this.maxBinLength = maxBinLength; | ||
this.maxArrayLength = maxArrayLength; | ||
this.maxMapLength = maxMapLength; | ||
this.maxExtLength = maxExtLength; | ||
this.keyDecoder = keyDecoder; | ||
constructor(options) { | ||
var _a, _b, _c, _d, _e, _f, _g; | ||
this.totalPos = 0; | ||
@@ -51,2 +44,11 @@ this.pos = 0; | ||
this.stack = []; | ||
this.extensionCodec = (_a = options === null || options === void 0 ? void 0 : options.extensionCodec) !== null && _a !== void 0 ? _a : ExtensionCodec_1.ExtensionCodec.defaultCodec; | ||
this.context = options === null || options === void 0 ? void 0 : options.context; // needs a type assertion because EncoderOptions has no context property when ContextType is undefined | ||
this.useBigInt64 = (_b = options === null || options === void 0 ? void 0 : options.useBigInt64) !== null && _b !== void 0 ? _b : false; | ||
this.maxStrLength = (_c = options === null || options === void 0 ? void 0 : options.maxStrLength) !== null && _c !== void 0 ? _c : int_1.UINT32_MAX; | ||
this.maxBinLength = (_d = options === null || options === void 0 ? void 0 : options.maxBinLength) !== null && _d !== void 0 ? _d : int_1.UINT32_MAX; | ||
this.maxArrayLength = (_e = options === null || options === void 0 ? void 0 : options.maxArrayLength) !== null && _e !== void 0 ? _e : int_1.UINT32_MAX; | ||
this.maxMapLength = (_f = options === null || options === void 0 ? void 0 : options.maxMapLength) !== null && _f !== void 0 ? _f : int_1.UINT32_MAX; | ||
this.maxExtLength = (_g = options === null || options === void 0 ? void 0 : options.maxExtLength) !== null && _g !== void 0 ? _g : int_1.UINT32_MAX; | ||
this.keyDecoder = ((options === null || options === void 0 ? void 0 : options.keyDecoder) !== undefined) ? options.keyDecoder : sharedCachedKeyDecoder; | ||
} | ||
@@ -247,3 +249,8 @@ reinitializeState() { | ||
// uint 64 | ||
object = this.readU64(); | ||
if (this.useBigInt64) { | ||
object = this.readU64AsBigInt(); | ||
} | ||
else { | ||
object = this.readU64(); | ||
} | ||
} | ||
@@ -264,3 +271,8 @@ else if (headByte === 0xd0) { | ||
// int 64 | ||
object = this.readI64(); | ||
if (this.useBigInt64) { | ||
object = this.readI64AsBigInt(); | ||
} | ||
else { | ||
object = this.readI64(); | ||
} | ||
} | ||
@@ -388,3 +400,3 @@ else if (headByte === 0xd9) { | ||
const state = stack[stack.length - 1]; | ||
if (state.type === 0 /* State.ARRAY */) { | ||
if (state.type === STATE_ARRAY) { | ||
state.array[state.position] = object; | ||
@@ -400,3 +412,3 @@ state.position++; | ||
} | ||
else if (state.type === 1 /* State.MAP_KEY */) { | ||
else if (state.type === STATE_MAP_KEY) { | ||
if (!isValidMapKeyType(object)) { | ||
@@ -409,3 +421,3 @@ throw new DecodeError_1.DecodeError("The type of key must be string or number but " + typeof object); | ||
state.key = object; | ||
state.type = 2 /* State.MAP_VALUE */; | ||
state.type = STATE_MAP_VALUE; | ||
continue DECODE; | ||
@@ -423,3 +435,3 @@ } | ||
state.key = null; | ||
state.type = 1 /* State.MAP_KEY */; | ||
state.type = STATE_MAP_KEY; | ||
continue DECODE; | ||
@@ -464,3 +476,3 @@ } | ||
this.stack.push({ | ||
type: 1 /* State.MAP_KEY */, | ||
type: STATE_MAP_KEY, | ||
size, | ||
@@ -477,3 +489,3 @@ key: null, | ||
this.stack.push({ | ||
type: 0 /* State.ARRAY */, | ||
type: STATE_ARRAY, | ||
size, | ||
@@ -497,7 +509,4 @@ array: new Array(size), | ||
} | ||
else if (byteLength > utf8_1.TEXT_DECODER_THRESHOLD) { | ||
object = (0, utf8_1.utf8DecodeTD)(this.bytes, offset, byteLength); | ||
} | ||
else { | ||
object = (0, utf8_1.utf8DecodeJs)(this.bytes, offset, byteLength); | ||
object = (0, utf8_1.utf8Decode)(this.bytes, offset, byteLength); | ||
} | ||
@@ -510,3 +519,3 @@ this.pos += headerOffset + byteLength; | ||
const state = this.stack[this.stack.length - 1]; | ||
return state.type === 1 /* State.MAP_KEY */; | ||
return state.type === STATE_MAP_KEY; | ||
} | ||
@@ -584,2 +593,12 @@ return false; | ||
} | ||
readU64AsBigInt() { | ||
const value = this.view.getBigUint64(this.pos); | ||
this.pos += 8; | ||
return value; | ||
} | ||
readI64AsBigInt() { | ||
const value = this.view.getBigInt64(this.pos); | ||
this.pos += 8; | ||
return value; | ||
} | ||
readF32() { | ||
@@ -586,0 +605,0 @@ const value = this.view.getFloat32(this.pos); |
@@ -1,48 +0,12 @@ | ||
import type { ExtensionCodecType } from "./ExtensionCodec"; | ||
import type { ContextOf, SplitUndefined } from "./context"; | ||
export declare type EncodeOptions<ContextType = undefined> = Partial<Readonly<{ | ||
extensionCodec: ExtensionCodecType<ContextType>; | ||
/** | ||
* The maximum depth in nested objects and arrays. | ||
* | ||
* Defaults to 100. | ||
*/ | ||
maxDepth: number; | ||
/** | ||
* The initial size of the internal buffer. | ||
* | ||
* Defaults to 2048. | ||
*/ | ||
initialBufferSize: number; | ||
/** | ||
* If `true`, the keys of an object is sorted. In other words, the encoded | ||
* binary is canonical and thus comparable to another encoded binary. | ||
* | ||
* Defaults to `false`. If enabled, it spends more time in encoding objects. | ||
*/ | ||
sortKeys: boolean; | ||
/** | ||
* If `true`, non-integer numbers are encoded in float32, not in float64 (the default). | ||
* | ||
* Only use it if precisions don't matter. | ||
* | ||
* Defaults to `false`. | ||
*/ | ||
forceFloat32: boolean; | ||
/** | ||
* If `true`, an object property with `undefined` value are ignored. | ||
* e.g. `{ foo: undefined }` will be encoded as `{}`, as `JSON.stringify()` does. | ||
* | ||
* Defaults to `false`. If enabled, it spends more time in encoding objects. | ||
*/ | ||
ignoreUndefined: boolean; | ||
/** | ||
* If `true`, integer numbers are encoded as floating point numbers, | ||
* with the `forceFloat32` option taken into account. | ||
* | ||
* Defaults to `false`. | ||
*/ | ||
forceIntegerToFloat: boolean; | ||
}>> & ContextOf<ContextType>; | ||
import type { EncoderOptions } from "./Encoder"; | ||
import type { SplitUndefined } from "./context"; | ||
/** | ||
* @deprecated Use {@link EncoderOptions} instead. | ||
*/ | ||
export type EncodeOptions = never; | ||
/** | ||
* @deprecated No longer supported. | ||
*/ | ||
export declare const defaultEncodeOptions: never; | ||
/** | ||
* It encodes `value` in the MessagePack format and | ||
@@ -53,2 +17,2 @@ * returns a byte buffer. | ||
*/ | ||
export declare function encode<ContextType = undefined>(value: unknown, options?: EncodeOptions<SplitUndefined<ContextType>>): Uint8Array; | ||
export declare function encode<ContextType = undefined>(value: unknown, options?: EncoderOptions<SplitUndefined<ContextType>>): Uint8Array; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.encode = void 0; | ||
exports.encode = exports.defaultEncodeOptions = void 0; | ||
const Encoder_1 = require("./Encoder"); | ||
const defaultEncodeOptions = {}; | ||
/** | ||
* @deprecated No longer supported. | ||
*/ | ||
exports.defaultEncodeOptions = undefined; | ||
/** | ||
* It encodes `value` in the MessagePack format and | ||
@@ -12,4 +15,4 @@ * returns a byte buffer. | ||
*/ | ||
function encode(value, options = defaultEncodeOptions) { | ||
const encoder = new Encoder_1.Encoder(options.extensionCodec, options.context, options.maxDepth, options.initialBufferSize, options.sortKeys, options.forceFloat32, options.ignoreUndefined, options.forceIntegerToFloat); | ||
function encode(value, options) { | ||
const encoder = new Encoder_1.Encoder(options); | ||
return encoder.encodeSharedRef(value); | ||
@@ -16,0 +19,0 @@ } |
import { ExtensionCodecType } from "./ExtensionCodec"; | ||
import type { ContextOf } from "./context"; | ||
export declare const DEFAULT_MAX_DEPTH = 100; | ||
export declare const DEFAULT_INITIAL_BUFFER_SIZE = 2048; | ||
export type EncoderOptions<ContextType = undefined> = Partial<Readonly<{ | ||
extensionCodec: ExtensionCodecType<ContextType>; | ||
/** | ||
* Encodes bigint as Int64 or Uint64 if it's set to true. | ||
* {@link forceIntegerToFloat} does not affect bigint. | ||
* Depends on ES2020's {@link DataView#setBigInt64} and | ||
* {@link DataView#setBigUint64}. | ||
* | ||
* Defaults to false. | ||
*/ | ||
useBigInt64: boolean; | ||
/** | ||
* The maximum depth in nested objects and arrays. | ||
* | ||
* Defaults to 100. | ||
*/ | ||
maxDepth: number; | ||
/** | ||
* The initial size of the internal buffer. | ||
* | ||
* Defaults to 2048. | ||
*/ | ||
initialBufferSize: number; | ||
/** | ||
* If `true`, the keys of an object is sorted. In other words, the encoded | ||
* binary is canonical and thus comparable to another encoded binary. | ||
* | ||
* Defaults to `false`. If enabled, it spends more time in encoding objects. | ||
*/ | ||
sortKeys: boolean; | ||
/** | ||
* If `true`, non-integer numbers are encoded in float32, not in float64 (the default). | ||
* | ||
* Only use it if precisions don't matter. | ||
* | ||
* Defaults to `false`. | ||
*/ | ||
forceFloat32: boolean; | ||
/** | ||
* If `true`, an object property with `undefined` value are ignored. | ||
* e.g. `{ foo: undefined }` will be encoded as `{}`, as `JSON.stringify()` does. | ||
* | ||
* Defaults to `false`. If enabled, it spends more time in encoding objects. | ||
*/ | ||
ignoreUndefined: boolean; | ||
/** | ||
* If `true`, integer numbers are encoded as floating point numbers, | ||
* with the `forceFloat32` option taken into account. | ||
* | ||
* Defaults to `false`. | ||
*/ | ||
forceIntegerToFloat: boolean; | ||
}>> & ContextOf<ContextType>; | ||
export declare class Encoder<ContextType = undefined> { | ||
private readonly extensionCodec; | ||
private readonly context; | ||
private readonly useBigInt64; | ||
private readonly maxDepth; | ||
@@ -16,3 +71,3 @@ private readonly initialBufferSize; | ||
private bytes; | ||
constructor(extensionCodec?: ExtensionCodecType<ContextType>, context?: ContextType, maxDepth?: number, initialBufferSize?: number, sortKeys?: boolean, forceFloat32?: boolean, ignoreUndefined?: boolean, forceIntegerToFloat?: boolean); | ||
constructor(options?: EncoderOptions<ContextType>); | ||
private reinitializeState; | ||
@@ -35,2 +90,4 @@ /** | ||
private encodeNumber; | ||
private encodeNumberAsFloat; | ||
private encodeBigInt64; | ||
private writeStringHeader; | ||
@@ -55,2 +112,4 @@ private encodeString; | ||
private writeI64; | ||
private writeBigUint64; | ||
private writeBigInt64; | ||
} |
@@ -11,11 +11,13 @@ "use strict"; | ||
class Encoder { | ||
constructor(extensionCodec = ExtensionCodec_1.ExtensionCodec.defaultCodec, context = undefined, maxDepth = exports.DEFAULT_MAX_DEPTH, initialBufferSize = exports.DEFAULT_INITIAL_BUFFER_SIZE, sortKeys = false, forceFloat32 = false, ignoreUndefined = false, forceIntegerToFloat = false) { | ||
this.extensionCodec = extensionCodec; | ||
this.context = context; | ||
this.maxDepth = maxDepth; | ||
this.initialBufferSize = initialBufferSize; | ||
this.sortKeys = sortKeys; | ||
this.forceFloat32 = forceFloat32; | ||
this.ignoreUndefined = ignoreUndefined; | ||
this.forceIntegerToFloat = forceIntegerToFloat; | ||
constructor(options) { | ||
var _a, _b, _c, _d, _e, _f, _g, _h; | ||
this.extensionCodec = (_a = options === null || options === void 0 ? void 0 : options.extensionCodec) !== null && _a !== void 0 ? _a : ExtensionCodec_1.ExtensionCodec.defaultCodec; | ||
this.context = options === null || options === void 0 ? void 0 : options.context; // needs a type assertion because EncoderOptions has no context property when ContextType is undefined | ||
this.useBigInt64 = (_b = options === null || options === void 0 ? void 0 : options.useBigInt64) !== null && _b !== void 0 ? _b : false; | ||
this.maxDepth = (_c = options === null || options === void 0 ? void 0 : options.maxDepth) !== null && _c !== void 0 ? _c : exports.DEFAULT_MAX_DEPTH; | ||
this.initialBufferSize = (_d = options === null || options === void 0 ? void 0 : options.initialBufferSize) !== null && _d !== void 0 ? _d : exports.DEFAULT_INITIAL_BUFFER_SIZE; | ||
this.sortKeys = (_e = options === null || options === void 0 ? void 0 : options.sortKeys) !== null && _e !== void 0 ? _e : false; | ||
this.forceFloat32 = (_f = options === null || options === void 0 ? void 0 : options.forceFloat32) !== null && _f !== void 0 ? _f : false; | ||
this.ignoreUndefined = (_g = options === null || options === void 0 ? void 0 : options.ignoreUndefined) !== null && _g !== void 0 ? _g : false; | ||
this.forceIntegerToFloat = (_h = options === null || options === void 0 ? void 0 : options.forceIntegerToFloat) !== null && _h !== void 0 ? _h : false; | ||
this.pos = 0; | ||
@@ -57,3 +59,8 @@ this.view = new DataView(new ArrayBuffer(this.initialBufferSize)); | ||
else if (typeof object === "number") { | ||
this.encodeNumber(object); | ||
if (!this.forceIntegerToFloat) { | ||
this.encodeNumber(object); | ||
} | ||
else { | ||
this.encodeNumberAsFloat(object); | ||
} | ||
} | ||
@@ -63,2 +70,5 @@ else if (typeof object === "string") { | ||
} | ||
else if (this.useBigInt64 && typeof object === "bigint") { | ||
this.encodeBigInt64(object); | ||
} | ||
else { | ||
@@ -94,3 +104,3 @@ this.encodeObject(object, depth); | ||
encodeNumber(object) { | ||
if (Number.isSafeInteger(object) && !this.forceIntegerToFloat) { | ||
if (!this.forceIntegerToFloat && Number.isSafeInteger(object)) { | ||
if (object >= 0) { | ||
@@ -116,3 +126,3 @@ if (object < 0x80) { | ||
} | ||
else { | ||
else if (!this.useBigInt64) { | ||
// uint 64 | ||
@@ -122,2 +132,5 @@ this.writeU8(0xcf); | ||
} | ||
else { | ||
this.encodeNumberAsFloat(object); | ||
} | ||
} | ||
@@ -144,3 +157,3 @@ else { | ||
} | ||
else { | ||
else if (!this.useBigInt64) { | ||
// int 64 | ||
@@ -150,18 +163,35 @@ this.writeU8(0xd3); | ||
} | ||
else { | ||
this.encodeNumberAsFloat(object); | ||
} | ||
} | ||
} | ||
else { | ||
// non-integer numbers | ||
if (this.forceFloat32) { | ||
// float 32 | ||
this.writeU8(0xca); | ||
this.writeF32(object); | ||
} | ||
else { | ||
// float 64 | ||
this.writeU8(0xcb); | ||
this.writeF64(object); | ||
} | ||
this.encodeNumberAsFloat(object); | ||
} | ||
} | ||
encodeNumberAsFloat(object) { | ||
if (this.forceFloat32) { | ||
// float 32 | ||
this.writeU8(0xca); | ||
this.writeF32(object); | ||
} | ||
else { | ||
// float 64 | ||
this.writeU8(0xcb); | ||
this.writeF64(object); | ||
} | ||
} | ||
encodeBigInt64(object) { | ||
if (object >= BigInt(0)) { | ||
// uint 64 | ||
this.writeU8(0xcf); | ||
this.writeBigUint64(object); | ||
} | ||
else { | ||
// int 64 | ||
this.writeU8(0xd3); | ||
this.writeBigInt64(object); | ||
} | ||
} | ||
writeStringHeader(byteLength) { | ||
@@ -193,17 +223,7 @@ if (byteLength < 32) { | ||
const maxHeaderSize = 1 + 4; | ||
const strLength = object.length; | ||
if (strLength > utf8_1.TEXT_ENCODER_THRESHOLD) { | ||
const byteLength = (0, utf8_1.utf8Count)(object); | ||
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength); | ||
this.writeStringHeader(byteLength); | ||
(0, utf8_1.utf8EncodeTE)(object, this.bytes, this.pos); | ||
this.pos += byteLength; | ||
} | ||
else { | ||
const byteLength = (0, utf8_1.utf8Count)(object); | ||
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength); | ||
this.writeStringHeader(byteLength); | ||
(0, utf8_1.utf8EncodeJs)(object, this.bytes, this.pos); | ||
this.pos += byteLength; | ||
} | ||
const byteLength = (0, utf8_1.utf8Count)(object); | ||
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength); | ||
this.writeStringHeader(byteLength); | ||
(0, utf8_1.utf8Encode)(object, this.bytes, this.pos); | ||
this.pos += byteLength; | ||
} | ||
@@ -415,4 +435,14 @@ encodeObject(object, depth) { | ||
} | ||
writeBigUint64(value) { | ||
this.ensureBufferSizeToWrite(8); | ||
this.view.setBigUint64(this.pos, value); | ||
this.pos += 8; | ||
} | ||
writeBigInt64(value) { | ||
this.ensureBufferSizeToWrite(8); | ||
this.view.setBigInt64(this.pos, value); | ||
this.pos += 8; | ||
} | ||
} | ||
exports.Encoder = Encoder; | ||
//# sourceMappingURL=Encoder.js.map |
import { ExtData } from "./ExtData"; | ||
export declare type ExtensionDecoderType<ContextType> = (data: Uint8Array, extensionType: number, context: ContextType) => unknown; | ||
export declare type ExtensionEncoderType<ContextType> = (input: unknown, context: ContextType) => Uint8Array | null; | ||
export declare type ExtensionCodecType<ContextType> = { | ||
export type ExtensionDecoderType<ContextType> = (data: Uint8Array, extensionType: number, context: ContextType) => unknown; | ||
export type ExtensionEncoderType<ContextType> = (input: unknown, context: ContextType) => Uint8Array | null; | ||
export type ExtensionCodecType<ContextType> = { | ||
__brand?: ContextType; | ||
@@ -6,0 +6,0 @@ tryToEncode(object: unknown, context: ContextType): ExtData | null; |
@@ -12,6 +12,11 @@ import { encode } from "./encode"; | ||
import { Decoder, DataViewIndexOutOfBoundsError } from "./Decoder"; | ||
export { Decoder, DataViewIndexOutOfBoundsError }; | ||
import type { DecoderOptions } from "./Decoder"; | ||
export type { DecoderOptions }; | ||
import { DecodeError } from "./DecodeError"; | ||
export { Decoder, DecodeError, DataViewIndexOutOfBoundsError }; | ||
export { DecodeError }; | ||
import { Encoder } from "./Encoder"; | ||
export { Encoder }; | ||
import type { EncoderOptions } from "./Encoder"; | ||
export type { EncoderOptions }; | ||
import { ExtensionCodec } from "./ExtensionCodec"; | ||
@@ -18,0 +23,0 @@ export { ExtensionCodec }; |
"use strict"; | ||
// Main Functions: | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.decodeTimestampExtension = exports.encodeTimestampExtension = exports.decodeTimestampToTimeSpec = exports.encodeTimeSpecToTimestamp = exports.encodeDateToTimeSpec = exports.EXT_TIMESTAMP = exports.ExtData = exports.ExtensionCodec = exports.Encoder = exports.DataViewIndexOutOfBoundsError = exports.DecodeError = exports.Decoder = exports.decodeStream = exports.decodeMultiStream = exports.decodeArrayStream = exports.decodeAsync = exports.decodeMulti = exports.decode = exports.encode = void 0; | ||
exports.decodeTimestampExtension = exports.encodeTimestampExtension = exports.decodeTimestampToTimeSpec = exports.encodeTimeSpecToTimestamp = exports.encodeDateToTimeSpec = exports.EXT_TIMESTAMP = exports.ExtData = exports.ExtensionCodec = exports.Encoder = exports.DecodeError = exports.DataViewIndexOutOfBoundsError = exports.Decoder = exports.decodeStream = exports.decodeMultiStream = exports.decodeArrayStream = exports.decodeAsync = exports.decodeMulti = exports.decode = exports.encode = void 0; | ||
const encode_1 = require("./encode"); | ||
@@ -22,3 +22,3 @@ Object.defineProperty(exports, "encode", { enumerable: true, get: function () { return encode_1.encode; } }); | ||
Object.defineProperty(exports, "Encoder", { enumerable: true, get: function () { return Encoder_1.Encoder; } }); | ||
// Utilitiies for Extension Types: | ||
// Utilities for Extension Types: | ||
const ExtensionCodec_1 = require("./ExtensionCodec"); | ||
@@ -25,0 +25,0 @@ Object.defineProperty(exports, "ExtensionCodec", { enumerable: true, get: function () { return ExtensionCodec_1.ExtensionCodec; } }); |
export declare const EXT_TIMESTAMP = -1; | ||
export declare type TimeSpec = { | ||
export type TimeSpec = { | ||
sec: number; | ||
@@ -4,0 +4,0 @@ nsec: number; |
@@ -1,4 +0,4 @@ | ||
export declare type ReadableStreamLike<T> = AsyncIterable<T> | ReadableStream<T>; | ||
export type ReadableStreamLike<T> = AsyncIterable<T> | ReadableStream<T>; | ||
export declare function isAsyncIterable<T>(object: ReadableStreamLike<T>): object is AsyncIterable<T>; | ||
export declare function asyncIterableFromStream<T>(stream: ReadableStream<T>): AsyncIterable<T>; | ||
export declare function ensureAsyncIterable<T>(streamLike: ReadableStreamLike<T>): AsyncIterable<T>; |
export declare function utf8Count(str: string): number; | ||
export declare function utf8EncodeJs(str: string, output: Uint8Array, outputOffset: number): void; | ||
export declare const TEXT_ENCODER_THRESHOLD: number; | ||
declare function utf8EncodeTEencodeInto(str: string, output: Uint8Array, outputOffset: number): void; | ||
export declare const utf8EncodeTE: typeof utf8EncodeTEencodeInto; | ||
export declare function utf8EncodeTE(str: string, output: Uint8Array, outputOffset: number): void; | ||
export declare function utf8Encode(str: string, output: Uint8Array, outputOffset: number): void; | ||
export declare function utf8DecodeJs(bytes: Uint8Array, inputOffset: number, byteLength: number): string; | ||
export declare const TEXT_DECODER_THRESHOLD: number; | ||
export declare function utf8DecodeTD(bytes: Uint8Array, inputOffset: number, byteLength: number): string; | ||
export {}; | ||
export declare function utf8Decode(bytes: Uint8Array, inputOffset: number, byteLength: number): string; |
"use strict"; | ||
var _a, _b, _c; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.utf8DecodeTD = exports.TEXT_DECODER_THRESHOLD = exports.utf8DecodeJs = exports.utf8EncodeTE = exports.TEXT_ENCODER_THRESHOLD = exports.utf8EncodeJs = exports.utf8Count = void 0; | ||
/* eslint-disable @typescript-eslint/no-unnecessary-condition */ | ||
const int_1 = require("./int"); | ||
const TEXT_ENCODING_AVAILABLE = (typeof process === "undefined" || ((_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a["TEXT_ENCODING"]) !== "never") && | ||
typeof TextEncoder !== "undefined" && | ||
typeof TextDecoder !== "undefined"; | ||
exports.utf8Decode = exports.utf8DecodeTD = exports.utf8DecodeJs = exports.utf8Encode = exports.utf8EncodeTE = exports.utf8EncodeJs = exports.utf8Count = void 0; | ||
function utf8Count(str) { | ||
@@ -93,15 +87,25 @@ const strLength = str.length; | ||
exports.utf8EncodeJs = utf8EncodeJs; | ||
const sharedTextEncoder = TEXT_ENCODING_AVAILABLE ? new TextEncoder() : undefined; | ||
exports.TEXT_ENCODER_THRESHOLD = !TEXT_ENCODING_AVAILABLE | ||
? int_1.UINT32_MAX | ||
: typeof process !== "undefined" && ((_b = process === null || process === void 0 ? void 0 : process.env) === null || _b === void 0 ? void 0 : _b["TEXT_ENCODING"]) !== "force" | ||
? 200 | ||
: 0; | ||
function utf8EncodeTEencode(str, output, outputOffset) { | ||
output.set(sharedTextEncoder.encode(str), outputOffset); | ||
} | ||
function utf8EncodeTEencodeInto(str, output, outputOffset) { | ||
// TextEncoder and TextDecoder are standardized in whatwg encoding: | ||
// https://encoding.spec.whatwg.org/ | ||
// and available in all the modern browsers: | ||
// https://caniuse.com/textencoder | ||
// They are available in Node.js since v12 LTS as well: | ||
// https://nodejs.org/api/globals.html#textencoder | ||
const sharedTextEncoder = new TextEncoder(); | ||
// This threshold should be determined by benchmarking, which might vary in engines and input data. | ||
// Run `npx ts-node benchmark/encode-string.ts` for details. | ||
const TEXT_ENCODER_THRESHOLD = 50; | ||
function utf8EncodeTE(str, output, outputOffset) { | ||
sharedTextEncoder.encodeInto(str, output.subarray(outputOffset)); | ||
} | ||
exports.utf8EncodeTE = (sharedTextEncoder === null || sharedTextEncoder === void 0 ? void 0 : sharedTextEncoder.encodeInto) ? utf8EncodeTEencodeInto : utf8EncodeTEencode; | ||
exports.utf8EncodeTE = utf8EncodeTE; | ||
function utf8Encode(str, output, outputOffset) { | ||
if (str.length > TEXT_ENCODER_THRESHOLD) { | ||
utf8EncodeTE(str, output, outputOffset); | ||
} | ||
else { | ||
utf8EncodeJs(str, output, outputOffset); | ||
} | ||
} | ||
exports.utf8Encode = utf8Encode; | ||
const CHUNK_SIZE = 4096; | ||
@@ -157,8 +161,6 @@ function utf8DecodeJs(bytes, inputOffset, byteLength) { | ||
exports.utf8DecodeJs = utf8DecodeJs; | ||
const sharedTextDecoder = TEXT_ENCODING_AVAILABLE ? new TextDecoder() : null; | ||
exports.TEXT_DECODER_THRESHOLD = !TEXT_ENCODING_AVAILABLE | ||
? int_1.UINT32_MAX | ||
: typeof process !== "undefined" && ((_c = process === null || process === void 0 ? void 0 : process.env) === null || _c === void 0 ? void 0 : _c["TEXT_DECODER"]) !== "force" | ||
? 200 | ||
: 0; | ||
const sharedTextDecoder = new TextDecoder(); | ||
// This threshold should be determined by benchmarking, which might vary in engines and input data. | ||
// Run `npx ts-node benchmark/decode-string.ts` for details. | ||
const TEXT_DECODER_THRESHOLD = 200; | ||
function utf8DecodeTD(bytes, inputOffset, byteLength) { | ||
@@ -169,2 +171,11 @@ const stringBytes = bytes.subarray(inputOffset, inputOffset + byteLength); | ||
exports.utf8DecodeTD = utf8DecodeTD; | ||
function utf8Decode(bytes, inputOffset, byteLength) { | ||
if (byteLength > TEXT_DECODER_THRESHOLD) { | ||
return utf8DecodeTD(bytes, inputOffset, byteLength); | ||
} | ||
else { | ||
return utf8DecodeJs(bytes, inputOffset, byteLength); | ||
} | ||
} | ||
exports.utf8Decode = utf8Decode; | ||
//# sourceMappingURL=utf8.js.map |
{ | ||
"name": "@msgpack/msgpack", | ||
"version": "2.8.0", | ||
"version": "3.0.0-beta1", | ||
"description": "MessagePack for ECMA-262/JavaScript/TypeScript", | ||
@@ -16,11 +16,7 @@ "author": "The MessagePack community", | ||
"prepare": "npm run clean && webpack --bail && tsc --build tsconfig.dist.json tsconfig.dist.es5+esm.json && ts-node tools/esmify.ts dist.es5+esm/*.js dist.es5+esm/*/*.js", | ||
"prepublishOnly": "run-p 'test:dist:*' && npm run test:browser", | ||
"prepublishOnly": "npm run test:dist", | ||
"clean": "rimraf build dist dist.*", | ||
"test": "mocha 'test/**/*.test.ts'", | ||
"test:purejs": "TEXT_ENCODING=never mocha 'test/**/*.test.ts'", | ||
"test:te": "TEXT_ENCODING=force mocha 'test/**/*.test.ts'", | ||
"test:dist:purejs": "TS_NODE_PROJECT=tsconfig.test-dist-es5-purejs.json npm run test:purejs -- --reporter=dot", | ||
"test:cover": "npm run cover:clean && npm-run-all 'test:cover:*' && npm run cover:report", | ||
"test:cover:purejs": "npx nyc --no-clean npm run test:purejs", | ||
"test:cover:te": "npx nyc --no-clean npm run test:te", | ||
"test:dist": "npm run lint && npm run test && npm run test:browser && npm run test:deno", | ||
"test:cover": "npm run cover:clean && npx nyc --no-clean npm run 'test' && npm run cover:report", | ||
"test:deno": "deno test test/deno_test.ts", | ||
@@ -55,3 +51,3 @@ "test:fuzz": "npm exec --yes -- jsfuzz@git+https://gitlab.com/gitlab-org/security-products/analyzers/fuzzers/jsfuzz.git --fuzzTime 60 --no-versifier test/decode.jsfuzz.js corpus", | ||
"engines": { | ||
"node": ">= 10" | ||
"node": ">= 14" | ||
}, | ||
@@ -66,3 +62,2 @@ "devDependencies": { | ||
"assert": "latest", | ||
"blob-polyfill": "latest", | ||
"buffer": "latest", | ||
@@ -85,3 +80,2 @@ "core-js": "latest", | ||
"msgpack-test-js": "latest", | ||
"npm-run-all": "latest", | ||
"prettier": "latest", | ||
@@ -93,3 +87,2 @@ "rimraf": "latest", | ||
"typescript": "latest", | ||
"web-streams-polyfill": "latest", | ||
"webpack": "latest", | ||
@@ -96,0 +89,0 @@ "webpack-cli": "latest" |
@@ -41,10 +41,10 @@ # MessagePack for JavaScript/ECMA-262 <!-- omit in toc --> | ||
- [API](#api) | ||
- [`encode(data: unknown, options?: EncodeOptions): Uint8Array`](#encodedata-unknown-options-encodeoptions-uint8array) | ||
- [`EncodeOptions`](#encodeoptions) | ||
- [`decode(buffer: ArrayLike<number> | BufferSource, options?: DecodeOptions): unknown`](#decodebuffer-arraylikenumber--buffersource-options-decodeoptions-unknown) | ||
- [`DecodeOptions`](#decodeoptions) | ||
- [`decodeMulti(buffer: ArrayLike<number> | BufferSource, options?: DecodeOptions): Generator<unknown, void, unknown>`](#decodemultibuffer-arraylikenumber--buffersource-options-decodeoptions-generatorunknown-void-unknown) | ||
- [`decodeAsync(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeAsyncOptions): Promise<unknown>`](#decodeasyncstream-readablestreamlikearraylikenumber--buffersource-options-decodeasyncoptions-promiseunknown) | ||
- [`decodeArrayStream(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeAsyncOptions): AsyncIterable<unknown>`](#decodearraystreamstream-readablestreamlikearraylikenumber--buffersource-options-decodeasyncoptions-asynciterableunknown) | ||
- [`decodeMultiStream(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeAsyncOptions): AsyncIterable<unknown>`](#decodemultistreamstream-readablestreamlikearraylikenumber--buffersource-options-decodeasyncoptions-asynciterableunknown) | ||
- [`encode(data: unknown, options?: EncoderOptions): Uint8Array`](#encodedata-unknown-options-encoderoptions-uint8array) | ||
- [`EncoderOptions`](#encoderoptions) | ||
- [`decode(buffer: ArrayLike<number> | BufferSource, options?: DecoderOptions): unknown`](#decodebuffer-arraylikenumber--buffersource-options-decoderoptions-unknown) | ||
- [`DecoderOptions`](#decoderoptions) | ||
- [`decodeMulti(buffer: ArrayLike<number> | BufferSource, options?: DecoderOptions): Generator<unknown, void, unknown>`](#decodemultibuffer-arraylikenumber--buffersource-options-decoderoptions-generatorunknown-void-unknown) | ||
- [`decodeAsync(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecoderOptions): Promise<unknown>`](#decodeasyncstream-readablestreamlikearraylikenumber--buffersource-options-decoderoptions-promiseunknown) | ||
- [`decodeArrayStream(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecoderOptions): AsyncIterable<unknown>`](#decodearraystreamstream-readablestreamlikearraylikenumber--buffersource-options-decoderoptions-asynciterableunknown) | ||
- [`decodeMultiStream(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecoderOptions): AsyncIterable<unknown>`](#decodemultistreamstream-readablestreamlikearraylikenumber--buffersource-options-decoderoptions-asynciterableunknown) | ||
- [Reusing Encoder and Decoder instances](#reusing-encoder-and-decoder-instances) | ||
@@ -84,3 +84,3 @@ - [Extension Types](#extension-types) | ||
### `encode(data: unknown, options?: EncodeOptions): Uint8Array` | ||
### `encode(data: unknown, options?: EncoderOptions): Uint8Array` | ||
@@ -110,3 +110,3 @@ It encodes `data` into a single MessagePack-encoded object, and returns a byte array as `Uint8Array`. It throws errors if `data` is, or includes, a non-serializable object such as a `function` or a `symbol`. | ||
#### `EncodeOptions` | ||
#### `EncoderOptions` | ||
@@ -124,3 +124,3 @@ Name|Type|Default | ||
### `decode(buffer: ArrayLike<number> | BufferSource, options?: DecodeOptions): unknown` | ||
### `decode(buffer: ArrayLike<number> | BufferSource, options?: DecoderOptions): unknown` | ||
@@ -145,3 +145,3 @@ It decodes `buffer` that includes a MessagePack-encoded object, and returns the decoded object typed `unknown`. | ||
#### `DecodeOptions` | ||
#### `DecoderOptions` | ||
@@ -160,3 +160,3 @@ Name|Type|Default | ||
### `decodeMulti(buffer: ArrayLike<number> | BufferSource, options?: DecodeOptions): Generator<unknown, void, unknown>` | ||
### `decodeMulti(buffer: ArrayLike<number> | BufferSource, options?: DecoderOptions): Generator<unknown, void, unknown>` | ||
@@ -179,3 +179,3 @@ It decodes `buffer` that includes multiple MessagePack-encoded objects, and returns decoded objects as a generator. See also `decodeMultiStream()`, which is an asynchronous variant of this function. | ||
### `decodeAsync(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeAsyncOptions): Promise<unknown>` | ||
### `decodeAsync(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecoderOptions): Promise<unknown>` | ||
@@ -186,4 +186,2 @@ It decodes `stream`, where `ReadableStreamLike<T>` is defined as `ReadableStream<T> | AsyncIterable<T>`, in an async iterable of byte arrays, and returns decoded object as `unknown` type, wrapped in `Promise`. | ||
`DecodeAsyncOptions` is the same as `DecodeOptions` for `decode()`. | ||
This function is designed to work with whatwg `fetch()` like this: | ||
@@ -204,3 +202,3 @@ | ||
### `decodeArrayStream(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeAsyncOptions): AsyncIterable<unknown>` | ||
### `decodeArrayStream(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecoderOptions): AsyncIterable<unknown>` | ||
@@ -222,3 +220,3 @@ It is alike to `decodeAsync()`, but only accepts a `stream` that includes an array of items, and emits a decoded item one by one. | ||
### `decodeMultiStream(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecodeAsyncOptions): AsyncIterable<unknown>` | ||
### `decodeMultiStream(stream: ReadableStreamLike<ArrayLike<number> | BufferSource>, options?: DecoderOptions): AsyncIterable<unknown>` | ||
@@ -246,3 +244,3 @@ It is alike to `decodeAsync()` and `decodeArrayStream()`, but the input `stream` must consist of multiple MessagePack-encoded items. This is an asynchronous variant for `decodeMulti()`. | ||
`Encoder` and `Decoder` classes is provided to have better performance by reusing instances: | ||
`Encoder` and `Decoder` classes are provided to have better performance by reusing instances: | ||
@@ -265,2 +263,4 @@ ```typescript | ||
`Encoder` and `Decoder` take the same options as `encode()` and `decode()` respectively. | ||
## Extension Types | ||
@@ -319,3 +319,3 @@ | ||
When you use an extension codec, it might be necessary to have encoding/decoding state to keep track of which objects got encoded/re-created. To do this, pass a `context` to the `EncodeOptions` and `DecodeOptions`: | ||
When you use an extension codec, it might be necessary to have encoding/decoding state to keep track of which objects got encoded/re-created. To do this, pass a `context` to the `EncoderOptions` and `DecoderOptions`: | ||
@@ -322,0 +322,0 @@ ```typescript |
import { Decoder } from "./Decoder"; | ||
import type { ExtensionCodecType } from "./ExtensionCodec"; | ||
import type { ContextOf, SplitUndefined } from "./context"; | ||
import type { DecoderOptions } from "./Decoder"; | ||
import type { SplitUndefined } from "./context"; | ||
export type DecodeOptions<ContextType = undefined> = Readonly< | ||
Partial<{ | ||
extensionCodec: ExtensionCodecType<ContextType>; | ||
/** | ||
* @deprecated Use {@link DecoderOptions} instead. | ||
*/ | ||
export type DecodeOptions = never; | ||
/** | ||
* Maximum string length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxStrLength: number; | ||
/** | ||
* Maximum binary length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxBinLength: number; | ||
/** | ||
* Maximum array length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxArrayLength: number; | ||
/** | ||
* Maximum map length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxMapLength: number; | ||
/** | ||
* Maximum extension length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxExtLength: number; | ||
}> | ||
> & | ||
ContextOf<ContextType>; | ||
/** | ||
* @deprecated No longer supported. | ||
*/ | ||
export const defaultDecodeOptions: never = undefined as never; | ||
export const defaultDecodeOptions: DecodeOptions = {}; | ||
/** | ||
@@ -56,13 +26,5 @@ * It decodes a single MessagePack object in a buffer. | ||
buffer: ArrayLike<number> | BufferSource, | ||
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any, | ||
options?: DecoderOptions<SplitUndefined<ContextType>>, | ||
): unknown { | ||
const decoder = new Decoder( | ||
options.extensionCodec, | ||
(options as typeof options & { context: any }).context, | ||
options.maxStrLength, | ||
options.maxBinLength, | ||
options.maxArrayLength, | ||
options.maxMapLength, | ||
options.maxExtLength, | ||
); | ||
const decoder = new Decoder(options); | ||
return decoder.decode(buffer); | ||
@@ -80,14 +42,6 @@ } | ||
buffer: ArrayLike<number> | BufferSource, | ||
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any, | ||
options?: DecoderOptions<SplitUndefined<ContextType>>, | ||
): Generator<unknown, void, unknown> { | ||
const decoder = new Decoder( | ||
options.extensionCodec, | ||
(options as typeof options & { context: any }).context, | ||
options.maxStrLength, | ||
options.maxBinLength, | ||
options.maxArrayLength, | ||
options.maxMapLength, | ||
options.maxExtLength, | ||
); | ||
const decoder = new Decoder(options); | ||
return decoder.decodeMulti(buffer); | ||
} |
import { Decoder } from "./Decoder"; | ||
import { ensureAsyncIterable } from "./utils/stream"; | ||
import { defaultDecodeOptions } from "./decode"; | ||
import type { DecoderOptions } from "./Decoder"; | ||
import type { ReadableStreamLike } from "./utils/stream"; | ||
import type { DecodeOptions } from "./decode"; | ||
import type { SplitUndefined } from "./context"; | ||
@@ -12,17 +11,8 @@ | ||
*/ | ||
export async function decodeAsync<ContextType>( | ||
export async function decodeAsync<ContextType = undefined>( | ||
streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>, | ||
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any, | ||
options?: DecoderOptions<SplitUndefined<ContextType>>, | ||
): Promise<unknown> { | ||
const stream = ensureAsyncIterable(streamLike); | ||
const decoder = new Decoder( | ||
options.extensionCodec, | ||
(options as typeof options & { context: any }).context, | ||
options.maxStrLength, | ||
options.maxBinLength, | ||
options.maxArrayLength, | ||
options.maxMapLength, | ||
options.maxExtLength, | ||
); | ||
const decoder = new Decoder(options); | ||
return decoder.decodeAsync(stream); | ||
@@ -37,16 +27,6 @@ } | ||
streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>, | ||
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any, | ||
options?: DecoderOptions<SplitUndefined<ContextType>>, | ||
): AsyncGenerator<unknown, void, unknown> { | ||
const stream = ensureAsyncIterable(streamLike); | ||
const decoder = new Decoder( | ||
options.extensionCodec, | ||
(options as typeof options & { context: any }).context, | ||
options.maxStrLength, | ||
options.maxBinLength, | ||
options.maxArrayLength, | ||
options.maxMapLength, | ||
options.maxExtLength, | ||
); | ||
const decoder = new Decoder(options); | ||
return decoder.decodeArrayStream(stream); | ||
@@ -61,16 +41,6 @@ } | ||
streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>, | ||
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any, | ||
options?: DecoderOptions<SplitUndefined<ContextType>>, | ||
): AsyncGenerator<unknown, void, unknown> { | ||
const stream = ensureAsyncIterable(streamLike); | ||
const decoder = new Decoder( | ||
options.extensionCodec, | ||
(options as typeof options & { context: any }).context, | ||
options.maxStrLength, | ||
options.maxBinLength, | ||
options.maxArrayLength, | ||
options.maxMapLength, | ||
options.maxExtLength, | ||
); | ||
const decoder = new Decoder(options); | ||
return decoder.decodeStream(stream); | ||
@@ -82,7 +52,2 @@ } | ||
*/ | ||
export function decodeStream<ContextType>( | ||
streamLike: ReadableStreamLike<ArrayLike<number> | BufferSource>, | ||
options: DecodeOptions<SplitUndefined<ContextType>> = defaultDecodeOptions as any, | ||
): AsyncGenerator<unknown, void, unknown> { | ||
return decodeMultiStream(streamLike, options); | ||
} | ||
export const decodeStream: never = undefined as never; |
import { prettyByte } from "./utils/prettyByte"; | ||
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec"; | ||
import { getInt64, getUint64, UINT32_MAX } from "./utils/int"; | ||
import { utf8DecodeJs, TEXT_DECODER_THRESHOLD, utf8DecodeTD } from "./utils/utf8"; | ||
import { utf8Decode } from "./utils/utf8"; | ||
import { createDataView, ensureUint8Array } from "./utils/typedArrays"; | ||
import { CachedKeyDecoder, KeyDecoder } from "./CachedKeyDecoder"; | ||
import { DecodeError } from "./DecodeError"; | ||
import type { ContextOf } from "./context"; | ||
const enum State { | ||
ARRAY, | ||
MAP_KEY, | ||
MAP_VALUE, | ||
} | ||
export type DecoderOptions<ContextType = undefined> = Readonly< | ||
Partial<{ | ||
extensionCodec: ExtensionCodecType<ContextType>; | ||
/** | ||
* Decodes Int64 and Uint64 as bigint if it's set to true. | ||
* Depends on ES2020's {@link DataView#getBigInt64} and | ||
* {@link DataView#getBigUint64}. | ||
* | ||
* Defaults to false. | ||
*/ | ||
useBigInt64: boolean; | ||
/** | ||
* Maximum string length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxStrLength: number; | ||
/** | ||
* Maximum binary length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxBinLength: number; | ||
/** | ||
* Maximum array length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxArrayLength: number; | ||
/** | ||
* Maximum map length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxMapLength: number; | ||
/** | ||
* Maximum extension length. | ||
* | ||
* Defaults to 4_294_967_295 (UINT32_MAX). | ||
*/ | ||
maxExtLength: number; | ||
/** | ||
* An object key decoder. Defaults to the shared instance of {@link CachedKeyDecoder}. | ||
* `null` is a special value to disable the use of the key decoder at all. | ||
*/ | ||
keyDecoder: KeyDecoder | null; | ||
}> | ||
> & | ||
ContextOf<ContextType>; | ||
const STATE_ARRAY = "array"; | ||
const STATE_MAP_KEY = "map_key"; | ||
const STATE_MAP_VALUE = "map_value"; | ||
type MapKeyType = string | number; | ||
const isValidMapKeyType = (key: unknown): key is MapKeyType => { | ||
const keyType = typeof key; | ||
return keyType === "string" || keyType === "number"; | ||
return typeof key === "string" || typeof key === "number"; | ||
}; | ||
type StackMapState = { | ||
type: State.MAP_KEY | State.MAP_VALUE; | ||
type: typeof STATE_MAP_KEY | typeof STATE_MAP_VALUE; | ||
size: number; | ||
@@ -32,3 +83,3 @@ key: MapKeyType | null; | ||
type StackArrayState = { | ||
type: State.ARRAY; | ||
type: typeof STATE_ARRAY; | ||
size: number; | ||
@@ -46,14 +97,12 @@ array: Array<unknown>; | ||
// IE11: Hack to support IE11. | ||
// IE11: Drop this hack and just use RangeError when IE11 is obsolete. | ||
export const DataViewIndexOutOfBoundsError: typeof Error = (() => { | ||
try { | ||
// IE11: The spec says it should throw RangeError, | ||
// IE11: but in IE11 it throws TypeError. | ||
EMPTY_VIEW.getInt8(0); | ||
} catch (e: any) { | ||
return e.constructor; | ||
try { | ||
// IE11: The spec says it should throw RangeError, | ||
// IE11: but in IE11 it throws TypeError. | ||
EMPTY_VIEW.getInt8(0); | ||
} catch (e) { | ||
if (!(e instanceof RangeError)) { | ||
throw new Error("This module is not supported in the current JavaScript engine because DataView does not throw RangeError on out-of-bounds access"); | ||
} | ||
throw new Error("never reached"); | ||
})(); | ||
} | ||
export const DataViewIndexOutOfBoundsError = RangeError; | ||
@@ -65,2 +114,12 @@ const MORE_DATA = new DataViewIndexOutOfBoundsError("Insufficient data"); | ||
export class Decoder<ContextType = undefined> { | ||
private readonly extensionCodec: ExtensionCodecType<ContextType>; | ||
private readonly context: ContextType; | ||
private readonly useBigInt64: boolean; | ||
private readonly maxStrLength: number; | ||
private readonly maxBinLength: number; | ||
private readonly maxArrayLength: number; | ||
private readonly maxMapLength: number; | ||
private readonly maxExtLength: number; | ||
private readonly keyDecoder: KeyDecoder | null; | ||
private totalPos = 0; | ||
@@ -74,13 +133,15 @@ private pos = 0; | ||
public constructor( | ||
private readonly extensionCodec: ExtensionCodecType<ContextType> = ExtensionCodec.defaultCodec as any, | ||
private readonly context: ContextType = undefined as any, | ||
private readonly maxStrLength = UINT32_MAX, | ||
private readonly maxBinLength = UINT32_MAX, | ||
private readonly maxArrayLength = UINT32_MAX, | ||
private readonly maxMapLength = UINT32_MAX, | ||
private readonly maxExtLength = UINT32_MAX, | ||
private readonly keyDecoder: KeyDecoder | null = sharedCachedKeyDecoder, | ||
) {} | ||
public constructor(options?: DecoderOptions<ContextType>) { | ||
this.extensionCodec = options?.extensionCodec ?? (ExtensionCodec.defaultCodec as ExtensionCodecType<ContextType>); | ||
this.context = (options as { context: ContextType } | undefined)?.context as ContextType; // needs a type assertion because EncoderOptions has no context property when ContextType is undefined | ||
this.useBigInt64 = options?.useBigInt64 ?? false; | ||
this.maxStrLength = options?.maxStrLength ?? UINT32_MAX; | ||
this.maxBinLength = options?.maxBinLength ?? UINT32_MAX; | ||
this.maxArrayLength = options?.maxArrayLength ?? UINT32_MAX; | ||
this.maxMapLength = options?.maxMapLength ?? UINT32_MAX; | ||
this.maxExtLength = options?.maxExtLength ?? UINT32_MAX; | ||
this.keyDecoder = (options?.keyDecoder !== undefined) ? options.keyDecoder : sharedCachedKeyDecoder; | ||
} | ||
private reinitializeState() { | ||
@@ -290,3 +351,7 @@ this.totalPos = 0; | ||
// uint 64 | ||
object = this.readU64(); | ||
if (this.useBigInt64) { | ||
object = this.readU64AsBigInt(); | ||
} else { | ||
object = this.readU64(); | ||
} | ||
} else if (headByte === 0xd0) { | ||
@@ -303,3 +368,7 @@ // int 8 | ||
// int 64 | ||
object = this.readI64(); | ||
if (this.useBigInt64) { | ||
object = this.readI64AsBigInt(); | ||
} else { | ||
object = this.readI64(); | ||
} | ||
} else if (headByte === 0xd9) { | ||
@@ -406,3 +475,3 @@ // str 8 | ||
const state = stack[stack.length - 1]!; | ||
if (state.type === State.ARRAY) { | ||
if (state.type === STATE_ARRAY) { | ||
state.array[state.position] = object; | ||
@@ -416,3 +485,3 @@ state.position++; | ||
} | ||
} else if (state.type === State.MAP_KEY) { | ||
} else if (state.type === STATE_MAP_KEY) { | ||
if (!isValidMapKeyType(object)) { | ||
@@ -426,3 +495,3 @@ throw new DecodeError("The type of key must be string or number but " + typeof object); | ||
state.key = object; | ||
state.type = State.MAP_VALUE; | ||
state.type = STATE_MAP_VALUE; | ||
continue DECODE; | ||
@@ -440,3 +509,3 @@ } else { | ||
state.key = null; | ||
state.type = State.MAP_KEY; | ||
state.type = STATE_MAP_KEY; | ||
continue DECODE; | ||
@@ -488,3 +557,3 @@ } | ||
this.stack.push({ | ||
type: State.MAP_KEY, | ||
type: STATE_MAP_KEY, | ||
size, | ||
@@ -503,3 +572,3 @@ key: null, | ||
this.stack.push({ | ||
type: State.ARRAY, | ||
type: STATE_ARRAY, | ||
size, | ||
@@ -526,6 +595,4 @@ array: new Array<unknown>(size), | ||
object = this.keyDecoder.decode(this.bytes, offset, byteLength); | ||
} else if (byteLength > TEXT_DECODER_THRESHOLD) { | ||
object = utf8DecodeTD(this.bytes, offset, byteLength); | ||
} else { | ||
object = utf8DecodeJs(this.bytes, offset, byteLength); | ||
object = utf8Decode(this.bytes, offset, byteLength); | ||
} | ||
@@ -539,3 +606,3 @@ this.pos += headerOffset + byteLength; | ||
const state = this.stack[this.stack.length - 1]!; | ||
return state.type === State.MAP_KEY; | ||
return state.type === STATE_MAP_KEY; | ||
} | ||
@@ -630,2 +697,14 @@ return false; | ||
private readU64AsBigInt(): bigint { | ||
const value = this.view.getBigUint64(this.pos); | ||
this.pos += 8; | ||
return value; | ||
} | ||
private readI64AsBigInt(): bigint { | ||
const value = this.view.getBigInt64(this.pos); | ||
this.pos += 8; | ||
return value; | ||
} | ||
private readF32() { | ||
@@ -632,0 +711,0 @@ const value = this.view.getFloat32(this.pos); |
import { Encoder } from "./Encoder"; | ||
import type { ExtensionCodecType } from "./ExtensionCodec"; | ||
import type { ContextOf, SplitUndefined } from "./context"; | ||
import type { EncoderOptions } from "./Encoder"; | ||
import type { SplitUndefined } from "./context"; | ||
export type EncodeOptions<ContextType = undefined> = Partial< | ||
Readonly<{ | ||
extensionCodec: ExtensionCodecType<ContextType>; | ||
/** | ||
* @deprecated Use {@link EncoderOptions} instead. | ||
*/ | ||
export type EncodeOptions = never; | ||
/** | ||
* The maximum depth in nested objects and arrays. | ||
* | ||
* Defaults to 100. | ||
*/ | ||
maxDepth: number; | ||
/** | ||
* @deprecated No longer supported. | ||
*/ | ||
export const defaultEncodeOptions: never = undefined as never; | ||
/** | ||
* The initial size of the internal buffer. | ||
* | ||
* Defaults to 2048. | ||
*/ | ||
initialBufferSize: number; | ||
/** | ||
* If `true`, the keys of an object is sorted. In other words, the encoded | ||
* binary is canonical and thus comparable to another encoded binary. | ||
* | ||
* Defaults to `false`. If enabled, it spends more time in encoding objects. | ||
*/ | ||
sortKeys: boolean; | ||
/** | ||
* If `true`, non-integer numbers are encoded in float32, not in float64 (the default). | ||
* | ||
* Only use it if precisions don't matter. | ||
* | ||
* Defaults to `false`. | ||
*/ | ||
forceFloat32: boolean; | ||
/** | ||
* If `true`, an object property with `undefined` value are ignored. | ||
* e.g. `{ foo: undefined }` will be encoded as `{}`, as `JSON.stringify()` does. | ||
* | ||
* Defaults to `false`. If enabled, it spends more time in encoding objects. | ||
*/ | ||
ignoreUndefined: boolean; | ||
/** | ||
* If `true`, integer numbers are encoded as floating point numbers, | ||
* with the `forceFloat32` option taken into account. | ||
* | ||
* Defaults to `false`. | ||
*/ | ||
forceIntegerToFloat: boolean; | ||
}> | ||
> & | ||
ContextOf<ContextType>; | ||
const defaultEncodeOptions: EncodeOptions = {}; | ||
/** | ||
@@ -68,15 +23,6 @@ * It encodes `value` in the MessagePack format and | ||
value: unknown, | ||
options: EncodeOptions<SplitUndefined<ContextType>> = defaultEncodeOptions as any, | ||
options?: EncoderOptions<SplitUndefined<ContextType>>, | ||
): Uint8Array { | ||
const encoder = new Encoder( | ||
options.extensionCodec, | ||
(options as typeof options & { context: any }).context, | ||
options.maxDepth, | ||
options.initialBufferSize, | ||
options.sortKeys, | ||
options.forceFloat32, | ||
options.ignoreUndefined, | ||
options.forceIntegerToFloat, | ||
); | ||
const encoder = new Encoder(options); | ||
return encoder.encodeSharedRef(value); | ||
} |
@@ -1,2 +0,2 @@ | ||
import { utf8EncodeJs, utf8Count, TEXT_ENCODER_THRESHOLD, utf8EncodeTE } from "./utils/utf8"; | ||
import { utf8Count, utf8Encode } from "./utils/utf8"; | ||
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec"; | ||
@@ -6,22 +6,102 @@ import { setInt64, setUint64 } from "./utils/int"; | ||
import type { ExtData } from "./ExtData"; | ||
import type { ContextOf, SplitUndefined } from "./context"; | ||
export const DEFAULT_MAX_DEPTH = 100; | ||
export const DEFAULT_INITIAL_BUFFER_SIZE = 2048; | ||
export type EncoderOptions<ContextType = undefined> = Partial< | ||
Readonly<{ | ||
extensionCodec: ExtensionCodecType<ContextType>; | ||
/** | ||
* Encodes bigint as Int64 or Uint64 if it's set to true. | ||
* {@link forceIntegerToFloat} does not affect bigint. | ||
* Depends on ES2020's {@link DataView#setBigInt64} and | ||
* {@link DataView#setBigUint64}. | ||
* | ||
* Defaults to false. | ||
*/ | ||
useBigInt64: boolean; | ||
/** | ||
* The maximum depth in nested objects and arrays. | ||
* | ||
* Defaults to 100. | ||
*/ | ||
maxDepth: number; | ||
/** | ||
* The initial size of the internal buffer. | ||
* | ||
* Defaults to 2048. | ||
*/ | ||
initialBufferSize: number; | ||
/** | ||
* If `true`, the keys of an object is sorted. In other words, the encoded | ||
* binary is canonical and thus comparable to another encoded binary. | ||
* | ||
* Defaults to `false`. If enabled, it spends more time in encoding objects. | ||
*/ | ||
sortKeys: boolean; | ||
/** | ||
* If `true`, non-integer numbers are encoded in float32, not in float64 (the default). | ||
* | ||
* Only use it if precisions don't matter. | ||
* | ||
* Defaults to `false`. | ||
*/ | ||
forceFloat32: boolean; | ||
/** | ||
* If `true`, an object property with `undefined` value are ignored. | ||
* e.g. `{ foo: undefined }` will be encoded as `{}`, as `JSON.stringify()` does. | ||
* | ||
* Defaults to `false`. If enabled, it spends more time in encoding objects. | ||
*/ | ||
ignoreUndefined: boolean; | ||
/** | ||
* If `true`, integer numbers are encoded as floating point numbers, | ||
* with the `forceFloat32` option taken into account. | ||
* | ||
* Defaults to `false`. | ||
*/ | ||
forceIntegerToFloat: boolean; | ||
}> | ||
> & ContextOf<ContextType>; | ||
export class Encoder<ContextType = undefined> { | ||
private pos = 0; | ||
private view = new DataView(new ArrayBuffer(this.initialBufferSize)); | ||
private bytes = new Uint8Array(this.view.buffer); | ||
private readonly extensionCodec: ExtensionCodecType<ContextType>; | ||
private readonly context: ContextType; | ||
private readonly useBigInt64: boolean; | ||
private readonly maxDepth: number; | ||
private readonly initialBufferSize: number; | ||
private readonly sortKeys: boolean; | ||
private readonly forceFloat32: boolean; | ||
private readonly ignoreUndefined: boolean; | ||
private readonly forceIntegerToFloat: boolean; | ||
public constructor( | ||
private readonly extensionCodec: ExtensionCodecType<ContextType> = ExtensionCodec.defaultCodec as any, | ||
private readonly context: ContextType = undefined as any, | ||
private readonly maxDepth = DEFAULT_MAX_DEPTH, | ||
private readonly initialBufferSize = DEFAULT_INITIAL_BUFFER_SIZE, | ||
private readonly sortKeys = false, | ||
private readonly forceFloat32 = false, | ||
private readonly ignoreUndefined = false, | ||
private readonly forceIntegerToFloat = false, | ||
) {} | ||
private pos: number; | ||
private view: DataView; | ||
private bytes: Uint8Array; | ||
public constructor(options?: EncoderOptions<ContextType>) { | ||
this.extensionCodec = options?.extensionCodec ?? (ExtensionCodec.defaultCodec as ExtensionCodecType<ContextType>); | ||
this.context = (options as { context: ContextType } | undefined)?.context as ContextType; // needs a type assertion because EncoderOptions has no context property when ContextType is undefined | ||
this.useBigInt64 = options?.useBigInt64 ?? false; | ||
this.maxDepth = options?.maxDepth ?? DEFAULT_MAX_DEPTH; | ||
this.initialBufferSize = options?.initialBufferSize ?? DEFAULT_INITIAL_BUFFER_SIZE; | ||
this.sortKeys = options?.sortKeys ?? false; | ||
this.forceFloat32 = options?.forceFloat32 ?? false; | ||
this.ignoreUndefined = options?.ignoreUndefined ?? false; | ||
this.forceIntegerToFloat = options?.forceIntegerToFloat ?? false; | ||
this.pos = 0; | ||
this.view = new DataView(new ArrayBuffer(this.initialBufferSize)); | ||
this.bytes = new Uint8Array(this.view.buffer); | ||
} | ||
private reinitializeState() { | ||
@@ -61,5 +141,11 @@ this.pos = 0; | ||
} else if (typeof object === "number") { | ||
this.encodeNumber(object); | ||
if (!this.forceIntegerToFloat) { | ||
this.encodeNumber(object); | ||
} else { | ||
this.encodeNumberAsFloat(object); | ||
} | ||
} else if (typeof object === "string") { | ||
this.encodeString(object); | ||
} else if (this.useBigInt64 && typeof object === "bigint") { | ||
this.encodeBigInt64(object); | ||
} else { | ||
@@ -100,4 +186,5 @@ this.encodeObject(object, depth); | ||
} | ||
private encodeNumber(object: number) { | ||
if (Number.isSafeInteger(object) && !this.forceIntegerToFloat) { | ||
private encodeNumber(object: number): void { | ||
if (!this.forceIntegerToFloat && Number.isSafeInteger(object)) { | ||
if (object >= 0) { | ||
@@ -119,6 +206,8 @@ if (object < 0x80) { | ||
this.writeU32(object); | ||
} else { | ||
} else if (!this.useBigInt64) { | ||
// uint 64 | ||
this.writeU8(0xcf); | ||
this.writeU64(object); | ||
} else { | ||
this.encodeNumberAsFloat(object); | ||
} | ||
@@ -141,22 +230,39 @@ } else { | ||
this.writeI32(object); | ||
} else { | ||
} else if (!this.useBigInt64) { | ||
// int 64 | ||
this.writeU8(0xd3); | ||
this.writeI64(object); | ||
} else { | ||
this.encodeNumberAsFloat(object); | ||
} | ||
} | ||
} else { | ||
// non-integer numbers | ||
if (this.forceFloat32) { | ||
// float 32 | ||
this.writeU8(0xca); | ||
this.writeF32(object); | ||
} else { | ||
// float 64 | ||
this.writeU8(0xcb); | ||
this.writeF64(object); | ||
} | ||
this.encodeNumberAsFloat(object); | ||
} | ||
} | ||
private encodeNumberAsFloat(object: number): void { | ||
if (this.forceFloat32) { | ||
// float 32 | ||
this.writeU8(0xca); | ||
this.writeF32(object); | ||
} else { | ||
// float 64 | ||
this.writeU8(0xcb); | ||
this.writeF64(object); | ||
} | ||
} | ||
private encodeBigInt64(object: bigint): void { | ||
if (object >= BigInt(0)) { | ||
// uint 64 | ||
this.writeU8(0xcf); | ||
this.writeBigUint64(object); | ||
} else { | ||
// int 64 | ||
this.writeU8(0xd3); | ||
this.writeBigInt64(object); | ||
} | ||
} | ||
private writeStringHeader(byteLength: number) { | ||
@@ -185,17 +291,8 @@ if (byteLength < 32) { | ||
const maxHeaderSize = 1 + 4; | ||
const strLength = object.length; | ||
if (strLength > TEXT_ENCODER_THRESHOLD) { | ||
const byteLength = utf8Count(object); | ||
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength); | ||
this.writeStringHeader(byteLength); | ||
utf8EncodeTE(object, this.bytes, this.pos); | ||
this.pos += byteLength; | ||
} else { | ||
const byteLength = utf8Count(object); | ||
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength); | ||
this.writeStringHeader(byteLength); | ||
utf8EncodeJs(object, this.bytes, this.pos); | ||
this.pos += byteLength; | ||
} | ||
const byteLength = utf8Count(object); | ||
this.ensureBufferSizeToWrite(maxHeaderSize + byteLength); | ||
this.writeStringHeader(byteLength); | ||
utf8Encode(object, this.bytes, this.pos); | ||
this.pos += byteLength; | ||
} | ||
@@ -395,2 +492,3 @@ | ||
this.ensureBufferSizeToWrite(4); | ||
this.view.setFloat32(this.pos, value); | ||
@@ -402,2 +500,3 @@ this.pos += 4; | ||
this.ensureBufferSizeToWrite(8); | ||
this.view.setFloat64(this.pos, value); | ||
@@ -420,2 +519,16 @@ this.pos += 8; | ||
} | ||
private writeBigUint64(value: bigint) { | ||
this.ensureBufferSizeToWrite(8); | ||
this.view.setBigUint64(this.pos, value); | ||
this.pos += 8; | ||
} | ||
private writeBigInt64(value: bigint) { | ||
this.ensureBufferSizeToWrite(8); | ||
this.view.setBigInt64(this.pos, value); | ||
this.pos += 8; | ||
} | ||
} |
@@ -17,9 +17,14 @@ // Main Functions: | ||
import { Decoder, DataViewIndexOutOfBoundsError } from "./Decoder"; | ||
export { Decoder, DataViewIndexOutOfBoundsError }; | ||
import type { DecoderOptions } from "./Decoder"; | ||
export type { DecoderOptions }; | ||
import { DecodeError } from "./DecodeError"; | ||
export { Decoder, DecodeError, DataViewIndexOutOfBoundsError }; | ||
export { DecodeError }; | ||
import { Encoder } from "./Encoder"; | ||
export { Encoder }; | ||
import type { EncoderOptions } from "./Encoder"; | ||
export type { EncoderOptions }; | ||
// Utilitiies for Extension Types: | ||
// Utilities for Extension Types: | ||
@@ -26,0 +31,0 @@ import { ExtensionCodec } from "./ExtensionCodec"; |
@@ -1,9 +0,2 @@ | ||
/* eslint-disable @typescript-eslint/no-unnecessary-condition */ | ||
import { UINT32_MAX } from "./int"; | ||
const TEXT_ENCODING_AVAILABLE = | ||
(typeof process === "undefined" || process?.env?.["TEXT_ENCODING"] !== "never") && | ||
typeof TextEncoder !== "undefined" && | ||
typeof TextDecoder !== "undefined"; | ||
export function utf8Count(str: string): number { | ||
@@ -92,19 +85,27 @@ const strLength = str.length; | ||
const sharedTextEncoder = TEXT_ENCODING_AVAILABLE ? new TextEncoder() : undefined; | ||
export const TEXT_ENCODER_THRESHOLD = !TEXT_ENCODING_AVAILABLE | ||
? UINT32_MAX | ||
: typeof process !== "undefined" && process?.env?.["TEXT_ENCODING"] !== "force" | ||
? 200 | ||
: 0; | ||
// TextEncoder and TextDecoder are standardized in whatwg encoding: | ||
// https://encoding.spec.whatwg.org/ | ||
// and available in all the modern browsers: | ||
// https://caniuse.com/textencoder | ||
// They are available in Node.js since v12 LTS as well: | ||
// https://nodejs.org/api/globals.html#textencoder | ||
function utf8EncodeTEencode(str: string, output: Uint8Array, outputOffset: number): void { | ||
output.set(sharedTextEncoder!.encode(str), outputOffset); | ||
const sharedTextEncoder = new TextEncoder(); | ||
// This threshold should be determined by benchmarking, which might vary in engines and input data. | ||
// Run `npx ts-node benchmark/encode-string.ts` for details. | ||
const TEXT_ENCODER_THRESHOLD = 50; | ||
export function utf8EncodeTE(str: string, output: Uint8Array, outputOffset: number): void { | ||
sharedTextEncoder.encodeInto(str, output.subarray(outputOffset)); | ||
} | ||
function utf8EncodeTEencodeInto(str: string, output: Uint8Array, outputOffset: number): void { | ||
sharedTextEncoder!.encodeInto(str, output.subarray(outputOffset)); | ||
export function utf8Encode(str: string, output: Uint8Array, outputOffset: number): void { | ||
if (str.length > TEXT_ENCODER_THRESHOLD) { | ||
utf8EncodeTE(str, output, outputOffset); | ||
} else { | ||
utf8EncodeJs(str, output, outputOffset); | ||
} | ||
} | ||
export const utf8EncodeTE = sharedTextEncoder?.encodeInto ? utf8EncodeTEencodeInto : utf8EncodeTEencode; | ||
const CHUNK_SIZE = 0x1_000; | ||
@@ -161,12 +162,19 @@ | ||
const sharedTextDecoder = TEXT_ENCODING_AVAILABLE ? new TextDecoder() : null; | ||
export const TEXT_DECODER_THRESHOLD = !TEXT_ENCODING_AVAILABLE | ||
? UINT32_MAX | ||
: typeof process !== "undefined" && process?.env?.["TEXT_DECODER"] !== "force" | ||
? 200 | ||
: 0; | ||
const sharedTextDecoder = new TextDecoder(); | ||
// This threshold should be determined by benchmarking, which might vary in engines and input data. | ||
// Run `npx ts-node benchmark/decode-string.ts` for details. | ||
const TEXT_DECODER_THRESHOLD = 200; | ||
export function utf8DecodeTD(bytes: Uint8Array, inputOffset: number, byteLength: number): string { | ||
const stringBytes = bytes.subarray(inputOffset, inputOffset + byteLength); | ||
return sharedTextDecoder!.decode(stringBytes); | ||
return sharedTextDecoder.decode(stringBytes); | ||
} | ||
export function utf8Decode(bytes: Uint8Array, inputOffset: number, byteLength: number): string { | ||
if (byteLength > TEXT_DECODER_THRESHOLD) { | ||
return utf8DecodeTD(bytes, inputOffset, byteLength); | ||
} else { | ||
return utf8DecodeJs(bytes, inputOffset, byteLength); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
660045
32
7963
0
1