Skip to content
Myr

utils.js

This file

Path

lib/utils.js

271 lines · 5.2 KiB · JavaScript

Last changed by

Change req.query to a getterDouglas Christopher Wilsonbefore adoptiondcc4eaa

History of this file

Take it

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

add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1

lines 1–4why, one step back
1/**
2 * Module dependencies.3 */4

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

lines 5–6why, one step back
5var contentDisposition = require('content-disposition');
6var deprecate = require('depd')('express');

add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1

lines 7–8why, one step back
7var mime = require('send').mime;
8var basename = require('path').basename;

Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed

line 9why, one step back
9var etag = require('etag');

add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1

line 10why, one step back
10var proxyaddr = require('proxy-addr');

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

lines 11–12why, one step back
11var qs = require('qs');
12var querystring = require('querystring');

add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1

lines 13–23why, one step back
13var typer = require('media-typer');
1415/**16 * Return strong ETag for `body`.17 *18 * @param {String|Buffer} body19 * @param {String} [encoding]20 * @return {String}21 * @api private22 */23

Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed

lines 24–26why, one step back
24exports.etag = function (body, encoding) {
25 var buf = !Buffer.isBuffer(body)26 ? new Buffer(body, encoding)

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 27why, one step back
27 : body;

add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1

line 28why, one step back
28

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

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

add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1

lines 30–41why, one step back
30};
3132/**33 * Return weak ETag for `body`.34 *35 * @param {String|Buffer} body36 * @param {String} [encoding]37 * @return {String}38 * @api private39 */4041exports.wetag = function wetag(body, encoding){

Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed

lines 42–43why, one step back
42 var buf = !Buffer.isBuffer(body)
43 ? new Buffer(body, encoding)

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 44why, one step back
44 : body;

add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1

line 45why, one step back
45

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

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

add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1

lines 47–124why, one step back
47};
4849/**50 * Check if `path` looks absolute.51 *52 * @param {String} path53 * @return {Boolean}54 * @api private55 */5657exports.isAbsolute = function(path){58 if ('/' == path[0]) return true;59 if (':' == path[1] && '\\' == path[2]) return true;60 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute path61};6263/**64 * Flatten the given `arr`.65 *66 * @param {Array} arr67 * @return {Array}68 * @api private69 */7071exports.flatten = function(arr, ret){72 ret = ret || [];73 var len = arr.length;74 for (var i = 0; i < len; ++i) {75 if (Array.isArray(arr[i])) {76 exports.flatten(arr[i], ret);77 } else {78 ret.push(arr[i]);79 }80 }81 return ret;82};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 */124

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

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

add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1

lines 127–188why, one step back
127
128/**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(/ *= */);144 if ('q' == pms[0]) {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/**

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

lines 189–222why, one step back
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 break;209 case 'extended':210 fn = qs.parse;211 break;212 case 'simple':213 fn = querystring.parse;214 break;215 default:216 throw new TypeError('unknown value for query parser function: ' + val);217 }218219 return fn;220}221222/**

add deprecation message to non-plural req.accepts*Douglas Christopher Wilsonbefore adoption2cccbc1

lines 223–271why, one step back
223 * Compile "proxy trust" value to function.
224 *225 * @param {Boolean|String|Number|Array|Function} val226 * @return {Function}227 * @api private228 */229230exports.compileTrust = function(val) {231 if (typeof val === 'function') return val;232233 if (val === true) {234 // Support plain true/false235 return function(){ return true };236 }237238 if (typeof val === 'number') {239 // Support trusting hop count240 return function(a, i){ return i < val };241 }242243 if (typeof val === 'string') {244 // Support comma-separated values245 val = val.split(/ *, */);246 }247248 return proxyaddr.compile(val || []);249}250251/**252 * Set the charset in a given Content-Type string.253 *254 * @param {String} type255 * @param {String} charset256 * @return {String}257 * @api private258 */259260exports.setCharset = function(type, charset){261 if (!type || !charset) return type;262263 // parse type264 var parsed = typer.parse(type);265266 // set charset267 parsed.parameters.charset = charset;268269 // format type270 return typer.format(parsed);271};