Skip to content

Commit

Permalink
fix(socket-options): allow socket.domain string|fn for setting domain…
Browse files Browse the repository at this point in the history
… only on socket path - fixes #690
  • Loading branch information
shakyShane committed Jun 27, 2015
1 parent b2fa337 commit 5157432
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
25 changes: 12 additions & 13 deletions lib/connect-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,8 @@ var connectUtils = {

var socket = options.get("socket");
var template = fs.readFileSync(config.templates.connector, "utf-8");
var connectionUrl = require("url").parse(socket.get("namespace"));
var url = connectUtils.getConnectionUrl(options);

/**
* If namespace is a URL, just return it
*/
if (connectionUrl.host && connectionUrl.protocol) {
url = "'%s'".replace("%s", socket.get("namespace"));
}

template = template
.replace("%path%", socket.get("path"))
.replace("%url%", url);
Expand All @@ -69,6 +61,12 @@ var connectUtils = {
},
getConnectionUrl: function (options) {

var socketOpts = options.get('socket').toJS();

if (!socketOpts.namespace.match(/^\//)) {
socketOpts.namespace = "/" + socketOpts.namespace;
}

var protocol = "";
var withHostnamePort = "'{protocol}' + location.hostname + ':{port}{ns}'"; //
var withHost = "'{protocol}' + location.host + '{ns}'"; //
Expand All @@ -82,17 +80,18 @@ var connectUtils = {
string = withHostnamePort;
}

var socketOpts = options.get('socket');

if (socketOpts.has('domain')) {
if (socketOpts.domain) {
string = withDomain;
if (typeof socketOpts.domain === "function") {
socketOpts.domain = socketOpts.domain.call(null, options);
}
}

return string
.replace("{protocol}", protocol)
.replace("{port}", options.get("port"))
.replace("{domain}", socketOpts.get("domain"))
.replace("{ns}", socketOpts.get("namespace"));
.replace("{domain}", socketOpts.domain)
.replace("{ns}", socketOpts.namespace);
},
/**
* @param {Object} [options]
Expand Down
31 changes: 31 additions & 0 deletions test/specs/utils/utils.connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,35 @@ describe("Connection utils", function () {
var actual = utils.socketConnector(options);
assert.include(actual, "___browserSync___.io('localhost:3000/browser-sync', ___browserSync___.socketConfig);");
});
it("should allow setting of the socket domain + namespace", function () {
var options = merge({
port: 3000,
server: "test/fixtures",
mode: "server",
socket: {
namespace: 'shane',
domain: 'localhost:3000'
}
});
var actual = utils.socketConnector(options);
assert.include(actual, "___browserSync___.io('localhost:3000/shane', ___browserSync___.socketConfig);");
});
it("should allow setting of the socket domain (fn)+ namespace", function () {
var options = merge({
port: 3000,
server: "test/fixtures",
mode: "server",
urls: {
local: 'http://localhost:3002'
},
socket: {
namespace: 'shane',
domain: function (options) {
return options.getIn(['urls', 'local']);
}
}
});
var actual = utils.socketConnector(options);
assert.include(actual, "___browserSync___.io('http://localhost:3002/shane', ___browserSync___.socketConfig);");
});
});

0 comments on commit 5157432

Please sign in to comment.