All files / centro/lib/server_transports primus.js

100% Statements 42/42
100% Branches 12/12
100% Functions 12/12
100% Lines 42/42

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                            1x 1x 1x 1x   1x   124x   124x                 484x   8x     476x     124x   540x   8x     16x     532x   64x     468x   468x           468x             92x 92x       468x       124x 124x   124x 124x 124x       540x   124x       124x       124x 124x 124x 124x 124x 124x   124x   62x     62x             124x   62x     62x      
/*eslint-env node */
 
/**
 * Primus transport. This allows messages to be sent over {@link https://github.com/primus/primus|Primus} connections.
 *
 * @module centro-js/lib/server_transports/primus
 * @param {Object} config - Configuration options. This supports all the options supported by `Primus.createServer` and {@link https://github.com/davedoesdev/primus-backpressure#primusduplexmsg_stream-options|PrimusDuplex} as well as the following:
 * @param {Primus} [config.server] - If you want to supply your own Primus server object. Otherwise, `Primus.createServer` will be called to create one.
 * @param {string} [config.pathname=/centro/v2/primus] - Pathname prefix on which Primus listens for connections.
 * @param {Object} [config.primus_transport] - Primus transformer-specific configuration.
 * @param {Object} [config.primus] - If present then this is used in preference to `config`.
 */
"use strict";
 
var centro = require('../..'),
    Primus = require('primus'),
    PrimusDuplex = require('primus-backpressure').PrimusDuplex,
    on_connection = require('./http').on_connection;
 
module.exports = function (config, authorize, connected, ready, error, warning)
{
    config = config.primus || config;
 
    var server = config.server || Primus.createServer(
                        Object.assign(
                        {
                            pathname: '/centro/v' + centro.version + '/primus',
                            maxLength: 1 * 1024 * 1024
                        }, config, { transport: config.primus_transport }));
 
    function onclose(req, cb)
    {
        if (req.socket.destroyed)
        {
            return cb();
        }
 
        req.socket.on('close', cb);
    }
 
    server.authorize(function (req, cb)
    {
        authorize(req, function ()
        {
            cb({ statusCode: 503, message: 'closed' });
        }, function (cb)
        {
            onclose(req, cb);
        }, function (err, handshakes)
        {
            if (err)
            {
                return cb(err);
            }
 
            req.handshakes = handshakes;
 
            cb();
        });
    });
 
    function connection(spark)
    {
        connected(spark.request.handshakes,
                  new PrimusDuplex(spark, Object.assign(
                  {
                      allowHalfOpen: false
                  }, config)),
                  function ()
                  {
                      spark.request.socket.destroy();
                      spark.end();
                  },
                  function (cb)
                  {
                      onclose(spark.request, cb);
                  });
    }
 
    server.on('connection', connection);
    server.on('error', error);
 
    const on_conn = on_connection.bind(server.server, warning);
    server.server.prependListener('connection', on_conn);
    server.server.prependListener('secureConnection', on_conn);
 
    function on_upgrade(req, socket)
    {
        on_connection.call(this, warning, socket);
    }
    server.server.prependListener('upgrade', on_upgrade);
 
    function initialised()
    {
        ready(null,
        {
            close: function (cb)
            {
                server.auth = null;
                server.removeListener('connection', connection);
                server.removeListener('error', error);
                server.server.removeListener('connection', on_conn);
                server.server.removeListener('secureConnection', on_conn);
                server.server.removeListener('upgrade', on_upgrade);
 
                if (config.server)
                {
                    return cb();
                }
 
                server.destroy(config, cb);
            },
 
            server: server
        });
    }
 
    if (config.server)
    {
        return initialised();
    }
 
    server.on('initialised', initialised);
};