diff --git a/lib/binarypack.js b/lib/binarypack.js index 91a7a3f..208545b 100644 --- a/lib/binarypack.js +++ b/lib/binarypack.js @@ -212,7 +212,7 @@ BinaryPack.Packer.prototype.pack_bin = function(blob){ } BinaryPack.Packer.prototype.pack_string = function(str){ - var length = str.length; + var length = Buffer.byteLength(str); if (length <= 0x0f){ this.bufferBuilder.append(0xb0 + length, 2); } else if (length <= 0xffff){ diff --git a/lib/bufferbuilder.js b/lib/bufferbuilder.js index a0bd8d4..dd8bb0c 100644 --- a/lib/bufferbuilder.js +++ b/lib/bufferbuilder.js @@ -21,13 +21,16 @@ BufferBuilder.prototype.append = function(data, type) { // 7: Int32 // 8: Float // 9: Double - - if(type < 2) { - this._length += data.length; - } else { - this._length += this._sizes[type]; - } - + switch(type) { + case 0: + this._length += Buffer.byteLength(data); + break; + case 1: + this._length += data.length; + break; + default: + this._length += this._sizes[type]; + }; this._pieces.push([data, type]); }; @@ -55,7 +58,7 @@ BufferBuilder.prototype.getBuffer = function() { cursor.writeUInt32BE(tuple[0]); break; case 5: - cursor.writeInt8BE(tuple[0]); + cursor.writeInt8(tuple[0]); break; case 6: cursor.writeInt16BE(tuple[0]); diff --git a/package.json b/package.json index 4d429bd..29869d4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "binarypack", "description": "BinaryPack is a JSON-like binary serialization format", - "version": "0.0.2", + "version": "0.0.4", "homepage": "https://github.com/binaryjs/node-binarypack", "author": "Eric Zhang", "repository": {