|
@ -7,37 +7,21 @@ function Packet(type, size) { |
|
|
this.size = +size; |
|
|
this.size = +size; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
exports.name = "javascript"; |
|
|
function ReplyParser(return_buffers) { |
|
|
|
|
|
|
|
|
function ReplyParser(options) { |
|
|
|
|
|
this.name = exports.name; |
|
|
this.name = exports.name; |
|
|
this.options = options; |
|
|
this.return_buffers = return_buffers; |
|
|
|
|
|
|
|
|
this._buffer = null; |
|
|
this._buffer = null; |
|
|
this._offset = 0; |
|
|
this._offset = 0; |
|
|
this._encoding = "utf-8"; |
|
|
this._encoding = "utf-8"; |
|
|
this._reply_type = null; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
exports.Parser = ReplyParser; |
|
|
|
|
|
|
|
|
|
|
|
function IncompleteReadBuffer(message) { |
|
|
function IncompleteReadBuffer(message) { |
|
|
this.name = "IncompleteReadBuffer"; |
|
|
this.name = "IncompleteReadBuffer"; |
|
|
this.message = message; |
|
|
this.message = message; |
|
|
} |
|
|
} |
|
|
util.inherits(IncompleteReadBuffer, Error); |
|
|
util.inherits(IncompleteReadBuffer, Error); |
|
|
|
|
|
|
|
|
// Buffer.toString() is quite slow for small strings
|
|
|
|
|
|
function small_toString(buf, start, end) { |
|
|
|
|
|
var tmp = "", i; |
|
|
|
|
|
|
|
|
|
|
|
for (i = start; i < end; i++) { |
|
|
|
|
|
tmp += String.fromCharCode(buf[i]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return tmp; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ReplyParser.prototype._parseResult = function (type) { |
|
|
ReplyParser.prototype._parseResult = function (type) { |
|
|
var start, end, offset, packetHeader; |
|
|
var start, end, offset, packetHeader; |
|
|
|
|
|
|
|
@ -56,10 +40,8 @@ ReplyParser.prototype._parseResult = function (type) { |
|
|
|
|
|
|
|
|
if (type === 45) { |
|
|
if (type === 45) { |
|
|
return new Error(this._buffer.toString(this._encoding, start, end)); |
|
|
return new Error(this._buffer.toString(this._encoding, start, end)); |
|
|
} else if (this.options.return_buffers) { |
|
|
} else if (this.return_buffers) { |
|
|
return this._buffer.slice(start, end); |
|
|
return this._buffer.slice(start, end); |
|
|
} else if (end - start < 65536) { // completely arbitrary
|
|
|
|
|
|
return small_toString(this._buffer, start, end); |
|
|
|
|
|
} |
|
|
} |
|
|
return this._buffer.toString(this._encoding, start, end); |
|
|
return this._buffer.toString(this._encoding, start, end); |
|
|
} else if (type === 58) { // :
|
|
|
} else if (type === 58) { // :
|
|
@ -100,7 +82,7 @@ ReplyParser.prototype._parseResult = function (type) { |
|
|
throw new IncompleteReadBuffer("Wait for more data."); |
|
|
throw new IncompleteReadBuffer("Wait for more data."); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (this.options.return_buffers) { |
|
|
if (this.return_buffers) { |
|
|
return this._buffer.slice(start, end); |
|
|
return this._buffer.slice(start, end); |
|
|
} |
|
|
} |
|
|
return this._buffer.toString(this._encoding, start, end); |
|
|
return this._buffer.toString(this._encoding, start, end); |
|
@ -234,3 +216,6 @@ ReplyParser.prototype._packetEndOffset = function () { |
|
|
ReplyParser.prototype._bytesRemaining = function () { |
|
|
ReplyParser.prototype._bytesRemaining = function () { |
|
|
return (this._buffer.length - this._offset) < 0 ? 0 : (this._buffer.length - this._offset); |
|
|
return (this._buffer.length - this._offset) < 0 ? 0 : (this._buffer.length - this._offset); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
exports.Parser = ReplyParser; |
|
|
|
|
|
exports.name = "javascript"; |
|
|