Skip to content

Commit 3b6a9f2

Browse files
committed
wip
1 parent ecab24b commit 3b6a9f2

File tree

11 files changed

+129
-106
lines changed

11 files changed

+129
-106
lines changed

lib/Server.js

Lines changed: 48 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ 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);
4851

4952
if (this.options.transportMode !== undefined) {
@@ -53,75 +56,34 @@ class Server {
5356
}
5457

5558
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-
6359
updateCompiler(this.compiler, this.options);
6460

65-
this.heartbeatInterval = 30000;
66-
// this.SocketServerImplementation is a class, so it must be instantiated before use
67-
this.socketServerImplementation = getSocketServerImplementation(
61+
// TODO: need to investigate
62+
// if this value is deleted, all transportMode will be broken
63+
// this.sockPath = this.options.sockPath;
64+
// this.sockPath = this.options.sockPath;
65+
66+
this.SocketServerImplementation = getSocketServerImplementation(
6867
this.options
6968
);
7069

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-
// Replace leading and trailing slashes to normalize path
95-
this.sockPath = `/${
96-
this.options.sockPath
97-
? this.options.sockPath.replace(/^\/|\/$/g, '')
98-
: 'sockjs-node'
99-
}`;
100-
101-
if (this.progress) {
70+
if (this.options.progress) {
10271
this.setupProgressPlugin();
10372
}
104-
10573
this.setupHooks();
10674
this.setupApp();
10775
this.setupCheckHostRoute();
10876
this.setupDevMiddleware();
109-
110-
// set express routes
111-
routes(this);
112-
113-
// Keep track of websocket proxies for external websocket upgrade.
114-
this.websocketProxies = [];
115-
11677
this.setupFeatures();
11778
this.setupHttps();
11879
this.createServer();
11980

81+
routes(this);
12082
killable(this.listeningApp);
12183

122-
// Proxy websockets without the initial http request
84+
// Proxy WebSockets without the initial http request
12385
// https://github.com/chimurai/http-proxy-middleware#external-websocket-upgrade
124-
this.websocketProxies.forEach(function (wsProxy) {
86+
this.websocketProxies.forEach((wsProxy) => {
12587
this.listeningApp.on('upgrade', wsProxy.upgrade);
12688
}, this);
12789
}
@@ -170,8 +132,8 @@ class Server {
170132
compile.tap('webpack-dev-server', invalidPlugin);
171133
invalid.tap('webpack-dev-server', invalidPlugin);
172134
done.tap('webpack-dev-server', (stats) => {
173-
this._sendStats(this.sockets, this.getStats(stats));
174-
this._stats = stats;
135+
this.sendStats(this.sockets, this.getStats(stats));
136+
this.stats = stats;
175137
});
176138
};
177139

@@ -554,10 +516,7 @@ class Server {
554516
}
555517
}
556518

557-
// checking if it's set to true or not set (Default : undefined => true)
558-
this.serveIndex = this.serveIndex || this.serveIndex === undefined;
559-
560-
if (this.options.contentBase && this.serveIndex) {
519+
if (this.options.contentBase && this.options.serveIndex) {
561520
runnableFeatures.push('contentBaseIndex');
562521
}
563522

@@ -664,8 +623,7 @@ class Server {
664623
}
665624

666625
createSocketServer() {
667-
const SocketServerImplementation = this.socketServerImplementation;
668-
this.socketServer = new SocketServerImplementation(this);
626+
this.socketServer = new this.SocketServerImplementation(this);
669627

670628
this.socketServer.onConnection((connection, headers) => {
671629
if (!connection) {
@@ -697,8 +655,8 @@ class Server {
697655
}
698656
});
699657

700-
if (this.clientLogLevel) {
701-
this.sockWrite([connection], 'log-level', this.clientLogLevel);
658+
if (this.options.clientLogLevel) {
659+
this.sockWrite([connection], 'log-level', this.options.clientLogLevel);
702660
}
703661

704662
if (this.options.hot === true || this.options.hot === 'only') {
@@ -710,19 +668,19 @@ class Server {
710668
this.sockWrite([connection], 'liveReload', this.options.liveReload);
711669
}
712670

713-
if (this.progress) {
714-
this.sockWrite([connection], 'progress', this.progress);
671+
if (this.options.progress) {
672+
this.sockWrite([connection], 'progress', this.options.progress);
715673
}
716674

717-
if (this.clientOverlay) {
718-
this.sockWrite([connection], 'overlay', this.clientOverlay);
675+
if (this.options.clientOverlay) {
676+
this.sockWrite([connection], 'overlay', this.options.clientOverlay);
719677
}
720678

721-
if (!this._stats) {
679+
if (!this.stats) {
722680
return;
723681
}
724682

725-
this._sendStats([connection], this.getStats(this._stats), true);
683+
this.sendStats([connection], this.getStats(this.stats), true);
726684
});
727685
}
728686

@@ -794,8 +752,8 @@ class Server {
794752
getStats(statsObj) {
795753
const stats = Server.DEFAULT_STATS;
796754

797-
if (this.originalStats.warningsFilter) {
798-
stats.warningsFilter = this.originalStats.warningsFilter;
755+
if (this.options.stats.warningsFilter) {
756+
stats.warningsFilter = this.options.stats.warningsFilter;
799757
}
800758

801759
return statsObj.toJson(stats);
@@ -807,10 +765,10 @@ class Server {
807765
}
808766

809767
setContentHeaders(req, res, next) {
810-
if (this.headers) {
768+
if (this.options.headers) {
811769
// eslint-disable-next-line
812-
for (const name in this.headers) {
813-
res.setHeader(name, this.headers[name]);
770+
for (const name in this.options.headers) {
771+
res.setHeader(name, this.options.headers[name]);
814772
}
815773
}
816774

@@ -827,7 +785,7 @@ class Server {
827785

828786
checkHeaders(headers, headerToCheck) {
829787
// allow user to opt-out this security check, at own risk
830-
if (this.disableHostCheck) {
788+
if (this.options.disableHostCheck) {
831789
return true;
832790
}
833791

@@ -867,11 +825,14 @@ class Server {
867825
if (isValidHostname) {
868826
return true;
869827
}
828+
829+
const allowedHosts = this.options.allowedHosts;
830+
870831
// always allow localhost host, for convenience
871832
// allow if hostname is in allowedHosts
872-
if (this.allowedHosts && this.allowedHosts.length) {
873-
for (let hostIdx = 0; hostIdx < this.allowedHosts.length; hostIdx++) {
874-
const allowedHost = this.allowedHosts[hostIdx];
833+
if (allowedHosts && allowedHosts.length) {
834+
for (let hostIdx = 0; hostIdx < allowedHosts.length; hostIdx++) {
835+
const allowedHost = allowedHosts[hostIdx];
875836

876837
if (allowedHost === hostname) {
877838
return true;
@@ -893,10 +854,12 @@ class Server {
893854
}
894855

895856
// also allow public hostname if provided
896-
if (typeof this.publicHost === 'string') {
897-
const idxPublic = this.publicHost.indexOf(':');
857+
if (typeof this.options.public === 'string') {
858+
const idxPublic = this.options.public.indexOf(':');
898859
const publicHostname =
899-
idxPublic >= 0 ? this.publicHost.substr(0, idxPublic) : this.publicHost;
860+
idxPublic >= 0
861+
? this.options.public.substr(0, idxPublic)
862+
: this.options.public;
900863

901864
if (hostname === publicHostname) {
902865
return true;
@@ -935,7 +898,7 @@ class Server {
935898
}
936899

937900
// send stats to a socket or multiple sockets
938-
_sendStats(sockets, stats, force) {
901+
sendStats(sockets, stats, force) {
939902
const shouldEmit =
940903
!force &&
941904
stats &&
@@ -962,10 +925,10 @@ class Server {
962925
// duplicate the same massaging of options that watchpack performs
963926
// https://github.com/webpack/watchpack/blob/master/lib/DirectoryWatcher.js#L49
964927
// this isn't an elegant solution, but we'll improve it in the future
965-
const usePolling = this.watchOptions.poll ? true : undefined;
928+
const usePolling = this.options.watchOptions.poll ? true : undefined;
966929
const interval =
967-
typeof this.watchOptions.poll === 'number'
968-
? this.watchOptions.poll
930+
typeof this.options.watchOptions.poll === 'number'
931+
? this.options.watchOptions.poll
969932
: undefined;
970933

971934
const watchOptions = {
@@ -975,7 +938,7 @@ class Server {
975938
atomic: false,
976939
alwaysStat: true,
977940
ignorePermissionErrors: true,
978-
ignored: this.watchOptions.ignored,
941+
ignored: this.options.watchOptions.ignored,
979942
usePolling,
980943
interval,
981944
};

lib/servers/SockJSServer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module.exports = class SockJSServer extends BaseServer {
4444
});
4545

4646
this.socket.installHandlers(this.server.listeningApp, {
47-
prefix: this.server.sockPath,
47+
prefix: this.server.options.sockPath,
4848
});
4949
}
5050

lib/servers/WebsocketServer.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = class WebsocketServer extends BaseServer {
1111
super(server);
1212
this.wsServer = new ws.Server({
1313
noServer: true,
14-
path: this.server.sockPath,
14+
path: this.server.options.sockPath,
1515
});
1616

1717
this.server.listeningApp.on('upgrade', (req, sock, head) => {
@@ -29,6 +29,7 @@ module.exports = class WebsocketServer extends BaseServer {
2929
});
3030

3131
const noop = () => {};
32+
const heartbeatInterval = 30000;
3233

3334
setInterval(() => {
3435
this.wsServer.clients.forEach((socket) => {
@@ -39,7 +40,7 @@ module.exports = class WebsocketServer extends BaseServer {
3940
socket.isAlive = false;
4041
socket.ping(noop);
4142
});
42-
}, this.server.heartbeatInterval);
43+
}, heartbeatInterval);
4344
}
4445

4546
send(connection, message) {

lib/utils/normalizeOptions.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,21 @@ 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+
: {};
23+
options.sockPath = `/${
24+
options.sockPath ? options.sockPath.replace(/^\/|\/$/g, '') : 'sockjs-node'
25+
}`;
1426

1527
// normalize transportMode option
1628
if (options.transportMode === undefined) {
@@ -32,10 +44,6 @@ function normalizeOptions(compiler, options) {
3244
options.transportMode.client = options.transportMode.client || 'sockjs';
3345
}
3446
}
35-
36-
if (!options.watchOptions) {
37-
options.watchOptions = {};
38-
}
3947
}
4048

4149
module.exports = normalizeOptions;

setupTest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
'use strict';
22

3-
jest.setTimeout(60000);
3+
jest.setTimeout(5000);

test/server/__snapshots__/Server.test.js.snap

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Array [
55
Array [
66
"client",
77
"index.js?http:",
8-
"localhost:8100",
8+
"localhost:8100&sockPath=",
9+
"sockjs-node",
910
],
1011
Array [
1112
"node_modules",
@@ -24,7 +25,8 @@ Array [
2425
Array [
2526
"client",
2627
"index.js?http:",
27-
"localhost:8100",
28+
"localhost:8100&sockPath=",
29+
"sockjs-node",
2830
],
2931
Array [
3032
"node_modules",

test/server/servers/SockJSServer.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ describe('SockJSServer', () => {
2121
error: () => {},
2222
debug: () => {},
2323
},
24-
sockPath: '/sockjs-node',
24+
options: {
25+
sockPath: '/sockjs-node',
26+
},
2527
listeningApp,
2628
};
2729

test/server/servers/WebsocketServer.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ describe('WebsocketServer', () => {
2222
error: () => {},
2323
debug: () => {},
2424
},
25-
sockPath: '/ws-server',
25+
options: {
26+
sockPath: '/ws-server',
27+
},
2628
listeningApp,
27-
heartbeatInterval: 800,
2829
};
2930
socketServer = new WebsocketServer(server);
3031
done();

0 commit comments

Comments
 (0)