Code coverage report for lib/parsers/javascript.js

Statements: 98.1% (103 / 105)      Branches: 95.92% (47 / 49)      Functions: 100% (8 / 8)      Lines: 98.1% (103 / 105)      Ignored: 1 statement, 1 branch     

All files » lib/parsers/ » 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    13   13 999 999 999     13 34 34   13   13 7975         7975   2596 2596   2596         2596   2596 1785 811   705   106 5379     4905   4905     4905 107     4798 4798   4798 29       4769   4769 474 473 473   473 6     467 5 5     462 462   462   462 4000   4000     4000 3992     454       13 2581   2581   2581 6522   6522 2544     3978 3978   3978 1733   1733 2245 90   90 2155 552   552 1603 1192   1168 411     408   408   398 3 2   1 1         35 35         13     2581 2545 2545 2545     36 36     13 5378     5378   5378     13 7974   7974 22422     22422 1       7974 7974     13 6989     13 13  
'use strict';
 
var util   = require('util');
 
function JavascriptReplyParser() {
    this.name = exports.name;
    this._buffer            = new Buffer(0);
    this._offset            = 0;
}
 
function IncompleteReadBuffer(message) {
    this.name = 'IncompleteReadBuffer';
    this.message = message;
}
util.inherits(IncompleteReadBuffer, Error);
 
JavascriptReplyParser.prototype._parseResult = function (type) {
    var start = 0,
        end = 0,
        offset = 0,
        packetHeader = 0;
 
    if (type === 43 || type === 58 || type === 45) { // + or : or -
        // up to the delimiter
        end = this._packetEndOffset() - 1;
        start = this._offset;
 
        Iif (end > this._buffer.length) {
            throw new IncompleteReadBuffer('Wait for more data.');
        }
 
        // include the delimiter
        this._offset = end + 2;
 
        if (type === 43) {
            return this._buffer.slice(start, end);
        } else if (type === 58) {
            // return the coerced numeric value
            return +this._buffer.toString('ascii', start, end);
        }
        return new Error(this._buffer.toString('utf-8', 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 = this.parseHeader();
 
        // packets with a size of -1 are considered null
        if (packetHeader === -1) {
            return null;
        }
 
        end = this._offset + packetHeader;
        start = this._offset;
 
        if (end > this._buffer.length) {
            throw new IncompleteReadBuffer('Wait for more data.');
        }
 
        // set the offset to after the delimiter
        this._offset = end + 2;
 
        return this._buffer.slice(start, end);
    } else if (type === 42) { // *
        offset = this._offset;
        packetHeader = this.parseHeader();
 
        if (packetHeader === -1) {
            return null;
        }
 
        if (packetHeader > 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; i++) {
            ntype = this._buffer[this._offset++];
 
            Iif (this._offset > this._buffer.length) {
                throw new IncompleteReadBuffer('Wait for more data.');
            }
            res = this._parseResult(ntype);
            reply.push(res);
        }
 
        return reply;
    }
};
 
JavascriptReplyParser.prototype.execute = function (buffer) {
    this.append(buffer);
 
    var type, ret, offset;
 
    while (true) {
        offset = this._offset;
        // at least 4 bytes: :1\r\n
        if (this._bytesRemaining() < 4) {
            break;
        }
 
        try {
            type = this._buffer[this._offset++];
 
            if (type === 43) { // Strings +
                ret = this._parseResult(type);
 
                this.send_reply(ret);
            } else  if (type === 45) { // Errors -
                ret = this._parseResult(type);
 
                this.send_error(ret);
            } else if (type === 58) { // Integers :
                ret = this._parseResult(type);
 
                this.send_reply(ret);
            } else if (type === 36) { // Bulk strings $
                ret = this._parseResult(type);
 
                this.send_reply(ret);
            } else if (type === 42) { // Arrays *
                // 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);
            } else if (type === 10 || type === 13) {
                break;
            } else {
                var err = new Error('Protocol error, got "' + String.fromCharCode(type) + '" as reply type byte');
                this.send_error(err);
            }
        } catch (err) {
            // catch the error (not enough data), rewind, and wait
            // for the next packet to appear
            this._offset = offset;
            break;
        }
    }
};
 
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(),
        value = this._buffer.toString('ascii', this._offset, end - 1) | 0;
 
    this._offset = end + 1;
 
    return value;
};
 
JavascriptReplyParser.prototype._packetEndOffset = function () {
    var offset = this._offset;
 
    while (this._buffer[offset] !== 0x0d && this._buffer[offset + 1] !== 0x0a) {
        offset++;
 
        /* istanbul ignore if: activate the js parser out of memory test to test this */
        Iif (offset >= this._buffer.length) {
            throw new IncompleteReadBuffer('Did not see LF after NL reading multi bulk count (' + offset + ' => ' + this._buffer.length + ', ' + this._offset + ')');
        }
    }
 
    offset++;
    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';