Path: blob/main/components/gitpod-protocol/src/context-url.ts
2498 views
/**1* Copyright (c) 2020 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { Workspace } from ".";78/**9* The whole point of these methods is to overcome inconsistencies in our data model.10* Ideally we remove it at some point once we fixed our model, as it:11* - duplicates logic12* - but additional burden on clients (using this, copying this to other languages!)13*14* TODO(gpl) See if we can get this into `server` code to remove the burden from clients15*/16export namespace ContextURL {17export const IMAGEBUILD_PREFIX = "imagebuild";18export const SNAPSHOT_PREFIX = "snapshot";19export const REFERRER_PREFIX = "referrer:";2021/**22* This function will (try to) return the HTTP(S) URL of the context the user originally created this workspace on.23* Especially it will not contain any modifiers or be of different scheme than HTTP(S).24*25* Use this function if you need to provided a _working_ URL to the original context.26* @param ws27* @returns28*/29export function getNormalizedURL(ws: Pick<Workspace, "context" | "contextURL"> | undefined): URL | undefined {30const normalized = normalize(ws);31if (!normalized) {32return undefined;33}3435try {36return new URL(normalized);37} catch (err) {38console.debug(`unable to parse URL from normalized contextURL: '${normalized}'`);39}40return undefined;41}4243export function normalize(ws: Pick<Workspace, "context" | "contextURL"> | undefined): string | undefined {44if (!ws) {45return undefined;46}47if (ws.context.normalizedContextURL) {48return ws.context.normalizedContextURL;49}5051// fallback: we do not yet set normalizedContextURL on all workspaces, yet, let alone older existing workspaces52let fallback: string | undefined = undefined;53try {54fallback = removePrefixes(ws.contextURL);55} catch (err) {56console.error(`unable to remove prefixes from contextURL: '${ws.contextURL}'`, err);57}58return fallback;59}6061/**62* The field "contextUrl" might contain prefixes like:63* - envvar1=value1/...64* - prebuild/...65* This is the analogon to the (Prefix)ContextParser structure in "server".66*/67function removePrefixes(contextUrl: string | undefined): string | undefined {68if (contextUrl === undefined) {69return undefined;70}7172const segments = contextUrl.split("/");73if (segments.length === 1) {74return segments[0]; // this might be something, we just try75}7677const segmentsToURL = (offset: number): string => {78let rest = segments.slice(offset).join("/");79if (/^git@[^:\/]+:/.test(rest)) {80rest = rest.replace(/^git@([^:\/]+):/, "https://$1/");81}82if (!rest.startsWith("http")) {83rest = "https://" + rest;84}85return rest;86};8788const firstSegment = segments[0];89if (90firstSegment === IMAGEBUILD_PREFIX ||91firstSegment === SNAPSHOT_PREFIX ||92firstSegment.startsWith(REFERRER_PREFIX)93) {94return segmentsToURL(1);95}9697// check for env vars98if (firstSegment.indexOf("=") !== -1) {99return segmentsToURL(1);100}101102return segmentsToURL(0);103}104}105106107