You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-09 00:22:08 +03:00
Minor changes
Move utility functions in lib/utils.js Improve the js parser in cases the buffer is incomplete Rename lib/parser to lib/parsers Fix smaller issues with test suite and fix parser errors not being catched Fixed wrong test for the new .end flush parameter Fixed test suite options being partly mutated Add some more tests
This commit is contained in:
@@ -18,7 +18,13 @@ HiredisReplyParser.prototype.execute = function (data) {
|
||||
var reply;
|
||||
this.reader.feed(data);
|
||||
while (true) {
|
||||
reply = this.reader.get();
|
||||
try {
|
||||
reply = this.reader.get();
|
||||
} catch (err) {
|
||||
// Protocol errors land here
|
||||
this.send_error(err);
|
||||
break;
|
||||
}
|
||||
|
||||
if (reply === undefined) {
|
||||
break;
|
@@ -30,14 +30,13 @@ ReplyParser.prototype._parseResult = function (type) {
|
||||
end = this._packetEndOffset() - 1;
|
||||
start = this._offset;
|
||||
|
||||
// include the delimiter
|
||||
this._offset = end + 2;
|
||||
|
||||
if (end > this._buffer.length) {
|
||||
this._offset = start;
|
||||
throw new IncompleteReadBuffer("Wait for more data.");
|
||||
}
|
||||
|
||||
// include the delimiter
|
||||
this._offset = end + 2;
|
||||
|
||||
if (type === 45) {
|
||||
return new Error(this._buffer.toString(this._encoding, start, end));
|
||||
} else if (this.return_buffers) {
|
||||
@@ -49,14 +48,13 @@ ReplyParser.prototype._parseResult = function (type) {
|
||||
end = this._packetEndOffset() - 1;
|
||||
start = this._offset;
|
||||
|
||||
// include the delimiter
|
||||
this._offset = end + 2;
|
||||
|
||||
if (end > this._buffer.length) {
|
||||
this._offset = start;
|
||||
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);
|
||||
} else if (type === 36) { // $
|
||||
@@ -74,14 +72,13 @@ ReplyParser.prototype._parseResult = function (type) {
|
||||
end = this._offset + packetHeader.size;
|
||||
start = this._offset;
|
||||
|
||||
// set the offset to after the delimiter
|
||||
this._offset = end + 2;
|
||||
|
||||
if (end > this._buffer.length) {
|
||||
this._offset = offset;
|
||||
throw new IncompleteReadBuffer("Wait for more data.");
|
||||
}
|
||||
|
||||
// set the offset to after the delimiter
|
||||
this._offset = end + 2;
|
||||
|
||||
if (this.return_buffers) {
|
||||
return this._buffer.slice(start, end);
|
||||
}
|
||||
@@ -157,6 +154,11 @@ ReplyParser.prototype.execute = function (buffer) {
|
||||
ret = this._parseResult(type);
|
||||
|
||||
this.send_reply(ret);
|
||||
} else if (type === 10 || type === 13) {
|
||||
break;
|
||||
} else {
|
||||
var err = new Error('Protocol error, got "' + String.fromCharCode(type) + '" as reply type byte');
|
||||
this.send_error(err);
|
||||
}
|
||||
} catch (err) {
|
||||
// catch the error (not enough data), rewind, and wait
|
@@ -1,14 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
function to_array(args) {
|
||||
var len = args.length,
|
||||
arr = new Array(len), i;
|
||||
|
||||
for (i = 0; i < len; i += 1) {
|
||||
arr[i] = args[i];
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
module.exports = to_array;
|
53
lib/utils.js
Normal file
53
lib/utils.js
Normal file
@@ -0,0 +1,53 @@
|
||||
'use strict';
|
||||
|
||||
// hgetall converts its replies to an Object. If the reply is empty, null is returned.
|
||||
function replyToObject(reply) {
|
||||
var obj = {}, j, jl, key, val;
|
||||
|
||||
if (reply.length === 0 || !Array.isArray(reply)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (j = 0, jl = reply.length; j < jl; j += 2) {
|
||||
key = reply[j].toString('binary');
|
||||
val = reply[j + 1];
|
||||
obj[key] = val;
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
function replyToStrings(reply) {
|
||||
var i;
|
||||
|
||||
if (Buffer.isBuffer(reply)) {
|
||||
return reply.toString();
|
||||
}
|
||||
|
||||
if (Array.isArray(reply)) {
|
||||
for (i = 0; i < reply.length; i++) {
|
||||
// Recusivly call the function as slowlog returns deep nested replies
|
||||
reply[i] = replyToStrings(reply[i]);
|
||||
}
|
||||
return reply;
|
||||
}
|
||||
|
||||
return reply;
|
||||
}
|
||||
|
||||
function toArray(args) {
|
||||
var len = args.length,
|
||||
arr = new Array(len), i;
|
||||
|
||||
for (i = 0; i < len; i += 1) {
|
||||
arr[i] = args[i];
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
reply_to_strings: replyToStrings,
|
||||
reply_to_object: replyToObject,
|
||||
to_array: toArray
|
||||
};
|
Reference in New Issue
Block a user