util: fix isValidURL

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-03-25 16:01:10 +01:00
parent b0518231d0
commit 9b9ff70b75
2 changed files with 8 additions and 4 deletions

View File

@@ -198,9 +198,12 @@ describe('isValidUrl', () => {
test.each([
['https://github.com/docker/buildx.git', true],
['https://github.com/docker/buildx.git#refs/pull/648/head', true],
['git@github.com:moby/buildkit.git', false],
['git://github.com/user/repo.git', false],
['github.com/moby/buildkit.git#main', false],
['v0.4.1', false]
])('given %p', async (url, expected) => {
expect(Util.isValidUrl(url)).toEqual(expected);
expect(Util.isValidURL(url)).toEqual(expected);
});
});

View File

@@ -65,13 +65,14 @@ export class Util {
}
}
public static isValidUrl(url: string): boolean {
public static isValidURL(urlStr: string): boolean {
let url;
try {
new URL(url);
url = new URL(urlStr);
} catch (e) {
return false;
}
return true;
return url.protocol === 'http:' || url.protocol === 'https:';
}
public static async powershellCommand(script: string, params?: Record<string, string>) {