refactor: move content negotiation to accepts

This commit is contained in:
Jonathan Ong 2013-12-27 13:56:21 -08:00
parent 3891e1787b
commit 7fe4133b4a
3 changed files with 10 additions and 41 deletions

View file

@ -11,6 +11,7 @@ var request = require('./request');
var response = require('./response'); var response = require('./response');
var Cookies = require('cookies'); var Cookies = require('cookies');
var Keygrip = require('keygrip'); var Keygrip = require('keygrip');
var accepts = require('accepts');
var assert = require('assert'); var assert = require('assert');
var http = require('http'); var http = require('http');
var co = require('co'); var co = require('co');
@ -157,6 +158,7 @@ app.createContext = function(req, res){
context.onerror = context.onerror.bind(context); context.onerror = context.onerror.bind(context);
context.originalUrl = request.originalUrl = req.url; context.originalUrl = request.originalUrl = req.url;
context.cookies = new Cookies(req, res, this.keys); context.cookies = new Cookies(req, res, this.keys);
context.accept = request.accept = accepts(req);
return context; return context;
}; };

View file

@ -4,7 +4,6 @@
*/ */
var debug = require('debug')('koa:context'); var debug = require('debug')('koa:context');
var Negotiator = require('negotiator');
var qs = require('querystring'); var qs = require('querystring');
var fresh = require('fresh'); var fresh = require('fresh');
var mime = require('mime'); var mime = require('mime');
@ -394,16 +393,8 @@ module.exports = {
* @api public * @api public
*/ */
accepts: function(types){ accepts: function(){
if (!Array.isArray(types)) types = [].slice.call(arguments); return this.accept.types.apply(this.accept, arguments);
var n = new Negotiator(this.req);
if (!types.length) return n.preferredMediaTypes();
if (!this.header.accept) return types[0];
var mimes = types.map(extToMime);
var accepts = n.preferredMediaTypes(mimes);
var first = accepts[0];
if (!first) return false;
return types[mimes.indexOf(first)];
}, },
/** /**
@ -419,11 +410,8 @@ module.exports = {
* @api public * @api public
*/ */
acceptsEncodings: function(encodings) { acceptsEncodings: function(){
if (!Array.isArray(encodings)) encodings = [].slice.call(arguments); return this.accept.encodings.apply(this.accept, arguments);
var n = new Negotiator(this.req);
if (!encodings.length) return n.preferredEncodings();
return n.preferredEncodings(encodings)[0] || 'identity';
}, },
/** /**
@ -439,12 +427,8 @@ module.exports = {
* @api public * @api public
*/ */
acceptsCharsets: function(charsets) { acceptsCharsets: function(){
if (!Array.isArray(charsets)) charsets = [].slice.call(arguments); return this.accept.charsets.apply(this.accept, arguments);
var n = new Negotiator(this.req);
if (!charsets.length) return n.preferredCharsets();
if (!this.header['accept-charset']) return charsets[0];
return n.preferredCharsets(charsets)[0] || false;
}, },
/** /**
@ -461,11 +445,7 @@ module.exports = {
*/ */
acceptsLanguages: function(langs) { acceptsLanguages: function(langs) {
if (!Array.isArray(langs)) langs = [].slice.call(arguments); return this.accept.languages.apply(this.accept, arguments);
var n = new Negotiator(this.req);
if (!langs.length) return n.preferredLanguages();
if (!this.header['accept-language']) return langs[0];
return n.preferredLanguages(langs)[0] || false;
}, },
/** /**
@ -614,19 +594,6 @@ module.exports = {
} }
}; };
/**
* Convert extnames to mime.
*
* @param {String} type
* @return {String}
* @api private
*/
function extToMime(type) {
if (~type.indexOf('/')) return type;
return mime.lookup(type);
}
/** /**
* Check if `exected` mime type * Check if `exected` mime type
* matches `actual` mime type with * matches `actual` mime type with

View file

@ -21,12 +21,12 @@
], ],
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"accepts": "~1.0.0",
"on-socket-error": "~1.0.1", "on-socket-error": "~1.0.1",
"co": "~3.0.1", "co": "~3.0.1",
"debug": "*", "debug": "*",
"mime": "~1.2.11", "mime": "~1.2.11",
"fresh": "~0.2.0", "fresh": "~0.2.0",
"negotiator": "~0.3.0",
"koa-compose": "~2.1.0", "koa-compose": "~2.1.0",
"cookies": "~0.3.7", "cookies": "~0.3.7",
"keygrip": "~1.0.0" "keygrip": "~1.0.0"