Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

handle utf8 chars

  • Loading branch information...
commit 3fd0ece1a704a29c3c2aefb55a1aab7a8a99223f 1 parent 58a58e3
Jean-Sébastien Ney jney authored

Showing 2 changed files with 28 additions and 21 deletions. Show diff stats Hide diff stats

  1. +15 21 lib/beanstalk_client.js
  2. +13 0 tests/test_utf8.js
36 lib/beanstalk_client.js
@@ -16,7 +16,7 @@ var yaml = new function() {
16 16 .replace(/(\w)\-(\w)/mgi, '$1_$2') // replace minuses in hash names
17 17 .replace(/\n([\w\d_-]+)\:\ ([\.\,\w\d_-]+)/mig, '\n $1: \'$2\'') // format hashes
18 18 ;
19   -
  19 +
20 20 try {
21 21 return require('yaml').eval(corrected);
22 22 } catch(e) {
@@ -25,18 +25,18 @@ var yaml = new function() {
25 25 return str;
26 26 }
27 27 };
28   -};
  28 +};
29 29
30 30 // ###simple debug console
31 31 var Debug = new function() {
32 32 var active = false;
33   -
  33 +
34 34 this.log = function(str) {
35 35 if(active) {
36 36 console.log(str.toString());
37 37 }
38 38 };
39   -
  39 +
40 40 this.activate = function() {
41 41 active = true;
42 42 };
@@ -72,7 +72,7 @@ BeanstalkCommand.prototype.responseHandler = function(data, obj, callback) {
72 72 var lines = data.toString().split('\r\n');
73 73 var chunks = lines[0].split(' ');
74 74 var jobdata = false;
75   -
  75 +
76 76 if(obj.expected != chunks[0]) {
77 77 this.emit('command_done', chunks);
78 78 return false;
@@ -97,10 +97,10 @@ BeanstalkCommand.prototype.responseHandler = function(data, obj, callback) {
97 97 chunks = BeanstalkJob.create(chunks);
98 98 }
99 99 }
100   -
  100 +
101 101 this.emit('command_done', chunks);
102 102 }
103   -
  103 +
104 104 return true;
105 105 };
106 106
@@ -113,7 +113,7 @@ function BeanstalkClient() {
113 113 this.port = 11300;
114 114 this.conn;
115 115 this.default_priority = 10;
116   -
  116 +
117 117 this.queue = [];
118 118 this.waitingForResponses = false;
119 119 };
@@ -122,11 +122,11 @@ util.inherits(BeanstalkClient, events.EventEmitter);
122 122 // Singleton like method that returns an instance
123 123 BeanstalkClient.prototype.Instance = function(server) {
124 124 var p = [];
125   -
  125 +
126 126 if(server) {
127 127 p = server.split(':');
128 128 }
129   -
  129 +
130 130 this.address = (p[0]) ? p[0] : this.address;
131 131 this.port = (p[1]) ? p[1] : this.port;
132 132 return this;
@@ -134,7 +134,7 @@ BeanstalkClient.prototype.Instance = function(server) {
134 134
135 135 // executes command
136 136 BeanstalkClient.prototype.command = function(obj) {
137   - var _self = this;
  137 + var _self = this;
138 138 var cmd = new BeanstalkCommand();
139 139
140 140 // makes sure that if there's another command queued, it gets done
@@ -149,7 +149,7 @@ BeanstalkClient.prototype.command = function(obj) {
149 149 }
150 150 });
151 151
152   -
  152 +
153 153 // handles data that comes back from the connection
154 154 var dataHandler = function(data) {
155 155 Debug.log('response:');
@@ -170,7 +170,7 @@ BeanstalkClient.prototype.command = function(obj) {
170 170 _self.queue.push(data);
171 171 }
172 172 };
173   -
  173 +
174 174 if(!this.conn) {
175 175 // if there's no connection, create one
176 176 this.conn = net.createConnection(this.port, this.address);
@@ -200,7 +200,7 @@ BeanstalkClient.prototype.command = function(obj) {
200 200 this.conn.removeAllListeners('data');
201 201 requestExec(obj.command);
202 202 }
203   -
  203 +
204 204 this.conn.addListener('data', dataHandler);
205 205 return cmd;
206 206 };
@@ -255,15 +255,9 @@ BeanstalkClient.prototype.put = function(data, priority, delay, ttr) {
255 255 if(typeof ttr == 'undefined') {
256 256 ttr = 100000;
257 257 }
258   -
259   - function lengthInUtf8Bytes(str) {
260   - // Matches only the 10.. bytes that are non-initial characters in a multi-byte sequence.
261   - var m = encodeURIComponent(str).match(/%[89ABab]/g);
262   - return str.length + (m ? m.length : 0);
263   - }
264 258
265 259 return this.command({
266   - command: 'put '+priority+' '+delay+' '+ttr+' '+lengthInUtf8Bytes(data.toString())+'\r\n'+data+'\r\n',
  260 + command: 'put '+priority+' '+delay+' '+ttr+' '+Buffer.byteLength(data.toString(), 'utf8')+'\r\n'+data+'\r\n',
267 261 expected: 'INSERTED'
268 262 });
269 263 };
13 tests/test_utf8.js
... ... @@ -0,0 +1,13 @@
  1 +console.log('testing utf8');
  2 +var assert = require('assert');
  3 +var bs = require('../lib/beanstalk_client');
  4 +var client = bs.Client();
  5 +
  6 +client.use('default').onSuccess(function(data) {
  7 + client.put("ééééé", 100, 0).onSuccess(function(data) {
  8 + console.log(data);
  9 + assert.ok(!isNaN(data[0]));
  10 + client.disconnect();
  11 + });
  12 +});
  13 +

0 comments on commit 3fd0ece

Please sign in to comment.
Something went wrong with that request. Please try again.