@ygoe/msgpack
Version:
A minimal yet complete MessagePack implementation for JavaScript
2 lines • 7.4 kB
JavaScript
!function(){"use strict";function e(e,t){if(t&&t.multiple&&!Array.isArray(e))throw new Error("Invalid argument type: Expected an Array to serialize multiple values.");const r=4294967296;let n,i,o=new Uint8Array(128),f=0;if(t&&t.multiple)for(let t=0;t<e.length;t++)a(e[t]);else a(e);return o.subarray(0,f);function a(e,o){switch(typeof e){case"undefined":l(e);break;case"boolean":!function(e){c(e?195:194)}(e);break;case"number":!function(e){if(isFinite(e)&&Math.floor(e)===e)if(e>=0&&e<=127)c(e);else if(e<0&&e>=-32)c(e);else if(e>0&&e<=255)s([204,e]);else if(e>=-128&&e<=127)s([208,e]);else if(e>0&&e<=65535)s([205,e>>>8,e]);else if(e>=-32768&&e<=32767)s([209,e>>>8,e]);else if(e>0&&e<=4294967295)s([206,e>>>24,e>>>16,e>>>8,e]);else if(e>=-2147483648&&e<=2147483647)s([210,e>>>24,e>>>16,e>>>8,e]);else if(e>0&&e<=0x10000000000000000){let t=e/r,n=e%r;s([211,t>>>24,t>>>16,t>>>8,t,n>>>24,n>>>16,n>>>8,n])}else e>=-0x8000000000000000&&e<=0x8000000000000000?(c(211),d(e)):s(e<0?[211,128,0,0,0,0,0,0,0]:[207,255,255,255,255,255,255,255,255]);else i||(n=new ArrayBuffer(8),i=new DataView(n)),i.setFloat64(0,e),c(203),s(new Uint8Array(n))}(e);break;case"string":!function(e){let t=function(e){let t=!0,r=e.length;for(let n=0;n<r;n++)if(e.charCodeAt(n)>127){t=!1;break}let n=0,i=new Uint8Array(e.length*(t?1:4));for(let t=0;t!==r;t++){let o=e.charCodeAt(t);if(o<128)i[n++]=o;else{if(o<2048)i[n++]=o>>6|192;else{if(o>55295&&o<56320){if(++t>=r)throw new Error("UTF-8 encode: incomplete surrogate pair");let f=e.charCodeAt(t);if(f<56320||f>57343)throw new Error("UTF-8 encode: second surrogate character 0x"+f.toString(16)+" at index "+t+" out of range");o=65536+((1023&o)<<10)+(1023&f),i[n++]=o>>18|240,i[n++]=o>>12&63|128}else i[n++]=o>>12|224;i[n++]=o>>6&63|128}i[n++]=63&o|128}}return t?i:i.subarray(0,n)}(e),r=t.length;r<=31?c(160+r):s(r<=255?[217,r]:r<=65535?[218,r>>>8,r]:[219,r>>>24,r>>>16,r>>>8,r]);s(t)}(e);break;case"object":null===e?l(e):e instanceof Date?function(e){let t=e.getTime()/1e3;if(0===e.getMilliseconds()&&t>=0&&t<4294967296)s([214,255,t>>>24,t>>>16,t>>>8,t]);else if(t>=0&&t<17179869184){let n=1e6*e.getMilliseconds();s([215,255,n>>>22,n>>>14,n>>>6,n<<2>>>0|t/r,t>>>24,t>>>16,t>>>8,t])}else{let r=1e6*e.getMilliseconds();s([199,12,255,r>>>24,r>>>16,r>>>8,r]),d(t)}}(e):Array.isArray(e)?u(e):e instanceof Uint8Array||e instanceof Uint8ClampedArray?function(e){let t=e.length;s(t<=15?[196,t]:t<=65535?[197,t>>>8,t]:[198,t>>>24,t>>>16,t>>>8,t]);s(e)}(e):e instanceof Int8Array||e instanceof Int16Array||e instanceof Uint16Array||e instanceof Int32Array||e instanceof Uint32Array||e instanceof Float32Array||e instanceof Float64Array?u(e):function(e){let t=0;for(let r in e)void 0!==e[r]&&t++;t<=15?c(128+t):s(t<=65535?[222,t>>>8,t]:[223,t>>>24,t>>>16,t>>>8,t]);for(let t in e){let r=e[t];void 0!==r&&(a(t),a(r))}}(e);break;default:if(o||!t||!t.invalidTypeReplacement)throw new Error("Invalid argument type: The type '"+typeof e+"' cannot be serialized.");"function"==typeof t.invalidTypeReplacement?a(t.invalidTypeReplacement(e),!0):a(t.invalidTypeReplacement,!0)}}function l(e){c(192)}function u(e){let t=e.length;t<=15?c(144+t):s(t<=65535?[220,t>>>8,t]:[221,t>>>24,t>>>16,t>>>8,t]);for(let r=0;r<t;r++)a(e[r])}function c(e){if(o.length<f+1){let e=2*o.length;for(;e<f+1;)e*=2;let t=new Uint8Array(e);t.set(o),o=t}o[f]=e,f++}function s(e){if(o.length<f+e.length){let t=2*o.length;for(;t<f+e.length;)t*=2;let r=new Uint8Array(t);r.set(o),o=r}o.set(e,f),f+=e.length}function d(e){let t,n;e>=0?(t=e/r,n=e%r):(e++,t=~(t=Math.abs(e)/r),n=~(n=Math.abs(e)%r)),s([t>>>24,t>>>16,t>>>8,t,n>>>24,n>>>16,n>>>8,n])}}function t(e,t){const r=4294967296;let n,i=0;if(e instanceof ArrayBuffer&&(e=new Uint8Array(e)),"object"!=typeof e||void 0===e.length)throw new Error("Invalid argument type: Expected a byte array (Array or Uint8Array) to deserialize.");if(!e.length)throw new Error("Invalid argument: The byte array to deserialize is empty.");if(e instanceof Uint8Array||(e=new Uint8Array(e)),t&&t.multiple)for(n=[];i<e.length;)n.push(o());else n=o();return n;function o(){const t=e[i++];if(t>=0&&t<=127)return t;if(t>=128&&t<=143)return c(t-128);if(t>=144&&t<=159)return s(t-144);if(t>=160&&t<=191)return d(t-160);if(192===t)return null;if(193===t)throw new Error("Invalid byte code 0xc1 found.");if(194===t)return!1;if(195===t)return!0;if(196===t)return u(-1,1);if(197===t)return u(-1,2);if(198===t)return u(-1,4);if(199===t)return y(-1,1);if(200===t)return y(-1,2);if(201===t)return y(-1,4);if(202===t)return l(4);if(203===t)return l(8);if(204===t)return a(1);if(205===t)return a(2);if(206===t)return a(4);if(207===t)return a(8);if(208===t)return f(1);if(209===t)return f(2);if(210===t)return f(4);if(211===t)return f(8);if(212===t)return y(1);if(213===t)return y(2);if(214===t)return y(4);if(215===t)return y(8);if(216===t)return y(16);if(217===t)return d(-1,1);if(218===t)return d(-1,2);if(219===t)return d(-1,4);if(220===t)return s(-1,2);if(221===t)return s(-1,4);if(222===t)return c(-1,2);if(223===t)return c(-1,4);if(t>=224&&t<=255)return t-256;throw console.debug("msgpack array:",e),new Error("Invalid byte value '"+t+"' at index "+(i-1)+" in the MessagePack binary data (length "+e.length+"): Expecting a range of 0 to 255. This is not a byte array.")}function f(t){let r=0,n=!0;for(;t-- >0;)if(n){let t=e[i++];r+=127&t,128&t&&(r-=128),n=!1}else r*=256,r+=e[i++];return r}function a(t){let r=0;for(;t-- >0;)r*=256,r+=e[i++];return r}function l(t){let r=new DataView(e.buffer,i+e.byteOffset,t);return i+=t,4===t?r.getFloat32(0,!1):8===t?r.getFloat64(0,!1):void 0}function u(t,r){t<0&&(t=a(r));let n=e.subarray(i,i+t);return i+=t,n}function c(e,t){e<0&&(e=a(t));let r={};for(;e-- >0;){r[o()]=o()}return r}function s(e,t){e<0&&(e=a(t));let r=[];for(;e-- >0;)r.push(o());return r}function d(t,r){t<0&&(t=a(r));let n=i;return i+=t,function(e,t,r){let n=t,i="";r+=t;for(;n<r;){let t=e[n++];if(t>127)if(t>191&&t<224){if(n>=r)throw new Error("UTF-8 decode: incomplete 2-byte sequence");t=(31&t)<<6|63&e[n++]}else if(t>223&&t<240){if(n+1>=r)throw new Error("UTF-8 decode: incomplete 3-byte sequence");t=(15&t)<<12|(63&e[n++])<<6|63&e[n++]}else{if(!(t>239&&t<248))throw new Error("UTF-8 decode: unknown multibyte start 0x"+t.toString(16)+" at index "+(n-1));if(n+2>=r)throw new Error("UTF-8 decode: incomplete 4-byte sequence");t=(7&t)<<18|(63&e[n++])<<12|(63&e[n++])<<6|63&e[n++]}if(t<=65535)i+=String.fromCharCode(t);else{if(!(t<=1114111))throw new Error("UTF-8 decode: code point 0x"+t.toString(16)+" exceeds UTF-16 reach");t-=65536,i+=String.fromCharCode(t>>10|55296),i+=String.fromCharCode(1023&t|56320)}}return i}(e,n,t)}function y(e,t){e<0&&(e=a(t));let n=a(1),o=u(e);switch(n){case 255:return function(e){if(4===e.length){let t=(e[0]<<24>>>0)+(e[1]<<16>>>0)+(e[2]<<8>>>0)+e[3];return new Date(1e3*t)}if(8===e.length){let t=(e[0]<<22>>>0)+(e[1]<<14>>>0)+(e[2]<<6>>>0)+(e[3]>>>2),n=(3&e[3])*r+(e[4]<<24>>>0)+(e[5]<<16>>>0)+(e[6]<<8>>>0)+e[7];return new Date(1e3*n+t/1e6)}if(12===e.length){let t=(e[0]<<24>>>0)+(e[1]<<16>>>0)+(e[2]<<8>>>0)+e[3];i-=8;let r=f(8);return new Date(1e3*r+t/1e6)}throw new Error("Invalid data length for a date value.")}(o)}return{type:n,data:o}}}let r={serialize:e,deserialize:t,encode:e,decode:t};"object"==typeof module&&module&&"object"==typeof module.exports?module.exports=r:window[window.msgpackJsName||"msgpack"]=r}();
//# sourceMappingURL=msgpack.min.js.map