Permalink
Browse files

handle utf8 chars

  • Loading branch information...
1 parent 58a58e3 commit 3fd0ece1a704a29c3c2aefb55a1aab7a8a99223f @jney jney committed Feb 25, 2013
Showing with 28 additions and 21 deletions.
  1. +15 −21 lib/beanstalk_client.js
  2. +13 −0 tests/test_utf8.js
View
@@ -16,7 +16,7 @@ var yaml = new function() {
.replace(/(\w)\-(\w)/mgi, '$1_$2') // replace minuses in hash names
.replace(/\n([\w\d_-]+)\:\ ([\.\,\w\d_-]+)/mig, '\n $1: \'$2\'') // format hashes
;
-
+
try {
return require('yaml').eval(corrected);
} catch(e) {
@@ -25,18 +25,18 @@ var yaml = new function() {
return str;
}
};
-};
+};
// ###simple debug console
var Debug = new function() {
var active = false;
-
+
this.log = function(str) {
if(active) {
console.log(str.toString());
}
};
-
+
this.activate = function() {
active = true;
};
@@ -72,7 +72,7 @@ BeanstalkCommand.prototype.responseHandler = function(data, obj, callback) {
var lines = data.toString().split('\r\n');
var chunks = lines[0].split(' ');
var jobdata = false;
-
+
if(obj.expected != chunks[0]) {
this.emit('command_done', chunks);
return false;
@@ -97,10 +97,10 @@ BeanstalkCommand.prototype.responseHandler = function(data, obj, callback) {
chunks = BeanstalkJob.create(chunks);
}
}
-
+
this.emit('command_done', chunks);
}
-
+
return true;
};
@@ -113,7 +113,7 @@ function BeanstalkClient() {
this.port = 11300;
this.conn;
this.default_priority = 10;
-
+
this.queue = [];
this.waitingForResponses = false;
};
@@ -122,19 +122,19 @@ util.inherits(BeanstalkClient, events.EventEmitter);
// Singleton like method that returns an instance
BeanstalkClient.prototype.Instance = function(server) {
var p = [];
-
+
if(server) {
p = server.split(':');
}
-
+
this.address = (p[0]) ? p[0] : this.address;
this.port = (p[1]) ? p[1] : this.port;
return this;
};
// executes command
BeanstalkClient.prototype.command = function(obj) {
- var _self = this;
+ var _self = this;
var cmd = new BeanstalkCommand();
// makes sure that if there's another command queued, it gets done
@@ -149,7 +149,7 @@ BeanstalkClient.prototype.command = function(obj) {
}
});
-
+
// handles data that comes back from the connection
var dataHandler = function(data) {
Debug.log('response:');
@@ -170,7 +170,7 @@ BeanstalkClient.prototype.command = function(obj) {
_self.queue.push(data);
}
};
-
+
if(!this.conn) {
// if there's no connection, create one
this.conn = net.createConnection(this.port, this.address);
@@ -200,7 +200,7 @@ BeanstalkClient.prototype.command = function(obj) {
this.conn.removeAllListeners('data');
requestExec(obj.command);
}
-
+
this.conn.addListener('data', dataHandler);
return cmd;
};
@@ -255,15 +255,9 @@ BeanstalkClient.prototype.put = function(data, priority, delay, ttr) {
if(typeof ttr == 'undefined') {
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+' '+lengthInUtf8Bytes(data.toString())+'\r\n'+data+'\r\n',
+ command: 'put '+priority+' '+delay+' '+ttr+' '+Buffer.byteLength(data.toString(), 'utf8')+'\r\n'+data+'\r\n',
expected: 'INSERTED'
});
};
View
@@ -0,0 +1,13 @@
+console.log('testing utf8');
+var assert = require('assert');
+var bs = require('../lib/beanstalk_client');
+var client = bs.Client();
+
+client.use('default').onSuccess(function(data) {
+ client.put("ééééé", 100, 0).onSuccess(function(data) {
+ console.log(data);
+ assert.ok(!isNaN(data[0]));
+ client.disconnect();
+ });
+});
+

0 comments on commit 3fd0ece

Please sign in to comment.