Skip to content

Commit b86fc8d

Browse files
refactor: code
1 parent 8c832a6 commit b86fc8d

File tree

4 files changed

+327
-33
lines changed

4 files changed

+327
-33
lines changed

lib/Server.js

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ class Server {
160160
options.allowedHosts = [options.allowedHosts];
161161
}
162162

163+
if (typeof options.bonjour === "undefined") {
164+
options.bonjour = false;
165+
} else if (typeof options.bonjour === "boolean") {
166+
options.bonjour = options.bonjour ? {} : false;
167+
}
168+
163169
if (
164170
typeof options.client === "undefined" ||
165171
(typeof options.client === "object" && options.client !== null)
@@ -203,7 +209,22 @@ class Server {
203209
options.compress = true;
204210
}
205211

206-
options.devMiddleware = options.devMiddleware || {};
212+
if (typeof options.devMiddleware === "undefined") {
213+
options.devMiddleware = {};
214+
}
215+
216+
// No need to normalize `headers`
217+
218+
if (typeof options.historyApiFallback === "undefined") {
219+
options.historyApiFallback = false;
220+
} else if (
221+
typeof options.historyApiFallback === "boolean" &&
222+
options.historyApiFallback
223+
) {
224+
options.historyApiFallback = {};
225+
}
226+
227+
// No need to normalize `host`
207228

208229
options.hot =
209230
typeof options.hot === "boolean" || options.hot === "only"
@@ -525,6 +546,31 @@ class Server {
525546
});
526547
}
527548

549+
if (typeof options.watchFiles === "string") {
550+
options.watchFiles = [{ paths: options.watchFiles, options: {} }];
551+
} else if (
552+
typeof options.watchFiles === "object" &&
553+
options.watchFiles !== null &&
554+
!Array.isArray(options.watchFiles)
555+
) {
556+
options.watchFiles = [
557+
{
558+
paths: options.watchFiles.paths,
559+
options: options.watchFiles.options || {},
560+
},
561+
];
562+
} else if (Array.isArray(options.watchFiles)) {
563+
options.watchFiles = options.watchFiles.map((item) => {
564+
if (typeof item === "string") {
565+
return { paths: item, options: {} };
566+
}
567+
568+
return { paths: item.paths, options: item.options || {} };
569+
});
570+
} else {
571+
options.watchFiles = [];
572+
}
573+
528574
const defaultWebSocketServerType = "ws";
529575
const defaultWebSocketServerOptions = { path: "/ws" };
530576

@@ -844,24 +890,20 @@ class Server {
844890
}
845891

846892
setupHistoryApiFallbackFeature() {
847-
const historyApiFallback = require("connect-history-api-fallback");
893+
const { historyApiFallback } = this.options;
848894

849-
const options =
850-
typeof this.options.historyApiFallback !== "boolean"
851-
? this.options.historyApiFallback
852-
: {};
853-
854-
let logger;
855-
856-
if (typeof options.verbose === "undefined") {
857-
logger = this.logger.log.bind(
895+
if (
896+
typeof historyApiFallback.logger === "undefined" &&
897+
!historyApiFallback.verbose
898+
) {
899+
historyApiFallback.logger = this.logger.log.bind(
858900
this.logger,
859901
"[connect-history-api-fallback]"
860902
);
861903
}
862904

863905
// Fall back to /index.html if nothing else matches.
864-
this.app.use(historyApiFallback({ logger, ...options }));
906+
this.app.use(require("connect-history-api-fallback")(historyApiFallback));
865907
}
866908

867909
setupStaticFeature() {
@@ -911,23 +953,12 @@ class Server {
911953
}
912954

913955
setupWatchFiles() {
914-
if (this.options.watchFiles) {
915-
const { watchFiles } = this.options;
916-
917-
if (typeof watchFiles === "string") {
918-
this.watchFiles(watchFiles, {});
919-
} else if (Array.isArray(watchFiles)) {
920-
watchFiles.forEach((file) => {
921-
if (typeof file === "string") {
922-
this.watchFiles(file, {});
923-
} else {
924-
this.watchFiles(file.paths, file.options || {});
925-
}
926-
});
927-
} else {
928-
// { paths: [...], options: {} }
929-
this.watchFiles(watchFiles.paths, watchFiles.options || {});
930-
}
956+
const { watchFiles } = this.options;
957+
958+
if (watchFiles.length > 0) {
959+
watchFiles.forEach((item) => {
960+
this.watchFiles(item.paths, item.options);
961+
});
931962
}
932963
}
933964

@@ -940,7 +971,7 @@ class Server {
940971
}
941972

942973
setupHeadersFeature() {
943-
this.app.all("*", this.setContentHeaders.bind(this));
974+
this.app.all("*", this.setHeaders.bind(this));
944975
}
945976

946977
setupMagicHtmlFeature() {
@@ -1555,12 +1586,14 @@ class Server {
15551586
return statsObj.toJson(stats);
15561587
}
15571588

1558-
setContentHeaders(req, res, next) {
1589+
setHeaders(req, res, next) {
15591590
let { headers } = this.options;
1591+
15601592
if (headers) {
15611593
if (typeof headers === "function") {
15621594
headers = headers(req, res, this.middleware.context);
15631595
}
1596+
15641597
// eslint-disable-next-line guard-for-in
15651598
for (const name in headers) {
15661599
res.setHeader(name, headers[name]);
@@ -1623,11 +1656,11 @@ class Server {
16231656
return true;
16241657
}
16251658

1626-
const allowedHosts = this.options.allowedHosts;
1659+
const { allowedHosts } = this.options;
16271660

16281661
// always allow localhost host, for convenience
16291662
// allow if hostname is in allowedHosts
1630-
if (Array.isArray(allowedHosts) && allowedHosts.length) {
1663+
if (Array.isArray(allowedHosts) && allowedHosts.length > 0) {
16311664
for (let hostIdx = 0; hostIdx < allowedHosts.length; hostIdx++) {
16321665
const allowedHost = allowedHosts[hostIdx];
16331666

test/e2e/web-socket-communication.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const config = require("../fixtures/client-config/webpack.config");
88
const runBrowser = require("../helpers/run-browser");
99
const port = require("../ports-map")["web-socket-communication"];
1010

11+
jest.setTimeout(60000);
12+
1113
describe("web socket communication", () => {
1214
const webSocketServers = ["ws", "sockjs"];
1315

@@ -224,6 +226,7 @@ describe("web socket communication", () => {
224226
});
225227

226228
await page.waitForNavigation({
229+
timeout: 60000,
227230
waitUntil: "networkidle0",
228231
});
229232

0 commit comments

Comments
 (0)