Code coverage report for src/rpc.js

Statements: 100% (30 / 30)      Branches: 100% (12 / 12)      Functions: 100% (5 / 5)      Lines: 100% (30 / 30)      Ignored: none     

All files » src/ » rpc.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          1 955 955 955       955 955   955   955 208 747 208   539 539         955 955                     1 24 24 23                       1 12 12 12 6                             1 643 19 19 18         1
/**
 * @class Rpc
 * @constructor
 */
 
function Rpc(fn, klass, serialize) {
    this._fn = fn;
    this._klass = klass;
    this._serialize = serialize || Rpc._makeDefaultSerialize(fn.length);
 
    // directionality is determined by the prefix, rpc for bidirectional, s2c
    // for server -> client, or c2s for client -> server
    this._toClient = false;
    this._toServer = false;
 
    var name = fn.name || fn.wasabiFnName;
 
    if (/^s2c/.test(name)) {
        this._toClient = true;
    } else if (/^c2s/.test(name)) {
        this._toServer = true;
    } else {
        this._toClient = true;
        this._toServer = true;
    }
 
    // parse the number and names of the arguments in the function definition
    // this seems ridiculously hacky, but works on any ECMA-compliant platform
    var funStr = fn.toString();
    this._args =  funStr.slice(funStr.indexOf('(') + 1, funStr.indexOf(')')).match(/([\w]+)/g) || [];
}
 
/**
 * Populate `obj` with keys corresponding to the names of the argument in the
 * nth position of the original function and values from the indexed values in
 * `obj`
 * @method _populateKeys
 * @private
 * @param {Object} obj The target object containing indexed values
 */
Rpc.prototype._populateKeys = function (obj) {
    var i;
    for (i = 0; i < obj.length; i++) {
        obj[this._args[i]] = obj[i];
    }
};
 
/**
 * Populate `obj` with keys corresponding to the names of the argument in the
 * nth position of the original function and values from the indexed values in
 * `obj`
 * @method _populateIndexes
 * @private
 * @param {Object} obj The target object containing indexed values
 */
Rpc.prototype._populateIndexes = function (obj) {
    var i;
    for (i = 0; i < this._args.length; i++) {
        if (obj[i] === undefined) {
            obj[i] = obj[this._args[i]];
        }
    }
};
 
/**
 * Creates a default serialization function for an RPC definition when no args
 * function is supplied. The resultant function simply calls `desc.any(i, 16)`
 * the appropriate number of times. This means that by default, RPC arguments
 * have 16 bit precision.
 * @method _makeDefaultSerialize
 * @static
 * @param {Number} nargs The number of arguments to serialize
 * @return The serialize function
 */
Rpc._makeDefaultSerialize = function (nargs) {
    return function _defaultSerialize(desc) {
        var i;
        for (i = 0; i < nargs; i++) {
            desc.any(i, 16);
        }
    };
};
 
module.exports = Rpc;