Skip to content

Commit e57c349

Browse files
refactor: code
1 parent de0af2a commit e57c349

File tree

8 files changed

+1461
-1382
lines changed

8 files changed

+1461
-1382
lines changed

lib/Server.js

Lines changed: 49 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -520,70 +520,51 @@ class Server {
520520
? options.hot
521521
: true;
522522

523-
// normalize the `server` option
524-
const defaultServerType = "http";
525-
const defaultServerOptions = { requestCert: false };
526-
527-
switch (typeof options.server) {
528-
case "undefined":
529-
options.server = {};
530-
if (options.http2) {
531-
options.server = {
532-
type: "spdy",
533-
options: defaultServerOptions,
534-
};
535-
} else if (options.https) {
536-
options.server = {
537-
type: "https",
538-
options:
539-
typeof options.https === "object"
540-
? options.https
541-
: defaultServerOptions,
542-
};
543-
} else {
544-
options.server.type = defaultServerType;
545-
}
546-
break;
547-
case "string":
548-
options.server = {
549-
type: options.server,
550-
};
551-
if (options.server.type !== "http") {
552-
options.server.options = defaultServerOptions;
553-
}
554-
break;
555-
case "object":
556-
if (typeof options.https === "object") {
557-
options.server.options = {
558-
...defaultServerOptions,
559-
...options.https,
560-
...options.server.options,
561-
};
562-
} else {
563-
options.server.options = {
564-
...defaultServerOptions,
565-
...options.server.options,
566-
};
567-
}
568-
break;
569-
default:
570-
options.server = {
571-
type: defaultServerType,
572-
options: defaultServerOptions,
573-
};
523+
const isHTTPs = Boolean(options.https);
524+
const isSPDY = Boolean(options.http2);
525+
526+
options.server = {
527+
type:
528+
// eslint-disable-next-line no-nested-ternary
529+
typeof options.server === "string"
530+
? options.server
531+
: // eslint-disable-next-line no-nested-ternary
532+
typeof (options.server || {}).type === "string"
533+
? options.server.type
534+
: // eslint-disable-next-line no-nested-ternary
535+
isSPDY
536+
? "spdy"
537+
: isHTTPs
538+
? "https"
539+
: "http",
540+
options: {
541+
...options.https,
542+
...(options.server || {}).options,
543+
},
544+
};
545+
546+
if (
547+
options.server.type === "spdy" &&
548+
typeof options.server.options.spdy === "undefined"
549+
) {
550+
options.server.options.spdy = {
551+
protocols: ["h2", "http/1.1"],
552+
};
574553
}
575554

576-
// server options
577-
if (options.server.options) {
578-
const { server } = options;
555+
if (options.server.type === "https" || options.server.type === "spdy") {
556+
if (typeof options.server.options.requestCert === "undefined") {
557+
options.server.options.requestCert = false;
558+
}
559+
579560
// TODO remove the `cacert` option in favor `ca` in the next major release
580561
for (const property of ["cacert", "ca", "cert", "crl", "key", "pfx"]) {
581-
if (typeof server.options[property] === "undefined") {
562+
if (typeof options.server.options[property] === "undefined") {
582563
// eslint-disable-next-line no-continue
583564
continue;
584565
}
585566

586-
const value = server.options[property];
567+
const value = options.server.options[property];
587568
const readFile = (item) => {
588569
if (
589570
Buffer.isBuffer(item) ||
@@ -606,14 +587,14 @@ class Server {
606587
}
607588
};
608589

609-
server.options[property] = Array.isArray(value)
590+
options.server.options[property] = Array.isArray(value)
610591
? value.map((item) => readFile(item))
611592
: readFile(value);
612593
}
613594

614595
let fakeCert;
615596

616-
if (!server.options.key || !server.options.cert) {
597+
if (!options.server.options.key || !options.server.options.cert) {
617598
const certificateDir = Server.findCacheDir();
618599
const certificatePath = path.join(certificateDir, "server.pem");
619600
let certificateExists;
@@ -728,20 +709,20 @@ class Server {
728709
this.logger.info(`SSL certificate: ${certificatePath}`);
729710
}
730711

731-
if (server.options.cacert) {
712+
if (options.server.options.cacert) {
732713
if (options.server.options.ca) {
733714
this.logger.warn(
734715
"Do not specify 'ca' and 'cacert' options together, the 'ca' option will be used."
735716
);
736717
} else {
737-
server.options.ca = options.server.options.cacert;
718+
options.server.options.ca = options.server.options.cacert;
738719
}
739720

740-
delete server.options.cacert;
721+
delete options.server.options.cacert;
741722
}
742723

743-
server.options.key = server.options.key || fakeCert;
744-
server.options.cert = server.options.cert || fakeCert;
724+
options.server.options.key = options.server.options.key || fakeCert;
725+
options.server.options.cert = options.server.options.cert || fakeCert;
745726
}
746727

747728
if (typeof options.ipc === "boolean") {
@@ -1606,27 +1587,11 @@ class Server {
16061587
}
16071588

16081589
createServer() {
1609-
switch (this.options.server.type) {
1610-
case "https":
1611-
this.server = require("https").createServer(
1612-
this.options.server.options,
1613-
this.app
1614-
);
1615-
break;
1616-
case "spdy":
1617-
this.server = require("spdy").createServer(
1618-
{
1619-
...this.options.server.options,
1620-
spdy: {
1621-
protocols: ["h2", "http/1.1"],
1622-
},
1623-
},
1624-
this.app
1625-
);
1626-
break;
1627-
default:
1628-
this.server = require("http").createServer(this.app);
1629-
}
1590+
// eslint-disable-next-line import/no-dynamic-require
1591+
this.server = require(this.options.server.type).createServer(
1592+
this.options.server.options,
1593+
this.app
1594+
);
16301595

16311596
this.server.on("connection", (socket) => {
16321597
// Add socket to list

lib/options.json

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,10 +634,10 @@
634634
"Server": {
635635
"anyOf": [
636636
{
637-
"enum": ["http", "https", "spdy"],
638-
"cli": {
639-
"exclude": true
640-
}
637+
"$ref": "#/definitions/ServerEnum"
638+
},
639+
{
640+
"$ref": "#/definitions/ServerString"
641641
},
642642
{
643643
"$ref": "#/definitions/ServerObject"
@@ -649,6 +649,19 @@
649649
"ServerType": {
650650
"enum": ["http", "https", "spdy"]
651651
},
652+
"ServerEnum": {
653+
"enum": ["http", "https", "spdy"],
654+
"cli": {
655+
"exclude": true
656+
}
657+
},
658+
"ServerString": {
659+
"type": "string",
660+
"minLength": 1,
661+
"cli": {
662+
"exclude": true
663+
}
664+
},
652665
"ServerObject": {
653666
"type": "object",
654667
"properties": {

test/__snapshots__/validate-options.test.js.snap.webpack4

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ exports[`options validate should throw an error on the "server" option with '{"t
617617
exports[`options validate should throw an error on the "server" option with '{"type":"https","options":{"ca":true}}' value 1`] = `
618618
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
619619
- options.server should be one of these:
620-
\\"http\\" | \\"https\\" | \\"spdy\\" | object { type?, options? }
620+
\\"http\\" | \\"https\\" | \\"spdy\\" | non-empty string | object { type?, options? }
621621
-> Allows to set server and options (by default 'http').
622622
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverserver
623623
Details:
@@ -634,7 +634,7 @@ exports[`options validate should throw an error on the "server" option with '{"t
634634
exports[`options validate should throw an error on the "server" option with '{"type":"https","options":{"cert":true}}' value 1`] = `
635635
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
636636
- options.server should be one of these:
637-
\\"http\\" | \\"https\\" | \\"spdy\\" | object { type?, options? }
637+
\\"http\\" | \\"https\\" | \\"spdy\\" | non-empty string | object { type?, options? }
638638
-> Allows to set server and options (by default 'http').
639639
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverserver
640640
Details:
@@ -651,7 +651,7 @@ exports[`options validate should throw an error on the "server" option with '{"t
651651
exports[`options validate should throw an error on the "server" option with '{"type":"https","options":{"key":10}}' value 1`] = `
652652
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
653653
- options.server should be one of these:
654-
\\"http\\" | \\"https\\" | \\"spdy\\" | object { type?, options? }
654+
\\"http\\" | \\"https\\" | \\"spdy\\" | non-empty string | object { type?, options? }
655655
-> Allows to set server and options (by default 'http').
656656
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverserver
657657
Details:
@@ -674,7 +674,7 @@ exports[`options validate should throw an error on the "server" option with '{"t
674674
exports[`options validate should throw an error on the "server" option with '{"type":"https","options":{"pfx":10}}' value 1`] = `
675675
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
676676
- options.server should be one of these:
677-
\\"http\\" | \\"https\\" | \\"spdy\\" | object { type?, options? }
677+
\\"http\\" | \\"https\\" | \\"spdy\\" | non-empty string | object { type?, options? }
678678
-> Allows to set server and options (by default 'http').
679679
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverserver
680680
Details:
@@ -694,38 +694,6 @@ exports[`options validate should throw an error on the "server" option with '{"t
694694
-> Request for an SSL certificate."
695695
`;
696696

697-
exports[`options validate should throw an error on the "server" option with '{"type":"invalid"}' value 1`] = `
698-
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
699-
- options.server.type should be one of these:
700-
\\"http\\" | \\"https\\" | \\"spdy\\""
701-
`;
702-
703-
exports[`options validate should throw an error on the "server" option with 'http2' value 1`] = `
704-
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
705-
- options.server should be one of these:
706-
\\"http\\" | \\"https\\" | \\"spdy\\" | object { type?, options? }
707-
-> Allows to set server and options (by default 'http').
708-
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverserver
709-
Details:
710-
* options.server should be one of these:
711-
\\"http\\" | \\"https\\" | \\"spdy\\"
712-
* options.server should be an object:
713-
object { type?, options? }"
714-
`;
715-
716-
exports[`options validate should throw an error on the "server" option with 'invalid' value 1`] = `
717-
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
718-
- options.server should be one of these:
719-
\\"http\\" | \\"https\\" | \\"spdy\\" | object { type?, options? }
720-
-> Allows to set server and options (by default 'http').
721-
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverserver
722-
Details:
723-
* options.server should be one of these:
724-
\\"http\\" | \\"https\\" | \\"spdy\\"
725-
* options.server should be an object:
726-
object { type?, options? }"
727-
`;
728-
729697
exports[`options validate should throw an error on the "static" option with '' value 1`] = `
730698
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
731699
- options.static should be a non-empty string."

test/__snapshots__/validate-options.test.js.snap.webpack5

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ exports[`options validate should throw an error on the "server" option with '{"t
617617
exports[`options validate should throw an error on the "server" option with '{"type":"https","options":{"ca":true}}' value 1`] = `
618618
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
619619
- options.server should be one of these:
620-
\\"http\\" | \\"https\\" | \\"spdy\\" | object { type?, options? }
620+
\\"http\\" | \\"https\\" | \\"spdy\\" | non-empty string | object { type?, options? }
621621
-> Allows to set server and options (by default 'http').
622622
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverserver
623623
Details:
@@ -634,7 +634,7 @@ exports[`options validate should throw an error on the "server" option with '{"t
634634
exports[`options validate should throw an error on the "server" option with '{"type":"https","options":{"cert":true}}' value 1`] = `
635635
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
636636
- options.server should be one of these:
637-
\\"http\\" | \\"https\\" | \\"spdy\\" | object { type?, options? }
637+
\\"http\\" | \\"https\\" | \\"spdy\\" | non-empty string | object { type?, options? }
638638
-> Allows to set server and options (by default 'http').
639639
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverserver
640640
Details:
@@ -651,7 +651,7 @@ exports[`options validate should throw an error on the "server" option with '{"t
651651
exports[`options validate should throw an error on the "server" option with '{"type":"https","options":{"key":10}}' value 1`] = `
652652
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
653653
- options.server should be one of these:
654-
\\"http\\" | \\"https\\" | \\"spdy\\" | object { type?, options? }
654+
\\"http\\" | \\"https\\" | \\"spdy\\" | non-empty string | object { type?, options? }
655655
-> Allows to set server and options (by default 'http').
656656
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverserver
657657
Details:
@@ -674,7 +674,7 @@ exports[`options validate should throw an error on the "server" option with '{"t
674674
exports[`options validate should throw an error on the "server" option with '{"type":"https","options":{"pfx":10}}' value 1`] = `
675675
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
676676
- options.server should be one of these:
677-
\\"http\\" | \\"https\\" | \\"spdy\\" | object { type?, options? }
677+
\\"http\\" | \\"https\\" | \\"spdy\\" | non-empty string | object { type?, options? }
678678
-> Allows to set server and options (by default 'http').
679679
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverserver
680680
Details:
@@ -694,38 +694,6 @@ exports[`options validate should throw an error on the "server" option with '{"t
694694
-> Request for an SSL certificate."
695695
`;
696696

697-
exports[`options validate should throw an error on the "server" option with '{"type":"invalid"}' value 1`] = `
698-
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
699-
- options.server.type should be one of these:
700-
\\"http\\" | \\"https\\" | \\"spdy\\""
701-
`;
702-
703-
exports[`options validate should throw an error on the "server" option with 'http2' value 1`] = `
704-
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
705-
- options.server should be one of these:
706-
\\"http\\" | \\"https\\" | \\"spdy\\" | object { type?, options? }
707-
-> Allows to set server and options (by default 'http').
708-
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverserver
709-
Details:
710-
* options.server should be one of these:
711-
\\"http\\" | \\"https\\" | \\"spdy\\"
712-
* options.server should be an object:
713-
object { type?, options? }"
714-
`;
715-
716-
exports[`options validate should throw an error on the "server" option with 'invalid' value 1`] = `
717-
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
718-
- options.server should be one of these:
719-
\\"http\\" | \\"https\\" | \\"spdy\\" | object { type?, options? }
720-
-> Allows to set server and options (by default 'http').
721-
-> Read more at https://webpack.js.org/configuration/dev-server/#devserverserver
722-
Details:
723-
* options.server should be one of these:
724-
\\"http\\" | \\"https\\" | \\"spdy\\"
725-
* options.server should be an object:
726-
object { type?, options? }"
727-
`;
728-
729697
exports[`options validate should throw an error on the "static" option with '' value 1`] = `
730698
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
731699
- options.static should be a non-empty string."

0 commit comments

Comments
 (0)