utils.js
This file
Path
lib/utils.js
254 lines · 4.6 KiB · JavaScript
Last changed by
Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
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–158why, 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:120 fn = exports.wetag;121 break;122 case false:123 break;124 case 'strong':125 fn = exports.etag;126 break;127 case 'weak':128 fn = exports.wetag;129 break;130 default:131 throw new TypeError('unknown value for etag function: ' + val);132 }133134 return fn;135}136137/**138 * Compile "query parser" value to function.139 *140 * @param {String|Function} val141 * @return {Function}142 * @api private143 */144145exports.compileQueryParser = function compileQueryParser(val) {146 var fn;147148 if (typeof val === 'function') {149 return val;150 }151152 switch (val) {153 case true:154 fn = querystring.parse;155 break;156 case false:157 break;158 case 'extended':Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 159why, one step back159 fn = parseExtendedQueryString;deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 160–208why, one step back160 break;161 case 'simple':162 fn = querystring.parse;163 break;164 default:165 throw new TypeError('unknown value for query parser function: ' + val);166 }167168 return fn;169}170171/**172 * Compile "proxy trust" value to function.173 *174 * @param {Boolean|String|Number|Array|Function} val175 * @return {Function}176 * @api private177 */178179exports.compileTrust = function(val) {180 if (typeof val === 'function') return val;181182 if (val === true) {183 // Support plain true/false184 return function(){ return true };185 }186187 if (typeof val === 'number') {188 // Support trusting hop count189 return function(a, i){ return i < val };190 }191192 if (typeof val === 'string') {193 // Support comma-separated values194 val = val.split(/ *, */);195 }196197 return proxyaddr.compile(val || []);198}199200/**201 * Set the charset in a given Content-Type string.202 *203 * @param {String} type204 * @param {String} charset205 * @return {String}206 * @api private207 */208Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 209–212why, one step back209exports.setCharset = function setCharset(type, charset) {210 if (!type || !charset) {211 return type;212 }deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 213–214why, one step back213214 // parse typeMerge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 215why, one step back215 var parsed = contentType.parse(type);deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
lines 216–220why, one step back216217 // set charset218 parsed.parameters.charset = charset;219220 // format typeMerge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 221why, one step back221 return contentType.format(parsed);deps: jade@~1.5.0Douglas Christopher Wilsonbefore adoption3b7ca43
line 222why, one step back222};Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 223–241why, one step back223224/**225 * Create an ETag generator function, generating ETags with226 * the given options.227 *228 * @param {object} options229 * @return {function}230 * @private231 */232233function createETagGenerator (options) {234 return function generateETag (body, encoding) {235 var buf = !Buffer.isBuffer(body)236 ? Buffer.from(body, encoding)237 : body238239 return etag(buf, options)240 }241}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 242–254why, one step back242243/**244 * Parse an extended query string with qs.245 *246 * @return {Object}247 * @private248 */249250function parseExtendedQueryString(str) {251 return qs.parse(str, {252 allowPrototypes: true253 });254}