utils.js
This file
Path
lib/utils.js
313 lines · 6.3 KiB · JavaScript
Last changed by
refactorTJ Holowaychukbefore adoptionf8a33d1
History of this fileTake it
The walk stopped after 200 commits: lines older than 0f20a5e point at it.
add CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 1–5why, one step back12/**3 * Module dependencies.4 */5add etag utilTJ Holowaychukbefore adoptionf2f0976
line 6why, one step back6var mime = require('connect').mimemergeTJ Holowaychukbefore adoptione2ad0d3
line 7why, one step back7 , crc32 = require('buffer-crc32');add etag utilTJ Holowaychukbefore adoptionf2f0976
lines 8–9why, one step back89/**refactorTJ Holowaychukbefore adoptionf8a33d1
lines 10–15why, one step back10 * toString ref.11 */1213var toString = {}.toString;1415/**add etag utilTJ Holowaychukbefore adoptionf2f0976
lines 16–23why, one step back16 * Return ETag for `body`.17 *18 * @param {String|Buffer} body19 * @return {String}20 * @api private21 */2223exports.etag = function(body){mergeTJ Holowaychukbefore adoptione2ad0d3
line 24why, one step back24 return '"' + crc32.signed(body) + '"';add etag utilTJ Holowaychukbefore adoptionf2f0976
line 25why, one step back25};add CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 26–28why, one step back2627/**28 * Make `locals()` bound to the given `obj`.Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptionc6d7352
lines 29–30why, one step back29 *30 * This is used for `app.locals` and `res.locals`.add CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 31–83why, one step back31 *32 * @param {Object} obj33 * @return {Function}34 * @api private35 */3637exports.locals = function(obj){38 function locals(obj){39 for (var key in obj) locals[key] = obj[key];40 return obj;41 };4243 return locals;44};4546/**47 * Check if `path` looks absolute.48 *49 * @param {String} path50 * @return {Boolean}51 * @api private52 */5354exports.isAbsolute = function(path){55 if ('/' == path[0]) return true;56 if (':' == path[1] && '\\' == path[2]) return true;57};5859/**60 * Flatten the given `arr`.61 *62 * @param {Array} arr63 * @return {Array}64 * @api private65 */6667exports.flatten = function(arr, ret){68 var ret = ret || []69 , len = arr.length;70 for (var i = 0; i < len; ++i) {71 if (Array.isArray(arr[i])) {72 exports.flatten(arr[i], ret);73 } else {74 ret.push(arr[i]);75 }76 }77 return ret;78};7980/**81 * Normalize the given `type`, for example "html" becomes "text/html".82 *83 * @param {String} typeadd support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 84why, one step back84 * @return {Object}add CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 85–88why, one step back85 * @api private86 */8788exports.normalizeType = function(type){add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 89–91why, one step back89 return ~type.indexOf('/')90 ? acceptParams(type)91 : { value: mime.lookup(type), params: {} };add CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 92–105why, one step back92};9394/**95 * Normalize `types`, for example "html" becomes "text/html".96 *97 * @param {Array} types98 * @return {Array}99 * @api private100 */101102exports.normalizeTypes = function(types){103 var ret = [];104105 for (var i = 0; i < types.length; ++i) {add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 106why, one step back106 ret.push(exports.normalizeType(types[i]));add CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 107–131why, one step back107 }108109 return ret;110};111112/**113 * Return the acceptable type in `types`, if any.114 *115 * @param {Array} types116 * @param {String} str117 * @return {String}118 * @api private119 */120121exports.acceptsArray = function(types, str){122 // accept anything when Accept is not present123 if (!str) return types[0];124125 // parse126 var accepted = exports.parseAccept(str)127 , normalized = exports.normalizeTypes(types)128 , len = accepted.length;129130 for (var i = 0; i < len; ++i) {131 for (var j = 0, jlen = types.length; j < jlen; ++j) {add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 132why, one step back132 if (exports.accept(normalized[j], accepted[i])) {add CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 133–156why, one step back133 return types[j];134 }135 }136 }137};138139/**140 * Check if `type(s)` are acceptable based on141 * the given `str`.142 *143 * @param {String|Array} type(s)144 * @param {String} str145 * @return {Boolean|String}146 * @api private147 */148149exports.accepts = function(type, str){150 if ('string' == typeof type) type = type.split(/ *, */);151 return exports.acceptsArray(type, str);152};153154/**155 * Check if `type` array is acceptable for `other`.156 *add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 157why, one step back157 * @param {Object} typeadd CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 158–163why, one step back158 * @param {Object} other159 * @return {Boolean}160 * @api private161 */162163exports.accept = function(type, other){add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 164–167why, one step back164 var t = type.value.split('/');165 return (t[0] == other.type || '*' == other.type)166 && (t[1] == other.subtype || '*' == other.subtype)167 && paramsEqual(type.params, other.params);add CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 168–170why, one step back168};169170/**add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 171–175why, one step back171 * Check if accept params are equal.172 *173 * @param {Object} a174 * @param {Object} b175 * @return {Boolean}ocdTJ Holowaychukbefore adoption53b8e25
line 176why, one step back176 * @api privateadd support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 177why, one step back177 */ocdTJ Holowaychukbefore adoption53b8e25
line 178why, one step back178add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 179–185why, one step back179function paramsEqual(a, b){180 return !Object.keys(a).some(function(k) {181 return a[k] != b[k];182 });183}184185/**add CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 186–198why, one step back186 * Parse accept `str`, returning187 * an array objects containing188 * `.type` and `.subtype` along189 * with the values provided by190 * `parseQuality()`.191 *192 * @param {Type} name193 * @return {Type}194 * @api private195 */196197exports.parseAccept = function(str){198 return exportsadd support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 199why, one step back199 .parseParams(str)add CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 200–209why, one step back200 .map(function(obj){201 var parts = obj.value.split('/');202 obj.type = parts[0];203 obj.subtype = parts[1];204 return obj;205 });206};207208/**209 * Parse quality `str`, returning anadd support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 210–211why, one step back210 * array of objects with `.value`,211 * `.quality` and optional `.params`add CORS exampleTJ Holowaychukbefore adoption0f20a5e
line 212why, one step back212 *add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 213–214why, one step back213 * @param {String} str214 * @return {Array}add CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 215–217why, one step back215 * @api private216 */217add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 218why, one step back218exports.parseParams = function(str){add CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 219–220why, one step back219 return str220 .split(/ *, */)add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 221why, one step back221 .map(acceptParams)add CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 222–225why, one step back222 .filter(function(obj){223 return obj.quality;224 })225 .sort(function(a, b){Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
lines 226–230why, one step back226 if (a.quality === b.quality) {227 return a.originalIndex - b.originalIndex;228 } else {229 return b.quality - a.quality;230 }add CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 231–234why, one step back231 });232};233234/**add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 235–236why, one step back235 * Parse accept params `str` returning an236 * object with `.value`, `.quality` and `.params`.Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 237why, one step back237 * also includes `.originalIndex` for stable sortingadd CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 238–243why, one step back238 *239 * @param {String} str240 * @return {Object}241 * @api private242 */243Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 244why, one step back244function acceptParams(str, index) {add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 245why, one step back245 var parts = str.split(/ *; */);Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30
line 246why, one step back246 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };add CORS exampleTJ Holowaychukbefore adoption0f20a5e
line 247why, one step back247add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
lines 248–255why, one step back248 for (var i = 1; i < parts.length; ++i) {249 var pms = parts[i].split(/ *= */);250 if ('q' == pms[0]) {251 ret.quality = parseFloat(pms[1]);252 } else {253 ret.params[pms[0]] = pms[1];254 }255 }add CORS exampleTJ Holowaychukbefore adoption0f20a5e
line 256why, one step back256add support for custom Accept parametersPavel Brylovbefore adoption9e684d4
line 257why, one step back257 return ret;add CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 258–293why, one step back258}259260/**261 * Escape special characters in the given string of html.262 *263 * @param {String} html264 * @return {String}265 * @api private266 */267268exports.escape = function(html) {269 return String(html)270 .replace(/&/g, '&')271 .replace(/"/g, '"')272 .replace(/</g, '<')273 .replace(/>/g, '>');274};275276/**277 * Normalize the given path string,278 * returning a regular expression.279 *280 * An empty array should be passed,281 * which will contain the placeholder282 * key names. For example "/user/:id" will283 * then contain ["id"].284 *285 * @param {String|RegExp|Array} path286 * @param {Array} keys287 * @param {Boolean} sensitive288 * @param {Boolean} strict289 * @return {RegExp}290 * @api private291 */292293exports.pathRegexp = function(path, keys, sensitive, strict) {refactorTJ Holowaychukbefore adoptionf8a33d1
line 294why, one step back294 if (toString.call(path) == '[object RegExp]') return path;add CORS exampleTJ Holowaychukbefore adoption0f20a5e
lines 295–313why, one step back295 if (Array.isArray(path)) path = '(' + path.join('|') + ')';296 path = path297 .concat(strict ? '' : '/?')298 .replace(/\/\(/g, '(?:/')299 .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g, function(_, slash, format, key, capture, optional, star){300 keys.push({ name: key, optional: !! optional });301 slash = slash || '';302 return ''303 + (optional ? '' : slash)304 + '(?:'305 + (optional ? slash : '')306 + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'307 + (optional || '')308 + (star ? '(/*)?' : '');309 })310 .replace(/([\/.])/g, '\\$1')311 .replace(/\*/g, '(.*)');312 return new RegExp('^' + path + '$', sensitive ? '' : 'i');313}