source: frontend/node_modules/express/lib/utils.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/*!
2 * express
3 * Copyright(c) 2009-2013 TJ Holowaychuk
4 * Copyright(c) 2014-2015 Douglas Christopher Wilson
5 * MIT Licensed
6 */
7
8'use strict';
9
10/**
11 * Module dependencies.
12 * @api private
13 */
14
15var Buffer = require('safe-buffer').Buffer
16var contentDisposition = require('content-disposition');
17var contentType = require('content-type');
18var deprecate = require('depd')('express');
19var flatten = require('array-flatten');
20var mime = require('send').mime;
21var etag = require('etag');
22var proxyaddr = require('proxy-addr');
23var qs = require('qs');
24var querystring = require('querystring');
25
26/**
27 * Return strong ETag for `body`.
28 *
29 * @param {String|Buffer} body
30 * @param {String} [encoding]
31 * @return {String}
32 * @api private
33 */
34
35exports.etag = createETagGenerator({ weak: false })
36
37/**
38 * Return weak ETag for `body`.
39 *
40 * @param {String|Buffer} body
41 * @param {String} [encoding]
42 * @return {String}
43 * @api private
44 */
45
46exports.wetag = createETagGenerator({ weak: true })
47
48/**
49 * Check if `path` looks absolute.
50 *
51 * @param {String} path
52 * @return {Boolean}
53 * @api private
54 */
55
56exports.isAbsolute = function(path){
57 if ('/' === path[0]) return true;
58 if (':' === path[1] && ('\\' === path[2] || '/' === path[2])) return true; // Windows device path
59 if ('\\\\' === path.substring(0, 2)) return true; // Microsoft Azure absolute path
60};
61
62/**
63 * Flatten the given `arr`.
64 *
65 * @param {Array} arr
66 * @return {Array}
67 * @api private
68 */
69
70exports.flatten = deprecate.function(flatten,
71 'utils.flatten: use array-flatten npm module instead');
72
73/**
74 * Normalize the given `type`, for example "html" becomes "text/html".
75 *
76 * @param {String} type
77 * @return {Object}
78 * @api private
79 */
80
81exports.normalizeType = function(type){
82 return ~type.indexOf('/')
83 ? acceptParams(type)
84 : { value: mime.lookup(type), params: {} };
85};
86
87/**
88 * Normalize `types`, for example "html" becomes "text/html".
89 *
90 * @param {Array} types
91 * @return {Array}
92 * @api private
93 */
94
95exports.normalizeTypes = function(types){
96 var ret = [];
97
98 for (var i = 0; i < types.length; ++i) {
99 ret.push(exports.normalizeType(types[i]));
100 }
101
102 return ret;
103};
104
105/**
106 * Generate Content-Disposition header appropriate for the filename.
107 * non-ascii filenames are urlencoded and a filename* parameter is added
108 *
109 * @param {String} filename
110 * @return {String}
111 * @api private
112 */
113
114exports.contentDisposition = deprecate.function(contentDisposition,
115 'utils.contentDisposition: use content-disposition npm module instead');
116
117/**
118 * Parse accept params `str` returning an
119 * object with `.value`, `.quality` and `.params`.
120 *
121 * @param {String} str
122 * @return {Object}
123 * @api private
124 */
125
126function acceptParams (str) {
127 var parts = str.split(/ *; */);
128 var ret = { value: parts[0], quality: 1, params: {} }
129
130 for (var i = 1; i < parts.length; ++i) {
131 var pms = parts[i].split(/ *= */);
132 if ('q' === pms[0]) {
133 ret.quality = parseFloat(pms[1]);
134 } else {
135 ret.params[pms[0]] = pms[1];
136 }
137 }
138
139 return ret;
140}
141
142/**
143 * Compile "etag" value to function.
144 *
145 * @param {Boolean|String|Function} val
146 * @return {Function}
147 * @api private
148 */
149
150exports.compileETag = function(val) {
151 var fn;
152
153 if (typeof val === 'function') {
154 return val;
155 }
156
157 switch (val) {
158 case true:
159 case 'weak':
160 fn = exports.wetag;
161 break;
162 case false:
163 break;
164 case 'strong':
165 fn = exports.etag;
166 break;
167 default:
168 throw new TypeError('unknown value for etag function: ' + val);
169 }
170
171 return fn;
172}
173
174/**
175 * Compile "query parser" value to function.
176 *
177 * @param {String|Function} val
178 * @return {Function}
179 * @api private
180 */
181
182exports.compileQueryParser = function compileQueryParser(val) {
183 var fn;
184
185 if (typeof val === 'function') {
186 return val;
187 }
188
189 switch (val) {
190 case true:
191 case 'simple':
192 fn = querystring.parse;
193 break;
194 case false:
195 fn = newObject;
196 break;
197 case 'extended':
198 fn = parseExtendedQueryString;
199 break;
200 default:
201 throw new TypeError('unknown value for query parser function: ' + val);
202 }
203
204 return fn;
205}
206
207/**
208 * Compile "proxy trust" value to function.
209 *
210 * @param {Boolean|String|Number|Array|Function} val
211 * @return {Function}
212 * @api private
213 */
214
215exports.compileTrust = function(val) {
216 if (typeof val === 'function') return val;
217
218 if (val === true) {
219 // Support plain true/false
220 return function(){ return true };
221 }
222
223 if (typeof val === 'number') {
224 // Support trusting hop count
225 return function(a, i){ return i < val };
226 }
227
228 if (typeof val === 'string') {
229 // Support comma-separated values
230 val = val.split(',')
231 .map(function (v) { return v.trim() })
232 }
233
234 return proxyaddr.compile(val || []);
235}
236
237/**
238 * Set the charset in a given Content-Type string.
239 *
240 * @param {String} type
241 * @param {String} charset
242 * @return {String}
243 * @api private
244 */
245
246exports.setCharset = function setCharset(type, charset) {
247 if (!type || !charset) {
248 return type;
249 }
250
251 // parse type
252 var parsed = contentType.parse(type);
253
254 // set charset
255 parsed.parameters.charset = charset;
256
257 // format type
258 return contentType.format(parsed);
259};
260
261/**
262 * Create an ETag generator function, generating ETags with
263 * the given options.
264 *
265 * @param {object} options
266 * @return {function}
267 * @private
268 */
269
270function createETagGenerator (options) {
271 return function generateETag (body, encoding) {
272 var buf = !Buffer.isBuffer(body)
273 ? Buffer.from(body, encoding)
274 : body
275
276 return etag(buf, options)
277 }
278}
279
280/**
281 * Parse an extended query string with qs.
282 *
283 * @param {String} str
284 * @return {Object}
285 * @private
286 */
287
288function parseExtendedQueryString(str) {
289 return qs.parse(str, {
290 allowPrototypes: true,
291 arrayLimit: 1000
292 });
293}
294
295/**
296 * Return new empty object.
297 *
298 * @return {Object}
299 * @api private
300 */
301
302function newObject() {
303 return {};
304}
Note: See TracBrowser for help on using the repository browser.