Skip to content
Myr

utils.js

This file

Path

lib/utils.js

291 lines · 5.5 KiB · JavaScript

Last changed by

Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed

History of this file

Take it

The walk stopped after 200 commits: lines older than bffb71d point at it.

deps: proxy-addr@1.0.1Douglas Christopher Wilsonbefore adoptionbffb71d

lines 1–6why, one step back
1/**
2 * Module dependencies.3 */45var mime = require('send').mime;6var basename = require('path').basename;

Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed

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

deps: proxy-addr@1.0.1Douglas Christopher Wilsonbefore adoptionbffb71d

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

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

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

Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff

line 11why, one step back
11var typer = require('media-typer');

deps: proxy-addr@1.0.1Douglas Christopher Wilsonbefore adoptionbffb71d

lines 12–21why, one step back
12
13/**14 * Return strong ETag for `body`.15 *16 * @param {String|Buffer} body17 * @param {String} [encoding]18 * @return {String}19 * @api private20 */21

Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed

lines 22–25why, one step back
22exports.etag = function (body, encoding) {
23 var buf = !Buffer.isBuffer(body)24 ? new Buffer(body, encoding)25 : body

deps: proxy-addr@1.0.1Douglas Christopher Wilsonbefore adoptionbffb71d

line 26why, one step back
26

Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed

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

deps: proxy-addr@1.0.1Douglas Christopher Wilsonbefore adoptionbffb71d

lines 28–39why, one step back
28};
2930/**31 * Return weak ETag for `body`.32 *33 * @param {String|Buffer} body34 * @param {String} [encoding]35 * @return {String}36 * @api private37 */3839exports.wetag = function wetag(body, encoding){

Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed

lines 40–42why, one step back
40 var buf = !Buffer.isBuffer(body)
41 ? new Buffer(body, encoding)42 : body

deps: proxy-addr@1.0.1Douglas Christopher Wilsonbefore adoptionbffb71d

line 43why, one step back
43

Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed

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

deps: proxy-addr@1.0.1Douglas Christopher Wilsonbefore adoptionbffb71d

lines 45–128why, one step back
45};
4647/**48 * Check if `path` looks absolute.49 *50 * @param {String} path51 * @return {Boolean}52 * @api private53 */5455exports.isAbsolute = function(path){56 if ('/' == path[0]) return true;57 if (':' == path[1] && '\\' == path[2]) return true;58 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute path59};6061/**62 * Flatten the given `arr`.63 *64 * @param {Array} arr65 * @return {Array}66 * @api private67 */6869exports.flatten = function(arr, ret){70 ret = ret || [];71 var len = arr.length;72 for (var i = 0; i < len; ++i) {73 if (Array.isArray(arr[i])) {74 exports.flatten(arr[i], ret);75 } else {76 ret.push(arr[i]);77 }78 }79 return ret;80};8182/**83 * Normalize the given `type`, for example "html" becomes "text/html".84 *85 * @param {String} type86 * @return {Object}87 * @api private88 */8990exports.normalizeType = function(type){91 return ~type.indexOf('/')92 ? acceptParams(type)93 : { value: mime.lookup(type), params: {} };94};9596/**97 * Normalize `types`, for example "html" becomes "text/html".98 *99 * @param {Array} types100 * @return {Array}101 * @api private102 */103104exports.normalizeTypes = function(types){105 var ret = [];106107 for (var i = 0; i < types.length; ++i) {108 ret.push(exports.normalizeType(types[i]));109 }110111 return ret;112};113114/**115 * Generate Content-Disposition header appropriate for the filename.116 * non-ascii filenames are urlencoded and a filename* parameter is added117 *118 * @param {String} filename119 * @return {String}120 * @api private121 */122123exports.contentDisposition = function(filename){124 var ret = 'attachment';125 if (filename) {126 filename = basename(filename);127 // if filename contains non-ascii characters, add a utf-8 version ala RFC 5987128 ret = /[^\040-\176]/.test(filename)

Fix res.attachment Unicode filenames in Safari刘星before adoptione606d99

line 129why, one step back
129 ? 'attachment; filename="' + encodeURI(filename) + '"; filename*=UTF-8\'\'' + encodeURI(filename)

deps: proxy-addr@1.0.1Douglas Christopher Wilsonbefore adoptionbffb71d

lines 130–196why, one step back
130 : 'attachment; filename="' + filename + '"';
131 }132133 return ret;134};135136/**137 * Parse accept params `str` returning an138 * object with `.value`, `.quality` and `.params`.139 * also includes `.originalIndex` for stable sorting140 *141 * @param {String} str142 * @return {Object}143 * @api private144 */145146function acceptParams(str, index) {147 var parts = str.split(/ *; */);148 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };149150 for (var i = 1; i < parts.length; ++i) {151 var pms = parts[i].split(/ *= */);152 if ('q' == pms[0]) {153 ret.quality = parseFloat(pms[1]);154 } else {155 ret.params[pms[0]] = pms[1];156 }157 }158159 return ret;160}161162/**163 * Compile "etag" value to function.164 *165 * @param {Boolean|String|Function} val166 * @return {Function}167 * @api private168 */169170exports.compileETag = function(val) {171 var fn;172173 if (typeof val === 'function') {174 return val;175 }176177 switch (val) {178 case true:179 fn = exports.wetag;180 break;181 case false:182 break;183 case 'strong':184 fn = exports.etag;185 break;186 case 'weak':187 fn = exports.wetag;188 break;189 default:190 throw new TypeError('unknown value for etag function: ' + val);191 }192193 return fn;194}195196/**

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

lines 197–231why, one step back
197 * Compile "query parser" value to function.
198 *199 * @param {String|Function} val200 * @return {Function}201 * @api private202 */203204exports.compileQueryParser = function compileQueryParser(val) {205 var fn;206207 if (typeof val === 'function') {208 return val;209 }210211 switch (val) {212 case true:213 fn = querystring.parse;214 break;215 case false:216 fn = newObject;217 break;218 case 'extended':219 fn = qs.parse;220 break;221 case 'simple':222 fn = querystring.parse;223 break;224 default:225 throw new TypeError('unknown value for query parser function: ' + val);226 }227228 return fn;229}230231/**

deps: proxy-addr@1.0.1Douglas Christopher Wilsonbefore adoptionbffb71d

lines 232–271why, one step back
232 * Compile "proxy trust" value to function.
233 *234 * @param {Boolean|String|Number|Array|Function} val235 * @return {Function}236 * @api private237 */238239exports.compileTrust = function(val) {240 if (typeof val === 'function') return val;241242 if (val === true) {243 // Support plain true/false244 return function(){ return true };245 }246247 if (typeof val === 'number') {248 // Support trusting hop count249 return function(a, i){ return i < val };250 }251252 if (typeof val === 'string') {253 // Support comma-separated values254 val = val.split(/ *, */);255 }256257 return proxyaddr.compile(val || []);258}259260/**261 * Set the charset in a given Content-Type string.262 *263 * @param {String} type264 * @param {String} charset265 * @return {String}266 * @api private267 */268269exports.setCharset = function(type, charset){270 if (!type || !charset) return type;271

Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff

lines 272–273why, one step back
272 // parse type
273 var parsed = typer.parse(type);

deps: proxy-addr@1.0.1Douglas Christopher Wilsonbefore adoptionbffb71d

line 274why, one step back
274

Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff

lines 275–276why, one step back
275 // set charset
276 parsed.parameters.charset = charset;

deps: proxy-addr@1.0.1Douglas Christopher Wilsonbefore adoptionbffb71d

line 277why, one step back
277

Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff

lines 278–279why, one step back
278 // format type
279 return typer.format(parsed);

deps: proxy-addr@1.0.1Douglas Christopher Wilsonbefore adoptionbffb71d

line 280why, one step back
280};

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

lines 281–291why, one step back
281
282/**283 * Return new empty objet.284 *285 * @return {Object}286 * @api private287 */288289function newObject() {290 return {};291}