Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 94f10c2 point at it.

Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024

lines 1–7why, one step back
1/*!
2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2014-2015 Douglas Christopher Wilson5 * MIT Licensed6 */7

perf: enable strict modeDouglas Christopher Wilsonbefore adoptione71014f

lines 8–9why, one step back
8'use strict';
9

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 10–11why, one step back
10/**
11 * Module dependencies.

Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024

line 12why, one step back
12 * @api private

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 13–14why, one step back
13 */
14

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

line 15why, one step back
15var contentDisposition = require('content-disposition');

Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024

line 16why, one step back
16var contentType = require('content-type');

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

line 17why, one step back
17var deprecate = require('depd')('express');

Use array-flatten module for flattening arraysDeepak Kapoorbefore adoptionf7e94a3

line 18why, one step back
18var flatten = require('array-flatten');

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 19–37why, one step back
19var mime = require('send').mime;
20var basename = require('path').basename;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 = 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 back
38 : body;

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

line 39why, one step back
39

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 40why, one step back
40 return etag(buf, {weak: false});

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 41–54why, one step back
41};
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){53 var buf = !Buffer.isBuffer(body)54 ? new Buffer(body, encoding)

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 55why, one step back
55 : body;

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

line 56why, one step back
56

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 57why, one step back
57 return etag(buf, {weak: true});

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 58–81why, one step back
58};
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 */81

Use array-flatten module for flattening arraysDeepak Kapoorbefore adoptionf7e94a3

lines 82–83why, one step back
82exports.flatten = deprecate.function(flatten,
83 'utils.flatten: use array-flatten npm module instead');

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 84–125why, one step back
84
85/**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 */125

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

lines 126–127why, one step back
126exports.contentDisposition = deprecate.function(contentDisposition,
127 'utils.contentDisposition: use content-disposition npm module instead');

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 128–211why, one step back
128
129/**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 back
212 fn = parseExtendedQueryString;

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 213–261why, one step back
213 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 */261

Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024

lines 262–265why, one step back
262exports.setCharset = function setCharset(type, charset) {
263 if (!type || !charset) {264 return type;265 }

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 266–267why, one step back
266
267 // parse type

Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024

line 268why, one step back
268 var parsed = contentType.parse(type);

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 269–273why, one step back
269
270 // set charset271 parsed.parameters.charset = charset;272273 // format type

Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024

line 274why, one step back
274 return contentType.format(parsed);

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

line 275why, one step back
275};

deps: qs@4.0.0Douglas Christopher Wilsonbefore adoption7bea53b

lines 276–289why, one step back
276
277/**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}

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 290–291why, one step back
290
291/**

Fix typo in jsdoc descriptionVladimir Grinenkobefore adoption72d35b6

line 292why, one step back
292 * Return new empty object.

Fix using same param name in array of pathsDouglas Christopher Wilsonbefore adoption94f10c2

lines 293–300why, one step back
293 *
294 * @return {Object}295 * @api private296 */297298function newObject() {299 return {};300}