Skip to content
Myr

utils.js

This file

Path

lib/utils.js

339 lines · 6.9 KiB · JavaScript

Last changed by

deprecation messages are bright red on TTYsDouglas Christopher Wilsonbefore adoptiond98e2e7

History of this file

Take it

The walk stopped after 200 commits: lines older than 2604be5 point at it.

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 1–6why, one step back
1
2/**3 * Module dependencies.4 */56var mime = require('connect').mime

Add standard deprecation utilityDouglas Christopher Wilsonbefore adoption8dc4ff2

line 7why, one step back
7 , deprecate = require('util').deprecate

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 8–10why, one step back
8 , crc32 = require('buffer-crc32');
910/**

refactorTJ Holowaychukbefore adoptionf8a33d1

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

Add standard deprecation utilityDouglas Christopher Wilsonbefore adoption8dc4ff2

lines 15–16why, one step back
15
16/**

deprecation messages are bright red on TTYsDouglas Christopher Wilsonbefore adoptiond98e2e7

lines 17–18why, one step back
17 * Deprecate function, like core `util.deprecate`,
18 * but with NODE_ENV and color support.

Add standard deprecation utilityDouglas Christopher Wilsonbefore adoption8dc4ff2

lines 19–26why, one step back
19 *
20 * @param {Function} fn21 * @param {String} msg22 * @return {Function}23 * @api private24 */2526exports.deprecate = function(fn, msg){

deprecation messages are bright red on TTYsDouglas Christopher Wilsonbefore adoptiond98e2e7

lines 27–37why, one step back
27 if (process.env.NODE_ENV === 'test') return fn;
2829 // prepend module name30 msg = 'express: ' + msg;3132 if (process.stderr.isTTY) {33 // colorize34 msg = '\x1b[31;1m' + msg + '\x1b[0m';35 }3637 return deprecate(fn, msg);

Add standard deprecation utilityDouglas Christopher Wilsonbefore adoption8dc4ff2

line 38why, one step back
38};

refactorTJ Holowaychukbefore adoptionf8a33d1

lines 39–40why, one step back
39
40/**

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 41–61why, one step back
41 * Return ETag for `body`.
42 *43 * @param {String|Buffer} body44 * @return {String}45 * @api private46 */4748exports.etag = function(body){49 return '"' + crc32.signed(body) + '"';50};5152/**53 * Make `locals()` bound to the given `obj`.54 *55 * This is used for `app.locals` and `res.locals`.56 *57 * @param {Object} obj58 * @return {Function}59 * @api private60 */61

lint: remove unused stuffJonathan Ongbefore adoption9bed2b8

line 62why, one step back
62exports.locals = function(){

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 63–81why, one step back
63 function locals(obj){
64 for (var key in obj) locals[key] = obj[key];65 return obj;66 };6768 return locals;69};7071/**72 * Check if `path` looks absolute.73 *74 * @param {String} path75 * @return {Boolean}76 * @api private77 */7879exports.isAbsolute = function(path){80 if ('/' == path[0]) return true;81 if (':' == path[1] && '\\' == path[2]) return true;

Merge pull request #1740 from superic/masterTJ Holowaychukbefore adoptione43ff07

line 82why, one step back
82 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute path

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 83–109why, one step back
83};
8485/**86 * Flatten the given `arr`.87 *88 * @param {Array} arr89 * @return {Array}90 * @api private91 */9293exports.flatten = function(arr, ret){94 var ret = ret || []95 , len = arr.length;96 for (var i = 0; i < len; ++i) {97 if (Array.isArray(arr[i])) {98 exports.flatten(arr[i], ret);99 } else {100 ret.push(arr[i]);101 }102 }103 return ret;104};105106/**107 * Normalize the given `type`, for example "html" becomes "text/html".108 *109 * @param {String} type

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

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

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 111–114why, one step back
111 * @api private
112 */113114exports.normalizeType = function(type){

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

lines 115–117why, one step back
115 return ~type.indexOf('/')
116 ? acceptParams(type)117 : { value: mime.lookup(type), params: {} };

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 118–131why, one step back
118};
119120/**121 * Normalize `types`, for example "html" becomes "text/html".122 *123 * @param {Array} types124 * @return {Array}125 * @api private126 */127128exports.normalizeTypes = function(types){129 var ret = [];130131 for (var i = 0; i < types.length; ++i) {

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 132why, one step back
132 ret.push(exports.normalizeType(types[i]));

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 133–157why, one step back
133 }
134135 return ret;136};137138/**139 * Return the acceptable type in `types`, if any.140 *141 * @param {Array} types142 * @param {String} str143 * @return {String}144 * @api private145 */146147exports.acceptsArray = function(types, str){148 // accept anything when Accept is not present149 if (!str) return types[0];150151 // parse152 var accepted = exports.parseAccept(str)153 , normalized = exports.normalizeTypes(types)154 , len = accepted.length;155156 for (var i = 0; i < len; ++i) {157 for (var j = 0, jlen = types.length; j < jlen; ++j) {

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 158why, one step back
158 if (exports.accept(normalized[j], accepted[i])) {

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 159–182why, one step back
159 return types[j];
160 }161 }162 }163};164165/**166 * Check if `type(s)` are acceptable based on167 * the given `str`.168 *169 * @param {String|Array} type(s)170 * @param {String} str171 * @return {Boolean|String}172 * @api private173 */174175exports.accepts = function(type, str){176 if ('string' == typeof type) type = type.split(/ *, */);177 return exports.acceptsArray(type, str);178};179180/**181 * Check if `type` array is acceptable for `other`.182 *

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 183why, one step back
183 * @param {Object} type

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 184–189why, one step back
184 * @param {Object} other
185 * @return {Boolean}186 * @api private187 */188189exports.accept = function(type, other){

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

lines 190–193why, one step back
190 var t = type.value.split('/');
191 return (t[0] == other.type || '*' == other.type)192 && (t[1] == other.subtype || '*' == other.subtype)193 && paramsEqual(type.params, other.params);

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 194–196why, one step back
194};
195196/**

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

lines 197–201why, one step back
197 * Check if accept params are equal.
198 *199 * @param {Object} a200 * @param {Object} b201 * @return {Boolean}

ocdTJ Holowaychukbefore adoption53b8e25

line 202why, one step back
202 * @api private

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 203why, one step back
203 */

ocdTJ Holowaychukbefore adoption53b8e25

line 204why, one step back
204

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

lines 205–211why, one step back
205function paramsEqual(a, b){
206 return !Object.keys(a).some(function(k) {207 return a[k] != b[k];208 });209}210211/**

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 212–224why, one step back
212 * Parse accept `str`, returning
213 * an array objects containing214 * `.type` and `.subtype` along215 * with the values provided by216 * `parseQuality()`.217 *218 * @param {Type} name219 * @return {Type}220 * @api private221 */222223exports.parseAccept = function(str){224 return exports

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 225why, one step back
225 .parseParams(str)

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 226–235why, one step back
226 .map(function(obj){
227 var parts = obj.value.split('/');228 obj.type = parts[0];229 obj.subtype = parts[1];230 return obj;231 });232};233234/**235 * Parse quality `str`, returning an

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

lines 236–237why, one step back
236 * array of objects with `.value`,
237 * `.quality` and optional `.params`

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

line 238why, one step back
238 *

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

lines 239–240why, one step back
239 * @param {String} str
240 * @return {Array}

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 241–243why, one step back
241 * @api private
242 */243

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 244why, one step back
244exports.parseParams = function(str){

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 245–246why, one step back
245 return str
246 .split(/ *, */)

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 247why, one step back
247 .map(acceptParams)

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 248–251why, one step back
248 .filter(function(obj){
249 return obj.quality;250 })251 .sort(function(a, b){

Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30

lines 252–256why, one step back
252 if (a.quality === b.quality) {
253 return a.originalIndex - b.originalIndex;254 } else {255 return b.quality - a.quality;256 }

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 257–260why, one step back
257 });
258};259260/**

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

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

Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30

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

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 264–269why, one step back
264 *
265 * @param {String} str266 * @return {Object}267 * @api private268 */269

Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30

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

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

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

Merge pull request #1578 from Notificare/masterTJ Holowaychukbefore adoption5ba6c30

line 272why, one step back
272 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

line 273why, one step back
273

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

lines 274–281why, one step back
274 for (var i = 1; i < parts.length; ++i) {
275 var pms = parts[i].split(/ *= */);276 if ('q' == pms[0]) {277 ret.quality = parseFloat(pms[1]);278 } else {279 ret.params[pms[0]] = pms[1];280 }281 }

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

line 282why, one step back
282

add support for custom Accept parametersPavel Brylovbefore adoption9e684d4

line 283why, one step back
283 return ret;

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 284–319why, one step back
284}
285286/**287 * Escape special characters in the given string of html.288 *289 * @param {String} html290 * @return {String}291 * @api private292 */293294exports.escape = function(html) {295 return String(html)296 .replace(/&/g, '&amp;')297 .replace(/"/g, '&quot;')298 .replace(/</g, '&lt;')299 .replace(/>/g, '&gt;');300};301302/**303 * Normalize the given path string,304 * returning a regular expression.305 *306 * An empty array should be passed,307 * which will contain the placeholder308 * key names. For example "/user/:id" will309 * then contain ["id"].310 *311 * @param {String|RegExp|Array} path312 * @param {Array} keys313 * @param {Boolean} sensitive314 * @param {Boolean} strict315 * @return {RegExp}316 * @api private317 */318319exports.pathRegexp = function(path, keys, sensitive, strict) {

refactorTJ Holowaychukbefore adoptionf8a33d1

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

Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption2604be5

lines 321–339why, one step back
321 if (Array.isArray(path)) path = '(' + path.join('|') + ')';
322 path = path323 .concat(strict ? '' : '/?')324 .replace(/\/\(/g, '(?:/')325 .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?(\*)?/g, function(_, slash, format, key, capture, optional, star){326 keys.push({ name: key, optional: !! optional });327 slash = slash || '';328 return ''329 + (optional ? '' : slash)330 + '(?:'331 + (optional ? slash : '')332 + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')'333 + (optional || '')334 + (star ? '(/*)?' : '');335 })336 .replace(/([\/.])/g, '\\$1')337 .replace(/\*/g, '(.*)');338 return new RegExp('^' + path + '$', sensitive ? '' : 'i');339}