···2121 return remote
2222}
23232424+function rewriteRemoteForLocation (remote) {
2525+ if (!remote || typeof window === 'undefined' || !window.location) return remote
2626+2727+ try {
2828+ var parts = remote.split('~')
2929+ var base = parts[0]
3030+ var suffix = parts.length > 1 ? '~' + parts.slice(1).join('~') : ''
3131+3232+ var u = URL.parse(base)
3333+3434+ if (!u || !u.protocol || !/^wss?:$/.test(u.protocol)) return remote
3535+3636+ // Only rewrite when the remote thinks it's localhost
3737+ if (u.hostname !== 'localhost' && u.hostname !== '127.0.0.1') return remote
3838+3939+ var loc = window.location
4040+4141+ // Use the page's hostname, keep the original port
4242+ u.protocol = loc.protocol === 'https:' ? 'wss:' : 'ws:'
4343+ u.hostname = loc.hostname
4444+ if (!u.port) {
4545+ u.port = loc.port || (loc.protocol === 'https:' ? '443' : '80')
4646+ }
4747+4848+ var formatted = URL.format(u)
4949+ if (formatted.charAt(formatted.length - 1) === '/')
5050+ formatted = formatted.slice(0, -1)
5151+5252+ return formatted + suffix
5353+ } catch (e) {
5454+ return remote
5555+ }
5656+}
5757+2458module.exports = function () {
2559 var remote = loadRemote()
6060+6161+ // In the browser, rewrite localhost remotes to match the page host,
6262+ // while preserving the pubkey and port from the original remote.
6363+ remote = rewriteRemoteForLocation(remote)
26642765 //TODO: use _several_ remotes, so if one goes down,
2866 // you can still communicate via another...
···4583 blobsUrl: blobsUrl
4684 }
4785}
4848-