All files / centro/lib/server_extensions full.js

100% Statements 31/31
100% Branches 15/15
100% Functions 11/11
100% Lines 31/31

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                  1x 1x                       1x   66x   22x     22x   22x           44x     44x   44x     44x   88110x         44x   22x   22x   22022x   22022x     22x   22x     22x     22000x           22x   22x   22x   22056x   22056x     34x   34x     34x       22022x         44x                                                  
/*eslint-env node */
 
/**
 * Centro extension for defining {@link https://github.com/davedoesdev/mqlobber#mqlobberservereventsfull|full} event behaviour.
 *
 * @module centro-js/lib/server_extensions/full
 */
"use strict";
 
var wu = require('wu'),
    fl_prop = 'centro_server_extension_full';
 
/**
 * Attach behaviour to the {@link https://github.com/davedoesdev/mqlobber#mqlobberservereventsfull|full} event.
 *
 * @param {Object} config - Configuration options.
 * @param {boolean} [config.close_conn] - Close connection when a `full` event occurs.
 * @param {boolean} [config.skip_message] - When a `full` event occurs, drop messages that would be sent to the client until a {@link https://github.com/davedoesdev/mqlobber#mqlobberservereventsremovedduplex|removed} event occurs.
 * @param {centro-js/lib/server_extensions/full.skipCallback} [config.on_skip] - Called when a message is dropped.
 * @param {boolean} [config.delay_message] - When a `full` event occurs, postpone messages that would be sent to the client until a `removed` event occurs.
 * @param {centro-js/lib/server_extensions/full.delayCallback} [config.on_delay] - Called when a message is postponed.
 */
exports.full = function (config)
{
    if (config.close_conn)
    {
        return {
            pre_connect: function (info)
            {
                info.mqserver.on('full', function ()
                {
                    info.destroy();
                });
            }
        };
    }
 
    var r = {
        pre_connect: function (info)
        {
            info.mqserver.on('full', function ()
            {
                this[fl_prop] = true;
            });
 
            info.mqserver.on('removed', function ()
            {
                this[fl_prop] = false;
            });
        }
    };
 
    if (config.skip_message)
    {
        r.ready = function ()
        {
            this.fsq.filters.push(function (info, handlers, cb)
            {
                cb(null, true, wu(handlers).filter(function (h)
                {
                    if (h.mqlobber_server && h.mqlobber_server[fl_prop])
                    {
                        /* istanbul ignore else */
                        if (config.on_skip)
                        {
                            config.on_skip(info, handlers, h);
                        }
 
                        return false;
                    }
 
                    return true;
                }));
            });
        };
    }
    // https://github.com/gotwarlost/istanbul/issues/781
    else { /* istanbul ignore else */ if (config.delay_message)
    {
        r.ready = function ()
        {
            this.fsq.filters.push(function (info, handlers, cb)
            {
                for (var h of handlers)
                {
                    if (h.mqlobber_server && h.mqlobber_server[fl_prop])
                    {
                        /* istanbul ignore else */
                        if (config.on_delay)
                        {
                            config.on_delay(info, handlers, h);
                        }
 
                        return cb(null, false);
                    }
                }
 
                return cb(null, true, handlers);
            });
        };
    }}
 
    return r;
};
 
/*eslint-disable no-unused-vars */
 
/**
 * Callback type for dropping a message.
 *
 * @callback skipCallback
 * @memberof centro-js/lib/server_extensions/full
 * @param {Object} info - Metadata for the message. See {@link https://github.com/davedoesdev/mqlobber#mqlobberservereventsmessagestream-info-multiplex-done|MQlobber.events.message} for details.
 */
/* istanbul ignore next */
exports._skipCallback = function (info) {};
 
 
/**
 * Callback type for postponing a message.
 *
 * @callback delayCallback
 * @memberof centro-js/lib/server_extensions/full
 * @param {Object} info - Metadata for the message. See {@link https://github.com/davedoesdev/mqlobber#mqlobberservereventsmessagestream-info-multiplex-done|MQlobber.events.message} for details.
 */
/* istanbul ignore next */
exports._delayCallback = function (info) {};