utils.js
This file
Path
lib/utils.js
111 lines · 2.1 KiB · JavaScript
Last changed by
Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874
History of this fileTake it
The walk stopped after 200 commits: lines older than f976625 point at it.
Release 3.2.4TJ Holowaychukbefore adoptionf976625
lines 1–4why, one step back1/**2 * Module dependencies.3 */4remove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08
lines 5–6why, one step back5var mime = require('send').mime;6var crc32 = require('buffer-crc32');Release 3.2.4TJ Holowaychukbefore adoptionf976625
lines 7–30why, one step back78/**9 * Return ETag for `body`.10 *11 * @param {String|Buffer} body12 * @return {String}13 * @api private14 */1516exports.etag = function(body){17 return '"' + crc32.signed(body) + '"';18};1920/**21 * Check if `path` looks absolute.22 *23 * @param {String} path24 * @return {Boolean}25 * @api private26 */2728exports.isAbsolute = function(path){29 if ('/' == path[0]) return true;30 if (':' == path[1] && '\\' == path[2]) return true;Merge pull request #1740 from superic/masterTJ Holowaychukbefore adoptione43ff07
line 31why, one step back31 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute pathRelease 3.2.4TJ Holowaychukbefore adoptionf976625
lines 32–42why, one step back32};3334/**35 * Flatten the given `arr`.36 *37 * @param {Array} arr38 * @return {Array}39 * @api private40 */4142exports.flatten = function(arr, ret){Merge pull request #2004 from agchou/clean-up-codeJonathan Ongbefore adoption0120874
lines 43–44why, one step back43 ret = ret || [];44 var len = arr.length;Release 3.2.4TJ Holowaychukbefore adoptionf976625
lines 45–111why, one step back45 for (var i = 0; i < len; ++i) {46 if (Array.isArray(arr[i])) {47 exports.flatten(arr[i], ret);48 } else {49 ret.push(arr[i]);50 }51 }52 return ret;53};5455/**56 * Normalize the given `type`, for example "html" becomes "text/html".57 *58 * @param {String} type59 * @return {Object}60 * @api private61 */6263exports.normalizeType = function(type){64 return ~type.indexOf('/')65 ? acceptParams(type)66 : { value: mime.lookup(type), params: {} };67};6869/**70 * Normalize `types`, for example "html" becomes "text/html".71 *72 * @param {Array} types73 * @return {Array}74 * @api private75 */7677exports.normalizeTypes = function(types){78 var ret = [];7980 for (var i = 0; i < types.length; ++i) {81 ret.push(exports.normalizeType(types[i]));82 }8384 return ret;85};8687/**88 * Parse accept params `str` returning an89 * object with `.value`, `.quality` and `.params`.90 * also includes `.originalIndex` for stable sorting91 *92 * @param {String} str93 * @return {Object}94 * @api private95 */9697function acceptParams(str, index) {98 var parts = str.split(/ *; */);99 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };100101 for (var i = 1; i < parts.length; ++i) {102 var pms = parts[i].split(/ *= */);103 if ('q' == pms[0]) {104 ret.quality = parseFloat(pms[1]);105 } else {106 ret.params[pms[0]] = pms[1];107 }108 }109110 return ret;111}