From 182f9d6fa45834a13735b15d60c46b7e24cff6bb Mon Sep 17 00:00:00 2001 From: Jonathan Ong Date: Wed, 12 Mar 2014 18:29:14 -0700 Subject: [PATCH] refactor: use statuses --- lib/application.js | 8 ++++---- lib/response.js | 38 +++++++------------------------------- lib/status.js | 17 ----------------- package.json | 1 + test/context/onerror.js | 8 ++++---- 5 files changed, 16 insertions(+), 56 deletions(-) delete mode 100644 lib/status.js diff --git a/lib/application.js b/lib/application.js index 013724d..1b135ba 100644 --- a/lib/application.js +++ b/lib/application.js @@ -11,6 +11,7 @@ var request = require('./request'); var response = require('./response'); var Cookies = require('cookies'); var accepts = require('accepts'); +var status = require('statuses'); var assert = require('assert'); var http = require('http'); var only = require('only'); @@ -172,17 +173,16 @@ function *respond(next){ if (res.headersSent || !this.writable) return; var body = this.body; - var status = this.status = this.status || 404; + var code = this.status = this.status || 404; var head = 'HEAD' == this.method; - var noContent = ~[204, 205, 304].indexOf(status); // ignore body - if (noContent) return res.end(); + if (status.empty[code]) return res.end(); // status body if (null == body) { this.type = 'text'; - body = http.STATUS_CODES[status]; + body = status[code]; } // Buffer body diff --git a/lib/response.js b/lib/response.js index 7c4f545..8915660 100644 --- a/lib/response.js +++ b/lib/response.js @@ -4,7 +4,7 @@ */ var debug = require('debug')('koa:response'); -var statuses = require('./status'); +var status = require('statuses'); var http = require('http'); var path = require('path'); var mime = require('mime'); @@ -66,19 +66,14 @@ module.exports = { /** * Set response status code. * - * @param {Number|String} val + * @param {Number|String} code * @api public */ - set status(val) { - if ('string' == typeof val) { - var n = statuses[val.toLowerCase()]; - if (!n) throw new Error(statusError(val)); - val = n; - } - - this._status = this.res.statusCode = val; - if (this.body && ~[204, 205, 304].indexOf(val)) this.body = null; + set status(code) { + if ('number' != typeof code) code = status(code); + this._status = this.res.statusCode = code; + if (this.body && status.empty[code]) this.body = null; }, /** @@ -217,7 +212,7 @@ module.exports = { redirect: function(url, alt){ if ('back' == url) url = this.ctx.get('Referrer') || alt || '/'; this.set('Location', url); - if (!~[300, 301, 302, 303, 305, 307].indexOf(this.status)) this.status = 302; + if (!status.redirect[this.status]) this.status = 302; // html if (this.ctx.accepts('html')) { @@ -454,25 +449,6 @@ module.exports = { } }; -/** - * Return status error message. - * - * @param {String} val - * @return {String} - * @api private - */ - -function statusError(val) { - var s = 'invalid status string "' + val + '", try:\n\n'; - - Object.keys(statuses).forEach(function(name){ - var n = statuses[name]; - s += ' - ' + n + ' "' + name + '"\n'; - }); - - return s; -} - /** * Escape special characters in the given string of html. * diff --git a/lib/status.js b/lib/status.js deleted file mode 100644 index a9b879b..0000000 --- a/lib/status.js +++ /dev/null @@ -1,17 +0,0 @@ - -/** - * Module dependencies. - */ - -var http = require('http'); -var codes = http.STATUS_CODES; - -/** - * Produce exports[STATUS] = CODE map. - */ - -Object.keys(codes).forEach(function(code){ - var n = ~~code; - var s = codes[n].toLowerCase(); - exports[s] = n; -}); diff --git a/package.json b/package.json index 72c192c..a53a33a 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ ], "license": "MIT", "dependencies": { + "statuses": "~1.0.1", "accepts": "~1.0.0", "type-is": "~1.0.0", "finished": "~1.1.1", diff --git a/test/context/onerror.js b/test/context/onerror.js index 6ec053f..65a9262 100644 --- a/test/context/onerror.js +++ b/test/context/onerror.js @@ -9,14 +9,14 @@ describe('ctx.onerror(err)', function(){ app.use(function *(next){ this.body = 'something else'; - this.throw(499, 'boom'); + this.throw(418, 'boom'); }) var server = app.listen(); request(server) .get('/') - .expect(499) + .expect(418) .expect('Content-Type', 'text/plain; charset=utf-8') .expect('Content-Length', '4') .end(done); @@ -30,14 +30,14 @@ describe('ctx.onerror(err)', function(){ this.set('X-CSRF-Token', 'asdf'); this.body = 'response'; - this.throw(499, 'boom'); + this.throw(418, 'boom'); }) var server = app.listen(); request(server) .get('/') - .expect(499) + .expect(418) .expect('Content-Type', 'text/plain; charset=utf-8') .expect('Content-Length', '4') .end(function(err, res){