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

Always use strict equality #1225

Merged
merged 2 commits into from May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/application.js
Expand Up @@ -197,7 +197,7 @@ module.exports = class Application extends Emitter {
onerror(err) {
if (!(err instanceof Error)) throw new TypeError(util.format('non-error thrown: %j', err));

if (404 == err.status || err.expose) return;
if (404 === err.status || err.expose) return;
if (this.silent) return;

const msg = err.stack || err.toString();
Expand Down Expand Up @@ -257,7 +257,7 @@ function respond(ctx) {

// responses
if (Buffer.isBuffer(body)) return res.end(body);
if ('string' == typeof body) return res.end(body);
if ('string' === typeof body) return res.end(body);
if (body instanceof Stream) return body.pipe(res);

// body: json
Expand Down
4 changes: 2 additions & 2 deletions lib/context.js
Expand Up @@ -144,10 +144,10 @@ const proto = module.exports = {
this.type = 'text';

// ENOENT support
if ('ENOENT' == err.code) err.status = 404;
if ('ENOENT' === err.code) err.status = 404;

// default to 500
if ('number' != typeof err.status || !statuses[err.status]) err.status = 500;
if ('number' !== typeof err.status || !statuses[err.status]) err.status = 500;

// respond
const code = statuses[err.status];
Expand Down
10 changes: 5 additions & 5 deletions lib/request.js
Expand Up @@ -272,7 +272,7 @@ module.exports = {
get hostname() {
const host = this.host;
if (!host) return '';
if ('[' == host[0]) return this.URL.hostname || ''; // IPv6
if ('[' === host[0]) return this.URL.hostname || ''; // IPv6
return host.split(':', 1)[0];
},

Expand Down Expand Up @@ -311,10 +311,10 @@ module.exports = {
const s = this.ctx.status;

// GET or HEAD for weak freshness validation only
if ('GET' != method && 'HEAD' != method) return false;
if ('GET' !== method && 'HEAD' !== method) return false;

// 2xx or 304 as per rfc2616 14.26
if ((s >= 200 && s < 300) || 304 == s) {
if ((s >= 200 && s < 300) || 304 === s) {
return fresh(this.header, this.response.header);
}

Expand Down Expand Up @@ -382,7 +382,7 @@ module.exports = {

get length() {
const len = this.get('Content-Length');
if (len == '') return;
if (len === '') return;
return ~~len;
},

Expand Down Expand Up @@ -415,7 +415,7 @@ module.exports = {
*/

get secure() {
return 'https' == this.protocol;
return 'https' === this.protocol;
},

/**
Expand Down
12 changes: 6 additions & 6 deletions lib/response.js
Expand Up @@ -153,7 +153,7 @@ module.exports = {
const setType = !this.has('Content-Type');

// string
if ('string' == typeof val) {
if ('string' === typeof val) {
if (setType) this.type = /^\s*</.test(val) ? 'html' : 'text';
this.length = Buffer.byteLength(val);
return;
Expand All @@ -167,12 +167,12 @@ module.exports = {
}

// stream
if ('function' == typeof val.pipe) {
if ('function' === typeof val.pipe) {
onFinish(this.res, destroy.bind(null, val));
ensureErrorHandler(val, err => this.ctx.onerror(err));

// overwriting
if (null != original && original != val) this.remove('Content-Length');
if (null != original && original !== val) this.remove('Content-Length');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (null != original && original !== val) this.remove('Content-Length');
if (null !== original && original !== val) this.remove('Content-Length');

Copy link
Contributor Author

@medanat medanat Apr 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the nature of type coercion with nulls, I'm not sure this will be a safe move.

null == undefined -> true, null === undefined -> false


if (setType) this.type = 'bin';
return;
Expand Down Expand Up @@ -258,7 +258,7 @@ module.exports = {

redirect(url, alt) {
// location
if ('back' == url) url = this.ctx.get('Referrer') || alt || '/';
if ('back' === url) url = this.ctx.get('Referrer') || alt || '/';
this.set('Location', encodeUrl(url));

// status
Expand Down Expand Up @@ -325,7 +325,7 @@ module.exports = {
*/

set lastModified(val) {
if ('string' == typeof val) val = new Date(val);
if ('string' === typeof val) val = new Date(val);
this.set('Last-Modified', val.toUTCString());
},

Expand Down Expand Up @@ -458,7 +458,7 @@ module.exports = {
set(field, val) {
if (this.headerSent) return;

if (2 == arguments.length) {
if (2 === arguments.length) {
if (Array.isArray(val)) val = val.map(v => typeof v === 'string' ? v : String(v));
else if (typeof val !== 'string') val = String(val);
this.res.setHeader(field, val);
Expand Down