Code coverage report for node/v2/init.js

Statements: 93.02% (40 / 43)      Branches: 83.33% (15 / 18)      Functions: 100% (5 / 5)      Lines: 97.56% (40 / 41)      Ignored: none     

All files » node/v2/ » init.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                                            1 1 1 1 1   1 1   1           1   1 2692 1346   1346 1346 1346 1346     1   1                 1 2672 1336   1336 1336 1336 1336     1   1               1 1346 1346 1346     1 1336 1336 1336     1 2682 5364 5364       2682    
// 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 TypedError = require('error/typed');
var bufrw = require('bufrw');
var WriteResult = bufrw.WriteResult;
var ReadResult = bufrw.ReadResult;
var header = require('./header');
 
module.exports.Request = InitRequest;
module.exports.Response = InitResponse;
 
var MissingInitHeaderError = TypedError({
    type: 'tchannel.missing-init-header',
    message: 'missing init frame header {field}',
    field: null
});
 
var RequiredHeaderFields = ['host_port', 'process_name'];
 
function InitRequest(version, headers) {
    if (!(this instanceof InitRequest)) {
        return new InitRequest(version, headers);
    }
    var self = this;
    self.type = InitRequest.TypeCode;
    self.version = version || 0;
    self.headers = headers || {};
}
 
InitRequest.TypeCode = 0x01;
 
InitRequest.RW = bufrw.Struct(InitRequest, [
    {call: {writeInto: writeFieldGuard}},
    {name: 'version', rw: bufrw.UInt16BE}, // version:2
    {name: 'headers', rw: header.header2}, // nh:2 (hk~2 hv~2){nh}
    {call: {readFrom: readFieldGuard}}
]);
 
// TODO: MissingInitHeaderError check / guard
 
function InitResponse(version, headers) {
    if (!(this instanceof InitResponse)) {
        return new InitResponse(version, headers);
    }
    var self = this;
    self.type = InitResponse.TypeCode;
    self.version = version || 0;
    self.headers = headers || {};
}
 
InitResponse.TypeCode = 0x02;
 
InitResponse.RW = bufrw.Struct(InitResponse, [
    {call: {writeInto: writeFieldGuard}},
    {name: 'version', rw: bufrw.UInt16BE}, // version:2
    {name: 'headers', rw: header.header2}, // nh:2 (hk~2 hv~2){nh}
    {call: {readFrom: readFieldGuard}}
]);
 
 
function writeFieldGuard(initBody, buffer, offset) {
    var err = requiredFieldGuard(initBody.headers);
    Iif (err) return WriteResult.error(err, offset);
    else return WriteResult.just(offset);
}
 
function readFieldGuard(initBody, buffer, offset) {
    var err = requiredFieldGuard(initBody.headers);
    Iif (err) return ReadResult.error(err, offset);
    else return ReadResult.just(offset);
}
 
function requiredFieldGuard(headers) {
    for (var i = 0; i < RequiredHeaderFields.length; i++) {
        var field = RequiredHeaderFields[i];
        Iif (headers[field] === undefined) {
            return MissingInitHeaderError({field: field});
        }
    }
    return null;
}