Skip to content

Commit

Permalink
fix(dev-server): ssl support (#1653)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhaenisch authored and adamdbradley committed Jul 26, 2019
1 parent adca6d0 commit e6cc6da
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 50 deletions.
10 changes: 2 additions & 8 deletions src/compiler/config/test/validate-dev-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,9 @@ describe('validateDevServer', () => {
expect(config.devServer.protocol).toBe('http');
});

it('should set https protocol', () => {
config.devServer.protocol = 'HTTPS' as any;
it('should set https protocol if credentials are set', () => {
config.devServer.https = { key: 'fake-key', cert: 'fake-cert' };
validateConfig(config, [], false);
expect(config.devServer.protocol).toBe('https');
});

it('should default protocol http', () => {
validateConfig(config, [], false);
expect(config.devServer.protocol).toBe('http');
});

});
9 changes: 1 addition & 8 deletions src/compiler/config/validate-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,5 @@ export function validateDevServer(config: d.Config, diagnostics: d.Diagnostic[])
}

function validateProtocol(devServer: d.DevServerConfig) {
if (typeof devServer.protocol === 'string') {
let protocol: string = devServer.protocol.trim().toLowerCase() as any;
protocol = protocol.replace(':', '').replace('/', '');
devServer.protocol = protocol as any;
}
if (devServer.protocol !== 'http' && devServer.protocol !== 'https') {
devServer.protocol = 'http';
}
devServer.protocol = devServer.https ? 'https' : 'http';
}
8 changes: 8 additions & 0 deletions src/declarations/dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export interface StencilDevServerConfig {
* Base path to be used by the server. Defaults to the root pathname.
*/
basePath?: string;
/**
* When set, the dev server will run via https using the SSL certificate and key you provide (use `fs` if you want to read them from files).
*/
https?: Credentials;
/**
* The URL the dev server should first open to. Defaults to `/`.
*/
Expand Down Expand Up @@ -52,6 +56,10 @@ export interface DevServerConfig extends StencilDevServerConfig {
protocol?: 'http' | 'https';
}

export interface Credentials {
key: string;
cert: string;
}

export type PageReloadStrategy = 'hmr' | 'pageReload' | null;

Expand Down
13 changes: 2 additions & 11 deletions src/dev-server/server-http.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import * as d from '../declarations';
import { createRequestHandler } from './request-handler';
import { findClosestOpenPort } from './find-closest-port';
import { getSSL } from './ssl';
import * as http from 'http';
import * as https from 'https';


export async function createHttpServer(devServerConfig: d.DevServerConfig, fs: d.FileSystem, destroys: d.DevServerDestroy[]) {
// figure out the port to be listening on
// by figuring out the first one available
Expand All @@ -14,16 +12,9 @@ export async function createHttpServer(devServerConfig: d.DevServerConfig, fs: d
// create our request handler
const reqHandler = createRequestHandler(devServerConfig, fs);

let server: http.Server;

if (devServerConfig.protocol === 'https') {
// https server
server = https.createServer(await getSSL(), reqHandler) as any;
const credentials = devServerConfig.https;

} else {
// http server
server = http.createServer(reqHandler);
}
let server = credentials ? https.createServer(credentials, reqHandler) : http.createServer(reqHandler);

destroys.push(() => {
// close down the serve on destroy
Expand Down
23 changes: 0 additions & 23 deletions src/dev-server/ssl.ts

This file was deleted.

0 comments on commit e6cc6da

Please sign in to comment.