All files / mqlobber/lib server.js

100% Statements 214/214
100% Branches 106/106
100% Functions 38/38
100% Lines 214/214

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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578    6x 6x 6x 6x 6x 6x 6x 6x                                         1907x   1907x   1907x 1907x 1907x 1907x         1907x         15x           515097x   87x     1907x   1907x       548477x   2x       1907x 1907x   1907x   3x     1907x   4x     1907x   503868x     1907x   1907x   1907x   5x       1907x       5120x     5120x   5095x         1907x   440426x       440455x   440455x   440426x 440426x     440455x   8x   2x   6x     440447x     440426x 440426x   440426x   424292x     440426x   440426x           440426x   440422x 440422x     440422x 440422x 440422x     440426x   440422x 440422x 440422x 440422x     440426x       440418x         440416x   25x     440416x   440416x   440416x   440418x   440416x     2x       440416x   16134x   2x     16134x   16134x   16132x   16132x   2x     16130x   12x     16118x 16118x   16118x       440416x     440426x   440426x   440406x       1907x 1907x         2x     1907x   1907x   63444x     1907x   1905x   1905x   1905x   501961x     440422x           61545x         61539x 61539x 61539x   61539x       61535x   61535x 61535x   61535x   48461x 48461x   10x       61535x   61535x   19976x   27948x   19976x     7972x         61539x   4x     61535x     61535x     3604x 3604x     3588x   3604x     270x 270x     258x   270x     266x   262x   266x     57393x   2x     57391x 57391x 57391x   57391x   57391x   9116x   9116x   2x     9114x         57389x   57389x     49409x     57389x     2x 2x       1905x       6x                                   6x   3622x   3612x 3612x     3622x       3622x   3618x       4x       3622x     3356x   3356x   4x     3352x 3352x         266x                           6x   2447x   2170x 2170x     2447x       2447x   2440x       7x       2447x   2177x   2177x     3090x   3090x   4x     3086x 3086x   3086x       2177x   5263x   3090x   2173x     2177x   270x     268x   268x   2x     266x 266x         2x       6x   440406x   440406x       2x       6x   3588x     6x   258x     6x   267x     6x   49409x     6x  
"use strict";
 
var EventEmitter = require('events').EventEmitter,
    BPMux = require('bpmux').BPMux,
    util = require('util'),
    TYPE_SUBSCRIBE = 0,
    TYPE_UNSUBSCRIBE = 1,
    TYPE_UNSUBSCRIBE_ALL = 2,
    TYPE_PUBLISH = 3,
    thirty_two_bits = Math.pow(2, 32);
 
/**
Create a new `MQlobberServer` object for publishing and subscribing to messages
on behalf of a client.
 
@constructor
 
@param {QlobberFSQ|QlobberPG} fsq File system queue - an instance of [`QlobberFSQ`](https://github.com/davedoesdev/qlobber-fsq#qlobberfsqoptions).  This does the heavy-lifting of reading and writing messages to a directory on the file system. Alternatively, you can pass an instance of [`QlobberPG`](https://github.com/davedoesdev/qlobber-pg), which uses PostgreSQL to process messages.
 
@param {Duplex} stream Connection to the client. The client should use [`MQlobberClient`](#mqlobberclientstream-options) on its side of the connection.  How the connection is made is up to the caller - it just has to supply a [`Duplex`](https://nodejs.org/dist/latest-v4.x/docs/api/stream.html#stream_class_stream_duplex). For example, [`net.Socket`](https://nodejs.org/dist/latest-v4.x/docs/api/net.html#net_class_net_socket) or [`PrimusDuplex`](https://github.com/davedoesdev/primus-backpressure#primusduplexmsg_stream-options).
 
@param {Object} [options] Configuration options. This is passed down to [`BPMux`](https://github.com/davedoesdev/bpmux#bpmuxcarrier-options) (which multiplexes message streams over the connection to the client). It also supports the following additional properties:
- `{Boolean} send_expires` Whether to include message expiry time in metadata sent to the client. Defaults to `false`.
 
- `{Boolean} send_size` Whether to include message size in metadata sent to then client. Defaults to `false`.
 
- `{Boolean} defer_to_final_handler` If `true` then a message stream is only considered finished when all `MQlobberServer` objects finish processing it. Defaults to `false`.
*/
function MQlobberServer(fsq, stream, options)
{
    EventEmitter.call(this);
 
    options = options || {};
 
    this.fsq = fsq;
    this.subs = new Set();
    this._done = false;
    this.mux = new BPMux(stream, util._extend(util._extend({}, options),
               {
                   high_channels: true
               }));
 
    var ths = this;
        
    function error(err)
    {
        /*jshint validthis: true */
        ths.emit('error', err, this);
    }
 
    function warning(err)
    {
        /*jshint validthis: true */
        if (err && !ths.emit('warning', err, this))
        {
            console.error(err);
        }
    }
    this._warning = warning;
 
    this.mux.on('error', error);
 
    function handshake_sent(duplex, complete)
    {
        if (!complete)
        {
            ths.emit('backoff');
        }
    }
 
    this.mux.on('handshake_sent', handshake_sent);
    this.mux.on('pre_handshake_sent', handshake_sent);
 
    this.mux.on('drain', function ()
    {
        ths.emit('drain');
    });
 
    this.mux.on('full', function ()
    {
        ths.emit('full');
    });
 
    this.mux.on('removed', function (duplex)
    {
        ths.emit('removed', duplex);
    });
 
    this.mux.on('finish', function ()
    {
        ths._done = true;
 
        if (!ths.emit('unsubscribe_all_requested', warning))
        {
            ths.default_unsubscribe_all_requested_handler();
        }
    });
 
    this.relay_error = function (err)
    {
        /*jshint validthis: true */
        /* istanbul ignore else */
        if (this._readableState.pipes)
        {
            // In Node 12, a single pipe isn't in an array
            [].concat(this._readableState.pipes).forEach(function (dest)
            {
                dest.emit('error', err);
            });
        }
    };
 
    this.handler = function (data, info, cb3)
    {
        var called = false;
 
        function cb(err, cb2)
        {
            warning(err);
 
            if (!called)
            {
                cb3.num_handlers -= 1;
                called = true;
            }
 
            if (options.defer_to_final_handler && (cb3.num_handlers > 0))
            {
                if (cb2)
                {
                    return cb2(err);
                }
                return;
            }
 
            cb3(err, cb2);
        }
 
        data.on('error', warning);
        data.on('error', ths.relay_error);
 
        if (!info.single)
        {
            data.on('end', cb);
        }
 
        var hdata = [Buffer.alloc(1)], buf;
 
        hdata[0].writeUInt8((info.single ? 1 : 0) |
                            ((options.send_expires ? 1 : 0) << 1) |
                            ((info.existing ? 1 : 0) << 2) |
                            ((options.send_size ? 1 : 0) << 3),
                            0);
 
        if (options.send_expires)
        {
            buf = Buffer.alloc(8);
            var expires = Math.floor(info.expires / 1000);
            // 32 bits are only enough for dates up to 2106
            // can't use >> and & because JS converts to 32 bits for bitwise ops
            buf.writeUInt32BE(Math.floor(expires / thirty_two_bits), 0);
            buf.writeUInt32BE(expires % thirty_two_bits, 4);
            hdata.push(buf);
        }
 
        if (options.send_size)
        {
            buf = Buffer.alloc(8);
            buf.writeUInt32BE(Math.floor(info.size / thirty_two_bits), 0);
            buf.writeUInt32BE(info.size % thirty_two_bits, 4);
            hdata.push(buf);
        }
        
        hdata.push(Buffer.from(info.topic, 'utf8'));
        
        function multiplex(on_error)
        {
            var duplex = ths.mux.multiplex(Object.assign({}, options,
            {
                handshake_data: Buffer.concat(hdata)
            }));
 
            duplex.on('error', function ()
            {
                this.peer_error_then_end();
            });
 
            duplex.on('error', on_error || cb);
            
            duplex.on('readable', function ()
            {
                while (true)
                {
                    if (this.read() === null)
                    {
                        break;
                    }
 
                    warning.call(this, new Error('unexpected data'));
                }
            });
 
            if (info.single)
            {
                var end = function()
                {
                    cb(new Error('ended before handshaken'));
                };
 
                duplex.on('end', end);
 
                duplex.on('handshake', function (hdata)
                {
                    this.removeListener('end', end);
 
                    if (hdata.length < 1)
                    {
                        return cb(new Error('buffer too small'));
                    }
 
                    if (hdata.readUInt8(0, true) !== 0)
                    {
                        return cb(new Error('client error'));
                    }
 
                    duplex.emit('ack', info);
                    ths.emit('ack', info);
 
                    cb();
                });
            }
            
            return duplex;
        }
 
        multiplex.num_handlers = cb3.num_handlers;
 
        if (!ths.emit('message', data, info, multiplex, cb))
        {
            ths.default_message_handler(data, info, multiplex, cb);
        }
    };
 
    this.handler.accept_stream = true;
    this.handler.mqlobber_server = this;
 
    function end()
    {
        /*jshint validthis: true */
        error.call(this, new Error('ended before handshaken'));
    }
 
    this.mux.on('end', end);
 
    this.mux.on('peer_multiplex', function (duplex)
    {
        duplex.on('error', warning);
    });
 
    this.mux.once('handshake', function (duplex, hdata, delay)
    {
        this.removeListener('end', end);
 
        duplex.end(); // currently no data on initial duplex
 
        this.on('handshake', function (duplex, hdata, delay)
        {
            if (!delay)
            {
                // duplex was initiated by us (outgoing message)
                return;
            }
 
            function dend()
            {
                /*jshint validthis: true */
                this.end();
            }
 
            // only read from incoming duplex but don't end until all data is
            // read in order to allow application to apply back-pressure
            duplex.on('end', dend);
            duplex.on('error', dend);
            duplex.on('error', ths.relay_error);
 
            var handshake = delay();
 
            function done(err)
            {
                warning.call(duplex, err);
 
                var hdata = Buffer.alloc(1);
                hdata.writeUInt8(err ? 1 : 0);
 
                if (arguments.length > 1)
                {
                    var data = arguments[arguments.length - 1];
                    if (Buffer.isBuffer(data))
                    {
                        hdata = Buffer.concat([hdata, data]);
                    }
                }
 
                handshake(hdata);
                
                duplex.on('readable', function ()
                {
                    while (true)
                    {
                        if (this.read() === null)
                        {
                            break;
                        }
 
                        warning.call(this, new Error('unexpected data'));
                    }
                });
            }
 
            if (hdata.length < 1)
            {
                return done(new Error('buffer too small'));
            }
 
            var type = hdata.readUInt8(0, true),
                topic;
 
            switch (type)
            {
                case TYPE_SUBSCRIBE:
                    topic = hdata.toString('utf8', 1);
                    if (!ths.emit('pre_subscribe_requested', topic, done) &&
                        !ths.emit('subscribe_requested', topic, done))
                    {
                        ths.default_subscribe_requested_handler(topic, done);
                    }
                    break;
 
                case TYPE_UNSUBSCRIBE:
                    topic = hdata.toString('utf8', 1);
                    if (!ths.emit('pre_unsubscribe_requested', topic, done) &&
                        !ths.emit('unsubscribe_requested', topic, done))
                    {
                        ths.default_unsubscribe_requested_handler(topic, done);
                    }
                    break;
 
                case TYPE_UNSUBSCRIBE_ALL:
                    if (!ths.emit('unsubscribe_all_requested', done))
                    {
                        ths.default_unsubscribe_all_requested_handler(done);
                    }
                    break;
 
                case TYPE_PUBLISH:
                    if (hdata.length < 2)
                    {
                        return done(new Error('buffer too small'));
                    }
 
                    var options = {},
                        flags = hdata.readUInt8(1, true),
                        pos = 2;
 
                    options.single = !!(flags & 1);
 
                    if (flags & 2)
                    {
                        pos += 4;
 
                        if (hdata.length < pos)
                        {
                            return done(new Error('buffer too small'));
                        }
 
                        options.ttl = Math.min(
                                options.single ? fsq._single_ttl : fsq._multi_ttl,
                                hdata.readUInt32BE(2, true) * 1000);
                    }
 
                    topic = hdata.toString('utf8', pos);
 
                    if (!ths.emit('pre_publish_requested', topic, duplex, options, done) &&
                        !ths.emit('publish_requested', topic, duplex, options, done))
                    {
                        ths.default_publish_requested_handler(topic, duplex, options, done);
                    }
 
                    break;
 
                default:
                    done(new Error('unknown type: ' + type));
                    break;
            }
        });
 
        ths.emit('handshake', hdata, delay);
    });
}
 
util.inherits(MQlobberServer, EventEmitter);
 
/**
Subscribe the connected client to messages.
 
Note: If the client is already subscribed to `topic`, this function will do
nothing (other than call `cb`).
 
@param {String} topic Which messages the client should receive. Message topics are split into words using `.` as the separator. You can use `*` to match exactly one word in a topic or `#` to match zero or more words. For example, `foo.*` would match `foo.bar` whereas `foo.#` would match `foo`, `foo.bar` and `foo.bar.wup`. Note these are the default separator and wildcard characters.  They can be changed when [constructing the `QlobberFSQ` instance] (https://github.com/davedoesdev/qlobber-fsq#qlobberfsqoptions) passed to `MQlobberServer`'s [constructor](#mqlobberserverfsq-stream-options).
 
@param {Object} [options] Optional settings for this subscription:
- `{Boolean} subscribe_to_existing` If `true` then the client will be sent any existing, unexpired messages that match `topic`, as well as new ones.  Defaults to `false` (only new messages).
 
@param {Function} [cb] Optional function to call once the subscription has been made. This will be passed the following arguments:
- `{Object} err` If an error occurred then details of the error, otherwise `null`.
 
- `{Integer} n` The number of subscriptions made (0 if `topic` was already subscribed to, 1 if not).
*/
MQlobberServer.prototype.subscribe = function (topic, options, cb)
{
    if (typeof options === 'function')
    {
        cb = options;
        options = undefined;
    }
 
    var ths = this;
 
    function cb2(err, n)
    {
        if (cb)
        {
            cb(err, n);
        }
        else
        {
            ths._warning(err);
        }
    }
 
    if (!this._done && !this.subs.has(topic))
    {
        // assumes fsq.subscribe calls back synchronously
        this.fsq.subscribe(topic, this.handler, options, function (err)
        {
            if (err)
            {
                return cb2(err, 0);
            }
 
            ths.subs.add(topic);
            cb2(null, 1);
        });
    }
    else
    {
        cb2(null, 0);
    }
};
 
/**
Unsubscribe the connected client from messages.
 
@param {String} [topic] Which messages the client should no longer receive.  If topic is `undefined` then the client will receive no more messages at all.
 
@param {Function} [cb] Optional function to call once the subscription has been removed. This will be passed the following arguments:
- `{Object} err` If an error occurred then details of the error, otherwise `null`'.
 
- `{Integer} n` The number of subscriptions removed.
*/
MQlobberServer.prototype.unsubscribe = function (topic, cb)
{
    if (typeof topic === 'function')
    {
        cb = topic;
        topic = undefined;
    }
 
    var ths = this;
 
    function cb2(err, n)
    {
        if (cb)
        {
            cb(err, n);
        }
        else
        {
            ths._warning(err);
        }
    }
 
    if (topic === undefined)
    {
        var count = 0,
        
        unsub = function (t)
        {
            // assumes fsq.unsubscribe calls back synchronously
            ths.fsq.unsubscribe(t, ths.handler, function (err)
            {
                if (err)
                {
                    return cb2(err, count);
                }
 
                ths.subs.delete(t);
                count += 1;
 
                process.nextTick(next);
            });
        },
 
        next = function ()
        {
            for (var t of ths.subs)
            {
                return unsub(t);
            }
            cb2(null, count);
        };
 
        next();
    }
    else if (this.subs.has(topic))
    {
        // assumes fsq.unsubscribe calls back synchronously
        this.fsq.unsubscribe(topic, this.handler, function (err)
        {
            if (err)
            {
                return cb2(err, 0);
            }
 
            ths.subs.delete(topic);
            cb2(null, 1);
        });
    }
    else
    {
        cb2(null, 0);
    }
};
 
MQlobberServer.prototype.default_message_handler = function (data, info, multiplex, cb)
{
    try
    {
        data.pipe(multiplex());
    }
    catch (ex)
    {
        cb(ex);
    }
};
 
MQlobberServer.prototype.default_subscribe_requested_handler = function (topic, done)
{
    this.subscribe(topic, done);
};
 
MQlobberServer.prototype.default_unsubscribe_requested_handler = function (topic, done)
{
    this.unsubscribe(topic, done);
};
 
MQlobberServer.prototype.default_unsubscribe_all_requested_handler = function (done)
{
    this.unsubscribe(done);
};
 
MQlobberServer.prototype.default_publish_requested_handler = function (topic, duplex, options, done)
{
    duplex.pipe(this.fsq.publish(topic, options, done));
};
 
exports.MQlobberServer = MQlobberServer;