pax_global_header00006660000000000000000000000064124121421560014510gustar00rootroot0000000000000052 comment=57e2960cfc6e8863c258613aa7e5947169e1d824 accepts-1.1.1/000077500000000000000000000000001241214215600131325ustar00rootroot00000000000000accepts-1.1.1/.gitignore000066400000000000000000000000641241214215600151220ustar00rootroot00000000000000 .DS_Store* *.log *.gz node_modules coverage cache accepts-1.1.1/.travis.yml000066400000000000000000000002521241214215600152420ustar00rootroot00000000000000node_js: - "0.8" - "0.10" - "0.11" language: node_js script: "npm run-script test-travis" after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" accepts-1.1.1/HISTORY.md000066400000000000000000000020161241214215600146140ustar00rootroot000000000000001.1.1 / 2014-09-28 ================== * deps: mime-types@~2.0.2 - deps: mime-db@~1.1.0 * deps: negotiator@0.4.8 - Fix all negotiations to be case-insensitive - Stable sort preferences of same quality according to client order 1.1.0 / 2014-09-02 ================== * update `mime-types` 1.0.7 / 2014-07-04 ================== * Fix wrong type returned from `type` when match after unknown extension 1.0.6 / 2014-06-24 ================== * deps: negotiator@0.4.7 1.0.5 / 2014-06-20 ================== * fix crash when unknown extension given 1.0.4 / 2014-06-19 ================== * use `mime-types` 1.0.3 / 2014-06-11 ================== * deps: negotiator@0.4.6 - Order by specificity when quality is the same 1.0.2 / 2014-05-29 ================== * Fix interpretation when header not in request * deps: pin negotiator@0.4.5 1.0.1 / 2014-01-18 ================== * Identity encoding isn't always acceptable * deps: negotiator@~0.4.0 1.0.0 / 2013-12-27 ================== * Genesis accepts-1.1.1/LICENSE000066400000000000000000000021101241214215600141310ustar00rootroot00000000000000(The MIT License) Copyright (c) 2014 Jonathan Ong Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. accepts-1.1.1/README.md000066400000000000000000000055701241214215600144200ustar00rootroot00000000000000# accepts [![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][downloads-url] [![Node.js Version][node-version-image]][node-version-url] [![Build Status][travis-image]][travis-url] [![Test Coverage][coveralls-image]][coveralls-url] Higher level content negotation based on [negotiator](https://github.com/federomero/negotiator). Extracted from [koa](https://github.com/koajs/koa) for general use. In addition to negotatior, it allows: - Allows types as an array or arguments list, ie `(['text/html', 'application/json'])` as well as `('text/html', 'application/json')`. - Allows type shorthands such as `json`. - Returns `false` when no types match - Treats non-existent headers as `*` ## API ### var accept = new Accepts(req) ```js var accepts = require('accepts') http.createServer(function (req, res) { var accept = accepts(req) }) ``` ### accept\[property\]\(\) Returns all the explicitly accepted content property as an array in descending priority. - `accept.types()` - `accept.encodings()` - `accept.charsets()` - `accept.languages()` They are also aliased in singular form such as `accept.type()`. `accept.languages()` is also aliased as `accept.langs()`, etc. Note: you should almost never do this in a real app as it defeats the purpose of content negotiation. Example: ```js // in Google Chrome var encodings = accept.encodings() // -> ['sdch', 'gzip', 'deflate'] ``` Since you probably don't support `sdch`, you should just supply the encodings you support: ```js var encoding = accept.encodings('gzip', 'deflate') // -> 'gzip', probably ``` ### accept\[property\]\(values, ...\) You can either have `values` be an array or have an argument list of values. If the client does not accept any `values`, `false` will be returned. If the client accepts any `values`, the preferred `value` will be return. For `accept.types()`, shorthand mime types are allowed. Example: ```js // req.headers.accept = 'application/json' accept.types('json') // -> 'json' accept.types('html', 'json') // -> 'json' accept.types('html') // -> false // req.headers.accept = '' // which is equivalent to `*` accept.types() // -> [], no explicit types accept.types('text/html', 'text/json') // -> 'text/html', since it was first ``` ## License [MIT](LICENSE) [npm-image]: https://img.shields.io/npm/v/accepts.svg?style=flat [npm-url]: https://npmjs.org/package/accepts [node-version-image]: https://img.shields.io/badge/node.js-%3E%3D_0.8-brightgreen.svg?style=flat [node-version-url]: http://nodejs.org/download/ [travis-image]: https://img.shields.io/travis/jshttp/accepts.svg?style=flat [travis-url]: https://travis-ci.org/jshttp/accepts [coveralls-image]: https://img.shields.io/coveralls/jshttp/accepts.svg?style=flat [coveralls-url]: https://coveralls.io/r/jshttp/accepts [downloads-image]: https://img.shields.io/npm/dm/accepts.svg?style=flat [downloads-url]: https://npmjs.org/package/accepts accepts-1.1.1/index.js000066400000000000000000000076701241214215600146110ustar00rootroot00000000000000var Negotiator = require('negotiator') var mime = require('mime-types') var slice = [].slice module.exports = Accepts function Accepts(req) { if (!(this instanceof Accepts)) return new Accepts(req) this.headers = req.headers this.negotiator = Negotiator(req) } /** * Check if the given `type(s)` is acceptable, returning * the best match when true, otherwise `undefined`, in which * case you should respond with 406 "Not Acceptable". * * The `type` value may be a single mime type string * such as "application/json", the extension name * such as "json" or an array `["json", "html", "text/plain"]`. When a list * or array is given the _best_ match, if any is returned. * * Examples: * * // Accept: text/html * this.types('html'); * // => "html" * * // Accept: text/*, application/json * this.types('html'); * // => "html" * this.types('text/html'); * // => "text/html" * this.types('json', 'text'); * // => "json" * this.types('application/json'); * // => "application/json" * * // Accept: text/*, application/json * this.types('image/png'); * this.types('png'); * // => undefined * * // Accept: text/*;q=.5, application/json * this.types(['html', 'json']); * this.types('html', 'json'); * // => "json" * * @param {String|Array} type(s)... * @return {String|Array|Boolean} * @api public */ Accepts.prototype.type = Accepts.prototype.types = function (types) { if (!Array.isArray(types)) types = slice.call(arguments); var n = this.negotiator; if (!types.length) return n.mediaTypes(); if (!this.headers.accept) return types[0]; var mimes = types.map(extToMime); var accepts = n.mediaTypes(mimes.filter(validMime)); var first = accepts[0]; if (!first) return false; return types[mimes.indexOf(first)]; } /** * Return accepted encodings or best fit based on `encodings`. * * Given `Accept-Encoding: gzip, deflate` * an array sorted by quality is returned: * * ['gzip', 'deflate'] * * @param {String|Array} encoding(s)... * @return {String|Array} * @api public */ Accepts.prototype.encoding = Accepts.prototype.encodings = function (encodings) { if (!Array.isArray(encodings)) encodings = slice.call(arguments); var n = this.negotiator; if (!encodings.length) return n.encodings(); return n.encodings(encodings)[0] || false; } /** * Return accepted charsets or best fit based on `charsets`. * * Given `Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5` * an array sorted by quality is returned: * * ['utf-8', 'utf-7', 'iso-8859-1'] * * @param {String|Array} charset(s)... * @return {String|Array} * @api public */ Accepts.prototype.charset = Accepts.prototype.charsets = function (charsets) { if (!Array.isArray(charsets)) charsets = [].slice.call(arguments); var n = this.negotiator; if (!charsets.length) return n.charsets(); if (!this.headers['accept-charset']) return charsets[0]; return n.charsets(charsets)[0] || false; } /** * Return accepted languages or best fit based on `langs`. * * Given `Accept-Language: en;q=0.8, es, pt` * an array sorted by quality is returned: * * ['es', 'pt', 'en'] * * @param {String|Array} lang(s)... * @return {Array|String} * @api public */ Accepts.prototype.lang = Accepts.prototype.langs = Accepts.prototype.language = Accepts.prototype.languages = function (langs) { if (!Array.isArray(langs)) langs = slice.call(arguments); var n = this.negotiator; if (!langs.length) return n.languages(); if (!this.headers['accept-language']) return langs[0]; return n.languages(langs)[0] || false; } /** * 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 mime is valid. * * @param {String} type * @return {String} * @api private */ function validMime(type) { return typeof type === 'string'; } accepts-1.1.1/package.json000066400000000000000000000015561241214215600154270ustar00rootroot00000000000000{ "name": "accepts", "description": "Higher-level content negotiation", "version": "1.1.1", "author": "Jonathan Ong (http://jongleberry.com)", "license": "MIT", "repository": "jshttp/accepts", "dependencies": { "mime-types": "~2.0.2", "negotiator": "0.4.8" }, "devDependencies": { "istanbul": "~0.3.0", "mocha": "1" }, "files": [ "LICENSE", "HISTORY.md", "index.js" ], "engines": { "node": ">= 0.8.0" }, "scripts": { "test": "mocha --reporter spec --check-leaks --bail test/", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/", "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/" }, "keywords": [ "content", "negotiation", "accept", "accepts" ] } accepts-1.1.1/test/000077500000000000000000000000001241214215600141115ustar00rootroot00000000000000accepts-1.1.1/test/charset.js000066400000000000000000000044061241214215600161040ustar00rootroot00000000000000 var accepts = require('..') var assert = require('assert') describe('accepts.charsets()', function(){ describe('with no arguments', function(){ describe('when Accept-Charset is populated', function(){ it('should return accepted types', function(){ var req = createRequest('utf-8, iso-8859-1;q=0.2, utf-7;q=0.5') var accept = accepts(req) assert.deepEqual(accept.charsets(), ['utf-8', 'utf-7', 'iso-8859-1']) }) }) describe('when Accept-Charset is not in request', function(){ it('should return *', function(){ var req = createRequest() var accept = accepts(req) assert.deepEqual(accept.charsets(), ['*']) }) }) describe('when Accept-Charset is empty', function(){ it('should return an empty array', function(){ var req = createRequest('') var accept = accepts(req) assert.deepEqual(accept.charsets(), []) }) }) }) describe('with multiple arguments', function(){ describe('when Accept-Charset is populated', function(){ describe('if any types match', function(){ it('should return the best fit', function(){ var req = createRequest('utf-8, iso-8859-1;q=0.2, utf-7;q=0.5') var accept = accepts(req) assert.equal(accept.charsets('utf-7', 'utf-8'), 'utf-8') }) }) describe('if no types match', function(){ it('should return false', function(){ var req = createRequest('utf-8, iso-8859-1;q=0.2, utf-7;q=0.5') var accept = accepts(req) assert.strictEqual(accept.charsets('utf-16'), false) }) }) }) describe('when Accept-Charset is not populated', function(){ it('should return the first type', function(){ var req = createRequest() var accept = accepts(req) assert.equal(accept.charsets('utf-7', 'utf-8'), 'utf-7') }) }) }) describe('with an array', function(){ it('should return the best fit', function(){ var req = createRequest('utf-8, iso-8859-1;q=0.2, utf-7;q=0.5') var accept = accepts(req) assert.equal(accept.charsets(['utf-7', 'utf-8']), 'utf-8') }) }) }) function createRequest(charset) { return { headers: { 'accept-charset': charset } } } accepts-1.1.1/test/encoding.js000066400000000000000000000046021241214215600162370ustar00rootroot00000000000000 var accepts = require('..') var assert = require('assert') describe('accepts.encodings()', function(){ describe('with no arguments', function(){ describe('when Accept-Encoding is populated', function(){ it('should return accepted types', function(){ var req = createRequest('gzip, compress;q=0.2') var accept = accepts(req) assert.deepEqual(accept.encodings(), ['gzip', 'compress', 'identity']) assert.equal(accept.encodings('gzip', 'compress'), 'gzip') }) }) describe('when Accept-Encoding is not in request', function(){ it('should return identity', function(){ var req = createRequest() var accept = accepts(req) assert.deepEqual(accept.encodings(), ['identity']) assert.equal(accept.encodings('gzip', 'deflate', 'identity'), 'identity') }) describe('when identity is not included', function(){ it('should return false', function(){ var req = createRequest() var accept = accepts(req) assert.strictEqual(accept.encodings('gzip', 'deflate'), false) }) }) }) describe('when Accept-Encoding is empty', function(){ it('should return identity', function(){ var req = createRequest('') var accept = accepts(req) assert.deepEqual(accept.encodings(), ['identity']) assert.equal(accept.encodings('gzip', 'deflate', 'identity'), 'identity') }) describe('when identity is not included', function(){ it('should return false', function(){ var req = createRequest('') var accept = accepts(req) assert.strictEqual(accept.encodings('gzip', 'deflate'), false) }) }) }) }) describe('with multiple arguments', function(){ it('should return the best fit', function(){ var req = createRequest('gzip, compress;q=0.2') var accept = accepts(req) assert.equal(accept.encodings('compress', 'gzip'), 'gzip') assert.equal(accept.encodings('gzip', 'compress'), 'gzip') }) }) describe('with an array', function(){ it('should return the best fit', function(){ var req = createRequest('gzip, compress;q=0.2') var accept = accepts(req) assert.equal(accept.encodings(['compress', 'gzip']), 'gzip') }) }) }) function createRequest(encoding) { return { headers: { 'accept-encoding': encoding } } } accepts-1.1.1/test/language.js000066400000000000000000000042451241214215600162370ustar00rootroot00000000000000 var accepts = require('..') var assert = require('assert') describe('accepts.languages()', function(){ describe('with no arguments', function(){ describe('when Accept-Language is populated', function(){ it('should return accepted types', function(){ var req = createRequest('en;q=0.8, es, pt') var accept = accepts(req) assert.deepEqual(accept.languages(), ['es', 'pt', 'en']) }) }) describe('when Accept-Language is not in request', function(){ it('should return *', function(){ var req = createRequest() var accept = accepts(req) assert.deepEqual(accept.languages(), ['*']) }) }) describe('when Accept-Language is empty', function(){ it('should return an empty array', function(){ var req = createRequest('') var accept = accepts(req) assert.deepEqual(accept.languages(), []) }) }) }) describe('with multiple arguments', function(){ describe('when Accept-Language is populated', function(){ describe('if any types types match', function(){ it('should return the best fit', function(){ var req = createRequest('en;q=0.8, es, pt') var accept = accepts(req) assert.equal(accept.languages('es', 'en'), 'es') }) }) describe('if no types match', function(){ it('should return false', function(){ var req = createRequest('en;q=0.8, es, pt') var accept = accepts(req) assert.strictEqual(accept.languages('fr', 'au'), false) }) }) }) describe('when Accept-Language is not populated', function(){ it('should return the first type', function(){ var req = createRequest() var accept = accepts(req) assert.equal(accept.languages('es', 'en'), 'es') }) }) }) describe('with an array', function(){ it('should return the best fit', function(){ var req = createRequest('en;q=0.8, es, pt') var accept = accepts(req) assert.equal(accept.languages(['es', 'en']), 'es') }) }) }) function createRequest(language) { return { headers: { 'accept-language': language } } } accepts-1.1.1/test/type.js000066400000000000000000000100341241214215600154260ustar00rootroot00000000000000 var accepts = require('..') var assert = require('assert') describe('accepts.types()', function(){ describe('with no arguments', function(){ describe('when Accept is populated', function(){ it('should return all accepted types', function(){ var req = createRequest('application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain') var accept = accepts(req) assert.deepEqual(accept.types(), ['text/html', 'text/plain', 'image/jpeg', 'application/*']) }) }) describe('when Accept not in request', function(){ it('should return */*', function(){ var req = createRequest() var accept = accepts(req) assert.deepEqual(accept.types(), ['*/*']) }) }) describe('when Accept is empty', function(){ it('should return []', function(){ var req = createRequest('') var accept = accepts(req) assert.deepEqual(accept.types(), []) }) }) }) describe('with no valid types', function(){ describe('when Accept is populated', function(){ it('should return false', function(){ var req = createRequest('application/*;q=0.2, image/jpeg;q=0.8, text/html, text/plain') var accept = accepts(req) assert.strictEqual(accept.types('image/png', 'image/tiff'), false) }) }) describe('when Accept is not populated', function(){ it('should return the first type', function(){ var req = createRequest() var accept = accepts(req) assert.equal(accept.types('text/html', 'text/plain', 'image/jpeg', 'application/*'), 'text/html') }) }) }) describe('when extensions are given', function(){ it('should convert to mime types', function(){ var req = createRequest('text/plain, text/html') var accept = accepts(req) assert.equal(accept.types('html'), 'html') assert.equal(accept.types('.html'), '.html') assert.equal(accept.types('txt'), 'txt') assert.equal(accept.types('.txt'), '.txt') assert.strictEqual(accept.types('png'), false) assert.strictEqual(accept.types('bogus'), false) }) }) describe('when an array is given', function(){ it('should return the first match', function(){ var req = createRequest('text/plain, text/html') var accept = accepts(req) assert.equal(accept.types(['png', 'text', 'html']), 'text') assert.equal(accept.types(['png', 'html']), 'html') assert.equal(accept.types(['bogus', 'html']), 'html') }) }) describe('when multiple arguments are given', function(){ it('should return the first match', function(){ var req = createRequest('text/plain, text/html') var accept = accepts(req) assert.equal(accept.types('png', 'text', 'html'), 'text') assert.equal(accept.types('png', 'html'), 'html') assert.equal(accept.types('bogus', 'html'), 'html') }) }) describe('when present in Accept as an exact match', function(){ it('should return the type', function(){ var req = createRequest('text/plain, text/html') var accept = accepts(req) assert.equal(accept.types('text/html'), 'text/html') assert.equal(accept.types('text/plain'), 'text/plain') }) }) describe('when present in Accept as a type match', function(){ it('should return the type', function(){ var req = createRequest('application/json, */*') var accept = accepts(req) assert.equal(accept.types('text/html'), 'text/html') assert.equal(accept.types('text/plain'), 'text/plain') assert.equal(accept.types('image/png'), 'image/png') }) }) describe('when present in Accept as a subtype match', function(){ it('should return the type', function(){ var req = createRequest('application/json, text/*') var accept = accepts(req) assert.equal(accept.types('text/html'), 'text/html') assert.equal(accept.types('text/plain'), 'text/plain') assert.strictEqual(accept.types('image/png'), false) }) }) }) function createRequest(type) { return { headers: { 'accept': type } } }