1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-07 13:22:56 +03:00

Fix some minor issues and add more tests

Do not mutate the options object and add some more tests
This commit is contained in:
Ruben Bridgewater
2015-10-17 00:49:41 +02:00
parent 587b1c95a0
commit 2a65ee48dd
16 changed files with 633 additions and 405 deletions

View File

@@ -2,12 +2,10 @@
var util = require('util');
function ReplyParser() {
function JavascriptReplyParser() {
this.name = exports.name;
this._buffer = new Buffer(0);
this._offset = 0;
this._encoding = 'utf-8';
}
function IncompleteReadBuffer(message) {
@@ -16,13 +14,13 @@ function IncompleteReadBuffer(message) {
}
util.inherits(IncompleteReadBuffer, Error);
ReplyParser.prototype._parseResult = function (type) {
JavascriptReplyParser.prototype._parseResult = function (type) {
var start = 0,
end = 0,
offset = 0,
packetHeader = 0;
if (type === 43 || type === 45) { // + or -
if (type === 43 || type === 58 || type === 45) { // + or : or -
// up to the delimiter
end = this._packetEndOffset() - 1;
start = this._offset;
@@ -34,24 +32,13 @@ ReplyParser.prototype._parseResult = function (type) {
// include the delimiter
this._offset = end + 2;
if (type === 45) {
return new Error(this._buffer.toString(this._encoding, start, end));
if (type === 43) {
return this._buffer.slice(start, end);
} else if (type === 58) {
// return the coerced numeric value
return +this._buffer.toString('ascii', start, end);
}
return this._buffer.slice(start, end);
} else if (type === 58) { // :
// up to the delimiter
end = this._packetEndOffset() - 1;
start = this._offset;
if (end > this._buffer.length) {
throw new IncompleteReadBuffer('Wait for more data.');
}
// include the delimiter
this._offset = end + 2;
// return the coerced numeric value
return +this._buffer.toString('ascii', start, end);
return new Error(this._buffer.toString('utf-8', start, end));
} else if (type === 36) { // $
// set a rewind point, as the packet could be larger than the
// buffer in memory
@@ -107,7 +94,7 @@ ReplyParser.prototype._parseResult = function (type) {
}
};
ReplyParser.prototype.execute = function (buffer) {
JavascriptReplyParser.prototype.execute = function (buffer) {
this.append(buffer);
var type, ret, offset;
@@ -161,7 +148,7 @@ ReplyParser.prototype.execute = function (buffer) {
}
};
ReplyParser.prototype.append = function (newBuffer) {
JavascriptReplyParser.prototype.append = function (newBuffer) {
// out of data
if (this._offset >= this._buffer.length) {
@@ -174,7 +161,7 @@ ReplyParser.prototype.append = function (newBuffer) {
this._offset = 0;
};
ReplyParser.prototype.parseHeader = function () {
JavascriptReplyParser.prototype.parseHeader = function () {
var end = this._packetEndOffset(),
value = this._buffer.toString('ascii', this._offset, end - 1) | 0;
@@ -183,7 +170,7 @@ ReplyParser.prototype.parseHeader = function () {
return value;
};
ReplyParser.prototype._packetEndOffset = function () {
JavascriptReplyParser.prototype._packetEndOffset = function () {
var offset = this._offset;
while (this._buffer[offset] !== 0x0d && this._buffer[offset + 1] !== 0x0a) {
@@ -199,9 +186,9 @@ ReplyParser.prototype._packetEndOffset = function () {
return offset;
};
ReplyParser.prototype._bytesRemaining = function () {
JavascriptReplyParser.prototype._bytesRemaining = function () {
return (this._buffer.length - this._offset) < 0 ? 0 : (this._buffer.length - this._offset);
};
exports.Parser = ReplyParser;
exports.Parser = JavascriptReplyParser;
exports.name = 'javascript';