diff --git a/dist/binary.js b/dist/binary.js index 2d5764f..f87ab6c 100644 --- a/dist/binary.js +++ b/dist/binary.js @@ -1,4 +1,4 @@ -/*! binary.js build:0.0.1, development. Copyright(c) 2012 Eric Zhang MIT Licensed */ +/*! binary.js build:0.0.2, development. Copyright(c) 2012 Eric Zhang MIT Licensed */ (function(exports){ /** * EventEmitter v3.1.5 @@ -310,6 +310,14 @@ var util = { } }); }, + extend: function(dest, source) { + for(var key in source) { + if(source.hasOwnProperty(key)) { + dest[key] = source[key]; + } + } + return dest; + }, info: console.log.bind(console), pack: BinaryPack.pack, unpack: BinaryPack.unpack, @@ -344,9 +352,102 @@ var util = { }(this)) }; -// Stream shim for client side -var Stream = EventEmitter; +// For browser-side code compatibility +// No-op for Node.js +var Buffer = {isBuffer: function(){return false;}}; + + + +function Stream() { + EventEmitter.call(this); +} + +util.inherits(Stream, EventEmitter); +Stream.prototype.pipe = function(dest, options) { + var source = this; + + function ondata(chunk) { + if (dest.writable) { + if (false === dest.write(chunk) && source.pause) { + source.pause(); + } + } + } + + source.on('data', ondata); + + function ondrain() { + if (source.readable && source.resume) { + source.resume(); + } + } + + dest.on('drain', ondrain); + + // If the 'end' option is not supplied, dest.end() will be called when + // source gets the 'end' or 'close' events. Only dest.end() once. + if (!dest._isStdio && (!options || options.end !== false)) { + source.on('end', onend); + source.on('close', onclose); + } + + var didOnEnd = false; + function onend() { + if (didOnEnd) return; + didOnEnd = true; + + dest.end(); + } + + + function onclose() { + if (didOnEnd) return; + didOnEnd = true; + + dest.destroy(); + } + + // don't leave dangling pipes when there are errors. + function onerror(er) { + cleanup(); + if (this.listeners('error').length === 0) { + throw er; // Unhandled stream error in pipe. + } + } + + source.on('error', onerror); + dest.on('error', onerror); + + // remove all the event listeners that were added. + function cleanup() { + source.removeListener('data', ondata); + dest.removeListener('drain', ondrain); + + source.removeListener('end', onend); + source.removeListener('close', onclose); + + source.removeListener('error', onerror); + dest.removeListener('error', onerror); + + source.removeListener('end', cleanup); + source.removeListener('close', cleanup); + + dest.removeListener('end', cleanup); + dest.removeListener('close', cleanup); + } + + source.on('end', cleanup); + source.on('close', cleanup); + + dest.on('end', cleanup); + dest.on('close', cleanup); + + dest.emit('pipe', source); + + // Allow for unix-like usage: A.pipe(B).pipe(C) + return dest; +}; function BinaryStream(socket, id, create, meta) { if (!(this instanceof BinaryStream)) return new BinaryStream(options); @@ -456,13 +557,16 @@ BinaryStream.prototype.resume = function() { }; -function BinaryClient(socket) { +function BinaryClient(socket, options) { if (!(this instanceof BinaryClient)) return new BinaryClient(options); EventEmitter.call(this); var self = this; + this._options = util.extend({ + chunkSize: 40960 + }, options); this._streams = {}; @@ -478,6 +582,9 @@ function BinaryClient(socket) { this._socket.binaryType = 'arraybuffer'; + this._socket.addEventListener('open', function(){ + self.emit('open'); + }); this._socket.addEventListener('error', function(error){ self.emit('error', error); }); @@ -591,11 +698,29 @@ function BinaryClient(socket) { util.inherits(BinaryClient, EventEmitter); BinaryClient.prototype.send = function(data, meta){ + var stream = this.createStream(meta); if(data instanceof Stream) { - data.pipe(this.createStream(meta)); + data.pipe(stream); + } else if (Buffer.isBuffer(data)) { + (new BufferReadStream(data, {chunkSize: this._options.chunkSize})).pipe(stream); + } else if (data.constructor === window.Blob || data.constructor == window.ArrayBuffer) { + (new BlobReadStream(data, {chunkSize: this._options.chunkSize})).pipe(stream); + } else if (typeof data === 'object' && 'BYTES_PER_ELEMENT' in data) { + var blob; + if(!binaryFeatures.useArrayBufferView) { + // Warn + data = data.buffer; + } + if(binaryFeatures.useBlobBuilder) { + var builder = new BlobBuilder(); + builder.append(data); + blob = builder.getBlob() + } else { + blob = new Blob([data]); + } + (new BlobReadStream(blob, {chunkSize: this._options.chunkSize})).pipe(stream); } else { - var stream = this.createStream(meta); - // Do chunking + stream.write(data); } }; diff --git a/dist/binary.min.js b/dist/binary.min.js index b3844b9..594f797 100644 --- a/dist/binary.min.js +++ b/dist/binary.min.js @@ -1,3 +1,3 @@ -/*! binary.min.js build:0.0.1, production. Copyright(c) 2012 Eric Zhang MIT Licensed */ +/*! binary.min.js build:0.0.2, production. Copyright(c) 2012 Eric Zhang 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(e,t,n,r){if(!(this instanceof s))return new s(options);var o=this;i.call(this),this.id=t,this._socket=e,this._socket.addEventListener("error",function(e){o.readable=!1,o.writable=!1,o.emit("error",e)}),this._socket.addEventListener("close",function(e,t){o.readable=!1,o.writable=!1,o.emit("close",e,t)}),this.writable=!0,this.readable=!0,this._paused=!1,n&&this._write(1,r,this.id)}function o(e){if(!(this instanceof o))return new o(options);t.call(this);var n=this;this._streams={},this._nextId=0,typeof e=="string"?this._socket=new WebSocket(e):this._socket=e,this._socket.binaryType="arraybuffer",this._socket.addEventListener("error",function(e){n.emit("error",e)}),this._socket.addEventListener("close",function(e,t){n.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],i=e[2],s=n._receiveStream(i);n.emit("stream",s,t);break;case 2:var o=e[1],i=e[2],s=n._streams[i];s?s._onData(o):n.emit("error","Received `data` message for unknown stream: "+i);break;case 3:var i=e[2],s=n._streams[i];s?s._onPause():n.emit("error","Received `pause` message for unknown stream: "+i);break;case 4:var i=e[2],s=n._streams[i];s?s._onResume():n.emit("error","Received `resume` message for unknown stream: "+i);break;case 5:var i=e[2],s=n._streams[i];s?s._onEnd():n.emit("error","Received `end` message for unknown stream: "+i);break;case 6:var i=e[2],s=n._streams[i];s?(s._onClose(),delete n._streams[i]):n.emit("error","Received `close` message for unknown stream: "+i);break;default:n.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;nthis._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;nthis._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 MIT Licensed */ +/*! binary.js build:0.0.2, development. Copyright(c) 2012 Eric Zhang MIT Licensed */ (function(exports){ /** * EventEmitter v3.1.5 @@ -310,6 +310,14 @@ var util = { } }); }, + extend: function(dest, source) { + for(var key in source) { + if(source.hasOwnProperty(key)) { + dest[key] = source[key]; + } + } + return dest; + }, info: console.log.bind(console), pack: BinaryPack.pack, unpack: BinaryPack.unpack, @@ -344,9 +352,102 @@ var util = { }(this)) }; -// Stream shim for client side -var Stream = EventEmitter; +// For browser-side code compatibility +// No-op for Node.js +var Buffer = {isBuffer: function(){return false;}}; + + + +function Stream() { + EventEmitter.call(this); +} + +util.inherits(Stream, EventEmitter); +Stream.prototype.pipe = function(dest, options) { + var source = this; + + function ondata(chunk) { + if (dest.writable) { + if (false === dest.write(chunk) && source.pause) { + source.pause(); + } + } + } + + source.on('data', ondata); + + function ondrain() { + if (source.readable && source.resume) { + source.resume(); + } + } + + dest.on('drain', ondrain); + + // If the 'end' option is not supplied, dest.end() will be called when + // source gets the 'end' or 'close' events. Only dest.end() once. + if (!dest._isStdio && (!options || options.end !== false)) { + source.on('end', onend); + source.on('close', onclose); + } + + var didOnEnd = false; + function onend() { + if (didOnEnd) return; + didOnEnd = true; + + dest.end(); + } + + + function onclose() { + if (didOnEnd) return; + didOnEnd = true; + + dest.destroy(); + } + + // don't leave dangling pipes when there are errors. + function onerror(er) { + cleanup(); + if (this.listeners('error').length === 0) { + throw er; // Unhandled stream error in pipe. + } + } + + source.on('error', onerror); + dest.on('error', onerror); + + // remove all the event listeners that were added. + function cleanup() { + source.removeListener('data', ondata); + dest.removeListener('drain', ondrain); + + source.removeListener('end', onend); + source.removeListener('close', onclose); + + source.removeListener('error', onerror); + dest.removeListener('error', onerror); + + source.removeListener('end', cleanup); + source.removeListener('close', cleanup); + + dest.removeListener('end', cleanup); + dest.removeListener('close', cleanup); + } + + source.on('end', cleanup); + source.on('close', cleanup); + + dest.on('end', cleanup); + dest.on('close', cleanup); + + dest.emit('pipe', source); + + // Allow for unix-like usage: A.pipe(B).pipe(C) + return dest; +}; function BinaryStream(socket, id, create, meta) { if (!(this instanceof BinaryStream)) return new BinaryStream(options); @@ -456,13 +557,16 @@ BinaryStream.prototype.resume = function() { }; -function BinaryClient(socket) { +function BinaryClient(socket, options) { if (!(this instanceof BinaryClient)) return new BinaryClient(options); EventEmitter.call(this); var self = this; + this._options = util.extend({ + chunkSize: 40960 + }, options); this._streams = {}; @@ -478,6 +582,9 @@ function BinaryClient(socket) { this._socket.binaryType = 'arraybuffer'; + this._socket.addEventListener('open', function(){ + self.emit('open'); + }); this._socket.addEventListener('error', function(error){ self.emit('error', error); }); @@ -591,11 +698,29 @@ function BinaryClient(socket) { util.inherits(BinaryClient, EventEmitter); BinaryClient.prototype.send = function(data, meta){ + var stream = this.createStream(meta); if(data instanceof Stream) { - data.pipe(this.createStream(meta)); + data.pipe(stream); + } else if (Buffer.isBuffer(data)) { + (new BufferReadStream(data, {chunkSize: this._options.chunkSize})).pipe(stream); + } else if (data.constructor === window.Blob || data.constructor == window.ArrayBuffer) { + (new BlobReadStream(data, {chunkSize: this._options.chunkSize})).pipe(stream); + } else if (typeof data === 'object' && 'BYTES_PER_ELEMENT' in data) { + var blob; + if(!binaryFeatures.useArrayBufferView) { + // Warn + data = data.buffer; + } + if(binaryFeatures.useBlobBuilder) { + var builder = new BlobBuilder(); + builder.append(data); + blob = builder.getBlob() + } else { + blob = new Blob([data]); + } + (new BlobReadStream(blob, {chunkSize: this._options.chunkSize})).pipe(stream); } else { - var stream = this.createStream(meta); - // Do chunking + stream.write(data); } }; diff --git a/examples/public/binarypack.js b/examples/public/binarypack.js index 5b86ab8..2d783d0 100644 --- a/examples/public/binarypack.js +++ b/examples/public/binarypack.js @@ -1,5 +1,6 @@ -/*! binarypack.js build:0.0.0, development. Copyright(c) 2012 Eric Zhang MIT Licensed */ -var binaryFeatures = { +/*! binarypack.js build:0.0.1, development. Copyright(c) 2012 Eric Zhang MIT Licensed */ +(function(exports){ +exports.binaryFeatures = { useBlobBuilder: (function(){ try { new Blob([]); @@ -17,7 +18,7 @@ var binaryFeatures = { })() }; -var BlobBuilder = window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder || window.BlobBuilder; +exports.BlobBuilder = window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder || window.BlobBuilder; function BufferBuilder(){ this._pieces = []; @@ -56,19 +57,19 @@ BufferBuilder.prototype.getBuffer = function() { return new Blob(this._parts); } }; -BinaryPack = { +exports.BinaryPack = { unpack: function(data){ - var unpacker = new BinaryPack.Unpacker(data); + var unpacker = new Unpacker(data); return unpacker.unpack(); }, pack: function(data){ - var packer = new BinaryPack.Packer(); + var packer = new Packer(); var buffer = packer.pack(data); return buffer; } }; -BinaryPack.Unpacker = function(data){ +function Unpacker (data){ // Data is ArrayBuffer this.index = 0; this.dataBuffer = data; @@ -77,7 +78,7 @@ BinaryPack.Unpacker = function(data){ } -BinaryPack.Unpacker.prototype.unpack = function(){ +Unpacker.prototype.unpack = function(){ var type = this.unpack_uint8(); if (type < 0x80){ var positive_fixnum = type; @@ -160,13 +161,13 @@ BinaryPack.Unpacker.prototype.unpack = function(){ } } -BinaryPack.Unpacker.prototype.unpack_uint8 = function(){ +Unpacker.prototype.unpack_uint8 = function(){ var byte = this.dataView[this.index] & 0xff; this.index++; return byte; }; -BinaryPack.Unpacker.prototype.unpack_uint16 = function(){ +Unpacker.prototype.unpack_uint16 = function(){ var bytes = this.read(2); var uint16 = ((bytes[0] & 0xff) * 256) + (bytes[1] & 0xff); @@ -174,7 +175,7 @@ BinaryPack.Unpacker.prototype.unpack_uint16 = function(){ return uint16; } -BinaryPack.Unpacker.prototype.unpack_uint32 = function(){ +Unpacker.prototype.unpack_uint32 = function(){ var bytes = this.read(4); var uint32 = ((bytes[0] * 256 + @@ -185,7 +186,7 @@ BinaryPack.Unpacker.prototype.unpack_uint32 = function(){ return uint32; } -BinaryPack.Unpacker.prototype.unpack_uint64 = function(){ +Unpacker.prototype.unpack_uint64 = function(){ var bytes = this.read(8); var uint64 = ((((((bytes[0] * 256 + @@ -201,29 +202,29 @@ BinaryPack.Unpacker.prototype.unpack_uint64 = function(){ } -BinaryPack.Unpacker.prototype.unpack_int8 = function(){ +Unpacker.prototype.unpack_int8 = function(){ var uint8 = this.unpack_uint8(); return (uint8 < 0x80 ) ? uint8 : uint8 - (1 << 8); }; -BinaryPack.Unpacker.prototype.unpack_int16 = function(){ +Unpacker.prototype.unpack_int16 = function(){ var uint16 = this.unpack_uint16(); return (uint16 < 0x8000 ) ? uint16 : uint16 - (1 << 16); } -BinaryPack.Unpacker.prototype.unpack_int32 = function(){ +Unpacker.prototype.unpack_int32 = function(){ var uint32 = this.unpack_uint32(); return (uint32 < Math.pow(2, 31) ) ? uint32 : uint32 - Math.pow(2, 32); } -BinaryPack.Unpacker.prototype.unpack_int64 = function(){ +Unpacker.prototype.unpack_int64 = function(){ var uint64 = this.unpack_uint64(); return (uint64 < Math.pow(2, 63) ) ? uint64 : uint64 - Math.pow(2, 64); } -BinaryPack.Unpacker.prototype.unpack_raw = function(size){ +Unpacker.prototype.unpack_raw = function(size){ if ( this.length < this.index + size){ throw new Error('BinaryPackFailure: index is out of range' + ' ' + this.index + ' ' + size + ' ' + this.length); @@ -236,7 +237,7 @@ BinaryPack.Unpacker.prototype.unpack_raw = function(size){ return buf; } -BinaryPack.Unpacker.prototype.unpack_string = function(size){ +Unpacker.prototype.unpack_string = function(size){ var bytes = this.read(size); var i = 0, str = '', c, code; while(i < size){ @@ -259,7 +260,7 @@ BinaryPack.Unpacker.prototype.unpack_string = function(size){ return str; } -BinaryPack.Unpacker.prototype.unpack_array = function(size){ +Unpacker.prototype.unpack_array = function(size){ var objects = new Array(size); for(var i = 0; i < size ; i++){ objects[i] = this.unpack(); @@ -267,7 +268,7 @@ BinaryPack.Unpacker.prototype.unpack_array = function(size){ return objects; } -BinaryPack.Unpacker.prototype.unpack_map = function(size){ +Unpacker.prototype.unpack_map = function(size){ var map = {}; for(var i = 0; i < size ; i++){ var key = this.unpack(); @@ -277,7 +278,7 @@ BinaryPack.Unpacker.prototype.unpack_map = function(size){ return map; } -BinaryPack.Unpacker.prototype.unpack_float = function(){ +Unpacker.prototype.unpack_float = function(){ var uint32 = this.unpack_uint32(); var sign = uint32 >> 31; var exp = ((uint32 >> 23) & 0xff) - 127; @@ -286,7 +287,7 @@ BinaryPack.Unpacker.prototype.unpack_float = function(){ fraction * Math.pow(2, exp - 23); } -BinaryPack.Unpacker.prototype.unpack_double = function(){ +Unpacker.prototype.unpack_double = function(){ var h32 = this.unpack_uint32(); var l32 = this.unpack_uint32(); var sign = h32 >> 31; @@ -297,7 +298,7 @@ BinaryPack.Unpacker.prototype.unpack_double = function(){ return (sign == 0 ? 1 : -1) * frac; } -BinaryPack.Unpacker.prototype.read = function(length){ +Unpacker.prototype.read = function(length){ var j = this.index; if (j + length <= this.length) { return this.dataView.subarray(j, j + length); @@ -306,11 +307,11 @@ BinaryPack.Unpacker.prototype.read = function(length){ } } -BinaryPack.Packer = function(){ +function Packer (){ this.bufferBuilder = new BufferBuilder(); } -BinaryPack.Packer.prototype.pack = function(value){ +Packer.prototype.pack = function(value){ var type = typeof(value); if (type == 'string'){ this.pack_string(value); @@ -341,7 +342,7 @@ BinaryPack.Packer.prototype.pack = function(value){ } else { this.pack_bin(value); } - } else if (Uint8Array.prototype.__proto__.isPrototypeOf(value) || constructor == Blob){ + } else if ('BYTES_PER_ELEMENT' in value || constructor == Blob){ if(binaryFeatures.useArrayBufferView) { this.pack_bin(value); } else { @@ -364,7 +365,7 @@ BinaryPack.Packer.prototype.pack = function(value){ } -BinaryPack.Packer.prototype.pack_bin = function(blob){ +Packer.prototype.pack_bin = function(blob){ var length = blob.length || blob.byteLength || blob.size; if (length <= 0x1f){ this.pack_uint8(0xa0 + length); @@ -381,7 +382,7 @@ BinaryPack.Packer.prototype.pack_bin = function(blob){ this.bufferBuilder.append(blob); } -BinaryPack.Packer.prototype.pack_string = function(str){ +Packer.prototype.pack_string = function(str){ var length = str.length; if (length <= 0x1f){ this.pack_uint8(0xb0 + length); @@ -398,7 +399,7 @@ BinaryPack.Packer.prototype.pack_string = function(str){ this.bufferBuilder.append(str); } -BinaryPack.Packer.prototype.pack_array = function(ary){ +Packer.prototype.pack_array = function(ary){ var length = ary.length; if (length <= 0x0f){ this.pack_uint8(0x90 + length); @@ -416,7 +417,7 @@ BinaryPack.Packer.prototype.pack_array = function(ary){ } } -BinaryPack.Packer.prototype.pack_integer = function(num){ +Packer.prototype.pack_integer = function(num){ if ( -0x20 <= num && num <= 0x7f){ this.bufferBuilder.append(num & 0xff); } else if (0x00 <= num && num <= 0xff){ @@ -448,7 +449,7 @@ BinaryPack.Packer.prototype.pack_integer = function(num){ } } -BinaryPack.Packer.prototype.pack_double = function(num){ +Packer.prototype.pack_double = function(num){ var sign = 0; if (num < 0){ sign = 1; @@ -466,7 +467,7 @@ BinaryPack.Packer.prototype.pack_double = function(num){ this.pack_int32(l32); } -BinaryPack.Packer.prototype.pack_object = function(obj){ +Packer.prototype.pack_object = function(obj){ var keys = Object.keys(obj); var length = keys.length; if (length <= 0x0f){ @@ -488,16 +489,16 @@ BinaryPack.Packer.prototype.pack_object = function(obj){ } } -BinaryPack.Packer.prototype.pack_uint8 = function(num){ +Packer.prototype.pack_uint8 = function(num){ this.bufferBuilder.append(num); } -BinaryPack.Packer.prototype.pack_uint16 = function(num){ +Packer.prototype.pack_uint16 = function(num){ this.bufferBuilder.append(num >> 8); this.bufferBuilder.append(num & 0xff); } -BinaryPack.Packer.prototype.pack_uint32 = function(num){ +Packer.prototype.pack_uint32 = function(num){ var n = num & 0xffffffff; this.bufferBuilder.append((n & 0xff000000) >>> 24); this.bufferBuilder.append((n & 0x00ff0000) >>> 16); @@ -505,7 +506,7 @@ BinaryPack.Packer.prototype.pack_uint32 = function(num){ this.bufferBuilder.append((n & 0x000000ff)); } -BinaryPack.Packer.prototype.pack_uint64 = function(num){ +Packer.prototype.pack_uint64 = function(num){ var high = num / Math.pow(2, 32); var low = num % Math.pow(2, 32); this.bufferBuilder.append((high & 0xff000000) >>> 24); @@ -518,23 +519,23 @@ BinaryPack.Packer.prototype.pack_uint64 = function(num){ this.bufferBuilder.append((low & 0x000000ff)); } -BinaryPack.Packer.prototype.pack_int8 = function(num){ +Packer.prototype.pack_int8 = function(num){ this.bufferBuilder.append(num & 0xff); } -BinaryPack.Packer.prototype.pack_int16 = function(num){ +Packer.prototype.pack_int16 = function(num){ this.bufferBuilder.append((num & 0xff00) >> 8); this.bufferBuilder.append(num & 0xff); } -BinaryPack.Packer.prototype.pack_int32 = function(num){ +Packer.prototype.pack_int32 = function(num){ this.bufferBuilder.append((num >>> 24) & 0xff); this.bufferBuilder.append((num & 0x00ff0000) >>> 16); this.bufferBuilder.append((num & 0x0000ff00) >>> 8); this.bufferBuilder.append((num & 0x000000ff)); } -BinaryPack.Packer.prototype.pack_int64 = function(num){ +Packer.prototype.pack_int64 = function(num){ var high = Math.floor(num / Math.pow(2, 32)); var low = num % Math.pow(2, 32); this.bufferBuilder.append((high & 0xff000000) >>> 24); @@ -546,3 +547,5 @@ BinaryPack.Packer.prototype.pack_int64 = function(num){ this.bufferBuilder.append((low & 0x0000ff00) >>> 8); this.bufferBuilder.append((low & 0x000000ff)); } + +})(this);