Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

handle unicode characters #12

Merged
merged 1 commit into from

2 participants

@jney

this is an alternate solution to handle #11
with tests

@pascalopitz pascalopitz merged commit 43c6555 into pascalopitz:master
@jney

nice. my test seems to fails on travis. i will add an environement variable to fix it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 25, 2013
  1. @jney

    handle utf8 chars

    jney authored
This page is out of date. Refresh to see the latest.
Showing with 28 additions and 21 deletions.
  1. +15 −21 lib/beanstalk_client.js
  2. +13 −0 tests/test_utf8.js
View
36 lib/beanstalk_client.js
@@ -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,11 +122,11 @@ 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;
@@ -134,7 +134,7 @@ BeanstalkClient.prototype.Instance = function(server) {
// 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
13 tests/test_utf8.js
@@ -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();
+ });
+});
+
Something went wrong with that request. Please try again.