add createContext()

This commit is contained in:
TJ Holowaychuk 2013-08-18 11:14:01 -07:00
parent aa8475dc78
commit 8ccfc14e88

View file

@ -37,11 +37,7 @@ function Application() {
this.poweredBy = true; this.poweredBy = true;
this.jsonSpaces = 2; this.jsonSpaces = 2;
this.middleware = []; this.middleware = [];
this._Context = function Context(app, req, res){ this.Context = createContext();
this.app = app;
this.req = req;
this.res = res;
}
this.context(context); this.context(context);
} }
@ -101,7 +97,7 @@ app.use = function(fn){
*/ */
app.context = function(obj){ app.context = function(obj){
var ctx = this._Context.prototype; var ctx = this.Context.prototype;
Object.getOwnPropertyNames(obj).forEach(function(name){ Object.getOwnPropertyNames(obj).forEach(function(name){
var descriptor = Object.getOwnPropertyDescriptor(obj, name); var descriptor = Object.getOwnPropertyDescriptor(obj, name);
Object.defineProperty(ctx, name, descriptor); Object.defineProperty(ctx, name, descriptor);
@ -123,7 +119,7 @@ app.callback = function(){
var self = this; var self = this;
return function(req, res){ return function(req, res){
var ctx = new self._Context(self, req, res); var ctx = new self.Context(self, req, res);
function done(err) { function done(err) {
if (err) ctx.error(err); if (err) ctx.error(err);
@ -198,9 +194,26 @@ function respond(next){
/** /**
* Default downstream middleware. * Default downstream middleware.
*
* @api private
*/ */
function *downstream() { function *downstream() {
this.status = 200; this.status = 200;
if (this.app.poweredBy) this.set('X-Powered-By', 'koa'); if (this.app.poweredBy) this.set('X-Powered-By', 'koa');
} }
/**
* Create a new `Context` constructor.
*
* @return {Function}
* @api private
*/
function createContext() {
return function Context(app, req, res){
this.app = app;
this.req = req;
this.res = res;
}
}