Code coverage report for lib/application.js

Statements: 100% (37 / 37)      Branches: 70% (7 / 10)      Functions: 100% (7 / 7)      Lines: 100% (35 / 35)      Ignored: none     

All files » lib/ » application.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                        1 1 1 1   1           1               1 30 15   15     1   1               1 2                 1 17 17                 1 6 6   6 6   6 6 6     6 6 6                     1 11 11 11                         1 11 11    
/*!
 * koa.io - lib/application.js
 * Copyright(c) 2014 dead_horse <dead_horse@qq.com>
 * MIT Licensed
 */
 
'use strict';
 
/**
 * Module dependencies.
 */
 
var Socket = require('./socket.io');
var util = require('util');
var http = require('http');
var Koa = require('koa');
 
var Session;
 
/**
 * Module exports.
 */
 
module.exports = Application;
 
/**
 * Application constructor
 *
 * @param {Object} options for Socket.io
 */
 
function Application(options) {
  if (!(this instanceof Application)) return new Application;
  Koa.call(this);
 
  this.io = new Socket(options);
}
 
util.inherits(Application, Koa);
 
var app = Application.prototype;
 
/**
 * get the keys for signed cookies
 *
 * @return {Array}
 */
 
app.__defineGetter__('keys', function () {
  return this._keys;
});
 
/**
 * set the keys for signed cookies
 *
 * @param [Array] keys
 */
 
app.__defineSetter__('keys', function (keys) {
  this._keys = keys;
  this.io.keys = keys;
});
 
/**
 * an easy way to
 * @param {[type]} opts [description]
 * @return {[type]} [description]
 */
 
app.session = function (opts) {
  opts = opts || {};
  if (!Session) Session = require('koa-generic-session');
 
  var session = Session(opts);
  this.use(session);
 
  var namespace = opts.namespace || ['/'];
  Eif (!Array.isArray(namespace)) {
    namespace = [namespace];
  }
 
  var self = this;
  namespace.forEach(function () {
    self.io.of(namespace).use(session);
  });
};
 
/**
 * create a http server
 * and attach socket.io to this server
 *
 * @return {HttpServer}
 */
 
app.createServer = function () {
  var server = http.createServer(this.callback());
  this.io.attach(server);
  return server;
}
 
/**
 * create a http server
 * attach socket.io to this server
 * listen
 *
 * @param {Mixed} ...
 * @return {Server}
 * @api public
 */
 
app.listen = function () {
  var server = this.createServer();
  return server.listen.apply(server, arguments);
};