Skip to content
Myr

utils.js

This file

Path

lib/utils.js

269 lines · 5.1 KiB · JavaScript

Last changed by

refactor: simplify `normalizeTypes` function (#6097)Ayoub Mabroukbefore adoption511d9df

History of this file

Take it

The walk stopped after 200 commits: lines older than 33bb8fc point at it.

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 1–7why, one step back
1/*!
2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2014-2015 Douglas Christopher Wilson5 * MIT Licensed6 */7

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 8–9why, one step back
8'use strict';
9

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

lines 10–11why, one step back
10/**
11 * Module dependencies.

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

line 12why, one step back
12 * @api private

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

lines 13–14why, one step back
13 */
14

chore: replace `methods` dependency with standard library (#6196)Jon Koopsbefore adoptionb11122b

line 15why, one step back
15var { METHODS } = require('node:http');

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

line 16why, one step back
16var contentType = require('content-type');

Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed

line 17why, one step back
17var etag = require('etag');

Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6

line 18why, one step back
18var mime = require('mime-types')

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

lines 19–21why, one step back
19var 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 back
22
23/**24 * A list of lowercased HTTP methods that are supported by Node.js.25 * @api private26 */27exports.methods = METHODS.map((method) => method.toLowerCase());

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

lines 28–37why, one step back
28
29/**30 * Return strong ETag for `body`.31 *32 * @param {String|Buffer} body33 * @param {String} [encoding]34 * @return {String}35 * @api private36 */37

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 38why, one step back
38exports.etag = createETagGenerator({ weak: false })

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

lines 39–48why, one step back
39
40/**41 * Return weak ETag for `body`.42 *43 * @param {String|Buffer} body44 * @param {String} [encoding]45 * @return {String}46 * @api private47 */48

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 49why, one step back
49exports.wetag = createETagGenerator({ weak: true })

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

lines 50–61why, one step back
50
51/**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 back
62 : { value: (mime.lookup(type) || 'application/octet-stream'), params: {} }

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

lines 63–72why, one step back
63};
6465/**66 * Normalize `types`, for example "html" becomes "text/html".67 *68 * @param {Array} types69 * @return {Array}70 * @api private71 */72

refactor: simplify `normalizeTypes` function (#6097)Ayoub Mabroukbefore adoption511d9df

lines 73–74why, one step back
73exports.normalizeTypes = function(types) {
74 return types.map(exports.normalizeType);

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

line 75why, one step back
75};

refactor: simplify `normalizeTypes` function (#6097)Ayoub Mabroukbefore adoption511d9df

line 76why, one step back
76

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

lines 77–86why, one step back
77
78/**79 * Parse accept params `str` returning an80 * object with `.value`, `.quality` and `.params`.81 *82 * @param {String} str83 * @return {Object}84 * @api private85 */86

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 87why, one step back
87function acceptParams (str) {

Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52

lines 88–91why, one step back
88 var length = str.length;
89 var colonIndex = str.indexOf(';');90 var index = colonIndex === -1 ? length : colonIndex;91 var ret = { value: str.slice(0, index).trim(), quality: 1, params: {} };

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

line 92why, one step back
92

Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52

lines 93–109why, one step back
93 while (index < length) {
94 var splitIndex = str.indexOf('=', index);95 if (splitIndex === -1) break;9697 var colonIndex = str.indexOf(';', index);98 var endIndex = colonIndex === -1 ? length : colonIndex;99100 if (splitIndex > endIndex) {101 index = str.lastIndexOf(';', splitIndex - 1) + 1;102 continue;103 }104105 var key = str.slice(index, splitIndex).trim();106 var value = str.slice(splitIndex + 1, endIndex).trim();107108 if (key === 'q') {109 ret.quality = parseFloat(value);

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

line 110why, one step back
110 } else {

Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52

line 111why, one step back
111 ret.params[key] = value;

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

line 112why, one step back
112 }

Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52

lines 113–114why, one step back
113
114 index = endIndex + 1;

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

lines 115–136why, one step back
115 }
116117 return ret;118}119120/**121 * Compile "etag" value to function.122 *123 * @param {Boolean|String|Function} val124 * @return {Function}125 * @api private126 */127128exports.compileETag = function(val) {129 var fn;130131 if (typeof val === 'function') {132 return val;133 }134135 switch (val) {136 case true:

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

line 137why, one step back
137 case 'weak':

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

lines 138–168why, one step back
138 fn = exports.wetag;
139 break;140 case false:141 break;142 case 'strong':143 fn = exports.etag;144 break;145 default:146 throw new TypeError('unknown value for etag function: ' + val);147 }148149 return fn;150}151152/**153 * Compile "query parser" value to function.154 *155 * @param {String|Function} val156 * @return {Function}157 * @api private158 */159160exports.compileQueryParser = function compileQueryParser(val) {161 var fn;162163 if (typeof val === 'function') {164 return val;165 }166167 switch (val) {168 case true:

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

line 169why, one step back
169 case 'simple':

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

lines 170–174why, one step back
170 fn = querystring.parse;
171 break;172 case false:173 break;174 case 'extended':

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 175why, one step back
175 fn = parseExtendedQueryString;

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

lines 176–206why, one step back
176 break;
177 default:178 throw new TypeError('unknown value for query parser function: ' + val);179 }180181 return fn;182}183184/**185 * Compile "proxy trust" value to function.186 *187 * @param {Boolean|String|Number|Array|Function} val188 * @return {Function}189 * @api private190 */191192exports.compileTrust = function(val) {193 if (typeof val === 'function') return val;194195 if (val === true) {196 // Support plain true/false197 return function(){ return true };198 }199200 if (typeof val === 'number') {201 // Support trusting hop count202 return function(a, i){ return i < val };203 }204205 if (typeof val === 'string') {206 // Support comma-separated values

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

lines 207–208why, one step back
207 val = val.split(',')
208 .map(function (v) { return v.trim() })

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

lines 209–222why, one step back
209 }
210211 return proxyaddr.compile(val || []);212}213214/**215 * Set the charset in a given Content-Type string.216 *217 * @param {String} type218 * @param {String} charset219 * @return {String}220 * @api private221 */222

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 223–226why, one step back
223exports.setCharset = function setCharset(type, charset) {
224 if (!type || !charset) {225 return type;226 }

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

lines 227–228why, one step back
227
228 // parse type

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

line 229why, one step back
229 var parsed = contentType.parse(type);

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

lines 230–234why, one step back
230
231 // set charset232 parsed.parameters.charset = charset;233234 // format type

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

line 235why, one step back
235 return contentType.format(parsed);

examples: fix up route-separation codeDouglas Christopher Wilsonbefore adoption33bb8fc

line 236why, one step back
236};

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 237–255why, one step back
237
238/**239 * Create an ETag generator function, generating ETags with240 * the given options.241 *242 * @param {object} options243 * @return {function}244 * @private245 */246247function createETagGenerator (options) {248 return function generateETag (body, encoding) {249 var buf = !Buffer.isBuffer(body)250 ? Buffer.from(body, encoding)251 : body252253 return etag(buf, options)254 }255}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 256–259why, one step back
256
257/**258 * Parse an extended query string with qs.259 *

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 260why, one step back
260 * @param {String} str

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 261–269why, one step back
261 * @return {Object}
262 * @private263 */264265function parseExtendedQueryString(str) {266 return qs.parse(str, {267 allowPrototypes: true268 });269}