Skip to content

Commit 9c56a5a

Browse files
authored
refactor(server): use this.options.xxx (#2558)
BREAKING CHANGE: only for Node.js API, all options are now in `devServer.options`
1 parent e826119 commit 9c56a5a

File tree

5 files changed

+99
-87
lines changed

5 files changed

+99
-87
lines changed

lib/Server.js

Lines changed: 44 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -43,76 +43,36 @@ class Server {
4343

4444
this.compiler = compiler;
4545
this.options = options;
46-
46+
this.sockets = [];
47+
this.contentBaseWatchers = [];
48+
// Keep track of websocket proxies for external websocket upgrade.
49+
this.websocketProxies = [];
4750
this.log = _log || createLogger(options);
48-
49-
if (this.options.transportMode !== undefined) {
50-
this.log.warn(
51-
'transportMode is an experimental option, meaning its usage could potentially change without warning'
52-
);
53-
}
51+
// this value of ws can be overwritten for tests
52+
this.wsHeartbeatInterval = 30000;
5453

5554
normalizeOptions(this.compiler, this.options);
56-
57-
// don't move this position because addEntries called by updateCompiler checks this.options.hot|hotOnly
58-
this.options.hot =
59-
typeof this.options.hot === 'boolean' || this.options.hot === 'only'
60-
? this.options.hot
61-
: true;
62-
6355
updateCompiler(this.compiler, this.options);
6456

65-
this.heartbeatInterval = 30000;
66-
// this.SocketServerImplementation is a class, so it must be instantiated before use
67-
this.socketServerImplementation = getSocketServerImplementation(
57+
this.SocketServerImplementation = getSocketServerImplementation(
6858
this.options
6959
);
7060

71-
this.originalStats =
72-
this.options.stats && Object.keys(this.options.stats).length
73-
? this.options.stats
74-
: {};
75-
76-
this.sockets = [];
77-
this.contentBaseWatchers = [];
78-
79-
// TODO this.<property> is deprecated (remove them in next major release.) in favor this.options.<property>
80-
this.headers = this.options.headers;
81-
this.progress = this.options.progress;
82-
83-
this.serveIndex = this.options.serveIndex;
84-
85-
this.clientOverlay = this.options.overlay;
86-
this.clientLogLevel = this.options.clientLogLevel;
87-
88-
this.publicHost = this.options.public;
89-
this.allowedHosts = this.options.allowedHosts;
90-
this.disableHostCheck = !!this.options.disableHostCheck;
91-
92-
this.watchOptions = options.watchOptions || {};
93-
94-
if (this.progress) {
61+
if (this.options.progress) {
9562
this.setupProgressPlugin();
9663
}
97-
9864
this.setupHooks();
9965
this.setupApp();
10066
this.setupCheckHostRoute();
10167
this.setupDevMiddleware();
102-
103-
// set express routes
104-
routes(this);
105-
106-
// Keep track of websocket proxies for external websocket upgrade.
107-
this.websocketProxies = [];
108-
10968
this.setupFeatures();
11069
this.setupHttps();
11170
this.createServer();
11271

72+
routes(this);
11373
killable(this.listeningApp);
11474

115-
// Proxy websockets without the initial http request
75+
// Proxy WebSocket without the initial http request
11676
// https://github.com/chimurai/http-proxy-middleware#external-websocket-upgrade
11777
this.websocketProxies.forEach(function (wsProxy) {
11878
this.listeningApp.on('upgrade', wsProxy.upgrade);
@@ -163,8 +123,8 @@ class Server {
163123
compile.tap('webpack-dev-server', invalidPlugin);
164124
invalid.tap('webpack-dev-server', invalidPlugin);
165125
done.tap('webpack-dev-server', (stats) => {
166-
this._sendStats(this.sockets, this.getStats(stats));
167-
this._stats = stats;
126+
this.sendStats(this.sockets, this.getStats(stats));
127+
this.stats = stats;
168128
});
169129
};
170130

@@ -534,10 +494,7 @@ class Server {
534494
}
535495
}
536496

537-
// checking if it's set to true or not set (Default : undefined => true)
538-
this.serveIndex = this.serveIndex || this.serveIndex === undefined;
539-
540-
if (this.options.contentBase && this.serveIndex) {
497+
if (this.options.contentBase && this.options.serveIndex) {
541498
runnableFeatures.push('contentBaseIndex');
542499
}
543500

@@ -625,8 +582,7 @@ class Server {
625582
}
626583

627584
createSocketServer() {
628-
const SocketServerImplementation = this.socketServerImplementation;
629-
this.socketServer = new SocketServerImplementation(this);
585+
this.socketServer = new this.SocketServerImplementation(this);
630586

631587
this.socketServer.onConnection((connection, headers) => {
632588
if (!connection) {
@@ -658,8 +614,8 @@ class Server {
658614
}
659615
});
660616

661-
if (this.clientLogLevel) {
662-
this.sockWrite([connection], 'log-level', this.clientLogLevel);
617+
if (this.options.clientLogLevel) {
618+
this.sockWrite([connection], 'log-level', this.options.clientLogLevel);
663619
}
664620

665621
if (this.options.hot === true || this.options.hot === 'only') {
@@ -671,19 +627,19 @@ class Server {
671627
this.sockWrite([connection], 'liveReload', this.options.liveReload);
672628
}
673629

674-
if (this.progress) {
675-
this.sockWrite([connection], 'progress', this.progress);
630+
if (this.options.progress) {
631+
this.sockWrite([connection], 'progress', this.options.progress);
676632
}
677633

678-
if (this.clientOverlay) {
679-
this.sockWrite([connection], 'overlay', this.clientOverlay);
634+
if (this.options.clientOverlay) {
635+
this.sockWrite([connection], 'overlay', this.options.clientOverlay);
680636
}
681637

682-
if (!this._stats) {
638+
if (!this.stats) {
683639
return;
684640
}
685641

686-
this._sendStats([connection], this.getStats(this._stats), true);
642+
this.sendStats([connection], this.getStats(this.stats), true);
687643
});
688644
}
689645

@@ -755,8 +711,8 @@ class Server {
755711
getStats(statsObj) {
756712
const stats = Server.DEFAULT_STATS;
757713

758-
if (this.originalStats.warningsFilter) {
759-
stats.warningsFilter = this.originalStats.warningsFilter;
714+
if (this.options.stats.warningsFilter) {
715+
stats.warningsFilter = this.options.stats.warningsFilter;
760716
}
761717

762718
return statsObj.toJson(stats);
@@ -768,10 +724,10 @@ class Server {
768724
}
769725

770726
setContentHeaders(req, res, next) {
771-
if (this.headers) {
727+
if (this.options.headers) {
772728
// eslint-disable-next-line
773-
for (const name in this.headers) {
774-
res.setHeader(name, this.headers[name]);
729+
for (const name in this.options.headers) {
730+
res.setHeader(name, this.options.headers[name]);
775731
}
776732
}
777733

@@ -788,7 +744,7 @@ class Server {
788744

789745
checkHeaders(headers, headerToCheck) {
790746
// allow user to opt-out this security check, at own risk
791-
if (this.disableHostCheck) {
747+
if (this.options.disableHostCheck) {
792748
return true;
793749
}
794750

@@ -828,11 +784,14 @@ class Server {
828784
if (isValidHostname) {
829785
return true;
830786
}
787+
788+
const allowedHosts = this.options.allowedHosts;
789+
831790
// always allow localhost host, for convenience
832791
// allow if hostname is in allowedHosts
833-
if (this.allowedHosts && this.allowedHosts.length) {
834-
for (let hostIdx = 0; hostIdx < this.allowedHosts.length; hostIdx++) {
835-
const allowedHost = this.allowedHosts[hostIdx];
792+
if (allowedHosts && allowedHosts.length) {
793+
for (let hostIdx = 0; hostIdx < allowedHosts.length; hostIdx++) {
794+
const allowedHost = allowedHosts[hostIdx];
836795

837796
if (allowedHost === hostname) {
838797
return true;
@@ -854,10 +813,12 @@ class Server {
854813
}
855814

856815
// also allow public hostname if provided
857-
if (typeof this.publicHost === 'string') {
858-
const idxPublic = this.publicHost.indexOf(':');
816+
if (typeof this.options.public === 'string') {
817+
const idxPublic = this.options.public.indexOf(':');
859818
const publicHostname =
860-
idxPublic >= 0 ? this.publicHost.substr(0, idxPublic) : this.publicHost;
819+
idxPublic >= 0
820+
? this.options.public.substr(0, idxPublic)
821+
: this.options.public;
861822

862823
if (hostname === publicHostname) {
863824
return true;
@@ -896,7 +857,7 @@ class Server {
896857
}
897858

898859
// send stats to a socket or multiple sockets
899-
_sendStats(sockets, stats, force) {
860+
sendStats(sockets, stats, force) {
900861
const shouldEmit =
901862
!force &&
902863
stats &&
@@ -923,10 +884,10 @@ class Server {
923884
// duplicate the same massaging of options that watchpack performs
924885
// https://github.com/webpack/watchpack/blob/master/lib/DirectoryWatcher.js#L49
925886
// this isn't an elegant solution, but we'll improve it in the future
926-
const usePolling = this.watchOptions.poll ? true : undefined;
887+
const usePolling = this.options.watchOptions.poll ? true : undefined;
927888
const interval =
928-
typeof this.watchOptions.poll === 'number'
929-
? this.watchOptions.poll
889+
typeof this.options.watchOptions.poll === 'number'
890+
? this.options.watchOptions.poll
930891
: undefined;
931892

932893
const watchOptions = {
@@ -936,7 +897,7 @@ class Server {
936897
atomic: false,
937898
alwaysStat: true,
938899
ignorePermissionErrors: true,
939-
ignored: this.watchOptions.ignored,
900+
ignored: this.options.watchOptions.ignored,
940901
usePolling,
941902
interval,
942903
};

lib/servers/WebsocketServer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module.exports = class WebsocketServer extends BaseServer {
3939
socket.isAlive = false;
4040
socket.ping(noop);
4141
});
42-
}, this.server.heartbeatInterval);
42+
}, this.server.wsHeartbeatInterval);
4343
}
4444

4545
send(connection, message) {

lib/utils/normalizeOptions.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@ function normalizeOptions(compiler, options) {
88
// Setup default value
99
options.contentBase =
1010
options.contentBase !== undefined ? options.contentBase : process.cwd();
11-
12-
// Setup default value
1311
options.contentBasePublicPath = options.contentBasePublicPath || '/';
12+
options.hot =
13+
typeof options.hot === 'boolean' || options.hot === 'only'
14+
? options.hot
15+
: true;
16+
options.serveIndex =
17+
options.serveIndex !== undefined ? options.serveIndex : true;
18+
options.watchOptions = options.watchOptions || {};
19+
options.stats =
20+
options.stats && Object.keys(options.stats).length !== 0
21+
? options.stats
22+
: {};
1423

1524
// normalize transportMode option
1625
if (options.transportMode === undefined) {

test/server/servers/WebsocketServer.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('WebsocketServer', () => {
2828
},
2929
},
3030
listeningApp,
31-
heartbeatInterval: 800,
31+
wsHeartbeatInterval: 800,
3232
};
3333
socketServer = new WebsocketServer(server);
3434
done();

0 commit comments

Comments
 (0)