Skip to content

Commit 5f64974

Browse files
anikethsahahiroppy
authored andcommitted
refactor: extract complex if condition to improve readability (#2105)
* chore: extract complex if condition to improve perf * chore: better names and more extract * chore: changed name from isHostname to isValidHostname
1 parent fa73768 commit 5f64974

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

client-src/default/utils/createSocketUrl.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ function createSocketUrl(resourceQuery) {
3232
// why do we need this check?
3333
// hostname n/a for file protocol (example, when using electron, ionic)
3434
// see: https://github.com/webpack/webpack-dev-server/pull/384
35-
if (
35+
const isAnyHostname =
3636
(hostname === '0.0.0.0' || hostname === '::') &&
3737
self.location.hostname &&
3838
// eslint-disable-next-line no-bitwise
39-
!!~self.location.protocol.indexOf('http')
40-
) {
39+
!!~self.location.protocol.indexOf('http');
40+
41+
if (isAnyHostname) {
4142
hostname = self.location.hostname;
4243
}
4344

@@ -58,7 +59,8 @@ function createSocketUrl(resourceQuery) {
5859
let sockPort = urlParts.port;
5960

6061
// eslint-disable-next-line no-undefined
61-
if (path !== null && path !== undefined && path !== '/') {
62+
const shouldParsePath = path !== null && path !== undefined && path !== '/';
63+
if (shouldParsePath) {
6264
const parsedQuery = querystring.parse(path);
6365
// all of these sock url params are optionally passed in through
6466
// resourceQuery, so we need to fall back to the default if

lib/Server.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -870,13 +870,18 @@ class Server {
870870
// an IPv6-address in URLs,
871871
// these are removed from the hostname in url.parse(),
872872
// so we have the pure IPv6-address in hostname.
873-
if (ip.isV4Format(hostname) || ip.isV6Format(hostname)) {
873+
// always allow localhost host, for convenience (hostname === 'localhost')
874+
// allow hostname of listening address (hostname === this.hostname)
875+
const isValidHostname =
876+
ip.isV4Format(hostname) ||
877+
ip.isV6Format(hostname) ||
878+
hostname === 'localhost' ||
879+
hostname === this.hostname;
880+
881+
if (isValidHostname) {
874882
return true;
875883
}
876884
// always allow localhost host, for convenience
877-
if (hostname === 'localhost') {
878-
return true;
879-
}
880885
// allow if hostname is in allowedHosts
881886
if (this.allowedHosts && this.allowedHosts.length) {
882887
for (let hostIdx = 0; hostIdx < this.allowedHosts.length; hostIdx++) {
@@ -889,23 +894,18 @@ class Server {
889894
// support "." as a subdomain wildcard
890895
// e.g. ".example.com" will allow "example.com", "www.example.com", "subdomain.example.com", etc
891896
if (allowedHost[0] === '.') {
892-
// "example.com"
893-
if (hostname === allowedHost.substring(1)) {
894-
return true;
895-
}
896-
// "*.example.com"
897-
if (hostname.endsWith(allowedHost)) {
897+
// "example.com" (hostname === allowedHost.substring(1))
898+
// "*.example.com" (hostname.endsWith(allowedHost))
899+
if (
900+
hostname === allowedHost.substring(1) ||
901+
hostname.endsWith(allowedHost)
902+
) {
898903
return true;
899904
}
900905
}
901906
}
902907
}
903908

904-
// allow hostname of listening address
905-
if (hostname === this.hostname) {
906-
return true;
907-
}
908-
909909
// also allow public hostname if provided
910910
if (typeof this.publicHost === 'string') {
911911
const idxPublic = this.publicHost.indexOf(':');
@@ -955,13 +955,14 @@ class Server {
955955

956956
// send stats to a socket or multiple sockets
957957
_sendStats(sockets, stats, force) {
958-
if (
958+
const shouldEmit =
959959
!force &&
960960
stats &&
961961
(!stats.errors || stats.errors.length === 0) &&
962962
stats.assets &&
963-
stats.assets.every((asset) => !asset.emitted)
964-
) {
963+
stats.assets.every((asset) => !asset.emitted);
964+
965+
if (shouldEmit) {
965966
return this.sockWrite(sockets, 'still-ok');
966967
}
967968

0 commit comments

Comments
 (0)