Code coverage report for node/v2/frame.js

Statements: 83.96% (89 / 106)      Branches: 58.06% (18 / 31)      Functions: 50% (4 / 8)      Lines: 88.89% (88 / 99)      Ignored: none     

All files » node/v2/ » frame.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                                            1 1   1       1   1 12494 12494 12494 12494 6202   6292   12494     1 1 1 1 1     1   1 2 2   2 2 2 2 2 2   2 2 2   2     1 6202   6202   6202 6202 6202 6202   6202 6202 6202 6202   6202 6202           6202   6202 6202 6202 6202   6202   6202 6202                 6202 6202   6202 6202     1 6292 6292   6292 6292 6292     6292   6292 6292 6292   6292 6292 6292   6292 6292 6292   6292 6292 6292   6292 6292 6287   6287 6287 6287 6287   6287     1       1         1         1         1  
// Copyright (c) 2015 Uber Technologies, Inc.
 
// 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.
 
'use strict';
 
var bufrw = require('bufrw');
var errors = require('../errors');
 
var Types = require('./index.js').Types;
 
/* jshint maxparams:5 */
 
module.exports = Frame;
 
function Frame(id, body) {
    var self = this;
    self.size = 0;
    self.type = (body && body.type) || 0;
    if (id === null || id === undefined) {
        self.id = Frame.NullId;
    } else {
        self.id = id;
    }
    self.body = body;
}
 
Frame.Overhead = 0x10;
Frame.MaxSize = 0xffff;
Frame.MaxBodySize = Frame.MaxSize - Frame.Overhead;
Frame.MaxId = 0xfffffffe;
Frame.NullId = 0xffffffff;
 
// size:2: type:1 reserved:1 id:4 reserved:8 ...
Frame.RW = bufrw.Base(frameLength, readFrameFrom, writeFrameInto);
 
function frameLength(frame) {
    var body = frame.body;
    var bodyRW = body.constructor.RW;
 
    var length = 0;
    length += bufrw.UInt16BE.width; // size:2:
    length += bufrw.UInt8.width;    // type:1
    length += 1;                    // reserved:1
    length += bufrw.UInt32BE.width; // id:4
    length += 8;                    // reserved:8 ...
 
    var res = bodyRW.byteLength(body);
    Eif (!res.err) {
        res.length += length;
    }
    return res;
}
 
function readFrameFrom(buffer, offset) {
    var frame = new Frame();
 
    var res;
 
    res = bufrw.UInt16BE.readFrom(buffer, offset);
    Iif (res.err) return res;
    offset = res.offset;
    frame.size = res.value;
 
    res = bufrw.UInt8.readFrom(buffer, offset);
    Iif (res.err) return res;
    offset = res.offset;
    frame.type = res.value;
 
    var BodyType = Frame.Types[frame.type];
    Iif (!BodyType) {
        return bufrw.ReadResult.error(errors.InvalidFrameTypeError({
            typeNumber: frame.type
        }), offset - 1);
    }
 
    offset += 1;
 
    res = bufrw.UInt32BE.readFrom(buffer, offset);
    Iif (res.err) return res;
    offset = res.offset;
    frame.id = res.value;
 
    offset += 8;
 
    res = BodyType.RW.readFrom(buffer, offset);
    Iif (res.err) {
        if (frame.type === Types.CallRequest ||
            frame.type === Types.CallRequestCont
        ) {
            // TODO: wrapped?
            res.err.frameId = frame.id;
        }
        return res;
    }
    offset = res.offset;
    frame.body = res.value;
 
    res.value = frame;
    return res;
}
 
function writeFrameInto(frame, buffer, offset) {
    var body = frame.body;
    var bodyRW = body.constructor.RW;
 
    var start = offset;
    var end = offset;
    var res;
 
    // skip size, write later
    offset += bufrw.UInt16BE.width;
 
    res = bufrw.UInt8.writeInto(frame.type, buffer, offset);
    Iif (res.err) return res;
    offset = res.offset;
 
    end = offset + 1;
    buffer.fill(0, offset, end);
    offset = end;
 
    res = bufrw.UInt32BE.writeInto(frame.id, buffer, offset);
    Iif (res.err) return res;
    offset = res.offset;
 
    end = offset + 8;
    buffer.fill(0, offset, end);
    offset = end;
 
    res = bodyRW.writeInto(body, buffer, offset);
    if (res.err) return res;
    offset = res.offset;
 
    frame.size = res.offset - start;
    res = bufrw.UInt16BE.writeInto(frame.size, buffer, start);
    Iif (res.err) return res;
    res.offset = offset;
 
    return res;
}
 
Frame.fromBuffer = function fromBuffer(buffer) {
    return bufrw.fromBuffer(Frame.RW, buffer, 0);
};
 
Frame.prototype.byteLength = function byteLength() {
    var self = this;
    return bufrw.byteLength(Frame.RW, self);
};
 
Frame.prototype.intoBuffer = function intoBuffer(buffer) {
    var self = this;
    return bufrw.intoBuffer(Frame.RW, self, buffer);
};
 
Frame.prototype.toBuffer = function toBuffer() {
    var self = this;
    return bufrw.toBuffer(Frame.RW, self);
};
 
Frame.Types = {};