From 99d089d25aefd190bfa04f669819deb991a67626 Mon Sep 17 00:00:00 2001 From: Yuta Hiroto Date: Mon, 5 Aug 2019 19:04:22 +0100 Subject: [PATCH] refactor(server): remove some variables of options (#2175) --- lib/Server.js | 61 +++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/lib/Server.js b/lib/Server.js index 4edba202de..4e2c85489e 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -83,21 +83,7 @@ class Server { this.sockets = []; this.contentBaseWatchers = []; - - // TODO this. is deprecated (remove them in next major release.) in favor this.options. this.hot = this.options.hot || this.options.hotOnly; - this.headers = this.options.headers; - this.progress = this.options.progress; - - this.serveIndex = this.options.serveIndex; - - this.clientOverlay = this.options.overlay; - this.clientLogLevel = this.options.clientLogLevel; - - this.publicHost = this.options.public; - this.allowedHosts = this.options.allowedHosts; - this.disableHostCheck = !!this.options.disableHostCheck; - this.watchOptions = options.watchOptions || {}; // Replace leading and trailing slashes to normalize path @@ -107,7 +93,7 @@ class Server { : 'sockjs-node' }`; - if (this.progress) { + if (this.options.progress) { this.setupProgressPlugin(); } @@ -532,9 +518,10 @@ class Server { } // checking if it's set to true or not set (Default : undefined => true) - this.serveIndex = this.serveIndex || this.serveIndex === undefined; + this.options.serveIndex = + this.options.serveIndex || this.options.serveIndex === undefined; - if (this.options.contentBase && this.serveIndex) { + if (this.options.contentBase && this.options.serveIndex) { runnableFeatures.push('contentBaseIndex'); } @@ -691,8 +678,8 @@ class Server { } }); - if (this.clientLogLevel) { - this.sockWrite([connection], 'log-level', this.clientLogLevel); + if (this.options.clientLogLevel) { + this.sockWrite([connection], 'log-level', this.options.clientLogLevel); } if (this.hot) { @@ -704,12 +691,12 @@ class Server { this.sockWrite([connection], 'liveReload', this.options.liveReload); } - if (this.progress) { - this.sockWrite([connection], 'progress', this.progress); + if (this.options.progress) { + this.sockWrite([connection], 'progress', this.options.progress); } - if (this.clientOverlay) { - this.sockWrite([connection], 'overlay', this.clientOverlay); + if (this.options.overlay) { + this.sockWrite([connection], 'overlay', this.options.overlay); } if (!this._stats) { @@ -802,10 +789,10 @@ class Server { } setContentHeaders(req, res, next) { - if (this.headers) { + if (this.options.headers) { // eslint-disable-next-line - for (const name in this.headers) { - res.setHeader(name, this.headers[name]); + for (const name in this.options.headers) { + res.setHeader(name, this.options.headers[name]); } } @@ -821,8 +808,10 @@ class Server { } checkHeaders(headers, headerToCheck) { + const optionsDisableHostCheck = !!this.options.disableHostCheck; + // allow user to opt-out this security check, at own risk - if (this.disableHostCheck) { + if (optionsDisableHostCheck) { return true; } @@ -864,9 +853,13 @@ class Server { } // always allow localhost host, for convenience // allow if hostname is in allowedHosts - if (this.allowedHosts && this.allowedHosts.length) { - for (let hostIdx = 0; hostIdx < this.allowedHosts.length; hostIdx++) { - const allowedHost = this.allowedHosts[hostIdx]; + if (this.options.allowedHosts && this.options.allowedHosts.length) { + for ( + let hostIdx = 0; + hostIdx < this.options.allowedHosts.length; + hostIdx++ + ) { + const allowedHost = this.options.allowedHosts[hostIdx]; if (allowedHost === hostname) { return true; @@ -888,10 +881,12 @@ class Server { } // also allow public hostname if provided - if (typeof this.publicHost === 'string') { - const idxPublic = this.publicHost.indexOf(':'); + if (typeof this.options.public === 'string') { + const idxPublic = this.options.public.indexOf(':'); const publicHostname = - idxPublic >= 0 ? this.publicHost.substr(0, idxPublic) : this.publicHost; + idxPublic >= 0 + ? this.options.public.substr(0, idxPublic) + : this.options.public; if (hostname === publicHostname) { return true;