utils.js
This file
Path
lib/utils.js
225 lines · 4.5 KiB · JavaScript
Last changed by
Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061
History of this fileTake it
The walk stopped after 200 commits: lines older than 9fb6615 point at it.
refactor signed cookie testsTJ Holowaychukbefore adoption9fb6615
lines 1–4why, one step back1/**2 * Module dependencies.3 */4remove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08
lines 5–6why, one step back5var mime = require('send').mime;6var crc32 = require('buffer-crc32');Merge pull request #2058 from thetalecrafter/content-dispositionRoman Shtylmanbefore adoptionbdb3bb9
line 7why, one step back7var basename = require('path').basename;Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060
line 8why, one step back8var deprecate = require('util').deprecate;Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
line 9why, one step back9var proxyaddr = require('proxy-addr');Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060
lines 10–11why, one step back1011/**Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061
lines 12–16why, one step back12 * Simple detection of charset parameter in content-type13 */14var charsetRegExp = /;\s*charset\s*=/;1516/**Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 17–18why, one step back17 * Deprecate function, like core `util.deprecate`,18 * but with NODE_ENV and color support.Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060
lines 19–26why, one step back19 *20 * @param {Function} fn21 * @param {String} msg22 * @return {Function}23 * @api private24 */2526exports.deprecate = function(fn, msg){Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 27–37why, one step back27 if (process.env.NODE_ENV === 'test') return fn;2829 // prepend module name30 msg = 'express: ' + msg;3132 if (process.stderr.isTTY) {33 // colorize34 msg = '\x1b[31;1m' + msg + '\x1b[0m';35 }3637 return deprecate(fn, msg);Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060
line 38why, one step back38};refactor signed cookie testsTJ Holowaychukbefore adoption9fb6615
lines 39–74why, one step back3940/**41 * Return ETag for `body`.42 *43 * @param {String|Buffer} body44 * @return {String}45 * @api private46 */4748exports.etag = function(body){49 return '"' + crc32.signed(body) + '"';50};5152/**53 * Check if `path` looks absolute.54 *55 * @param {String} path56 * @return {Boolean}57 * @api private58 */5960exports.isAbsolute = function(path){61 if ('/' == path[0]) return true;62 if (':' == path[1] && '\\' == path[2]) return true;63 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute path64};6566/**67 * Flatten the given `arr`.68 *69 * @param {Array} arr70 * @return {Array}71 * @api private72 */7374exports.flatten = function(arr, ret){Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874
lines 75–76why, one step back75 ret = ret || [];76 var len = arr.length;refactor signed cookie testsTJ Holowaychukbefore adoption9fb6615
lines 77–113why, one step back77 for (var i = 0; i < len; ++i) {78 if (Array.isArray(arr[i])) {79 exports.flatten(arr[i], ret);80 } else {81 ret.push(arr[i]);82 }83 }84 return ret;85};8687/**88 * Normalize the given `type`, for example "html" becomes "text/html".89 *90 * @param {String} type91 * @return {Object}92 * @api private93 */9495exports.normalizeType = function(type){96 return ~type.indexOf('/')97 ? acceptParams(type)98 : { value: mime.lookup(type), params: {} };99};100101/**102 * Normalize `types`, for example "html" becomes "text/html".103 *104 * @param {Array} types105 * @return {Array}106 * @api private107 */108109exports.normalizeTypes = function(types){110 var ret = [];111112 for (var i = 0; i < types.length; ++i) {113 ret.push(exports.normalizeType(types[i]));Merge pull request #2058 from thetalecrafter/content-dispositionRoman Shtylmanbefore adoptionbdb3bb9
lines 114–135why, one step back114 }115116 return ret;117};118119/**120 * Generate Content-Disposition header appropriate for the filename.121 * non-ascii filenames are urlencoded and a filename* parameter is added122 *123 * @param {String} filename124 * @return {String}125 * @api private126 */127128exports.contentDisposition = function(filename){129 var ret = 'attachment';130 if (filename) {131 filename = basename(filename);132 // if filename contains non-ascii characters, add a utf-8 version ala RFC 5987133 ret = /[^\040-\176]/.test(filename)134 ? 'attachment; filename=' + encodeURI(filename) + '; filename*=UTF-8\'\'' + encodeURI(filename)135 : 'attachment; filename="' + filename + '"';refactor signed cookie testsTJ Holowaychukbefore adoption9fb6615
lines 136–164why, one step back136 }137138 return ret;139};140141/**142 * Parse accept params `str` returning an143 * object with `.value`, `.quality` and `.params`.144 * also includes `.originalIndex` for stable sorting145 *146 * @param {String} str147 * @return {Object}148 * @api private149 */150151function acceptParams(str, index) {152 var parts = str.split(/ *; */);153 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };154155 for (var i = 1; i < parts.length; ++i) {156 var pms = parts[i].split(/ *= */);157 if ('q' == pms[0]) {158 ret.quality = parseFloat(pms[1]);159 } else {160 ret.params[pms[0]] = pms[1];161 }162 }163164 return ret;Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 165–193why, one step back165}166167/**168 * Compile "proxy trust" value to function.169 *170 * @param {Boolean|String|Number|Array|Function} val171 * @return {Function}172 * @api private173 */174175exports.compileTrust = function(val) {176 if (typeof val === 'function') return val;177178 if (val === true) {179 // Support plain true/false180 return function(){ return true };181 }182183 if (typeof val === 'number') {184 // Support trusting hop count185 return function(a, i){ return i < val };186 }187188 if (typeof val === 'string') {189 // Support comma-separated values190 val = val.split(/ *, */);191 }192193 return proxyaddr.compile(val || []);refactor signed cookie testsTJ Holowaychukbefore adoption9fb6615
line 194why, one step back194}Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061
lines 195–225why, one step back195196/**197 * Set the charset in a given Content-Type string.198 *199 * @param {String} type200 * @param {String} charset201 * @return {String}202 * @api private203 */204205exports.setCharset = function(type, charset){206 if (!type || !charset) return type;207208 var exists = charsetRegExp.test(type);209210 // removing existing charset211 if (exists) {212 var parts = type.split(';');213214 for (var i = 1; i < parts.length; i++) {215 if (charsetRegExp.test(';' + parts[i])) {216 parts.splice(i, 1);217 break;218 }219 }220221 type = parts.join(';');222 }223224 return type + '; charset=' + charset;225};