-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-buffer.min.js
More file actions
1 lines (1 loc) · 12.8 KB
/
string-buffer.min.js
File metadata and controls
1 lines (1 loc) · 12.8 KB
1
(function(t){function r(t,r){switch(r){case U:(t%1||-128>t||t>=128)&&e(r);break;case v:(t%1||0>t||t>=256)&&e(r);break;case A:(t%1||-32768>t||t>=32768)&&e(r);break;case w:(t%1||0>t||t>=65536)&&e(r);break;case B:(t%1||t<-Math.pow(2,31)||t>=Math.pow(2,31))&&e(r);break;case y:(t%1||0>t||t>=Math.pow(2,32))&&e(r);break;case I:var n,f,i=t;i=Math.abs(i),f=Math.log(i)/Math.log(2),f=0>f?Math.ceil(f):Math.floor(f),n=103+f,(0>n||n>255)&&e(r),i*=Math.pow(2,-f+24),i%1&&e(r);break;case E:break;case p:"string"!=typeof t&&e(r)}}function e(t){throw Error("Type assertion failure, check that your value is actually of type "+n(t)+".")}function n(t){return t===(d|v)?"char":t===v?"unsigned char":t===(d|w)?"short":t===w?"unsigned short":t===(d|y)?"int":t===y?"unsigned int":t===I?"float":t===E?"double":t===p?"string":void 0}function f(t){if(!(this instanceof f))return new f(t);if(this.buffer="","string"==typeof t)this.write(t,0);else if(Array.isArray(t))for(var r=0;r<t.length;++r)this.writeUInt8(t[r],r);else f.isStringBuffer(t)&&(this.buffer=t.buffer)}function h(t){var r="";if(t.length<2)for(var e=t.length;2>e;++e)r+="0";return r+=t}function u(t){var r=(128&t[0])>>>7,e=((127&t[0])<<1)+((128&t[1])>>>7),n=0;for(t[1]&=127,i=0;i<=2;++i)n+=t[i+1]<<8*(2-i);return n|=8388608,o(r?-n:n,e-150)}function s(t){var r=(128&t[0])>>>7,e=((127&t[0])<<4)+((240&t[1])>>>4),n=0;for(t[1]&=15,i=0;i<=6;++i)n+=o(t[i+1],8*(6-i));return n+=4503599627370496,o(r?-n:n,e-1075)}function a(t,r){var e=[];if(r===v)return e.push(t),e;if(r===(d|v))return 0>t?a(l(-t,8),v):a(t,v);if(r===w)return e.push(t>>>8),e.push(255&t),e;if(r===(d|w))return 0>t?a(l(-t,16),w):a(t,w);if(r===y)return e.push(t>>>24&255),e.push(t>>>16&255),e.push(t>>>8&255),e.push(255&t),e;if(r===(d|y))return 0>t?a(l(-t,32),y):a(t,y);if(r===I){t=+t;var n,f,i=127,h=t;return n=0>h?1:0,h=Math.abs(h),f=Math.log(h)/Math.log(2),f=f>0?Math.floor(f):Math.ceil(f),h*=Math.pow(2,-f+23),i+=f,h=Math.round(h),h&=8388607,e.push(n<<7),e[0]+=(254&i)>>>1,e.push((1&i)<<7),e[1]+=h>>>16&127,e.push(h>>>8&255),e.push(255&h),e}if(r===E){t=+t;var n,f,i=1023,h=t;n=0>h?1:0,h=Math.abs(h),f=Math.log(h)/Math.log(2),f=f>0?Math.floor(f):Math.ceil(f),h*=Math.pow(2,-f+52),i+=f,h=Math.round(h),h=parseInt(h.toString(2).substr(1),2),e.push(n<<7),e[0]+=i>>>4,e.push((15&i)<<4),e[1]+=15&Math.floor(o(h,-48));for(var u=40,s=0;6>s;++s,u-=8)e.push(255&Math.floor(o(h,-u)));return e}}function o(t,r){return t*Math.pow(2,r)}function l(t,r){return r>31||!r?~t:(t^c(r))+1}function c(t){return(1<<t)-1}function b(t,r,e){for(var n=t.length-1;n>=0&&r.apply(e,[t[n],n,t])!==!1;n--);}function g(t,r,e){var n;if(e||(e=!1),0>t&&0===r.length)return 0;if(n=0>t?r.length+ +!e+t:t,0>n||n>r.length-+e)throw RangeError("Tried to "+(e?"read":"write")+" at index "+String(t)+" but it is out of range.");return n}var t=this,d=1,v=0,w=2,y=4,I=8,E=24,p=32,U=1,A=3,B=5;f.concat=function(t){return f(t.reduce(function(t,r){return t+r.buffer},""))},f.isStringBuffer=function(t){return t instanceof f},f.fromBlob=function(t,r){try{var e=f(),n=new FileReader;n.addEventListener("loadend",function(){for(var t=new Uint8Array(n.result),f=0;f<t.length;++f)e.writeUInt8(t[f],-1);r(null,e)}),n.readAsArrayBuffer(t)}catch(i){r(i)}},f.fromFile=function(t,r){return f.fromBlob(t.slice(),r)},f.fromBuffer=function(t){var r=f();try{if(!Buffer.isBuffer(t))throw TypeError("Must pass a buffer.");for(var e=0;e<t.length;++e)r.writeUInt8(t[e],-1);return r}catch(n){throw ReferenceError("Can only use fromBuffer in a Node.js environment.")}},f.fromTypedArray=function(t,r){var e=f();if(t instanceof Uint8Array||t instanceof Uint8ClampedArray)for(var n=0;n<t.length;++n)e.writeUInt8(t[n],-1);else if(t instanceof Int8Array)for(var n=0;n<t.length;++n)e.writeInt8(t[n],-1);else if(t instanceof Uint16Array)if(r)for(var n=0;n<t.length;++n)e.writeUInt16BE(t[n],-1);else for(var n=0;n<t.length;++n)e.writeUInt16LE(t[n],-1);else if(t instanceof Int16Array)if(r)for(var n=0;n<t.length;++n)e.writeInt16BE(t[n],-1);else for(var n=0;n<t.length;++n)e.writeInt16LE(t[n],-1);else if(t instanceof Uint32Array)if(r)for(var n=0;n<t.length;++n)e.writeUInt32BE(t[n],-1);else for(var n=0;n<t.length;++n)e.writeUInt32LE(t[n],-1);else if(t instanceof Int32Array)if(r)for(var n=0;n<t.length;++n)e.writeInt32BE(t[n],-1);else for(var n=0;n<t.length;++n)e.writeInt32LE(t[n],-1);else if(t instanceof Float32Array)if(r)for(var n=0;n<t.length;++n)e.writeFloatBE(t[n],-1);else for(var n=0;n<t.length;++n)e.writeFloatLE(t[n],-1);else{if(!(t instanceof Float64Array))throw TypeError("Must supply a typed array.");if(r)for(var n=0;n<t.length;++n)e.writeDoubleBE(t[n],-1);else for(var n=0;n<t.length;++n)e.writeDoubleLE(t[n],-1)}return e},f.byteLength=function(t,r){r||(r="ascii");var e;switch(t+="",r){case"ascii":case"binary":case"raw":e=t.length;break;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":e=2*t.length;break;case"hex":e=t.length>>>1;break;default:e=t.length}return e},f.INSPECT_MAX_BYTES=50,f.prototype={writeUInt8:function(t,e,n){return t=+t,n||r(t,v),e=g(e,this.buffer),e===this.buffer.length?this.buffer+=String.fromCharCode(t):this.buffer=this.buffer.substr(0,e)+String.fromCharCode(t)+this.buffer.substr(e+1),e+1},writeInt8:function(t,e,n){return t=+t,n||r(t,d|v),t=0>t?l(-t,8):t,e=g(e,this.buffer),e===this.buffer.length?this.buffer+=String.fromCharCode(t):this.buffer=this.buffer.substr(0,e)+String.fromCharCode(t)+this.buffer.substr(e+1),e+1},writeUInt16LE:function(t,e,n){t=+t,n||r(t,w);var f=a(t,w),i="";return b(f,function(t){i+=String.fromCharCode(t)}),e=g(e,this.buffer),e>=this.buffer.length||-1===e?this.buffer+=i:this.buffer=this.buffer.substr(0,e)+i+this.buffer.substr(e+2),e+2},writeUInt16BE:function(t,e,n){t=+t,n||r(t,w);var f=a(t,w),i="";return f.forEach(function(t){i+=String.fromCharCode(t)}),e=g(e,this.buffer),e>=this.buffer.length||-1===e?this.buffer+=i:this.buffer=this.buffer.substr(0,e)+i+this.buffer.substr(e+2),e+2},writeInt16LE:function(t,e,n){return t=+t,n||r(t,d|w),t=0>t?l(-t,16):t,0>e&&(e=this.buffer.length-(e+1)%this.buffer.length),this.writeUInt16LE(t,e)},writeInt16BE:function(t,e,n){return t=+t,n||r(t,d|w),t=0>t?l(-t,16):t,0>e&&(e=this.buffer.length-(e+1)%this.buffer.length),this.writeUInt16BE(t,e)},writeUInt32LE:function(t,e,n){t=+t;var f=a(t,y),i="";n||r(t,y),b(f,function(t){i+=String.fromCharCode(t)}),e=g(e,this.buffer),e===this.buffer.length||-1===e?this.buffer+=i:this.buffer=this.buffer.substr(0,e)+i+this.buffer.substr(e+4)},writeUInt32BE:function(t,e,n){t=+t,n||r(t,y);var f=a(t,y),i="";f.forEach(function(t){i+=String.fromCharCode(t)}),e=g(e,this.buffer),e===this.buffer.length||-1===e?this.buffer+=i:this.buffer=this.buffer.substr(0,e)+i+this.buffer.substr(e+4)},writeInt32LE:function(t,e,n){return t=+t,n||r(t,d|y),t=0>t?l(-t,32):t,this.writeUInt32LE(t,e)},writeInt32BE:function(t,e,n){return t=+t,n||r(t,d|y),t=0>t?l(-t,32):t,this.writeUInt32BE(t,e)},writeFloatLE:function(t,e,n){n||r(t,I);var f=a(t,I);e=g(e,this.buffer);for(var i=f.length-1;i>=0;--i)this.writeUInt8(f[i],e+(f.length-1-i));return e+4},writeFloatBE:function(t,e,n){n||r(t,I);var f=a(t,I);return e=g(e,this.buffer),f.forEach(function(t,r){this.writeUInt8(t,e+r)},this),e+4},writeDoubleLE:function(t,e,n){n||r(t,E);var f=a(t,E);for(e=g(e,this.buffer),i=f.length-1;i>=0;--i)this.writeUInt8(f[i],e+(f.length-1-i));return e+8},writeDoubleBE:function(t,e,n){n||r(t,E);var f=a(t,E);return e=g(e,this.buffer),f.forEach(function(t,r){this.writeUInt8(t,e+r)},this),e+8},readUInt8:function(t){return t=g(t,this.buffer,!0),this.buffer.charCodeAt(t)},readInt8:function(t){var r=this.buffer.charCodeAt(t);return 128&r?-l(r,8):r},readUInt16LE:function(t){return t=g(t,this.buffer,!0),this.buffer.charCodeAt(t)|this.buffer.charCodeAt(t+1)<<8},readUInt16BE:function(t){return t=g(t,this.buffer,!0),this.buffer.charCodeAt(t)<<8|this.buffer.charCodeAt(t+1)},readInt16LE:function(t){var r=this.readUInt16LE(t);return 32768&r?-l(r,16):r},readInt16BE:function(t){var r=this.readUInt16BE(t);return 32768&r?-l(r,16):r},readUInt32LE:function(t){return t=g(t,this.buffer,!0),0>t&&(t=this.buffer.length-(t+1)%this.buffer.length),this.buffer.charCodeAt(t)|this.buffer.charCodeAt(t+1)<<8|this.buffer.charCodeAt(t+2)<<16|this.buffer.charCodeAt(t+3)<<24},readUInt32BE:function(t){return t=g(t,this.buffer,!0),this.buffer.charCodeAt(t)<<24|this.buffer.charCodeAt(t+1)<<16|this.buffer.charCodeAt(t+2)<<8|this.buffer.charCodeAt(t+3)},readInt32LE:function(t){var r=this.readUInt32LE(t);return 2147483648&r?-l(r,32):r},readInt32BE:function(t){var r=this.readUInt32BE(t);return 2147483648&r?-l(r,32):r},readFloatLE:function(t){var r=[];t=g(t,this.buffer,!0);for(var e=0;4>e;++e)r.push(this.readUInt8(t+e));return r.reverse(),u(r)},readFloatBE:function(t){var r=[];t=g(t,this.buffer,!0);for(var e=0;4>e;++e)r.push(this.readUInt8(t+e));return u(r)},readDoubleLE:function(t){var r=[];t=g(t,this.buffer,!0);for(var e=0;8>e;++e)r.push(this.readUInt8(t+e));return r.reverse(),s(r)},readDoubleBE:function(t){var r=[];t=g(t,this.buffer,!0);for(var e=0;8>e;++e)r.push(this.readUInt8(t+e));return s(r)},fill:function(t,r,e){"string"==typeof t&&(t=t.charCodeAt(0)),r||(r=0),e||(e=this.length),r=g(r,this.buffer),e=g(e,this.buffer);for(var n="",f=r;e>f;++f)n+=String.fromCharCode(t);return this.buffer=this.buffer.substr(0,r)+n+this.buffer.substr(e),this},slice:function(t,r){t||(t=0),r||(r=this.length),t=g(t,this.buffer,!0),r=g(r,this.buffer,!0);var e=f();return e.buffer=this.buffer.substr(t,r-t),e},copy:function(t,r,e,n){return r||(r=0),e||(e=0),n||(n=this.length),0>r&&(r=this.buffer.length-(r+1)%this.buffer.length),e=g(e,this.buffer,!0),n=g(n,this.buffer,!0),r=g(r,this.buffer),0>e&&(e=this.buffer.length-(e+1)%this.buffer.length),0>n&&(n=this.buffer.length-(n+1)%this.buffer.length),t.buffer=t.buffer.substr(0,r)+this.buffer.substr(e,n-e)+t.buffer.substr(r+n-e),this},equals:function(t){return this.buffer===t.buffer},toJSON:function(){return JSON.stringify(this.buffer)},toString:function(){return this.buffer},write:function(t,r){r=g(r,this.buffer);for(var e=0;e<t.length;++e)this.writeUInt8(t.charCodeAt(e),r+e)},inspect:function(){return"<StringBuffer "+function(){for(var t="",r=0;r<f.INSPECT_MAX_BYTES&&r<this.length;++r)t+=h(this.readUInt8(r).toString(16))+(r===f.INSPECT_MAX_BYTES-1||r===this.length-1?"":" ");return t}.call(this)+">"},get:function(t){return this.buffer.charCodeAt(t)},clear:function(){return this.buffer="",this},toTypedArray:function(){var t,r;switch(r="boolean"==typeof arguments[1]?arguments[1]:"boolean"==typeof arguments[0]?arguments[0]:!1,typeof arguments[0]){case"function":t=arguments[0];break;case"undefined":t=Uint8Array;break;default:throw TypeError("Must supply a typed array to convert to.")}switch(t){case Uint8Array:for(var e=new Uint8Array(this.length),n=0;n<this.length;++n)e[n]=this.readUInt8(n);return e;case Uint8ClampedArray:for(var e=new Uint8ClampedArray(this.length),n=0;n<this.length;++n)e[n]=this.readUInt8(n);return e;case Int8Array:for(var e=new Int8Array(this.length),n=0;n<this.length;++n)e[n]=this.readInt8(n);return e;case Uint16Array:var e=new Uint16Array(Math.ceil(this.length/2));if(r){for(var n=0;n<this.length;n+=2)e[n/2]=this.readUInt16BE(n);return e}for(var n=0;n<this.length;n+=2)e[n/2]=this.readUInt16LE(n);return e;case Int16Array:var e=new Int16Array(Math.ceil(this.length/2));if(r){for(var n=0;n<this.length;n+=2)e[n/2]=this.readInt16BE(n);return e}for(var n=0;n<this.length;n+=2)e[n/2]=this.readInt16LE(n);return e;case Uint32Array:var e=new Uint32Array(Math.ceil(this.length/4));if(r){for(var n=0;n<this.length;n+=4)e[n/4]=this.readUInt32BE(n);return e}for(var n=0;n<this.length;n+=4)e[n/4]=this.readUInt32LE(n);return e;case Int32Array:var e=new Int32Array(Math.ceil(this.length/4));if(r){for(var n=0;n<this.length;n+=4)e[n/4]=this.readInt32BE(n);return e}for(var n=0;n<this.length;n+=4)e[n/4]=this.readInt32LE(n);return e;case Float32Array:var e=new Float32Array(Math.ceil(this.length/4));if(r){for(var n=0;n<this.length;n+=4)e[n/4]=this.readFloatBE(n);return e}for(var n=0;n<this.length;n+=4)e[n/4]=this.readFloatLE(n);return e;case Float64Array:var e=new Float64Array(Math.ceil(this.length/8));if(r){for(var n=0;n<this.length;n+=8)e[n/8]=this.readFloatBE(n);return e}for(var n=0;n<this.length;n+=8)e[n/8]=this.readFloatLE(n);return e;default:throw TypeError("Must provide a typed array to convert to.")}},toBlob:function(t,r){t||(t="application/octet-binary");try{return new Blob([this.toTypedArray(8,r)],{type:t})}catch(e){throw Error("StringBuffer#toBlob only works in the browser.")}},toBuffer:function(){var t,r;try{t=new Buffer(this.length)}catch(e){throw ReferenceError("Can only use toBuffer in a Node.js environment.")}for(r=0;r<this.length;++r)t.writeUInt8(this.readUInt8(r),r);return t}},Object.defineProperty(f.prototype,"length",{get:function(){return this.buffer.length},set:function(t){if(t>this.buffer.length)for(var r=t;r<this.buffer.length;++r)this.buffer+="\x00";t<this.buffer.length&&(this.buffer=this.buffer.substr(0,t))}}),"undefined"!=typeof exports?"undefined"!=typeof module&&module.exports&&(exports=module.exports=f):t.StringBuffer=f}).call(this);