Skip to content
Myr

utils.js

This file

Path

lib/utils.js

302 lines · 5.8 KiB · JavaScript

Last changed by

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

History of this file

Take it

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

fix package.json to reflect supported node versionDouglas Christopher Wilsonbefore adoptionc054581

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

Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d

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

fix package.json to reflect supported node versionDouglas Christopher Wilsonbefore adoptionc054581

line 8why, one step back
8var basename = require('path').basename;

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

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

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

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

Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff

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

Merge tag '3.6.0'Douglas Christopher Wilsonbefore adoption35c5060

lines 13–14why, one step back
13
14/**

Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d

line 15why, one step back
15 * Return strong ETag for `body`.

fix package.json to reflect supported node versionDouglas Christopher Wilsonbefore adoptionc054581

lines 16–17why, one step back
16 *
17 * @param {String|Buffer} body

Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d

line 18why, one step back
18 * @param {String} [encoding]

fix package.json to reflect supported node versionDouglas Christopher Wilsonbefore adoptionc054581

lines 19–22why, one step back
19 * @return {String}
20 * @api private21 */22

Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d

lines 23–33why, one step back
23exports.etag = function etag(body, encoding){
24 if (body.length === 0) {25 // fast-path empty body26 return '"1B2M2Y8AsgTpgAmY7PhCfg=="'27 }2829 var hash = crypto30 .createHash('md5')31 .update(body, encoding)32 .digest('base64')33 return '"' + hash + '"'

fix package.json to reflect supported node versionDouglas Christopher Wilsonbefore adoptionc054581

lines 34–36why, one step back
34};
3536/**

Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d

lines 37–58why, one step back
37 * Return weak ETag for `body`.
38 *39 * @param {String|Buffer} body40 * @param {String} [encoding]41 * @return {String}42 * @api private43 */4445exports.wetag = function wetag(body, encoding){46 if (body.length === 0) {47 // fast-path empty body48 return 'W/"0-0"'49 }5051 var buf = Buffer.isBuffer(body)52 ? body53 : new Buffer(body, encoding)54 var len = buf.length55 return 'W/"' + len.toString(16) + '-' + crc32.unsigned(buf) + '"'56};5758/**

fix package.json to reflect supported node versionDouglas Christopher Wilsonbefore adoptionc054581

lines 59–139why, one step back
59 * Check if `path` looks absolute.
60 *61 * @param {String} path62 * @return {Boolean}63 * @api private64 */6566exports.isAbsolute = function(path){67 if ('/' == path[0]) return true;68 if (':' == path[1] && '\\' == path[2]) return true;69 if ('\\\\' == path.substring(0, 2)) return true; // Microsoft Azure absolute path70};7172/**73 * Flatten the given `arr`.74 *75 * @param {Array} arr76 * @return {Array}77 * @api private78 */7980exports.flatten = function(arr, ret){81 ret = ret || [];82 var len = arr.length;83 for (var i = 0; i < len; ++i) {84 if (Array.isArray(arr[i])) {85 exports.flatten(arr[i], ret);86 } else {87 ret.push(arr[i]);88 }89 }90 return ret;91};9293/**94 * Normalize the given `type`, for example "html" becomes "text/html".95 *96 * @param {String} type97 * @return {Object}98 * @api private99 */100101exports.normalizeType = function(type){102 return ~type.indexOf('/')103 ? acceptParams(type)104 : { value: mime.lookup(type), params: {} };105};106107/**108 * Normalize `types`, for example "html" becomes "text/html".109 *110 * @param {Array} types111 * @return {Array}112 * @api private113 */114115exports.normalizeTypes = function(types){116 var ret = [];117118 for (var i = 0; i < types.length; ++i) {119 ret.push(exports.normalizeType(types[i]));120 }121122 return ret;123};124125/**126 * Generate Content-Disposition header appropriate for the filename.127 * non-ascii filenames are urlencoded and a filename* parameter is added128 *129 * @param {String} filename130 * @return {String}131 * @api private132 */133134exports.contentDisposition = function(filename){135 var ret = 'attachment';136 if (filename) {137 filename = basename(filename);138 // if filename contains non-ascii characters, add a utf-8 version ala RFC 5987139 ret = /[^\040-\176]/.test(filename)

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

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

fix package.json to reflect supported node versionDouglas Christopher Wilsonbefore adoptionc054581

lines 141–170why, one step back
141 : 'attachment; filename="' + filename + '"';
142 }143144 return ret;145};146147/**148 * Parse accept params `str` returning an149 * object with `.value`, `.quality` and `.params`.150 * also includes `.originalIndex` for stable sorting151 *152 * @param {String} str153 * @return {Object}154 * @api private155 */156157function acceptParams(str, index) {158 var parts = str.split(/ *; */);159 var ret = { value: parts[0], quality: 1, params: {}, originalIndex: index };160161 for (var i = 1; i < parts.length; ++i) {162 var pms = parts[i].split(/ *= */);163 if ('q' == pms[0]) {164 ret.quality = parseFloat(pms[1]);165 } else {166 ret.params[pms[0]] = pms[1];167 }168 }169170 return ret;

Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d

lines 171–204why, one step back
171}
172173/**174 * Compile "etag" value to function.175 *176 * @param {Boolean|String|Function} val177 * @return {Function}178 * @api private179 */180181exports.compileETag = function(val) {182 var fn;183184 if (typeof val === 'function') {185 return val;186 }187188 switch (val) {189 case true:190 fn = exports.wetag;191 break;192 case false:193 break;194 case 'strong':195 fn = exports.etag;196 break;197 case 'weak':198 fn = exports.wetag;199 break;200 default:201 throw new TypeError('unknown value for etag function: ' + val);202 }203204 return fn;

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

lines 205–207why, one step back
205}
206207/**

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

lines 208–242why, one step back
208 * Compile "query parser" value to function.
209 *210 * @param {String|Function} val211 * @return {Function}212 * @api private213 */214215exports.compileQueryParser = function compileQueryParser(val) {216 var fn;217218 if (typeof val === 'function') {219 return val;220 }221222 switch (val) {223 case true:224 fn = querystring.parse;225 break;226 case false:227 fn = newObject;228 break;229 case 'extended':230 fn = qs.parse;231 break;232 case 'simple':233 fn = querystring.parse;234 break;235 default:236 throw new TypeError('unknown value for query parser function: ' + val);237 }238239 return fn;240}241242/**

Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c

lines 243–268why, one step back
243 * Compile "proxy trust" value to function.
244 *245 * @param {Boolean|String|Number|Array|Function} val246 * @return {Function}247 * @api private248 */249250exports.compileTrust = function(val) {251 if (typeof val === 'function') return val;252253 if (val === true) {254 // Support plain true/false255 return function(){ return true };256 }257258 if (typeof val === 'number') {259 // Support trusting hop count260 return function(a, i){ return i < val };261 }262263 if (typeof val === 'string') {264 // Support comma-separated values265 val = val.split(/ *, */);266 }267268 return proxyaddr.compile(val || []);

fix package.json to reflect supported node versionDouglas Christopher Wilsonbefore adoptionc054581

line 269why, one step back
269}

Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061

lines 270–282why, one step back
270
271/**272 * Set the charset in a given Content-Type string.273 *274 * @param {String} type275 * @param {String} charset276 * @return {String}277 * @api private278 */279280exports.setCharset = function(type, charset){281 if (!type || !charset) return type;282

Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff

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

Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061

line 285why, one step back
285

Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff

lines 286–287why, one step back
286 // set charset
287 parsed.parameters.charset = charset;

Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061

line 288why, one step back
288

Merge tag '3.12.0'Douglas Christopher Wilsonbefore adoption3ffceff

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

Merge tag '3.8.0'Douglas Christopher Wilsonbefore adoptiondb4a061

line 291why, one step back
291};

Add configurable query parserDouglas Christopher Wilsonbefore adoptione29fa25

lines 292–302why, one step back
292
293/**294 * Return new empty objet.295 *296 * @return {Object}297 * @api private298 */299300function newObject() {301 return {};302}