Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Core Functions for Clarity and Performance #6093

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,10 @@ app.engine = function engine(ext, fn) {
throw new Error('callback function required');
}

// get file extension
var extension = ext[0] !== '.'
? '.' + ext
: ext;
// Get file extension
const extension = ext.startsWith('.') ? ext : `.${ext}`;

// store engine
// Store engine function
this.engines[extension] = fn;

return this;
Expand Down
11 changes: 1 addition & 10 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,7 @@ defineGetter(req, 'query', function query(){
*/

req.is = function is(types) {
var arr = types;

// support flattened arguments
if (!Array.isArray(types)) {
arr = new Array(arguments.length);
for (var i = 0; i < arr.length; i++) {
arr[i] = arguments[i];
}
}

const arr = Array.isArray(types) ? types : Array.from(arguments);
return typeis(this, arr);
};

Expand Down
87 changes: 40 additions & 47 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ exports.wetag = createETagGenerator({ weak: true })
* @api private
*/

exports.normalizeType = function(type){
return ~type.indexOf('/')
exports.normalizeType = function(type) {
return type.includes('/')
? acceptParams(type)
: { value: (mime.lookup(type) || 'application/octet-stream'), params: {} }
: { value: mime.lookup(type) || 'application/octet-stream', params: {} };
};

/**
Expand All @@ -64,15 +64,16 @@ exports.normalizeType = function(type){
* @api private
*/

exports.normalizeTypes = function(types){
var ret = [];
exports.normalizeTypes = function(types) {
const len = types.length;
const ret = new Array(len);

for (var i = 0; i < types.length; ++i) {
ret.push(exports.normalizeType(types[i]));
for (let i = 0; i < len; i++) {
ret[i] = exports.normalizeType(types[i]);
}

return ret;
};
}

/**
* Parse accept params `str` returning an
Expand All @@ -83,16 +84,16 @@ exports.normalizeTypes = function(types){
* @api private
*/

function acceptParams (str) {
var parts = str.split(/ *; */);
var ret = { value: parts[0], quality: 1, params: {} }
function acceptParams(str) {
const parts = str.split(/ *; */);
const ret = { value: parts[0], quality: 1, params: {} };

for (var i = 1; i < parts.length; ++i) {
var pms = parts[i].split(/ *= */);
if ('q' === pms[0]) {
ret.quality = parseFloat(pms[1]);
for (let i = 1; i < parts.length; i++) {
const [key, value] = parts[i].split(/ *= */);
if (key === 'q') {
ret.quality = parseFloat(value);
} else {
ret.params[pms[0]] = pms[1];
ret.params[key] = value;
}
}

Expand All @@ -108,27 +109,23 @@ function acceptParams (str) {
*/

exports.compileETag = function(val) {
var fn;

if (typeof val === 'function') {
return val;
}

if (val === false) {
return;
}

switch (val) {
case true:
case 'weak':
fn = exports.wetag;
break;
case false:
break;
return exports.wetag;
case 'strong':
fn = exports.etag;
break;
return exports.etag;
default:
throw new TypeError('unknown value for etag function: ' + val);
throw new TypeError(`unknown value for etag function: ${val}`);
}

return fn;
}

/**
Expand All @@ -139,28 +136,24 @@ exports.compileETag = function(val) {
* @api private
*/

exports.compileQueryParser = function compileQueryParser(val) {
var fn;

exports.compileQueryParser = function (val) {
if (typeof val === 'function') {
return val;
}

if (val === false) {
return;
}

switch (val) {
case true:
case 'simple':
fn = querystring.parse;
break;
case false:
break;
return querystring.parse;
case 'extended':
fn = parseExtendedQueryString;
break;
return parseExtendedQueryString;
default:
throw new TypeError('unknown value for query parser function: ' + val);
throw new TypeError(`unknown value for query parser function: ${val}`);
}

return fn;
}

/**
Expand Down Expand Up @@ -226,16 +219,16 @@ exports.setCharset = function setCharset(type, charset) {
* @private
*/

function createETagGenerator (options) {
return function generateETag (body, encoding) {
var buf = !Buffer.isBuffer(body)
? Buffer.from(body, encoding)
: body

return etag(buf, options)
}
function createETagGenerator(options) {
return (body, encoding) => {
const buf = Buffer.isBuffer(body)
? body
: Buffer.from(body, encoding);
return etag(buf, options);
};
}


/**
* Parse an extended query string with qs.
*
Expand Down
27 changes: 10 additions & 17 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,23 +167,17 @@ View.prototype.render = function render(options, callback) {
*/

View.prototype.resolve = function resolve(dir, file) {
var ext = this.ext;
const ext = this.ext;

// <path>.<ext>
var path = join(dir, file);
var stat = tryStat(path);
// Check <path>.<ext>
let path = join(dir, file);
if (tryStat(path)?.isFile()) return path;

if (stat && stat.isFile()) {
return path;
}

// <path>/index.<ext>
path = join(dir, basename(file, ext), 'index' + ext);
stat = tryStat(path);
// Check <path>/index.<ext>
path = join(dir, basename(file, ext), `index${ext}`);
if (tryStat(path)?.isFile()) return path;

if (stat && stat.isFile()) {
return path;
}
return undefined;
};

/**
Expand All @@ -195,11 +189,10 @@ View.prototype.resolve = function resolve(dir, file) {
*/

function tryStat(path) {
debug('stat "%s"', path);

debug(`stat "${path}"`);
try {
return fs.statSync(path);
} catch (e) {
} catch {
return undefined;
}
}
Loading