utils.js
This file
Path
lib/utils.js
184 lines · 4.0 KiB · JavaScript
Last changed by
Merge pull request #1935 from visionmedia/router-params-middlewareRoman Shtylmanbefore adoptioncaa25b5
History of this fileTake it
The walk stopped after 200 commits: lines older than 916acd1 point at it.
replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1
lines 1–5why, one step back12/**3 * Module dependencies.4 */5remove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08
lines 6–8why, one step back6var mime = require('send').mime;7var crc32 = require('buffer-crc32');8var parse = require('url').parse;replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1
lines 9–10why, one step back910/**refactorTJ Holowaychukbefore adoptionf8a33d1
lines 11–16why, one step back11 * toString ref.12 */1314var toString = {}.toString;1516/**replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1
lines 17–38why, one step back17 * Return ETag for `body`.18 *19 * @param {String|Buffer} body20 * @return {String}21 * @api private22 */2324exports.etag = function(body){25 return '"' + crc32.signed(body) + '"';26};2728/**29 * Check if `path` looks absolute.30 *31 * @param {String} path32 * @return {Boolean}33 * @api private34 */3536exports.isAbsolute = function(path){37 if ('/' == path[0]) return true;38 if (':' == path[1] && '\\' == path[2]) return true;Merge pull request #1740 from superic/masterTJ Holowaychukbefore adoptione43ff07
line 39why, one step back39 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute pathreplace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1
lines 40–66why, one step back40};4142/**43 * Flatten the given `arr`.44 *45 * @param {Array} arr46 * @return {Array}47 * @api private48 */4950exports.flatten = function(arr, ret){51 var ret = ret || []52 , len = arr.length;53 for (var i = 0; i < len; ++i) {54 if (Array.isArray(arr[i])) {55 exports.flatten(arr[i], ret);56 } else {57 ret.push(arr[i]);58 }59 }60 return ret;61};6263/**64 * Normalize the given `type`, for example "html" becomes "text/html".65 *66 * @param {String} typeadd support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 67why, one step back67 * @return {Object}replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1
lines 68–71why, one step back68 * @api private69 */7071exports.normalizeType = function(type){add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 72–74why, one step back72 return ~type.indexOf('/')73 ? acceptParams(type)74 : { value: mime.lookup(type), params: {} };replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1
lines 75–88why, one step back75};7677/**78 * Normalize `types`, for example "html" becomes "text/html".79 *80 * @param {Array} types81 * @return {Array}82 * @api private83 */8485exports.normalizeTypes = function(types){86 var ret = [];8788 for (var i = 0; i < types.length; ++i) {add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 89why, one step back89 ret.push(exports.normalizeType(types[i]));replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1
lines 90–96why, one step back90 }9192 return ret;93};949596/**add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 97–98why, one step back97 * Parse accept params `str` returning an98 * object with `.value`, `.quality` and `.params`.Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 99why, one step back99 * also includes `.originalIndex` for stable sortingreplace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1
lines 100–105why, one step back100 *101 * @param {String} str102 * @return {Object}103 * @api private104 */105Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 106why, one step back106function acceptParams(str, index) {add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 107why, one step back107 var parts = str.split(/ *; */);Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 108why, one step back108 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1
line 109why, one step back109add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 110–117why, one step back110 for (var i = 1; i < parts.length; ++i) {111 var pms = parts[i].split(/ *= */);112 if ('q' == pms[0]) {113 ret.quality = parseFloat(pms[1]);114 } else {115 ret.params[pms[0]] = pms[1];116 }117 }replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1
line 118why, one step back118add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 119why, one step back119 return ret;replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1
lines 120–134why, one step back120}121122/**123 * Normalize the given path string,124 * returning a regular expression.125 *126 * An empty array should be passed,127 * which will contain the placeholder128 * key names. For example "/user/:id" will129 * then contain ["id"].130 *131 * @param {String|RegExp|Array} path132 * @param {Array} keys133 * @param {Boolean} sensitive134 * @param {Boolean} strictMerge pull request #1935 from visionmedia/router-params-middlewareRoman Shtylmanbefore adoptioncaa25b5
line 135why, one step back135 * @param {Boolean} end (whether to append $ to regex)replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1
lines 136–139why, one step back136 * @return {RegExp}137 * @api private138 */139Merge pull request #1935 from visionmedia/router-params-middlewareRoman Shtylmanbefore adoptioncaa25b5
line 140why, one step back140exports.pathRegexp = function(path, keys, sensitive, strict, end) {refactorTJ Holowaychukbefore adoptionf8a33d1
line 141why, one step back141 if (toString.call(path) == '[object RegExp]') return path;replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1
lines 142–158why, one step back142 if (Array.isArray(path)) path = '(' + path.join('|') + ')';143 path = path144 .concat(strict ? '' : '/?')145 .replace(/\/\(/g, '(?:/')146 .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g, function(_, slash, format, key, capture, optional, star){147 keys.push({ name: key, optional: !! optional });148 slash = slash || '';149 return ''150 + (optional ? '' : slash)151 + '(?:'152 + (optional ? slash : '')153 + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'154 + (optional || '')155 + (star ? '(/*)?' : '');156 })157 .replace(/([\/.])/g, '\\$1')158 .replace(/\*/g, '(.*)');Merge pull request #1935 from visionmedia/router-params-middlewareRoman Shtylmanbefore adoptioncaa25b5
line 159why, one step back159 return new RegExp('^' + path + ((end) ? '$' : ''), sensitive ? '' : 'i');replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1
line 160why, one step back160}move `connect.query()` into our repoRoman Shtylmanbefore adoption9bc63d9
lines 161–184why, one step back161162/**163 * Parse the `req` url with memoization.164 *165 * @param {ServerRequest} req166 * @return {Object}167 * @api private168 */169170exports.parseUrl = function(req){171 var parsed = req._parsedUrl;172 if (parsed && parsed.href == req.url) {173 return parsed;174 } else {175 parsed = parse(req.url);176177 if (parsed.auth && !parsed.protocol && ~parsed.href.indexOf('//')) {178 // This parses pathnames, and a strange pathname like //r@e should work179 parsed = parse(req.url.replace(/@/g, '%40'));180 }181182 return req._parsedUrl = parsed;183 }184};