Skip to content

Commit b8d5c1e

Browse files
fix: allow ca, key and cert will be string (regression) (#1676)
1 parent df113eb commit b8d5c1e

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

lib/Server.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,21 @@ class Server {
570570
const value = options.https[property];
571571
const isBuffer = value instanceof Buffer;
572572

573-
if (value && !isBuffer && fs.lstatSync(value).isFile()) {
574-
options.https[property] = fs.readFileSync(path.resolve(value));
573+
if (value && !isBuffer) {
574+
let stats = null;
575+
576+
try {
577+
stats = fs.lstatSync(value).isFile();
578+
} catch (error) {
579+
// ignore error
580+
}
581+
582+
if (stats) {
583+
// It is file
584+
options.https[property] = fs.readFileSync(path.resolve(value));
585+
} else {
586+
options.https[property] = value;
587+
}
575588
}
576589
}
577590

@@ -610,7 +623,7 @@ class Server {
610623
const pems = createCertificate(attrs);
611624

612625
fs.writeFileSync(certPath, pems.private + pems.cert, {
613-
encoding: 'utf-8',
626+
encoding: 'utf8',
614627
});
615628
}
616629

test/Https.test.js

+41-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ const contentBasePublic = path.join(
1818
describe('HTTPS', () => {
1919
let server;
2020
let req;
21-
afterEach(helper.close);
2221

23-
describe('to directory', () => {
22+
describe('is boolean', () => {
2423
beforeAll((done) => {
2524
server = helper.start(
2625
config,
@@ -36,6 +35,10 @@ describe('HTTPS', () => {
3635
it('Request to index', (done) => {
3736
req.get('/').expect(200, /Heyo/, done);
3837
});
38+
39+
afterAll(() => {
40+
helper.close();
41+
});
3942
});
4043

4144
describe('ca, pfx, key and cert are buffer', () => {
@@ -68,7 +71,7 @@ describe('HTTPS', () => {
6871
});
6972
});
7073

71-
describe('ca, pfx, key and cert are string', () => {
74+
describe('ca, pfx, key and cert are paths', () => {
7275
beforeAll((done) => {
7376
server = helper.start(
7477
config,
@@ -91,4 +94,39 @@ describe('HTTPS', () => {
9194
req.get('/').expect(200, /Heyo/, done);
9295
});
9396
});
97+
98+
describe('ca, pfx, key and cert are raw strings', () => {
99+
beforeAll((done) => {
100+
server = helper.start(
101+
config,
102+
{
103+
contentBase: contentBasePublic,
104+
https: {
105+
ca: fs
106+
.readFileSync(path.join(httpsCertificateDirectory, 'ca.pem'))
107+
.toString(),
108+
// pfx can't be string because it is binary format
109+
pfx: fs.readFileSync(
110+
path.join(httpsCertificateDirectory, 'server.pfx')
111+
),
112+
key: fs
113+
.readFileSync(path.join(httpsCertificateDirectory, 'server.key'))
114+
.toString(),
115+
cert: fs
116+
.readFileSync(path.join(httpsCertificateDirectory, 'server.crt'))
117+
.toString(),
118+
passphrase: 'webpack-dev-server',
119+
},
120+
},
121+
done
122+
);
123+
req = request(server.app);
124+
});
125+
126+
it('Request to index', (done) => {
127+
req.get('/').expect(200, /Heyo/, done);
128+
});
129+
});
130+
131+
afterEach(helper.close);
94132
});

0 commit comments

Comments
 (0)