Skip to content
Myr

utils.js

This file

Path

lib/utils.js

251 lines · 4.6 KiB · JavaScript

Last changed by

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 15why, one step back
15var Buffer = require('safe-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) {

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 87why, one step back
87 var parts = str.split(/ *; */);

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 88why, one step back
88 var ret = { value: parts[0], quality: 1, params: {} }

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 89–91why, one step back
89
90 for (var i = 1; i < parts.length; ++i) {91 var pms = parts[i].split(/ *= */);

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

line 92why, one step back
92 if ('q' === pms[0]) {

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 93–118why, one step back
93 ret.quality = parseFloat(pms[1]);
94 } else {95 ret.params[pms[0]] = pms[1];96 }97 }9899 return ret;100}101102/**103 * Compile "etag" value to function.104 *105 * @param {Boolean|String|Function} val106 * @return {Function}107 * @api private108 */109110exports.compileETag = function(val) {111 var fn;112113 if (typeof val === 'function') {114 return val;115 }116117 switch (val) {118 case true:

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 120–150why, one step back
120 fn = exports.wetag;
121 break;122 case false:123 break;124 case 'strong':125 fn = exports.etag;126 break;127 default:128 throw new TypeError('unknown value for etag function: ' + val);129 }130131 return fn;132}133134/**135 * Compile "query parser" value to function.136 *137 * @param {String|Function} val138 * @return {Function}139 * @api private140 */141142exports.compileQueryParser = function compileQueryParser(val) {143 var fn;144145 if (typeof val === 'function') {146 return val;147 }148149 switch (val) {150 case true:

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 152–156why, one step back
152 fn = querystring.parse;
153 break;154 case false:155 break;156 case 'extended':

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 158–188why, one step back
158 break;
159 default:160 throw new TypeError('unknown value for query parser function: ' + val);161 }162163 return fn;164}165166/**167 * Compile "proxy trust" value to function.168 *169 * @param {Boolean|String|Number|Array|Function} val170 * @return {Function}171 * @api private172 */173174exports.compileTrust = function(val) {175 if (typeof val === 'function') return val;176177 if (val === true) {178 // Support plain true/false179 return function(){ return true };180 }181182 if (typeof val === 'number') {183 // Support trusting hop count184 return function(a, i){ return i < val };185 }186187 if (typeof val === 'string') {188 // Support comma-separated values

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 191–204why, one step back
191 }
192193 return proxyaddr.compile(val || []);194}195196/**197 * Set the charset in a given Content-Type string.198 *199 * @param {String} type200 * @param {String} charset201 * @return {String}202 * @api private203 */204

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 205–208why, one step back
205exports.setCharset = function setCharset(type, charset) {
206 if (!type || !charset) {207 return type;208 }

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 209–210why, one step back
209
210 // parse type

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

lines 212–216why, one step back
212
213 // set charset214 parsed.parameters.charset = charset;215216 // format type

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Merge tag '3.16.2'Douglas Christopher Wilsonbefore adoptione05a520

line 218why, one step back
218};

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 219–237why, one step back
219
220/**221 * Create an ETag generator function, generating ETags with222 * the given options.223 *224 * @param {object} options225 * @return {function}226 * @private227 */228229function createETagGenerator (options) {230 return function generateETag (body, encoding) {231 var buf = !Buffer.isBuffer(body)232 ? Buffer.from(body, encoding)233 : body234235 return etag(buf, options)236 }237}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 238–241why, one step back
238
239/**240 * Parse an extended query string with qs.241 *

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 243–251why, one step back
243 * @return {Object}
244 * @private245 */246247function parseExtendedQueryString(str) {248 return qs.parse(str, {249 allowPrototypes: true250 });251}