Skip to content

backport fix for https and add null check #604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,30 @@ var url = require('url');
var SockJS = require("sockjs-client");
var stripAnsi = require('strip-ansi');

function getCurrentScriptSource() {
// `document.currentScript` is the most accurate way to find the current script,
// but is not supported in all browsers.
if(document.currentScript)
return document.currentScript.getAttribute("src");
// Fall back to getting all scripts in the document.
var scriptElements = document.scripts || [];
var currentScript = scriptElements[scriptElements.length - 1];
if(currentScript)
return currentScript.getAttribute("src");
// Fail as there was no script to use.
throw new Error("[WDS] Failed to get current script source");
}

// If this bundle is inlined, use the resource query to get the correct url.
// Else, get the url from the <script> this file was called with.
var urlParts;
if (typeof __resourceQuery === "string" && __resourceQuery) {
urlParts = url.parse(__resourceQuery.substr(1));
} else {
var scriptElements = document.getElementsByTagName("script");
urlParts = url.parse(scriptElements[scriptElements.length-1].getAttribute("src").replace(/\/[^\/]+$/, ""))
// Else, get the url from the <script> this file was called with.
var scriptHost = getCurrentScriptSource();
scriptHost = scriptHost.replace(/\/[^\/]+$/, "");
urlParts = url.parse((scriptHost ? scriptHost : "/"), false, true);
}

var sock = null;
Expand Down Expand Up @@ -57,14 +73,29 @@ var onSocketMsg = {
}
};

var newConnection = function() {
sock = new SockJS(url.format({

var hostname = urlParts.hostname;
if(!urlParts.hostname || urlParts.hostname === '0.0.0.0') {
// why do we need this check?
// hostname n/a for file protocol (example, when using electron, ionic)
// see: https://github.com/webpack/webpack-dev-server/pull/384
if(window.location.hostname && !!~window.location.protocol.indexOf('http')) {
hostname = window.location.hostname;
}
}

var port = (!urlParts.port || urlParts.port === '0') ? window.location.port : urlParts.port;

var formattedUrl = url.format({
protocol: (window.location.protocol === "https:" || urlParts.hostname === '0.0.0.0') ? window.location.protocol : urlParts.protocol,
auth: urlParts.auth,
hostname: (urlParts.hostname === '0.0.0.0') ? window.location.hostname : urlParts.hostname,
port: (urlParts.port === '0') ? window.location.port : urlParts.port,
hostname: hostname,
port: port,
pathname: urlParts.path == null || urlParts.path === '/' ? "/sockjs-node" : urlParts.path
}));
});

var newConnection = function() {
sock = new SockJS(formattedUrl);

sock.onclose = function() {
console.error("[WDS] Disconnected!");
Expand Down