All files / lib/middleware speed.js

88.89% Statements 64/72
100% Branches 16/16
76.47% Functions 13/17
87.3% Lines 55/63

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 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              1x   1x   1x                       1x   7x   6x 6x 6x 6x   6x 6x   6x 6x   4x 1x     3x   2x       2x 1x 1x     2x                   1x   6x   5x 5x 5x 5x   5x 5x   5x 5x   2x   1x   1x       1x     5x 5x   2x   1x   1x         1x 1x 1x                         1x 12x 28x 28x   12x                     3x 3x   2x 1x 1x 1x   2x    
"use strict";
/**
 * Middleware to manage responses speed.
 *
 * @module
 */
 
var U = require("glace-utils");
 
var MAX_PACKAGE_SIZE = 8192; // empirical value
 
var self = module.exports = function () {
    self.__changeReqSpeed(this);
    self.__changeResSpeed(this);
    return false;
};
/**
 * Helper to change requests speed.
 *
 * @ignore
 * @function
 * @arg {BaseProxy} ctx - Proxy instance. 
 */
self.__changeReqSpeed = ctx => {
 
    if (ctx.reqSpeed == null) return;
 
    var req = ctx.req;
    var timer = 1000;
    var size = Math.ceil(ctx.reqSpeed * 128);
    var prms = Promise.resolve();
 
    [size, timer] = self.__balance(size, timer);
    var promisify = self.__promisify(size, timer);
 
    var emit = req.emit;
    req.emit = function (ev, chunk) {
 
        if (!["data", "end"].includes(ev)) {
            return emit.apply(this, arguments);
        };
 
        if (size === 0) return; // reject requests if speed is zero
 
        prms = promisify(prms, chunk, chk => {
            return emit.call(this, "data", chk);
        });
 
        if (ev === "end") {
            prms = prms.then(() => {
                return emit.call(this, "end");
            });
        };
        return prms;
    };
};
/**
 * Helper to change responses speed.
 *
 * @ignore
 * @function
 * @arg {BaseProxy} ctx - Proxy instance.
 */
self.__changeResSpeed = ctx => {
 
    if (ctx.resSpeed == null) return;
 
    var res = ctx.res;
    var timer = 1000;
    var size = Math.ceil(ctx.resSpeed * 128);
    var prms = Promise.resolve();
 
    [size, timer] = self.__balance(size, timer);
    var promisify = self.__promisify(size, timer);
 
    var write = res.write;
    res.write = function () {
 
        if (size === 0) return; // reject responses if speed is zero
 
        var args = Array.from(arguments);
 
        prms = promisify(prms, args[0], chk => {
            args[0] = chk;
            return write.apply(this, args);
        });
        return prms;
    };
 
    var end = res.end;
    res.end = function () {
 
        if (size === 0) return; // reject responses if speed is zero
 
        var args = Array.from(arguments);
 
        prms = promisify(prms, args[0], chk => {
            args[0] = chk;
            return write.apply(this, args);
        });
 
        return prms.then(() => {
            args[0] = null;
            return end.apply(res, args);
        });
    };
};
/**
 * Helper to balance data size and throttle time.
 *
 * @ignore
 * @function
 * @arg {number} size - Data size, bytes.
 * @arg {number} timer - Throttle time, ms.
 * @return {number[]} Balanced size and timer.
 */
self.__balance = (size, timer) => { 
    while (size > MAX_PACKAGE_SIZE) {
        size = Math.ceil(size / 2);
        timer = Math.ceil(timer / 2);
    };
    return [size, timer];
};
/**
 * Helper to promisify requests and responses with size and time.
 *
 * @ignore
 * @function
 * @arg {number} size - Data size, bytes.
 * @arg {number} timer - Throttle time, ms.
 * @return {function} Function to promisify request or response.
 */
self.__promisify = (size, timer) => (prms, chunk, cb) => {
    if (!chunk) return prms;
 
    for (var i = 0; i < chunk.length; i += size) {
        prms = prms
            .then(() => U.sleep(timer))
            .then((idx => () => cb(chunk.slice(idx, idx + size)))(i));
    };
    return prms;
};