Skip to content
Myr

utils.js

This file

Path

lib/utils.js

303 lines · 5.8 KiB · JavaScript

Last changed by

pref: remove unnecessary regexp for trust proxyDouglas Christopher Wilsonbefore adoption6fbc269

History of this file

Take it

The walk stopped after 200 commits: lines older than 76bf96e point at it.

deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e

lines 1–159why, one step back
1/*!
2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2014-2015 Douglas Christopher Wilson5 * MIT Licensed6 */78'use strict';910/**11 * Module dependencies.12 * @api private13 */1415var Buffer = require('safe-buffer').Buffer16var 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');2526/**27 * Return strong ETag for `body`.28 *29 * @param {String|Buffer} body30 * @param {String} [encoding]31 * @return {String}32 * @api private33 */3435exports.etag = createETagGenerator({ weak: false })3637/**38 * Return weak ETag for `body`.39 *40 * @param {String|Buffer} body41 * @param {String} [encoding]42 * @return {String}43 * @api private44 */4546exports.wetag = createETagGenerator({ weak: true })4748/**49 * Check if `path` looks absolute.50 *51 * @param {String} path52 * @return {Boolean}53 * @api private54 */5556exports.isAbsolute = function(path){57 if ('/' === path[0]) return true;58 if (':' === path[1] && ('\\' === path[2] || '/' === path[2])) return true; // Windows device path59 if ('\\\\' === path.substring(0, 2)) return true; // Microsoft Azure absolute path60};6162/**63 * Flatten the given `arr`.64 *65 * @param {Array} arr66 * @return {Array}67 * @api private68 */6970exports.flatten = deprecate.function(flatten,71 'utils.flatten: use array-flatten npm module instead');7273/**74 * Normalize the given `type`, for example "html" becomes "text/html".75 *76 * @param {String} type77 * @return {Object}78 * @api private79 */8081exports.normalizeType = function(type){82 return ~type.indexOf('/')83 ? acceptParams(type)84 : { value: mime.lookup(type), params: {} };85};8687/**88 * Normalize `types`, for example "html" becomes "text/html".89 *90 * @param {Array} types91 * @return {Array}92 * @api private93 */9495exports.normalizeTypes = function(types){96 var ret = [];9798 for (var i = 0; i < types.length; ++i) {99 ret.push(exports.normalizeType(types[i]));100 }101102 return ret;103};104105/**106 * Generate Content-Disposition header appropriate for the filename.107 * non-ascii filenames are urlencoded and a filename* parameter is added108 *109 * @param {String} filename110 * @return {String}111 * @api private112 */113114exports.contentDisposition = deprecate.function(contentDisposition,115 'utils.contentDisposition: use content-disposition npm module instead');116117/**118 * Parse accept params `str` returning an119 * object with `.value`, `.quality` and `.params`.120 * also includes `.originalIndex` for stable sorting121 *122 * @param {String} str123 * @return {Object}124 * @api private125 */126127function acceptParams(str, index) {128 var parts = str.split(/ *; */);129 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };130131 for (var i = 1; i < parts.length; ++i) {132 var pms = parts[i].split(/ *= */);133 if ('q' === pms[0]) {134 ret.quality = parseFloat(pms[1]);135 } else {136 ret.params[pms[0]] = pms[1];137 }138 }139140 return ret;141}142143/**144 * Compile "etag" value to function.145 *146 * @param {Boolean|String|Function} val147 * @return {Function}148 * @api private149 */150151exports.compileETag = function(val) {152 var fn;153154 if (typeof val === 'function') {155 return val;156 }157158 switch (val) {159 case true:

lint: coalesce duplicate switch statementsAbderrahmenlabefore adoption0fbbc29

line 160why, one step back
160 case 'weak':

deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e

lines 161–191why, one step back
161 fn = exports.wetag;
162 break;163 case false:164 break;165 case 'strong':166 fn = exports.etag;167 break;168 default:169 throw new TypeError('unknown value for etag function: ' + val);170 }171172 return fn;173}174175/**176 * Compile "query parser" value to function.177 *178 * @param {String|Function} val179 * @return {Function}180 * @api private181 */182183exports.compileQueryParser = function compileQueryParser(val) {184 var fn;185186 if (typeof val === 'function') {187 return val;188 }189190 switch (val) {191 case true:

lint: coalesce duplicate switch statementsAbderrahmenlabefore adoption0fbbc29

line 192why, one step back
192 case 'simple':

deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e

lines 193–230why, one step back
193 fn = querystring.parse;
194 break;195 case false:196 fn = newObject;197 break;198 case 'extended':199 fn = parseExtendedQueryString;200 break;201 default:202 throw new TypeError('unknown value for query parser function: ' + val);203 }204205 return fn;206}207208/**209 * Compile "proxy trust" value to function.210 *211 * @param {Boolean|String|Number|Array|Function} val212 * @return {Function}213 * @api private214 */215216exports.compileTrust = function(val) {217 if (typeof val === 'function') return val;218219 if (val === true) {220 // Support plain true/false221 return function(){ return true };222 }223224 if (typeof val === 'number') {225 // Support trusting hop count226 return function(a, i){ return i < val };227 }228229 if (typeof val === 'string') {230 // Support comma-separated values

pref: remove unnecessary regexp for trust proxyDouglas Christopher Wilsonbefore adoption6fbc269

lines 231–232why, one step back
231 val = val.split(',')
232 .map(function (v) { return v.trim() })

deps: finalhandler@1.1.1Douglas Christopher Wilsonbefore adoption76bf96e

lines 233–303why, one step back
233 }
234235 return proxyaddr.compile(val || []);236}237238/**239 * Set the charset in a given Content-Type string.240 *241 * @param {String} type242 * @param {String} charset243 * @return {String}244 * @api private245 */246247exports.setCharset = function setCharset(type, charset) {248 if (!type || !charset) {249 return type;250 }251252 // parse type253 var parsed = contentType.parse(type);254255 // set charset256 parsed.parameters.charset = charset;257258 // format type259 return contentType.format(parsed);260};261262/**263 * Create an ETag generator function, generating ETags with264 * the given options.265 *266 * @param {object} options267 * @return {function}268 * @private269 */270271function createETagGenerator (options) {272 return function generateETag (body, encoding) {273 var buf = !Buffer.isBuffer(body)274 ? Buffer.from(body, encoding)275 : body276277 return etag(buf, options)278 }279}280281/**282 * Parse an extended query string with qs.283 *284 * @return {Object}285 * @private286 */287288function parseExtendedQueryString(str) {289 return qs.parse(str, {290 allowPrototypes: true291 });292}293294/**295 * Return new empty object.296 *297 * @return {Object}298 * @api private299 */300301function newObject() {302 return {};303}