remove app.context() for now

get away from promoting the extension of prototypes, aside
from it looking better there isnt really a compelling reason
to allow this
master
TJ Holowaychuk 2013-11-07 17:05:26 -08:00
parent 8ee8abcc32
commit c1bed668bd
4 changed files with 25 additions and 168 deletions

View File

@ -6,8 +6,7 @@
var debug = require('debug')('koa:application');
var Emitter = require('events').EventEmitter;
var compose = require('koa-compose');
var context = require('./context');
var Cookies = require('cookies');
var Context = require('./context');
var Stream = require('stream');
var http = require('http');
var co = require('co');
@ -39,8 +38,6 @@ function Application() {
this.poweredBy = true;
this.jsonSpaces = 2;
this.middleware = [];
this.Context = createContext();
this.context(context);
}
/**
@ -78,44 +75,6 @@ app.use = function(fn){
return this;
};
/**
* Mixin `obj` to this app's context.
*
* app.context({
* get something(){
* return 'hi';
* },
*
* set something(val){
* this._something = val;
* },
*
* render: function(){
* this.body = '<html></html>';
* }
* });
*
* @param {Object} obj
* @return {Application} self
* @api public
*/
app.context = function(obj){
var ctx = this.Context.prototype;
var names = Object.getOwnPropertyNames(obj);
debug('context: %j', names);
names.forEach(function(name){
if (Object.getOwnPropertyDescriptor(ctx, name)) {
debug('context: overwriting %j', name);
}
var descriptor = Object.getOwnPropertyDescriptor(obj, name);
Object.defineProperty(ctx, name, descriptor);
});
return this;
};
/**
* Return a request handler callback
* for node's native http server.
@ -130,7 +89,7 @@ app.callback = function(){
var self = this;
return function(req, res){
var ctx = new self.Context(self, req, res);
var ctx = new Context(self, req, res);
co.call(ctx, gen)(function(err){
if (err) ctx.onerror(err);
@ -206,20 +165,3 @@ function *respond(next){
if (head) return res.end();
res.end(body);
}
/**
* 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;
this.cookies = new Cookies(req, res);
this.onerror = this.onerror.bind(this);
}
}

View File

@ -5,6 +5,7 @@
var debug = require('debug')('koa:context');
var Negotiator = require('negotiator');
var statuses = require('./status');
var Cookies = require('cookies');
var qs = require('querystring');
var Stream = require('stream');
var fresh = require('fresh');
@ -17,11 +18,31 @@ var url = require('url');
var parse = url.parse;
var stringify = url.format;
/**
* Expose `Context`.
*/
module.exports = Context;
/**
* Initialie a new Context.
*
* @api private
*/
function Context(app, req, res){
this.cookies = new Cookies(req, res);
this.onerror = this.onerror.bind(this);
this.app = app;
this.req = req;
this.res = res;
}
/**
* Prototype.
*/
module.exports = {
Context.prototype = {
/**
* Return request header.

View File

@ -295,101 +295,3 @@ describe('app.respond', function(){
})
})
})
describe('app.context(obj)', function(){
it('should merge regular object properties', function(done){
var app = koa();
app.context({
a: 1,
b: 2
});
app.use(function *(next){
this.a.should.equal(1);
this.b.should.equal(2);
this.status = 204;
});
var server = app.listen();
request(server)
.get('/')
.expect(204)
.end(done);
})
it('should merge accessor properties', function(done){
var app = koa();
app.context({
get something() {
return this._something || 'hi';
},
set something(value) {
this._something = value;
}
});
app.use(function *(next){
this.something.should.equal('hi');
this.something = 'hello';
this.something.should.equal('hello');
this.status = 204;
});
var server = app.listen();
request(server)
.get('/')
.expect(204)
.end(done);
})
it('should merge multiple objects', function(done){
var app = koa();
app.context({
a: 1
});
app.context({
b: 2
});
app.use(function *(){
this.a.should.equal(1);
this.b.should.equal(2);
this.status = 204;
});
var server = app.listen();
request(server)
.get('/')
.expect(204)
.end(done);
})
it('should not butcher the original prototype', function(done){
var app1 = koa();
var app2 = koa();
app1.context({
a: 1
});
app2.use(function *(next){
assert.equal(this.a, undefined);
this.status = 204;
});
var server = http.createServer(app2.callback());
request(server)
.get('/')
.expect(204)
.end(done);
})
})

View File

@ -1,18 +1,10 @@
var _context = require('../lib/context');
var Context = require('../lib/context');
var request = require('supertest');
var assert = require('assert');
var koa = require('..');
var fs = require('fs');
function Context(app, req, res) {
this.app = app;
this.req = req;
this.res = res;
}
Context.prototype = _context;
function context(req, res) {
req = req || { headers: {} };
res = res || { _headers: {} };