Code coverage report for node/v2/header.js

Statements: 92.41% (73 / 79)      Branches: 70% (14 / 20)      Functions: 100% (4 / 4)      Lines: 100% (72 / 72)      Ignored: none     

All files » node/v2/ » header.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                                            1 1 1           1 4 2   2 2 2 2 2   1   1 43 43 43 43   43   43 16 16 16 16   16 16 16     43     1 1726 1726 1726   1726   1726 2463 2463   2463 2463 2463 2463   2463     1726     1 1716 1716 1716 1716 1716 1716 1716   1716 1716 1716 1716   1716 2455   2455 2455 2451   2451 2         2449   2449 2449 2449   2449 2               2447   2447     1708     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 inherits = require('util').inherits;
var errors = require('../errors');
 
// TODO: different struct pattern that doesn't realize a temporary list of
// [key, val] tuples may be better. At the very least, such structure would
// allow for more precise error reporting.
 
function HeaderRW(countrw, keyrw, valrw) {
    if (!(this instanceof HeaderRW)) {
        return new HeaderRW(countrw, keyrw, valrw);
    }
    var self = this;
    self.countrw = countrw;
    self.keyrw = keyrw;
    self.valrw = valrw;
    bufrw.Base.call(self);
}
inherits(HeaderRW, bufrw.Base);
 
HeaderRW.prototype.byteLength = function byteLength(headers) {
    var self = this;
    var length = 0;
    var keys = Object.keys(headers);
    var res;
 
    length += self.countrw.width;
 
    for (var i = 0; i < keys.length; i++) {
        var key = keys[i];
        res = self.keyrw.byteLength(key);
        Iif (res.err) return res;
        length += res.length;
 
        res = self.valrw.byteLength(headers[key]);
        Iif (res.err) return res;
        length += res.length;
    }
 
    return bufrw.LengthResult.just(length);
};
 
HeaderRW.prototype.writeInto = function writeInto(headers, buffer, offset) {
    var self = this;
    var keys = Object.keys(headers);
    var res;
 
    res = self.countrw.writeInto(keys.length, buffer, offset);
 
    for (var i = 0; i < keys.length; i++) {
        Iif (res.err) return res;
        offset = res.offset;
 
        var key = keys[i];
        res = self.keyrw.writeInto(key, buffer, offset);
        Iif (res.err) return res;
        offset = res.offset;
 
        res = self.valrw.writeInto(headers[key], buffer, offset);
    }
 
    return res;
};
 
HeaderRW.prototype.readFrom = function readFrom(buffer, offset) {
    var self = this;
    var headers = {};
    var start = 0;
    var n = 0;
    var key = '';
    var val = '';
    var res;
 
    res = self.countrw.readFrom(buffer, offset);
    Iif (res.err) return res;
    offset = res.offset;
    n = res.value;
 
    for (var i = 0; i < n; i++) {
        start = offset;
 
        res = self.keyrw.readFrom(buffer, offset);
        if (res.err) return res;
        key = res.value;
 
        if (!key.length) {
            return bufrw.ReadResult.error(errors.NullKeyError({
                offset: offset,
                endOffset: res.offset
            }), offset, headers);
        }
        offset = res.offset;
 
        res = self.valrw.readFrom(buffer, offset);
        Iif (res.err) return res;
        val = res.value;
 
        if (headers[key] !== undefined) {
            return bufrw.ReadResult.error(errors.DuplicateHeaderKeyError({
                offset: start,
                endOffset: res.offset,
                key: key,
                value: val,
                priorValue: headers[key]
            }), offset, headers);
        }
        offset = res.offset;
 
        headers[key] = val;
    }
 
    return bufrw.ReadResult.just(offset, headers);
};
 
module.exports = HeaderRW;
 
// nh:1 (hk~1 hv~1){nh}
module.exports.header1 = HeaderRW(bufrw.UInt8, bufrw.str1, bufrw.str1);
 
// nh:2 (hk~2 hv~2){nh}
module.exports.header2 = HeaderRW(bufrw.UInt16BE, bufrw.str2, bufrw.str2);