koa-lite/lib/context.js

497 lines
7.7 KiB
JavaScript
Raw Normal View History

2013-08-17 07:15:57 +00:00
/**
* Module dependencies.
*/
var debug = require('debug')('koa:context');
var http = require('http');
/**
* Prototype.
*/
module.exports = {
2013-08-17 07:15:57 +00:00
/**
* Inspect implementation.
*
* @return {Object}
* @api public
*/
inspect: function(){
return this.toJSON();
},
/**
* Return JSON representation.
*
* Here we explicitly invoke .toJSON() on each
* object, as iteration will otherwise fail due
* to the getters and cause utilities such as
* clone() to fail.
*
* @return {Object}
* @api public
*/
toJSON: function(){
return {
request: this.request.toJSON(),
response: this.response.toJSON()
}
},
/**
* Throw an error with `msg` and optional `status`
* defaulting to 500. Note that these are user-level
* errors, and the message may be exposed to the client.
*
* this.throw(403)
* this.throw('name required', 400)
* this.throw(400, 'name required')
* this.throw('something exploded')
*
* @param {String|Number} msg or status
* @param {String|Number} msg or status
* @api public
*/
throw: function(msg, status){
if ('number' == typeof msg) {
var tmp = msg;
msg = status || http.STATUS_CODES[tmp];
status = tmp;
}
var err = new Error(msg);
err.status = status || 500;
err.expose = true;
throw err;
},
/**
* Alias for .throw() for backwards compatibility.
* Do not use - will be removed in the future.
*
* @param {String|Number} msg
* @param {Number} status
* @api private
*/
error: function(msg, status){
console.warn('ctx.error is deprecated, use ctx.throw');
this.throw(msg, status);
},
/**
* Default error handling.
*
* @param {Error} err
* @api private
*/
onerror: function(err){
// don't do anything if there is no error.
// this allows you to pass `this.onerror`
// to node-style callbacks.
if (!err) return;
// nothing we can do here other
// than delegate to the app-level
// handler and log.
2013-12-18 01:37:35 +00:00
if (this.headerSent || !this.socket.writable) {
err.headerSent = true;
this.app.emit('error', err, this);
return;
}
// delegate
this.app.emit('error', err, this);
// force text/plain
this.type = 'text';
// ENOENT support
if ('ENOENT' == err.code) err.status = 404;
// default to 500
err.status = err.status || 500;
// respond
var code = http.STATUS_CODES[err.status];
var msg = err.expose ? err.message : code;
this.status = err.status;
this.res.end(msg);
},
2013-08-17 07:15:57 +00:00
/**
* Delegate to Request#header.
2013-08-17 07:15:57 +00:00
*/
get header() {
return this.request.header;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#url.
2013-08-17 07:15:57 +00:00
*/
get url() {
return this.request.url;
},
2013-08-17 07:15:57 +00:00
/**
* Delegate to Request#url=.
*/
2013-08-28 02:30:35 +00:00
set url(val) {
this.request.url = val;
2013-08-28 02:30:35 +00:00
},
2013-08-28 02:30:35 +00:00
/**
* Delegate to Request#method.
2013-08-28 02:30:35 +00:00
*/
get method() {
return this.request.method;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#method=.
2013-08-17 07:15:57 +00:00
*/
set method(val) {
this.request.method = val;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Response#status.
2013-08-17 07:15:57 +00:00
*/
2013-10-24 06:54:07 +00:00
get status() {
return this.response.status;
2013-10-24 06:44:22 +00:00
},
2013-08-17 07:15:57 +00:00
/**
* Delegate to Response#status=.
2013-08-17 07:15:57 +00:00
*/
set status(val) {
this.response.status = val;
2013-10-24 06:44:22 +00:00
},
2013-08-17 07:15:57 +00:00
/**
* Delegate to Response#body.
2013-08-17 07:15:57 +00:00
*/
get body() {
return this.response.body;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Response#body=.
2013-08-17 07:15:57 +00:00
*/
set body(val) {
this.response.body = val;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#path.
2013-08-17 07:15:57 +00:00
*/
get path() {
return this.request.path;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#path=.
2013-08-17 07:15:57 +00:00
*/
set path(val) {
this.request.path = val;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#query.
2013-08-17 07:15:57 +00:00
*/
get query() {
return this.request.query;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#query=.
2013-08-17 07:15:57 +00:00
*/
set query(val) {
this.request.query = val;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#querystring.
2013-08-17 07:15:57 +00:00
*/
get querystring() {
return this.request.querystring;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#querystring=.
2013-08-17 07:15:57 +00:00
*/
set querystring(val) {
this.request.querystring = val;
2013-08-17 07:15:57 +00:00
},
2013-11-19 03:29:29 +00:00
/**
* Delegate to Request#search.
*/
get search() {
return this.request.search;
},
/**
* Delegate to Request#search=.
*/
set search(val) {
this.request.search = val;
},
2013-08-17 07:15:57 +00:00
/**
* Delegate to Request#host.
2013-08-17 07:15:57 +00:00
*/
get host() {
return this.request.host;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#fresh.
2013-08-17 07:15:57 +00:00
*/
get fresh() {
return this.request.fresh;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#stale.
2013-08-17 07:15:57 +00:00
*/
get stale() {
return this.request.stale;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#idempotent.
2013-08-17 07:15:57 +00:00
*/
get idempotent() {
return this.request.idempotent;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#socket.
2013-08-17 07:15:57 +00:00
*/
get socket() {
return this.request.socket;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#length.
2013-08-17 07:15:57 +00:00
*/
get length() {
return this.request.length;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#length.
*/
set length(val) {
this.response.length = val;
},
2013-08-17 07:15:57 +00:00
/**
* Delegate to Request#protocol.
2013-08-17 07:15:57 +00:00
*/
get protocol() {
return this.request.protocol;
},
2013-08-17 07:15:57 +00:00
/**
* Delegate to Request#secure.
*/
2013-08-17 07:15:57 +00:00
get secure() {
return this.request.secure;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#ip.
2013-08-17 07:15:57 +00:00
*/
get ip() {
return this.request.ip;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#ips.
2013-08-17 07:15:57 +00:00
*/
get ips() {
return this.request.ips;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#subdomains.
2013-08-17 07:15:57 +00:00
*/
get subdomains() {
return this.request.subdomains;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Response#headerSent.
2013-08-17 07:15:57 +00:00
*/
get headerSent() {
return this.response.headerSent;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Response#type=.
2013-08-17 07:15:57 +00:00
*/
set type(val) {
this.response.type = val;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#type.
2013-08-17 07:15:57 +00:00
*/
get type() {
return this.request.type;
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Response#lastModified=.
*/
set lastModified(val) {
this.response.lastModified = val;
},
/**
* Delegate to Response#etag=.
*/
set etag(val) {
this.response.etag = val;
},
/**
* Delegate to Request#remove().
*/
remove: function() {
return this.response.remove.apply(this.response, arguments);
},
2013-08-17 07:15:57 +00:00
/**
* Delegate to Request#accepts().
2013-08-17 07:15:57 +00:00
*/
accepts: function() {
return this.request.accepts.apply(this.request, arguments);
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#acceptsCharsets().
2013-08-17 07:15:57 +00:00
*/
acceptsCharsets: function() {
return this.request.acceptsCharsets.apply(this.request, arguments);
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#acceptsEncodings().
2013-08-17 07:15:57 +00:00
*/
acceptsEncodings: function() {
return this.request.acceptsEncodings.apply(this.request, arguments);
2013-08-17 07:15:57 +00:00
},
/**
* Delegate to Request#acceptsLanguages().
2013-08-17 07:15:57 +00:00
*/
acceptsLanguages: function() {
return this.request.acceptsLanguages.apply(this.request, arguments);
2013-08-17 07:15:57 +00:00
},
2013-10-24 06:54:07 +00:00
2013-11-08 22:38:35 +00:00
/**
* Delegate to Response#vary().
2013-11-08 22:38:35 +00:00
*/
vary: function() {
return this.response.vary.apply(this.response, arguments);
},
2013-08-17 07:15:57 +00:00
/**
* Delegate to Request#is().
*/
is: function() {
return this.request.is.apply(this.request, arguments);
},
/**
* Delegate to Response#append().
*/
append: function() {
return this.response.append.apply(this.response, arguments);
},
/**
* Delegate to Request#get().
*/
get: function() {
return this.request.get.apply(this.request, arguments);
},
/**
* Delegate to Response#set().
*/
set: function() {
return this.response.set.apply(this.response, arguments);
},
/**
* Delegate to Response#redirect().
*/
redirect: function() {
return this.response.redirect.apply(this.response, arguments);
},
/**
* Delegate to Response#attachment().
*/
attachment: function() {
return this.response.attachment.apply(this.response, arguments);
2013-12-29 13:46:12 +00:00
}
2013-08-17 07:15:57 +00:00
};