Skip to content

Commit

Permalink
fix: remove double regex use
Browse files Browse the repository at this point in the history
  • Loading branch information
kosmoz committed Mar 4, 2024
1 parent 0e29a94 commit 1e3b76b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/utils/url-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as assert from 'node:assert';

import {parseArtifactHubReferenceUrl, parseManifestUrl} from './url-parser.js';

describe('utils/manifest/parse', () => {
describe('utils/manifest/parseManifestUrl', () => {
it('should parse version from GitHub release manifest url', () => {
const manifest = parseManifestUrl(
'https://raw.githubusercontent.com/username/repository/v0.0.1-alpha.10/path/to/file.yaml',
Expand All @@ -21,6 +21,11 @@ describe('utils/manifest/parse', () => {
assert.equal(manifest.semVer.raw, 'v0.0.1-alpha.10');
assert.equal(manifest.path, '/install/cyclops-install.yaml');
});
it('should throw for invalid manifest url', () => {
assert.throws(() => {
parseManifestUrl('https://raw.githubusercontent.com/kosmoz/malicious/not-a-package/run.exe');
});
});
});

describe('utils/manifest/parseArtifactHubReference', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/utils/url-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const MANIFEST_REGEX =
/^https:\/\/raw.githubusercontent.com\/(?<owner>[\da-z-]+)\/(?<repo>[\da-z-]+)\/(?<version>[\d.a-z-]+)(?<path>\/.*)/;

export function parseManifestUrl(manifestUrl: string): ManifestUrl {
if (MANIFEST_REGEX.test(manifestUrl)) {
const group = MANIFEST_REGEX.exec(manifestUrl)?.groups ?? {};
const group = MANIFEST_REGEX.exec(manifestUrl)?.groups;
if (group !== undefined) {
return new ManifestUrl(manifestUrl, group.owner, group.repo, group.version, group.path);
}

Expand All @@ -16,8 +16,8 @@ export function parseManifestUrl(manifestUrl: string): ManifestUrl {
const ARTIFACT_HUB_URL_REGEX = /^https:\/\/artifacthub.io\/packages\/helm\/(?<owner>[\da-z-]+)\/(?<repo>[\da-z-]+)/;

export function parseArtifactHubReferenceUrl(referenceUrl: string): ArtifactHubReference {
if (ARTIFACT_HUB_URL_REGEX.test(referenceUrl)) {
const group = ARTIFACT_HUB_URL_REGEX.exec(referenceUrl)?.groups ?? {};
const group = ARTIFACT_HUB_URL_REGEX.exec(referenceUrl)?.groups;
if (group !== undefined) {
return new ArtifactHubReference(referenceUrl, group.owner, group.repo);
}

Expand Down

0 comments on commit 1e3b76b

Please sign in to comment.