Code coverage report for lib/parser/javascript.js

Statements: 89.31% (142 / 159)      Branches: 82.43% (61 / 74)      Functions: 92.31% (12 / 13)      Lines: 89.31% (142 / 159)      Ignored: none     

All files » lib/parser/ » javascript.js
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 3023     3 880 880     3 3   3 26 26   26 26 26 26 26     3   3   3 5 5   3     3 1315   1315 3341     1315     3 1322   1322   179 179     179   179         179 6   173 173         1143   263 263     263   263         263 1       262 880     737   737     737 18     719 719     719   719 3 3     716 26   690   143 143 143   143 3     140 2 2     138 138   138   138 765   765     765 761 16   761     134       3 180   180   180 731 731   731 174     557   557 164   164       164 393 12   12       12 381 196   196       196 185 59   58           58 2     58 126     126   126   122         6 1   5 5         3 180         180 26   26       154 149 149   149         5 5                       5     3 880     880   880     3 1322   1322 3369   3369         1322 1322     3 871     3       3 12     3 540    
var events = require("events"),
    util   = require("../util");
 
function Packet(type, size) {
    this.type = type;
    this.size = +size;
}
 
exports.name = "javascript";
exports.debug_mode = false;
 
function ReplyParser(options) {
    this.name = exports.name;
    this.options = options || { };
 
    this._buffer            = null;
    this._offset            = 0;
    this._encoding          = "utf-8";
    this._debug_mode        = options.debug_mode;
    this._reply_type        = null;
}
 
util.inherits(ReplyParser, events.EventEmitter);
 
exports.Parser = ReplyParser;
 
function IncompleteReadBuffer(message) {
    this.name = "IncompleteReadBuffer";
    this.message = message;
}
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) {
    var start, end, offset, packetHeader;
 
    if (type === 43 || type === 45) { // + or -
        // up to the delimiter
        end = this._packetEndOffset() - 1;
        start = this._offset;
 
        // include the delimiter
        this._offset = end + 2;
 
        Iif (end > this._buffer.length) {
            this._offset = start;
            throw new IncompleteReadBuffer("Wait for more data.");
        }
 
        if (this.options.return_buffers) {
            return this._buffer.slice(start, end);
        } else {
            Eif (end - start < 65536) { // completely arbitrary
                return small_toString(this._buffer, start, end);
            } else {
                return this._buffer.toString(this._encoding, start, end);
            }
        }
    } else if (type === 58) { // :
        // up to the delimiter
        end = this._packetEndOffset() - 1;
        start = this._offset;
 
        // include the delimiter
        this._offset = end + 2;
 
        Iif (end > this._buffer.length) {
            this._offset = start;
            throw new IncompleteReadBuffer("Wait for more data.");
        }
 
        if (this.options.return_buffers) {
            return this._buffer.slice(start, end);
        }
 
        // return the coerced numeric value
        return +small_toString(this._buffer, start, end);
    } else if (type === 36) { // $
        // set a rewind point, as the packet could be larger than the
        // buffer in memory
        offset = this._offset - 1;
 
        packetHeader = new Packet(type, this.parseHeader());
 
        // packets with a size of -1 are considered null
        if (packetHeader.size === -1) {
            return undefined;
        }
 
        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.");
        }
 
        if (this.options.return_buffers) {
            return this._buffer.slice(start, end);
        } else {
            return this._buffer.toString(this._encoding, start, end);
        }
    } else Eif (type === 42) { // *
        offset = this._offset;
        packetHeader = new Packet(type, this.parseHeader());
 
        if (packetHeader.size < 0) {
            return null;
        }
 
        if (packetHeader.size > this._bytesRemaining()) {
            this._offset = offset - 1;
            throw new IncompleteReadBuffer("Wait for more data.");
        }
 
        var reply = [ ];
        var ntype, i, res;
 
        offset = this._offset - 1;
 
        for (i = 0; i < packetHeader.size; i++) {
            ntype = this._buffer[this._offset++];
 
            Iif (this._offset > this._buffer.length) {
                throw new IncompleteReadBuffer("Wait for more data.");
            }
            res = this._parseResult(ntype);
            if (res === undefined) {
                res = null;
            }
            reply.push(res);
        }
 
        return reply;
    }
};
 
ReplyParser.prototype.execute = function (buffer) {
    this.append(buffer);
 
    var type, ret, offset;
 
    while (true) {
        offset = this._offset;
        try {
            // at least 4 bytes: :1\r\n
            if (this._bytesRemaining() < 4) {
                break;
            }
 
            type = this._buffer[this._offset++];
 
            if (type === 43) { // +
                ret = this._parseResult(type);
 
                Iif (ret === null) {
                    break;
                }
 
                this.send_reply(ret);
            } else  if (type === 45) { // -
                ret = this._parseResult(type);
 
                Iif (ret === null) {
                    break;
                }
 
                this.send_error(ret);
            } else if (type === 58) { // :
                ret = this._parseResult(type);
 
                Iif (ret === null) {
                    break;
                }
 
                this.send_reply(ret);
            } else if (type === 36) { // $
                ret = this._parseResult(type);
 
                Iif (ret === null) {
                    break;
                }
 
                // check the state for what is the result of
                // a -1, set it back up for a null reply
                if (ret === undefined) {
                    ret = null;
                }
 
                this.send_reply(ret);
            } else Eif (type === 42) { // *
                // set a rewind point. if a failure occurs,
                // wait for the next execute()/append() and try again
                offset = this._offset - 1;
 
                ret = this._parseResult(type);
 
                this.send_reply(ret);
            }
        } catch (err) {
            // catch the error (not enough data), rewind, and wait
            // for the next packet to appear
            if (! (err instanceof IncompleteReadBuffer)) {
              throw err;
            }
            this._offset = offset;
            break;
        }
    }
};
 
ReplyParser.prototype.append = function (newBuffer) {
    Iif (!newBuffer) {
        return;
    }
 
    // first run
    if (this._buffer === null) {
        this._buffer = newBuffer;
 
        return;
    }
 
    // out of data
    if (this._offset >= this._buffer.length) {
        this._buffer = newBuffer;
        this._offset = 0;
 
        return;
    }
 
    // very large packet
    // check for concat, if we have it, use it
    Eif (Buffer.concat !== undefined) {
        this._buffer = Buffer.concat([this._buffer.slice(this._offset), newBuffer]);
    } else {
        var remaining = this._bytesRemaining(),
            newLength = remaining + newBuffer.length,
            tmpBuffer = new Buffer(newLength);
 
        this._buffer.copy(tmpBuffer, 0, this._offset);
        newBuffer.copy(tmpBuffer, remaining, 0);
 
        this._buffer = tmpBuffer;
    }
 
    this._offset = 0;
};
 
ReplyParser.prototype.parseHeader = function () {
    var end   = this._packetEndOffset(),
        value = small_toString(this._buffer, this._offset, end - 1);
 
    this._offset = end + 1;
 
    return value;
};
 
ReplyParser.prototype._packetEndOffset = function () {
    var offset = this._offset;
 
    while (this._buffer[offset] !== 0x0d && this._buffer[offset + 1] !== 0x0a) {
        offset++;
 
        Iif (offset >= this._buffer.length) {
            throw new IncompleteReadBuffer("didn't see LF after NL reading multi bulk count (" + offset + " => " + this._buffer.length + ", " + this._offset + ")");
        }
    }
 
    offset++;
    return offset;
};
 
ReplyParser.prototype._bytesRemaining = function () {
    return (this._buffer.length - this._offset) < 0 ? 0 : (this._buffer.length - this._offset);
};
 
ReplyParser.prototype.parser_error = function (message) {
    this.emit("error", message);
};
 
ReplyParser.prototype.send_error = function (reply) {
    this.emit("reply error", reply);
};
 
ReplyParser.prototype.send_reply = function (reply) {
    this.emit("reply", reply);
};