From 0acd00f8cadc160aaf380ddf4de6fb7dc1e7a9db Mon Sep 17 00:00:00 2001 From: Tim Kang Date: Thu, 6 Nov 2014 19:04:17 -0800 Subject: [PATCH 1/4] Expose hostname parameter of tiny-lr --- tasks/lib/livereload.js | 13 ++++++++----- test/fixtures/livereload/Gruntfile.js | 3 ++- test/tasks/livereload_test.js | 14 +++++++------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/tasks/lib/livereload.js b/tasks/lib/livereload.js index 5e8ab04..94b40f4 100644 --- a/tasks/lib/livereload.js +++ b/tasks/lib/livereload.js @@ -26,8 +26,11 @@ module.exports = function(grunt) { } else { options = _.defaults(options, defaults); } - if (servers[options.port]) { - this.server = servers[options.port]; + + var host = (options.hostname || '') + ':' + options.port; + + if (servers[host]) { + this.server = servers[host]; } else { this.server = tinylr(options); this.server.server.removeAllListeners('error'); @@ -39,11 +42,11 @@ module.exports = function(grunt) { } process.exit(1); }); - this.server.listen(options.port, function(err) { + this.server.listen(options.port, options.hostname, function (err) { if (err) { return grunt.fatal(err); } - grunt.log.verbose.writeln('Live reload server started on port: ' + options.port); + grunt.log.verbose.writeln('Live reload server started on ' + host); }); - servers[options.port] = this.server; + servers[host] = this.server; } } diff --git a/test/fixtures/livereload/Gruntfile.js b/test/fixtures/livereload/Gruntfile.js index 249f0c2..2e8bbc3 100644 --- a/test/fixtures/livereload/Gruntfile.js +++ b/test/fixtures/livereload/Gruntfile.js @@ -12,11 +12,12 @@ module.exports = function(grunt) { files: ['lib/*.js'], tasks: ['before'], }, - customport: { + customhost: { files: ['lib/*.js'], tasks: ['before'], options: { livereload: { + hostname: 'localhost', port: 8675, }, }, diff --git a/test/tasks/livereload_test.js b/test/tasks/livereload_test.js index 8beb82c..4a0b13f 100644 --- a/test/tasks/livereload_test.js +++ b/test/tasks/livereload_test.js @@ -56,18 +56,18 @@ exports.livereload = { result = helper.unixify(result); helper.verboseLog(result); test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.'); - test.ok(result.indexOf('Live reload server started on port: 35729') !== -1, 'live reload server should have been started on port 35729.'); + test.ok(result.indexOf('Live reload server started on :35729') !== -1, 'live reload server should have been started on port 35729.'); test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js'); resultData = JSON.parse(resultData); test.equal(resultData.tinylr, 'Welcome', 'tinylr server should have welcomed you.'); test.done(); }); }, - customport: function(test) { + customhost: function(test) { test.expect(4); var resultData = ''; var cwd = path.resolve(fixtures, 'livereload'); - var assertWatch = helper.assertTask(['watch:customport', '-v'], {cwd: cwd}); + var assertWatch = helper.assertTask(['watch:customhost', '-v'], {cwd: cwd}); assertWatch([function() { request(8675, function(data) { resultData += data; @@ -77,7 +77,7 @@ exports.livereload = { result = helper.unixify(result); helper.verboseLog(result); test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.'); - test.ok(result.indexOf('Live reload server started on port: 8675') !== -1, 'live reload server should have been started on port 35729.'); + test.ok(result.indexOf('Live reload server started on localhost:8675') !== -1, 'live reload server should have been started on localhost:8675.'); test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js'); resultData = JSON.parse(resultData); test.equal(resultData.tinylr, 'Welcome', 'tinylr server should have welcomed you.'); @@ -116,7 +116,7 @@ exports.livereload = { result = helper.unixify(result); helper.verboseLog(result); test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.'); - test.ok(result.indexOf('Live reload server started on port: 9876') !== -1, 'live reload server should have been started on port 9876.'); + test.ok(result.indexOf('Live reload server started on :9876') !== -1, 'live reload server should have been started on port 9876.'); test.ok(/Live reloading (lib\/one\.js, lib\/two.js|lib\/two.js, lib\/one.js)\.\.\./.test(result), 'live reload should have triggered on lib/one.js and lib/two.js'); resultData = JSON.parse(resultData); test.equal(resultData.tinylr, 'Welcome', 'tinylr server should have welcomed you.'); @@ -137,7 +137,7 @@ exports.livereload = { result = helper.unixify(result); helper.verboseLog(result); test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.'); - test.ok(result.indexOf('Live reload server started on port: 1337') !== -1, 'live reload server should have been started on port 1337.'); + test.ok(result.indexOf('Live reload server started on :1337') !== -1, 'live reload server should have been started on port 1337.'); test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js'); resultData = JSON.parse(resultData); test.equal(resultData.tinylr, 'Welcome', 'tinylr server should have welcomed you.'); @@ -157,7 +157,7 @@ exports.livereload = { }], function(result) { result = helper.unixify(result); helper.verboseLog(result); - test.ok(result.indexOf('Live reload server started on port: 35729') !== -1, 'live reload server should have been started on port 35729.'); + test.ok(result.indexOf('Live reload server started on :35729') !== -1, 'live reload server should have been started on port 35729.'); test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js'); resultData = JSON.parse(resultData); test.equal(resultData.tinylr, 'Welcome', 'tinylr server should have welcomed you.'); From a8bb23099064fea5cc1a626c4da565c0f8b38fb5 Mon Sep 17 00:00:00 2001 From: Tim Kang Date: Fri, 20 Feb 2015 20:48:30 -0800 Subject: [PATCH 2/4] Make it more clear that the server is listening on INADDR_ANY --- tasks/lib/livereload.js | 4 ++-- test/tasks/livereload_test.js | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tasks/lib/livereload.js b/tasks/lib/livereload.js index a1f0010..6999ac0 100644 --- a/tasks/lib/livereload.js +++ b/tasks/lib/livereload.js @@ -27,7 +27,7 @@ module.exports = function(grunt) { options = _.defaults(options, defaults); } - var host = (options.hostname || '') + ':' + options.port; + var host = (options.host || '*') + ':' + options.port; if (servers[host]) { this.server = servers[host]; @@ -42,7 +42,7 @@ module.exports = function(grunt) { } process.exit(1); }); - this.server.listen(options.port, options.hostname, function (err) { + this.server.listen(options.port, options.host, function(err) { if (err) { return grunt.fatal(err); } grunt.log.verbose.writeln('Live reload server started on ' + host); }); diff --git a/test/tasks/livereload_test.js b/test/tasks/livereload_test.js index dcc0039..67c8328 100644 --- a/test/tasks/livereload_test.js +++ b/test/tasks/livereload_test.js @@ -56,7 +56,7 @@ exports.livereload = { result = helper.unixify(result); helper.verboseLog(result); test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.'); - test.ok(result.indexOf('Live reload server started on :35729') !== -1, + test.ok(result.indexOf('Live reload server started on *:35729') !== -1, 'live reload server should have been started on port 35729.'); test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js'); resultData = JSON.parse(resultData); @@ -119,7 +119,7 @@ exports.livereload = { result = helper.unixify(result); helper.verboseLog(result); test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.'); - test.ok(result.indexOf('Live reload server started on :9876') !== -1, + test.ok(result.indexOf('Live reload server started on *:9876') !== -1, 'live reload server should have been started on port 9876.'); test.ok(/Live reloading (lib\/one\.js, lib\/two.js|lib\/two.js, lib\/one.js)\.\.\./.test(result), 'live reload should have triggered on lib/one.js and lib/two.js'); @@ -142,7 +142,7 @@ exports.livereload = { result = helper.unixify(result); helper.verboseLog(result); test.ok(result.indexOf('I ran before livereload.') !== -1, 'task should have ran before live reload.'); - test.ok(result.indexOf('Live reload server started on :1337') !== -1, + test.ok(result.indexOf('Live reload server started on *:1337') !== -1, 'live reload server should have been started on port 1337.'); test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js'); resultData = JSON.parse(resultData); @@ -163,7 +163,7 @@ exports.livereload = { }], function(result) { result = helper.unixify(result); helper.verboseLog(result); - test.ok(result.indexOf('Live reload server started on :35729') !== -1, + test.ok(result.indexOf('Live reload server started on *:35729') !== -1, 'live reload server should have been started on port 35729.'); test.ok(result.indexOf('Live reloading lib/one.js...') !== -1, 'live reload should have triggered on lib/one.js'); resultData = JSON.parse(resultData); From a0bd4375772d1e92c82162c3730b8cce6c4a58e9 Mon Sep 17 00:00:00 2001 From: Tim Kang Date: Fri, 20 Feb 2015 21:10:38 -0800 Subject: [PATCH 3/4] Update docs to demonstrate `hostname` option usage --- docs/watch-options.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/watch-options.md b/docs/watch-options.md index 40d5a1f..80c4913 100644 --- a/docs/watch-options.md +++ b/docs/watch-options.md @@ -171,7 +171,7 @@ watch: { }, ``` -It's possible to get livereload working over https connections. To do this, pass an object to `livereload` with a `key` and `cert` paths specified. +Passing an object to `livereload` allows listening on a specific port and hostname/IP or over https connections (by specifying `key` and `cert` paths). Example: ```js @@ -181,6 +181,7 @@ watch: { tasks: ['sass'], options: { livereload: { + host: 'localhost', port: 9000, key: grunt.file.read('path/to/ssl.key'), cert: grunt.file.read('path/to/ssl.crt') From 138b06727154252a74aa3b9ccf8e7eeaecd914be Mon Sep 17 00:00:00 2001 From: Tim Kang Date: Fri, 20 Feb 2015 21:25:29 -0800 Subject: [PATCH 4/4] Fix livereload test fixture that was missed in a8bb230 --- test/fixtures/livereload/Gruntfile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fixtures/livereload/Gruntfile.js b/test/fixtures/livereload/Gruntfile.js index 2e8bbc3..09b359f 100644 --- a/test/fixtures/livereload/Gruntfile.js +++ b/test/fixtures/livereload/Gruntfile.js @@ -17,7 +17,7 @@ module.exports = function(grunt) { tasks: ['before'], options: { livereload: { - hostname: 'localhost', + host: 'localhost', port: 8675, }, },