From 7fe4133b4af158daf51d1eddf5ba91b621a44847 Mon Sep 17 00:00:00 2001 From: Jonathan Ong Date: Fri, 27 Dec 2013 13:56:21 -0800 Subject: [PATCH] refactor: move content negotiation to accepts --- lib/application.js | 2 ++ lib/request.js | 47 +++++++--------------------------------------- package.json | 2 +- 3 files changed, 10 insertions(+), 41 deletions(-) diff --git a/lib/application.js b/lib/application.js index 89e575c..98fb103 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 Keygrip = require('keygrip'); +var accepts = require('accepts'); var assert = require('assert'); var http = require('http'); var co = require('co'); @@ -157,6 +158,7 @@ app.createContext = function(req, res){ context.onerror = context.onerror.bind(context); context.originalUrl = request.originalUrl = req.url; context.cookies = new Cookies(req, res, this.keys); + context.accept = request.accept = accepts(req); return context; }; diff --git a/lib/request.js b/lib/request.js index f445ede..87b87dc 100644 --- a/lib/request.js +++ b/lib/request.js @@ -4,7 +4,6 @@ */ var debug = require('debug')('koa:context'); -var Negotiator = require('negotiator'); var qs = require('querystring'); var fresh = require('fresh'); var mime = require('mime'); @@ -394,16 +393,8 @@ module.exports = { * @api public */ - accepts: function(types){ - if (!Array.isArray(types)) types = [].slice.call(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)]; + accepts: function(){ + return this.accept.types.apply(this.accept, arguments); }, /** @@ -419,11 +410,8 @@ module.exports = { * @api public */ - acceptsEncodings: function(encodings) { - if (!Array.isArray(encodings)) encodings = [].slice.call(arguments); - var n = new Negotiator(this.req); - if (!encodings.length) return n.preferredEncodings(); - return n.preferredEncodings(encodings)[0] || 'identity'; + acceptsEncodings: function(){ + return this.accept.encodings.apply(this.accept, arguments); }, /** @@ -439,12 +427,8 @@ module.exports = { * @api public */ - acceptsCharsets: function(charsets) { - if (!Array.isArray(charsets)) charsets = [].slice.call(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; + acceptsCharsets: function(){ + return this.accept.charsets.apply(this.accept, arguments); }, /** @@ -461,11 +445,7 @@ module.exports = { */ acceptsLanguages: function(langs) { - if (!Array.isArray(langs)) langs = [].slice.call(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; + return this.accept.languages.apply(this.accept, arguments); }, /** @@ -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 * matches `actual` mime type with diff --git a/package.json b/package.json index c87ca5e..178ee12 100644 --- a/package.json +++ b/package.json @@ -21,12 +21,12 @@ ], "license": "MIT", "dependencies": { + "accepts": "~1.0.0", "on-socket-error": "~1.0.1", "co": "~3.0.1", "debug": "*", "mime": "~1.2.11", "fresh": "~0.2.0", - "negotiator": "~0.3.0", "koa-compose": "~2.1.0", "cookies": "~0.3.7", "keygrip": "~1.0.0"