Skip to content

Commit

Permalink
fix(paths): Fix regression with absolute/relative paths to scripts/so…
Browse files Browse the repository at this point in the history
…ckets/https etc - fixes #463
  • Loading branch information
shakyShane committed Feb 19, 2015
1 parent ab408c6 commit 2386fe1
Show file tree
Hide file tree
Showing 13 changed files with 250 additions and 56 deletions.
11 changes: 1 addition & 10 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,7 @@ module.exports = {
"files:watch",
bs.options.get("files"),
bs.pluginManager.pluginOptions
),
mode: (function () {
if (bs.options.get("server")) {
return "server";
}
if (bs.options.get("proxy")) {
return "proxy";
}
return "snippet";
})()
)
}
});
},
Expand Down
4 changes: 3 additions & 1 deletion lib/browser-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ BrowserSync.prototype.init = function (options, cb) {
* Some options are not compatible and will cause us to
* end the process.
*/
utils.verifyConfig(options, bs.cb);
if (!utils.verifyConfig(options, bs.cb)) {
return;
}

/**
* Save a reference to the original options
Expand Down
4 changes: 4 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ module.exports = {
template: "/cli-template.js",
client: {
shims: "/client/client-shims.js"
},
errors: {
"server+proxy": "Invalid config. You cannot specify both server & proxy options.",
"proxy+https": "Invalid config. You set https: true, but your proxy target doesn't reflect this."
}
};
30 changes: 26 additions & 4 deletions lib/connect-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ var connectUtils = {
scriptTags: function (options) {

function getPath(relative, port) {
return options.get("scheme") + "://HOST:" + port + relative;
if (options.get("mode") === "snippet") {
return options.get("scheme") + "://HOST:" + port + relative;
} else {
return "//HOST:" + port + relative;
}
}

var template = fs.readFileSync(config.templates.scriptTag, "utf-8");
Expand All @@ -23,14 +27,15 @@ var connectUtils = {
var override = false;

if (_.isFunction(options.get("scriptPath"))) {
script = options.get("scriptPath")(scriptPath, options.get("port"), options);
var args = getScriptArgs(options, scriptPath);
script = options.get("scriptPath").apply(null, args);
override = true;
} else {
script = getPath(scriptPath, options.get("port"));
}

if (!override && (options.get("server") || options.get("proxy"))) {
script = getPath(scriptPath, options.get("port"));
script = scriptPath;
}

template = template
Expand Down Expand Up @@ -64,7 +69,7 @@ var connectUtils = {
},
getConnectionUrl: function (options) {

var protocol = options.get("scheme") + "://";
var protocol = "";
var namespace = options.getIn(["socket", "namespace"]);
var string = "'%protocol%' + location.%host% + '%ns%'";
var socketOpts = connectUtils.resolveSocketOptions(options);
Expand All @@ -73,6 +78,10 @@ var connectUtils = {
return "'%s'".replace("%s", namespace(defaultConfig.socket.namespace, options));
}

if (options.get("mode") === "snippet") {
protocol = options.get("scheme") + "://";
}

return string
.replace("%protocol%", protocol)
.replace("%host%", socketOpts.host)
Expand Down Expand Up @@ -124,4 +133,17 @@ var connectUtils = {
}
};

/**
* @param options
* @returns {*[]}
*/
function getScriptArgs (options, scriptPath) {
var abspath = options.get("scheme") + "://HOST:" + options.get("port") + scriptPath;
return [
scriptPath,
options.get("port"),
options.set("absolute", abspath)
];
}

module.exports = connectUtils;
17 changes: 17 additions & 0 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports.update = function (options) {

return options.withMutations(function (item) {

setMode(item);
setScheme(item);
setStartPath(item);
setServerOpts(item);
Expand All @@ -33,6 +34,22 @@ module.exports.update = function (options) {
});
};

/**
* Set the running mode
* @param item
*/
function setMode (item) {
item.set("mode", (function () {
if (item.get("server")) {
return "server";
}
if (item.get("proxy")) {
return "proxy";
}
return "snippet";
})());
}

/**
* @param item
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/script-tags.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<script type='text/javascript' id="__bs_script__">//<![CDATA[
document.write("<script async src='%script%'><\/script>".replace(/HOST/g, location.hostname).replace(/PORT/g, location.port));
document.write("<script async src='%script%'><\/script>".replace("HOST", location.hostname));
//]]></script>
13 changes: 9 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,20 @@ var utils = {
*/
getConfigErrors: function (options) {

var messages = {
"server+proxy": "Invalid config. You cannot specify both server & proxy options."
};
var messages = require("./config").errors;

var errors = [];

if (options.get("server") && options.get("proxy")) {
errors.push(messages["server+proxy"]);
}

if (options.get("https") && options.get("proxy")) {
if (options.getIn(["proxy", "url", "protocol"]) !== "https:") {
errors.push([messages["proxy+https"], options.getIn(["proxy", "target"])].join(" "));
}
}

return errors;
},
/**
Expand All @@ -311,7 +315,8 @@ var utils = {
verifyConfig: function (options, cb) {
var errors = utils.getConfigErrors(options);
if (errors.length) {
return utils.fail(true, errors.join("\n"), cb);
utils.fail(true, errors.join("\n"), cb);
return false;
}
return true;
},
Expand Down
5 changes: 4 additions & 1 deletion test/protractor/_run.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ module.exports = function (logger) {
return function (config, configFile, cb) {
browserSync.reset();
var instance = browserSync(config.bsConfig, function (err, bs) {
var url = bs.getOptionIn(["urls", "local"]);
var url = bs.getOptionIn(["urls", "local"]);
var uiurl = bs.getOptionIn(["urls", "ui"]);
process.env["BS_BASE"] = url;
process.env["BS_UI"] = uiurl;
process.env["BS_SCRIPT_PATH"] = bs.getOptionIn(["scriptPaths", "path"]);
logger.info("Testing BrowserSync at %s", url);

Expand All @@ -28,6 +30,7 @@ module.exports = function (logger) {
function runTests (config, configFile, bs, cb) {
var out = "";
exec("protractor " + configFile, function (err, stdout) {
console.log(stdout);
if (err) {
doCallback({
code: 1,
Expand Down
70 changes: 51 additions & 19 deletions test/protractor/tests.multi.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
"use strict";

var fs = require("fs");
var path = require("path");
var connect = require("connect");
var serveStatic = require("serve-static");
var http = require("http");
var utils = require("../../lib/server/utils");
var Immutable = require("immutable");

function getApp (bs, options) {

var html = "<!doctype html><html lang=\"en-US\"><head><meta charset=\"UTF-8\"><title>Browsersync</title></head><body>BS</body></html>";
var app = connect();

app.use("/", function (req, res) {
res.setHeader("content-type", "text/html");
res.end(html.replace("BS", bs.getOption("snippet")));
});

var appserver = utils.getServer(app, options);

return appserver;
}

module.exports = {
"Proxy Test Laravel App": {
Expand All @@ -28,6 +41,13 @@ module.exports = {
logLevel: "silent"
}
},
"Secure Proxy": {
bsConfig: {
proxy: "https://grenade.static:8890",
open: false,
logLevel: "silent"
}
},
"Server": {
bsConfig: {
server: "./test/fixtures",
Expand All @@ -48,23 +68,35 @@ module.exports = {
logLevel: "silent"
},
before: function (bs, cb) {
var filepath = path.resolve(__dirname + "/../fixtures/index.html");
var file = fs.readFileSync(filepath, "utf-8");
var modded = file.replace("<!-- BrowserSync -->", bs.getOption("snippet"));
var app = connect();
app.use(serveStatic(path.resolve(__dirname + "/../fixtures")));
var server = http.createServer(app).listen();
var port = server.address().port;
var url = "http://localhost:" + port;
process.env["BS_BASE"] = url;
fs.writeFileSync(filepath, modded);
var app = getApp(bs, Immutable.Map({scheme: "http"}));
app.server.listen();
process.env["BS_BASE"] = "http://localhost:" + app.server.address().port;
cb();
}
},
"Secure Snippet on Insecure Website": {
bsConfig: {
logLevel: "silent",
https: true
},
before: function (bs, cb) {

var app = getApp(bs, Immutable.Map({scheme: "http"}));
app.server.listen();
process.env["BS_BASE"] = "http://localhost:" + app.server.address().port;
cb();
}
},
"Secure Snippet on Secure Website": {
bsConfig: {
logLevel: "silent",
https: true
},
after: function (bs, cb) {
var filepath = path.resolve(__dirname + "/../fixtures/index.html");
var file = fs.readFileSync(filepath, "utf-8");
var modded = file.replace(bs.getOption("snippet"), "<!-- BrowserSync -->");
fs.writeFileSync(filepath, modded);
before: function (bs, cb) {

var app = getApp(bs, Immutable.Map({scheme: "https"}));
app.server.listen();
process.env["BS_BASE"] = "https://localhost:" + app.server.address().port;
cb();
}
}
Expand Down
24 changes: 13 additions & 11 deletions test/protractor/tests/snippet.injection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
*
*/
// abstract writing screen shot to a file
var fs = require("fs");
var slugify = require("slugify");
function writeScreenShot(data, filename) {
var stream = fs.createWriteStream(filename);
stream.write(new Buffer(data, "base64"));
stream.end();
}
//var fs = require("fs");
//var slugify = require("slugify");
//function writeScreenShot(data, filename) {
// var stream = fs.createWriteStream(filename);
// stream.write(new Buffer(data, "base64"));
// stream.end();
//}

/**
*
Expand All @@ -19,15 +19,17 @@ describe("Section Navigation", function () {
beforeEach(function () {
browser.ignoreSynchronization = true;
browser.get("/");
// within a test:
browser.takeScreenshot().then(function (png) {
writeScreenShot(png, "screenshots/ss_" + slugify(process.env["BS_TEST_NAME"]) + ".png");
});
});
it("should contain the BS script element", function () {
expect(element(by.id("__bs_script__")).isPresent()).toBeTruthy();
});
it("should contain the BS NOTIFY ELEMENT", function () {
//browser.pause();
expect(element(by.id("__bs_notify__")).isPresent()).toBeTruthy();
});
it("should launch UI", function () {
browser.ignoreSynchronization = false;
browser.get(process.env["BS_UI"]);
expect(element.all(by.css("[bs-heading]")).get(0).isPresent()).toBeTruthy();
});
});
23 changes: 23 additions & 0 deletions test/protractor/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";

var utils = require("../../lib/server/utils");
var connect = require("connect");

module.exports = {

getApp: function getApp (options) {

var html = "<!doctype html><html lang=\"en-US\"><head><meta charset=\"UTF-8\"><title>Browsersync</title></head><body>BS</body></html>";
var app = connect();

app.use("/", function (req, res) {
res.setHeader("content-type", "text/html");
res.end(html);
});

var appserver = utils.getServer(app, options);
appserver.html = html;

return appserver;
}
};
Loading

0 comments on commit 2386fe1

Please sign in to comment.