From c1bed668bdd1a6351d5fdc7b839068e6a1f2e770 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Thu, 7 Nov 2013 17:05:26 -0800 Subject: [PATCH] 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 --- lib/application.js | 62 +--------------------------- lib/context.js | 23 ++++++++++- test/application.js | 98 --------------------------------------------- test/context.js | 10 +---- 4 files changed, 25 insertions(+), 168 deletions(-) diff --git a/lib/application.js b/lib/application.js index 0d34769..1b358b2 100644 --- a/lib/application.js +++ b/lib/application.js @@ -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 = ''; - * } - * }); - * - * @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); - } -} diff --git a/lib/context.js b/lib/context.js index 0761774..080b609 100644 --- a/lib/context.js +++ b/lib/context.js @@ -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. diff --git a/test/application.js b/test/application.js index 9f96ef3..a9acf3c 100644 --- a/test/application.js +++ b/test/application.js @@ -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); - }) -}) \ No newline at end of file diff --git a/test/context.js b/test/context.js index ab0604b..36fdd60 100644 --- a/test/context.js +++ b/test/context.js @@ -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: {} };