Skip to content
Myr

utils.js

This file

Path

lib/utils.js

189 lines · 3.9 KiB · JavaScript

Last changed by

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

History of this file

Take it

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

update connectTJ Holowaychukbefore adoption929ffb8

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

remove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08

lines 5–6why, one step back
5var mime = require('send').mime;
6var crc32 = require('buffer-crc32');

Merge pull request #2058 from thetalecrafter/content-dispositionRoman Shtylmanbefore adoptionbdb3bb9

line 7why, one step back
7var basename = require('path').basename;

Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060

line 8why, one step back
8var deprecate = require('util').deprecate;

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

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

Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060

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

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

lines 12–13why, one step back
12 * Deprecate function, like core `util.deprecate`,
13 * but with NODE_ENV and color support.

Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060

lines 14–21why, one step back
14 *
15 * @param {Function} fn16 * @param {String} msg17 * @return {Function}18 * @api private19 */2021exports.deprecate = function(fn, msg){

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

lines 22–32why, one step back
22 if (process.env.NODE_ENV === 'test') return fn;
2324 // prepend module name25 msg = 'express: ' + msg;2627 if (process.stderr.isTTY) {28 // colorize29 msg = '\x1b[31;1m' + msg + '\x1b[0m';30 }3132 return deprecate(fn, msg);

Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060

line 33why, one step back
33};

update connectTJ Holowaychukbefore adoption929ffb8

lines 34–57why, one step back
34
35/**36 * Return ETag for `body`.37 *38 * @param {String|Buffer} body39 * @return {String}40 * @api private41 */4243exports.etag = function(body){44 return '"' + crc32.signed(body) + '"';45};4647/**48 * Check if `path` looks absolute.49 *50 * @param {String} path51 * @return {Boolean}52 * @api private53 */5455exports.isAbsolute = function(path){56 if ('/' == path[0]) return true;57 if (':' == path[1] && '\\' == path[2]) return true;

Merge pull request #1740 from superic/masterTJ Holowaychukbefore adoptione43ff07

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

update connectTJ Holowaychukbefore adoption929ffb8

lines 59–69why, one step back
59};
6061/**62 * Flatten the given `arr`.63 *64 * @param {Array} arr65 * @return {Array}66 * @api private67 */6869exports.flatten = function(arr, ret){

Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874

lines 70–71why, one step back
70 ret = ret || [];
71 var len = arr.length;

update connectTJ Holowaychukbefore adoption929ffb8

lines 72–108why, one step back
72 for (var i = 0; i < len; ++i) {
73 if (Array.isArray(arr[i])) {74 exports.flatten(arr[i], ret);75 } else {76 ret.push(arr[i]);77 }78 }79 return ret;80};8182/**83 * Normalize the given `type`, for example "html" becomes "text/html".84 *85 * @param {String} type86 * @return {Object}87 * @api private88 */8990exports.normalizeType = function(type){91 return ~type.indexOf('/')92 ? acceptParams(type)93 : { value: mime.lookup(type), params: {} };94};9596/**97 * Normalize `types`, for example "html" becomes "text/html".98 *99 * @param {Array} types100 * @return {Array}101 * @api private102 */103104exports.normalizeTypes = function(types){105 var ret = [];106107 for (var i = 0; i < types.length; ++i) {108 ret.push(exports.normalizeType(types[i]));

Merge pull request #2058 from thetalecrafter/content-dispositionRoman Shtylmanbefore adoptionbdb3bb9

lines 109–130why, one step back
109 }
110111 return ret;112};113114/**115 * Generate Content-Disposition header appropriate for the filename.116 * non-ascii filenames are urlencoded and a filename* parameter is added117 *118 * @param {String} filename119 * @return {String}120 * @api private121 */122123exports.contentDisposition = function(filename){124 var ret = 'attachment';125 if (filename) {126 filename = basename(filename);127 // if filename contains non-ascii characters, add a utf-8 version ala RFC 5987128 ret = /[^\040-\176]/.test(filename)129 ? 'attachment; filename=' + encodeURI(filename) + '; filename*=UTF-8\'\'' + encodeURI(filename)130 : 'attachment; filename="' + filename + '"';

update connectTJ Holowaychukbefore adoption929ffb8

lines 131–159why, one step back
131 }
132133 return ret;134};135136/**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;

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

lines 160–188why, one step back
160}
161162/**163 * Compile "proxy trust" value to function.164 *165 * @param {Boolean|String|Number|Array|Function} val166 * @return {Function}167 * @api private168 */169170exports.compileTrust = function(val) {171 if (typeof val === 'function') return val;172173 if (val === true) {174 // Support plain true/false175 return function(){ return true };176 }177178 if (typeof val === 'number') {179 // Support trusting hop count180 return function(a, i){ return i < val };181 }182183 if (typeof val === 'string') {184 // Support comma-separated values185 val = val.split(/ *, */);186 }187188 return proxyaddr.compile(val || []);

update connectTJ Holowaychukbefore adoption929ffb8

line 189why, one step back
189}