Skip to content
Myr

utils.js

This file

Path

lib/utils.js

266 lines · 4.9 KiB · JavaScript

Last changed by

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 8–9why, one step back
8/**
9 * Module dependencies.

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

line 10why, one step back
10 * @api private

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

line 14why, one step back
14var contentType = require('content-type');

Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed

line 15why, one step back
15var etag = require('etag');

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

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

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 19–28why, one step back
19
20/**21 * Return strong ETag for `body`.22 *23 * @param {String|Buffer} body24 * @param {String} [encoding]25 * @return {String}26 * @api private27 */28

Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed

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

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 32why, one step back
32 : body;

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

line 33why, one step back
33

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 35–46why, one step back
35};
3637/**38 * Return weak ETag for `body`.39 *40 * @param {String|Buffer} body41 * @param {String} [encoding]42 * @return {String}43 * @api private44 */4546exports.wetag = function wetag(body, encoding){

Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed

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

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 49why, one step back
49 : body;

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

line 50why, one step back
50

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 52–181why, one step back
52};
5354/**55 * Check if `path` looks absolute.56 *57 * @param {String} path58 * @return {Boolean}59 * @api private60 */6162exports.isAbsolute = function(path){63 if ('/' == path[0]) return true;64 if (':' == path[1] && '\\' == path[2]) return true;65 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute path66};6768/**69 * Flatten the given `arr`.70 *71 * @param {Array} arr72 * @return {Array}73 * @api private74 */7576exports.flatten = function(arr, ret){77 ret = ret || [];78 var len = arr.length;79 for (var i = 0; i < len; ++i) {80 if (Array.isArray(arr[i])) {81 exports.flatten(arr[i], ret);82 } else {83 ret.push(arr[i]);84 }85 }86 return ret;87};8889/**90 * Normalize the given `type`, for example "html" becomes "text/html".91 *92 * @param {String} type93 * @return {Object}94 * @api private95 */9697exports.normalizeType = function(type){98 return ~type.indexOf('/')99 ? acceptParams(type)100 : { value: mime.lookup(type), params: {} };101};102103/**104 * Normalize `types`, for example "html" becomes "text/html".105 *106 * @param {Array} types107 * @return {Array}108 * @api private109 */110111exports.normalizeTypes = function(types){112 var ret = [];113114 for (var i = 0; i < types.length; ++i) {115 ret.push(exports.normalizeType(types[i]));116 }117118 return ret;119};120121/**122 * Parse accept params `str` returning an123 * object with `.value`, `.quality` and `.params`.124 * also includes `.originalIndex` for stable sorting125 *126 * @param {String} str127 * @return {Object}128 * @api private129 */130131function acceptParams(str, index) {132 var parts = str.split(/ *; */);133 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };134135 for (var i = 1; i < parts.length; ++i) {136 var pms = parts[i].split(/ *= */);137 if ('q' == pms[0]) {138 ret.quality = parseFloat(pms[1]);139 } else {140 ret.params[pms[0]] = pms[1];141 }142 }143144 return ret;145}146147/**148 * Compile "etag" value to function.149 *150 * @param {Boolean|String|Function} val151 * @return {Function}152 * @api private153 */154155exports.compileETag = function(val) {156 var fn;157158 if (typeof val === 'function') {159 return val;160 }161162 switch (val) {163 case true:164 fn = exports.wetag;165 break;166 case false:167 break;168 case 'strong':169 fn = exports.etag;170 break;171 case 'weak':172 fn = exports.wetag;173 break;174 default:175 throw new TypeError('unknown value for etag function: ' + val);176 }177178 return fn;179}180181/**

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

lines 182–215why, one step back
182 * Compile "query parser" value to function.
183 *184 * @param {String|Function} val185 * @return {Function}186 * @api private187 */188189exports.compileQueryParser = function compileQueryParser(val) {190 var fn;191192 if (typeof val === 'function') {193 return val;194 }195196 switch (val) {197 case true:198 fn = querystring.parse;199 break;200 case false:201 break;202 case 'extended':203 fn = qs.parse;204 break;205 case 'simple':206 fn = querystring.parse;207 break;208 default:209 throw new TypeError('unknown value for query parser function: ' + val);210 }211212 return fn;213}214215/**

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 216–252why, one step back
216 * Compile "proxy trust" value to function.
217 *218 * @param {Boolean|String|Number|Array|Function} val219 * @return {Function}220 * @api private221 */222223exports.compileTrust = function(val) {224 if (typeof val === 'function') return val;225226 if (val === true) {227 // Support plain true/false228 return function(){ return true };229 }230231 if (typeof val === 'number') {232 // Support trusting hop count233 return function(a, i){ return i < val };234 }235236 if (typeof val === 'string') {237 // Support comma-separated values238 val = val.split(/ *, */);239 }240241 return proxyaddr.compile(val || []);242}243244/**245 * Set the charset in a given Content-Type string.246 *247 * @param {String} type248 * @param {String} charset249 * @return {String}250 * @api private251 */252

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 253–256why, one step back
253exports.setCharset = function setCharset(type, charset) {
254 if (!type || !charset) {255 return type;256 }

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 257–258why, one step back
257
258 // parse type

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

lines 260–264why, one step back
260
261 // set charset262 parsed.parameters.charset = charset;263264 // format type

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

deps: send@0.5.0Douglas Christopher Wilsonbefore adoption3136e98

line 266why, one step back
266};