You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-07 13:22:56 +03:00
@@ -1,11 +1,12 @@
|
||||
'use strict';
|
||||
|
||||
var util = require('util');
|
||||
var util = require('util');
|
||||
|
||||
function JavascriptReplyParser() {
|
||||
this.name = exports.name;
|
||||
this._buffer = new Buffer(0);
|
||||
this._offset = 0;
|
||||
this._buffer = new Buffer(0);
|
||||
this._offset = 0;
|
||||
this._buffers = [];
|
||||
}
|
||||
|
||||
function IncompleteReadBuffer(message) {
|
||||
@@ -66,7 +67,7 @@ JavascriptReplyParser.prototype._parseResult = function (type) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (packetHeader > this._bytesRemaining()) {
|
||||
if (packetHeader > this._buffer.length - this._offset) {
|
||||
this._offset = offset - 1;
|
||||
throw new IncompleteReadBuffer('Wait for more data.');
|
||||
}
|
||||
@@ -93,14 +94,37 @@ JavascriptReplyParser.prototype._parseResult = function (type) {
|
||||
};
|
||||
|
||||
JavascriptReplyParser.prototype.execute = function (buffer) {
|
||||
this.append(buffer);
|
||||
var i = buffer.length - 1;
|
||||
|
||||
var type, offset;
|
||||
while (buffer[i] !== 0x0a) {
|
||||
i--;
|
||||
if (i < 1) {
|
||||
this._buffers.push(buffer);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (this._buffers.length !== 0) {
|
||||
this._buffers.unshift(this._offset === 0 ? this._buffer : this._buffer.slice(this._offset));
|
||||
this._buffers.push(buffer);
|
||||
this._buffer = Buffer.concat(this._buffers);
|
||||
this._buffers = [];
|
||||
} else if (this._offset >= this._buffer.length) {
|
||||
this._buffer = buffer;
|
||||
} else {
|
||||
this._buffer = Buffer.concat([this._buffer.slice(this._offset), buffer]);
|
||||
}
|
||||
this._offset = 0;
|
||||
this.run();
|
||||
};
|
||||
|
||||
JavascriptReplyParser.prototype.run = function (buffer) {
|
||||
var type, offset = this._offset;
|
||||
|
||||
while (true) {
|
||||
offset = this._offset;
|
||||
// at least 4 bytes: :1\r\n
|
||||
if (this._bytesRemaining() < 4) {
|
||||
if (this._buffer.length - this._offset < 4) {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -109,7 +133,7 @@ JavascriptReplyParser.prototype.execute = function (buffer) {
|
||||
|
||||
if (type === 43 || type === 58 || type === 36) { // Strings + // Integers : // Bulk strings $
|
||||
this.send_reply(this._parseResult(type));
|
||||
} else if (type === 45) { // Errors -
|
||||
} else if (type === 45) { // Errors -
|
||||
this.send_error(this._parseResult(type));
|
||||
} else if (type === 42) { // Arrays *
|
||||
// set a rewind point. if a failure occurs,
|
||||
@@ -130,24 +154,11 @@ JavascriptReplyParser.prototype.execute = function (buffer) {
|
||||
}
|
||||
};
|
||||
|
||||
JavascriptReplyParser.prototype.append = function (newBuffer) {
|
||||
|
||||
// out of data
|
||||
if (this._offset >= this._buffer.length) {
|
||||
this._buffer = newBuffer;
|
||||
this._offset = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
this._buffer = Buffer.concat([this._buffer.slice(this._offset), newBuffer]);
|
||||
this._offset = 0;
|
||||
};
|
||||
|
||||
JavascriptReplyParser.prototype.parseHeader = function () {
|
||||
var end = this._packetEndOffset() + 1,
|
||||
value = this._buffer.toString('ascii', this._offset, end - 1) | 0;
|
||||
var end = this._packetEndOffset(),
|
||||
value = this._buffer.toString('ascii', this._offset, end) | 0;
|
||||
|
||||
this._offset = end + 1;
|
||||
this._offset = end + 2;
|
||||
|
||||
return value;
|
||||
};
|
||||
@@ -167,9 +178,5 @@ JavascriptReplyParser.prototype._packetEndOffset = function () {
|
||||
return offset;
|
||||
};
|
||||
|
||||
JavascriptReplyParser.prototype._bytesRemaining = function () {
|
||||
return (this._buffer.length - this._offset) < 0 ? 0 : (this._buffer.length - this._offset);
|
||||
};
|
||||
|
||||
exports.Parser = JavascriptReplyParser;
|
||||
exports.name = 'javascript';
|
||||
|
Reference in New Issue
Block a user