Skip to content
Myr

utils.js

This file

Path

lib/utils.js

298 lines · 5.6 KiB · JavaScript

Last changed by

lint: remove all unused varaiblesDouglas Christopher Wilsonbefore adoptione006622

History of this file

Take it

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

examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897

lines 1–67why, 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 contentDisposition = require('content-disposition');16var contentType = require('content-type');17var deprecate = require('depd')('express');18var flatten = require('array-flatten');19var mime = require('send').mime;20var etag = require('etag');21var proxyaddr = require('proxy-addr');22var qs = require('qs');23var querystring = require('querystring');2425/**26 * Return strong ETag for `body`.27 *28 * @param {String|Buffer} body29 * @param {String} [encoding]30 * @return {String}31 * @api private32 */3334exports.etag = function (body, encoding) {35 var buf = !Buffer.isBuffer(body)36 ? new Buffer(body, encoding)37 : body;3839 return etag(buf, {weak: false});40};4142/**43 * Return weak ETag for `body`.44 *45 * @param {String|Buffer} body46 * @param {String} [encoding]47 * @return {String}48 * @api private49 */5051exports.wetag = function wetag(body, encoding){52 var buf = !Buffer.isBuffer(body)53 ? new Buffer(body, encoding)54 : body;5556 return etag(buf, {weak: true});57};5859/**60 * Check if `path` looks absolute.61 *62 * @param {String} path63 * @return {Boolean}64 * @api private65 */6667exports.isAbsolute = function(path){

perf: use strict equality when possibleÁngel Sanzbefore adoptionb69b760

line 68why, one step back
68 if ('/' === path[0]) return true;

Fix Windows absolute path check using forward slasheshuaoguobefore adoption2e1284b

line 69why, one step back
69 if (':' === path[1] && ('\\' === path[2] || '/' === path[2])) return true; // Windows device path

perf: use strict equality when possibleÁngel Sanzbefore adoptionb69b760

line 70why, one step back
70 if ('\\\\' === path.substring(0, 2)) return true; // Microsoft Azure absolute path

examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897

lines 71–143why, one step back
71};
7273/**74 * Flatten the given `arr`.75 *76 * @param {Array} arr77 * @return {Array}78 * @api private79 */8081exports.flatten = deprecate.function(flatten,82 'utils.flatten: use array-flatten npm module instead');8384/**85 * Normalize the given `type`, for example "html" becomes "text/html".86 *87 * @param {String} type88 * @return {Object}89 * @api private90 */9192exports.normalizeType = function(type){93 return ~type.indexOf('/')94 ? acceptParams(type)95 : { value: mime.lookup(type), params: {} };96};9798/**99 * Normalize `types`, for example "html" becomes "text/html".100 *101 * @param {Array} types102 * @return {Array}103 * @api private104 */105106exports.normalizeTypes = function(types){107 var ret = [];108109 for (var i = 0; i < types.length; ++i) {110 ret.push(exports.normalizeType(types[i]));111 }112113 return ret;114};115116/**117 * Generate Content-Disposition header appropriate for the filename.118 * non-ascii filenames are urlencoded and a filename* parameter is added119 *120 * @param {String} filename121 * @return {String}122 * @api private123 */124125exports.contentDisposition = deprecate.function(contentDisposition,126 'utils.contentDisposition: use content-disposition npm module instead');127128/**129 * Parse accept params `str` returning an130 * object with `.value`, `.quality` and `.params`.131 * also includes `.originalIndex` for stable sorting132 *133 * @param {String} str134 * @return {Object}135 * @api private136 */137138function acceptParams(str, index) {139 var parts = str.split(/ *; */);140 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };141142 for (var i = 1; i < parts.length; ++i) {143 var pms = parts[i].split(/ *= */);

perf: use strict equality when possibleÁngel Sanzbefore adoptionb69b760

line 144why, one step back
144 if ('q' === pms[0]) {

examples: comment the usage of process.nextTickMuhammad Saqibbefore adoptiondd2b897

lines 145–298why, one step back
145 ret.quality = parseFloat(pms[1]);
146 } else {147 ret.params[pms[0]] = pms[1];148 }149 }150151 return ret;152}153154/**155 * Compile "etag" value to function.156 *157 * @param {Boolean|String|Function} val158 * @return {Function}159 * @api private160 */161162exports.compileETag = function(val) {163 var fn;164165 if (typeof val === 'function') {166 return val;167 }168169 switch (val) {170 case true:171 fn = exports.wetag;172 break;173 case false:174 break;175 case 'strong':176 fn = exports.etag;177 break;178 case 'weak':179 fn = exports.wetag;180 break;181 default:182 throw new TypeError('unknown value for etag function: ' + val);183 }184185 return fn;186}187188/**189 * Compile "query parser" value to function.190 *191 * @param {String|Function} val192 * @return {Function}193 * @api private194 */195196exports.compileQueryParser = function compileQueryParser(val) {197 var fn;198199 if (typeof val === 'function') {200 return val;201 }202203 switch (val) {204 case true:205 fn = querystring.parse;206 break;207 case false:208 fn = newObject;209 break;210 case 'extended':211 fn = parseExtendedQueryString;212 break;213 case 'simple':214 fn = querystring.parse;215 break;216 default:217 throw new TypeError('unknown value for query parser function: ' + val);218 }219220 return fn;221}222223/**224 * Compile "proxy trust" value to function.225 *226 * @param {Boolean|String|Number|Array|Function} val227 * @return {Function}228 * @api private229 */230231exports.compileTrust = function(val) {232 if (typeof val === 'function') return val;233234 if (val === true) {235 // Support plain true/false236 return function(){ return true };237 }238239 if (typeof val === 'number') {240 // Support trusting hop count241 return function(a, i){ return i < val };242 }243244 if (typeof val === 'string') {245 // Support comma-separated values246 val = val.split(/ *, */);247 }248249 return proxyaddr.compile(val || []);250}251252/**253 * Set the charset in a given Content-Type string.254 *255 * @param {String} type256 * @param {String} charset257 * @return {String}258 * @api private259 */260261exports.setCharset = function setCharset(type, charset) {262 if (!type || !charset) {263 return type;264 }265266 // parse type267 var parsed = contentType.parse(type);268269 // set charset270 parsed.parameters.charset = charset;271272 // format type273 return contentType.format(parsed);274};275276/**277 * Parse an extended query string with qs.278 *279 * @return {Object}280 * @private281 */282283function parseExtendedQueryString(str) {284 return qs.parse(str, {285 allowPrototypes: true286 });287}288289/**290 * Return new empty object.291 *292 * @return {Object}293 * @api private294 */295296function newObject() {297 return {};298}