utils.js
This file
Path
lib/utils.js
274 lines · 5.2 KiB · JavaScript
Last changed by
chore: replace `methods` dependency with standard library (#6196)Jon Koopsbefore adoptionb11122b
History of this fileTake it
The walk stopped after 200 commits: lines older than 2b2733c 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: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c
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: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c
lines 13–14why, one step back13 */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
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: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c
lines 19–21why, one step back19var proxyaddr = require('proxy-addr');20var qs = require('qs');21var querystring = require('querystring');chore: replace `methods` dependency with standard library (#6196)Jon Koopsbefore adoptionb11122b
lines 22–27why, one step back2223/**24 * A list of lowercased HTTP methods that are supported by Node.js.25 * @api private26 */27exports.methods = METHODS.map((method) => method.toLowerCase());deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c
lines 28–37why, one step back2829/**30 * Return strong ETag for `body`.31 *32 * @param {String|Buffer} body33 * @param {String} [encoding]34 * @return {String}35 * @api private36 */37Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 38why, one step back38exports.etag = createETagGenerator({ weak: false })deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c
lines 39–48why, one step back3940/**41 * Return weak ETag for `body`.42 *43 * @param {String|Buffer} body44 * @param {String} [encoding]45 * @return {String}46 * @api private47 */48Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 49why, one step back49exports.wetag = createETagGenerator({ weak: true })deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c
lines 50–61why, one step back5051/**52 * Normalize the given `type`, for example "html" becomes "text/html".53 *54 * @param {String} type55 * @return {Object}56 * @api private57 */5859exports.normalizeType = function(type){60 return ~type.indexOf('/')61 ? acceptParams(type)Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 62why, one step back62 : { value: (mime.lookup(type) || 'application/octet-stream'), params: {} }deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c
lines 63–91why, one step back63};6465/**66 * Normalize `types`, for example "html" becomes "text/html".67 *68 * @param {Array} types69 * @return {Array}70 * @api private71 */7273exports.normalizeTypes = function(types){74 var ret = [];7576 for (var i = 0; i < types.length; ++i) {77 ret.push(exports.normalizeType(types[i]));78 }7980 return ret;81};8283/**84 * Parse accept params `str` returning an85 * object with `.value`, `.quality` and `.params`.86 *87 * @param {String} str88 * @return {Object}89 * @api private90 */91Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 92why, one step back92function acceptParams (str) {Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52
lines 93–96why, one step back93 var length = str.length;94 var colonIndex = str.indexOf(';');95 var index = colonIndex === -1 ? length : colonIndex;96 var ret = { value: str.slice(0, index).trim(), quality: 1, params: {} };deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c
line 97why, one step back97Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52
lines 98–114why, one step back98 while (index < length) {99 var splitIndex = str.indexOf('=', index);100 if (splitIndex === -1) break;101102 var colonIndex = str.indexOf(';', index);103 var endIndex = colonIndex === -1 ? length : colonIndex;104105 if (splitIndex > endIndex) {106 index = str.lastIndexOf(';', splitIndex - 1) + 1;107 continue;108 }109110 var key = str.slice(index, splitIndex).trim();111 var value = str.slice(splitIndex + 1, endIndex).trim();112113 if (key === 'q') {114 ret.quality = parseFloat(value);deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c
line 115why, one step back115 } else {Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52
line 116why, one step back116 ret.params[key] = value;deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c
line 117why, one step back117 }Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52
lines 118–119why, one step back118119 index = endIndex + 1;deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c
lines 120–141why, one step back120 }121122 return ret;123}124125/**126 * Compile "etag" value to function.127 *128 * @param {Boolean|String|Function} val129 * @return {Function}130 * @api private131 */132133exports.compileETag = function(val) {134 var fn;135136 if (typeof val === 'function') {137 return val;138 }139140 switch (val) {141 case true:Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 142why, one step back142 case 'weak':deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c
lines 143–173why, one step back143 fn = exports.wetag;144 break;145 case false:146 break;147 case 'strong':148 fn = exports.etag;149 break;150 default:151 throw new TypeError('unknown value for etag function: ' + val);152 }153154 return fn;155}156157/**158 * Compile "query parser" value to function.159 *160 * @param {String|Function} val161 * @return {Function}162 * @api private163 */164165exports.compileQueryParser = function compileQueryParser(val) {166 var fn;167168 if (typeof val === 'function') {169 return val;170 }171172 switch (val) {173 case true:Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 174why, one step back174 case 'simple':deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c
lines 175–179why, one step back175 fn = querystring.parse;176 break;177 case false:178 break;179 case 'extended':Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 180why, one step back180 fn = parseExtendedQueryString;deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c
lines 181–211why, one step back181 break;182 default:183 throw new TypeError('unknown value for query parser function: ' + val);184 }185186 return fn;187}188189/**190 * Compile "proxy trust" value to function.191 *192 * @param {Boolean|String|Number|Array|Function} val193 * @return {Function}194 * @api private195 */196197exports.compileTrust = function(val) {198 if (typeof val === 'function') return val;199200 if (val === true) {201 // Support plain true/false202 return function(){ return true };203 }204205 if (typeof val === 'number') {206 // Support trusting hop count207 return function(a, i){ return i < val };208 }209210 if (typeof val === 'string') {211 // Support comma-separated valuesMerge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 212–213why, one step back212 val = val.split(',')213 .map(function (v) { return v.trim() })deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c
lines 214–227why, one step back214 }215216 return proxyaddr.compile(val || []);217}218219/**220 * Set the charset in a given Content-Type string.221 *222 * @param {String} type223 * @param {String} charset224 * @return {String}225 * @api private226 */227Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 228–231why, one step back228exports.setCharset = function setCharset(type, charset) {229 if (!type || !charset) {230 return type;231 }deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c
lines 232–233why, one step back232233 // parse typeMerge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 234why, one step back234 var parsed = contentType.parse(type);deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c
lines 235–239why, one step back235236 // set charset237 parsed.parameters.charset = charset;238239 // format typeMerge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 240why, one step back240 return contentType.format(parsed);deps: qs@2.2.0Douglas Christopher Wilsonbefore adoption2b2733c
line 241why, one step back241};Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 242–260why, one step back242243/**244 * Create an ETag generator function, generating ETags with245 * the given options.246 *247 * @param {object} options248 * @return {function}249 * @private250 */251252function createETagGenerator (options) {253 return function generateETag (body, encoding) {254 var buf = !Buffer.isBuffer(body)255 ? Buffer.from(body, encoding)256 : body257258 return etag(buf, options)259 }260}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 261–264why, one step back261262/**263 * Parse an extended query string with qs.264 *Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 265why, one step back265 * @param {String} strMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 266–274why, one step back266 * @return {Object}267 * @private268 */269270function parseExtendedQueryString(str) {271 return qs.parse(str, {272 allowPrototypes: true273 });274}