utils.js
This file
Path
lib/utils.js
254 lines · 5.0 KiB · JavaScript
Last changed by
Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff
History of this fileTake it
The walk stopped after 200 commits: lines older than 5ddbb69 point at it.
Merge pull request #1868 from dpatti/smarter-router-auto-optionsRoman Shtylmanbefore adoption5ddbb69
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.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
line 9why, one step back9var proxyaddr = require('proxy-addr');Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff
line 10why, one step back10var typer = require('media-typer');Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060
lines 11–12why, one step back1112/**Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
line 13why, one step back13 * Return strong ETag for `body`.Merge pull request #1868 from dpatti/smarter-router-auto-optionsRoman Shtylmanbefore adoption5ddbb69
lines 14–15why, one step back14 *15 * @param {String|Buffer} bodyMerge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
line 16why, one step back16 * @param {String} [encoding]Merge pull request #1868 from dpatti/smarter-router-auto-optionsRoman Shtylmanbefore adoption5ddbb69
lines 17–20why, one step back17 * @return {String}18 * @api private19 */20Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
lines 21–31why, one step back21exports.etag = function etag(body, encoding){22 if (body.length === 0) {23 // fast-path empty body24 return '"1B2M2Y8AsgTpgAmY7PhCfg=="'25 }2627 var hash = crypto28 .createHash('md5')29 .update(body, encoding)30 .digest('base64')31 return '"' + hash + '"'Merge pull request #1868 from dpatti/smarter-router-auto-optionsRoman Shtylmanbefore adoption5ddbb69
lines 32–34why, one step back32};3334/**Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
lines 35–56why, one step back35 * Return weak ETag for `body`.36 *37 * @param {String|Buffer} body38 * @param {String} [encoding]39 * @return {String}40 * @api private41 */4243exports.wetag = function wetag(body, encoding){44 if (body.length === 0) {45 // fast-path empty body46 return 'W/"0-0"'47 }4849 var buf = Buffer.isBuffer(body)50 ? body51 : new Buffer(body, encoding)52 var len = buf.length53 return 'W/"' + len.toString(16) + '-' + crc32.unsigned(buf) + '"'54};5556/**Merge pull request #1868 from dpatti/smarter-router-auto-optionsRoman Shtylmanbefore adoption5ddbb69
lines 57–78why, one step back57 * Check if `path` looks absolute.58 *59 * @param {String} path60 * @return {Boolean}61 * @api private62 */6364exports.isAbsolute = function(path){65 if ('/' == path[0]) return true;66 if (':' == path[1] && '\\' == path[2]) return true;67 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute path68};6970/**71 * Flatten the given `arr`.72 *73 * @param {Array} arr74 * @return {Array}75 * @api private76 */7778exports.flatten = function(arr, ret){Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874
lines 79–80why, one step back79 ret = ret || [];80 var len = arr.length;Merge pull request #1868 from dpatti/smarter-router-auto-optionsRoman Shtylmanbefore adoption5ddbb69
lines 81–117why, one step back81 for (var i = 0; i < len; ++i) {82 if (Array.isArray(arr[i])) {83 exports.flatten(arr[i], ret);84 } else {85 ret.push(arr[i]);86 }87 }88 return ret;89};9091/**92 * Normalize the given `type`, for example "html" becomes "text/html".93 *94 * @param {String} type95 * @return {Object}96 * @api private97 */9899exports.normalizeType = function(type){100 return ~type.indexOf('/')101 ? acceptParams(type)102 : { value: mime.lookup(type), params: {} };103};104105/**106 * Normalize `types`, for example "html" becomes "text/html".107 *108 * @param {Array} types109 * @return {Array}110 * @api private111 */112113exports.normalizeTypes = function(types){114 var ret = [];115116 for (var i = 0; i < types.length; ++i) {117 ret.push(exports.normalizeType(types[i]));Merge pull request #2058 from thetalecrafter/content-dispositionRoman Shtylmanbefore adoptionbdb3bb9
lines 118–137why, one step back118 }119120 return ret;121};122123/**124 * Generate Content-Disposition header appropriate for the filename.125 * non-ascii filenames are urlencoded and a filename* parameter is added126 *127 * @param {String} filename128 * @return {String}129 * @api private130 */131132exports.contentDisposition = function(filename){133 var ret = 'attachment';134 if (filename) {135 filename = basename(filename);136 // if filename contains non-ascii characters, add a utf-8 version ala RFC 5987137 ret = /[^\040-\176]/.test(filename)Fix res.attachment Unicode filenames in Safari刘星before adoptione606d99
line 138why, one step back138 ? 'attachment; filename="' + encodeURI(filename) + '"; filename*=UTF-8\'\'' + encodeURI(filename)Merge pull request #2058 from thetalecrafter/content-dispositionRoman Shtylmanbefore adoptionbdb3bb9
line 139why, one step back139 : 'attachment; filename="' + filename + '"';Merge pull request #1868 from dpatti/smarter-router-auto-optionsRoman Shtylmanbefore adoption5ddbb69
lines 140–168why, one step back140 }141142 return ret;143};144145/**146 * Parse accept params `str` returning an147 * object with `.value`, `.quality` and `.params`.148 * also includes `.originalIndex` for stable sorting149 *150 * @param {String} str151 * @return {Object}152 * @api private153 */154155function acceptParams(str, index) {156 var parts = str.split(/ *; */);157 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };158159 for (var i = 1; i < parts.length; ++i) {160 var pms = parts[i].split(/ *= */);161 if ('q' == pms[0]) {162 ret.quality = parseFloat(pms[1]);163 } else {164 ret.params[pms[0]] = pms[1];165 }166 }167168 return ret;Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
lines 169–202why, one step back169}170171/**172 * Compile "etag" value to function.173 *174 * @param {Boolean|String|Function} val175 * @return {Function}176 * @api private177 */178179exports.compileETag = function(val) {180 var fn;181182 if (typeof val === 'function') {183 return val;184 }185186 switch (val) {187 case true:188 fn = exports.wetag;189 break;190 case false:191 break;192 case 'strong':193 fn = exports.etag;194 break;195 case 'weak':196 fn = exports.wetag;197 break;198 default:199 throw new TypeError('unknown value for etag function: ' + val);200 }201202 return fn;Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
lines 203–231why, one step back203}204205/**206 * Compile "proxy trust" value to function.207 *208 * @param {Boolean|String|Number|Array|Function} val209 * @return {Function}210 * @api private211 */212213exports.compileTrust = function(val) {214 if (typeof val === 'function') return val;215216 if (val === true) {217 // Support plain true/false218 return function(){ return true };219 }220221 if (typeof val === 'number') {222 // Support trusting hop count223 return function(a, i){ return i < val };224 }225226 if (typeof val === 'string') {227 // Support comma-separated values228 val = val.split(/ *, */);229 }230231 return proxyaddr.compile(val || []);Merge pull request #1868 from dpatti/smarter-router-auto-optionsRoman Shtylmanbefore adoption5ddbb69
line 232why, one step back232}Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061
lines 233–245why, one step back233234/**235 * Set the charset in a given Content-Type string.236 *237 * @param {String} type238 * @param {String} charset239 * @return {String}240 * @api private241 */242243exports.setCharset = function(type, charset){244 if (!type || !charset) return type;245Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff
lines 246–247why, one step back246 // parse type247 var parsed = typer.parse(type);Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061
line 248why, one step back248Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff
lines 249–250why, one step back249 // set charset250 parsed.parameters.charset = charset;Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061
line 251why, one step back251Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff
lines 252–253why, one step back252 // format type253 return typer.format(parsed);Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061
line 254why, one step back254};