utils.js
This file
Path
lib/utils.js
293 lines · 5.5 KiB · JavaScript
Last changed by
Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
History of this fileTake it
The walk stopped after 200 commits: lines older than 88cffad 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: send@0.7.0Douglas Christopher Wilsonbefore adoption88cffad
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: send@0.7.0Douglas Christopher Wilsonbefore adoption88cffad
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');deps: send@0.7.0Douglas Christopher Wilsonbefore adoption88cffad
lines 16–17why, one step back16var mime = require('send').mime;17var basename = require('path').basename;Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
line 18why, one step back18var etag = require('etag');deps: send@0.7.0Douglas Christopher Wilsonbefore adoption88cffad
lines 19–31why, one step back19var proxyaddr = require('proxy-addr');20var qs = require('qs');21var querystring = require('querystring');2223/**24 * Return strong ETag for `body`.25 *26 * @param {String|Buffer} body27 * @param {String} [encoding]28 * @return {String}29 * @api private30 */31Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
lines 32–34why, one step back32exports.etag = function (body, encoding) {33 var buf = !Buffer.isBuffer(body)34 ? new Buffer(body, encoding)Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 35why, one step back35 : body;deps: send@0.7.0Douglas Christopher Wilsonbefore adoption88cffad
line 36why, one step back36Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 37why, one step back37 return etag(buf, {weak: false});deps: send@0.7.0Douglas Christopher Wilsonbefore adoption88cffad
lines 38–49why, one step back38};3940/**41 * Return weak ETag for `body`.42 *43 * @param {String|Buffer} body44 * @param {String} [encoding]45 * @return {String}46 * @api private47 */4849exports.wetag = function wetag(body, encoding){Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
lines 50–51why, one step back50 var buf = !Buffer.isBuffer(body)51 ? new Buffer(body, encoding)Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 52why, one step back52 : body;deps: send@0.7.0Douglas Christopher Wilsonbefore adoption88cffad
line 53why, one step back53Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 54why, one step back54 return etag(buf, {weak: true});deps: send@0.7.0Douglas Christopher Wilsonbefore adoption88cffad
lines 55–132why, one step back55};5657/**58 * Check if `path` looks absolute.59 *60 * @param {String} path61 * @return {Boolean}62 * @api private63 */6465exports.isAbsolute = function(path){66 if ('/' == path[0]) return true;67 if (':' == path[1] && '\\' == path[2]) return true;68 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute path69};7071/**72 * Flatten the given `arr`.73 *74 * @param {Array} arr75 * @return {Array}76 * @api private77 */7879exports.flatten = function(arr, ret){80 ret = ret || [];81 var len = arr.length;82 for (var i = 0; i < len; ++i) {83 if (Array.isArray(arr[i])) {84 exports.flatten(arr[i], ret);85 } else {86 ret.push(arr[i]);87 }88 }89 return ret;90};9192/**93 * Normalize the given `type`, for example "html" becomes "text/html".94 *95 * @param {String} type96 * @return {Object}97 * @api private98 */99100exports.normalizeType = function(type){101 return ~type.indexOf('/')102 ? acceptParams(type)103 : { value: mime.lookup(type), params: {} };104};105106/**107 * Normalize `types`, for example "html" becomes "text/html".108 *109 * @param {Array} types110 * @return {Array}111 * @api private112 */113114exports.normalizeTypes = function(types){115 var ret = [];116117 for (var i = 0; i < types.length; ++i) {118 ret.push(exports.normalizeType(types[i]));119 }120121 return ret;122};123124/**125 * Generate Content-Disposition header appropriate for the filename.126 * non-ascii filenames are urlencoded and a filename* parameter is added127 *128 * @param {String} filename129 * @return {String}130 * @api private131 */132Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
lines 133–134why, one step back133exports.contentDisposition = deprecate.function(contentDisposition,134 'utils.contentDisposition: use content-disposition npm module instead');deps: send@0.7.0Douglas Christopher Wilsonbefore adoption88cffad
lines 135–268why, one step back135136/**137 * Parse accept params `str` returning an138 * object with `.value`, `.quality` and `.params`.139 * also includes `.originalIndex` for stable sorting140 *141 * @param {String} str142 * @return {Object}143 * @api private144 */145146function acceptParams(str, index) {147 var parts = str.split(/ *; */);148 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };149150 for (var i = 1; i < parts.length; ++i) {151 var pms = parts[i].split(/ *= */);152 if ('q' == pms[0]) {153 ret.quality = parseFloat(pms[1]);154 } else {155 ret.params[pms[0]] = pms[1];156 }157 }158159 return ret;160}161162/**163 * Compile "etag" value to function.164 *165 * @param {Boolean|String|Function} val166 * @return {Function}167 * @api private168 */169170exports.compileETag = function(val) {171 var fn;172173 if (typeof val === 'function') {174 return val;175 }176177 switch (val) {178 case true:179 fn = exports.wetag;180 break;181 case false:182 break;183 case 'strong':184 fn = exports.etag;185 break;186 case 'weak':187 fn = exports.wetag;188 break;189 default:190 throw new TypeError('unknown value for etag function: ' + val);191 }192193 return fn;194}195196/**197 * Compile "query parser" value to function.198 *199 * @param {String|Function} val200 * @return {Function}201 * @api private202 */203204exports.compileQueryParser = function compileQueryParser(val) {205 var fn;206207 if (typeof val === 'function') {208 return val;209 }210211 switch (val) {212 case true:213 fn = querystring.parse;214 break;215 case false:216 fn = newObject;217 break;218 case 'extended':219 fn = qs.parse;220 break;221 case 'simple':222 fn = querystring.parse;223 break;224 default:225 throw new TypeError('unknown value for query parser function: ' + val);226 }227228 return fn;229}230231/**232 * Compile "proxy trust" value to function.233 *234 * @param {Boolean|String|Number|Array|Function} val235 * @return {Function}236 * @api private237 */238239exports.compileTrust = function(val) {240 if (typeof val === 'function') return val;241242 if (val === true) {243 // Support plain true/false244 return function(){ return true };245 }246247 if (typeof val === 'number') {248 // Support trusting hop count249 return function(a, i){ return i < val };250 }251252 if (typeof val === 'string') {253 // Support comma-separated values254 val = val.split(/ *, */);255 }256257 return proxyaddr.compile(val || []);258}259260/**261 * Set the charset in a given Content-Type string.262 *263 * @param {String} type264 * @param {String} charset265 * @return {String}266 * @api private267 */268Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 269–272why, one step back269exports.setCharset = function setCharset(type, charset) {270 if (!type || !charset) {271 return type;272 }deps: send@0.7.0Douglas Christopher Wilsonbefore adoption88cffad
lines 273–274why, one step back273274 // parse typeMerge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 275why, one step back275 var parsed = contentType.parse(type);deps: send@0.7.0Douglas Christopher Wilsonbefore adoption88cffad
lines 276–280why, one step back276277 // set charset278 parsed.parameters.charset = charset;279280 // format typeMerge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 281why, one step back281 return contentType.format(parsed);deps: send@0.7.0Douglas Christopher Wilsonbefore adoption88cffad
lines 282–284why, one step back282};283284/**Fix typo in jsdoc descriptionVladimir Grinenkobefore adoption72d35b6
line 285why, one step back285 * Return new empty object.deps: send@0.7.0Douglas Christopher Wilsonbefore adoption88cffad
lines 286–293why, one step back286 *287 * @return {Object}288 * @api private289 */290291function newObject() {292 return {};293}