Skip to content

Commit b806090

Browse files
committed
chore: add support for sha256 checksums in target specification
1 parent d50c847 commit b806090

3 files changed

Lines changed: 42 additions & 6 deletions

File tree

src/parse.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { Either, error, isOk, ok } from "./either";
2-
import type { SemanticVersion, TargetRelease } from "./types";
2+
import { none, some } from "./option";
3+
import type { SemanticVersion, Sha256Hash, TargetRelease } from "./types";
34

45
const regexes = {
56
owner: /\S+/,
67
repository: /\S+/,
7-
majorSemanticVersion: /v(0|[1-9]\d*)/,
8-
majorMinorSemanticVersion: /v(0|[1-9]\d*)\.(0|[1-9]\d*)/,
8+
majorSemanticVersion: /v(?:0|[1-9]\d*)/,
9+
majorMinorSemanticVersion: /v(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)/,
910
exactSemanticVersion:
10-
/v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?/,
11+
/v(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?:[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?/,
12+
sha256Hash: /[a-fA-F0-9]{64}/,
1113
};
1214

1315
export function parseEnvironmentVariable(envVarName: string): Either<string> {
@@ -39,21 +41,24 @@ function parseTargetReleaseVersion(value: string): Either<TargetRelease> {
3941
majorSemanticVersion,
4042
majorMinorSemanticVersion,
4143
exactSemanticVersion,
44+
sha256Hash,
4245
} = regexes;
4346
const regex = new RegExp(
44-
`^(${owner.source})/(${repository.source})@(${majorSemanticVersion.source}|${majorMinorSemanticVersion.source}|${exactSemanticVersion.source})$`
47+
`^(${owner.source})/(${repository.source})@(${majorSemanticVersion.source}|${majorMinorSemanticVersion.source}|${exactSemanticVersion.source})(?::sha256-(${sha256Hash.source}))?$`
4548
);
4649
const match = value.match(regex);
4750
if (match === null) {
4851
// This error message is never used
4952
return error(["not a valid target release"]);
5053
}
54+
console.log(match);
5155
const target: TargetRelease = {
5256
slug: {
5357
owner: match[1] as string,
5458
repository: match[2] as string,
5559
},
5660
tag: match[3] as SemanticVersion,
61+
checksum: match[4] !== undefined ? some(match[4] as Sha256Hash) : none(),
5762
};
5863
return ok(target);
5964
}

src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { Option } from "./option";
2+
13
/**
24
* The following formats are valid:
35
* - v1
@@ -22,6 +24,7 @@ export function isExactSemanticVersion(
2224
}
2325

2426
export type Sha1Hash = string & { readonly __tag: unique symbol };
27+
export type Sha256Hash = string & { readonly __tag: unique symbol };
2528

2629
export type RepositorySlug = {
2730
owner: string;
@@ -33,4 +36,5 @@ export type TargetTriple = string & { readonly __tag: unique symbol };
3336
export type TargetRelease = {
3437
slug: RepositorySlug;
3538
tag: SemanticVersion;
39+
checksum: Option<Sha256Hash>;
3640
};

test/test-parse-target-release.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import assert from "node:assert/strict";
33

44
import { parseTargetReleases } from "../src/parse";
55
import { Either, error, isErr, isOk, ok } from "../src/either";
6-
import type { SemanticVersion, TargetRelease } from "../src/types";
6+
import type { SemanticVersion, Sha256Hash, TargetRelease } from "../src/types";
7+
import { none, some } from "../src/option";
78

89
function err<A>(): Either<A> {
910
return error([""]);
@@ -45,6 +46,26 @@ test(
4546
repository: "bar",
4647
},
4748
tag: "v1" as SemanticVersion,
49+
checksum: none(),
50+
},
51+
])
52+
)
53+
);
54+
55+
test(
56+
"should parse a slug and version and regex",
57+
check(
58+
"foo/bar@v1:sha256-8a4600be96d2ec013209042458ce97a9652fcc46c1c855d0217aa42e330fc06e",
59+
ok([
60+
{
61+
slug: {
62+
owner: "foo",
63+
repository: "bar",
64+
},
65+
tag: "v1" as SemanticVersion,
66+
checksum: some(
67+
"8a4600be96d2ec013209042458ce97a9652fcc46c1c855d0217aa42e330fc06e" as Sha256Hash
68+
),
4869
},
4970
])
5071
)
@@ -61,13 +82,15 @@ test(
6182
repository: "bar",
6283
},
6384
tag: "v1" as SemanticVersion,
85+
checksum: none(),
6486
},
6587
{
6688
slug: {
6789
owner: "qux",
6890
repository: "baz",
6991
},
7092
tag: "v2.3.4" as SemanticVersion,
93+
checksum: none(),
7194
},
7295
])
7396
)
@@ -85,13 +108,15 @@ test(
85108
repository: "bar",
86109
},
87110
tag: "v1" as SemanticVersion,
111+
checksum: none(),
88112
},
89113
{
90114
slug: {
91115
owner: "qux",
92116
repository: "baz",
93117
},
94118
tag: "v2.3.4" as SemanticVersion,
119+
checksum: none(),
95120
},
96121
])
97122
)
@@ -110,13 +135,15 @@ test(
110135
repository: "bar",
111136
},
112137
tag: "v1" as SemanticVersion,
138+
checksum: none(),
113139
},
114140
{
115141
slug: {
116142
owner: "qux",
117143
repository: "baz",
118144
},
119145
tag: "v2.3.4" as SemanticVersion,
146+
checksum: none(),
120147
},
121148
])
122149
)

0 commit comments

Comments
 (0)