Skip to content
Myr

utils.js

This file

Path

lib/utils.js

183 lines · 3.9 KiB · JavaScript

Last changed by

use local copy of parseUrlRoman Shtylmanbefore adoption6a7363e

History of this file

Take 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–6why, one step back
1
2/**3 * Module dependencies.4 */56var mime = require('connect').mime

use local copy of parseUrlRoman Shtylmanbefore adoption6a7363e

lines 7–8why, one step back
7 , crc32 = require('buffer-crc32')
8 , parse = require('url').parse;

replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1

lines 9–10why, one step back
9
10/**

refactorTJ Holowaychukbefore adoptionf8a33d1

lines 11–16why, one step back
11 * toString ref.
12 */1314var toString = {}.toString;1516/**

replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1

lines 17–38why, one step back
17 * 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 back
39 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute path

replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1

lines 40–66why, one step back
40};
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} type

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 67why, one step back
67 * @return {Object}

replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1

lines 68–71why, one step back
68 * @api private
69 */7071exports.normalizeType = function(type){

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

lines 72–74why, one step back
72 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 back
75};
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 back
89 ret.push(exports.normalizeType(types[i]));

replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1

lines 90–96why, one step back
90 }
9192 return ret;93};949596/**

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

lines 97–98why, one step back
97 * Parse accept params `str` returning an
98 * object with `.value`, `.quality` and `.params`.

Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30

line 99why, one step back
99 * also includes `.originalIndex` for stable sorting

replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1

lines 100–105why, one step back
100 *
101 * @param {String} str102 * @return {Object}103 * @api private104 */105

Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30

line 106why, one step back
106function acceptParams(str, index) {

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 107why, one step back
107 var parts = str.split(/ *; */);

Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30

line 108why, one step back
108 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 back
109

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

lines 110–117why, one step back
110 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 back
118

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 119why, one step back
119 return ret;

replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1

lines 120–139why, one step back
120}
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} strict135 * @return {RegExp}136 * @api private137 */138139exports.pathRegexp = function(path, keys, sensitive, strict) {

refactorTJ Holowaychukbefore adoptionf8a33d1

line 140why, one step back
140 if (toString.call(path) == '[object RegExp]') return path;

replace 0.6.x travis with 0.10.xTJ Holowaychukbefore adoption916acd1

lines 141–159why, one step back
141 if (Array.isArray(path)) path = '(' + path.join('|') + ')';
142 path = path143 .concat(strict ? '' : '/?')144 .replace(/\/\(/g, '(?:/')145 .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g, function(_, slash, format, key, capture, optional, star){146 keys.push({ name: key, optional: !! optional });147 slash = slash || '';148 return ''149 + (optional ? '' : slash)150 + '(?:'151 + (optional ? slash : '')152 + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'153 + (optional || '')154 + (star ? '(/*)?' : '');155 })156 .replace(/([\/.])/g, '\\$1')157 .replace(/\*/g, '(.*)');158 return new RegExp('^' + path + '$', sensitive ? '' : 'i');159}

move `connect.query()` into our repoRoman Shtylmanbefore adoption9bc63d9

lines 160–183why, one step back
160
161/**162 * Parse the `req` url with memoization.163 *164 * @param {ServerRequest} req165 * @return {Object}166 * @api private167 */168169exports.parseUrl = function(req){170 var parsed = req._parsedUrl;171 if (parsed && parsed.href == req.url) {172 return parsed;173 } else {174 parsed = parse(req.url);175176 if (parsed.auth && !parsed.protocol && ~parsed.href.indexOf('//')) {177 // This parses pathnames, and a strange pathname like //r@e should work178 parsed = parse(req.url.replace(/@/g, '%40'));179 }180181 return req._parsedUrl = parsed;182 }183};