utils.js
This file
Path
lib/utils.js
256 lines · 4.7 KiB · JavaScript
Last changed by
Remove utils.contentDispositionDouglas Christopher Wilsonbefore adoptionbe35e49
History of this fileTake it
The walk stopped after 200 commits: lines older than ca306ea point at it.
deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionca306ea
lines 1–5why, one step back1/**2 * Module dependencies.3 */45var mime = require('send').mime;Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
line 6why, one step back6var etag = require('etag');deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionca306ea
line 7why, one step back7var proxyaddr = require('proxy-addr');Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
lines 8–9why, one step back8var qs = require('qs');9var querystring = require('querystring');deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionca306ea
lines 10–20why, one step back10var typer = require('media-typer');1112/**13 * Return strong ETag for `body`.14 *15 * @param {String|Buffer} body16 * @param {String} [encoding]17 * @return {String}18 * @api private19 */20Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
lines 21–23why, one step back21exports.etag = function (body, encoding) {22 var buf = !Buffer.isBuffer(body)23 ? new Buffer(body, encoding)Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 24why, one step back24 : body;deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionca306ea
line 25why, one step back25Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 26why, one step back26 return etag(buf, {weak: false});deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionca306ea
lines 27–38why, one step back27};2829/**30 * Return weak ETag for `body`.31 *32 * @param {String|Buffer} body33 * @param {String} [encoding]34 * @return {String}35 * @api private36 */3738exports.wetag = function wetag(body, encoding){Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
lines 39–40why, one step back39 var buf = !Buffer.isBuffer(body)40 ? new Buffer(body, encoding)Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 41why, one step back41 : body;deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionca306ea
line 42why, one step back42Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 43why, one step back43 return etag(buf, {weak: true});deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionca306ea
lines 44–173why, one step back44};4546/**47 * Check if `path` looks absolute.48 *49 * @param {String} path50 * @return {Boolean}51 * @api private52 */5354exports.isAbsolute = function(path){55 if ('/' == path[0]) return true;56 if (':' == path[1] && '\\' == path[2]) return true;57 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute path58};5960/**61 * Flatten the given `arr`.62 *63 * @param {Array} arr64 * @return {Array}65 * @api private66 */6768exports.flatten = function(arr, ret){69 ret = ret || [];70 var len = arr.length;71 for (var i = 0; i < len; ++i) {72 if (Array.isArray(arr[i])) {73 exports.flatten(arr[i], ret);74 } else {75 ret.push(arr[i]);76 }77 }78 return ret;79};8081/**82 * Normalize the given `type`, for example "html" becomes "text/html".83 *84 * @param {String} type85 * @return {Object}86 * @api private87 */8889exports.normalizeType = function(type){90 return ~type.indexOf('/')91 ? acceptParams(type)92 : { value: mime.lookup(type), params: {} };93};9495/**96 * Normalize `types`, for example "html" becomes "text/html".97 *98 * @param {Array} types99 * @return {Array}100 * @api private101 */102103exports.normalizeTypes = function(types){104 var ret = [];105106 for (var i = 0; i < types.length; ++i) {107 ret.push(exports.normalizeType(types[i]));108 }109110 return ret;111};112113/**114 * Parse accept params `str` returning an115 * object with `.value`, `.quality` and `.params`.116 * also includes `.originalIndex` for stable sorting117 *118 * @param {String} str119 * @return {Object}120 * @api private121 */122123function acceptParams(str, index) {124 var parts = str.split(/ *; */);125 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };126127 for (var i = 1; i < parts.length; ++i) {128 var pms = parts[i].split(/ *= */);129 if ('q' == pms[0]) {130 ret.quality = parseFloat(pms[1]);131 } else {132 ret.params[pms[0]] = pms[1];133 }134 }135136 return ret;137}138139/**140 * Compile "etag" value to function.141 *142 * @param {Boolean|String|Function} val143 * @return {Function}144 * @api private145 */146147exports.compileETag = function(val) {148 var fn;149150 if (typeof val === 'function') {151 return val;152 }153154 switch (val) {155 case true:156 fn = exports.wetag;157 break;158 case false:159 break;160 case 'strong':161 fn = exports.etag;162 break;163 case 'weak':164 fn = exports.wetag;165 break;166 default:167 throw new TypeError('unknown value for etag function: ' + val);168 }169170 return fn;171}172173/**Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
lines 174–207why, one step back174 * Compile "query parser" value to function.175 *176 * @param {String|Function} val177 * @return {Function}178 * @api private179 */180181exports.compileQueryParser = function compileQueryParser(val) {182 var fn;183184 if (typeof val === 'function') {185 return val;186 }187188 switch (val) {189 case true:190 fn = querystring.parse;191 break;192 case false:193 break;194 case 'extended':195 fn = qs.parse;196 break;197 case 'simple':198 fn = querystring.parse;199 break;200 default:201 throw new TypeError('unknown value for query parser function: ' + val);202 }203204 return fn;205}206207/**deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionca306ea
lines 208–256why, one step back208 * Compile "proxy trust" value to function.209 *210 * @param {Boolean|String|Number|Array|Function} val211 * @return {Function}212 * @api private213 */214215exports.compileTrust = function(val) {216 if (typeof val === 'function') return val;217218 if (val === true) {219 // Support plain true/false220 return function(){ return true };221 }222223 if (typeof val === 'number') {224 // Support trusting hop count225 return function(a, i){ return i < val };226 }227228 if (typeof val === 'string') {229 // Support comma-separated values230 val = val.split(/ *, */);231 }232233 return proxyaddr.compile(val || []);234}235236/**237 * Set the charset in a given Content-Type string.238 *239 * @param {String} type240 * @param {String} charset241 * @return {String}242 * @api private243 */244245exports.setCharset = function(type, charset){246 if (!type || !charset) return type;247248 // parse type249 var parsed = typer.parse(type);250251 // set charset252 parsed.parameters.charset = charset;253254 // format type255 return typer.format(parsed);256};