2015-10-05 23:18:03 +00:00
|
|
|
|
2015-10-11 22:59:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
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;
|
2014-12-18 02:06:45 +00:00
|
|
|
var compose_es7 = require('composition');
|
2014-10-01 12:12:20 +00:00
|
|
|
var onFinished = require('on-finished');
|
|
|
|
var response = require('./response');
|
2013-08-17 07:15:57 +00:00
|
|
|
var compose = require('koa-compose');
|
2014-05-01 23:25:08 +00:00
|
|
|
var isJSON = require('koa-is-json');
|
2013-11-14 02:34:15 +00:00
|
|
|
var context = require('./context');
|
|
|
|
var request = require('./request');
|
2014-10-01 12:12:20 +00:00
|
|
|
var statuses = require('statuses');
|
2013-11-14 02:34:15 +00:00
|
|
|
var Cookies = require('cookies');
|
2013-12-27 21:56:21 +00:00
|
|
|
var accepts = require('accepts');
|
2013-12-19 18:03:08 +00:00
|
|
|
var assert = require('assert');
|
2014-05-01 23:25:08 +00:00
|
|
|
var Stream = require('stream');
|
2013-08-17 07:15:57 +00:00
|
|
|
var http = require('http');
|
2014-03-11 18:01:33 +00:00
|
|
|
var only = require('only');
|
2013-08-17 07:15:57 +00:00
|
|
|
var co = require('co');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Application prototype.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var app = Application.prototype;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expose `Application`.
|
|
|
|
*/
|
|
|
|
|
2015-10-12 00:31:39 +00:00
|
|
|
module.exports = Application;
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize a new `Application`.
|
|
|
|
*
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Application() {
|
|
|
|
if (!(this instanceof Application)) return new Application;
|
|
|
|
this.env = process.env.NODE_ENV || 'development';
|
|
|
|
this.subdomainOffset = 2;
|
|
|
|
this.middleware = [];
|
2015-10-31 18:16:41 +00:00
|
|
|
this.proxy = false;
|
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`.
|
|
|
|
*/
|
|
|
|
|
2015-02-02 00:39:44 +00:00
|
|
|
Object.setPrototypeOf(Application.prototype, Emitter.prototype);
|
2013-08-17 07:15:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shorthand for:
|
|
|
|
*
|
|
|
|
* http.createServer(app.callback()).listen(...)
|
|
|
|
*
|
|
|
|
* @param {Mixed} ...
|
|
|
|
* @return {Server}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
app.listen = function(){
|
2013-11-27 05:26:05 +00:00
|
|
|
debug('listen');
|
2013-08-17 07:15:57 +00:00
|
|
|
var server = http.createServer(this.callback());
|
|
|
|
return server.listen.apply(server, arguments);
|
|
|
|
};
|
|
|
|
|
2014-03-11 18:01:33 +00:00
|
|
|
/**
|
|
|
|
* Return JSON representation.
|
2014-08-02 11:10:07 +00:00
|
|
|
* We only bother showing settings.
|
2014-03-11 18:01:33 +00:00
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
2014-08-02 11:10:07 +00:00
|
|
|
app.inspect =
|
2014-03-11 18:01:33 +00:00
|
|
|
app.toJSON = function(){
|
|
|
|
return only(this, [
|
|
|
|
'subdomainOffset',
|
2015-10-31 18:16:41 +00:00
|
|
|
'proxy',
|
2014-03-11 18:01:33 +00:00
|
|
|
'env'
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
2013-08-17 07:15:57 +00:00
|
|
|
/**
|
|
|
|
* Use the given middleware `fn`.
|
|
|
|
*
|
2013-12-19 18:03:08 +00:00
|
|
|
* @param {GeneratorFunction} fn
|
2013-08-17 07:15:57 +00:00
|
|
|
* @return {Application} self
|
|
|
|
* @api public
|
|
|
|
*/
|
|
|
|
|
|
|
|
app.use = function(fn){
|
2014-12-18 02:06:45 +00:00
|
|
|
if (!this.experimental) {
|
2015-12-16 10:48:13 +00:00
|
|
|
// es7 async functions are not allowed,
|
|
|
|
// so we have to make sure that `fn` is a generator function
|
2014-12-18 02:06:45 +00:00
|
|
|
assert(fn && 'GeneratorFunction' == fn.constructor.name, 'app.use() requires a generator function');
|
|
|
|
}
|
2013-12-30 00:04:06 +00:00
|
|
|
debug('use %s', fn._name || 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(){
|
2016-02-29 00:13:52 +00:00
|
|
|
if (this.experimental) {
|
|
|
|
console.error('Experimental ES7 Async Function support is deprecated. Please look into Koa v2 as the middleware signature has changed.')
|
|
|
|
}
|
2014-12-18 02:06:45 +00:00
|
|
|
var fn = this.experimental
|
2015-08-23 16:08:54 +00:00
|
|
|
? compose_es7(this.middleware)
|
|
|
|
: co.wrap(compose(this.middleware));
|
2013-08-17 07:15:57 +00:00
|
|
|
var self = this;
|
|
|
|
|
2014-05-02 00:46:09 +00:00
|
|
|
if (!this.listeners('error').length) this.on('error', this.onerror);
|
|
|
|
|
2016-08-31 02:24:28 +00:00
|
|
|
return function handleRequest(req, res){
|
2013-11-14 02:34:15 +00:00
|
|
|
var ctx = self.createContext(req, res);
|
2018-01-25 10:53:51 +00:00
|
|
|
self.handleRequest(ctx, fn);
|
2013-08-17 07:15:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-24 14:27:02 +00:00
|
|
|
app.handleRequest = function(ctx, fnMiddleware){
|
|
|
|
ctx.res.statusCode = 404;
|
|
|
|
onFinished(ctx.res, ctx.onerror);
|
|
|
|
fnMiddleware.call(ctx).then(function handleResponse() {
|
|
|
|
respond.call(ctx);
|
|
|
|
}).catch(ctx.onerror);
|
|
|
|
};
|
|
|
|
|
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;
|
2015-12-02 15:13:01 +00:00
|
|
|
context.cookies = new Cookies(req, res, {
|
|
|
|
keys: this.keys,
|
|
|
|
secure: request.secure
|
|
|
|
});
|
2013-12-27 21:56:21 +00:00
|
|
|
context.accept = request.accept = accepts(req);
|
2014-11-18 08:54:14 +00:00
|
|
|
context.state = {};
|
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){
|
2014-05-23 13:15:03 +00:00
|
|
|
assert(err instanceof Error, 'non-error thrown: ' + err);
|
|
|
|
|
2015-08-20 18:28:40 +00:00
|
|
|
if (404 == err.status || err.expose) return;
|
2015-10-05 22:51:26 +00:00
|
|
|
if (this.silent) return;
|
2015-10-09 00:02:36 +00:00
|
|
|
// DEPRECATE env-specific logging in v2
|
2014-05-01 23:29:37 +00:00
|
|
|
if ('test' == this.env) return;
|
2014-05-23 13:15:03 +00:00
|
|
|
|
|
|
|
var msg = err.stack || err.toString();
|
2014-05-01 23:18:40 +00:00
|
|
|
console.error();
|
2014-05-23 13:15:03 +00:00
|
|
|
console.error(msg.replace(/^/gm, ' '));
|
2014-05-01 23:18:40 +00:00
|
|
|
console.error();
|
2013-08-22 02:47:56 +00:00
|
|
|
};
|
|
|
|
|
2013-08-17 07:15:57 +00:00
|
|
|
/**
|
2015-08-23 16:08:54 +00:00
|
|
|
* Response helper.
|
2013-08-17 07:15:57 +00:00
|
|
|
*/
|
|
|
|
|
2015-08-23 16:08:54 +00:00
|
|
|
function respond() {
|
2014-04-15 15:39:40 +00:00
|
|
|
// allow bypassing koa
|
2014-01-24 22:38:40 +00:00
|
|
|
if (false === this.respond) return;
|
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
var res = this.res;
|
2014-02-15 10:06:08 +00:00
|
|
|
if (res.headersSent || !this.writable) return;
|
2013-12-22 17:26:21 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
var body = this.body;
|
2014-04-29 04:17:30 +00:00
|
|
|
var code = this.status;
|
2013-08-17 22:36:51 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
// ignore body
|
2014-10-01 12:12:20 +00:00
|
|
|
if (statuses.empty[code]) {
|
2014-04-09 16:21:15 +00:00
|
|
|
// strip headers
|
|
|
|
this.body = null;
|
|
|
|
return res.end();
|
|
|
|
}
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2014-04-15 02:06:32 +00:00
|
|
|
if ('HEAD' == this.method) {
|
|
|
|
if (isJSON(body)) this.length = Buffer.byteLength(JSON.stringify(body));
|
|
|
|
return res.end();
|
|
|
|
}
|
2014-03-23 11:01:14 +00:00
|
|
|
|
2014-04-15 15:39:40 +00:00
|
|
|
// status body
|
2014-04-15 02:06:32 +00:00
|
|
|
if (null == body) {
|
2013-11-08 00:15:47 +00:00
|
|
|
this.type = 'text';
|
2014-10-01 12:12:20 +00:00
|
|
|
body = this.message || String(code);
|
2015-03-29 03:53:54 +00:00
|
|
|
this.length = Buffer.byteLength(body);
|
2014-04-13 02:23:57 +00:00
|
|
|
return res.end(body);
|
2013-11-08 00:15:47 +00:00
|
|
|
}
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2014-04-15 15:39:40 +00:00
|
|
|
// responses
|
2014-04-15 02:06:32 +00:00
|
|
|
if (Buffer.isBuffer(body)) return res.end(body);
|
|
|
|
if ('string' == typeof body) return res.end(body);
|
|
|
|
if (body instanceof Stream) return body.pipe(res);
|
2013-08-17 07:15:57 +00:00
|
|
|
|
2013-11-08 00:15:47 +00:00
|
|
|
// body: json
|
2014-02-01 02:39:47 +00:00
|
|
|
body = JSON.stringify(body);
|
2013-11-08 00:15:47 +00:00
|
|
|
this.length = Buffer.byteLength(body);
|
|
|
|
res.end(body);
|
2014-03-24 18:21:15 +00:00
|
|
|
}
|