Skip to content

Commit

Permalink
Fixed submitting utf8 data by calculating job body data length correc…
Browse files Browse the repository at this point in the history
…tly, using Buffer.byteLength() for strings.

Finished the unit test by converting back to string in the unit test
before comparing what we get back to what we put in. The utf8 unit test
now passes.

Updated the documentation to note that put & all the peek commands
return Buffers.
  • Loading branch information
ceejbot committed Jul 4, 2012
1 parent 1c7df26 commit 7691503
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A straightforward and (nearly) complete [beanstalkd](http://kr.github.com/beanst

Heavily inspired by [node-beanstalk-client](https://github.com/benlund/node-beanstalk-client), which is a perfectly usable client but somewhat dusty. I wanted more complete support of the beanstalkd protocol in a project written in plain javascript.

All client method names are the same case & spelling as the beanstalk text command, with hyphens replaced by underscore.
All client method names are the same case & spelling as the beanstalk text command, with hyphens replaced by underscore. The single exception is "delete", which is renamed to "destroy".

For complete details on the beanstalkd commands, see [its protocol documentation](https://github.com/kr/beanstalkd/blob/master/doc/protocol.txt).

Expand Down Expand Up @@ -38,25 +38,25 @@ Responds with the name of the tube currently being used by the client.

`client.put(priority, delay, ttr, payload, function(err, jobid) {});`

Submit a job with the specified priority (smaller integers are higher priority), delay in seconds, and allowed time-to-run in seconds. The payload contains the job data the server will return to clients reserving jobs. No processing is done on the data. Responds with the id of the newly-created job.
Submit a job with the specified priority (smaller integers are higher priority), delay in seconds, and allowed time-to-run in seconds. The payload contains the job data the server will return to clients reserving jobs; it can be either a Buffer object or a string. No processing is done on the data. Responds with the id of the newly-created job.

#### peek_ready

`client.peek_ready(function(err, jobid, payload) {});`

Peek at the data for the job at the top of the ready queue of the tube currently in use. Responds with the job id and payload of the next job, or 'NOT_FOUND' if there are no qualifying jobs in the tube.
Peek at the data for the job at the top of the ready queue of the tube currently in use. Responds with the job id and payload of the next job, or 'NOT_FOUND' if there are no qualifying jobs in the tube. The payload is a Buffer object.

#### peek_delayed

`client.peek_delayed(function(err, jobid, payload) {});`

Peek at the data for the delayed job with the shortest delay in the tube currently in use. Responds with the job id and payload of the next job, or 'NOT_FOUND' if there are no qualifying jobs in the tube.
Peek at the data for the delayed job with the shortest delay in the tube currently in use. Responds with the job id and payload of the next job, or 'NOT_FOUND' if there are no qualifying jobs in the tube. The payload is a Buffer object.

#### peek_buried

`client.peek_buried(function(err, jobid, payload) {});`

Peek at the data for the next buried job in the tube currently in use. Responds with the job id and payload of the next job, or 'NOT_FOUND' if there are no qualifying jobs in the tube.
Peek at the data for the next buried job in the tube currently in use. Responds with the job id and payload of the next job, or 'NOT_FOUND' if there are no qualifying jobs in the tube. The payload is a Buffer object.

### Consuming jobs

Expand All @@ -82,13 +82,13 @@ Responds with an array containing the names of the tubes currently watched by th

`client.reserve(function(err, jobid, payload) {});`

Reserve a job. Responds with the id and the job data. The data is passed through untouched.
Reserve a job. Responds with the id and the job data. The payload is a Buffer object.

#### reserve_with_timeout

`client.reserve_with_timeout(seconds, function(err, jobid, payload) {});`

Reserve a job, waiting the specified number of seconds before timing out. *err* contains the string "TIMED_OUT" if the specified time elapsed before a job became available.
Reserve a job, waiting the specified number of seconds before timing out. *err* contains the string "TIMED_OUT" if the specified time elapsed before a job became available. Payload is a buffer.

#### touch

Expand Down Expand Up @@ -126,7 +126,7 @@ Kick at most *maxToKick* delayed and buried jobs back into the active queue. Res

`client.peek(id, function(err, jobid, payload) {});`

Peek at the data for the specified job.
Peek at the data for the specified job. Payload is a Buffer object.

#### pause_tube

Expand Down Expand Up @@ -278,4 +278,4 @@ If the handler paths don't start with `/` the current working directory will be

* Handle DEADLINE_SOON from the server.

* Hacky manner of exposing logging to the job handlers is hacky. Also too tied to the specific logging engine. Replace.
* Hacky manner of exposing logging to the job handlers is hacky. Also too tied to the specific logging engine. Replace.
17 changes: 14 additions & 3 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,21 +226,32 @@ function makeBeanstalkCommand(command, expectedResponse, sendsData)
// That's the case handled when args < 2.
return function()
{
var data, encoding;
var data, datalen, encoding;
var args = argHashToArray(arguments);
var callback = args.pop();
args.unshift(command);

if (sendsData)
{
data = args.pop();
args.push(data.length);
if (typeof data === 'string')
{
encoding = 'utf8';
datalen = Buffer.byteLength(data);
}
else
{
encoding = 'binary';
datalen = data.length;
}
args.push(datalen);
}

this.send.apply(this, args);
if (data)
{
encoding = (typeof data === 'string') ? 'utf8' : 'binary';
this.stream.write(data, encoding);
this.stream.write(data);
this.stream.write('\r\n');
}

Expand Down
6 changes: 3 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe('FiveBeansClient', function()
it('jobs can contain utf8 data', function(done)
{
var payload = "Many people like crème brûlée.";
var ptr = 0;
var returnString;
producer.put(0, 0, 60, payload, function(err, jobid)
{
should.not.exist(err);
Expand All @@ -206,9 +206,9 @@ describe('FiveBeansClient', function()
{
should.not.exist(err);
returnID.should.equal(jobid);

// we should get back exactly the same bytes we put in
returnPayload.should.equal(payload);
returnString = returnPayload.toString();
returnString.should.equal(payload);
consumer.destroy(returnID, function(err)
{
should.not.exist(err);
Expand Down

0 comments on commit 7691503

Please sign in to comment.