Code coverage report for lib/socket.io/router.js

Statements: 60% (33 / 55)      Branches: 33.33% (6 / 18)      Functions: 54.55% (6 / 11)      Lines: 60.38% (32 / 53)      Ignored: none     

All files » lib/socket.io/ » router.js
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                        1 1 1 1           1           1 2 1                 1 11 11 11 1 1       1 1     1 1                                 1                     1 6               1 6         6   6                               1 6           6 6 6     6 6        
/*!
 * koa.io - lib/socket.io/router.js
 * Copyright(c) 2014 dead_horse <dead_horse@qq.com>
 * MIT Licensed
 */
 
'use strict';
 
/**
 * Module dependencies.
 */
 
var debug = require('debug')('koa.io:socket.io:router');
var compose = require('koa-compose');
var util = require('util');
var co = require('co');
 
/**
 * Module expose.
 */
 
module.exports = Router;
 
/**
 * a simple event router object
 */
 
function Router() {
  if (!(this instanceof Router)) return new Router();
  this.fns = [];
};
 
/**
 * create a socket.io middleware
 *
 * @return {GeneratorFunction}
 */
 
Router.prototype.middleware = function () {
  var self = this;
  var gen = compose(this.fns);
  return function* router(next) {
    debug('in router middleware');
    Iif (!self.fns.length) {
      debug('router not exist');
      return yield* next;
    }
    var context = this;
    var socket = this.socket;
 
    // replace socket.onevent to start the router
    socket._onevent = socket.onevent;
    socket.onevent = function (packet) {
      var args = packet.data || [];
      if (!args.length) {
        debug('event args not exist');
        return socket._onevent(packet);
      }
 
      context.event = args[0];
      context.data = args.slice(1);
 
      co(gen).call(context, function (err) {
        if (err) console.error(err.stack);
        //TODO: error
        socket._onevent(packet);
      });
    };
 
    yield* next;
  };
};
 
 
/**
 * add a route into router
 * @param {String|RegExp} event
 * @param {GeneratorFunction} fn
 */
 
Router.prototype.route = function (event, fn) {
  this.fns.push(createRoute(event, fn));
};
 
/**
 * create a router with event match
 * @return {GeneratorFunction}
 */
 
function createRoute(event, fn) {
  Iif (typeof event === 'function') {
    fn = event;
    event = null;
  }
 
  var checker = createChecker(event);
 
  return function* (next) {
    debug('check `%s` to match `%s`, %s', this.event, event, checker(this.event));
    if (!checker(this.event)) {
      return yield* next;
    }
    var args = [next].concat(this.data);
    yield* fn.apply(this, args);
  }
}
 
/**
 * create a event checker
 * @param {String|RegExp} name
 * @return {function}
 */
 
function createChecker(name) {
  Iif (!name) {
    return function () {
      return true;
    }
  }
 
  Eif (typeof name === 'string') {
    name = name.replace(/\*/g, '.*?');
    name = new RegExp('^' + name + '$');
  }
 
  debug('regexp: %s', name);
  return function (event) {
    return !!event.match(name);
  }
}