Skip to content
Myr

utils.js

This file

Path

lib/utils.js

268 lines · 5.0 KiB · JavaScript

Last changed by

Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52

History of this file

Take 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 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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

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

fix(buffer): use node:buffer instead of safe-buffer (#6071)Bhavya Dhimanbefore adoptionc70197a

line 15why, one step back
15var Buffer = require('node:buffer').Buffer

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 19–31why, one step back
19var proxyaddr = require('proxy-addr');
20var qs = require('qs');21var querystring = require('querystring');2223/**24 * Return strong ETag for `body`.25 *26 * @param {String|Buffer} body27 * @param {String} [encoding]28 * @return {String}29 * @api private30 */31

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 33–42why, one step back
33
34/**35 * Return weak ETag for `body`.36 *37 * @param {String|Buffer} body38 * @param {String} [encoding]39 * @return {String}40 * @api private41 */42

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 44–55why, one step back
44
45/**46 * Normalize the given `type`, for example "html" becomes "text/html".47 *48 * @param {String} type49 * @return {Object}50 * @api private51 */5253exports.normalizeType = function(type){54 return ~type.indexOf('/')55 ? acceptParams(type)

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

line 56why, one step back
56 : { value: (mime.lookup(type) || 'application/octet-stream'), params: {} }

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 57–85why, one step back
57};
5859/**60 * Normalize `types`, for example "html" becomes "text/html".61 *62 * @param {Array} types63 * @return {Array}64 * @api private65 */6667exports.normalizeTypes = function(types){68 var ret = [];6970 for (var i = 0; i < types.length; ++i) {71 ret.push(exports.normalizeType(types[i]));72 }7374 return ret;75};7677/**78 * Parse accept params `str` returning an79 * object with `.value`, `.quality` and `.params`.80 *81 * @param {String} str82 * @return {Object}83 * @api private84 */85

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 91why, one step back
91

Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52

lines 92–108why, one step back
92 while (index < length) {
93 var splitIndex = str.indexOf('=', index);94 if (splitIndex === -1) break;9596 var colonIndex = str.indexOf(';', index);97 var endIndex = colonIndex === -1 ? length : colonIndex;9899 if (splitIndex > endIndex) {100 index = str.lastIndexOf(';', splitIndex - 1) + 1;101 continue;102 }103104 var key = str.slice(index, splitIndex).trim();105 var value = str.slice(splitIndex + 1, endIndex).trim();106107 if (key === 'q') {108 ret.quality = parseFloat(value);

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 109why, one step back
109 } else {

Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 111why, one step back
111 }

Use loop for acceptParams (#6066)Blake Embreybefore adoption805ef52

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 114–135why, one step back
114 }
115116 return ret;117}118119/**120 * Compile "etag" value to function.121 *122 * @param {Boolean|String|Function} val123 * @return {Function}124 * @api private125 */126127exports.compileETag = function(val) {128 var fn;129130 if (typeof val === 'function') {131 return val;132 }133134 switch (val) {135 case true:

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 137–167why, one step back
137 fn = exports.wetag;
138 break;139 case false:140 break;141 case 'strong':142 fn = exports.etag;143 break;144 default:145 throw new TypeError('unknown value for etag function: ' + val);146 }147148 return fn;149}150151/**152 * Compile "query parser" value to function.153 *154 * @param {String|Function} val155 * @return {Function}156 * @api private157 */158159exports.compileQueryParser = function compileQueryParser(val) {160 var fn;161162 if (typeof val === 'function') {163 return val;164 }165166 switch (val) {167 case true:

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 175–205why, one step back
175 break;
176 default:177 throw new TypeError('unknown value for query parser function: ' + val);178 }179180 return fn;181}182183/**184 * Compile "proxy trust" value to function.185 *186 * @param {Boolean|String|Number|Array|Function} val187 * @return {Function}188 * @api private189 */190191exports.compileTrust = function(val) {192 if (typeof val === 'function') return val;193194 if (val === true) {195 // Support plain true/false196 return function(){ return true };197 }198199 if (typeof val === 'number') {200 // Support trusting hop count201 return function(a, i){ return i < val };202 }203204 if (typeof val === 'string') {205 // Support comma-separated values

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 208–221why, one step back
208 }
209210 return proxyaddr.compile(val || []);211}212213/**214 * Set the charset in a given Content-Type string.215 *216 * @param {String} type217 * @param {String} charset218 * @return {String}219 * @api private220 */221

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 229–233why, one step back
229
230 // set charset231 parsed.parameters.charset = charset;232233 // format type

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 235why, one step back
235};

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 236–254why, one step back
236
237/**238 * Create an ETag generator function, generating ETags with239 * the given options.240 *241 * @param {object} options242 * @return {function}243 * @private244 */245246function createETagGenerator (options) {247 return function generateETag (body, encoding) {248 var buf = !Buffer.isBuffer(body)249 ? Buffer.from(body, encoding)250 : body251252 return etag(buf, options)253 }254}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 260–268why, one step back
260 * @return {Object}
261 * @private262 */263264function parseExtendedQueryString(str) {265 return qs.parse(str, {266 allowPrototypes: true267 });268}