utils.js
This file
Path
lib/utils.js
284 lines · 5.6 KiB · JavaScript
Last changed by
add etag utilTJ Holowaychukbefore adoptionf2f0976
History of this fileTake it
The walk stopped after 200 commits: lines older than 754dd32 point at it.
.npmignore lib-covTJ Holowaychukbefore adoption754dd32
lines 1–5why, one step back12/**3 * Module dependencies.4 */5add etag utilTJ Holowaychukbefore adoptionf2f0976
lines 6–21why, one step back6var mime = require('connect').mime7 , crc = require('crc');89/**10 * Return ETag for `body`.11 *12 * @param {String|Buffer} body13 * @return {String}14 * @api private15 */1617exports.etag = function(body){18 return Buffer.isBuffer(body)19 ? crc.buffer.crc32(body)20 : crc.crc32(body);21};.npmignore lib-covTJ Holowaychukbefore adoption754dd32
lines 22–23why, one step back2223/**Added res.locals.use(). Closes #1120TJ Holowaychukbefore adoptiondd33ef2
lines 24–44why, one step back24 * Make `locals()` bound to the given `obj`.25 * 26 * This is used for `app.locals` and `res.locals`. 27 *28 * @param {Object} obj29 * @return {Function}30 * @api private31 */3233exports.locals = function(obj){34 obj.viewCallbacks = obj.viewCallbacks || [];3536 function locals(obj){37 for (var key in obj) locals[key] = obj[key];38 return obj;39 };4041 return locals;42};4344/**.npmignore lib-covTJ Holowaychukbefore adoption754dd32
lines 45–265why, one step back45 * Check if `path` looks absolute.46 *47 * @param {String} path48 * @return {Boolean}49 * @api private50 */5152exports.isAbsolute = function(path){53 if ('/' == path[0]) return true;54 if (':' == path[1] && '\\' == path[2]) return true;55};5657/**58 * Flatten the given `arr`.59 *60 * @param {Array} arr61 * @return {Array}62 * @api private63 */6465exports.flatten = function(arr, ret){66 var ret = ret || []67 , len = arr.length;68 for (var i = 0; i < len; ++i) {69 if (Array.isArray(arr[i])) {70 exports.flatten(arr[i], ret);71 } else {72 ret.push(arr[i]);73 }74 }75 return ret;76};7778/**79 * Normalize the given `type`, for example "html" becomes "text/html".80 *81 * @param {String} type82 * @return {String}83 * @api private84 */8586exports.normalizeType = function(type){87 return ~type.indexOf('/') ? type : mime.lookup(type);88};8990/**91 * Normalize `types`, for example "html" becomes "text/html".92 *93 * @param {Array} types94 * @return {Array}95 * @api private96 */9798exports.normalizeTypes = function(types){99 var ret = [];100101 for (var i = 0; i < types.length; ++i) {102 ret.push(~types[i].indexOf('/')103 ? types[i]104 : mime.lookup(types[i]));105 }106107 return ret;108};109110/**111 * Return the acceptable type in `types`, if any.112 *113 * @param {Array} types114 * @param {String} str115 * @return {String}116 * @api private117 */118119exports.acceptsArray = function(types, str){120 // accept anything when Accept is not present121 if (!str) return types[0];122123 // parse124 var accepted = exports.parseAccept(str)125 , normalized = exports.normalizeTypes(types)126 , len = accepted.length;127128 for (var i = 0; i < len; ++i) {129 for (var j = 0, jlen = types.length; j < jlen; ++j) {130 if (exports.accept(normalized[j].split('/'), accepted[i])) {131 return types[j];132 }133 }134 }135};136137/**138 * Check if `type(s)` are acceptable based on139 * the given `str`.140 *141 * @param {String|Array} type(s)142 * @param {String} str143 * @return {Boolean|String}144 * @api private145 */146147exports.accepts = function(type, str){148 if ('string' == typeof type) type = type.split(/ *, */);149 return exports.acceptsArray(type, str);150};151152/**153 * Check if `type` array is acceptable for `other`.154 *155 * @param {Array} type156 * @param {Object} other157 * @return {Boolean}158 * @api private159 */160161exports.accept = function(type, other){162 return (type[0] == other.type || '*' == other.type)163 && (type[1] == other.subtype || '*' == other.subtype);164};165166/**167 * Parse accept `str`, returning168 * an array objects containing169 * `.type` and `.subtype` along170 * with the values provided by171 * `parseQuality()`.172 *173 * @param {Type} name174 * @return {Type}175 * @api private176 */177178exports.parseAccept = function(str){179 return exports180 .parseQuality(str)181 .map(function(obj){182 var parts = obj.value.split('/');183 obj.type = parts[0];184 obj.subtype = parts[1];185 return obj;186 });187};188189/**190 * Parse quality `str`, returning an191 * array of objects with `.value` and192 * `.quality`.193 *194 * @param {Type} name195 * @return {Type}196 * @api private197 */198199exports.parseQuality = function(str){200 return str201 .split(/ *, */)202 .map(quality)203 .filter(function(obj){204 return obj.quality;205 })206 .sort(function(a, b){207 return b.quality - a.quality;208 });209};210211/**212 * Parse quality `str` returning an213 * object with `.value` and `.quality`.214 *215 * @param {String} str216 * @return {Object}217 * @api private218 */219220function quality(str) {221 var parts = str.split(/ *; */)222 , val = parts[0];223224 var q = parts[1]225 ? parseFloat(parts[1].split(/ *= */)[1])226 : 1;227228 return { value: val, quality: q };229}230231/**232 * Escape special characters in the given string of html.233 *234 * @param {String} html235 * @return {String}236 * @api private237 */238239exports.escape = function(html) {240 return String(html)241 .replace(/&/g, '&')242 .replace(/"/g, '"')243 .replace(/</g, '<')244 .replace(/>/g, '>');245};246247/**248 * Normalize the given path string,249 * returning a regular expression.250 *251 * An empty array should be passed,252 * which will contain the placeholder253 * key names. For example "/user/:id" will254 * then contain ["id"].255 *256 * @param {String|RegExp|Array} path257 * @param {Array} keys258 * @param {Boolean} sensitive259 * @param {Boolean} strict260 * @return {RegExp}261 * @api private262 */263264exports.pathRegexp = function(path, keys, sensitive, strict) {265 if (path instanceof RegExp) return path;ocdTJ Holowaychukbefore adoption53f1ffb
line 266why, one step back266 if (Array.isArray(path)) path = '(' + path.join('|') + ')';.npmignore lib-covTJ Holowaychukbefore adoption754dd32
lines 267–269why, one step back267 path = path268 .concat(strict ? '' : '/?')269 .replace(/\/\(/g, '(?:/')* immediately after :param would become not greedyriadhbefore adoption73bed61
line 270why, one step back270 .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g, function(_, slash, format, key, capture, optional, star){.npmignore lib-covTJ Holowaychukbefore adoption754dd32
lines 271–277why, one step back271 keys.push({ name: key, optional: !! optional });272 slash = slash || '';273 return ''274 + (optional ? '' : slash)275 + '(?:'276 + (optional ? slash : '')277 + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'* immediately after :param would become not greedyriadhbefore adoption73bed61
line 278why, one step back278 + (optional || '')tests for issues 1115 fix for utilsriadhbefore adoption3827f5e
line 279why, one step back279 + (star ? '(/*)?' : '');.npmignore lib-covTJ Holowaychukbefore adoption754dd32
lines 280–284why, one step back280 })281 .replace(/([\/.])/g, '\\$1')282 .replace(/\*/g, '(.*)');283 return new RegExp('^' + path + '$', sensitive ? '' : 'i');284}