All files / centro/lib/server_extensions limit_conn.js

100% Statements 29/29
100% Branches 8/8
100% Functions 7/7
100% Lines 29/29

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                    1x               1x   44x     44x   44x   88x   88x   88x   192x   192x   44x     44x   22x     22x     148x 148x     88x   88x                       1x   44x     44x   44x   88x   88x   44x     44x   22x     22x     44x          
/*eslint-env node */
 
/**
 * Centro extension for limiting the total amount of data published and the
 * total number of messages published by a connected client.
 *
 * @module centro-js/lib/server_extensions/limit_conn
 */
"use strict";
 
var Transform = require('stream').Transform;
 
/**
 * Limit the total amount of data published by a connected client.
 *
 * @param {Object} config - Configuration options.
 * @param {integer} config.max_conn_published_data_length - Data limit for each client.
 */
exports.limit_conn_published_data = function (config)
{
    return {
        pre_connect: function (info)
        {
            var count = 0;
 
            this.pipeline(info.mqserver, 'publish_requested', function (topic, duplex, options, cb, next)
            {
                var t = new Transform();
 
                t.on('error', this.relay_error);
 
                t._transform = function (chunk, enc, cont)
                {
                    count += chunk.length;
 
                    if (count > config.max_conn_published_data_length)
                    {
                        cont(new Error('published data limit ' + config.max_conn_published_data_length +
                                       ' exceeded: ' + topic));
 
                        if (config.close_conn)
                        {
                            return info.destroy();                        
                        }
 
                        return;
                    }
 
                    this.push(chunk);
                    cont();
                };
 
                duplex.pipe(t);
                
                next(topic, t, options, cb);
            });
        }
    };
};
 
/**
 * Limit the number of messages published by a connected client.
 *
 * @param {Object} config - Configuration options
 * @param {integer} config.max_conn_published_messages - Message limit for each client.
 */
exports.limit_conn_published_messages = function (config)
{
    return {
        pre_connect: function (info)
        {
            var count = 0;
 
            this.pipeline(info.mqserver, 'publish_requested', function (topic, duplex, options, cb, next)
            {
                count += 1;
 
                if (count > config.max_conn_published_messages)
                {
                    cb(new Error('published message limit ' + config.max_conn_published_messages +
                                 ' exceeded: ' + topic));
 
                    if (config.close_conn)
                    {
                        return info.destroy();                        
                    }
 
                    return;
                }
 
                next(topic, duplex, options, cb);
            });
        }
    };
};