Skip to content

Commit 8468f93

Browse files
authored
refactor: move https normalization (#3331)
1 parent c43f266 commit 8468f93

File tree

2 files changed

+43
-50
lines changed

2 files changed

+43
-50
lines changed

lib/Server.js

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict';
22

3-
const path = require('path');
43
const url = require('url');
5-
const fs = require('graceful-fs');
64
const ipaddr = require('ipaddr.js');
75
const internalIp = require('internal-ip');
86
const killable = require('killable');
@@ -41,7 +39,7 @@ class Server {
4139
// this value of ws can be overwritten for tests
4240
this.wsHeartbeatInterval = 30000;
4341

44-
normalizeOptions(this.compiler, this.options);
42+
normalizeOptions(this.compiler, this.options, this.logger);
4543

4644
this.applyDevServerPlugin();
4745

@@ -63,7 +61,6 @@ class Server {
6361

6462
this.setupWatchFiles();
6563
this.setupFeatures();
66-
this.setupHttps();
6764
this.createServer();
6865

6966
killable(this.server);
@@ -505,51 +502,6 @@ class Server {
505502
});
506503
}
507504

508-
setupHttps() {
509-
// if the user enables http2, we can safely enable https
510-
if (
511-
(this.options.http2 && !this.options.https) ||
512-
this.options.https === true
513-
) {
514-
this.options.https = {
515-
requestCert: false,
516-
};
517-
}
518-
519-
if (this.options.https) {
520-
const getCertificate = require('./utils/getCertificate');
521-
522-
for (const property of ['cacert', 'pfx', 'key', 'cert']) {
523-
const value = this.options.https[property];
524-
const isBuffer = value instanceof Buffer;
525-
526-
if (value && !isBuffer) {
527-
let stats = null;
528-
529-
try {
530-
stats = fs.lstatSync(fs.realpathSync(value)).isFile();
531-
} catch (error) {
532-
// ignore error
533-
}
534-
535-
// It is file
536-
this.options.https[property] = stats
537-
? fs.readFileSync(path.resolve(value))
538-
: value;
539-
}
540-
}
541-
542-
let fakeCert;
543-
544-
if (!this.options.https.key || !this.options.https.cert) {
545-
fakeCert = getCertificate(this.logger);
546-
}
547-
548-
this.options.https.key = this.options.https.key || fakeCert;
549-
this.options.https.cert = this.options.https.cert || fakeCert;
550-
}
551-
}
552-
553505
createServer() {
554506
const https = require('https');
555507
const http = require('http');

lib/utils/normalizeOptions.js

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

33
const path = require('path');
4+
const fs = require('graceful-fs');
45
const isAbsoluteUrl = require('is-absolute-url');
56
const getCompilerConfigArray = require('./getCompilerConfigArray');
67

7-
function normalizeOptions(compiler, options) {
8+
function normalizeOptions(compiler, options, logger) {
89
// TODO: improve this to not use .find for compiler watchOptions
910
const configArr = getCompilerConfigArray(compiler);
1011
const watchOptionsConfig = configArr.find(
@@ -127,6 +128,46 @@ function normalizeOptions(compiler, options) {
127128
if (typeof options.compress === 'undefined') {
128129
options.compress = true;
129130
}
131+
132+
// if the user enables http2, we can safely enable https
133+
if ((options.http2 && !options.https) || options.https === true) {
134+
options.https = {
135+
requestCert: false,
136+
};
137+
}
138+
139+
if (options.https) {
140+
const getCertificate = require('./getCertificate');
141+
142+
for (const property of ['cacert', 'pfx', 'key', 'cert']) {
143+
const value = options.https[property];
144+
const isBuffer = value instanceof Buffer;
145+
146+
if (value && !isBuffer) {
147+
let stats = null;
148+
149+
try {
150+
stats = fs.lstatSync(fs.realpathSync(value)).isFile();
151+
} catch (error) {
152+
// ignore error
153+
}
154+
155+
// It is file
156+
options.https[property] = stats
157+
? fs.readFileSync(path.resolve(value))
158+
: value;
159+
}
160+
}
161+
162+
let fakeCert;
163+
164+
if (!options.https.key || !options.https.cert) {
165+
fakeCert = getCertificate(logger);
166+
}
167+
168+
options.https.key = options.https.key || fakeCert;
169+
options.https.cert = options.https.cert || fakeCert;
170+
}
130171
}
131172

132173
module.exports = normalizeOptions;

0 commit comments

Comments
 (0)