2013-08-17 07:15:57 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies.
|
|
|
|
*/
|
|
|
|
|
2013-08-28 04:23:11 +00:00
|
|
|
var debug = require('debug')('koa:application');
|
2013-08-17 07:15:57 +00:00
|
|
|
var Emitter = require('events').EventEmitter;
|
|
|
|
var compose = require('koa-compose');
|
2013-11-14 02:34:15 +00:00
|
|
|
var context = require('./context');
|
|
|
|
var request = require('./request');
|
|
|
|
var response = require('./response');
|
|
|
|
var Cookies = require('cookies');
|
2013-11-15 18:03:40 +00:00
|
|
|
var Keygrip = require('keygrip');
|
2013-08-17 07:15:57 +00:00
|
|
|
var Stream = require('stream');
|
|
|
|
var http = require('http');
|
|
|
|
var co = require('co');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Application prototype.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var app = Application.prototype;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expose `Application`.
|
|
|
|
*/
|
|
|
|
|
|
|
|
exports = module.exports = Application;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize a new `Application`.
|
|
|
|
*
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Application() {
|
|
|
|
if (!(this instanceof Application)) return new Application;
|
|
|
|
this.env = process.env.NODE_ENV || 'development';
|
2013-09-08 16:37:33 +00:00
|
|
|
this.on('error', this.onerror);
|
2013-08-22 02:48:47 +00:00
|
|
|
this.outputErrors = 'test' != this.env;
|
2013-08-17 07:15:57 +00:00
|
|
|
this.subdomainOffset = 2;
|
|
|
|
this.poweredBy = true;
|
|
|
|
this.jsonSpaces = 2;
|
|
|
|
this.middleware = [];
|
2013-11-14 02:34:15 +00:00
|
|
|
this.context = Object.create(context);
|
|
|
|
this.request = Object.create(request);
|
|
|
|
this.response = Object.create(response);
|
2013-08-17 07:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inherit from `Emitter.prototype`.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Application.prototype.__proto__ = Emitter.prototype;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shorthand for:
|
|
|
|
*
|
|
|
|
* http.createServer(app.callback()).listen(...)
|
|
|
|
*
|
|
|
|
* @param {Mixed} ...
|
|
|
|
* @return {Server}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
app.listen = function(){
|
|
|
|
var server = http.createServer(this.callback());
|
|
|
|
return server.listen.apply(server, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use the given middleware `fn`.
|
|
|
|
*
|
|
|
|
* @param {Function} fn
|
|
|
|
* @return {Application} self
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
app.use = function(fn){
|
2013-08-28 04:23:11 +00:00
|
|
|
debug('use %s', fn.name || '-');
|
2013-08-17 07:15:57 +00:00
|
|
|
this.middleware.push(fn);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a request handler callback
|
2013-08-30 21:09:18 +00:00
|
|
|
* for node's native http server.
|
2013-08-17 07:15:57 +00:00
|
|
|
*
|
|
|
|
* @return {Function}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
app.callback = function(){
|
|
|
|
var mw = [respond].concat(this.middleware);
|
2013-11-08 00:15:47 +00:00
|
|
|
var gen = compose(mw);
|
2013-08-17 07:15:57 +00:00
|
|
|
var self = this;
|
|
|
|
|
2013-11-08 09:17:41 +00:00
|
|
|
return function(req, res, next){
|
2013-11-14 02:34:15 +00:00
|
|
|
var ctx = self.createContext(req, res);
|
2013-11-08 09:17:41 +00:00
|
|
|
co.call(ctx, gen)(next || ctx.onerror);
|
2013-08-17 07:15:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-15 18:03:40 +00:00
|
|
|
/**
|
|
|
|
* Set signed cookie keys.
|
2013-11-20 06:20:17 +00:00
|
|
|
*
|
2013-11-15 18:03:40 +00:00
|
|
|
* These are passed to [KeyGrip](https://github.com/jed/keygrip),
|
|
|
|
* however you may also pass your own `KeyGrip` instance. For
|
|
|
|
* example the following are acceptable:
|
|
|
|
*
|
|
|
|
* app.keys = ['im a newer secret', 'i like turtle'];
|
|
|
|
* app.keys = new KeyGrip(['im a newer secret', 'i like turtle'], 'sha256');
|
|
|
|
*
|
|
|
|
* @param {Array|KeyGrip} keys
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
app.__defineSetter__('keys', function(keys){
|
|
|
|
var ok = Array.isArray(keys) || keys instanceof Keygrip;
|
|
|
|
if (!ok) throw new TypeError('app.keys must be an array or Keygrip');
|
|
|
|
if (!(keys instanceof Keygrip)) keys = new Keygrip(keys);
|
|
|
|
this._keys = keys;
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get `Keygrip` instance.
|
|
|
|
*
|
|
|
|
* @return {Keygrip}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
app.__defineGetter__('keys', function(){
|
|
|
|
return this._keys;
|
|
|
|
});
|
|
|
|
|
2013-11-14 02:34:15 +00:00
|
|
|
/**
|
|
|
|
* Initialize a new context.
|
|
|
|
*
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
|
|
|
app.createContext = function(req, res){
|
|
|
|
var context = Object.create(this.context);
|
|
|
|
var request = context.request = Object.create(this.request);
|
|
|
|
var response = context.response = Object.create(this.response);
|
|
|
|
context.app = request.app = response.app = this;
|
|
|
|
context.req = request.req = response.req = req;
|
|
|
|
context.res = request.res = response.res = res;
|
|
|
|
request.ctx = response.ctx = context;
|
2013-11-18 22:10:11 +00:00
|
|
|
request.response = response;
|
|
|
|
response.request = request;
|
2013-11-14 02:34:15 +00:00
|
|
|
context.onerror = context.onerror.bind(context);
|
2013-11-14 22:18:05 +00:00
|
|
|
context.originalUrl = request.originalUrl = req.url;
|
2013-11-15 18:03:40 +00:00
|
|
|
context.cookies = new Cookies(req, res, this.keys);
|
2013-11-14 02:34:15 +00:00
|
|
|
return context;
|
2013-11-15 18:09:56 +00:00
|
|
|
};
|
2013-11-14 02:34:15 +00:00
|
|
|
|
2013-08-22 02:47:56 +00:00
|
|
|
/**
|
|
|
|
* Default error handler.
|
|
|
|
*
|
|
|
|
* @param {Error} err
|
|
|
|
* @api private
|
|
|
|
*/
|
|
|
|
|
|
|
|
app.onerror = function(err){
|
2013-09-14 03:54:52 +00:00
|
|
|
if (!this.outputErrors) return;
|
|
|
|
if (404 == err.status) return;
|
|
|
|
console.error(err.stack);
|
2013-08-22 02:47:56 +00:00
|
|
|
};
|
|
|
|
|
2013-08-17 07:15:57 +00:00
|
|
|
/**
|
|
|
|
* Response middleware.
|
|
|
|
*/
|
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
function *respond(next){
|
|
|
|
this.status = 200;
|
|
|
|
if (this.app.poweredBy) this.set('X-Powered-By', 'koa');
|
2013-09-01 23:27:03 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
yield next;
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
var res = this.res;
|
|
|
|
var body = this.body;
|
|
|
|
var head = 'HEAD' == this.method;
|
2013-11-20 06:20:17 +00:00
|
|
|
var noContent = ~[204, 205, 304].indexOf(this.status);
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
// 404
|
|
|
|
if (null == body && 200 == this.status) {
|
|
|
|
this.status = 404;
|
|
|
|
}
|
2013-08-17 22:36:51 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
// ignore body
|
|
|
|
if (noContent) return res.end();
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
// status body
|
|
|
|
if (null == body) {
|
|
|
|
this.type = 'text';
|
|
|
|
body = http.STATUS_CODES[this.status];
|
|
|
|
}
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
// Buffer body
|
|
|
|
if (Buffer.isBuffer(body)) {
|
|
|
|
if (head) return res.end();
|
|
|
|
return res.end(body);
|
|
|
|
}
|
2013-08-17 22:36:51 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
// string body
|
|
|
|
if ('string' == typeof body) {
|
2013-08-17 07:15:57 +00:00
|
|
|
if (head) return res.end();
|
2013-11-08 00:15:47 +00:00
|
|
|
return res.end(body);
|
2013-08-17 07:15:57 +00:00
|
|
|
}
|
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
// Stream body
|
|
|
|
if (body instanceof Stream) {
|
|
|
|
if (!~body.listeners('error').indexOf(this.onerror)) body.on('error', this.onerror);
|
2013-11-20 06:20:17 +00:00
|
|
|
|
2013-11-15 19:33:14 +00:00
|
|
|
if (head) {
|
|
|
|
if (body.close) body.close();
|
|
|
|
return res.end();
|
|
|
|
}
|
2013-11-20 06:20:17 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
return body.pipe(res);
|
|
|
|
}
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
// body: json
|
|
|
|
body = JSON.stringify(body, null, this.app.jsonSpaces);
|
|
|
|
this.length = Buffer.byteLength(body);
|
|
|
|
if (head) return res.end();
|
|
|
|
res.end(body);
|
|
|
|
}
|