Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / errorhandler / node_modules / accepts / index.js
1 /*!
2  * accepts
3  * Copyright(c) 2014 Jonathan Ong
4  * Copyright(c) 2015 Douglas Christopher Wilson
5  * MIT Licensed
6  */
7
8 'use strict'
9
10 /**
11  * Module dependencies.
12  * @private
13  */
14
15 var Negotiator = require('negotiator')
16 var mime = require('mime-types')
17
18 /**
19  * Module exports.
20  * @public
21  */
22
23 module.exports = Accepts
24
25 /**
26  * Create a new Accepts object for the given req.
27  *
28  * @param {object} req
29  * @public
30  */
31
32 function Accepts(req) {
33   if (!(this instanceof Accepts))
34     return new Accepts(req)
35
36   this.headers = req.headers
37   this.negotiator = new Negotiator(req)
38 }
39
40 /**
41  * Check if the given `type(s)` is acceptable, returning
42  * the best match when true, otherwise `undefined`, in which
43  * case you should respond with 406 "Not Acceptable".
44  *
45  * The `type` value may be a single mime type string
46  * such as "application/json", the extension name
47  * such as "json" or an array `["json", "html", "text/plain"]`. When a list
48  * or array is given the _best_ match, if any is returned.
49  *
50  * Examples:
51  *
52  *     // Accept: text/html
53  *     this.types('html');
54  *     // => "html"
55  *
56  *     // Accept: text/*, application/json
57  *     this.types('html');
58  *     // => "html"
59  *     this.types('text/html');
60  *     // => "text/html"
61  *     this.types('json', 'text');
62  *     // => "json"
63  *     this.types('application/json');
64  *     // => "application/json"
65  *
66  *     // Accept: text/*, application/json
67  *     this.types('image/png');
68  *     this.types('png');
69  *     // => undefined
70  *
71  *     // Accept: text/*;q=.5, application/json
72  *     this.types(['html', 'json']);
73  *     this.types('html', 'json');
74  *     // => "json"
75  *
76  * @param {String|Array} types...
77  * @return {String|Array|Boolean}
78  * @public
79  */
80
81 Accepts.prototype.type =
82 Accepts.prototype.types = function (types_) {
83   var types = types_
84
85   // support flattened arguments
86   if (types && !Array.isArray(types)) {
87     types = new Array(arguments.length)
88     for (var i = 0; i < types.length; i++) {
89       types[i] = arguments[i]
90     }
91   }
92
93   // no types, return all requested types
94   if (!types || types.length === 0) {
95     return this.negotiator.mediaTypes()
96   }
97
98   if (!this.headers.accept) return types[0];
99   var mimes = types.map(extToMime);
100   var accepts = this.negotiator.mediaTypes(mimes.filter(validMime));
101   var first = accepts[0];
102   if (!first) return false;
103   return types[mimes.indexOf(first)];
104 }
105
106 /**
107  * Return accepted encodings or best fit based on `encodings`.
108  *
109  * Given `Accept-Encoding: gzip, deflate`
110  * an array sorted by quality is returned:
111  *
112  *     ['gzip', 'deflate']
113  *
114  * @param {String|Array} encodings...
115  * @return {String|Array}
116  * @public
117  */
118
119 Accepts.prototype.encoding =
120 Accepts.prototype.encodings = function (encodings_) {
121   var encodings = encodings_
122
123   // support flattened arguments
124   if (encodings && !Array.isArray(encodings)) {
125     encodings = new Array(arguments.length)
126     for (var i = 0; i < encodings.length; i++) {
127       encodings[i] = arguments[i]
128     }
129   }
130
131   // no encodings, return all requested encodings
132   if (!encodings || encodings.length === 0) {
133     return this.negotiator.encodings()
134   }
135
136   return this.negotiator.encodings(encodings)[0] || false
137 }
138
139 /**
140  * Return accepted charsets or best fit based on `charsets`.
141  *
142  * Given `Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5`
143  * an array sorted by quality is returned:
144  *
145  *     ['utf-8', 'utf-7', 'iso-8859-1']
146  *
147  * @param {String|Array} charsets...
148  * @return {String|Array}
149  * @public
150  */
151
152 Accepts.prototype.charset =
153 Accepts.prototype.charsets = function (charsets_) {
154   var charsets = charsets_
155
156   // support flattened arguments
157   if (charsets && !Array.isArray(charsets)) {
158     charsets = new Array(arguments.length)
159     for (var i = 0; i < charsets.length; i++) {
160       charsets[i] = arguments[i]
161     }
162   }
163
164   // no charsets, return all requested charsets
165   if (!charsets || charsets.length === 0) {
166     return this.negotiator.charsets()
167   }
168
169   return this.negotiator.charsets(charsets)[0] || false
170 }
171
172 /**
173  * Return accepted languages or best fit based on `langs`.
174  *
175  * Given `Accept-Language: en;q=0.8, es, pt`
176  * an array sorted by quality is returned:
177  *
178  *     ['es', 'pt', 'en']
179  *
180  * @param {String|Array} langs...
181  * @return {Array|String}
182  * @public
183  */
184
185 Accepts.prototype.lang =
186 Accepts.prototype.langs =
187 Accepts.prototype.language =
188 Accepts.prototype.languages = function (languages_) {
189   var languages = languages_
190
191   // support flattened arguments
192   if (languages && !Array.isArray(languages)) {
193     languages = new Array(arguments.length)
194     for (var i = 0; i < languages.length; i++) {
195       languages[i] = arguments[i]
196     }
197   }
198
199   // no languages, return all requested languages
200   if (!languages || languages.length === 0) {
201     return this.negotiator.languages()
202   }
203
204   return this.negotiator.languages(languages)[0] || false
205 }
206
207 /**
208  * Convert extnames to mime.
209  *
210  * @param {String} type
211  * @return {String}
212  * @private
213  */
214
215 function extToMime(type) {
216   return type.indexOf('/') === -1
217     ? mime.lookup(type)
218     : type
219 }
220
221 /**
222  * Check if mime is valid.
223  *
224  * @param {String} type
225  * @return {String}
226  * @private
227  */
228
229 function validMime(type) {
230   return typeof type === 'string';
231 }