diff --git a/dist/binary.js b/dist/binary.js index 3d2950c..55a58c3 100644 --- a/dist/binary.js +++ b/dist/binary.js @@ -1257,7 +1257,8 @@ BinaryStream.prototype.write = function(data, cb) { }; BinaryStream.prototype.end = function() { - this._onEnd(); + this._ended = true; + this.readable = false; this._write(5, null, this.id); }; diff --git a/dist/binary.min.js b/dist/binary.min.js index 8b198f6..2fc2454 100644 --- a/dist/binary.min.js +++ b/dist/binary.min.js @@ -1,3 +1,3 @@ /*! binary.min.js build:0.0.4, production. Copyright(c) 2012 Eric Zhang MIT Licensed */ (function(exports){ -(function(e){function t(){this._events={}}function i(){t.call(this)}function s(e,t){i.call(this),t=r.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 o(e,t,n,r){if(!(this instanceof o))return new o(options);var s=this;i.call(this),this.id=t,this._socket=e,this._socket.addEventListener("error",function(e){s.readable=!1,s.writable=!1,s.emit("error",e)}),this._socket.addEventListener("close",function(e,t){s._onClose()}),this.writable=!0,this.readable=!0,this.paused=!1,this._closed=!1,this._ended=!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={},typeof e=="string"?(this._nextId=0,this._socket=new WebSocket(e)):(this._nextId=1,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():i.emit("error","Received `close` message for unknown stream: "+n);break;default:i.emit("error","Unrecognized message type received: "+e[0])}})})}(function(e){function t(){this._pieces=[],this._parts=[]}function n(e){this.index=0,this.dataBuffer=e,this.dataView=new Uint8Array(this.dataBuffer),this.length=this.dataBuffer.byteLength}function r(){this.bufferBuilder=new t}e.binaryFeatures={useBlobBuilder:function(){try{return new Blob([]),!1}catch(e){return!0}}(),useArrayBufferView:function(){try{return(new Blob([new Uint8Array([])])).size===0}catch(e){return!0}}(),supportsBinaryWebsockets:function(){try{var e=new WebSocket("ws://localhost:0");return typeof e.binaryType!="undefined"?!0:!1}catch(t){return!1}}()},e.BlobBuilder=window.WebKitBlobBuilder||window.MozBlobBuilder||window.MSBlobBuilder||window.BlobBuilder,t.prototype.append=function(e){typeof e=="number"?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);binaryFeatures.useArrayBufferView||(e=e.buffer),this._parts.push(e),this._pieces=[]}},t.prototype.getBuffer=function(){this._flush();if(binaryFeatures.useBlobBuilder){var e=new BlobBuilder;for(var t=0,n=this._parts.length;t>31,n=(e>>23&255)-127,r=e&8388607|8388608;return(t==0?1:-1)*r*Math.pow(2,n-23)},n.prototype.unpack_double=function(){var e=this.unpack_uint32(),t=this.unpack_uint32(),n=e>>31,r=(e>>20&2047)-1023,i=e&1048575|1048576,s=i*Math.pow(2,r-20)+t*Math.pow(2,r-52);return(n==0?1:-1)*s},n.prototype.read=function(e){var t=this.index;if(t+e<=this.length)return this.dataView.subarray(t,t+e);throw new Error("BinaryPackFailure: read index out of range")},r.prototype.pack=function(e){var t=typeof e;if(t=="string")this.pack_string(e);else if(t=="number")Math.floor(e)===e?this.pack_integer(e):this.pack_double(e);else if(t=="boolean")e===!0?this.bufferBuilder.append(195):e===!1&&this.bufferBuilder.append(194);else if(t=="undefined")this.bufferBuilder.append(192);else{if(t!="object")throw new Error('Type "'+t+'" not yet supported');if(e===null)this.bufferBuilder.append(192);else{var n=e.constructor;if(n==Array)this.pack_array(e);else if(n==Blob||n==File)this.pack_bin(e);else if(n==ArrayBuffer)binaryFeatures.useArrayBufferView?this.pack_bin(new Uint8Array(e)):this.pack_bin(e);else if("BYTES_PER_ELEMENT"in e)binaryFeatures.useArrayBufferView?this.pack_bin(e):this.pack_bin(e.buffer);else if(n==Object)this.pack_object(e);else if(n==Date)this.pack_string(e.toString());else{if(typeof e.toBinaryPack!="function")throw new Error('Type "'+n.toString()+'" not yet supported');this.bufferBuilder.append(e.toBinaryPack())}}}return this.bufferBuilder.getBuffer()},r.prototype.pack_bin=function(e){var t=e.length||e.byteLength||e.size;if(t<=15)this.pack_uint8(160+t);else if(t<=65535)this.bufferBuilder.append(218),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(219),this.pack_uint32(t)}this.bufferBuilder.append(e)},r.prototype.pack_string=function(e){var t=e.length;if(t<=15)this.pack_uint8(176+t);else if(t<=65535)this.bufferBuilder.append(216),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(217),this.pack_uint32(t)}this.bufferBuilder.append(e)},r.prototype.pack_array=function(e){var t=e.length;if(t<=15)this.pack_uint8(144+t);else if(t<=65535)this.bufferBuilder.append(220),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(221),this.pack_uint32(t)}for(var n=0;n>8),this.bufferBuilder.append(e&255)},r.prototype.pack_uint32=function(e){var t=e&4294967295;this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255)},r.prototype.pack_uint64=function(e){var t=e/Math.pow(2,32),n=e%Math.pow(2,32);this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255),this.bufferBuilder.append((n&4278190080)>>>24),this.bufferBuilder.append((n&16711680)>>>16),this.bufferBuilder.append((n&65280)>>>8),this.bufferBuilder.append(n&255)},r.prototype.pack_int8=function(e){this.bufferBuilder.append(e&255)},r.prototype.pack_int16=function(e){this.bufferBuilder.append((e&65280)>>8),this.bufferBuilder.append(e&255)},r.prototype.pack_int32=function(e){this.bufferBuilder.append(e>>>24&255),this.bufferBuilder.append((e&16711680)>>>16),this.bufferBuilder.append((e&65280)>>>8),this.bufferBuilder.append(e&255)},r.prototype.pack_int64=function(e){var t=Math.floor(e/Math.pow(2,32)),n=e%Math.pow(2,32);this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255),this.bufferBuilder.append((n&4278190080)>>>24),this.bufferBuilder.append((n&16711680)>>>16),this.bufferBuilder.append((n&65280)>>>8),this.bufferBuilder.append(n&255)}})(this);var n=Array.isArray;t.prototype.addListener=function(e,t,r,i){if("function"!=typeof t)throw new Error("addListener only takes instances of Function");this.emit("newListener",e,typeof t.listener=="function"?t.listener:t),this._events[e]?n(this._events[e])?this._events[e].push(t):this._events[e]=[this._events[e],t]:this._events[e]=t},t.prototype.on=t.prototype.addListener,t.prototype.once=function(e,t,n){function i(){r.removeListener(e,i),t.apply(this,arguments)}if("function"!=typeof t)throw new Error(".once only takes instances of Function");var r=this;return i.listener=t,r.on(e,i),this},t.prototype.removeListener=function(e,t,r){if("function"!=typeof t)throw new Error("removeListener only takes instances of Function");if(!this._events[e])return this;var i=this._events[e];if(n(i)){var s=-1;for(var o=0,u=i.length;o0){var e=new Uint8Array(this._pieces);binaryFeatures.useArrayBufferView||(e=e.buffer),this._parts.push(e),this._pieces=[]}},t.prototype.getBuffer=function(){this._flush();if(binaryFeatures.useBlobBuilder){var e=new BlobBuilder;for(var t=0,n=this._parts.length;t>31,n=(e>>23&255)-127,r=e&8388607|8388608;return(t==0?1:-1)*r*Math.pow(2,n-23)},n.prototype.unpack_double=function(){var e=this.unpack_uint32(),t=this.unpack_uint32(),n=e>>31,r=(e>>20&2047)-1023,i=e&1048575|1048576,s=i*Math.pow(2,r-20)+t*Math.pow(2,r-52);return(n==0?1:-1)*s},n.prototype.read=function(e){var t=this.index;if(t+e<=this.length)return this.dataView.subarray(t,t+e);throw new Error("BinaryPackFailure: read index out of range")},r.prototype.pack=function(e){var t=typeof e;if(t=="string")this.pack_string(e);else if(t=="number")Math.floor(e)===e?this.pack_integer(e):this.pack_double(e);else if(t=="boolean")e===!0?this.bufferBuilder.append(195):e===!1&&this.bufferBuilder.append(194);else if(t=="undefined")this.bufferBuilder.append(192);else{if(t!="object")throw new Error('Type "'+t+'" not yet supported');if(e===null)this.bufferBuilder.append(192);else{var n=e.constructor;if(n==Array)this.pack_array(e);else if(n==Blob||n==File)this.pack_bin(e);else if(n==ArrayBuffer)binaryFeatures.useArrayBufferView?this.pack_bin(new Uint8Array(e)):this.pack_bin(e);else if("BYTES_PER_ELEMENT"in e)binaryFeatures.useArrayBufferView?this.pack_bin(e):this.pack_bin(e.buffer);else if(n==Object)this.pack_object(e);else if(n==Date)this.pack_string(e.toString());else{if(typeof e.toBinaryPack!="function")throw new Error('Type "'+n.toString()+'" not yet supported');this.bufferBuilder.append(e.toBinaryPack())}}}return this.bufferBuilder.getBuffer()},r.prototype.pack_bin=function(e){var t=e.length||e.byteLength||e.size;if(t<=15)this.pack_uint8(160+t);else if(t<=65535)this.bufferBuilder.append(218),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(219),this.pack_uint32(t)}this.bufferBuilder.append(e)},r.prototype.pack_string=function(e){var t=e.length;if(t<=15)this.pack_uint8(176+t);else if(t<=65535)this.bufferBuilder.append(216),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(217),this.pack_uint32(t)}this.bufferBuilder.append(e)},r.prototype.pack_array=function(e){var t=e.length;if(t<=15)this.pack_uint8(144+t);else if(t<=65535)this.bufferBuilder.append(220),this.pack_uint16(t);else{if(!(t<=4294967295))throw new Error("Invalid length");this.bufferBuilder.append(221),this.pack_uint32(t)}for(var n=0;n>8),this.bufferBuilder.append(e&255)},r.prototype.pack_uint32=function(e){var t=e&4294967295;this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255)},r.prototype.pack_uint64=function(e){var t=e/Math.pow(2,32),n=e%Math.pow(2,32);this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255),this.bufferBuilder.append((n&4278190080)>>>24),this.bufferBuilder.append((n&16711680)>>>16),this.bufferBuilder.append((n&65280)>>>8),this.bufferBuilder.append(n&255)},r.prototype.pack_int8=function(e){this.bufferBuilder.append(e&255)},r.prototype.pack_int16=function(e){this.bufferBuilder.append((e&65280)>>8),this.bufferBuilder.append(e&255)},r.prototype.pack_int32=function(e){this.bufferBuilder.append(e>>>24&255),this.bufferBuilder.append((e&16711680)>>>16),this.bufferBuilder.append((e&65280)>>>8),this.bufferBuilder.append(e&255)},r.prototype.pack_int64=function(e){var t=Math.floor(e/Math.pow(2,32)),n=e%Math.pow(2,32);this.bufferBuilder.append((t&4278190080)>>>24),this.bufferBuilder.append((t&16711680)>>>16),this.bufferBuilder.append((t&65280)>>>8),this.bufferBuilder.append(t&255),this.bufferBuilder.append((n&4278190080)>>>24),this.bufferBuilder.append((n&16711680)>>>16),this.bufferBuilder.append((n&65280)>>>8),this.bufferBuilder.append(n&255)}})(this);var n=Array.isArray;t.prototype.addListener=function(e,t,r,i){if("function"!=typeof t)throw new Error("addListener only takes instances of Function");this.emit("newListener",e,typeof t.listener=="function"?t.listener:t),this._events[e]?n(this._events[e])?this._events[e].push(t):this._events[e]=[this._events[e],t]:this._events[e]=t},t.prototype.on=t.prototype.addListener,t.prototype.once=function(e,t,n){function i(){r.removeListener(e,i),t.apply(this,arguments)}if("function"!=typeof t)throw new Error(".once only takes instances of Function");var r=this;return i.listener=t,r.on(e,i),this},t.prototype.removeListener=function(e,t,r){if("function"!=typeof t)throw new Error("removeListener only takes instances of Function");if(!this._events[e])return this;var i=this._events[e];if(n(i)){var s=-1;for(var o=0,u=i.length;o