From 486083ddba862feec102deac09fefdd601e69583 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Fri, 12 Feb 2021 10:30:20 -0500 Subject: [PATCH] return undefined instead of localhost from getClusterIdFromHost Signed-off-by: Sebastian Malton --- src/common/__tests__/cluster-store.test.ts | 12 +++++++++++- src/common/cluster-store.ts | 13 +++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/common/__tests__/cluster-store.test.ts b/src/common/__tests__/cluster-store.test.ts index fcbd5ebd6b..72073fef6e 100644 --- a/src/common/__tests__/cluster-store.test.ts +++ b/src/common/__tests__/cluster-store.test.ts @@ -2,7 +2,7 @@ import fs from "fs"; import mockFs from "mock-fs"; import yaml from "js-yaml"; import { Cluster } from "../../main/cluster"; -import { ClusterStore } from "../cluster-store"; +import { ClusterStore, getClusterIdFromHost } from "../cluster-store"; import { workspaceStore } from "../workspace-store"; const testDataIcon = fs.readFileSync("test-data/cluster-store-migration-icon.png"); @@ -446,3 +446,13 @@ describe("pre 3.6.0-beta.1 config with an existing cluster", () => { expect(icon.startsWith("data:;base64,")).toBe(true); }); }); + +describe("getClusterIdFromHost", () => { + it("should return undefined for non cluster frame hosts", () => { + expect(getClusterIdFromHost("localhost:45345")).toBeUndefined(); + }); + + it("should return undefined for non cluster frame hosts", () => { + expect(getClusterIdFromHost("fe540901-0bd6-4f6c-b472-bce1559d7c4a.localhost:59110")).toBe("fe540901-0bd6-4f6c-b472-bce1559d7c4a"); + }); +}); diff --git a/src/common/cluster-store.ts b/src/common/cluster-store.ts index d8bd28f1e8..e80684d54c 100644 --- a/src/common/cluster-store.ts +++ b/src/common/cluster-store.ts @@ -354,10 +354,15 @@ export class ClusterStore extends BaseStore { export const clusterStore = ClusterStore.getInstance(); -export function getClusterIdFromHost(hostname: string): ClusterId { - const subDomains = hostname.split(":")[0].split("."); +export function getClusterIdFromHost(host: string): ClusterId | undefined { + // e.g host == "%clusterId.localhost:45345" + const subDomains = host.split(":")[0].split("."); - return subDomains.slice(-2)[0]; // e.g host == "%clusterId.localhost:45345" + if (subDomains.length >= 2) { + return subDomains[0]; + } + + return undefined; } export function getClusterFrameUrl(clusterId: ClusterId) { @@ -365,7 +370,7 @@ export function getClusterFrameUrl(clusterId: ClusterId) { } export function getHostedClusterId() { - return getClusterIdFromHost(location.hostname); + return getClusterIdFromHost(location.host); } export function getHostedCluster(): Cluster {