Assume wss:// with no port when frontend is HTTPS (#2129)

This commit is contained in:
Masen Furer 2023-11-06 12:06:07 -08:00 committed by GitHub
parent 6a7bab8416
commit 53d4c438ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,6 +12,9 @@ import { initialEvents } from "utils/context.js"
const EVENTURL = env.EVENT
const UPLOADURL = env.UPLOAD
// These hostnames indicate that the backend and frontend are reachable via the same domain.
const SAME_DOMAIN_HOSTNAMES = ["localhost", "0.0.0.0", "::", "0:0:0:0:0:0:0:0"]
// Global variable to hold the token.
let token;
@ -74,12 +77,13 @@ export const getToken = () => {
export const getEventURL = () => {
// Get backend URL object from the endpoint.
const endpoint = new URL(EVENTURL);
if (endpoint.hostname === "localhost") {
// If the backend URL references localhost, and the frontend is not on localhost,
// then use the frontend host.
if (SAME_DOMAIN_HOSTNAMES.includes(endpoint.hostname)) {
// Use the frontend domain to access the backend
const frontend_hostname = window.location.hostname;
if (frontend_hostname !== "localhost") {
endpoint.hostname = frontend_hostname;
endpoint.hostname = frontend_hostname;
if (window.location.protocol === "https:" && endpoint.protocol === "ws:") {
endpoint.protocol = "wss:";
endpoint.port = ""; // Assume websocket is on https port via load balancer.
}
}
return endpoint