koa-lite/lib/application.js

248 lines
5.4 KiB
JavaScript
Raw Permalink Normal View History

'use strict';
2013-08-17 07:15:57 +00:00
/**
* Module dependencies.
*/
const debug = require('debug-ms')('koa:application');
const Emitter = require('events');
const util = require('util');
const Stream = require('stream');
const http = require('http');
const onFinished = require('./onfinish');
const response = require('./response');
const compose = require('./compose');
const isJSON = require('./isjson');
const context = require('./context');
const request = require('./request');
const statuses = require('./statuses');
2013-08-17 07:15:57 +00:00
/**
2015-10-13 06:19:42 +00:00
* Expose `Application` class.
* Inherits from `Emitter.prototype`.
2013-08-17 07:15:57 +00:00
*/
2015-10-13 06:19:42 +00:00
module.exports = class Application extends Emitter {
/**
* Initialize a new `Application`.
*
* @api public
*/
2014-03-11 18:01:33 +00:00
/**
*
* @param {object} [options] Application options
* @param {string} [options.env='development'] Environment
* @param {boolean} [options.proxy] Trust proxy headers
* @param {number} [options.subdomainOffset] Subdomain offset
*
*/
2014-03-11 18:01:33 +00:00
2019-08-19 03:54:41 +00:00
constructor(options) {
super();
2019-08-19 03:54:41 +00:00
options = options || {};
this.proxy = options.proxy || false;
this.subdomainOffset = options.subdomainOffset || 2;
this.env = options.env || process.env.NODE_ENV || 'development';
2019-08-19 03:54:41 +00:00
this.middleware = [];
2015-10-13 06:19:42 +00:00
this.context = Object.create(context);
this.request = Object.create(request);
this.response = Object.create(response);
if (util.inspect.custom) {
this[util.inspect.custom] = this.inspect;
}
}
2013-08-17 07:15:57 +00:00
2015-10-13 06:19:42 +00:00
/**
* Shorthand for:
*
* http.createServer(app.callback()).listen(...)
*
* @param {Mixed} ...
* @return {Server}
* @api public
*/
2017-04-23 23:14:16 +00:00
listen(...args) {
2015-10-13 06:19:42 +00:00
debug('listen');
const server = http.createServer(this.callback());
2017-04-23 23:14:16 +00:00
return server.listen(...args);
2015-10-13 06:19:42 +00:00
}
2014-05-02 00:46:09 +00:00
2015-10-13 06:19:42 +00:00
/**
* Return JSON representation.
* We only bother showing settings.
*
* @return {Object}
* @api public
*/
toJSON() {
return {
subdomainOffset: this.subdomainOffset,
proxy: this.proxy,
2019-10-11 17:46:53 +00:00
env: this.env
};
2013-08-17 07:15:57 +00:00
}
/**
* Inspect implementation.
*
* @return {Object}
* @api public
*/
2015-10-13 06:19:42 +00:00
inspect() {
return this.toJSON();
}
2015-10-13 06:19:42 +00:00
/**
* Use the given middleware `fn`.
*
* Old-style middleware will be converted.
*
* @param {Function} fn
2015-10-13 06:19:42 +00:00
* @return {Application} self
* @api public
*/
use(fn) {
if (typeof fn !== 'function') throw new TypeError('middleware must be a function!');
debug('use %s', fn._name || fn.name || '-');
2015-10-13 06:19:42 +00:00
this.middleware.push(fn);
return this;
}
2015-10-13 06:19:42 +00:00
/**
* Return a request handler callback
* for node's native http server.
*
* @return {Function}
* @api public
*/
callback() {
const fn = compose(this.middleware);
2015-10-13 06:19:42 +00:00
if (!this.listenerCount('error')) this.on('error', this.onerror);
2015-10-13 06:19:42 +00:00
const handleRequest = (req, res) => {
2015-10-13 06:19:42 +00:00
const ctx = this.createContext(req, res);
return this.handleRequest(ctx, fn);
2015-10-12 20:36:03 +00:00
};
return handleRequest;
2015-10-13 06:19:42 +00:00
}
2013-08-22 02:47:56 +00:00
/**
* Handle request in callback.
*
* @api private
*/
handleRequest(ctx, fnMiddleware) {
const res = ctx.res;
res.statusCode = 404;
const onerror = err => ctx.onerror(err);
const handleResponse = () => respond(ctx);
onFinished(res, onerror);
return fnMiddleware(ctx).then(handleResponse).catch(onerror);
}
2015-10-13 06:19:42 +00:00
/**
* Initialize a new context.
*
* @api private
*/
createContext(req, res) {
const context = Object.create(this.context);
const request = context.request = Object.create(this.request);
const 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;
request.response = response;
response.request = request;
context.originalUrl = request.originalUrl = req.url;
context.state = {};
return context;
}
2015-10-13 06:19:42 +00:00
/**
* Default error handler.
*
* @param {Error} err
* @api private
*/
onerror(err) {
if (!(err instanceof Error)) throw new TypeError(util.format('non-error thrown: %j', err));
2015-10-13 06:19:42 +00:00
if (404 == err.status || err.expose) return;
if (this.silent) return;
const msg = err.stack || err.toString();
console.error();
console.error(msg.replace(/^/gm, ' '));
console.error();
}
2015-10-12 20:36:03 +00:00
};
2013-08-22 02:47:56 +00:00
2013-08-17 07:15:57 +00:00
/**
* Response helper.
2013-08-17 07:15:57 +00:00
*/
function respond(ctx) {
2014-04-15 15:39:40 +00:00
// allow bypassing koa
if (false === ctx.respond) return;
2016-03-04 03:57:52 +00:00
if (!ctx.writable) return;
2013-12-22 17:26:21 +00:00
const res = ctx.res;
let body = ctx.body;
const code = ctx.status;
// ignore body
if (statuses.empty[code]) {
// strip headers
ctx.body = null;
return res.end();
}
2013-08-17 07:15:57 +00:00
if ('HEAD' == ctx.method) {
2016-03-04 03:57:52 +00:00
if (!res.headersSent && isJSON(body)) {
ctx.length = Buffer.byteLength(JSON.stringify(body));
}
2014-04-15 02:06:32 +00:00
return res.end();
}
2014-04-15 15:39:40 +00:00
// status body
2014-04-15 02:06:32 +00:00
if (null == body) {
if (ctx.req.httpVersionMajor >= 2) {
body = String(code);
} else {
body = ctx.message || String(code);
}
2016-03-04 03:57:52 +00:00
if (!res.headersSent) {
ctx.type = 'text';
ctx.length = Buffer.byteLength(body);
}
2014-04-13 02:23:57 +00:00
return res.end(body);
}
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
// body: json
body = JSON.stringify(body);
2016-03-04 03:57:52 +00:00
if (!res.headersSent) {
ctx.length = Buffer.byteLength(body);
}
res.end(body);
2014-03-24 18:21:15 +00:00
}