utils.js
This file
Path
lib/utils.js
250 lines · 4.6 KiB · JavaScript
Last changed by
Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
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');Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 17why, one step back17var mime = require('send').mime;Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
line 18why, one step back18var etag = require('etag');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–92why, 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)56 : { value: mime.lookup(type), params: {} };57};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 * also includes `.originalIndex` for stable sorting81 *82 * @param {String} str83 * @return {Object}84 * @api private85 */8687function acceptParams(str, index) {88 var parts = str.split(/ *; */);89 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };9091 for (var i = 1; i < parts.length; ++i) {92 var pms = parts[i].split(/ *= */);Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 93why, one step back93 if ('q' === pms[0]) {deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 94–119why, one step back94 ret.quality = parseFloat(pms[1]);95 } else {96 ret.params[pms[0]] = pms[1];97 }98 }99100 return ret;101}102103/**104 * Compile "etag" value to function.105 *106 * @param {Boolean|String|Function} val107 * @return {Function}108 * @api private109 */110111exports.compileETag = function(val) {112 var fn;113114 if (typeof val === 'function') {115 return val;116 }117118 switch (val) {119 case true:Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 120why, one step back120 case 'weak':deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 121–151why, one step back121 fn = exports.wetag;122 break;123 case false:124 break;125 case 'strong':126 fn = exports.etag;127 break;128 default:129 throw new TypeError('unknown value for etag function: ' + val);130 }131132 return fn;133}134135/**136 * Compile "query parser" value to function.137 *138 * @param {String|Function} val139 * @return {Function}140 * @api private141 */142143exports.compileQueryParser = function compileQueryParser(val) {144 var fn;145146 if (typeof val === 'function') {147 return val;148 }149150 switch (val) {151 case true:Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 152why, one step back152 case 'simple':deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 153–157why, one step back153 fn = querystring.parse;154 break;155 case false:156 break;157 case 'extended':Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 158why, one step back158 fn = parseExtendedQueryString;deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 159–204why, one step back159 break;160 default:161 throw new TypeError('unknown value for query parser function: ' + val);162 }163164 return fn;165}166167/**168 * Compile "proxy trust" value to function.169 *170 * @param {Boolean|String|Number|Array|Function} val171 * @return {Function}172 * @api private173 */174175exports.compileTrust = function(val) {176 if (typeof val === 'function') return val;177178 if (val === true) {179 // Support plain true/false180 return function(){ return true };181 }182183 if (typeof val === 'number') {184 // Support trusting hop count185 return function(a, i){ return i < val };186 }187188 if (typeof val === 'string') {189 // Support comma-separated values190 val = val.split(/ *, */);191 }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–250why, one step back238239/**240 * Parse an extended query string with qs.241 *242 * @return {Object}243 * @private244 */245246function parseExtendedQueryString(str) {247 return qs.parse(str, {248 allowPrototypes: true249 });250}