Skip to content

Commit

Permalink
some cleanup and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JerrySievert committed Jul 8, 2012
1 parent 7ec1815 commit 4048349
Showing 1 changed file with 24 additions and 29 deletions.
53 changes: 24 additions & 29 deletions lib/parser/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,46 @@ exports.Parser = FasterReplyParser;
FasterReplyParser.prototype._parseResult = function (type) {
var start, end, offset, packetHeader;

if (type === 43 || type === 45) { // +
if (type === 43 || type === 45) { // + or -
// up to the delimiter
end = this._packetEndOffset() - 1;
start = this._offset;

// include the delimiter
this._offset = end + 2;

if (this.options.return_buffers) {
return this._buffer.slice(start, end);
} else {
return this._buffer.slice(start, end).toString(this._encoding);
return this._buffer.toString(this._encoding, start, end);
}
} else if (type === 58) { // :
// up to the delimiter
end = this._packetEndOffset() - 1;
start = this._offset;

// include the delimiter
this._offset = end + 2;

// return the coerced numeric value
return +this._buffer.toString(this._encoding, start, end);
} else if (type === 36) { // $
// set a rewind point, as the packet could be larger than the
// buffer in memory
offset = this._offset - 1;

packetHeader = new Packet(type, this.parseHeader());

if (packetHeader.size === null) {
this._offset++;

return null;
}

// packets with a size of -1 are considered null
if (packetHeader.size === -1) {
return null;
}


end = this._offset + packetHeader.size;
start = this._offset;

// set the offset to after the delimiter
this._offset = end + 2;

if (end > this._buffer.length) {
Expand All @@ -73,7 +77,7 @@ FasterReplyParser.prototype._parseResult = function (type) {
if (this.options.return_buffers) {
return this._buffer.slice(start, end);
} else {
return this._buffer.slice(start, end).toString(this._encoding);
return this._buffer.toString(this._encoding, start, end);
}
} else if (type === 42) { // *
offset = this._offset;
Expand Down Expand Up @@ -113,7 +117,7 @@ FasterReplyParser.prototype.execute = function (buffer) {
try {
var ret;

// at least 4 bytes: *1\r\n
// at least 4 bytes: :1\r\n
if (this._bytesRemaining() < 4) {
break;
}
Expand All @@ -123,7 +127,7 @@ FasterReplyParser.prototype.execute = function (buffer) {
if (type === 43) { // +
ret = this._parseResult(type);
this.send_reply(ret);
} else if (type === 45) {
} else if (type === 45) { // -
ret = this._parseResult(type);
this.send_error(ret);
} else if (type === 58) { // :
Expand All @@ -132,11 +136,10 @@ FasterReplyParser.prototype.execute = function (buffer) {
} else if (type === 36) { // $
ret = this._parseResult(type);

if (ret === null) {
break;
}
this.send_reply(ret);
} else if (type === 42) { // *
// set a rewind point. if a failure occurs,
// wait for the next execute()/append() and try again
offset = this._offset - 1;
ret = this._parseResult(type);
if (ret === -1) {
Expand All @@ -147,6 +150,8 @@ FasterReplyParser.prototype.execute = function (buffer) {
this.send_reply(ret);
}
} catch(err) {
// catch the error (not enough data), rewind, and wait
// for the next packet to appear
this._offset = offset;
break;
}
Expand All @@ -158,26 +163,23 @@ FasterReplyParser.prototype.append = function(newBuffer) {
return;
}

var oldBuffer = this._buffer;
if (!oldBuffer) {
// first run
if (this._buffer === null) {
this._buffer = newBuffer;

return;
}

var bytesRemaining = this._bytesRemaining();

var newLength = bytesRemaining + newBuffer.length;

if (bytesRemaining === 0) {
// out of data
if (this._offset >= this._buffer.length) {
this._buffer = newBuffer;
this._offset = 0;

return;
}

// very large packet
this._buffer = Buffer.concat([this._buffer.slice(this._offset), newBuffer]);

this._offset = 0;
};

Expand All @@ -190,13 +192,6 @@ FasterReplyParser.prototype.parseHeader = function () {
return value;
};

FasterReplyParser.prototype.parseBuffer = function(length) {
var buffer = this._buffer.slice(this._offset, this._offset + length);

this._offset += length;
return buffer;
};

FasterReplyParser.prototype._packetEndOffset = function () {
var offset = this._offset;

Expand Down

0 comments on commit 4048349

Please sign in to comment.