Skip to content

Commit

Permalink
Clean up and add tests for HTTPS support
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Dec 24, 2017
1 parent 90b9684 commit ed11a2e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/Bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ class Bundler extends EventEmitter {
return Server.middleware(this);
}

async serve(port = 1234, https) {
async serve(port = 1234, https = false) {
let server = await Server.serve(this, port, https);
this.bundle();
return server;
Expand Down
18 changes: 8 additions & 10 deletions src/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const https = require('https');
const serveStatic = require('serve-static');
const getPort = require('get-port');
const serverErrors = require('./utils/customErrors').serverErrors;

const generateCertificate = require('./utils/generateCertificate');

function middleware(bundler) {
Expand Down Expand Up @@ -57,11 +56,14 @@ function middleware(bundler) {
};
}

async function serve(bundler, port, https) {
async function serve(bundler, port, useHTTPS = false) {
let handler = middleware(bundler);
let server = useHTTPS
? https.createServer(generateCertificate(), handler)
: http.createServer(handler);

let freePort = await getPort({port});
let server = https
? serveHttps(bundler, freePort)
: http.createServer(middleware(bundler)).listen(freePort);
server.listen(freePort);

return new Promise((resolve, reject) => {
server.on('error', err => {
Expand All @@ -76,6 +78,7 @@ async function serve(bundler, port, https) {
`configured port ${port} could not be used.`
)}`
: '';

bundler.logger.persistent(
`Server running at ${bundler.logger.chalk.cyan(
`${https ? 'https' : 'http'}://localhost:${server.address().port}`
Expand All @@ -87,10 +90,5 @@ async function serve(bundler, port, https) {
});
}

function serveHttps(bundler, freePort) {
const {key, cert} = generateCertificate();
return https.createServer({key, cert}, middleware(bundler)).listen(freePort);
}

exports.middleware = middleware;
exports.serve = serve;
41 changes: 29 additions & 12 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const assert = require('assert');
const fs = require('fs');
const {bundler} = require('./utils');
const http = require('http');
const https = require('https');

describe('server', function() {
let server;
Expand All @@ -12,20 +13,28 @@ describe('server', function() {
}
});

function get(file) {
function get(file, client = http) {
return new Promise((resolve, reject) => {
http.get('http://localhost:' + server.address().port + file, res => {
if (res.statusCode !== 200) {
return reject(new Error('Request failed: ' + res.statusCode));
}
client.get(
{
hostname: 'localhost',
port: server.address().port,
path: file,
rejectUnauthorized: false
},
res => {
if (res.statusCode !== 200) {
return reject(new Error('Request failed: ' + res.statusCode));
}

res.setEncoding('utf8');
let data = '';
res.on('data', c => (data += c));
res.on('end', () => {
resolve(data);
});
});
res.setEncoding('utf8');
let data = '';
res.on('data', c => (data += c));
res.on('end', () => {
resolve(data);
});
}
);
});
}

Expand Down Expand Up @@ -78,4 +87,12 @@ describe('server', function() {
b.errored = false;
await get('/');
});

it('should support HTTPS', async function() {
let b = bundler(__dirname + '/integration/commonjs/index.js');
server = await b.serve(0, true);

let data = await get('/dist/index.js', https);
assert.equal(data, fs.readFileSync(__dirname + '/dist/index.js', 'utf8'));
});
});

0 comments on commit ed11a2e

Please sign in to comment.