Skip to content

Commit efd4f39

Browse files
committed
feat(http-protocol): Add support for https comms
1 parent c0fe70d commit efd4f39

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

lib/cli/command.reload.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
var error = "Could not contact BrowserSync server.";
3+
var error = "Could not contact BrowserSync server.";
44

55
/**
66
* $ browser-sync reload <options>
@@ -14,16 +14,25 @@ var error = "Could not contact BrowserSync server.";
1414
module.exports = function (opts) {
1515

1616
var flags = opts.cli.flags;
17-
var proto = require("../http-protocol");
17+
if (!flags.url) {
18+
flags.url = "http://localhost:" + (flags.port || 3000);
19+
}
20+
var proto = require("../http-protocol");
21+
var scheme = flags.url.match(/^https/) ? "https" : "http";
1822

19-
var url = proto.getUrl({method: "reload", args: flags.files}, "http://localhost:" + (flags.port || 3000));
23+
var url = proto.getUrl({method: "reload", args: flags.files}, flags.url);
2024

21-
require("http").get(url, function (res) {
25+
require(scheme).get(url, function (res) {
2226
if (res.statusCode !== 200) {
2327
require("logger").logger.error(error);
2428
return opts.cb(new Error(error));
2529
} else {
2630
opts.cb(null, res);
2731
}
32+
}).on("error", function (err) {
33+
if (err.code === "ECONNREFUSED") {
34+
err.message = "BrowserSync not running at " + flags.url;
35+
}
36+
return opts.cb(err);
2837
});
2938
};

lib/cli/opts.reload.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"files": "File paths to reload",
3-
"port": "Target a running instance by port number"
3+
"port": "Target a running instance by port number",
4+
"url": "Provide the full the url to the running BrowserSync instance"
45
}

test/specs/commands/reload.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var browserSync = require(path.resolve("./"));
55

66
var pkg = require(path.resolve("package.json"));
77
var sinon = require("sinon");
8+
var assert = require("chai").assert;
89
var cli = require(path.resolve(pkg.bin));
910

1011
describe("E2E CLI `reload` with no files arg", function () {
@@ -59,4 +60,44 @@ describe("E2E CLI `reload` with no files arg", function () {
5960
});
6061
});
6162
});
63+
it("should make a http request with files arg over HTTPS", function (done) {
64+
65+
browserSync.reset();
66+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
67+
68+
browserSync
69+
.create()
70+
.init({server: "test/fixtures", open: false, https: true}, function (err, bs) {
71+
72+
var spy = sinon.spy(bs.events, "emit");
73+
74+
cli({
75+
cli: {
76+
input: ["reload"],
77+
flags: {
78+
url: bs.options.getIn(["urls", "local"]),
79+
files: "core.css"
80+
}
81+
},
82+
cb: function () {
83+
sinon.assert.calledWithExactly(spy, "file:changed", {path: "core.css", log: true, namespace: "core"});
84+
bs.cleanup();
85+
done();
86+
}
87+
});
88+
});
89+
});
90+
it("should handle ECONNREFUSED errors nicely", function (done) {
91+
cli({
92+
cli: {
93+
input: ["reload"],
94+
flags: {}
95+
},
96+
cb: function (err) {
97+
assert.equal(err.code, "ECONNREFUSED");
98+
assert.equal(err.message, "BrowserSync not running at http://localhost:3000");
99+
done();
100+
}
101+
});
102+
});
62103
});

0 commit comments

Comments
 (0)