2 lines
19 KiB
JavaScript
2 lines
19 KiB
JavaScript
|
/*! binary.js build:0.2.1, development. Copyright(c) 2012 Eric Zhang <eric@ericzhang.com> MIT Licensed */
|
||
|
!function(e){function t(){this._pieces=[],this._parts=[]}function r(e){this.index=0,this.dataBuffer=e,this.dataView=new Uint8Array(this.dataBuffer),this.length=this.dataBuffer.byteLength}function i(){this.bufferBuilder=new t}function n(e){var t=e.charCodeAt(0);return 2047>=t?"00":65535>=t?"000":2097151>=t?"0000":67108863>=t?"00000":"000000"}function s(e){return e.length>600?new Blob([e]).size:e.replace(/[^\u0000-\u007F]/g,n).length}function a(){this._events={}}function o(){a.call(this)}function u(e,t){o.call(this),t=d.extend({readDelay:0,paused:!1},t),this._source=e,this._start=0,this._readChunkSize=t.chunkSize||e.size,this._readDelay=t.readDelay,this.readable=!0,this.paused=t.paused,this._read()}function p(e,t,r,i){if(!(this instanceof p))return new p(options);o.call(this),this.id=t,this._socket=e,this.writable=!0,this.readable=!0,this.paused=!1,this._closed=!1,this._ended=!1,r&&this._write(1,i,this.id)}function h(e,t){if(!(this instanceof h))return new h(e,t);a.call(this);var r=this;this._options=d.extend({chunkSize:40960},t),this.streams={},"string"==typeof e?(this._nextId=0,this._socket=new WebSocket(e)):(this._nextId=1,this._socket=e),this._socket.binaryType="arraybuffer",this._socket.addEventListener("open",function(){r.emit("open")}),this._socket.addEventListener("error",function(e){for(var t=Object.keys(r.streams),i=0,n=t.length;n>i;i++)r.streams[t[i]]._onError(e);r.emit("error",e)}),this._socket.addEventListener("close",function(e,t){for(var i=Object.keys(r.streams),n=0,s=i.length;s>n;n++)r.streams[i[n]]._onClose();r.emit("close",e,t)}),this._socket.addEventListener("message",function(e){d.setZeroTimeout(function(){e=e.data;try{e=d.unpack(e)}catch(t){return r.emit("error",new Error("Received unparsable message: "+t))}if(!(e instanceof Array))return r.emit("error",new Error("Received non-array message"));if(3!=e.length)return r.emit("error",new Error("Received message with wrong part count: "+e.length));if("number"!=typeof e[0])return r.emit("error",new Error("Received message with non-number type: "+e[0]));switch(e[0]){case 0:break;case 1:var i=e[1],n=e[2],s=r._receiveStream(n);r.emit("stream",s,i);break;case 2:var a=e[1],n=e[2],s=r.streams[n];s?s._onData(a):r.emit("error",new Error("Received `data` message for unknown stream: "+n));break;case 3:var n=e[2],s=r.streams[n];s?s._onPause():r.emit("error",new Error("Received `pause` message for unknown stream: "+n));break;case 4:var n=e[2],s=r.streams[n];s?s._onResume():r.emit("error",new Error("Received `resume` message for unknown stream: "+n));break;case 5:var n=e[2],s=r.streams[n];s?s._onEnd():r.emit("error",new Error("Received `end` message for unknown stream: "+n));break;case 6:var n=e[2],s=r.streams[n];s?s._onClose():r.emit("error",new Error("Received `close` message for unknown stream: "+n));break;default:r.emit("error",new Error("Unrecognized message type received: "+e[0]))}})})}var c={};c.useBlobBuilder=function(){try{return new Blob([]),!1}catch(e){return!0}}(),c.useArrayBufferView=!c.useBlobBuilder&&function(){try{return 0===new Blob([new Uint8Array([])]).size}catch(e){return!0}}(),c.supportsBinaryWebsockets=function(){try{var e=new WebSocket("ws://null");return e.onerror=function(){},"undefined"!=typeof e.binaryType?!0:!1}catch(t){return!1}}(),e.binaryFeatures=c,e.BlobBuilder=window.WebKitBlobBuilder||window.MozBlobBuilder||window.MSBlobBuilder||window.BlobBuilder,t.prototype.append=function(e){"number"==typeof e?this._pieces.push(e):(this.flush(),this._parts.push(e))},t.prototype.flush=function(){if(this._pieces.length>0){var e=new Uint8Array(this._pieces);c.useArrayBufferView||(e=e.buffer),this._parts.push(e),this._pieces=[]}},t.prototype.getBuffer=function(){if(this.flush(),c.useBlobBuilder){for(var e=new BlobBuilder,t=0,r=this._parts.length;r>t;t++)e.append(this._parts[t]);return e.getBlob()}return new Blob(this._parts)},e.BinaryPack={unpack:function(e){var t=new r(e);return t.unpack()},pack:function(e){var t=new i;t.pack(e);var r=t.getBuffer();return r}},r.prototype.unpack=function(){var e=this.unpack_uint8();if(128>e){var t=e;return t}if(32>(224^e
|