diff --git a/components/dashboard/src/service/service.tsx b/components/dashboard/src/service/service.tsx index f12cb434649cd1..21ebd2b9face4d 100644 --- a/components/dashboard/src/service/service.tsx +++ b/components/dashboard/src/service/service.tsx @@ -124,7 +124,7 @@ export class IDEFrontendService implements IDEFrontendDashboardService.IServer { // send last heartbeat (wasClosed: true) const data = { sessionId: this.sessionId }; const blob = new Blob([JSON.stringify(data)], { type: "application/json" }); - const gitpodHostUrl = new GitpodHostUrl(new URL(window.location.toString())); + const gitpodHostUrl = new GitpodHostUrl(window.location.toString()); const url = gitpodHostUrl.withApi({ pathname: `/auth/workspacePageClose/${this.instanceID}` }).toString(); navigator.sendBeacon(url, blob); }); diff --git a/components/gitpod-protocol/src/util/generate-workspace-id.spec.ts b/components/gitpod-protocol/src/util/generate-workspace-id.spec.ts index 131603f2a364f9..fc68e17b065d62 100644 --- a/components/gitpod-protocol/src/util/generate-workspace-id.spec.ts +++ b/components/gitpod-protocol/src/util/generate-workspace-id.spec.ts @@ -16,7 +16,7 @@ class TestGenerateWorkspaceId { @test public async testGenerateWorkspaceId() { for (let i = 0; i < 10; i++) { const id = await generateWorkspaceID(); - expect(new GitpodHostUrl().withWorkspacePrefix(id, "eu").workspaceId).to.equal(id); + expect(new GitpodHostUrl("https://gitpod.io").withWorkspacePrefix(id, "eu").workspaceId).to.equal(id); } } @@ -50,7 +50,7 @@ class TestGenerateWorkspaceId { for (const d of data) { const id = await generateWorkspaceID(d[0], d[1]); expect(id).match(new RegExp("^" + d[2])); - expect(new GitpodHostUrl().withWorkspacePrefix(id, "eu").workspaceId).to.equal(id); + expect(new GitpodHostUrl("https://gitpod.io").withWorkspacePrefix(id, "eu").workspaceId).to.equal(id); expect(id.length <= 36, `"${id}" is longer than 36 chars (${id.length})`).to.be.true; } } diff --git a/components/gitpod-protocol/src/util/gitpod-host-url.spec.ts b/components/gitpod-protocol/src/util/gitpod-host-url.spec.ts index b60df49a90da1d..d48bbbf82e1452 100644 --- a/components/gitpod-protocol/src/util/gitpod-host-url.spec.ts +++ b/components/gitpod-protocol/src/util/gitpod-host-url.spec.ts @@ -11,15 +11,8 @@ const expect = chai.expect; @suite export class GitpodHostUrlTest { - @test public parseWorkspaceId_pathBased() { - const actual = GitpodHostUrl.fromWorkspaceUrl( - "http://35.223.201.195/workspace/bc77e03d-c781-4235-bca0-e24087f5e472/", - ).workspaceId; - expect(actual).to.equal("bc77e03d-c781-4235-bca0-e24087f5e472"); - } - @test public parseWorkspaceId_hosts_withEnvVarsInjected() { - const actual = GitpodHostUrl.fromWorkspaceUrl( + const actual = new GitpodHostUrl( "https://gray-grasshopper-nfbitfia.ws-eu02.gitpod-staging.com/#passedin=test%20value/https://github.com/gitpod-io/gitpod-test-repo", ).workspaceId; expect(actual).to.equal("gray-grasshopper-nfbitfia"); @@ -27,21 +20,21 @@ export class GitpodHostUrlTest { @test public async testWithoutWorkspacePrefix() { expect( - GitpodHostUrl.fromWorkspaceUrl("https://3000-moccasin-ferret-155799b3.ws-eu02.gitpod-staging.com/") + new GitpodHostUrl("https://3000-moccasin-ferret-155799b3.ws-eu02.gitpod-staging.com/") .withoutWorkspacePrefix() .toString(), ).to.equal("https://gitpod-staging.com/"); } @test public async testWithoutWorkspacePrefix2() { - expect( - GitpodHostUrl.fromWorkspaceUrl("https://gitpod-staging.com/").withoutWorkspacePrefix().toString(), - ).to.equal("https://gitpod-staging.com/"); + expect(new GitpodHostUrl("https://gitpod-staging.com/").withoutWorkspacePrefix().toString()).to.equal( + "https://gitpod-staging.com/", + ); } @test public async testWithoutWorkspacePrefix3() { expect( - GitpodHostUrl.fromWorkspaceUrl("https://gray-rook-5523v5d8.ws-dev.my-branch-1234.staging.gitpod-dev.com/") + new GitpodHostUrl("https://gray-rook-5523v5d8.ws-dev.my-branch-1234.staging.gitpod-dev.com/") .withoutWorkspacePrefix() .toString(), ).to.equal("https://my-branch-1234.staging.gitpod-dev.com/"); @@ -49,15 +42,13 @@ export class GitpodHostUrlTest { @test public async testWithoutWorkspacePrefix4() { expect( - GitpodHostUrl.fromWorkspaceUrl("https://my-branch-1234.staging.gitpod-dev.com/") - .withoutWorkspacePrefix() - .toString(), + new GitpodHostUrl("https://my-branch-1234.staging.gitpod-dev.com/").withoutWorkspacePrefix().toString(), ).to.equal("https://my-branch-1234.staging.gitpod-dev.com/"); } @test public async testWithoutWorkspacePrefix5() { expect( - GitpodHostUrl.fromWorkspaceUrl("https://abc-nice-brunch-4224.staging.gitpod-dev.com/") + new GitpodHostUrl("https://abc-nice-brunch-4224.staging.gitpod-dev.com/") .withoutWorkspacePrefix() .toString(), ).to.equal("https://abc-nice-brunch-4224.staging.gitpod-dev.com/"); @@ -65,9 +56,7 @@ export class GitpodHostUrlTest { @test public async testWithoutWorkspacePrefix6() { expect( - GitpodHostUrl.fromWorkspaceUrl( - "https://gray-rook-5523v5d8.ws-dev.abc-nice-brunch-4224.staging.gitpod-dev.com/", - ) + new GitpodHostUrl("https://gray-rook-5523v5d8.ws-dev.abc-nice-brunch-4224.staging.gitpod-dev.com/") .withoutWorkspacePrefix() .toString(), ).to.equal("https://abc-nice-brunch-4224.staging.gitpod-dev.com/"); diff --git a/components/gitpod-protocol/src/util/gitpod-host-url.ts b/components/gitpod-protocol/src/util/gitpod-host-url.ts index 28129f42837870..09f3160df01026 100644 --- a/components/gitpod-protocol/src/util/gitpod-host-url.ts +++ b/components/gitpod-protocol/src/util/gitpod-host-url.ts @@ -24,23 +24,22 @@ const workspaceUrlPrefixRegex = RegExp(`^(([0-9]{4,6}|debug)-)?${baseWorkspaceID export class GitpodHostUrl { readonly url: URL; - constructor(urlParam?: string | URL) { - if (urlParam === undefined || typeof urlParam === "string") { - this.url = new URL(urlParam || "https://gitpod.io"); + constructor(url: string) { + const urlParam = url as any; + if (typeof urlParam === "string") { + // public constructor + this.url = new URL(url); this.url.search = ""; this.url.hash = ""; this.url.pathname = ""; } else if (urlParam instanceof URL) { + // internal constructor, see with this.url = urlParam; } else { log.error("Unexpected urlParam", { urlParam }); } } - static fromWorkspaceUrl(url: string) { - return new GitpodHostUrl(new URL(url)); - } - withWorkspacePrefix(workspaceId: string, region: string) { return this.withDomainPrefix(`${workspaceId}.ws-${region}.`); } diff --git a/components/server/src/bitbucket-server/bitbucket-server-api.spec.ts b/components/server/src/bitbucket-server/bitbucket-server-api.spec.ts index fca5b9c611ed47..b8781b0908ddb0 100644 --- a/components/server/src/bitbucket-server/bitbucket-server-api.spec.ts +++ b/components/server/src/bitbucket-server/bitbucket-server-api.spec.ts @@ -44,7 +44,7 @@ class TestBitbucketServerApi { createGitpodToken: async () => ({ token: { value: "foobar123-token" } }), } as any); bind(Config).toConstantValue({ - hostUrl: new GitpodHostUrl(), + hostUrl: new GitpodHostUrl("https://gitpod.io"), }); bind(TokenProvider).toConstantValue({ getTokenForHost: async () => { diff --git a/components/server/src/bitbucket-server/bitbucket-server-context-parser.spec.ts b/components/server/src/bitbucket-server/bitbucket-server-context-parser.spec.ts index 776f8d4f485b92..9a2834869363de 100644 --- a/components/server/src/bitbucket-server/bitbucket-server-context-parser.spec.ts +++ b/components/server/src/bitbucket-server/bitbucket-server-context-parser.spec.ts @@ -44,7 +44,7 @@ class TestBitbucketServerContextParser { createGitpodToken: async () => ({ token: { value: "foobar123-token" } }), } as any); bind(Config).toConstantValue({ - hostUrl: new GitpodHostUrl(), + hostUrl: new GitpodHostUrl("https://gitpod.io"), }); bind(TokenProvider).toConstantValue({ getTokenForHost: async () => { diff --git a/components/server/src/bitbucket-server/bitbucket-server-file-provider.spec.ts b/components/server/src/bitbucket-server/bitbucket-server-file-provider.spec.ts index e5063236975b61..20a7ac6d6fd6ff 100644 --- a/components/server/src/bitbucket-server/bitbucket-server-file-provider.spec.ts +++ b/components/server/src/bitbucket-server/bitbucket-server-file-provider.spec.ts @@ -54,7 +54,7 @@ class TestBitbucketServerFileProvider { createGitpodToken: async () => ({ token: { value: "foobar123-token" } }), } as any); bind(Config).toConstantValue({ - hostUrl: new GitpodHostUrl(), + hostUrl: new GitpodHostUrl("https://gitpod.io"), }); bind(TokenProvider).toConstantValue({ getTokenForHost: async () => { diff --git a/components/server/src/bitbucket-server/bitbucket-server-repository-provider.spec.ts b/components/server/src/bitbucket-server/bitbucket-server-repository-provider.spec.ts index 3cb5ab00f51032..3e2a1c1b67331a 100644 --- a/components/server/src/bitbucket-server/bitbucket-server-repository-provider.spec.ts +++ b/components/server/src/bitbucket-server/bitbucket-server-repository-provider.spec.ts @@ -54,7 +54,7 @@ class TestBitbucketServerRepositoryProvider { createGitpodToken: async () => ({ token: { value: "foobar123-token" } }), } as any); bind(Config).toConstantValue({ - hostUrl: new GitpodHostUrl(), + hostUrl: new GitpodHostUrl("https://gitpod.io"), }); bind(TokenProvider).toConstantValue({ getTokenForHost: async () => { diff --git a/components/server/src/prebuilds/bitbucket-server-service.spec.ts b/components/server/src/prebuilds/bitbucket-server-service.spec.ts index 3e1b70c36ca36f..dab98d04ac3bb9 100644 --- a/components/server/src/prebuilds/bitbucket-server-service.spec.ts +++ b/components/server/src/prebuilds/bitbucket-server-service.spec.ts @@ -54,7 +54,7 @@ class TestBitbucketServerService { createGitpodToken: async () => ({ token: { value: "foobar123-token" } }), } as any); bind(Config).toConstantValue({ - hostUrl: new GitpodHostUrl(), + hostUrl: new GitpodHostUrl("https://gitpod.io"), }); bind(TokenProvider).toConstantValue({ getTokenForHost: async () => { diff --git a/components/supervisor/frontend/src/ide/supervisor-service-client.ts b/components/supervisor/frontend/src/ide/supervisor-service-client.ts index c17afdc6178e21..60d2aaf4f41098 100644 --- a/components/supervisor/frontend/src/ide/supervisor-service-client.ts +++ b/components/supervisor/frontend/src/ide/supervisor-service-client.ts @@ -10,7 +10,7 @@ import { ContentStatusResponse, } from "@gitpod/supervisor-api-grpc/lib/status_pb"; import { WorkspaceInfoResponse } from "@gitpod/supervisor-api-grpc/lib/info_pb"; -import { GitpodHostUrl } from "@gitpod/gitpod-protocol/lib/util/gitpod-host-url"; +import { workspaceUrl } from "../shared/urls"; export class SupervisorServiceClient { private static _instance: SupervisorServiceClient | undefined; @@ -41,7 +41,7 @@ export class SupervisorServiceClient { await new Promise((resolve) => setTimeout(resolve, 1000)); } try { - const wsSupervisorStatusUrl = GitpodHostUrl.fromWorkspaceUrl(window.location.href).with((url) => { + const wsSupervisorStatusUrl = workspaceUrl.with(() => { return { pathname: "/_supervisor/v1/status/supervisor/willShutdown/true", }; @@ -85,7 +85,7 @@ export class SupervisorServiceClient { wait = ""; } try { - const wsSupervisorStatusUrl = GitpodHostUrl.fromWorkspaceUrl(window.location.href).with((url) => { + const wsSupervisorStatusUrl = workspaceUrl.with(() => { return { pathname: "/_supervisor/v1/status/" + kind + wait, }; @@ -121,7 +121,7 @@ export class SupervisorServiceClient { await new Promise((resolve) => setTimeout(resolve, 1000)); } try { - const getWorkspaceInfoUrl = GitpodHostUrl.fromWorkspaceUrl(window.location.href).with((url) => { + const getWorkspaceInfoUrl = workspaceUrl.with(() => { return { pathname: "_supervisor/v1/info/workspace", }; diff --git a/components/supervisor/frontend/src/shared/urls.ts b/components/supervisor/frontend/src/shared/urls.ts index 29da389fb61f65..07d9ff8f10043c 100644 --- a/components/supervisor/frontend/src/shared/urls.ts +++ b/components/supervisor/frontend/src/shared/urls.ts @@ -6,7 +6,7 @@ import { GitpodHostUrl } from "@gitpod/gitpod-protocol/lib/util/gitpod-host-url"; -export const workspaceUrl = GitpodHostUrl.fromWorkspaceUrl(window.location.href); +export const workspaceUrl = new GitpodHostUrl(window.location.href); export const serverUrl = workspaceUrl.withoutWorkspacePrefix();