diff --git a/lib/Server.js b/lib/Server.js index 11e5f67d3d..de8f299859 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -570,8 +570,21 @@ class Server { const value = options.https[property]; const isBuffer = value instanceof Buffer; - if (value && !isBuffer && fs.lstatSync(value).isFile()) { - options.https[property] = fs.readFileSync(path.resolve(value)); + if (value && !isBuffer) { + let stats = null; + + try { + stats = fs.lstatSync(value).isFile(); + } catch (error) { + // ignore error + } + + if (stats) { + // It is file + options.https[property] = fs.readFileSync(path.resolve(value)); + } else { + options.https[property] = value; + } } } @@ -610,7 +623,7 @@ class Server { const pems = createCertificate(attrs); fs.writeFileSync(certPath, pems.private + pems.cert, { - encoding: 'utf-8', + encoding: 'utf8', }); } diff --git a/test/Https.test.js b/test/Https.test.js index 676e0ad245..449a9f3a92 100644 --- a/test/Https.test.js +++ b/test/Https.test.js @@ -18,9 +18,8 @@ const contentBasePublic = path.join( describe('HTTPS', () => { let server; let req; - afterEach(helper.close); - describe('to directory', () => { + describe('is boolean', () => { beforeAll((done) => { server = helper.start( config, @@ -36,6 +35,10 @@ describe('HTTPS', () => { it('Request to index', (done) => { req.get('/').expect(200, /Heyo/, done); }); + + afterAll(() => { + helper.close(); + }); }); describe('ca, pfx, key and cert are buffer', () => { @@ -68,7 +71,7 @@ describe('HTTPS', () => { }); }); - describe('ca, pfx, key and cert are string', () => { + describe('ca, pfx, key and cert are paths', () => { beforeAll((done) => { server = helper.start( config, @@ -91,4 +94,39 @@ describe('HTTPS', () => { req.get('/').expect(200, /Heyo/, done); }); }); + + describe('ca, pfx, key and cert are raw strings', () => { + beforeAll((done) => { + server = helper.start( + config, + { + contentBase: contentBasePublic, + https: { + ca: fs + .readFileSync(path.join(httpsCertificateDirectory, 'ca.pem')) + .toString(), + // pfx can't be string because it is binary format + pfx: fs.readFileSync( + path.join(httpsCertificateDirectory, 'server.pfx') + ), + key: fs + .readFileSync(path.join(httpsCertificateDirectory, 'server.key')) + .toString(), + cert: fs + .readFileSync(path.join(httpsCertificateDirectory, 'server.crt')) + .toString(), + passphrase: 'webpack-dev-server', + }, + }, + done + ); + req = request(server.app); + }); + + it('Request to index', (done) => { + req.get('/').expect(200, /Heyo/, done); + }); + }); + + afterEach(helper.close); });