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

lib: refactor code with startsWith/endsWith #5753

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions lib/_debug_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ Client.prototype._transform = function _transform(data, enc, cb) {
while (true) {
if (this.state === 'headers') {
// Not enough data
if (!/\r\n/.test(this.buffer))
if (!this.buffer.includes('\r\n'))
break;

if (/^\r\n/.test(this.buffer)) {
if (this.buffer.startsWith('\r\n')) {
this.buffer = this.buffer.slice(2);
this.state = 'body';
continue;
Expand Down
2 changes: 1 addition & 1 deletion lib/_debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,7 @@ Interface.prototype.setBreakpoint = function(script, line,
}

let req;
if (/\(\)$/.test(script)) {
if (script.endsWith('()')) {
// setBreakpoint('functionname()');
req = {
type: 'function',
Expand Down
5 changes: 2 additions & 3 deletions lib/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,8 @@ function masterInit() {
// Without --logfile=v8-%p.log, everything ends up in a single, unusable
// file. (Unusable because what V8 logs are memory addresses and each
// process has its own memory mappings.)
if (settings.execArgv.some(function(s) { return /^--prof/.test(s); }) &&
!settings.execArgv.some(function(s) { return /^--logfile=/.test(s); }))
{
if (settings.execArgv.some((s) => s.startsWith('--prof')) &&
!settings.execArgv.some((s) => s.startsWith('--logfile='))) {
settings.execArgv = settings.execArgv.concat(['--logfile=v8-%p.log']);
}
cluster.settings = settings;
Expand Down
10 changes: 5 additions & 5 deletions lib/os.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ exports.platform = function() {
return process.platform;
};

const trailingSlashRe = isWindows ? /[^:]\\$/
: /.\/$/;

exports.tmpdir = function() {
var path;
if (isWindows) {
path = process.env.TEMP ||
process.env.TMP ||
(process.env.SystemRoot || process.env.windir) + '\\temp';
if (path.length > 1 && path.endsWith('\\') && !path.endsWith(':\\'))
path = path.slice(0, -1);
} else {
path = process.env.TMPDIR ||
process.env.TMP ||
process.env.TEMP ||
'/tmp';
if (path.length > 1 && path.endsWith('/'))
path = path.slice(0, -1);
}
if (trailingSlashRe.test(path))
path = path.slice(0, -1);

return path;
};

Expand Down
2 changes: 1 addition & 1 deletion lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ Interface.prototype._normalWrite = function(b) {
this._line_buffer = null;
}
if (newPartContainsEnding) {
this._sawReturn = /\r$/.test(string);
this._sawReturn = string.endsWith('\r');

// got one or more newlines; process into "line" events
var lines = string.split(lineEnding);
Expand Down
2 changes: 1 addition & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ function REPLServer(prompt,
self.memory(cmd);

self.wrappedCmd = false;
if (e && !self.bufferedCommand && cmd.trim().match(/^npm /)) {
if (e && !self.bufferedCommand && cmd.trim().startsWith('npm ')) {
self.outputStream.write('npm should be run outside of the ' +
'node repl, in your normal shell.\n' +
'(Press Control-D to exit.)\n');
Expand Down
4 changes: 2 additions & 2 deletions lib/tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ exports.checkServerIdentity = function checkServerIdentity(host, cert) {
// Create regexp to much hostnames
function regexpify(host, wildcards) {
// Add trailing dot (make hostnames uniform)
if (!/\.$/.test(host)) host += '.';
if (!host || !host.endsWith('.')) host += '.';
Copy link
Contributor

Choose a reason for hiding this comment

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

What does the !host check add?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the host maybe undefined.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can it really? In that case, the old code would have produced "undefined.". Does that code path really exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes. If no !host path, make test will fails.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, it wouldn't have blown up, but would've turned undefined into "undefined", before appending a period to it. My point is, this was never a bug, so do we need that !host check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need it here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, my bad. I get it now.


// The same applies to hostname with more than one wildcard,
// if hostname has wildcard when wildcards are not allowed,
Expand Down Expand Up @@ -144,7 +144,7 @@ exports.checkServerIdentity = function checkServerIdentity(host, cert) {
}
} else if (cert.subject) {
// Transform hostname to canonical form
if (!/\.$/.test(host)) host += '.';
if (!host || !host.endsWith('.')) host += '.';

// Otherwise check all DNS/URI records from certificate
// (with allowed wildcards)
Expand Down