Skip to content

Commit

Permalink
Allow domain to be set in socket options
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Jun 27, 2015
1 parent 985682c commit b2fa337
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 30 deletions.
45 changes: 17 additions & 28 deletions lib/connect-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,41 +69,30 @@ var connectUtils = {
},
getConnectionUrl: function (options) {

var protocol = "";
var string = "'%protocol%' + location.%host% + '%ns%'";
var socketOpts = connectUtils.resolveSocketOptions(options);
var protocol = "";
var withHostnamePort = "'{protocol}' + location.hostname + ':{port}{ns}'"; //
var withHost = "'{protocol}' + location.host + '{ns}'"; //
var withDomain = "'{domain}{ns}'";

// default use-case is server/proxy
var string = withHost;

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

return string
.replace("%protocol%", protocol)
.replace("%host%", socketOpts.host)
.replace("%ns%", socketOpts.namespace);
},
/**
* @param options
* @returns {{host: string, namespace: string}}
*/
resolveSocketOptions: function (options) {
var socketOpts = options.get('socket');

var socket = options.get("socket");
var namespace = socket.get("namespace");
var external = options.get("mode") === "snippet";

return {
host: external
? "hostname"
: "host",
namespace: (function (external) {

return external
? [":", options.get("port"), namespace].join("")
: namespace;
if (socketOpts.has('domain')) {
string = withDomain;
}

})(external)
};
return string
.replace("{protocol}", protocol)
.replace("{port}", options.get("port"))
.replace("{domain}", socketOpts.get("domain"))
.replace("{ns}", socketOpts.get("namespace"));
},
/**
* @param {Object} [options]
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", location.hostname));
document.write("<script %async% src='%script%'><\/script>".replace("HOST", location.hostname));
//]]></script>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"istanbul-coveralls": "^1.0.3",
"mocha": "^2.2.5",
"q": "^1.4.1",
"request": "^2.57.0",
"request": "^2.58.0",
"sinon": "^1.15.3",
"slugify": "^0.1.1",
"socket.io-client": "^1.3.5",
Expand Down
16 changes: 16 additions & 0 deletions test/specs/utils/utils.connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ var utils = require("../../../lib/connect-utils");
var merge = require("../../../lib/cli/cli-options").merge;
var assert = require("chai").assert;

// server,proxy: ['' + location.host + '/browser-sync']
// snippet: ['http://' + location.hostname + ':3000/browser-sync']
// domain: ['<domain>/browser-sync']

describe("Connection utils", function () {
var options;
beforeEach(function () {
Expand Down Expand Up @@ -73,4 +77,16 @@ describe("Connection utils", function () {
var actual = utils.socketConnector(options);
assert.include(actual, "'https://' + location.hostname + ':4002/browser-sync'");
});
it("should allow setting of the socket domain", function () {
var options = merge({
port: 3000,
server: "test/fixtures",
mode: "server",
socket: {
domain: 'localhost:3000'
}
});
var actual = utils.socketConnector(options);
assert.include(actual, "___browserSync___.io('localhost:3000/browser-sync', ___browserSync___.socketConfig);");
});
});

1 comment on commit b2fa337

@oliverkane
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This particular change broke some functionality for me. When hosting behind a load balancer / proxy which uses a different combination of https or ports, the client script no longer points to the external URL, but rather uses the internal one. This is similar to the issue referencing this commit about "Docker" support.

Please sign in to comment.