Skip to content
Myr

utils.js

This file

Path

lib/utils.js

262 lines · 4.8 KiB · JavaScript

Last changed by

Remove utils.flattenDouglas Christopher Wilsonbefore adoption6c75119

History of this file

Take it

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 13–15why, one step back
13 */
1415var mime = require('send').mime;

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 18why, one step back
18var flatten = require('array-flatten');

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

line 19why, one step back
19var proxyaddr = require('proxy-addr');

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

lines 20–21why, one step back
20var qs = require('qs');
21var querystring = require('querystring');

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 22–31why, one step back
22
23/**24 * Return strong ETag for `body`.25 *26 * @param {String|Buffer} body27 * @param {String} [encoding]28 * @return {String}29 * @api private30 */31

Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed

lines 32–34why, one step back
32exports.etag = function (body, encoding) {
33 var buf = !Buffer.isBuffer(body)34 ? new Buffer(body, encoding)

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 35why, one step back
35 : body;

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

line 36why, one step back
36

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 37why, one step back
37 return etag(buf, {weak: false});

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 38–49why, one step back
38};
3940/**41 * Return weak ETag for `body`.42 *43 * @param {String|Buffer} body44 * @param {String} [encoding]45 * @return {String}46 * @api private47 */4849exports.wetag = function wetag(body, encoding){

Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed

lines 50–51why, one step back
50 var buf = !Buffer.isBuffer(body)
51 ? new Buffer(body, encoding)

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 52why, one step back
52 : body;

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

line 53why, one step back
53

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 54why, one step back
54 return etag(buf, {weak: true});

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 55–163why, one step back
55};
5657/**58 * Check if `path` looks absolute.59 *60 * @param {String} path61 * @return {Boolean}62 * @api private63 */6465exports.isAbsolute = function(path){66 if ('/' == path[0]) return true;67 if (':' == path[1] && '\\' == path[2]) return true;68 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute path69};7071/**72 * Normalize the given `type`, for example "html" becomes "text/html".73 *74 * @param {String} type75 * @return {Object}76 * @api private77 */7879exports.normalizeType = function(type){80 return ~type.indexOf('/')81 ? acceptParams(type)82 : { value: mime.lookup(type), params: {} };83};8485/**86 * Normalize `types`, for example "html" becomes "text/html".87 *88 * @param {Array} types89 * @return {Array}90 * @api private91 */9293exports.normalizeTypes = function(types){94 var ret = [];9596 for (var i = 0; i < types.length; ++i) {97 ret.push(exports.normalizeType(types[i]));98 }99100 return ret;101};102103/**104 * Parse accept params `str` returning an105 * object with `.value`, `.quality` and `.params`.106 * also includes `.originalIndex` for stable sorting107 *108 * @param {String} str109 * @return {Object}110 * @api private111 */112113function acceptParams(str, index) {114 var parts = str.split(/ *; */);115 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };116117 for (var i = 1; i < parts.length; ++i) {118 var pms = parts[i].split(/ *= */);119 if ('q' == pms[0]) {120 ret.quality = parseFloat(pms[1]);121 } else {122 ret.params[pms[0]] = pms[1];123 }124 }125126 return ret;127}128129/**130 * Compile "etag" value to function.131 *132 * @param {Boolean|String|Function} val133 * @return {Function}134 * @api private135 */136137exports.compileETag = function(val) {138 var fn;139140 if (typeof val === 'function') {141 return val;142 }143144 switch (val) {145 case true:146 fn = exports.wetag;147 break;148 case false:149 break;150 case 'strong':151 fn = exports.etag;152 break;153 case 'weak':154 fn = exports.wetag;155 break;156 default:157 throw new TypeError('unknown value for etag function: ' + val);158 }159160 return fn;161}162163/**

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

lines 164–184why, one step back
164 * Compile "query parser" value to function.
165 *166 * @param {String|Function} val167 * @return {Function}168 * @api private169 */170171exports.compileQueryParser = function compileQueryParser(val) {172 var fn;173174 if (typeof val === 'function') {175 return val;176 }177178 switch (val) {179 case true:180 fn = querystring.parse;181 break;182 case false:183 break;184 case 'extended':

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

lines 186–197why, one step back
186 break;
187 case 'simple':188 fn = querystring.parse;189 break;190 default:191 throw new TypeError('unknown value for query parser function: ' + val);192 }193194 return fn;195}196197/**

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 198–234why, one step back
198 * Compile "proxy trust" value to function.
199 *200 * @param {Boolean|String|Number|Array|Function} val201 * @return {Function}202 * @api private203 */204205exports.compileTrust = function(val) {206 if (typeof val === 'function') return val;207208 if (val === true) {209 // Support plain true/false210 return function(){ return true };211 }212213 if (typeof val === 'number') {214 // Support trusting hop count215 return function(a, i){ return i < val };216 }217218 if (typeof val === 'string') {219 // Support comma-separated values220 val = val.split(/ *, */);221 }222223 return proxyaddr.compile(val || []);224}225226/**227 * Set the charset in a given Content-Type string.228 *229 * @param {String} type230 * @param {String} charset231 * @return {String}232 * @api private233 */234

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 235–238why, one step back
235exports.setCharset = function setCharset(type, charset) {
236 if (!type || !charset) {237 return type;238 }

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 239–240why, one step back
239
240 // parse type

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 242–246why, one step back
242
243 // set charset244 parsed.parameters.charset = charset;245246 // format type

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

line 248why, one step back
248};

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 249–262why, one step back
249
250/**251 * Parse an extended query string with qs.252 *253 * @return {Object}254 * @private255 */256257function parseExtendedQueryString(str) {258 return qs.parse(str, {259 allowDots: false,260 allowPrototypes: true261 });262}