Skip to content

Commit

Permalink
Merge pull request #63 from roihuvaara/jr-spdy
Browse files Browse the repository at this point in the history
Allow specifying SPDY as protocol to support HTTP/2
  • Loading branch information
elhigu committed Feb 13, 2018
2 parents 79a5c6e + 0da32e8 commit 01c4b7a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
32 changes: 28 additions & 4 deletions lib/app/express/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,38 @@ function startServer(app) {
/**
* @private
*/
function createHttpsServer(app) {

function getSslOptions(app) {
if (!app.config.ssl || !app.config.ssl.key || !app.config.ssl.cert) {
throw new Error("Https requires ssl configuration with key and cert");
throw new Error( _.capitalize(app.config.protocol) + " requires ssl configuration with key and cert");
}
var sslOptions = {
key: fs.readFileSync(app.config.ssl.key, 'utf8'),
cert: fs.readFileSync(app.config.ssl.cert, 'utf8'),
passphrase: app.config.ssl.passphrase
};
app.server = require('https').createServer(sslOptions, app);

return sslOptions;
}

/**
* @private
*/
function createSpdyServer(app) {
var options = getSslOptions(app);
try {
const spdy = require('spdy');
app.server = spdy.createServer(options, app);
} catch (e) {
throw new Error("Optional dependency 'spdy' (https://www.npmjs.com/package/spdy) is required for spdy protocol.");
}
}

/**
* @private
*/
function createHttpsServer(app) {
app.server = require('https').createServer(getSslOptions(app), app);
}

/**
Expand Down Expand Up @@ -155,7 +177,9 @@ module.exports = {
next();
});

if (app.config.protocol === 'https') {
if (app.config.protocol === 'spdy') {
createSpdyServer(app);
} else if ( app.config.protocol === 'https' ) {
createHttpsServer(app);
} else {
createHttpServer(app);
Expand Down
19 changes: 19 additions & 0 deletions tests/app/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,25 @@ describe('main.js app create / start', function () {
});
});

it('should throw an error if config.protocol == spdy and no ssl config given', function () {
okConfig.protocol = 'spdy';
var fn = function () {
main.createApp(okConfig);
};
expect(fn).to.throw("Spdy requires ssl configuration with key and cert");
});
it('should throw an error if config.protocol == spdy and no spdy module installed', function () {
okConfig.protocol = 'spdy';
okConfig.ssl = {
key: path.join(__dirname, 'data', 'server.unencrypted.key'),
cert: path.join(__dirname, 'data', 'server.crt')
};
var fn = function () {
main.createApp(okConfig);
};
expect(fn).to.throw("Optional dependency 'spdy' (https://www.npmjs.com/package/spdy) is required for spdy protocol.");
});

it('should start correctly also when not in testing profile (NOTE: this will spam test run a bit)', function () {
delete okConfig['profile'];
var app = main.createApp(okConfig);
Expand Down

0 comments on commit 01c4b7a

Please sign in to comment.