Code coverage report for node/v2/frame.js

Statements: 77.78% (49 / 63)      Branches: 71.43% (15 / 21)      Functions: 55.56% (5 / 9)      Lines: 79.03% (49 / 62)      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                                            1 1   1             1             1               1               1   1 12686 6343   6343 6343 6343 6343 3166   3177   6343     1 1 1 1     1     3177 3177 3177 3177     3177             3177 3177 3177           3177                   3177 3177 3177     3177 3177 3177     3166 3166         3166 3166 3166 3166           3166         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 TypedError = require('error/typed');
 
var BufferTooSmallError = TypedError({
    type: 'tchannel.buffer-too-small',
    message: 'buffer too small, needed {expected} only have {got} bytess',
    expected: null,
    got: null
});
 
var ExtraFrameDataError = TypedError({
    type: 'tchannel.extra-frame-data',
    message: 'got {trailing} bytes of extra frame data beyond body',
    frame: null,
    trailing: null
});
 
var FieldTooLargeError = TypedError({
    type: 'tchannel.field-too-large',
    message: '{field} too large at {length} bytes, cannot exceed {max} bytes',
    field: null,
    length: null,
    max: null
});
 
var InvalidFrameTypeError = TypedError({
    type: 'tchannel.invalid-frame-type',
    message: 'invalid frame type {typeNumber}',
    typeNumber: null
});
 
/* jshint maxparams:5 */
 
module.exports = Frame;
 
function Frame(id, body) {
    if (!(this instanceof Frame)) {
        return new 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.MaxBodySize = 0xffff - Frame.Overhead;
Frame.MaxId = 0xfffffffe;
Frame.NullId = 0xffffffff;
 
// size:2: type:1 reserved:1 id:4 reserved:8 ...
Frame.RW = bufrw.Struct(Frame, [
    {call: {
        writeInto: function writeInto(frame, buffer, offset) {
            var body = frame.body;
            var bodyRW = body.constructor.RW;
            var res = bodyRW.byteLength(body);
            Iif (res.err) {
                return bufrw.WriteResult.error(res.err);
            }
            Iif (res.length > Frame.MaxBodySize) {
                return bufrw.WriteResult.error(FieldTooLargeError({
                    field: 'frame body',
                    length: res.length,
                    max: Frame.MaxBodySize
                }));
            }
            frame.size = Frame.Overhead + res.length;
            var got = buffer.length - offset;
            Iif (got < frame.size) {
                return bufrw.WriteResult.error(BufferTooSmallError({
                    expected: frame.size,
                    got: got
                }));
            }
            return bufrw.WriteResult.just(offset);
        }
    }},
    {name: 'size', rw: bufrw.UInt16BE}, // size:2
    {name: 'type', rw: bufrw.UInt8},    // type:1
    {rw: bufrw.Skip(1)},                // reserved:1
    {name: 'id', rw: bufrw.UInt32BE},   // id:4
    {rw: bufrw.Skip(8)},                // reserved:8
    {name: 'body', call: {              // ...
        byteLength: function byteLength(frame) {
            var body = frame.body;
            var bodyRW = body.constructor.RW;
            return bodyRW.byteLength(body);
        },
        writeInto: function writeInto(frame, buffer, offset) {
            var body = frame.body;
            var bodyRW = body.constructor.RW;
            return bodyRW.writeInto(body, buffer, offset);
        },
        readFrom: function readBodyFrom(frame, buffer, offset) {
            var BodyType = Frame.Types[frame.type];
            Iif (!BodyType) {
                return bufrw.ReadResult.error(InvalidFrameTypeError({
                    typeNumber: frame.type
                }), offset);
            }
            var res = BodyType.RW.readFrom(buffer, offset);
            Iif (res.err) return res;
            offset = res.offset;
            Iif (offset < buffer.length) {
                return bufrw.ReadResult.error(ExtraFrameDataError({
                    frame: frame,
                    trailing: buffer.length - offset
                }), offset, frame);
            }
            return res;
        }
    }}
]);
 
Frame.prototype.fromBuffer = function toBuffer(buffer) {
    var self = this;
    return bufrw.fromBuffer(Frame.RW, self, buffer);
};
 
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 = {};