Skip to content
Myr

utils.js

This file

Path

lib/utils.js

283 lines · 5.3 KiB · JavaScript

Last changed by

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

History of this file

Take it

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

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption3205f68

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');

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption3205f68

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');

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption3205f68

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');

Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff

line 13why, one step back
13var typer = require('media-typer');

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption3205f68

lines 14–23why, one step back
14
15/**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;

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption3205f68

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});

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption3205f68

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;

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption3205f68

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});

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption3205f68

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');

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption3205f68

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–223why, 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 fn = newObject;209 break;210 case 'extended':211 fn = qs.parse;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/**

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption3205f68

lines 224–263why, one step back
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(type, charset){262 if (!type || !charset) return type;263

Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff

lines 264–265why, one step back
264 // parse type
265 var parsed = typer.parse(type);

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption3205f68

line 266why, one step back
266

Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff

lines 267–268why, one step back
267 // set charset
268 parsed.parameters.charset = charset;

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption3205f68

line 269why, one step back
269

Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff

lines 270–271why, one step back
270 // format type
271 return typer.format(parsed);

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption3205f68

line 272why, one step back
272};

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

lines 273–283why, one step back
273
274/**275 * Return new empty objet.276 *277 * @return {Object}278 * @api private279 */280281function newObject() {282 return {};283}