utils.js
This file
Path
lib/utils.js
300 lines · 5.7 KiB · JavaScript
Last changed by
deps: qs@4.0.0Douglas Christopher Wilsonbefore adoption7bea53b
History of this fileTake it
The walk stopped after 200 commits: lines older than 6bc7574 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 */7perf: enable strict modeDouglas Christopher Wilsonbefore adoptione71014f
lines 8–9why, one step back8'use strict';9docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 10–11why, one step back10/**11 * Module dependencies.Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 12why, one step back12 * @api privatedocs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 13–14why, one step back13 */14Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
line 15why, one step back15var contentDisposition = require('content-disposition');Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 16why, one step back16var contentType = require('content-type');Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
line 17why, one step back17var deprecate = require('depd')('express');Use array-flatten module for flattening arraysDeepak Kapoorbefore adoptionf7e94a3
line 18why, one step back18var flatten = require('array-flatten');docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 19–20why, one step back19var mime = require('send').mime;20var basename = require('path').basename;Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
line 21why, one step back21var etag = require('etag');docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 22–34why, one step back22var 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 */34Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
lines 35–37why, one step back35exports.etag = function (body, encoding) {36 var buf = !Buffer.isBuffer(body)37 ? new Buffer(body, encoding)Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 38why, one step back38 : body;docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
line 39why, one step back39Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 40why, one step back40 return etag(buf, {weak: false});docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 41–52why, one step back41};4243/**44 * Return weak ETag for `body`.45 *46 * @param {String|Buffer} body47 * @param {String} [encoding]48 * @return {String}49 * @api private50 */5152exports.wetag = function wetag(body, encoding){Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
lines 53–54why, one step back53 var buf = !Buffer.isBuffer(body)54 ? new Buffer(body, encoding)Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 55why, one step back55 : body;docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
line 56why, one step back56Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 57why, one step back57 return etag(buf, {weak: true});docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 58–81why, one step back58};5960/**61 * Check if `path` looks absolute.62 *63 * @param {String} path64 * @return {Boolean}65 * @api private66 */6768exports.isAbsolute = function(path){69 if ('/' == path[0]) return true;70 if (':' == path[1] && '\\' == path[2]) return true;71 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute path72};7374/**75 * Flatten the given `arr`.76 *77 * @param {Array} arr78 * @return {Array}79 * @api private80 */81Use array-flatten module for flattening arraysDeepak Kapoorbefore adoptionf7e94a3
lines 82–83why, one step back82exports.flatten = deprecate.function(flatten,83 'utils.flatten: use array-flatten npm module instead');docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 84–125why, one step back8485/**86 * Normalize the given `type`, for example "html" becomes "text/html".87 *88 * @param {String} type89 * @return {Object}90 * @api private91 */9293exports.normalizeType = function(type){94 return ~type.indexOf('/')95 ? acceptParams(type)96 : { value: mime.lookup(type), params: {} };97};9899/**100 * Normalize `types`, for example "html" becomes "text/html".101 *102 * @param {Array} types103 * @return {Array}104 * @api private105 */106107exports.normalizeTypes = function(types){108 var ret = [];109110 for (var i = 0; i < types.length; ++i) {111 ret.push(exports.normalizeType(types[i]));112 }113114 return ret;115};116117/**118 * Generate Content-Disposition header appropriate for the filename.119 * non-ascii filenames are urlencoded and a filename* parameter is added120 *121 * @param {String} filename122 * @return {String}123 * @api private124 */125Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
lines 126–127why, one step back126exports.contentDisposition = deprecate.function(contentDisposition,127 'utils.contentDisposition: use content-disposition npm module instead');docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 128–211why, one step back128129/**130 * Parse accept params `str` returning an131 * object with `.value`, `.quality` and `.params`.132 * also includes `.originalIndex` for stable sorting133 *134 * @param {String} str135 * @return {Object}136 * @api private137 */138139function acceptParams(str, index) {140 var parts = str.split(/ *; */);141 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };142143 for (var i = 1; i < parts.length; ++i) {144 var pms = parts[i].split(/ *= */);145 if ('q' == pms[0]) {146 ret.quality = parseFloat(pms[1]);147 } else {148 ret.params[pms[0]] = pms[1];149 }150 }151152 return ret;153}154155/**156 * Compile "etag" value to function.157 *158 * @param {Boolean|String|Function} val159 * @return {Function}160 * @api private161 */162163exports.compileETag = function(val) {164 var fn;165166 if (typeof val === 'function') {167 return val;168 }169170 switch (val) {171 case true:172 fn = exports.wetag;173 break;174 case false:175 break;176 case 'strong':177 fn = exports.etag;178 break;179 case 'weak':180 fn = exports.wetag;181 break;182 default:183 throw new TypeError('unknown value for etag function: ' + val);184 }185186 return fn;187}188189/**190 * Compile "query parser" value to function.191 *192 * @param {String|Function} val193 * @return {Function}194 * @api private195 */196197exports.compileQueryParser = function compileQueryParser(val) {198 var fn;199200 if (typeof val === 'function') {201 return val;202 }203204 switch (val) {205 case true:206 fn = querystring.parse;207 break;208 case false:209 fn = newObject;210 break;211 case 'extended':deps: qs@4.0.0Douglas Christopher Wilsonbefore adoption7bea53b
line 212why, one step back212 fn = parseExtendedQueryString;docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 213–261why, one step back213 break;214 case 'simple':215 fn = querystring.parse;216 break;217 default:218 throw new TypeError('unknown value for query parser function: ' + val);219 }220221 return fn;222}223224/**225 * Compile "proxy trust" value to function.226 *227 * @param {Boolean|String|Number|Array|Function} val228 * @return {Function}229 * @api private230 */231232exports.compileTrust = function(val) {233 if (typeof val === 'function') return val;234235 if (val === true) {236 // Support plain true/false237 return function(){ return true };238 }239240 if (typeof val === 'number') {241 // Support trusting hop count242 return function(a, i){ return i < val };243 }244245 if (typeof val === 'string') {246 // Support comma-separated values247 val = val.split(/ *, */);248 }249250 return proxyaddr.compile(val || []);251}252253/**254 * Set the charset in a given Content-Type string.255 *256 * @param {String} type257 * @param {String} charset258 * @return {String}259 * @api private260 */261Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
lines 262–265why, one step back262exports.setCharset = function setCharset(type, charset) {263 if (!type || !charset) {264 return type;265 }docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 266–267why, one step back266267 // parse typeMerge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 268why, one step back268 var parsed = contentType.parse(type);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 269–273why, one step back269270 // set charset271 parsed.parameters.charset = charset;272273 // format typeMerge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024
line 274why, one step back274 return contentType.format(parsed);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
line 275why, one step back275};deps: qs@4.0.0Douglas Christopher Wilsonbefore adoption7bea53b
lines 276–289why, one step back276277/**278 * Parse an extended query string with qs.279 *280 * @return {Object}281 * @private282 */283284function parseExtendedQueryString(str) {285 return qs.parse(str, {286 allowDots: false,287 allowPrototypes: true288 });289}docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 290–291why, one step back290291/**Fix typo in jsdoc descriptionVladimir Grinenkobefore adoption72d35b6
line 292why, one step back292 * Return new empty object.docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 293–300why, one step back293 *294 * @return {Object}295 * @api private296 */297298function newObject() {299 return {};300}