Skip to content
Myr

utils.js

This file

Path

lib/utils.js

299 lines · 5.7 KiB · JavaScript

Last changed by

Fix Windows absolute path check using forward slasheshuaoguobefore adoption2e1284b

History of this file

Take it

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

tests: add test for rendering extensionless file without view engineDouglas Christopher Wilsonbefore adoption0634e7e

lines 1–68why, 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 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)38 : body;3940 return etag(buf, {weak: false});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)55 : body;5657 return etag(buf, {weak: true});58};5960/**61 * Check if `path` looks absolute.62 *63 * @param {String} path64 * @return {Boolean}65 * @api private66 */6768exports.isAbsolute = function(path){

perf: use strict equality when possibleÁngel Sanzbefore adoptionb69b760

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

Fix Windows absolute path check using forward slasheshuaoguobefore adoption2e1284b

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

perf: use strict equality when possibleÁngel Sanzbefore adoptionb69b760

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

tests: add test for rendering extensionless file without view engineDouglas Christopher Wilsonbefore adoption0634e7e

lines 72–144why, one step back
72};
7374/**75 * Flatten the given `arr`.76 *77 * @param {Array} arr78 * @return {Array}79 * @api private80 */8182exports.flatten = deprecate.function(flatten,83 'utils.flatten: use array-flatten npm module instead');8485/**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 */125126exports.contentDisposition = deprecate.function(contentDisposition,127 'utils.contentDisposition: use content-disposition npm module instead');128129/**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(/ *= */);

perf: use strict equality when possibleÁngel Sanzbefore adoptionb69b760

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

tests: add test for rendering extensionless file without view engineDouglas Christopher Wilsonbefore adoption0634e7e

lines 146–211why, one step back
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;

tests: add test for rendering extensionless file without view engineDouglas Christopher Wilsonbefore adoption0634e7e

lines 213–275why, 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 */261262exports.setCharset = function setCharset(type, charset) {263 if (!type || !charset) {264 return type;265 }266267 // parse type268 var parsed = contentType.parse(type);269270 // set charset271 parsed.parameters.charset = charset;272273 // format type274 return contentType.format(parsed);275};

deps: qs@4.0.0Douglas Christopher Wilsonbefore adoption7bea53b

lines 276–288why, 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 allowPrototypes: true287 });288}

tests: add test for rendering extensionless file without view engineDouglas Christopher Wilsonbefore adoption0634e7e

lines 289–299why, one step back
289
290/**291 * Return new empty object.292 *293 * @return {Object}294 * @api private295 */296297function newObject() {298 return {};299}