Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing options to tiny-lr (live reload) for HTTPS support #4249

Merged
merged 1 commit into from Jun 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/tasks/server/express-server.js
Expand Up @@ -62,7 +62,7 @@ module.exports = Task.extend({
if(!fs.existsSync(this.startOptions.sslKey)) {
throw new TypeError('SSL key couldn\'t be found in "' + this.startOptions.sslKey + '", please provide a path to an existing ssl key file with --ssl-key');
}
if(!fs.existsSync(this.startOptions.sslKey)) {
if(!fs.existsSync(this.startOptions.sslCert)) {
throw new TypeError('SSL certificate couldn\'t be found in "' + this.startOptions.sslCert + '", please provide a path to an existing ssl certificate file with --ssl-cert');
}
var options = {
Expand Down
36 changes: 24 additions & 12 deletions lib/tasks/server/livereload-server.js
Expand Up @@ -2,43 +2,55 @@

var Promise = require('../../ext/promise');
var path = require('path');
var fs = require('fs');
var Task = require('../../models/task');
var SilentError = require('silent-error');

function createServer() {
function createServer(options) {
var instance;

var Server = (require('tiny-lr')).Server;
Server.prototype.error = function() {
instance.error.apply(instance, arguments);
};
instance = new Server();
instance = new Server(options);
return instance;
}

module.exports = Task.extend({
liveReloadServer: function() {
liveReloadServer: function(options) {
if (this._liveReloadServer) {
return this._liveReloadServer;
}

this._liveReloadServer = createServer();
this._liveReloadServer = createServer(options);
return this._liveReloadServer;
},


listen: function(port) {
var server = this.liveReloadServer();
listen: function(options) {
var server = this.liveReloadServer(options);

return new Promise(function(resolve, reject) {
server.error = reject;
server.listen(port, resolve);
server.listen(options.liveReloadPort, resolve);
});
},

start: function(options) {
var tlroptions = {};
tlroptions.ssl = options.ssl || false;
tlroptions.port = options.liveReloadPort || 35729;

if (options.liveReload !== true) {
return Promise.resolve('Livereload server manually disabled.');
}

if (options.ssl) {
tlroptions.key = fs.readFileSync(options.sslKey);
tlroptions.cert = fs.readFileSync(options.sslCert);
}

// Reload on file changes
this.watcher.on('change', this.didChange.bind(this));
this.watcher.on('error', this.didChange.bind(this));
Expand All @@ -47,13 +59,13 @@ module.exports = Task.extend({
this.expressServer.on('restart', this.didRestart.bind(this));

// Start LiveReload server
return this.listen(options.liveReloadPort)
.then(this.writeBanner.bind(this, options.liveReloadPort))
.catch(this.writeErrorBanner.bind(this, options.liveReloadPort));
return this.listen(tlroptions)
.then(this.writeBanner.bind(this, options.liveReloadPort, tlroptions.ssl))
.catch(this.writeErrorBanner.bind(this, tlroptions.port));
},

writeBanner: function(port) {
this.ui.writeLine('Livereload server on port ' + port);
writeBanner: function(port, ssl) {
this.ui.writeLine('Livereload server on port ' + port + ' ' + (ssl ? '(https)' : '(http)'));
},

writeErrorBanner: function(port) {
Expand Down
35 changes: 34 additions & 1 deletion tests/unit/tasks/server/livereload-server-test.js
Expand Up @@ -56,7 +56,7 @@ describe('livereload-server', function() {
liveReloadPort: 1337,
liveReload: true
}).then(function() {
expect(ui.output).to.equal('Livereload server on port 1337' + EOL);
expect(ui.output).to.equal('Livereload server on port 1337 (http)' + EOL);
});
});

Expand All @@ -77,6 +77,39 @@ describe('livereload-server', function() {
});
});

describe('start with https', function() {
it('correctly indicates which port livereload is present on and running in https mode', function() {
return subject.start({
liveReloadPort: 1337,
liveReload: true,
ssl: true,
sslKey: 'tests/fixtures/ssl/server.key',
sslCert: 'tests/fixtures/ssl/server.crt'
}).then(function() {
expect(ui.output).to.equal('Livereload server on port 1337 (https)' + EOL);
});
});

it('informs of error during startup', function(done) {
var preexistingServer = net.createServer();
preexistingServer.listen(1337);

return subject.start({
liveReloadPort: 1337,
liveReload: true,
ssl: true,
sslKey: 'tests/fixtures/ssl/server.key',
sslCert: 'tests/fixtures/ssl/server.crt'
})
.catch(function(reason) {
expect(reason).to.equal('Livereload failed on port 1337. It is either in use or you do not have permission.' + EOL);
})
.finally(function() {
preexistingServer.close(done);
});
});
});

describe('express server restart', function() {
it('triggers when the express server restarts', function() {
var calls = 0;
Expand Down