utils.js
This file
Path
lib/utils.js
292 lines · 5.8 KiB · JavaScript
Last changed by
Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
History of this fileTake it
The walk stopped after 200 commits: lines older than 863160a point at it.
3.4.5Jonathan Ongbefore adoption863160a
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 tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
line 7why, one step back7var crypto = require('crypto');Merge pull request #2058 from thetalecrafter/content-dispositionRoman Shtylmanbefore adoptionbdb3bb9
line 8why, one step back8var basename = require('path').basename;Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060
line 9why, one step back9var deprecate = require('util').deprecate;Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
line 10why, one step back10var proxyaddr = require('proxy-addr');Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060
lines 11–12why, one step back1112/**Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061
lines 13–17why, one step back13 * Simple detection of charset parameter in content-type14 */15var charsetRegExp = /;\s*charset\s*=/;1617/**Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 18–19why, one step back18 * Deprecate function, like core `util.deprecate`,19 * but with NODE_ENV and color support.Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060
lines 20–27why, one step back20 *21 * @param {Function} fn22 * @param {String} msg23 * @return {Function}24 * @api private25 */2627exports.deprecate = function(fn, msg){Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 28–38why, one step back28 if (process.env.NODE_ENV === 'test') return fn;2930 // prepend module name31 msg = 'express: ' + msg;3233 if (process.stderr.isTTY) {34 // colorize35 msg = '\x1b[31;1m' + msg + '\x1b[0m';36 }3738 return deprecate(fn, msg);Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060
line 39why, one step back39};3.4.5Jonathan Ongbefore adoption863160a
lines 40–41why, one step back4041/**Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
line 42why, one step back42 * Return strong ETag for `body`.3.4.5Jonathan Ongbefore adoption863160a
lines 43–44why, one step back43 *44 * @param {String|Buffer} bodyMerge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
line 45why, one step back45 * @param {String} [encoding]3.4.5Jonathan Ongbefore adoption863160a
lines 46–49why, one step back46 * @return {String}47 * @api private48 */49Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
lines 50–60why, one step back50exports.etag = function etag(body, encoding){51 if (body.length === 0) {52 // fast-path empty body53 return '"1B2M2Y8AsgTpgAmY7PhCfg=="'54 }5556 var hash = crypto57 .createHash('md5')58 .update(body, encoding)59 .digest('base64')60 return '"' + hash + '"'3.4.5Jonathan Ongbefore adoption863160a
lines 61–63why, one step back61};6263/**Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
lines 64–85why, one step back64 * Return weak ETag for `body`.65 *66 * @param {String|Buffer} body67 * @param {String} [encoding]68 * @return {String}69 * @api private70 */7172exports.wetag = function wetag(body, encoding){73 if (body.length === 0) {74 // fast-path empty body75 return 'W/"0-0"'76 }7778 var buf = Buffer.isBuffer(body)79 ? body80 : new Buffer(body, encoding)81 var len = buf.length82 return 'W/"' + len.toString(16) + '-' + crc32.unsigned(buf) + '"'83};8485/**3.4.5Jonathan Ongbefore adoption863160a
lines 86–107why, one step back86 * Check if `path` looks absolute.87 *88 * @param {String} path89 * @return {Boolean}90 * @api private91 */9293exports.isAbsolute = function(path){94 if ('/' == path[0]) return true;95 if (':' == path[1] && '\\' == path[2]) return true;96 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute path97};9899/**100 * Flatten the given `arr`.101 *102 * @param {Array} arr103 * @return {Array}104 * @api private105 */106107exports.flatten = function(arr, ret){Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874
lines 108–109why, one step back108 ret = ret || [];109 var len = arr.length;3.4.5Jonathan Ongbefore adoption863160a
lines 110–146why, one step back110 for (var i = 0; i < len; ++i) {111 if (Array.isArray(arr[i])) {112 exports.flatten(arr[i], ret);113 } else {114 ret.push(arr[i]);115 }116 }117 return ret;118};119120/**121 * Normalize the given `type`, for example "html" becomes "text/html".122 *123 * @param {String} type124 * @return {Object}125 * @api private126 */127128exports.normalizeType = function(type){129 return ~type.indexOf('/')130 ? acceptParams(type)131 : { value: mime.lookup(type), params: {} };132};133134/**135 * Normalize `types`, for example "html" becomes "text/html".136 *137 * @param {Array} types138 * @return {Array}139 * @api private140 */141142exports.normalizeTypes = function(types){143 var ret = [];144145 for (var i = 0; i < types.length; ++i) {146 ret.push(exports.normalizeType(types[i]));Merge pull request #2058 from thetalecrafter/content-dispositionRoman Shtylmanbefore adoptionbdb3bb9
lines 147–168why, one step back147 }148149 return ret;150};151152/**153 * Generate Content-Disposition header appropriate for the filename.154 * non-ascii filenames are urlencoded and a filename* parameter is added155 *156 * @param {String} filename157 * @return {String}158 * @api private159 */160161exports.contentDisposition = function(filename){162 var ret = 'attachment';163 if (filename) {164 filename = basename(filename);165 // if filename contains non-ascii characters, add a utf-8 version ala RFC 5987166 ret = /[^\040-\176]/.test(filename)167 ? 'attachment; filename=' + encodeURI(filename) + '; filename*=UTF-8\'\'' + encodeURI(filename)168 : 'attachment; filename="' + filename + '"';3.4.5Jonathan Ongbefore adoption863160a
lines 169–197why, one step back169 }170171 return ret;172};173174/**175 * Parse accept params `str` returning an176 * object with `.value`, `.quality` and `.params`.177 * also includes `.originalIndex` for stable sorting178 *179 * @param {String} str180 * @return {Object}181 * @api private182 */183184function acceptParams(str, index) {185 var parts = str.split(/ *; */);186 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };187188 for (var i = 1; i < parts.length; ++i) {189 var pms = parts[i].split(/ *= */);190 if ('q' == pms[0]) {191 ret.quality = parseFloat(pms[1]);192 } else {193 ret.params[pms[0]] = pms[1];194 }195 }196197 return ret;Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
lines 198–231why, one step back198}199200/**201 * Compile "etag" value to function.202 *203 * @param {Boolean|String|Function} val204 * @return {Function}205 * @api private206 */207208exports.compileETag = function(val) {209 var fn;210211 if (typeof val === 'function') {212 return val;213 }214215 switch (val) {216 case true:217 fn = exports.wetag;218 break;219 case false:220 break;221 case 'strong':222 fn = exports.etag;223 break;224 case 'weak':225 fn = exports.wetag;226 break;227 default:228 throw new TypeError('unknown value for etag function: ' + val);229 }230231 return fn;Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 232–260why, one step back232}233234/**235 * Compile "proxy trust" value to function.236 *237 * @param {Boolean|String|Number|Array|Function} val238 * @return {Function}239 * @api private240 */241242exports.compileTrust = function(val) {243 if (typeof val === 'function') return val;244245 if (val === true) {246 // Support plain true/false247 return function(){ return true };248 }249250 if (typeof val === 'number') {251 // Support trusting hop count252 return function(a, i){ return i < val };253 }254255 if (typeof val === 'string') {256 // Support comma-separated values257 val = val.split(/ *, */);258 }259260 return proxyaddr.compile(val || []);3.4.5Jonathan Ongbefore adoption863160a
line 261why, one step back261}Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061
lines 262–292why, one step back262263/**264 * Set the charset in a given Content-Type string.265 *266 * @param {String} type267 * @param {String} charset268 * @return {String}269 * @api private270 */271272exports.setCharset = function(type, charset){273 if (!type || !charset) return type;274275 var exists = charsetRegExp.test(type);276277 // removing existing charset278 if (exists) {279 var parts = type.split(';');280281 for (var i = 1; i < parts.length; i++) {282 if (charsetRegExp.test(';' + parts[i])) {283 parts.splice(i, 1);284 break;285 }286 }287288 type = parts.join(';');289 }290291 return type + '; charset=' + charset;292};