utils.js
This file
Path
lib/utils.js
271 lines · 5.2 KiB · JavaScript
Last changed by
Revert "sec: security patch for CVE-2024-51999"Ulises Gasconbefore adoption697547c
History of this fileTake it
The walk stopped after 200 commits: lines older than f6ec710 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 '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 10–14why, one step back10/**11 * Module dependencies.12 * @api private13 */14chore: replace `methods` dependency with standard library (#6196)Jon Koopsbefore adoptionb11122b
line 15why, one step back15var { METHODS } = require('node:http');Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 16–17why, one step back16var contentType = require('content-type');17var etag = require('etag');Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 18why, one step back18var mime = require('mime-types')Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 19–20why, one step back19var proxyaddr = require('proxy-addr');20var qs = require('qs');chore: use node protocol for node builtin module (#6520)Shivam Sharmabefore adoption3910323
line 21why, one step back21var querystring = require('node:querystring');chore: enforce explicit Buffer import and add lint rule (#6525)Shivam Sharmabefore adoption98c85eb
line 22why, one step back22const { Buffer } = require('node:buffer');chore: use node protocol for node builtin module (#6520)Shivam Sharmabefore adoption3910323
line 23why, one step back23chore: replace `methods` dependency with standard library (#6196)Jon Koopsbefore adoptionb11122b
lines 24–29why, one step back2425/**26 * A list of lowercased HTTP methods that are supported by Node.js.27 * @api private28 */29exports.methods = METHODS.map((method) => method.toLowerCase());Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 30–39why, one step back3031/**32 * Return strong ETag for `body`.33 *34 * @param {String|Buffer} body35 * @param {String} [encoding]36 * @return {String}37 * @api private38 */39Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 40why, one step back40exports.etag = createETagGenerator({ weak: false })Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 41–50why, one step back4142/**43 * Return weak ETag for `body`.44 *45 * @param {String|Buffer} body46 * @param {String} [encoding]47 * @return {String}48 * @api private49 */50Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 51why, one step back51exports.wetag = createETagGenerator({ weak: true })Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 52–63why, one step back5253/**54 * Normalize the given `type`, for example "html" becomes "text/html".55 *56 * @param {String} type57 * @return {Object}58 * @api private59 */6061exports.normalizeType = function(type){62 return ~type.indexOf('/')63 ? acceptParams(type)Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 64why, one step back64 : { value: (mime.lookup(type) || 'application/octet-stream'), params: {} }Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 65–74why, one step back65};6667/**68 * Normalize `types`, for example "html" becomes "text/html".69 *70 * @param {Array} types71 * @return {Array}72 * @api private73 */74refactor: simplify `normalizeTypes` function (#6097)Ayoub Mabroukbefore adoption511d9df
lines 75–76why, one step back75exports.normalizeTypes = function(types) {76 return types.map(exports.normalizeType);Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 77why, one step back77};refactor: simplify `normalizeTypes` function (#6097)Ayoub Mabroukbefore adoption511d9df
line 78why, one step back78Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 79–88why, one step back7980/**81 * Parse accept params `str` returning an82 * object with `.value`, `.quality` and `.params`.83 *84 * @param {String} str85 * @return {Object}86 * @api private87 */88Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 89why, one step back89function acceptParams (str) {Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52
lines 90–93why, one step back90 var length = str.length;91 var colonIndex = str.indexOf(';');92 var index = colonIndex === -1 ? length : colonIndex;93 var ret = { value: str.slice(0, index).trim(), quality: 1, params: {} };Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 94why, one step back94Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52
lines 95–111why, one step back95 while (index < length) {96 var splitIndex = str.indexOf('=', index);97 if (splitIndex === -1) break;9899 var colonIndex = str.indexOf(';', index);100 var endIndex = colonIndex === -1 ? length : colonIndex;101102 if (splitIndex > endIndex) {103 index = str.lastIndexOf(';', splitIndex - 1) + 1;104 continue;105 }106107 var key = str.slice(index, splitIndex).trim();108 var value = str.slice(splitIndex + 1, endIndex).trim();109110 if (key === 'q') {111 ret.quality = parseFloat(value);Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 112why, one step back112 } else {Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52
line 113why, one step back113 ret.params[key] = value;Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 114why, one step back114 }Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52
lines 115–116why, one step back115116 index = endIndex + 1;Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 117–138why, one step back117 }118119 return ret;120}121122/**123 * Compile "etag" value to function.124 *125 * @param {Boolean|String|Function} val126 * @return {Function}127 * @api private128 */129130exports.compileETag = function(val) {131 var fn;132133 if (typeof val === 'function') {134 return val;135 }136137 switch (val) {138 case true:Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 139why, one step back139 case 'weak':Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 140–170why, one step back140 fn = exports.wetag;141 break;142 case false:143 break;144 case 'strong':145 fn = exports.etag;146 break;147 default:148 throw new TypeError('unknown value for etag function: ' + val);149 }150151 return fn;152}153154/**155 * Compile "query parser" value to function.156 *157 * @param {String|Function} val158 * @return {Function}159 * @api private160 */161162exports.compileQueryParser = function compileQueryParser(val) {163 var fn;164165 if (typeof val === 'function') {166 return val;167 }168169 switch (val) {170 case true:Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 171why, one step back171 case 'simple':Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 172–176why, one step back172 fn = querystring.parse;173 break;174 case false:175 break;176 case 'extended':Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 177why, one step back177 fn = parseExtendedQueryString;Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 178–208why, one step back178 break;179 default:180 throw new TypeError('unknown value for query parser function: ' + val);181 }182183 return fn;184}185186/**187 * Compile "proxy trust" value to function.188 *189 * @param {Boolean|String|Number|Array|Function} val190 * @return {Function}191 * @api private192 */193194exports.compileTrust = function(val) {195 if (typeof val === 'function') return val;196197 if (val === true) {198 // Support plain true/false199 return function(){ return true };200 }201202 if (typeof val === 'number') {203 // Support trusting hop count204 return function(a, i){ return i < val };205 }206207 if (typeof val === 'string') {208 // Support comma-separated valuesMerge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 209–210why, one step back209 val = val.split(',')210 .map(function (v) { return v.trim() })Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 211–238why, one step back211 }212213 return proxyaddr.compile(val || []);214}215216/**217 * Set the charset in a given Content-Type string.218 *219 * @param {String} type220 * @param {String} charset221 * @return {String}222 * @api private223 */224225exports.setCharset = function setCharset(type, charset) {226 if (!type || !charset) {227 return type;228 }229230 // parse type231 var parsed = contentType.parse(type);232233 // set charset234 parsed.parameters.charset = charset;235236 // format type237 return contentType.format(parsed);238};Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 239–257why, one step back239240/**241 * Create an ETag generator function, generating ETags with242 * the given options.243 *244 * @param {object} options245 * @return {function}246 * @private247 */248249function createETagGenerator (options) {250 return function generateETag (body, encoding) {251 var buf = !Buffer.isBuffer(body)252 ? Buffer.from(body, encoding)253 : body254255 return etag(buf, options)256 }257}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 258–261why, one step back258259/**260 * Parse an extended query string with qs.261 *Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 262why, one step back262 * @param {String} strMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 263–268why, one step back263 * @return {Object}264 * @private265 */266267function parseExtendedQueryString(str) {268 return qs.parse(str, {Revert "sec: security patch for CVE-2024-51999"Ulises Gasconbefore adoption697547c
line 269why, one step back269 allowPrototypes: trueMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 270–271why, one step back270 });271}