+38
-1
patchbay/config.js
+38
-1
patchbay/config.js
···
21
21
return remote
22
22
}
23
23
24
+
function rewriteRemoteForLocation (remote) {
25
+
if (!remote || typeof window === 'undefined' || !window.location) return remote
26
+
27
+
try {
28
+
var parts = remote.split('~')
29
+
var base = parts[0]
30
+
var suffix = parts.length > 1 ? '~' + parts.slice(1).join('~') : ''
31
+
32
+
var u = URL.parse(base)
33
+
34
+
if (!u || !u.protocol || !/^wss?:$/.test(u.protocol)) return remote
35
+
36
+
// Only rewrite when the remote thinks it's localhost
37
+
if (u.hostname !== 'localhost' && u.hostname !== '127.0.0.1') return remote
38
+
39
+
var loc = window.location
40
+
41
+
// Use the page's hostname, keep the original port
42
+
u.protocol = loc.protocol === 'https:' ? 'wss:' : 'ws:'
43
+
u.hostname = loc.hostname
44
+
if (!u.port) {
45
+
u.port = loc.port || (loc.protocol === 'https:' ? '443' : '80')
46
+
}
47
+
48
+
var formatted = URL.format(u)
49
+
if (formatted.charAt(formatted.length - 1) === '/')
50
+
formatted = formatted.slice(0, -1)
51
+
52
+
return formatted + suffix
53
+
} catch (e) {
54
+
return remote
55
+
}
56
+
}
57
+
24
58
module.exports = function () {
25
59
var remote = loadRemote()
60
+
61
+
// In the browser, rewrite localhost remotes to match the page host,
62
+
// while preserving the pubkey and port from the original remote.
63
+
remote = rewriteRemoteForLocation(remote)
26
64
27
65
//TODO: use _several_ remotes, so if one goes down,
28
66
// you can still communicate via another...
···
45
83
blobsUrl: blobsUrl
46
84
}
47
85
}
48
-