All files / lib baseProxy.js

74.36% Statements 29/39
60% Branches 6/10
33.33% Functions 3/9
77.14% Lines 27/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                      1x 1x   1x 24x   24x 24x 24x 24x 24x   24x 24x 24x 24x                   1x                           1x 13x 6x   7x 7x               1x 3x             1x               1x               1x                 1x               1x       1x  
"use strict";
 
/**
 * Base proxy.
 *
 * @class
 * @abstract
 * @name BaseProxy
 * @arg {object} [opts] - Options.
 * @arg {?number} [opts.speed=null] - Proxy speed, kb/s.
 * @arg {?number} [opts.reqSpeed=null] - Requests speed, kb/s.
 * @arg {?number} [opts.resSpeed=null] - Responses speed, kb/s.
 * @arg {boolean} [opts.useCache=false] - Flag to use cache middleware.
 * @arg {number} [opts.reconnect=0] - Number of times reconnect to remote
 *  side, if it breaks connection.
 * @arg {timeout} [opts.timeout=60000] - Time to wait remote side response, ms.
 * @arg {port} [opts.port=0] - Port. Default is random.
 */
 
var _ = require("lodash");
var U = require("glace-utils");
 
var BaseProxy = function (opts) {
    opts = U.defVal(opts, {});
 
    this.isRunning = false;
    this.reqSpeed = U.defVal(opts.reqSpeed, opts.speed);
    this.resSpeed = U.defVal(opts.resSpeed, opts.speed);
    this.responsesData = null;
    this.useCache = U.defVal(opts.useCache, false);
 
    this._reconnect = U.defVal(opts.reconnect, 0);
    this._timeout = U.defVal(opts.timeout, 60000);
    this._port = U.defVal(opts.port, 0);
    this._proxy = null;
};
 
/**
 * Gets port number.
 *
 * @method
 * @return {number} Proxy port number.
 * @throws {Error} If port number is not defined and will be chosen randomly.
 */
BaseProxy.prototype.getPort = function () {
    if (this._port === 0) throw Error(
        "Port is not defined and will be chosen randomly on proxy start");
    return this._port;
};
 
/**
 * Sets proxy speed.
 *
 * @method
 * @arg {number|object} speed - Proxy speed, kb/s.
 * @arg {?number} [speed.req] - Requests speed, kb/s.
 * @arg {?number} [speed.res] - Responses speed, kb/s.
 */
BaseProxy.prototype.setSpeed = function (speed) {
    if (_.isNumber(speed)) {
        this.reqSpeed = this.resSpeed = speed;
    } else {
        if (speed.req !== undefined) this.reqSpeed = speed.req;
        if (speed.res !== undefined) this.resSpeed = speed.res;
    };
};
/**
 * Resets proxy speed.
 *
 * @method
 */
BaseProxy.prototype.resetSpeed = function () {
    this.reqSpeed = this.resSpeed = null;
};
/**
 * Starts to measure responses and gather information of them.
 *
 * @method
 */
BaseProxy.prototype.measureResponses = function () {
    this.responsesData = [];
};
/**
 * Disables responses measurement.
 *
 * @method
 */
BaseProxy.prototype.unmeasureResponses = function () {
    this.responsesData = null;
};
/**
 * Gets responses data.
 *
 * @method
 */
BaseProxy.prototype.getResponsesData = function () {
    if (this.responsesData === null) return null;
    return _.cloneDeep(this.responsesData);
};
/**
 * Starts proxy.
 *
 * @method
 */
BaseProxy.prototype.start = function () {
    throw new Error("Should be implemented in derived class");
};
/**
 * Stops proxy server.
 *
 * @method
 */
BaseProxy.prototype.stop = function () {
    throw new Error("Should be implemented in derived class");
};
 
module.exports = BaseProxy;