Code coverage report for node/v2/lazy_frame.js

Statements: 90.91% (50 / 55)      Branches: 50% (6 / 12)      Functions: 100% (6 / 6)      Lines: 90.91% (50 / 55)      Ignored: none     

All files » node/v2/ » lazy_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                                            1 1 1   1   1   1 4   4 4 4 4 4   4 4       1   1 1 1   1 1 1 1 1     1 1 1       1         1 1 1     1     1 1     1 3     3 3 3     3     3   3 3   3           3     1 2           2 2       2 2    
// 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 assert = require('assert');
var bufrw = require('bufrw');
var errors = require('../errors');
 
var Frame = require('./frame.js');
 
module.exports = LazyFrame;
 
function LazyFrame(size, type, id, buffer) {
    var self = this;
 
    self.isLazy = true;
    self.size = size;
    self.type = type;
    self.id = id;
    self.buffer = buffer;
 
    self.body = null;
    self.bodyRW = null;
}
 
// size:2 type:1 reserved:1 id:4 reserved:8 ...
LazyFrame.RW = bufrw.Base(lazyFrameLength, readLazyFrameFrom, writeLazyFrameInto);
 
LazyFrame.TypeOffset = 2;
LazyFrame.IdOffset = 2 + 1 + 1;
LazyFrame.BodyOffset = Frame.Overhead;
 
LazyFrame.prototype.setId = function setId(id) {
    var self = this;
    assert.ok(self.buffer, 'must have a buffer supplied');
    self.id = id;
    self.buffer.writeUInt32BE(self.id, LazyFrame.IdOffset);
};
 
LazyFrame.prototype.readBody = function readBody() {
    var self = this;
    Iif (self.body) {
        return bufrw.ReadResult.just(self.body);
    }
 
    Iif (!self.buffer) {
        // TODO: typed error
        return bufrw.ReadResult.error(new Error('no buffer to read from'));
    }
 
    var res = self.bodyRW.readFrom(self.buffer, LazyFrame.BodyOffset);
    Eif (!res.err) {
        self.body = res.value;
    }
 
    return res;
};
 
function lazyFrameLength(lazyFrame) {
    return bufrw.LengthResult.just(lazyFrame.size);
}
 
function readLazyFrameFrom(buffer, offset) {
    var start = offset;
 
    // size:2:
    var size = buffer.readUInt16BE(offset);
    offset += size;
    var frameBuffer = buffer.slice(start, offset);
 
    // type:1
    var type = frameBuffer.readUInt8(LazyFrame.TypeOffset);
 
    // id:4
    var id = frameBuffer.readUInt32BE(LazyFrame.IdOffset);
 
    var lazyFrame = new LazyFrame(size, type, id, frameBuffer);
    lazyFrame.bodyRW = Frame.Types[lazyFrame.type].RW;
 
    Iif (!lazyFrame.bodyRW) {
        return bufrw.ReadResult.error(errors.InvalidFrameTypeError({
            typeNumber: lazyFrame.type
        }), offset + LazyFrame.TypeOffset);
    }
 
    return bufrw.ReadResult.just(offset, lazyFrame);
}
 
function writeLazyFrameInto(lazyFrame, buffer, offset) {
    Iif (!lazyFrame.buffer) {
        return bufrw.WriteResult.error(errors.CorruptWriteLazyFrame({
            context: 'missing buffer'
        }));
    }
 
    var remain = buffer.length - offset;
    Iif (lazyFrame.size > remain) {
        return bufrw.WriteResult.shortError(lazyFrame.size, remain, offset);
    }
 
    offset += lazyFrame.buffer.copy(buffer, offset, 0, lazyFrame.size);
    return bufrw.WriteResult.just(offset);
}