utils.js
This file
Path
lib/utils.js
251 lines · 4.6 KiB · JavaScript
Last changed by
Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
History of this fileTake it
The walk stopped after 200 commits: lines older than 3b7ca43 point at it.
Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 1–7why, one step back1/*!2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2014-2015 Douglas Christopher Wilson5 * MIT Licensed6 */7Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 8–9why, one step back8'use strict';9deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 10–11why, one step back10/**11 * Module dependencies.Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 12why, one step back12 * @api privatedeps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 13–14why, one step back13 */14Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 15why, one step back15var Buffer = require('safe-buffer').BufferMerge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 16why, one step back16var contentType = require('content-type');Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
line 17why, one step back17var etag = require('etag');Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 18why, one step back18var mime = require('mime-types')deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 19–31why, one step back19var proxyaddr = require('proxy-addr');20var qs = require('qs');21var querystring = require('querystring');2223/**24 * Return strong ETag for `body`.25 *26 * @param {String|Buffer} body27 * @param {String} [encoding]28 * @return {String}29 * @api private30 */31Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 32why, one step back32exports.etag = createETagGenerator({ weak: false })deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 33–42why, one step back3334/**35 * Return weak ETag for `body`.36 *37 * @param {String|Buffer} body38 * @param {String} [encoding]39 * @return {String}40 * @api private41 */42Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 43why, one step back43exports.wetag = createETagGenerator({ weak: true })deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 44–55why, one step back4445/**46 * Normalize the given `type`, for example "html" becomes "text/html".47 *48 * @param {String} type49 * @return {Object}50 * @api private51 */5253exports.normalizeType = function(type){54 return ~type.indexOf('/')55 ? acceptParams(type)Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 56why, one step back56 : { value: (mime.lookup(type) || 'application/octet-stream'), params: {} }deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 57–85why, one step back57};5859/**60 * Normalize `types`, for example "html" becomes "text/html".61 *62 * @param {Array} types63 * @return {Array}64 * @api private65 */6667exports.normalizeTypes = function(types){68 var ret = [];6970 for (var i = 0; i < types.length; ++i) {71 ret.push(exports.normalizeType(types[i]));72 }7374 return ret;75};7677/**78 * Parse accept params `str` returning an79 * object with `.value`, `.quality` and `.params`.80 *81 * @param {String} str82 * @return {Object}83 * @api private84 */85Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 86why, one step back86function acceptParams (str) {deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
line 87why, one step back87 var parts = str.split(/ *; */);Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 88why, one step back88 var ret = { value: parts[0], quality: 1, params: {} }deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 89–91why, one step back8990 for (var i = 1; i < parts.length; ++i) {91 var pms = parts[i].split(/ *= */);Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 92why, one step back92 if ('q' === pms[0]) {deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 93–118why, one step back93 ret.quality = parseFloat(pms[1]);94 } else {95 ret.params[pms[0]] = pms[1];96 }97 }9899 return ret;100}101102/**103 * Compile "etag" value to function.104 *105 * @param {Boolean|String|Function} val106 * @return {Function}107 * @api private108 */109110exports.compileETag = function(val) {111 var fn;112113 if (typeof val === 'function') {114 return val;115 }116117 switch (val) {118 case true:Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 119why, one step back119 case 'weak':deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 120–150why, one step back120 fn = exports.wetag;121 break;122 case false:123 break;124 case 'strong':125 fn = exports.etag;126 break;127 default:128 throw new TypeError('unknown value for etag function: ' + val);129 }130131 return fn;132}133134/**135 * Compile "query parser" value to function.136 *137 * @param {String|Function} val138 * @return {Function}139 * @api private140 */141142exports.compileQueryParser = function compileQueryParser(val) {143 var fn;144145 if (typeof val === 'function') {146 return val;147 }148149 switch (val) {150 case true:Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 151why, one step back151 case 'simple':deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 152–156why, one step back152 fn = querystring.parse;153 break;154 case false:155 break;156 case 'extended':Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 157why, one step back157 fn = parseExtendedQueryString;deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 158–188why, one step back158 break;159 default:160 throw new TypeError('unknown value for query parser function: ' + val);161 }162163 return fn;164}165166/**167 * Compile "proxy trust" value to function.168 *169 * @param {Boolean|String|Number|Array|Function} val170 * @return {Function}171 * @api private172 */173174exports.compileTrust = function(val) {175 if (typeof val === 'function') return val;176177 if (val === true) {178 // Support plain true/false179 return function(){ return true };180 }181182 if (typeof val === 'number') {183 // Support trusting hop count184 return function(a, i){ return i < val };185 }186187 if (typeof val === 'string') {188 // Support comma-separated valuesMerge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 189–190why, one step back189 val = val.split(',')190 .map(function (v) { return v.trim() })deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 191–204why, one step back191 }192193 return proxyaddr.compile(val || []);194}195196/**197 * Set the charset in a given Content-Type string.198 *199 * @param {String} type200 * @param {String} charset201 * @return {String}202 * @api private203 */204Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 205–208why, one step back205exports.setCharset = function setCharset(type, charset) {206 if (!type || !charset) {207 return type;208 }deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 209–210why, one step back209210 // parse typeMerge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 211why, one step back211 var parsed = contentType.parse(type);deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 212–216why, one step back212213 // set charset214 parsed.parameters.charset = charset;215216 // format typeMerge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 217why, one step back217 return contentType.format(parsed);deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
line 218why, one step back218};Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 219–237why, one step back219220/**221 * Create an ETag generator function, generating ETags with222 * the given options.223 *224 * @param {object} options225 * @return {function}226 * @private227 */228229function createETagGenerator (options) {230 return function generateETag (body, encoding) {231 var buf = !Buffer.isBuffer(body)232 ? Buffer.from(body, encoding)233 : body234235 return etag(buf, options)236 }237}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 238–241why, one step back238239/**240 * Parse an extended query string with qs.241 *Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 242why, one step back242 * @param {String} strMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 243–251why, one step back243 * @return {Object}244 * @private245 */246247function parseExtendedQueryString(str) {248 return qs.parse(str, {249 allowPrototypes: true250 });251}