Skip to content
Myr

utils.js

This file

Path

lib/utils.js

303 lines · 5.7 KiB · JavaScript

Last changed by

docs: add missing JSDoc param for parseExtendedQueryStringRakesh Bishtbefore adoption506fbd6

History of this file

Take it

The walk stopped after 200 commits: lines older than 55831bb point at it.

build: Node.js@10.18Douglas Christopher Wilsonbefore adoption55831bb

lines 1–125why, 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 *121 * @param {String} str122 * @return {Object}123 * @api private124 */125

lint: remove unused parameter from internal functionRakesh Bishtbefore adoptiona1efd9d

line 126why, one step back
126function acceptParams (str) {

build: Node.js@10.18Douglas Christopher Wilsonbefore adoption55831bb

line 127why, one step back
127 var parts = str.split(/ *; */);

lint: remove unused parameter from internal functionRakesh Bishtbefore adoptiona1efd9d

line 128why, one step back
128 var ret = { value: parts[0], quality: 1, params: {} }

build: Node.js@10.18Douglas Christopher Wilsonbefore adoption55831bb

lines 129–158why, one step back
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 }138139 return ret;140}141142/**143 * Compile "etag" value to function.144 *145 * @param {Boolean|String|Function} val146 * @return {Function}147 * @api private148 */149150exports.compileETag = function(val) {151 var fn;152153 if (typeof val === 'function') {154 return val;155 }156157 switch (val) {158 case true:

lint: coalesce duplicate switch statementsAbderrahmenlabefore adoption0fbbc29

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

build: Node.js@10.18Douglas Christopher Wilsonbefore adoption55831bb

lines 160–190why, one step back
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 }170171 return fn;172}173174/**175 * Compile "query parser" value to function.176 *177 * @param {String|Function} val178 * @return {Function}179 * @api private180 */181182exports.compileQueryParser = function compileQueryParser(val) {183 var fn;184185 if (typeof val === 'function') {186 return val;187 }188189 switch (val) {190 case true:

lint: coalesce duplicate switch statementsAbderrahmenlabefore adoption0fbbc29

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

build: Node.js@10.18Douglas Christopher Wilsonbefore adoption55831bb

lines 192–229why, one step back
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 }203204 return fn;205}206207/**208 * Compile "proxy trust" value to function.209 *210 * @param {Boolean|String|Number|Array|Function} val211 * @return {Function}212 * @api private213 */214215exports.compileTrust = function(val) {216 if (typeof val === 'function') return val;217218 if (val === true) {219 // Support plain true/false220 return function(){ return true };221 }222223 if (typeof val === 'number') {224 // Support trusting hop count225 return function(a, i){ return i < val };226 }227228 if (typeof val === 'string') {229 // Support comma-separated values

pref: remove unnecessary regexp for trust proxyDouglas Christopher Wilsonbefore adoption6fbc269

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

build: Node.js@10.18Douglas Christopher Wilsonbefore adoption55831bb

lines 232–282why, one step back
232 }
233234 return proxyaddr.compile(val || []);235}236237/**238 * Set the charset in a given Content-Type string.239 *240 * @param {String} type241 * @param {String} charset242 * @return {String}243 * @api private244 */245246exports.setCharset = function setCharset(type, charset) {247 if (!type || !charset) {248 return type;249 }250251 // parse type252 var parsed = contentType.parse(type);253254 // set charset255 parsed.parameters.charset = charset;256257 // format type258 return contentType.format(parsed);259};260261/**262 * Create an ETag generator function, generating ETags with263 * the given options.264 *265 * @param {object} options266 * @return {function}267 * @private268 */269270function createETagGenerator (options) {271 return function generateETag (body, encoding) {272 var buf = !Buffer.isBuffer(body)273 ? Buffer.from(body, encoding)274 : body275276 return etag(buf, options)277 }278}279280/**281 * Parse an extended query string with qs.282 *

docs: add missing JSDoc param for parseExtendedQueryStringRakesh Bishtbefore adoption506fbd6

line 283why, one step back
283 * @param {String} str

build: Node.js@10.18Douglas Christopher Wilsonbefore adoption55831bb

lines 284–303why, one step back
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}