utils.js
This file
Path
lib/utils.js
158 lines · 3.3 KiB · JavaScript
Last changed by
Merge pull request #1908 from visionmedia/locals-objectRoman Shtylmanbefore adoption991c2a9
History of this fileTake it
The walk stopped after 200 commits: lines older than 480d006 point at it.
Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 1–9why, one step back12/**3 * Module dependencies.4 */56var mime = require('connect').mime7 , crc32 = require('buffer-crc32');89/**refactorTJ Holowaychukbefore adoptionf8a33d1
lines 10–15why, one step back10 * toString ref.11 */1213var toString = {}.toString;1415/**Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 16–37why, one step back16 * Return ETag for `body`.17 *18 * @param {String|Buffer} body19 * @return {String}20 * @api private21 */2223exports.etag = function(body){24 return '"' + crc32.signed(body) + '"';25};2627/**28 * Check if `path` looks absolute.29 *30 * @param {String} path31 * @return {Boolean}32 * @api private33 */3435exports.isAbsolute = function(path){36 if ('/' == path[0]) return true;37 if (':' == path[1] && '\\' == path[2]) return true;Merge pull request #1740 from superic/masterTJ Holowaychukbefore adoptione43ff07
line 38why, one step back38 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute pathMerge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 39–65why, one step back39};4041/**42 * Flatten the given `arr`.43 *44 * @param {Array} arr45 * @return {Array}46 * @api private47 */4849exports.flatten = function(arr, ret){50 var ret = ret || []51 , len = arr.length;52 for (var i = 0; i < len; ++i) {53 if (Array.isArray(arr[i])) {54 exports.flatten(arr[i], ret);55 } else {56 ret.push(arr[i]);57 }58 }59 return ret;60};6162/**63 * Normalize the given `type`, for example "html" becomes "text/html".64 *65 * @param {String} typeadd support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 66why, one step back66 * @return {Object}Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 67–70why, one step back67 * @api private68 */6970exports.normalizeType = function(type){add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 71–73why, one step back71 return ~type.indexOf('/')72 ? acceptParams(type)73 : { value: mime.lookup(type), params: {} };Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 74–87why, one step back74};7576/**77 * Normalize `types`, for example "html" becomes "text/html".78 *79 * @param {Array} types80 * @return {Array}81 * @api private82 */8384exports.normalizeTypes = function(types){85 var ret = [];8687 for (var i = 0; i < types.length; ++i) {add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 88why, one step back88 ret.push(exports.normalizeType(types[i]));Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 89–95why, one step back89 }9091 return ret;92};939495/**add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 96–97why, one step back96 * Parse accept params `str` returning an97 * object with `.value`, `.quality` and `.params`.Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 98why, one step back98 * also includes `.originalIndex` for stable sortingMerge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 99–104why, one step back99 *100 * @param {String} str101 * @return {Object}102 * @api private103 */104Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 105why, one step back105function acceptParams(str, index) {add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 106why, one step back106 var parts = str.split(/ *; */);Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 107why, one step back107 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
line 108why, one step back108add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 109–116why, one step back109 for (var i = 1; i < parts.length; ++i) {110 var pms = parts[i].split(/ *= */);111 if ('q' == pms[0]) {112 ret.quality = parseFloat(pms[1]);113 } else {114 ret.params[pms[0]] = pms[1];115 }116 }Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
line 117why, one step back117add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 118why, one step back118 return ret;Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 119–138why, one step back119}120121/**122 * Normalize the given path string,123 * returning a regular expression.124 *125 * An empty array should be passed,126 * which will contain the placeholder127 * key names. For example "/user/:id" will128 * then contain ["id"].129 *130 * @param {String|RegExp|Array} path131 * @param {Array} keys132 * @param {Boolean} sensitive133 * @param {Boolean} strict134 * @return {RegExp}135 * @api private136 */137138exports.pathRegexp = function(path, keys, sensitive, strict) {refactorTJ Holowaychukbefore adoptionf8a33d1
line 139why, one step back139 if (toString.call(path) == '[object RegExp]') return path;Merge pull request #1462 from gmethvin/colon_authTJ Holowaychukbefore adoption480d006
lines 140–158why, one step back140 if (Array.isArray(path)) path = '(' + path.join('|') + ')';141 path = path142 .concat(strict ? '' : '/?')143 .replace(/\/\(/g, '(?:/')144 .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g, function(_, slash, format, key, capture, optional, star){145 keys.push({ name: key, optional: !! optional });146 slash = slash || '';147 return ''148 + (optional ? '' : slash)149 + '(?:'150 + (optional ? slash : '')151 + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'152 + (optional || '')153 + (star ? '(/*)?' : '');154 })155 .replace(/([\/.])/g, '\\$1')156 .replace(/\*/g, '(.*)');157 return new RegExp('^' + path + '$', sensitive ? '' : 'i');158}