utils.js
This file
Path
lib/utils.js
267 lines · 5.0 KiB · JavaScript
Last changed by
cleanup: remove unnecessary require for global BufferPhillip9587before adoption9f8589e
History of this fileTake it
The walk stopped after 200 commits: lines older than e05a520 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';9Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 10–11why, one step back10/**11 * Module dependencies.Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 12why, one step back12 * @api privateMerge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 13–14why, one step back13 */14Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 15why, one step back15var contentType = require('content-type');Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
line 16why, one step back16var etag = require('etag');Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 17why, one step back17var mime = require('mime-types')Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 18–30why, one step back18var proxyaddr = require('proxy-addr');19var qs = require('qs');20var querystring = require('querystring');2122/**23 * Return strong ETag for `body`.24 *25 * @param {String|Buffer} body26 * @param {String} [encoding]27 * @return {String}28 * @api private29 */30Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 31why, one step back31exports.etag = createETagGenerator({ weak: false })Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 32–41why, one step back3233/**34 * Return weak ETag for `body`.35 *36 * @param {String|Buffer} body37 * @param {String} [encoding]38 * @return {String}39 * @api private40 */41Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 42why, one step back42exports.wetag = createETagGenerator({ weak: true })Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 43–54why, one step back4344/**45 * Normalize the given `type`, for example "html" becomes "text/html".46 *47 * @param {String} type48 * @return {Object}49 * @api private50 */5152exports.normalizeType = function(type){53 return ~type.indexOf('/')54 ? acceptParams(type)Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 55why, one step back55 : { value: (mime.lookup(type) || 'application/octet-stream'), params: {} }Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 56–84why, one step back56};5758/**59 * Normalize `types`, for example "html" becomes "text/html".60 *61 * @param {Array} types62 * @return {Array}63 * @api private64 */6566exports.normalizeTypes = function(types){67 var ret = [];6869 for (var i = 0; i < types.length; ++i) {70 ret.push(exports.normalizeType(types[i]));71 }7273 return ret;74};7576/**77 * Parse accept params `str` returning an78 * object with `.value`, `.quality` and `.params`.79 *80 * @param {String} str81 * @return {Object}82 * @api private83 */84Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 85why, one step back85function acceptParams (str) {Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52
lines 86–89why, one step back86 var length = str.length;87 var colonIndex = str.indexOf(';');88 var index = colonIndex === -1 ? length : colonIndex;89 var ret = { value: str.slice(0, index).trim(), quality: 1, params: {} };Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 90why, one step back90Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52
lines 91–107why, one step back91 while (index < length) {92 var splitIndex = str.indexOf('=', index);93 if (splitIndex === -1) break;9495 var colonIndex = str.indexOf(';', index);96 var endIndex = colonIndex === -1 ? length : colonIndex;9798 if (splitIndex > endIndex) {99 index = str.lastIndexOf(';', splitIndex - 1) + 1;100 continue;101 }102103 var key = str.slice(index, splitIndex).trim();104 var value = str.slice(splitIndex + 1, endIndex).trim();105106 if (key === 'q') {107 ret.quality = parseFloat(value);Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 108why, one step back108 } else {Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52
line 109why, one step back109 ret.params[key] = value;Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 110why, one step back110 }Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52
lines 111–112why, one step back111112 index = endIndex + 1;Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 113–134why, one step back113 }114115 return ret;116}117118/**119 * Compile "etag" value to function.120 *121 * @param {Boolean|String|Function} val122 * @return {Function}123 * @api private124 */125126exports.compileETag = function(val) {127 var fn;128129 if (typeof val === 'function') {130 return val;131 }132133 switch (val) {134 case true:Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 135why, one step back135 case 'weak':Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 136–166why, one step back136 fn = exports.wetag;137 break;138 case false:139 break;140 case 'strong':141 fn = exports.etag;142 break;143 default:144 throw new TypeError('unknown value for etag function: ' + val);145 }146147 return fn;148}149150/**151 * Compile "query parser" value to function.152 *153 * @param {String|Function} val154 * @return {Function}155 * @api private156 */157158exports.compileQueryParser = function compileQueryParser(val) {159 var fn;160161 if (typeof val === 'function') {162 return val;163 }164165 switch (val) {166 case true:Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 167why, one step back167 case 'simple':Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 168–172why, one step back168 fn = querystring.parse;169 break;170 case false:171 break;172 case 'extended':Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 173why, one step back173 fn = parseExtendedQueryString;Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 174–204why, one step back174 break;175 default:176 throw new TypeError('unknown value for query parser function: ' + val);177 }178179 return fn;180}181182/**183 * Compile "proxy trust" value to function.184 *185 * @param {Boolean|String|Number|Array|Function} val186 * @return {Function}187 * @api private188 */189190exports.compileTrust = function(val) {191 if (typeof val === 'function') return val;192193 if (val === true) {194 // Support plain true/false195 return function(){ return true };196 }197198 if (typeof val === 'number') {199 // Support trusting hop count200 return function(a, i){ return i < val };201 }202203 if (typeof val === 'string') {204 // Support comma-separated valuesMerge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 205–206why, one step back205 val = val.split(',')206 .map(function (v) { return v.trim() })Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 207–220why, one step back207 }208209 return proxyaddr.compile(val || []);210}211212/**213 * Set the charset in a given Content-Type string.214 *215 * @param {String} type216 * @param {String} charset217 * @return {String}218 * @api private219 */220Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 221–224why, one step back221exports.setCharset = function setCharset(type, charset) {222 if (!type || !charset) {223 return type;224 }Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 225–226why, one step back225226 // parse typeMerge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 227why, one step back227 var parsed = contentType.parse(type);Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
lines 228–232why, one step back228229 // set charset230 parsed.parameters.charset = charset;231232 // format typeMerge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 233why, one step back233 return contentType.format(parsed);Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520
line 234why, one step back234};Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 235–253why, one step back235236/**237 * Create an ETag generator function, generating ETags with238 * the given options.239 *240 * @param {object} options241 * @return {function}242 * @private243 */244245function createETagGenerator (options) {246 return function generateETag (body, encoding) {247 var buf = !Buffer.isBuffer(body)248 ? Buffer.from(body, encoding)249 : body250251 return etag(buf, options)252 }253}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 254–257why, one step back254255/**256 * Parse an extended query string with qs.257 *Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 258why, one step back258 * @param {String} strMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 259–267why, one step back259 * @return {Object}260 * @private261 */262263function parseExtendedQueryString(str) {264 return qs.parse(str, {265 allowPrototypes: true266 });267}