utils.js
This file
Path
lib/utils.js
246 lines · 4.5 KiB · JavaScript
Last changed by
Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
History of this fileTake it
The walk stopped after 200 commits: lines older than 112dbb2 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';94.6.1Douglas Christopher Wilsonbefore adoption112dbb2
lines 10–11why, one step back10/**11 * Module dependencies.Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 12why, one step back12 * @api private4.6.1Douglas Christopher Wilsonbefore adoption112dbb2
lines 13–15why, one step back13 */1415var mime = require('send').mime;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');4.6.1Douglas Christopher Wilsonbefore adoption112dbb2
line 18why, one step back18var proxyaddr = require('proxy-addr');Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
lines 19–20why, one step back19var qs = require('qs');20var querystring = require('querystring');4.6.1Douglas Christopher Wilsonbefore adoption112dbb2
lines 21–30why, one step back2122/**23 * Return strong ETag for `body`.24 *25 * @param {String|Buffer} body26 * @param {String} [encoding]27 * @return {String}28 * @api private29 */30Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
lines 31–33why, one step back31exports.etag = function (body, encoding) {32 var buf = !Buffer.isBuffer(body)33 ? new Buffer(body, encoding)Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 34why, one step back34 : body;4.6.1Douglas Christopher Wilsonbefore adoption112dbb2
line 35why, one step back35Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 36why, one step back36 return etag(buf, {weak: false});4.6.1Douglas Christopher Wilsonbefore adoption112dbb2
lines 37–48why, one step back37};3839/**40 * Return weak ETag for `body`.41 *42 * @param {String|Buffer} body43 * @param {String} [encoding]44 * @return {String}45 * @api private46 */4748exports.wetag = function wetag(body, encoding){Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
lines 49–50why, one step back49 var buf = !Buffer.isBuffer(body)50 ? new Buffer(body, encoding)Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 51why, one step back51 : body;4.6.1Douglas Christopher Wilsonbefore adoption112dbb2
line 52why, one step back52Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 53why, one step back53 return etag(buf, {weak: true});4.6.1Douglas Christopher Wilsonbefore adoption112dbb2
lines 54–103why, one step back54};5556/**57 * Normalize the given `type`, for example "html" becomes "text/html".58 *59 * @param {String} type60 * @return {Object}61 * @api private62 */6364exports.normalizeType = function(type){65 return ~type.indexOf('/')66 ? acceptParams(type)67 : { value: mime.lookup(type), params: {} };68};6970/**71 * Normalize `types`, for example "html" becomes "text/html".72 *73 * @param {Array} types74 * @return {Array}75 * @api private76 */7778exports.normalizeTypes = function(types){79 var ret = [];8081 for (var i = 0; i < types.length; ++i) {82 ret.push(exports.normalizeType(types[i]));83 }8485 return ret;86};8788/**89 * Parse accept params `str` returning an90 * object with `.value`, `.quality` and `.params`.91 * also includes `.originalIndex` for stable sorting92 *93 * @param {String} str94 * @return {Object}95 * @api private96 */9798function acceptParams(str, index) {99 var parts = str.split(/ *; */);100 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };101102 for (var i = 1; i < parts.length; ++i) {103 var pms = parts[i].split(/ *= */);Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 104why, one step back104 if ('q' === pms[0]) {4.6.1Douglas Christopher Wilsonbefore adoption112dbb2
lines 105–148why, one step back105 ret.quality = parseFloat(pms[1]);106 } else {107 ret.params[pms[0]] = pms[1];108 }109 }110111 return ret;112}113114/**115 * Compile "etag" value to function.116 *117 * @param {Boolean|String|Function} val118 * @return {Function}119 * @api private120 */121122exports.compileETag = function(val) {123 var fn;124125 if (typeof val === 'function') {126 return val;127 }128129 switch (val) {130 case true:131 fn = exports.wetag;132 break;133 case false:134 break;135 case 'strong':136 fn = exports.etag;137 break;138 case 'weak':139 fn = exports.wetag;140 break;141 default:142 throw new TypeError('unknown value for etag function: ' + val);143 }144145 return fn;146}147148/**Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
lines 149–169why, one step back149 * Compile "query parser" value to function.150 *151 * @param {String|Function} val152 * @return {Function}153 * @api private154 */155156exports.compileQueryParser = function compileQueryParser(val) {157 var fn;158159 if (typeof val === 'function') {160 return val;161 }162163 switch (val) {164 case true:165 fn = querystring.parse;166 break;167 case false:168 break;169 case 'extended':Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 170why, one step back170 fn = parseExtendedQueryString;Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
lines 171–182why, one step back171 break;172 case 'simple':173 fn = querystring.parse;174 break;175 default:176 throw new TypeError('unknown value for query parser function: ' + val);177 }178179 return fn;180}181182/**4.6.1Douglas Christopher Wilsonbefore adoption112dbb2
lines 183–219why, one step back183 * Compile "proxy trust" value to function.184 *185 * @param {Boolean|String|Number|Array|Function} val186 * @return {Function}187 * @api private188 */189190exports.compileTrust = function(val) {191 if (typeof val === 'function') return val;192193 if (val === true) {194 // Support plain true/false195 return function(){ return true };196 }197198 if (typeof val === 'number') {199 // Support trusting hop count200 return function(a, i){ return i < val };201 }202203 if (typeof val === 'string') {204 // Support comma-separated values205 val = val.split(/ *, */);206 }207208 return proxyaddr.compile(val || []);209}210211/**212 * Set the charset in a given Content-Type string.213 *214 * @param {String} type215 * @param {String} charset216 * @return {String}217 * @api private218 */219Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 220–223why, one step back220exports.setCharset = function setCharset(type, charset) {221 if (!type || !charset) {222 return type;223 }4.6.1Douglas Christopher Wilsonbefore adoption112dbb2
lines 224–225why, one step back224225 // parse typeMerge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 226why, one step back226 var parsed = contentType.parse(type);4.6.1Douglas Christopher Wilsonbefore adoption112dbb2
lines 227–231why, one step back227228 // set charset229 parsed.parameters.charset = charset;230231 // format typeMerge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 232why, one step back232 return contentType.format(parsed);4.6.1Douglas Christopher Wilsonbefore adoption112dbb2
line 233why, one step back233};Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 234–246why, one step back234235/**236 * Parse an extended query string with qs.237 *238 * @return {Object}239 * @private240 */241242function parseExtendedQueryString(str) {243 return qs.parse(str, {244 allowPrototypes: true245 });246}