Skip to content

Commit b2fa337

Browse files
committed
Allow domain to be set in socket options
1 parent 985682c commit b2fa337

File tree

4 files changed

+35
-30
lines changed

4 files changed

+35
-30
lines changed

lib/connect-utils.js

+17-28
Original file line numberDiff line numberDiff line change
@@ -69,41 +69,30 @@ var connectUtils = {
6969
},
7070
getConnectionUrl: function (options) {
7171

72-
var protocol = "";
73-
var string = "'%protocol%' + location.%host% + '%ns%'";
74-
var socketOpts = connectUtils.resolveSocketOptions(options);
72+
var protocol = "";
73+
var withHostnamePort = "'{protocol}' + location.hostname + ':{port}{ns}'"; //
74+
var withHost = "'{protocol}' + location.host + '{ns}'"; //
75+
var withDomain = "'{domain}{ns}'";
76+
77+
// default use-case is server/proxy
78+
var string = withHost;
7579

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

80-
return string
81-
.replace("%protocol%", protocol)
82-
.replace("%host%", socketOpts.host)
83-
.replace("%ns%", socketOpts.namespace);
84-
},
85-
/**
86-
* @param options
87-
* @returns {{host: string, namespace: string}}
88-
*/
89-
resolveSocketOptions: function (options) {
85+
var socketOpts = options.get('socket');
9086

91-
var socket = options.get("socket");
92-
var namespace = socket.get("namespace");
93-
var external = options.get("mode") === "snippet";
94-
95-
return {
96-
host: external
97-
? "hostname"
98-
: "host",
99-
namespace: (function (external) {
100-
101-
return external
102-
? [":", options.get("port"), namespace].join("")
103-
: namespace;
87+
if (socketOpts.has('domain')) {
88+
string = withDomain;
89+
}
10490

105-
})(external)
106-
};
91+
return string
92+
.replace("{protocol}", protocol)
93+
.replace("{port}", options.get("port"))
94+
.replace("{domain}", socketOpts.get("domain"))
95+
.replace("{ns}", socketOpts.get("namespace"));
10796
},
10897
/**
10998
* @param {Object} [options]

lib/templates/script-tags.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<script type='text/javascript' id="__bs_script__">//<![CDATA[
2-
document.write("<script %async%src='%script%'><\/script>".replace("HOST", location.hostname));
2+
document.write("<script %async% src='%script%'><\/script>".replace("HOST", location.hostname));
33
//]]></script>

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"istanbul-coveralls": "^1.0.3",
7676
"mocha": "^2.2.5",
7777
"q": "^1.4.1",
78-
"request": "^2.57.0",
78+
"request": "^2.58.0",
7979
"sinon": "^1.15.3",
8080
"slugify": "^0.1.1",
8181
"socket.io-client": "^1.3.5",

test/specs/utils/utils.connect.js

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ var utils = require("../../../lib/connect-utils");
44
var merge = require("../../../lib/cli/cli-options").merge;
55
var assert = require("chai").assert;
66

7+
// server,proxy: ['' + location.host + '/browser-sync']
8+
// snippet: ['http://' + location.hostname + ':3000/browser-sync']
9+
// domain: ['<domain>/browser-sync']
10+
711
describe("Connection utils", function () {
812
var options;
913
beforeEach(function () {
@@ -73,4 +77,16 @@ describe("Connection utils", function () {
7377
var actual = utils.socketConnector(options);
7478
assert.include(actual, "'https://' + location.hostname + ':4002/browser-sync'");
7579
});
80+
it("should allow setting of the socket domain", function () {
81+
var options = merge({
82+
port: 3000,
83+
server: "test/fixtures",
84+
mode: "server",
85+
socket: {
86+
domain: 'localhost:3000'
87+
}
88+
});
89+
var actual = utils.socketConnector(options);
90+
assert.include(actual, "___browserSync___.io('localhost:3000/browser-sync', ___browserSync___.socketConfig);");
91+
});
7692
});

0 commit comments

Comments
 (0)