utils.js
This file
Path
lib/utils.js
284 lines · 5.4 KiB · JavaScript
Last changed by
Use array-flatten module for flattening arraysDeepak Kapoorbefore adoptionf7e94a3
History of this fileTake it
The walk stopped after 200 commits: lines older than ad3ca25 point at it.
Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 1–7why, one step back1/*!2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2014-2015 Douglas Christopher Wilson5 * MIT Licensed6 */7deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25
lines 8–9why, one step back8/**9 * Module dependencies.Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 10why, one step back10 * @api privatedeps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25
lines 11–12why, one step back11 */12Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
line 13why, one step back13var contentDisposition = require('content-disposition');Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 14why, one step back14var contentType = require('content-type');Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
line 15why, one step back15var deprecate = require('depd')('express');Use array-flatten module for flattening arraysDeepak Kapoorbefore adoptionf7e94a3
line 16why, one step back16var flatten = require('array-flatten');deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25
lines 17–18why, one step back17var mime = require('send').mime;18var basename = require('path').basename;Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
line 19why, one step back19var etag = require('etag');deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25
lines 20–32why, one step back20var proxyaddr = require('proxy-addr');21var qs = require('qs');22var querystring = require('querystring');2324/**25 * Return strong ETag for `body`.26 *27 * @param {String|Buffer} body28 * @param {String} [encoding]29 * @return {String}30 * @api private31 */32Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
lines 33–35why, one step back33exports.etag = function (body, encoding) {34 var buf = !Buffer.isBuffer(body)35 ? new Buffer(body, encoding)Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 36why, one step back36 : body;deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25
line 37why, one step back37Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 38why, one step back38 return etag(buf, {weak: false});deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25
lines 39–50why, one step back39};4041/**42 * Return weak ETag for `body`.43 *44 * @param {String|Buffer} body45 * @param {String} [encoding]46 * @return {String}47 * @api private48 */4950exports.wetag = function wetag(body, encoding){Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
lines 51–52why, one step back51 var buf = !Buffer.isBuffer(body)52 ? new Buffer(body, encoding)Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 53why, one step back53 : body;deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25
line 54why, one step back54Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 55why, one step back55 return etag(buf, {weak: true});deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25
lines 56–79why, one step back56};5758/**59 * Check if `path` looks absolute.60 *61 * @param {String} path62 * @return {Boolean}63 * @api private64 */6566exports.isAbsolute = function(path){67 if ('/' == path[0]) return true;68 if (':' == path[1] && '\\' == path[2]) return true;69 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute path70};7172/**73 * Flatten the given `arr`.74 *75 * @param {Array} arr76 * @return {Array}77 * @api private78 */79Use array-flatten module for flattening arraysDeepak Kapoorbefore adoptionf7e94a3
lines 80–81why, one step back80exports.flatten = deprecate.function(flatten,81 'utils.flatten: use array-flatten npm module instead');deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25
lines 82–123why, one step back8283/**84 * Normalize the given `type`, for example "html" becomes "text/html".85 *86 * @param {String} type87 * @return {Object}88 * @api private89 */9091exports.normalizeType = function(type){92 return ~type.indexOf('/')93 ? acceptParams(type)94 : { value: mime.lookup(type), params: {} };95};9697/**98 * Normalize `types`, for example "html" becomes "text/html".99 *100 * @param {Array} types101 * @return {Array}102 * @api private103 */104105exports.normalizeTypes = function(types){106 var ret = [];107108 for (var i = 0; i < types.length; ++i) {109 ret.push(exports.normalizeType(types[i]));110 }111112 return ret;113};114115/**116 * Generate Content-Disposition header appropriate for the filename.117 * non-ascii filenames are urlencoded and a filename* parameter is added118 *119 * @param {String} filename120 * @return {String}121 * @api private122 */123Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
lines 124–125why, one step back124exports.contentDisposition = deprecate.function(contentDisposition,125 'utils.contentDisposition: use content-disposition npm module instead');deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25
lines 126–259why, one step back126127/**128 * Parse accept params `str` returning an129 * object with `.value`, `.quality` and `.params`.130 * also includes `.originalIndex` for stable sorting131 *132 * @param {String} str133 * @return {Object}134 * @api private135 */136137function acceptParams(str, index) {138 var parts = str.split(/ *; */);139 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };140141 for (var i = 1; i < parts.length; ++i) {142 var pms = parts[i].split(/ *= */);143 if ('q' == pms[0]) {144 ret.quality = parseFloat(pms[1]);145 } else {146 ret.params[pms[0]] = pms[1];147 }148 }149150 return ret;151}152153/**154 * Compile "etag" value to function.155 *156 * @param {Boolean|String|Function} val157 * @return {Function}158 * @api private159 */160161exports.compileETag = function(val) {162 var fn;163164 if (typeof val === 'function') {165 return val;166 }167168 switch (val) {169 case true:170 fn = exports.wetag;171 break;172 case false:173 break;174 case 'strong':175 fn = exports.etag;176 break;177 case 'weak':178 fn = exports.wetag;179 break;180 default:181 throw new TypeError('unknown value for etag function: ' + val);182 }183184 return fn;185}186187/**188 * Compile "query parser" value to function.189 *190 * @param {String|Function} val191 * @return {Function}192 * @api private193 */194195exports.compileQueryParser = function compileQueryParser(val) {196 var fn;197198 if (typeof val === 'function') {199 return val;200 }201202 switch (val) {203 case true:204 fn = querystring.parse;205 break;206 case false:207 fn = newObject;208 break;209 case 'extended':210 fn = qs.parse;211 break;212 case 'simple':213 fn = querystring.parse;214 break;215 default:216 throw new TypeError('unknown value for query parser function: ' + val);217 }218219 return fn;220}221222/**223 * Compile "proxy trust" value to function.224 *225 * @param {Boolean|String|Number|Array|Function} val226 * @return {Function}227 * @api private228 */229230exports.compileTrust = function(val) {231 if (typeof val === 'function') return val;232233 if (val === true) {234 // Support plain true/false235 return function(){ return true };236 }237238 if (typeof val === 'number') {239 // Support trusting hop count240 return function(a, i){ return i < val };241 }242243 if (typeof val === 'string') {244 // Support comma-separated values245 val = val.split(/ *, */);246 }247248 return proxyaddr.compile(val || []);249}250251/**252 * Set the charset in a given Content-Type string.253 *254 * @param {String} type255 * @param {String} charset256 * @return {String}257 * @api private258 */259Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 260–263why, one step back260exports.setCharset = function setCharset(type, charset) {261 if (!type || !charset) {262 return type;263 }deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25
lines 264–265why, one step back264265 // parse typeMerge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 266why, one step back266 var parsed = contentType.parse(type);deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25
lines 267–271why, one step back267268 // set charset269 parsed.parameters.charset = charset;270271 // format typeMerge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 272why, one step back272 return contentType.format(parsed);deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25
lines 273–275why, one step back273};274275/**Fix typo in jsdoc descriptionVladimir Grinenkobefore adoption72d35b6
line 276why, one step back276 * Return new empty object.deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25
lines 277–284why, one step back277 *278 * @return {Object}279 * @api private280 */281282function newObject() {283 return {};284}