Created
April 17, 2019 13:15
-
-
Save bassosimone/a4d47b7264c07f72aaa6e11eb0ea5ff9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/www/index.html b/www/index.html | |
index 1aafce9..648bcd8 100644 | |
--- a/www/index.html | |
+++ b/www/index.html | |
@@ -32,6 +32,11 @@ | |
/* exported Run */ | |
/* globals libndt7 */ | |
+ function MaybeHostname() { | |
+ const url = new URL(window.location.href) | |
+ return (url.hash !== '') ? url.hash.substring(1) : '' | |
+ } | |
+ | |
function WithElementIdDo(elementId, callable) { | |
const elem = document.getElementById(elementId) | |
if (elem) { | |
@@ -51,7 +56,7 @@ | |
worker.postMessage({ | |
key: 'download', | |
value: { | |
- href: window.location.href | |
+ hostname: MaybeHostname() | |
}, | |
}) | |
worker.onmessage = function (ev) { | |
@@ -72,7 +77,7 @@ | |
worker.postMessage({ | |
key: 'upload', | |
value: { | |
- href: window.location.href | |
+ hostname: MaybeHostname() | |
}, | |
}) | |
worker.onmessage = function (ev) { | |
diff --git a/www/libndt7-core.js b/www/libndt7-core.js | |
index 73cc905..acdd14c 100644 | |
--- a/www/libndt7-core.js | |
+++ b/www/libndt7-core.js | |
@@ -55,8 +55,7 @@ const libndt7 = (function () { | |
// makeurl creates the URL from |settings| and |subtest| name. | |
const makeurl = function (settings, subtest) { | |
- let url = new URL(settings.href) | |
- url.protocol = (url.protocol === 'https:') ? 'wss:' : 'ws:' | |
+ let url = new URL('wss://' + settings.hostname) | |
url.pathname = '/ndt/v7/' + subtest | |
let params = new URLSearchParams() | |
settings.meta = (settings.meta !== undefined) ? settings : {} | |
@@ -165,6 +164,39 @@ const libndt7 = (function () { | |
} | |
} | |
+ // discoverHostname ensures settings.hostname is not empty, using | |
+ // mlab-ns to find out a suitable hostname if needed. | |
+ const discoverHostname = function (settings, callback) { | |
+ if (settings.hostname !== '') { | |
+ // Allow the user to specify a simplified hostname. | |
+ const re = /^mlab[1-9]{1}-[a-z]{3}[0-9]{2}$/ | |
+ if (settings.hostname.match(re)) { | |
+ settings.hostname = `ndt-iupui-${settings.hostname}.measurement-lab.org` | |
+ } | |
+ callback(settings) | |
+ return | |
+ } | |
+ // Implementation note: using the geo_options policy because in some | |
+ // sites, e.g. Turin, there is no mlab4. Using a testing mlabns service | |
+ // that returns us also the mlab4 sites if available. Of course, this | |
+ // will need to change when m-lab/ndt-server is widely deployed. | |
+ fetch('https://locate-dot-mlab-staging.appspot.com/ndt_ssl?policy=geo_options') | |
+ .then(function (response) { | |
+ return response.json() | |
+ }) | |
+ .then(function (doc) { | |
+ for (let i = 0; i < doc.length; ++i) { | |
+ const fqdn = doc[i].fqdn | |
+ if (fqdn.indexOf("-mlab4-") !== -1) { | |
+ settings.hostname = fqdn | |
+ callback(settings) | |
+ return | |
+ } | |
+ } | |
+ throw "Cannot find a suitable mlab server" | |
+ }) | |
+ } | |
+ | |
return { | |
// on is a publicly exported function that allows to set a handler | |
// for a specific event emitted by this library. |key| is the handler | |
@@ -175,12 +207,16 @@ const libndt7 = (function () { | |
// startDownload starts the ndt7 download. | |
startDownload: function () { | |
- download(setupconn(settings, 'download')) | |
+ discoverHostname(settings, function (settings) { | |
+ download(setupconn(settings, 'download')) | |
+ }) | |
}, | |
// startUpload starts the ndt7 upload. | |
startUpload: function () { | |
- upload(setupconn(settings, 'upload')) | |
+ discoverHostname(settings, function (settings) { | |
+ upload(setupconn(settings, 'upload')) | |
+ }) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment