|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var util = require('util'); |
| 4 | + |
| 5 | +function JavascriptReplyParser(return_buffers) { |
| 6 | + this.name = 'javascript'; |
| 7 | + this.buffer = new Buffer(0); |
| 8 | + this.offset = 0; |
| 9 | + this.bigStrSize = 0; |
| 10 | + this.chunksSize = 0; |
| 11 | + this.buffers = []; |
| 12 | + this.type = 0; |
| 13 | + this.protocolError = false; |
| 14 | + this.offsetCache = 0; |
| 15 | + if (return_buffers) { |
| 16 | + this.handleReply = function (start, end) { |
| 17 | + return this.buffer.slice(start, end); |
| 18 | + }; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +JavascriptReplyParser.prototype.handleReply = function (start, end) { |
| 23 | + return this.buffer.toString('utf-8', start, end); |
| 24 | +}; |
| 25 | + |
| 26 | +function IncompleteReadBuffer(message) { |
| 27 | + this.name = 'IncompleteReadBuffer'; |
| 28 | + this.message = message; |
| 29 | +} |
| 30 | +util.inherits(IncompleteReadBuffer, Error); |
| 31 | + |
| 32 | +JavascriptReplyParser.prototype.parseResult = function (type) { |
| 33 | + var start = 0, |
| 34 | + end = 0, |
| 35 | + packetHeader = 0, |
| 36 | + reply; |
| 37 | + |
| 38 | + if (type === 36) { // $ |
| 39 | + packetHeader = this.parseHeader(); |
| 40 | + // Packets with a size of -1 are considered null |
| 41 | + if (packetHeader === -1) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + end = this.offset + packetHeader; |
| 45 | + start = this.offset; |
| 46 | + if (end + 2 > this.buffer.length) { |
| 47 | + this.buffers.push(this.offsetCache === 0 ? this.buffer : this.buffer.slice(this.offsetCache)); |
| 48 | + this.chunksSize = this.buffers[0].length; |
| 49 | + // Include the packetHeader delimiter |
| 50 | + this.bigStrSize = packetHeader + 2; |
| 51 | + throw new IncompleteReadBuffer('Wait for more data.'); |
| 52 | + } |
| 53 | + // Set the offset to after the delimiter |
| 54 | + this.offset = end + 2; |
| 55 | + return this.handleReply(start, end); |
| 56 | + } else if (type === 58) { // : |
| 57 | + // Up to the delimiter |
| 58 | + end = this.packetEndOffset(); |
| 59 | + start = this.offset; |
| 60 | + // Include the delimiter |
| 61 | + this.offset = end + 2; |
| 62 | + // Return the coerced numeric value |
| 63 | + return +this.buffer.toString('ascii', start, end); |
| 64 | + } else if (type === 43) { // + |
| 65 | + end = this.packetEndOffset(); |
| 66 | + start = this.offset; |
| 67 | + this.offset = end + 2; |
| 68 | + return this.handleReply(start, end); |
| 69 | + } else if (type === 42) { // * |
| 70 | + packetHeader = this.parseHeader(); |
| 71 | + if (packetHeader === -1) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + reply = []; |
| 75 | + for (var i = 0; i < packetHeader; i++) { |
| 76 | + if (this.offset >= this.buffer.length) { |
| 77 | + throw new IncompleteReadBuffer('Wait for more data.'); |
| 78 | + } |
| 79 | + reply.push(this.parseResult(this.buffer[this.offset++])); |
| 80 | + } |
| 81 | + return reply; |
| 82 | + } else if (type === 45) { // - |
| 83 | + end = this.packetEndOffset(); |
| 84 | + start = this.offset; |
| 85 | + this.offset = end + 2; |
| 86 | + return new Error(this.buffer.toString('utf-8', start, end)); |
| 87 | + } else { |
| 88 | + return void 0; |
| 89 | + } |
| 90 | +}; |
| 91 | + |
| 92 | +JavascriptReplyParser.prototype.execute = function (buffer) { |
| 93 | + if (this.chunksSize !== 0) { |
| 94 | + if (this.bigStrSize > this.chunksSize + buffer.length) { |
| 95 | + this.buffers.push(buffer); |
| 96 | + this.chunksSize += buffer.length; |
| 97 | + return; |
| 98 | + } |
| 99 | + this.buffers.push(buffer); |
| 100 | + this.buffer = Buffer.concat(this.buffers, this.chunksSize + buffer.length); |
| 101 | + this.buffers = []; |
| 102 | + this.bigStrSize = 0; |
| 103 | + this.chunksSize = 0; |
| 104 | + } else if (this.offset >= this.buffer.length) { |
| 105 | + this.buffer = buffer; |
| 106 | + } else { |
| 107 | + this.buffer = Buffer.concat([this.buffer.slice(this.offset), buffer]); |
| 108 | + } |
| 109 | + this.offset = 0; |
| 110 | + this.protocolError = true; |
| 111 | + this.run(); |
| 112 | +}; |
| 113 | + |
| 114 | +JavascriptReplyParser.prototype.tryParsing = function () { |
| 115 | + try { |
| 116 | + return this.parseResult(this.type); |
| 117 | + } catch (err) { |
| 118 | + // Catch the error (not enough data), rewind if it's an array, |
| 119 | + // and wait for the next packet to appear |
| 120 | + this.offset = this.offsetCache; |
| 121 | + this.protocolError = false; |
| 122 | + return void 0; |
| 123 | + } |
| 124 | +}; |
| 125 | + |
| 126 | +JavascriptReplyParser.prototype.run = function (buffer) { |
| 127 | + // Set a rewind point. If a failure occurs, wait for the next execute()/append() and try again |
| 128 | + this.offsetCache = this.offset; |
| 129 | + this.type = this.buffer[this.offset++]; |
| 130 | + var reply = this.tryParsing(); |
| 131 | + |
| 132 | + while (reply !== undefined) { |
| 133 | + if (this.type === 45) { // Errors - |
| 134 | + this.returnError(reply); |
| 135 | + } else { |
| 136 | + this.returnReply(reply); // Strings + // Integers : // Bulk strings $ // Arrays * |
| 137 | + } |
| 138 | + this.offsetCache = this.offset; |
| 139 | + this.type = this.buffer[this.offset++]; |
| 140 | + reply = this.tryParsing(); |
| 141 | + } |
| 142 | + if (this.type !== undefined && this.protocolError === true) { |
| 143 | + // Reset the buffer so the parser can handle following commands properly |
| 144 | + this.buffer = new Buffer(0); |
| 145 | + this.returnFatalError(new Error('Protocol error, got ' + JSON.stringify(String.fromCharCode(this.type)) + ' as reply type byte')); |
| 146 | + } |
| 147 | +}; |
| 148 | + |
| 149 | +JavascriptReplyParser.prototype.parseHeader = function () { |
| 150 | + var end = this.packetEndOffset(), |
| 151 | + value = this.buffer.toString('ascii', this.offset, end) | 0; |
| 152 | + |
| 153 | + this.offset = end + 2; |
| 154 | + return value; |
| 155 | +}; |
| 156 | + |
| 157 | +JavascriptReplyParser.prototype.packetEndOffset = function () { |
| 158 | + var offset = this.offset, |
| 159 | + len = this.buffer.length - 1; |
| 160 | + |
| 161 | + while (this.buffer[offset] !== 0x0d && this.buffer[offset + 1] !== 0x0a) { |
| 162 | + offset++; |
| 163 | + |
| 164 | + if (offset >= len) { |
| 165 | + throw new IncompleteReadBuffer('Did not see LF after NL reading multi bulk count (' + offset + ' => ' + this.buffer.length + ', ' + this.offset + ')'); |
| 166 | + } |
| 167 | + } |
| 168 | + return offset; |
| 169 | +}; |
| 170 | + |
| 171 | +module.exports = JavascriptReplyParser; |
0 commit comments