All files / centro/lib/server_transports in-mem.js

100% Statements 79/79
100% Branches 26/26
100% Functions 22/22
100% Lines 78/78

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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225                                                1x 1x       2971x 2971x 2971x 2971x 2971x   560435x   29x 29x   29x     560435x   2971x 2971x   7172x 7172x 7172x   7172x   3197x   3975x   2971x     1x   1x   1217x 1217x     1x   531151x   1x 1x 1x       1x   600307x   600216x       91x           2971x 2971x 2971x 2971x   630354x   35x 35x 35x   35x     630319x   2970x     630354x   2971x     1x   1x   1452x 1452x     1x   602588x   1x 1x 1x       1x   531151x   531136x       15x       1x   379x   379x       379x         1777x             1777x   1230x   1230x                     942x         1556x   28x     1528x     1777x   1749x   249x     1500x     1777x         1x                            
/*eslint-env node */
 
/**
 * In-memory transport. This allows messages to be sent over an in-memory stream
 * to a server running in the same process. The in-memory stream has minimal
 * overhead (the messages aren't copied).
 *
 * An extra {@link centro-js/lib/server.CentroServer#transport_ops|transport operation} for this transport is added to the {@link centro-js/lib/server.CentroServer|server object}:
 *
 * <div class="prose transport_ops mb2 h5">
 * <style>.transport_ops table { width: 100%}</style>
 *
 * | Name    | Type |
 * | :------ | :------------------------------------------------------------------------------ |
 * | connect | {@link centro-js/lib/server_transports/in-mem.connectCallback connectCallback} |
 *
 * </div>
 *
 * @module centro-js/lib/server_transports/in-mem
 * @param {Object} config - Configuration options. This supports all the options supported by stream.Duplex as well as the following:
 * @param {Object} [config.in-mem] - If present then this is used in preference to `config`.
 */
"use strict";
 
var util = require('util'),
    Duplex = require('stream').Duplex;
 
function RightDuplex(left, options)
{
    Duplex.call(this, options);
    options = options || {};
    this.left = left;
    this._orig_emit = this.emit;
    this.emit = function (type)
    {
        if (type === 'error')
        {
            var args = Array.prototype.slice.call(arguments);
            process.nextTick(function ()
            {
                left._orig_emit.apply(left, args);
            });
        }
        return this._orig_emit.apply(this, arguments);
    };
    this._orig_destroy = this.destroy;
    this.destroy = function (err, now)
    {
        const destroy = () => {
            left.destroy();
            this._orig_destroy(err);
        };
        if ((options.destroy_wait === undefined) || now)
        {
            return destroy();
        }
        setTimeout(destroy, options.destroy_wait);
    };
    this._no_keep_alive = true;
}
 
util.inherits(RightDuplex, Duplex);
 
RightDuplex.prototype._final = function (cb)
{
    this.left.push(null);
    cb();
};
 
RightDuplex.prototype._read = function ()
{
    if (this._cb)
    {
        var cb = this._cb;
        this._cb = null;
        process.nextTick(cb);
    }
};
 
RightDuplex.prototype._write = function (chunk, encoding, cb)
{
    if (this.left.push(chunk, encoding))
    {
        process.nextTick(cb);
    }
    else
    {
        this.left._cb = cb;
    }
};
 
function LeftDuplex(options)
{
    Duplex.call(this, options);
    this.right = new RightDuplex(this, options);
    this._orig_emit = this.emit;
    this.emit = function (type)
    {
        if (type === 'error')
        {
            var ths = this,
                args = Array.prototype.slice.call(arguments);
            process.nextTick(function ()
            {
                ths.right._orig_emit.apply(ths.right, args);
            });
        }
        else if (type === 'close')
        {
            this.right.destroy(undefined, true);
        }
 
        return this._orig_emit.apply(this, arguments);
    };
    this._no_keep_alive = true;
}
 
util.inherits(LeftDuplex, Duplex);
 
LeftDuplex.prototype._final = function (cb)
{
    this.right.push(null);
    cb();
};
 
LeftDuplex.prototype._read = function ()
{
    if (this._cb)
    {
        var cb = this._cb;
        this._cb = null;
        process.nextTick(cb);
    }
};
 
LeftDuplex.prototype._write = function (chunk, encoding, cb)
{
    if (this.right.push(chunk, encoding))
    {
        process.nextTick(cb);
    }
    else
    {
        this.right._cb = cb;
    }
};
 
module.exports = function (config, authorize, connected, ready, unused_error, warning)
{
    config = config['in-mem'] || config;
 
    ready(null,
    {
        close: function (cb)
        {
            cb();
        },
 
        connect: function (cb)
        {
            var left = new LeftDuplex(Object.assign(
            {
                allowHalfOpen: false,
                autoDestroy: true,
                destroy_wait: 1000
            }, config));
 
            left.right.on('end', function ()
            {
                try
                {
                    this.destroy();
                }
                catch (ex)
                {
                    /* istanbul ignore next */
                    warning(ex);
                }
            });
 
            function destroy(mqserver, now)
            {
                left.right.destroy(undefined, now);
            }
 
            function onclose(cb)
            {
                if (left.right.destroyed)
                {
                    return cb();
                }
 
                left.right.on('close', cb);
            }
 
            authorize(left.right, now => destroy(null, now), onclose, function (err, handshakes)
            {
                if (err)
                {
                    return left.right.destroy();
                }
 
                connected(handshakes, left.right, destroy, onclose);
            });
 
            cb(null, left);
        }
    });
};
 
module.exports.LeftDuplex = LeftDuplex;
 
/*eslint-disable no-unused-vars */
 
/** Callback type for connecting to in-memory transport.
 *
 * @callback connectCallback
 * @memberof centro-js/lib/server_transports/in-mem
 * @param {?Error} err - Error, if one occurred.
 * @param {stream.Duplex} stream - Stream on which to talk to the server. Use this with {@link centro-js/lib/client.stream_auth|stream_auth}.
 */
/* istanbul ignore next */
exports._connectCallback = function (err, stream) {};