Skip to content
Myr

utils.js

This file

Path

lib/utils.js

293 lines · 5.5 KiB · JavaScript

Last changed by

Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024

History of this file

Take it

The walk stopped after 200 commits: lines older than ad3ca25 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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 8–9why, one step back
8/**
9 * Module dependencies.

Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 11–12why, one step back
11 */
12

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

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

Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024

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

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

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

Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 19–31why, one step back
19var proxyaddr = require('proxy-addr');
20var qs = require('qs');21var querystring = require('querystring');2223/**24 * Return strong ETag for `body`.25 *26 * @param {String|Buffer} body27 * @param {String} [encoding]28 * @return {String}29 * @api private30 */31

Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed

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

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 35why, one step back
35 : body;

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

line 36why, one step back
36

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 38–49why, one step back
38};
3940/**41 * Return weak ETag for `body`.42 *43 * @param {String|Buffer} body44 * @param {String} [encoding]45 * @return {String}46 * @api private47 */4849exports.wetag = function wetag(body, encoding){

Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed

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

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 52why, one step back
52 : body;

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

line 53why, one step back
53

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 55–132why, one step back
55};
5657/**58 * Check if `path` looks absolute.59 *60 * @param {String} path61 * @return {Boolean}62 * @api private63 */6465exports.isAbsolute = function(path){66 if ('/' == path[0]) return true;67 if (':' == path[1] && '\\' == path[2]) return true;68 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute path69};7071/**72 * Flatten the given `arr`.73 *74 * @param {Array} arr75 * @return {Array}76 * @api private77 */7879exports.flatten = function(arr, ret){80 ret = ret || [];81 var len = arr.length;82 for (var i = 0; i < len; ++i) {83 if (Array.isArray(arr[i])) {84 exports.flatten(arr[i], ret);85 } else {86 ret.push(arr[i]);87 }88 }89 return ret;90};9192/**93 * Normalize the given `type`, for example "html" becomes "text/html".94 *95 * @param {String} type96 * @return {Object}97 * @api private98 */99100exports.normalizeType = function(type){101 return ~type.indexOf('/')102 ? acceptParams(type)103 : { value: mime.lookup(type), params: {} };104};105106/**107 * Normalize `types`, for example "html" becomes "text/html".108 *109 * @param {Array} types110 * @return {Array}111 * @api private112 */113114exports.normalizeTypes = function(types){115 var ret = [];116117 for (var i = 0; i < types.length; ++i) {118 ret.push(exports.normalizeType(types[i]));119 }120121 return ret;122};123124/**125 * Generate Content-Disposition header appropriate for the filename.126 * non-ascii filenames are urlencoded and a filename* parameter is added127 *128 * @param {String} filename129 * @return {String}130 * @api private131 */132

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 135–268why, one step back
135
136/**137 * Parse accept params `str` returning an138 * object with `.value`, `.quality` and `.params`.139 * also includes `.originalIndex` for stable sorting140 *141 * @param {String} str142 * @return {Object}143 * @api private144 */145146function acceptParams(str, index) {147 var parts = str.split(/ *; */);148 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };149150 for (var i = 1; i < parts.length; ++i) {151 var pms = parts[i].split(/ *= */);152 if ('q' == pms[0]) {153 ret.quality = parseFloat(pms[1]);154 } else {155 ret.params[pms[0]] = pms[1];156 }157 }158159 return ret;160}161162/**163 * Compile "etag" value to function.164 *165 * @param {Boolean|String|Function} val166 * @return {Function}167 * @api private168 */169170exports.compileETag = function(val) {171 var fn;172173 if (typeof val === 'function') {174 return val;175 }176177 switch (val) {178 case true:179 fn = exports.wetag;180 break;181 case false:182 break;183 case 'strong':184 fn = exports.etag;185 break;186 case 'weak':187 fn = exports.wetag;188 break;189 default:190 throw new TypeError('unknown value for etag function: ' + val);191 }192193 return fn;194}195196/**197 * Compile "query parser" value to function.198 *199 * @param {String|Function} val200 * @return {Function}201 * @api private202 */203204exports.compileQueryParser = function compileQueryParser(val) {205 var fn;206207 if (typeof val === 'function') {208 return val;209 }210211 switch (val) {212 case true:213 fn = querystring.parse;214 break;215 case false:216 fn = newObject;217 break;218 case 'extended':219 fn = qs.parse;220 break;221 case 'simple':222 fn = querystring.parse;223 break;224 default:225 throw new TypeError('unknown value for query parser function: ' + val);226 }227228 return fn;229}230231/**232 * Compile "proxy trust" value to function.233 *234 * @param {Boolean|String|Number|Array|Function} val235 * @return {Function}236 * @api private237 */238239exports.compileTrust = function(val) {240 if (typeof val === 'function') return val;241242 if (val === true) {243 // Support plain true/false244 return function(){ return true };245 }246247 if (typeof val === 'number') {248 // Support trusting hop count249 return function(a, i){ return i < val };250 }251252 if (typeof val === 'string') {253 // Support comma-separated values254 val = val.split(/ *, */);255 }256257 return proxyaddr.compile(val || []);258}259260/**261 * Set the charset in a given Content-Type string.262 *263 * @param {String} type264 * @param {String} charset265 * @return {String}266 * @api private267 */268

Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024

lines 269–272why, one step back
269exports.setCharset = function setCharset(type, charset) {
270 if (!type || !charset) {271 return type;272 }

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 273–274why, one step back
273
274 // parse type

Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 276–280why, one step back
276
277 // set charset278 parsed.parameters.charset = charset;279280 // format type

Merge tag '3.20.0'Douglas Christopher Wilsonbefore adoption531f024

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 282–284why, one step back
282};
283284/**

Fix typo in jsdoc descriptionVladimir Grinenkobefore adoption72d35b6

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

deps: qs@1.2.2Douglas Christopher Wilsonbefore adoptionad3ca25

lines 286–293why, one step back
286 *
287 * @return {Object}288 * @api private289 */290291function newObject() {292 return {};293}