/usr/lib/nodejs/node-redis/parser.js is in node-node-redis 0.1.7-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | // The MIT License
//
// Copyright (c) 2013 Tim Smart
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and
// to permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
var utils = require('./utils');
var RedisParser = function RedisParser () {
this.resetState();
process.EventEmitter.call(this);
return this;
};
module.exports = RedisParser;
RedisParser.prototype = Object.create(process.EventEmitter.prototype);
// Reset state, no matter where we are at.
RedisParser.prototype.resetState = function resetState () {
this.reply = null;
this.expected = null;
this.multi = null;
this.replies = null;
this.pos = null;
this.flag = 'TYPE';
this.data = null;
this.last_data = null;
this.remaining = null;
};
// Handle an incoming buffer.
RedisParser.prototype.onIncoming = function onIncoming (buffer) {
var char_code,
pos = this.pos || 0,
length = buffer.length;
// Make sure the buffer is joint properly.
if ('TYPE' !== this.flag && 'BULK' !== this.flag && null !== this.data) {
// We need to wind back a step.
// If we have CR now, it would break the parser.
if (0 !== this.data.length) {
char_code = this.data.charCodeAt(this.data.length - 1);
this.data = this.data.slice(0, -1);
--pos;
} else {
char_code = buffer[pos];
}
}
for (; length > pos;) {
switch (this.flag) {
case 'TYPE':
// What are we doing next?
switch (buffer[pos++]) {
// Single line status reply.
case 43: // + SINGLE
this.flag = 'SINGLE';
break;
// Tells us the length of upcoming data.
case 36: // $ LENGTH
this.flag = 'BULK_LENGTH';
break;
// Tells us how many args are coming up.
case 42: // * MULTI
this.flag = 'MULTI_BULK';
break;
case 58: // : INTEGER
this.flag = 'INTEGER';
break;
// Errors
case 45: // - ERROR
this.flag = 'ERROR';
break;
}
// Fast forward a char.
char_code = buffer[pos];
this.data = '';
break;
// Single line status replies.
case 'SINGLE':
case 'ERROR':
// Add char to the data
this.data += String.fromCharCode(char_code);
pos++;
// Optimize for the common use case.
if ('O' === this.data && 75 === buffer[pos]) { // OK
// Send off the reply.
this.data = 'OK';
this.onData();
pos += 3; // Skip the `K\r\n`
// break early.
break;
}
// Otherwise check for CR
char_code = buffer[pos];
if (13 === char_code) { // \r CR
// Send the reply.
if ('SINGLE' === this.flag) {
this.onData();
} else {
this.onError();
}
// Skip \r\n
pos += 2;
}
break;
// We have a integer coming up. Look for a CR
// then assume that is the end.
case 'BULK_LENGTH':
// We are still looking for more digits.
// char_code already set by TYPE state.
this.data += String.fromCharCode(char_code);
pos++;
// Is the next char the end? Set next char_code while
// we are at it.
char_code = buffer[pos];
if (13 === char_code) { // \r CR
// Cast to int
this.data = +this.data;
// Null reply?
if (-1 !== this.data) {
this.flag = 'BULK';
this.last_data = this.data;
this.data = null;
} else {
this.data = null;
this.onData();
}
// Skip the \r\n
pos += 2;
}
break;
// Short bulk reply.
case 'BULK':
if (null === this.data && length >= (pos + this.last_data)) {
// Slow slice is slow.
if (14 > this.last_data) {
this.data = new Buffer(this.last_data);
for (var i = 0; i < this.last_data; i++) {
this.data[i] = buffer[i + pos];
}
} else {
this.data = buffer.slice(pos, this.last_data + pos);
}
// Fast forward past data.
pos += this.last_data + 2;
// Send it off.
this.onData();
} else if (this.data) {
// Still joining. pos = amount left to go.
if (this.remaining <= length) {
// End is within this buffer.
if (13 < this.remaining) {
buffer.copy(this.data, this.last_data - this.remaining, 0, this.remaining)
} else {
utils.copyBuffer(buffer, this.data, this.last_data - this.remaining, 0, this.remaining);
}
// Fast forward past data.
pos = this.remaining + 2;
this.remaining = null;
this.onData();
} else {
// We have more to come. Copy what we got then move on,
// decrementing the amount we have copied from this.remaining
if (13 < (this.remaining - length)) {
utils.copyBuffer(buffer, this.data, this.last_data - this.remaining, 0, length);
} else {
buffer.copy(this.data, this.last_data - this.remaining, 0, length);
}
// More to go.
this.remaining -= length;
pos = length;
}
} else {
// We will have to do a join.
this.data = new Buffer(this.last_data);
// Fast copy if small.
if (15 > this.last_data) {
utils.copyBuffer(buffer, this.data, 0, pos);
} else {
buffer.copy(this.data, 0, pos)
}
// Point pos to the amount we need.
this.remaining = this.last_data - (length - pos);
pos = length;
}
break;
// How many bulk's are coming?
case 'MULTI_BULK':
// We are still looking for more digits.
// char_code already set by TYPE state.
this.data += String.fromCharCode(char_code);
pos++;
// Is the next char the end? Set next char_code while
// we are at it.
char_code = buffer[pos];
if (13 === char_code) { // \r CR
// Cast to int
this.last_data = +this.data;
this.data = null;
// Are we multi?
if (null === this.expected) {
this.expected = this.last_data;
this.reply = [];
} else if (null === this.multi) {
this.multi = this.expected;
this.expected = null;
this.replies = [];
}
// Skip the \r\n
pos += 2;
this.flag = 'TYPE';
// Zero length replies.
if (0 === this.last_data) {
this.expected = this.reply = null;
this.data = [];
this.onData();
break;
} else if (-1 === this.last_data) {
// NIL reply.
this.expected = this.reply = null;
this.data = null;
this.onData();
break;
}
char_code = buffer[pos];
// Will have to look ahead to check for another MULTI in case
// we are a multi transaction.
if (36 === char_code) { // $ - BULK_LENGTH
// We are bulk data.
this.flag = 'BULK_LENGTH';
// We are skipping the TYPE check. Skip the $
pos++;
// We need to set char code and data.
char_code = buffer[pos];
this.data = '';
} else if (null === this.multi && char_code) {
// Multi trans time.
this.multi = this.expected;
this.expected = null;
this.replies = [];
}
}
break;
case 'INTEGER':
// We are still looking for more digits.
// char_code already set by TYPE state.
this.data += String.fromCharCode(char_code);
pos++;
// Is the next char the end? Set next char_code while
// we are at it.
char_code = buffer[pos];
if (13 === char_code) { // \r CR
// Cast to int
this.data = +this.data;
this.onData();
// Skip the \r\n
pos += 2;
}
break;
}
}
// In case we have multiple packets.
this.pos = pos - length;
};
// When we have recieved a chunk of response data.
RedisParser.prototype.onData = function onData () {
if (null !== this.expected) {
// Decrement the expected data replies and add the data.
this.reply.push(this.data);
this.expected--;
// Finished? Send it off.
if (0 === this.expected) {
if (null !== this.multi) {
this.replies.push(this.reply);
this.multi--;
if (0 === this.multi) {
this.emit('reply', this.replies);
this.replies = this.multi = null;
}
} else {
this.emit('reply', this.reply);
}
this.reply = this.expected = null;
}
} else {
if (null === this.multi) {
this.emit('reply', this.data);
} else {
this.replies.push(this.data);
this.multi--;
if (0 === this.multi) {
this.emit('reply', this.replies);
this.replies = this.multi = null;
}
}
}
this.last_data = null;
this.data = null;
this.flag = 'TYPE';
};
// Found an error.
RedisParser.prototype.onError = function onError () {
if (null === this.multi) {
this.emit('error', this.data);
} else {
this.replies.push(this.data);
this.multi--;
if (0 === this.multi) {
this.emit('reply', this.replies);
this.replies = this.multi = null;
}
}
this.last_data = null;
this.data = null;
this.flag = 'TYPE';
};
|