Permalink
Browse files

Put doesn't calculate data length correctly when unicode data is given

  • Loading branch information...
1 parent 5e2b881 commit 8954cef6f7ae675501468333937c10a5c886ce79 Amjad Mohamed committed Oct 4, 2012
Showing with 7 additions and 1 deletion.
  1. +7 −1 lib/beanstalk_client.js
View
@@ -256,8 +256,14 @@ BeanstalkClient.prototype.put = function(data, priority, delay, ttr) {
ttr = 100000;
}
+ function lengthInUtf8Bytes(str) {
+ // Matches only the 10.. bytes that are non-initial characters in a multi-byte sequence.
+ var m = encodeURIComponent(str).match(/%[89ABab]/g);
+ return str.length + (m ? m.length : 0);
+ }
+
return this.command({
- command: 'put '+priority+' '+delay+' '+ttr+' '+data.toString().length+'\r\n'+data+'\r\n',
+ command: 'put '+priority+' '+delay+' '+ttr+' '+lengthInUtf8Bytes(data.toString())+'\r\n'+data+'\r\n',
expected: 'INSERTED'
});
};

0 comments on commit 8954cef

Please sign in to comment.