/
binary.min.js
3 lines (3 loc) · 7.29 KB
/
binary.min.js
1
2
3
/*! binary.min.js build:0.0.2, production. Copyright(c) 2012 Eric Zhang <eric@ericzhang.com> MIT Licensed */
(function(exports){
(function(e){function t(){this._events={},this._maxListeners=10}function n(e,t,n,r,i){this.type=e,this.listener=t,this.scope=n,this.once=r,this.instance=i}function s(){t.call(this)}function o(e,t,n,r){if(!(this instanceof o))return new o(options);var i=this;s.call(this),this.id=t,this._socket=e,this._socket.addEventListener("error",function(e){i.readable=!1,i.writable=!1,i.emit("error",e)}),this._socket.addEventListener("close",function(e,t){i.readable=!1,i.writable=!1,i.emit("close",e,t)}),this.writable=!0,this.readable=!0,this._paused=!1,n&&this._write(1,r,this.id)}function u(e,n){if(!(this instanceof u))return new u(n);t.call(this);var i=this;this._options=r.extend({chunkSize:40960},n),this._streams={},this._nextId=0,typeof e=="string"?this._socket=new WebSocket(e):this._socket=e,this._socket.binaryType="arraybuffer",this._socket.addEventListener("open",function(){i.emit("open")}),this._socket.addEventListener("error",function(e){i.emit("error",e)}),this._socket.addEventListener("close",function(e,t){i.emit("close",e,t)}),this._socket.addEventListener("message",function(e,t){r.setZeroTimeout(function(){e.hasOwnProperty("data")&&(e=e.data),e=r.unpack(e);switch(e[0]){case 0:break;case 1:var t=e[1],n=e[2],s=i._receiveStream(n);i.emit("stream",s,t);break;case 2:var o=e[1],n=e[2],s=i._streams[n];s?s._onData(o):i.emit("error","Received `data` message for unknown stream: "+n);break;case 3:var n=e[2],s=i._streams[n];s?s._onPause():i.emit("error","Received `pause` message for unknown stream: "+n);break;case 4:var n=e[2],s=i._streams[n];s?s._onResume():i.emit("error","Received `resume` message for unknown stream: "+n);break;case 5:var n=e[2],s=i._streams[n];s?s._onEnd():i.emit("error","Received `end` message for unknown stream: "+n);break;case 6:var n=e[2],s=i._streams[n];s?(s._onClose(),delete i._streams[n]):i.emit("error","Received `close` message for unknown stream: "+n);break;default:i.emit("error","Unrecognized message type received: "+e[0])}})})}n.prototype.fire=function(e){this.listener.apply(this.scope||this.instance,e);if(this.once)return this.instance.removeListener(this.type,this.listener,this.scope),!1},t.prototype.eachListener=function(e,t){var n=null,r=null,i=null;if(this._events.hasOwnProperty(e)){r=this._events[e];for(n=0;n<r.length;n+=1){i=t.call(this,r[n],n);if(i===!1)n-=1;else if(i===!0)break}}return this},t.prototype.addListener=function(e,t,r,i){return this._events.hasOwnProperty(e)||(this._events[e]=[]),this._events[e].push(new n(e,t,r,i,this)),this.emit("newListener",e,t,r,i),this._maxListeners&&!this._events[e].warned&&this._events[e].length>this._maxListeners&&(typeof console!="undefined"&&console.warn("Possible EventEmitter memory leak detected. "+this._events[e].length+" listeners added. Use emitter.setMaxListeners() to increase limit."),this._events[e].warned=!0),this},t.prototype.on=t.prototype.addListener,t.prototype.once=function(e,t,n){return this.addListener(e,t,n,!0)},t.prototype.removeListener=function(e,t,n){return this.eachListener(e,function(r,i){r.listener===t&&(!n||r.scope===n)&&this._events[e].splice(i,1)}),this._events[e]&&this._events[e].length===0&&delete this._events[e],this},t.prototype.off=t.prototype.removeListener,t.prototype.removeAllListeners=function(e){return e&&this._events.hasOwnProperty(e)?delete this._events[e]:e||(this._events={}),this},t.prototype.listeners=function(e){if(this._events.hasOwnProperty(e)){var t=[];return this.eachListener(e,function(e){t.push(e.listener)}),t}return[]},t.prototype.emit=function(e){var t=[],n=null;for(n=1;n<arguments.length;n+=1)t.push(arguments[n]);return this.eachListener(e,function(e){return e.fire(t)}),this},t.prototype.setMaxListeners=function(e){return this._maxListeners=e,this},t.extend=function(){var e={},t=this.prototype,n=null;for(n in t)t.hasOwnProperty(n)&&(e[n]=t[n]);return e},typeof define=="function"&&define.amd&&define(function(){return t});var r={inherits:function(e,t){e.super_=t,e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}})},extend:function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n]);return e},info:console.log.bind(console),pack:BinaryPack.pack,unpack:BinaryPack.unpack,setZeroTimeout:function(e){function r(r){t.push(r),e.postMessage(n,"*")}function i(r){r.source==e&&r.data==n&&(r.stopPropagation&&r.stopPropagation(),t.length&&t.shift()())}var t=[],n="zero-timeout-message";return e.addEventListener?e.addEventListener("message",i,!0):e.attachEvent&&e.attachEvent("onmessage",i),r}(this)},i={isBuffer:function(){return!1}};r.inherits(s,t),s.prototype.pipe=function(e,t){function r(t){e.writable&&!1===e.write(t)&&n.pause&&n.pause()}function i(){n.readable&&n.resume&&n.resume()}function o(){if(s)return;s=!0,e.end()}function u(){if(s)return;s=!0,e.destroy()}function a(e){f();if(this.listeners("error").length===0)throw e}function f(){n.removeListener("data",r),e.removeListener("drain",i),n.removeListener("end",o),n.removeListener("close",u),n.removeListener("error",a),e.removeListener("error",a),n.removeListener("end",f),n.removeListener("close",f),e.removeListener("end",f),e.removeListener("close",f)}var n=this;n.on("data",r),e.on("drain",i),!e._isStdio&&(!t||t.end!==!1)&&(n.on("end",o),n.on("close",u));var s=!1;return n.on("error",a),e.on("error",a),n.on("end",f),n.on("close",f),e.on("end",f),e.on("close",f),e.emit("pipe",n),e},r.inherits(o,s),o.prototype._onClose=function(){this.readable=!1,this.writable=!1,this.emit("close")},o.prototype._onPause=function(){this.emit("pause"),this._paused=!0},o.prototype._onResume=function(){this.emit("resume"),this.emit("drain"),this._paused=!1},o.prototype._write=function(e,t,n,i){var s=r.pack([e,t,n]);return this._socket.send(s,{binary:!0},i)!==!1},o.prototype.write=function(e,t){if(this.writable){var n=this._write(2,e,this.id,t);return!this._paused&&n}throw new Error("Stream is not writable")},o.prototype.end=function(){this.readable=!1,this._write(5,null,this.id)},o.prototype.destroy=o.prototype.destroySoon=function(){this.readable=!1,this.writable=!1,this._write(6,null,this.id)},o.prototype._onEnd=function(){this.readable=!1,this.emit("end")},o.prototype._onData=function(e){this.readable&&this.emit("data",e)},o.prototype.pause=function(){this._onPause(),this._write(3,null,this.id)},o.prototype.resume=function(){this._onResume(),this._write(4,null,this.id)},r.inherits(u,t),u.prototype.send=function(e,t){var n=this.createStream(t);if(e instanceof s)e.pipe(n);else if(i.isBuffer(e))(new BufferReadStream(e,{chunkSize:this._options.chunkSize})).pipe(n);else if(e.constructor===window.Blob||e.constructor==window.ArrayBuffer)(new BlobReadStream(e,{chunkSize:this._options.chunkSize})).pipe(n);else if(typeof e=="object"&&"BYTES_PER_ELEMENT"in e){var r;binaryFeatures.useArrayBufferView||(e=e.buffer);if(binaryFeatures.useBlobBuilder){var o=new BlobBuilder;o.append(e),r=o.getBlob()}else r=new Blob([e]);(new BlobReadStream(r,{chunkSize:this._options.chunkSize})).pipe(n)}else n.write(e)},u.prototype._receiveStream=function(e){var t=new o(this._socket,e,!1);return this._streams[e]=t,t},u.prototype.createStream=function(e){var t=this._nextId;this._nextId+=2;var n=new o(this._socket,t,!0,e);return this._streams[t]=n,n},u.prototype.close=u.prototype.destroy=function(e,t){this._socket.close(e,t)},e.BinaryClient=u})(this)