Skip to content

Commit ab48526

Browse files
hiroppyevilebottnawi
authored andcommitted
refactor(server): clean up lib/server.js (#1873)
1 parent c2360e4 commit ab48526

File tree

1 file changed

+119
-114
lines changed

1 file changed

+119
-114
lines changed

lib/Server.js

Lines changed: 119 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,6 @@ if (semver.satisfies(process.version, '8.6.0 - 9')) {
6363
}
6464

6565
class Server {
66-
static get DEFAULT_STATS() {
67-
return {
68-
all: false,
69-
hash: true,
70-
assets: true,
71-
warnings: true,
72-
errors: true,
73-
errorDetails: false,
74-
};
75-
}
76-
7766
constructor(compiler, options = {}, _log) {
7867
if (options.lazy && !options.filename) {
7968
throw new Error("'filename' option must be set in lazy mode.");
@@ -142,108 +131,8 @@ class Server {
142131
this.websocketProxies = [];
143132

144133
this.setupFeatures();
145-
146-
// if the user enables http2, we can safely enable https
147-
if (this.options.http2 && !this.options.https) {
148-
this.options.https = true;
149-
}
150-
151-
if (this.options.https) {
152-
// for keep supporting CLI parameters
153-
if (typeof this.options.https === 'boolean') {
154-
this.options.https = {
155-
ca: this.options.ca,
156-
pfx: this.options.pfx,
157-
key: this.options.key,
158-
cert: this.options.cert,
159-
passphrase: this.options.pfxPassphrase,
160-
requestCert: this.options.requestCert || false,
161-
};
162-
}
163-
164-
for (const property of ['ca', 'pfx', 'key', 'cert']) {
165-
const value = this.options.https[property];
166-
const isBuffer = value instanceof Buffer;
167-
168-
if (value && !isBuffer) {
169-
let stats = null;
170-
171-
try {
172-
stats = fs.lstatSync(fs.realpathSync(value)).isFile();
173-
} catch (error) {
174-
// ignore error
175-
}
176-
177-
if (stats) {
178-
// It is file
179-
this.options.https[property] = fs.readFileSync(path.resolve(value));
180-
} else {
181-
this.options.https[property] = value;
182-
}
183-
}
184-
}
185-
186-
let fakeCert;
187-
188-
if (!this.options.https.key || !this.options.https.cert) {
189-
fakeCert = getCertificate(this.log);
190-
}
191-
192-
this.options.https.key = this.options.https.key || fakeCert;
193-
this.options.https.cert = this.options.https.cert || fakeCert;
194-
195-
// Only prevent HTTP/2 if http2 is explicitly set to false
196-
const isHttp2 = this.options.http2 !== false;
197-
198-
// note that options.spdy never existed. The user was able
199-
// to set options.https.spdy before, though it was not in the
200-
// docs. Keep options.https.spdy if the user sets it for
201-
// backwards compatability, but log a deprecation warning.
202-
if (this.options.https.spdy) {
203-
// for backwards compatability: if options.https.spdy was passed in before,
204-
// it was not altered in any way
205-
this.log.warn(
206-
'Providing custom spdy server options is deprecated and will be removed in the next major version.'
207-
);
208-
} else {
209-
// if the normal https server gets this option, it will not affect it.
210-
this.options.https.spdy = {
211-
protocols: ['h2', 'http/1.1'],
212-
};
213-
}
214-
215-
// `spdy` is effectively unmaintained, and as a consequence of an
216-
// implementation that extensively relies on Node’s non-public APIs, broken
217-
// on Node 10 and above. In those cases, only https will be used for now.
218-
// Once express supports Node's built-in HTTP/2 support, migrating over to
219-
// that should be the best way to go.
220-
// The relevant issues are:
221-
// - https://github.com/nodejs/node/issues/21665
222-
// - https://github.com/webpack/webpack-dev-server/issues/1449
223-
// - https://github.com/expressjs/express/issues/3388
224-
if (semver.gte(process.version, '10.0.0') || !isHttp2) {
225-
if (this.options.http2) {
226-
// the user explicitly requested http2 but is not getting it because
227-
// of the node version.
228-
this.log.warn(
229-
'HTTP/2 is currently unsupported for Node 10.0.0 and above, but will be supported once Express supports it'
230-
);
231-
}
232-
this.listeningApp = https.createServer(this.options.https, this.app);
233-
} else {
234-
/* eslint-disable global-require */
235-
// The relevant issues are:
236-
// https://github.com/spdy-http2/node-spdy/issues/350
237-
// https://github.com/webpack/webpack-dev-server/issues/1592
238-
this.listeningApp = require('spdy').createServer(
239-
this.options.https,
240-
this.app
241-
);
242-
/* eslint-enable global-require */
243-
}
244-
} else {
245-
this.listeningApp = http.createServer(this.app);
246-
}
134+
this.setupHttps();
135+
this.createServer();
247136

248137
killable(this.listeningApp);
249138

@@ -625,6 +514,122 @@ class Server {
625514
});
626515
}
627516

517+
setupHttps() {
518+
// if the user enables http2, we can safely enable https
519+
if (this.options.http2 && !this.options.https) {
520+
this.options.https = true;
521+
}
522+
523+
if (this.options.https) {
524+
// for keep supporting CLI parameters
525+
if (typeof this.options.https === 'boolean') {
526+
this.options.https = {
527+
ca: this.options.ca,
528+
pfx: this.options.pfx,
529+
key: this.options.key,
530+
cert: this.options.cert,
531+
passphrase: this.options.pfxPassphrase,
532+
requestCert: this.options.requestCert || false,
533+
};
534+
}
535+
536+
for (const property of ['ca', 'pfx', 'key', 'cert']) {
537+
const value = this.options.https[property];
538+
const isBuffer = value instanceof Buffer;
539+
540+
if (value && !isBuffer) {
541+
let stats = null;
542+
543+
try {
544+
stats = fs.lstatSync(fs.realpathSync(value)).isFile();
545+
} catch (error) {
546+
// ignore error
547+
}
548+
549+
// It is file
550+
this.options.https[property] = stats
551+
? fs.readFileSync(path.resolve(value))
552+
: value;
553+
}
554+
}
555+
556+
let fakeCert;
557+
558+
if (!this.options.https.key || !this.options.https.cert) {
559+
fakeCert = getCertificate(this.log);
560+
}
561+
562+
this.options.https.key = this.options.https.key || fakeCert;
563+
this.options.https.cert = this.options.https.cert || fakeCert;
564+
565+
// note that options.spdy never existed. The user was able
566+
// to set options.https.spdy before, though it was not in the
567+
// docs. Keep options.https.spdy if the user sets it for
568+
// backwards compatibility, but log a deprecation warning.
569+
if (this.options.https.spdy) {
570+
// for backwards compatibility: if options.https.spdy was passed in before,
571+
// it was not altered in any way
572+
this.log.warn(
573+
'Providing custom spdy server options is deprecated and will be removed in the next major version.'
574+
);
575+
} else {
576+
// if the normal https server gets this option, it will not affect it.
577+
this.options.https.spdy = {
578+
protocols: ['h2', 'http/1.1'],
579+
};
580+
}
581+
}
582+
}
583+
584+
createServer() {
585+
if (this.options.https) {
586+
// Only prevent HTTP/2 if http2 is explicitly set to false
587+
const isHttp2 = this.options.http2 !== false;
588+
589+
// `spdy` is effectively unmaintained, and as a consequence of an
590+
// implementation that extensively relies on Node’s non-public APIs, broken
591+
// on Node 10 and above. In those cases, only https will be used for now.
592+
// Once express supports Node's built-in HTTP/2 support, migrating over to
593+
// that should be the best way to go.
594+
// The relevant issues are:
595+
// - https://github.com/nodejs/node/issues/21665
596+
// - https://github.com/webpack/webpack-dev-server/issues/1449
597+
// - https://github.com/expressjs/express/issues/3388
598+
if (semver.gte(process.version, '10.0.0') || !isHttp2) {
599+
if (this.options.http2) {
600+
// the user explicitly requested http2 but is not getting it because
601+
// of the node version.
602+
this.log.warn(
603+
'HTTP/2 is currently unsupported for Node 10.0.0 and above, but will be supported once Express supports it'
604+
);
605+
}
606+
this.listeningApp = https.createServer(this.options.https, this.app);
607+
} else {
608+
// The relevant issues are:
609+
// https://github.com/spdy-http2/node-spdy/issues/350
610+
// https://github.com/webpack/webpack-dev-server/issues/1592
611+
// eslint-disable-next-line global-require
612+
this.listeningApp = require('spdy').createServer(
613+
this.options.https,
614+
this.app
615+
);
616+
}
617+
} else {
618+
this.listeningApp = http.createServer(this.app);
619+
}
620+
}
621+
622+
static get DEFAULT_STATS() {
623+
return {
624+
all: false,
625+
hash: true,
626+
assets: true,
627+
warnings: true,
628+
errors: true,
629+
errorDetails: false,
630+
};
631+
}
632+
628633
getStats(statsObj) {
629634
const stats = Server.DEFAULT_STATS;
630635

@@ -693,7 +698,7 @@ class Server {
693698
if (ip.isV4Format(hostname) || ip.isV6Format(hostname)) {
694699
return true;
695700
}
696-
// always allow localhost host, for convience
701+
// always allow localhost host, for convenience
697702
if (hostname === 'localhost') {
698703
return true;
699704
}

0 commit comments

Comments
 (0)