Code coverage report for node/as/thrift.js

Statements: 89.47% (68 / 76)      Branches: 66.67% (20 / 30)      Functions: 100% (8 / 8)      Lines: 89.47% (68 / 76)      Ignored: none     

All files » node/as/ » thrift.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                                            1   1   1 2 2 2   2 2     1   2   2 2   2 2   2   1   2         2 2     2     2   2   1 2       2     2 2 2 1   1     2   2     2 2 2 1   1               1   2   2   2 2   2 2         2 2     2   2   1 2       2 2       2 2 1   1         2   2         1 2 2 2 2       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 assert = require('assert');
 
module.exports = TChannelAsThrift;
 
function TChannelAsThrift(opts) {
    var self = this;
    assert(opts && opts.spec, 'TChannelAsThrift expected spec');
    self.spec = opts.spec;
 
    var bossMode = opts && opts.bossMode;
    self.bossMode = typeof bossMode === 'boolean' ? bossMode : false;
}
 
TChannelAsThrift.prototype.register =
function register(channel, name, opts, handle) {
    var self = this;
 
    var argsName = name + '_args';
    var argsType = self.spec.getType(argsName);
 
    var returnName = name + '_result';
    var resultType = self.spec.getType(returnName);
 
    channel.register(name, handleThriftRequest);
 
    function handleThriftRequest(req, res, inHeadBuffer, inBodyBuffer) {
 
        Iif (req.headers.as !== 'thrift') {
            return res.sendError('BadRequest', 'Expected as=thrift TChannel request header');
        }
 
        // Process incoming thrift body
        var inBodyResult = argsType.fromBuffer(inBodyBuffer);
        Iif (inBodyResult.err) {
            return res.sendError('BadRequest', inBodyResult.err.message);
        }
        var inBody = inBodyResult.value;
 
        // TODO process inHeadBuffer into inHead
        var inHead = null;
 
        handle(opts, req, inHead, inBody, handleThriftResponse);
 
        function handleThriftResponse(err, thriftRes) {
            Iif (err) {
                return res.sendError('UnexpectedError', err.message);
            }
 
            assert(typeof thriftRes.ok === 'boolean',
                'expected response.ok to be a boolean');
 
            var outResult = {};
            var outBody = thriftRes.body;
            if (!thriftRes.ok) {
                outResult[outBody.nameAsThrift] = outBody;
            } else {
                outResult.success = outBody;
            }
 
            var outRes = resultType.toBuffer(outResult);
 
            Iif (outRes.err && self.bossMode) {
                return res.sendError('UnexpectedError', outRes.err.message);
            } else {
                var outHeadBuffer = null;
                var outBodyBuffer = outRes.toValue();
                if (thriftRes.ok) {
                    return res.sendOk(outHeadBuffer, outBodyBuffer);
                } else {
                    return res.sendNotOk(outHeadBuffer, outBodyBuffer);
                }
            }
        }
    }
};
 
/* jshint maxparams:5 */
TChannelAsThrift.prototype.send =
function send(request, endpoint, outHead, outBody, callback) {
    var self = this;
 
    assert(typeof endpoint === 'string', 'send requires endpoint');
 
    var argsType = self.spec.getType(endpoint + '_args');
    var resultType = self.spec.getType(endpoint + '_result');
 
    var outRes = argsType.toBuffer(outBody);
    Iif (outRes.err) {
        callback(outRes.err, null);
        return;
    }
 
    var outHeadBuffer = null;
    var outBodyBuffer = outRes.value;
 
    // Punch as=thrift into the transport headers
    request.headers.as = "thrift";
 
    request.send(endpoint, outHeadBuffer, outBodyBuffer, handleResponse);
 
    function handleResponse(err, res, arg2, arg3) {
        Iif (err) {
            return callback(err);
        }
 
        var inBodyResult = resultType.fromBuffer(arg3);
        Iif (inBodyResult.err) {
            return inBodyResult.toCallback(callback); // TODO WrappedError
        }
 
        var inBody;
        if (res.ok) {
            inBody = inBodyResult.value.success;
        } else {
            inBody = onlyProperty(inBodyResult.value);
        }
 
        // TODO translate inHeadBuffer into inHead
        // var inHeadBuffer = arg2;
        var inHead = null;
 
        callback(null, new Response(res.ok, inHead, inBody));
    }
 
};
 
function Response(ok, head, body) {
    var self = this;
    self.ok = ok;
    self.head = head;
    self.body = body;
}
 
// TODO proper Thriftify result union that reifies as the selected field.
function onlyProperty(object) {
    for (var name in object) {
        Eif (object[name] !== null) {
            object[name].nameAsThrift = name;
            return object[name];
        }
    }
}