Skip to content

Commit 5157432

Browse files
committed
fix(socket-options): allow socket.domain string|fn for setting domain only on socket path - fixes #690
1 parent b2fa337 commit 5157432

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

lib/connect-utils.js

+12-13
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,8 @@ var connectUtils = {
5151

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

57-
/**
58-
* If namespace is a URL, just return it
59-
*/
60-
if (connectionUrl.host && connectionUrl.protocol) {
61-
url = "'%s'".replace("%s", socket.get("namespace"));
62-
}
63-
6456
template = template
6557
.replace("%path%", socket.get("path"))
6658
.replace("%url%", url);
@@ -69,6 +61,12 @@ var connectUtils = {
6961
},
7062
getConnectionUrl: function (options) {
7163

64+
var socketOpts = options.get('socket').toJS();
65+
66+
if (!socketOpts.namespace.match(/^\//)) {
67+
socketOpts.namespace = "/" + socketOpts.namespace;
68+
}
69+
7270
var protocol = "";
7371
var withHostnamePort = "'{protocol}' + location.hostname + ':{port}{ns}'"; //
7472
var withHost = "'{protocol}' + location.host + '{ns}'"; //
@@ -82,17 +80,18 @@ var connectUtils = {
8280
string = withHostnamePort;
8381
}
8482

85-
var socketOpts = options.get('socket');
86-
87-
if (socketOpts.has('domain')) {
83+
if (socketOpts.domain) {
8884
string = withDomain;
85+
if (typeof socketOpts.domain === "function") {
86+
socketOpts.domain = socketOpts.domain.call(null, options);
87+
}
8988
}
9089

9190
return string
9291
.replace("{protocol}", protocol)
9392
.replace("{port}", options.get("port"))
94-
.replace("{domain}", socketOpts.get("domain"))
95-
.replace("{ns}", socketOpts.get("namespace"));
93+
.replace("{domain}", socketOpts.domain)
94+
.replace("{ns}", socketOpts.namespace);
9695
},
9796
/**
9897
* @param {Object} [options]

test/specs/utils/utils.connect.js

+31
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,35 @@ describe("Connection utils", function () {
8989
var actual = utils.socketConnector(options);
9090
assert.include(actual, "___browserSync___.io('localhost:3000/browser-sync', ___browserSync___.socketConfig);");
9191
});
92+
it("should allow setting of the socket domain + namespace", function () {
93+
var options = merge({
94+
port: 3000,
95+
server: "test/fixtures",
96+
mode: "server",
97+
socket: {
98+
namespace: 'shane',
99+
domain: 'localhost:3000'
100+
}
101+
});
102+
var actual = utils.socketConnector(options);
103+
assert.include(actual, "___browserSync___.io('localhost:3000/shane', ___browserSync___.socketConfig);");
104+
});
105+
it("should allow setting of the socket domain (fn)+ namespace", function () {
106+
var options = merge({
107+
port: 3000,
108+
server: "test/fixtures",
109+
mode: "server",
110+
urls: {
111+
local: 'http://localhost:3002'
112+
},
113+
socket: {
114+
namespace: 'shane',
115+
domain: function (options) {
116+
return options.getIn(['urls', 'local']);
117+
}
118+
}
119+
});
120+
var actual = utils.socketConnector(options);
121+
assert.include(actual, "___browserSync___.io('http://localhost:3002/shane', ___browserSync___.socketConfig);");
122+
});
92123
});

0 commit comments

Comments
 (0)