Browse Source

Update utf8 compatible by default binarypack

ericz 11 years ago
parent
commit
e520030639
3 changed files with 27 additions and 14 deletions
  1. 1 1
      deps/js-binarypack
  2. 26 13
      dist/peer.js
  3. 0 0
      dist/peer.min.js

+ 1 - 1
deps/js-binarypack

@@ -1 +1 @@
-Subproject commit 6d2ef54c2be76a1da0d4635cdb180182d30629fa
+Subproject commit 22e161e3ccb4e20f7f6c14bbaff65c18c21d86d1

+ 26 - 13
dist/peer.js

@@ -62,11 +62,11 @@ exports.BinaryPack = {
   unpack: function(data){
     var unpacker = new Unpacker(data);
     return unpacker.unpack();
-  },
-  pack: function(data, utf8){
-    var packer = new Packer(utf8);
+  },
+  pack: function(data){
+    var packer = new Packer();
     packer.pack(data);
-    var buffer = packer.getBuffer();
+    var buffer = packer.getBuffer();
     return buffer;
   }
 };
@@ -309,8 +309,7 @@ Unpacker.prototype.read = function(length){
   }
 }
 
-function Packer(utf8){
-  this.utf8 = utf8;
+function Packer(){
   this.bufferBuilder = new BufferBuilder();
 }
 
@@ -392,13 +391,8 @@ Packer.prototype.pack_bin = function(blob){
 }
 
 Packer.prototype.pack_string = function(str){
-  var length;
-  if (this.utf8) {
-    var blob = new Blob([str]);
-    length = blob.size;
-  } else {
-    length = str.length;
-  }
+  var length = utf8Length(str);
+
   if (length <= 0x0f){
     this.pack_uint8(0xb0 + length);
   } else if (length <= 0xffff){
@@ -562,6 +556,25 @@ Packer.prototype.pack_int64 = function(num){
   this.bufferBuilder.append((low  & 0x0000ff00) >>>  8);
   this.bufferBuilder.append((low  & 0x000000ff));
 }
+
+function _utf8Replace(m){
+  var code = m.charCodeAt(0);
+
+  if(code <= 0x7ff) return '00';
+  if(code <= 0xffff) return '000';
+  if(code <= 0x1fffff) return '0000';
+  if(code <= 0x3ffffff) return '00000';
+  return '000000';
+}
+
+function utf8Length(str){
+  if (str.length > 600) {
+    // Blob method faster for large strings
+    return (new Blob([str])).size;
+  } else {
+    return str.replace(/[^\u0000-\u007F]/g, _utf8Replace).length;
+  }
+}
 /**
  * Light EventEmitter. Ported from Node.js/events.js
  * Eric Zhang

File diff suppressed because it is too large
+ 0 - 0
dist/peer.min.js


Some files were not shown because too many files changed in this diff