All files / centro/lib/server_extensions throttle.js

100% Statements 15/15
100% Branches 0/0
100% Functions 6/6
100% Lines 15/15

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                  1x             1x   30x     46x   30x 30x 30x 30x                     1x   30x     46x   30x 30x 30x 30x          
/*eslint-env node */
 
/**
 * Centro extension for limiting the rate of messages streams.
 *
 * @module centro-js/lib/server_extensions/throttle
 */
"use strict";
 
var Throttle = require('stream-throttle').Throttle;
 
/**
 * Limit the rate at which message data can be published by clients.
 *
 * @param {Object} config - See {@link https://github.com/tjgq/node-stream-throttle|Throttle}.
 */
exports.throttle_publish_streams = function (config)
{
    return {
        pre_connect: function (info)
        {
            this.pipeline(info.mqserver, 'publish_requested', function (topic, duplex, options, cb, next)
            {
                var t = new Throttle(config);
                t.on('error', this.relay_error);
                duplex.pipe(t);
                next(topic, t, options, cb);
            });
        }
    };
};
 
/**
 * Limit the rate at which message data is sent to clients.
 *
 * @param {Object} config - See {@link https://github.com/tjgq/node-stream-throttle|Throttle}.
 */
exports.throttle_message_streams = function (config)
{
    return {
        pre_connect: function (info)
        {
            this.pipeline(info.mqserver, 'message', function (stream, info, multiplex, cb, next)
            {
                var t = new Throttle(config);
                t.on('error', this.relay_error);
                stream.pipe(t);
                next(t, info, multiplex, cb);
            });
        }
    };
};