Skip to content
Myr

utils.js

This file

Path

lib/utils.js

271 lines · 5.2 KiB · JavaScript

Last changed by

chore: use node protocol for node builtin module (#6520)Shivam Sharmabefore adoption3910323

History of this file

Take it

The walk stopped after 200 commits: lines older than e66625b 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

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

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

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

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');

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

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')

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

lines 19–20why, one step back
19var proxyaddr = require('proxy-addr');
20var qs = require('qs');

chore: use node protocol for node builtin module (#6520)Shivam Sharmabefore adoption3910323

line 21why, one step back
21var querystring = require('node:querystring');

chore: enforce explicit Buffer import and add lint rule (#6525)Shivam Sharmabefore adoption98c85eb

line 22why, one step back
22const { Buffer } = require('node:buffer');

chore: use node protocol for node builtin module (#6520)Shivam Sharmabefore adoption3910323

line 23why, one step back
23

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

lines 24–29why, one step back
24
25/**26 * A list of lowercased HTTP methods that are supported by Node.js.27 * @api private28 */29exports.methods = METHODS.map((method) => method.toLowerCase());

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

lines 30–39why, one step back
30
31/**32 * Return strong ETag for `body`.33 *34 * @param {String|Buffer} body35 * @param {String} [encoding]36 * @return {String}37 * @api private38 */39

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

lines 41–50why, one step back
41
42/**43 * Return weak ETag for `body`.44 *45 * @param {String|Buffer} body46 * @param {String} [encoding]47 * @return {String}48 * @api private49 */50

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

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

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

lines 65–74why, one step back
65};
6667/**68 * Normalize `types`, for example "html" becomes "text/html".69 *70 * @param {Array} types71 * @return {Array}72 * @api private73 */74

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

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

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

line 77why, one step back
77};

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

line 78why, one step back
78

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

lines 79–88why, one step back
79
80/**81 * Parse accept params `str` returning an82 * object with `.value`, `.quality` and `.params`.83 *84 * @param {String} str85 * @return {Object}86 * @api private87 */88

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52

lines 90–93why, one step back
90 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: {} };

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

line 94why, one step back
94

Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52

lines 95–111why, one step back
95 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);

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

line 112why, one step back
112 } else {

Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52

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

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

line 114why, one step back
114 }

Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52

lines 115–116why, one step back
115
116 index = endIndex + 1;

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

lines 117–138why, one step back
117 }
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 back
139 case 'weak':

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

lines 140–170why, one step back
140 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 back
171 case 'simple':

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

lines 172–176why, one step back
172 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 back
177 fn = parseExtendedQueryString;

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

lines 178–208why, one step back
178 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 values

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

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

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

lines 211–224why, one step back
211 }
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 */224

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

lines 229–230why, one step back
229
230 // parse type

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

lines 232–236why, one step back
232
233 // set charset234 parsed.parameters.charset = charset;235236 // format type

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Remove res.send(body, status) signatureDouglas Christopher Wilsonbefore adoptione66625b

line 238why, one step back
238};

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 239–257why, one step back
239
240/**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 back
258
259/**260 * Parse an extended query string with qs.261 *

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 263–271why, one step back
263 * @return {Object}
264 * @private265 */266267function parseExtendedQueryString(str) {268 return qs.parse(str, {269 allowPrototypes: true270 });271}