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 fileTake it
The walk stopped after 200 commits: lines older than 9024d24 point at it.
deps: supertest@~0.13.0Douglas Christopher Wilsonbefore adoption9024d24
lines 1–6why, one step back1/**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 back7var etag = require('etag');deps: supertest@~0.13.0Douglas Christopher Wilsonbefore adoption9024d24
line 8why, one step back8var proxyaddr = require('proxy-addr');Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
lines 9–10why, one step back9var qs = require('qs');10var querystring = require('querystring');Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff
line 11why, one step back11var typer = require('media-typer');deps: supertest@~0.13.0Douglas Christopher Wilsonbefore adoption9024d24
lines 12–21why, one step back1213/**14 * Return strong ETag for `body`.15 *16 * @param {String|Buffer} body17 * @param {String} [encoding]18 * @return {String}19 * @api private20 */21Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
lines 22–25why, one step back22exports.etag = function (body, encoding) {23 var buf = !Buffer.isBuffer(body)24 ? new Buffer(body, encoding)25 : bodydeps: supertest@~0.13.0Douglas Christopher Wilsonbefore adoption9024d24
line 26why, one step back26Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
line 27why, one step back27 return etag(buf, {weak: false})deps: supertest@~0.13.0Douglas Christopher Wilsonbefore adoption9024d24
lines 28–39why, one step back28};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 back40 var buf = !Buffer.isBuffer(body)41 ? new Buffer(body, encoding)42 : bodydeps: supertest@~0.13.0Douglas Christopher Wilsonbefore adoption9024d24
line 43why, one step back43Use etag to generate ETag headersDouglas Christopher Wilsonbefore adoption51d33ed
line 44why, one step back44 return etag(buf, {weak: true})deps: supertest@~0.13.0Douglas Christopher Wilsonbefore adoption9024d24
lines 45–128why, one step back45};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 back129 ? 'attachment; filename="' + encodeURI(filename) + '"; filename*=UTF-8\'\'' + encodeURI(filename)deps: supertest@~0.13.0Douglas Christopher Wilsonbefore adoption9024d24
lines 130–196why, one step back130 : '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 back197 * 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: supertest@~0.13.0Douglas Christopher Wilsonbefore adoption9024d24
lines 232–271why, one step back232 * 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;271Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff
lines 272–273why, one step back272 // parse type273 var parsed = typer.parse(type);deps: supertest@~0.13.0Douglas Christopher Wilsonbefore adoption9024d24
line 274why, one step back274Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff
lines 275–276why, one step back275 // set charset276 parsed.parameters.charset = charset;deps: supertest@~0.13.0Douglas Christopher Wilsonbefore adoption9024d24
line 277why, one step back277Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff
lines 278–279why, one step back278 // format type279 return typer.format(parsed);deps: supertest@~0.13.0Douglas Christopher Wilsonbefore adoption9024d24
line 280why, one step back280};Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25
lines 281–291why, one step back281282/**283 * Return new empty objet.284 *285 * @return {Object}286 * @api private287 */288289function newObject() {290 return {};291}