Skip to content
Myr

utils.js

This file

Path

lib/utils.js

306 lines · 5.8 KiB · JavaScript

Last changed by

Use safe-buffer for improved Buffer APIDouglas Christopher Wilsonbefore adoption12c3712

History of this file

Take it

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

build: mocha@3.4.1Douglas Christopher Wilsonbefore adoption6549469

lines 1–14why, one step back
1/*!
2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2014-2015 Douglas Christopher Wilson5 * MIT Licensed6 */78'use strict';910/**11 * Module dependencies.12 * @api private13 */14

Use safe-buffer for improved Buffer APIDouglas Christopher Wilsonbefore adoption12c3712

line 15why, one step back
15var Buffer = require('safe-buffer').Buffer

build: mocha@3.4.1Douglas Christopher Wilsonbefore adoption6549469

lines 16–34why, one step back
16var contentDisposition = require('content-disposition');
17var contentType = require('content-type');18var deprecate = require('depd')('express');19var flatten = require('array-flatten');20var mime = require('send').mime;21var etag = require('etag');22var proxyaddr = require('proxy-addr');23var qs = require('qs');24var querystring = require('querystring');2526/**27 * Return strong ETag for `body`.28 *29 * @param {String|Buffer} body30 * @param {String} [encoding]31 * @return {String}32 * @api private33 */34

perf: re-use options object when generating ETagsLawrence Pagebefore adoptiond9d09b8

line 35why, one step back
35exports.etag = createETagGenerator({ weak: false })

build: mocha@3.4.1Douglas Christopher Wilsonbefore adoption6549469

lines 36–45why, one step back
36
37/**38 * Return weak ETag for `body`.39 *40 * @param {String|Buffer} body41 * @param {String} [encoding]42 * @return {String}43 * @api private44 */45

perf: re-use options object when generating ETagsLawrence Pagebefore adoptiond9d09b8

line 46why, one step back
46exports.wetag = createETagGenerator({ weak: true })

build: mocha@3.4.1Douglas Christopher Wilsonbefore adoption6549469

lines 47–263why, one step back
47
48/**49 * Check if `path` looks absolute.50 *51 * @param {String} path52 * @return {Boolean}53 * @api private54 */5556exports.isAbsolute = function(path){57 if ('/' === path[0]) return true;58 if (':' === path[1] && ('\\' === path[2] || '/' === path[2])) return true; // Windows device path59 if ('\\\\' === path.substring(0, 2)) return true; // Microsoft Azure absolute path60};6162/**63 * Flatten the given `arr`.64 *65 * @param {Array} arr66 * @return {Array}67 * @api private68 */6970exports.flatten = deprecate.function(flatten,71 'utils.flatten: use array-flatten npm module instead');7273/**74 * Normalize the given `type`, for example "html" becomes "text/html".75 *76 * @param {String} type77 * @return {Object}78 * @api private79 */8081exports.normalizeType = function(type){82 return ~type.indexOf('/')83 ? acceptParams(type)84 : { value: mime.lookup(type), params: {} };85};8687/**88 * Normalize `types`, for example "html" becomes "text/html".89 *90 * @param {Array} types91 * @return {Array}92 * @api private93 */9495exports.normalizeTypes = function(types){96 var ret = [];9798 for (var i = 0; i < types.length; ++i) {99 ret.push(exports.normalizeType(types[i]));100 }101102 return ret;103};104105/**106 * Generate Content-Disposition header appropriate for the filename.107 * non-ascii filenames are urlencoded and a filename* parameter is added108 *109 * @param {String} filename110 * @return {String}111 * @api private112 */113114exports.contentDisposition = deprecate.function(contentDisposition,115 'utils.contentDisposition: use content-disposition npm module instead');116117/**118 * Parse accept params `str` returning an119 * object with `.value`, `.quality` and `.params`.120 * also includes `.originalIndex` for stable sorting121 *122 * @param {String} str123 * @return {Object}124 * @api private125 */126127function acceptParams(str, index) {128 var parts = str.split(/ *; */);129 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };130131 for (var i = 1; i < parts.length; ++i) {132 var pms = parts[i].split(/ *= */);133 if ('q' === pms[0]) {134 ret.quality = parseFloat(pms[1]);135 } else {136 ret.params[pms[0]] = pms[1];137 }138 }139140 return ret;141}142143/**144 * Compile "etag" value to function.145 *146 * @param {Boolean|String|Function} val147 * @return {Function}148 * @api private149 */150151exports.compileETag = function(val) {152 var fn;153154 if (typeof val === 'function') {155 return val;156 }157158 switch (val) {159 case true:160 fn = exports.wetag;161 break;162 case false:163 break;164 case 'strong':165 fn = exports.etag;166 break;167 case 'weak':168 fn = exports.wetag;169 break;170 default:171 throw new TypeError('unknown value for etag function: ' + val);172 }173174 return fn;175}176177/**178 * Compile "query parser" value to function.179 *180 * @param {String|Function} val181 * @return {Function}182 * @api private183 */184185exports.compileQueryParser = function compileQueryParser(val) {186 var fn;187188 if (typeof val === 'function') {189 return val;190 }191192 switch (val) {193 case true:194 fn = querystring.parse;195 break;196 case false:197 fn = newObject;198 break;199 case 'extended':200 fn = parseExtendedQueryString;201 break;202 case 'simple':203 fn = querystring.parse;204 break;205 default:206 throw new TypeError('unknown value for query parser function: ' + val);207 }208209 return fn;210}211212/**213 * Compile "proxy trust" value to function.214 *215 * @param {Boolean|String|Number|Array|Function} val216 * @return {Function}217 * @api private218 */219220exports.compileTrust = function(val) {221 if (typeof val === 'function') return val;222223 if (val === true) {224 // Support plain true/false225 return function(){ return true };226 }227228 if (typeof val === 'number') {229 // Support trusting hop count230 return function(a, i){ return i < val };231 }232233 if (typeof val === 'string') {234 // Support comma-separated values235 val = val.split(/ *, */);236 }237238 return proxyaddr.compile(val || []);239}240241/**242 * Set the charset in a given Content-Type string.243 *244 * @param {String} type245 * @param {String} charset246 * @return {String}247 * @api private248 */249250exports.setCharset = function setCharset(type, charset) {251 if (!type || !charset) {252 return type;253 }254255 // parse type256 var parsed = contentType.parse(type);257258 // set charset259 parsed.parameters.charset = charset;260261 // format type262 return contentType.format(parsed);263};

perf: re-use options object when generating ETagsLawrence Pagebefore adoptiond9d09b8

lines 264–276why, one step back
264
265/**266 * Create an ETag generator function, generating ETags with267 * the given options.268 *269 * @param {object} options270 * @return {function}271 * @private272 */273274function createETagGenerator (options) {275 return function generateETag (body, encoding) {276 var buf = !Buffer.isBuffer(body)

Use safe-buffer for improved Buffer APIDouglas Christopher Wilsonbefore adoption12c3712

line 277why, one step back
277 ? Buffer.from(body, encoding)

perf: re-use options object when generating ETagsLawrence Pagebefore adoptiond9d09b8

lines 278–282why, one step back
278 : body
279280 return etag(buf, options)281 }282}

build: mocha@3.4.1Douglas Christopher Wilsonbefore adoption6549469

lines 283–306why, one step back
283
284/**285 * Parse an extended query string with qs.286 *287 * @return {Object}288 * @private289 */290291function parseExtendedQueryString(str) {292 return qs.parse(str, {293 allowPrototypes: true294 });295}296297/**298 * Return new empty object.299 *300 * @return {Object}301 * @api private302 */303304function newObject() {305 return {};306}