@mark1russell7/package-id 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -2
- package/build/index.d.ts +102 -2
- package/build/index.d.ts.map +1 -1
- package/build/index.js +474 -3
- package/build/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +15 -2
- package/src/osv.ts +64 -2
- package/src/vers.ts +135 -0
- package/src/version.ts +353 -0
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# @mark1russell7/package-id
|
|
2
2
|
|
|
3
|
-
Package identities as plain functions with no fetch. The
|
|
3
|
+
Package identities as plain functions with no fetch. The parts are the package URL (purl) of the [purl specification](https://github.com/package-url/purl-spec), the version order of each ecosystem and the version ranges of the [vers specification](https://github.com/package-url/vers-spec).
|
|
4
4
|
|
|
5
5
|
```ts
|
|
6
|
-
import { formatPurl, osvPurl, packagePurlOf, parsePurl } from "@mark1russell7/package-id";
|
|
6
|
+
import { compareVersions, formatPurl, osvPurl, packagePurlOf, parsePurl, parseVers, versContains } from "@mark1russell7/package-id";
|
|
7
7
|
|
|
8
8
|
parsePurl("pkg:npm/%40angular/animation@12.3.1");
|
|
9
9
|
// { type: "npm", namespace: "@angular", name: "animation", version: "12.3.1" }
|
|
@@ -16,6 +16,12 @@ packagePurlOf("pkg:npm/a@1.0.0?arch=x#sub");
|
|
|
16
16
|
|
|
17
17
|
osvPurl("Maven", "org.example:core");
|
|
18
18
|
// "pkg:maven/org.example/core"
|
|
19
|
+
|
|
20
|
+
compareVersions("deb", "1.0~rc1", "1.0");
|
|
21
|
+
// -1
|
|
22
|
+
|
|
23
|
+
versContains(parseVers("vers:npm/>=1.0.0|<1.2.3")!, "1.1.0");
|
|
24
|
+
// true
|
|
19
25
|
```
|
|
20
26
|
|
|
21
27
|
## The functions
|
|
@@ -29,6 +35,14 @@ osvPurl("Maven", "org.example:core");
|
|
|
29
35
|
| `osvPurl(ecosystem, name)` | The purl of a package of an OSV ecosystem, with no version. |
|
|
30
36
|
| `osvPackagePurl(pkg)` | The purl of an OSV record package. A valid purl of the record wins. |
|
|
31
37
|
| `OSV_PURL_TYPES` | The purl type of each OSV ecosystem that has one. |
|
|
38
|
+
| `compareVersions(scheme, a, b)` | Below 0, 0 or above 0 for the order of two versions, or `undefined` for a text that is not a version. |
|
|
39
|
+
| `compareSemver`, `compareGo`, `comparePep440`, `compareDebian`, `compareRpm` | The order of one scheme: SemVer 2.0.0, Go modules, PEP 440, dpkg and `rpmvercmp`. |
|
|
40
|
+
| `compareIntdot`, `compareGeneric` | The order of the `vers` schemes `intdot` (integers with dots) and `generic` (the versions of vendors, for example `V6.80`). |
|
|
41
|
+
| `versionScheme(type)` | The version scheme of a purl type, or `undefined` when the order of the type is not known. |
|
|
42
|
+
| `parseVers(text)` | The scheme and the constraints of a `vers` range, or `undefined` for a text that is not a range. |
|
|
43
|
+
| `formatVers(vers)` | The `vers` text of a range, with the constraints in the order of their versions. |
|
|
44
|
+
| `versContains(vers, version)` | True when the version is in the range. The result is `undefined` when the order is not known. |
|
|
45
|
+
| `versEnd(vers, version)` | The end `<` of the part of the range that holds the version: for an advisory, the fixed version. |
|
|
32
46
|
|
|
33
47
|
## The canonical form
|
|
34
48
|
|
|
@@ -39,6 +53,10 @@ osvPurl("Maven", "org.example:core");
|
|
|
39
53
|
- The subpath has no empty, `.` or `..` segment.
|
|
40
54
|
- Each part is percent-encoded, but a colon stays as it is. A qualifier value keeps its `/`.
|
|
41
55
|
|
|
56
|
+
## An unknown order
|
|
57
|
+
|
|
58
|
+
A comparison does not guess. A version that is not a version of its scheme gives `undefined`. A scheme with no known order also gives `undefined`, for example Maven. The `generic` order also gives `undefined` for two different runs of letters, for example `2.9.1.0_FR` and `2.9.1.0_ESP`. Thus a caller that matches advisories to installed versions does not take an unknown result as a match or as no match.
|
|
59
|
+
|
|
42
60
|
## License
|
|
43
61
|
|
|
44
62
|
MIT
|
package/build/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Package identities: package URLs (purl)
|
|
2
|
+
* Package identities: package URLs (purl), the purl of each OSV ecosystem, the version order of each ecosystem and version ranges (vers). The functions are plain functions with
|
|
3
3
|
* no fetch.
|
|
4
4
|
*/
|
|
5
5
|
/**
|
|
@@ -44,6 +44,40 @@ export declare function formatPurl(purl: Purl): string | undefined;
|
|
|
44
44
|
* names a package with this text names each version of it. It gives `undefined` when the text is not a valid purl.
|
|
45
45
|
*/
|
|
46
46
|
export declare function packagePurlOf(text: string): string | undefined;
|
|
47
|
+
/** The comparators of a `vers` constraint. */
|
|
48
|
+
type VersComparator = "=" | "!=" | "<" | "<=" | ">" | ">=";
|
|
49
|
+
/** One constraint of a range: a comparator and a version. */
|
|
50
|
+
interface VersConstraint {
|
|
51
|
+
readonly comparator: VersComparator;
|
|
52
|
+
readonly version: string;
|
|
53
|
+
}
|
|
54
|
+
/** A version range. A range with the constraint `*` holds each version, and its list of constraints is empty. */
|
|
55
|
+
interface Vers {
|
|
56
|
+
/** The version scheme, for example `npm`, `pypi` or `deb`. */
|
|
57
|
+
readonly scheme: string;
|
|
58
|
+
/** True: the range is `*`, so it holds each version. */
|
|
59
|
+
readonly all: boolean;
|
|
60
|
+
readonly constraints: readonly VersConstraint[];
|
|
61
|
+
}
|
|
62
|
+
/** This function reads a `vers` text. A text that is not a range gives `undefined`. */
|
|
63
|
+
export declare function parseVers(text: string): Vers | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* This function gives the `vers` text of a range. The constraints come in the order of the versions, when the scheme
|
|
66
|
+
* has a known order. The comparator `=` has no sign, as the specification tells.
|
|
67
|
+
*/
|
|
68
|
+
export declare function formatVers(vers: Vers): string;
|
|
69
|
+
/**
|
|
70
|
+
* This function tells if a version is in a range, with the algorithm of the `vers` specification. The result is
|
|
71
|
+
* `undefined` when the scheme has no known order, or when a version is not a version of the scheme. Thus a caller
|
|
72
|
+
* does not take an unknown result as a match or as no match.
|
|
73
|
+
*/
|
|
74
|
+
export declare function versContains(vers: Vers, version: string): boolean | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* This function gives the end of the part of a range that holds a version, when that end is a constraint `<`. For the
|
|
77
|
+
* range of an advisory, it is the first fixed version above the version. The result is `undefined` for a version out
|
|
78
|
+
* of the range, or for an unknown order. It is also `undefined` for a part with no such end: `<=`, an open end or `=`.
|
|
79
|
+
*/
|
|
80
|
+
export declare function versEnd(vers: Vers, version: string): string | undefined;
|
|
47
81
|
/**
|
|
48
82
|
* The purl type of each OSV ecosystem that has one. An ecosystem that is not in the table has no purl.
|
|
49
83
|
* For a Linux distribution, the table key is the part of the ecosystem before the first `:`.
|
|
@@ -63,5 +97,71 @@ export declare function osvPackagePurl(pkg: {
|
|
|
63
97
|
readonly name: string;
|
|
64
98
|
readonly purl?: string;
|
|
65
99
|
}): string | undefined;
|
|
66
|
-
|
|
100
|
+
/** One event of an OSV range, as the OSV schema writes it: each event has one of the four fields. */
|
|
101
|
+
interface OsvEvent {
|
|
102
|
+
readonly introduced?: string;
|
|
103
|
+
readonly fixed?: string;
|
|
104
|
+
readonly last_affected?: string;
|
|
105
|
+
readonly limit?: string;
|
|
106
|
+
}
|
|
107
|
+
/** A range of an OSV record: its type (`SEMVER`, `ECOSYSTEM` or `GIT`) and its events. */
|
|
108
|
+
interface OsvRange {
|
|
109
|
+
readonly type: string;
|
|
110
|
+
readonly events: readonly OsvEvent[];
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* This function gives the `vers` range of an OSV range. A range of the type `SEMVER` has the scheme `semver`. A range
|
|
114
|
+
* of the type `ECOSYSTEM` has the purl type of its ecosystem as its scheme. A `GIT` range, an ecosystem with no purl type
|
|
115
|
+
* or an event with no version gives `undefined`.
|
|
116
|
+
*
|
|
117
|
+
* The events come in the order of their versions, as the OSV schema tells, when the scheme has a known order. Else they
|
|
118
|
+
* keep the order of the record. The event `introduced 0` is the start of each version, so it gives no constraint.
|
|
119
|
+
*/
|
|
120
|
+
export declare function osvRangeVers(ecosystem: string, range: OsvRange): Vers | undefined;
|
|
121
|
+
/**
|
|
122
|
+
* The version order of each ecosystem: SemVer, Go modules, PEP 440, Debian and RPM. The `vers` schemes `intdot` and
|
|
123
|
+
* `generic` have an order too. Each function compares two versions of one scheme. A text that is not a version of the
|
|
124
|
+
* scheme gives `undefined`, not a guess.
|
|
125
|
+
*/
|
|
126
|
+
/** The version schemes that this module compares. */
|
|
127
|
+
type VersionScheme = "semver" | "golang" | "pypi" | "deb" | "rpm" | "intdot" | "generic";
|
|
128
|
+
/** This function gives the version scheme of a purl type, or `undefined` for a type whose order this module does not know. */
|
|
129
|
+
export declare function versionScheme(type: string): VersionScheme | undefined;
|
|
130
|
+
/**
|
|
131
|
+
* This function compares two versions of one scheme. The result is below 0 when `a` comes before `b`, 0 when they are
|
|
132
|
+
* equal and above 0 when `a` comes after `b`. When a text is not a version of the scheme, the result is `undefined`.
|
|
133
|
+
*/
|
|
134
|
+
export declare function compareVersions(scheme: string, a: string, b: string): number | undefined;
|
|
135
|
+
/** This function compares two SemVer 2.0.0 versions. It accepts a leading `v` or `=`, as npm does. */
|
|
136
|
+
export declare function compareSemver(a: string, b: string): number | undefined;
|
|
137
|
+
/**
|
|
138
|
+
* This function compares two versions of Go modules. A Go version starts with `v` and follows SemVer. A pseudo-version is
|
|
139
|
+
* a pre-release, so it has its place in the order of SemVer. The suffix `+incompatible` is build metadata.
|
|
140
|
+
*/
|
|
141
|
+
export declare function compareGo(a: string, b: string): number | undefined;
|
|
142
|
+
/** This function compares two PEP 440 versions, with the order of the Python packaging specification. */
|
|
143
|
+
export declare function comparePep440(a: string, b: string): number | undefined;
|
|
144
|
+
/** This function compares two Debian versions (`[epoch:]upstream[-revision]`) with the order of dpkg. */
|
|
145
|
+
export declare function compareDebian(a: string, b: string): number | undefined;
|
|
146
|
+
/** This function compares two RPM versions (`[epoch:]version[-release]`) with the order of `rpmvercmp`. */
|
|
147
|
+
export declare function compareRpm(a: string, b: string): number | undefined;
|
|
148
|
+
/**
|
|
149
|
+
* This function compares two versions of the `vers` scheme `intdot`: integers with a `.` between them. The integers
|
|
150
|
+
* compare from left to right. A missing integer is 0, so `1.0` equals `1.0.0`.
|
|
151
|
+
*/
|
|
152
|
+
export declare function compareIntdot(a: string, b: string): number | undefined;
|
|
153
|
+
/**
|
|
154
|
+
* This function compares two versions of the `vers` scheme `generic`, the versions of vendors, for example `V6.80` or
|
|
155
|
+
* `2.9.1.0_FR`. A version starts with a digit, after one optional `v`. The segments are the runs of digits and of letters.
|
|
156
|
+
* The other characters separate the segments.
|
|
157
|
+
*
|
|
158
|
+
* The segments compare from left to right, and the first difference decides. Two runs of digits compare as integers.
|
|
159
|
+
* Two runs of letters are equal when they are the same with no regard to case. Other pairs have no known order. These
|
|
160
|
+
* pairs give `undefined`: two different runs of letters, or a run of digits and a run of letters.
|
|
161
|
+
*
|
|
162
|
+
* When one version ends first, a rest of zeros is equal to the end. A rest with a number above 0 before its first letters
|
|
163
|
+
* comes after the end. A rest with letters before such a number, for example ` SP1` or `-beta`, has no known order.
|
|
164
|
+
*/
|
|
165
|
+
export declare function compareGeneric(a: string, b: string): number | undefined;
|
|
166
|
+
export type { OsvEvent, OsvRange, Purl, Vers, VersComparator, VersConstraint, VersionScheme };
|
|
67
167
|
//# sourceMappingURL=index.d.ts.map
|
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/purl.ts","../src/osv.ts"],"mappings":";;;;;;;;;;;UAQiB;;WAEN;;WAEA;;WAEA;;WAEA;;WAEA,aAAa,SAAS;;WAEtB;;;;;;;wBAmDK,cAAc,MAAM,OAAO;;;;;wBAmC3B,UAAU,eAAe;;;;;wBA2DzB,WAAW,MAAM;;;;;wBAkBjB,cAAc;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/purl.ts","../src/vers.ts","../src/osv.ts","../src/version.ts"],"mappings":";;;;;;;;;;;UAQiB;;WAEN;;WAEA;;WAEA;;WAEA;;WAEA,aAAa,SAAS;;WAEtB;;;;;;;wBAmDK,cAAc,MAAM,OAAO;;;;;wBAmC3B,UAAU,eAAe;;;;;wBA2DzB,WAAW,MAAM;;;;;wBAkBjB,cAAc;;KChLlB;;UAGK;WACN,YAAY;WACZ;;;UAIM;;WAEN;;WAEA;WACA,sBAAsB;;;wBAsBjB,UAAU,eAAe;;;;;wBAsBzB,WAAW,MAAM;;;;;;wBAuBjB,aAAa,MAAM,MAAM;;;;;;wBAqCzB,QAAQ,MAAM,MAAM;;;;;qBChHvB,gBAAgB,SAAS;;;;;wBAgDtB,QAAQ,mBAAmB;;;;;wBAe3B,eAAe;WAAgB;WAA4B;WAAuB;;;UAMjF;WACN;WACA;WACA;WACA;;;UAIM;WACN;WACA,iBAAiB;;;;;;;;;;wBAWZ,aAAa,mBAAmB,OAAO,WAAW;;;;;;;KChGtD;;wBAeI,cAAc,eAAe;;;;;wBAQ7B,gBAAgB,gBAAgB,WAAW;;wBAiE3C,cAAc,WAAW;;;;;wBAUzB,UAAU,WAAW;;wBA+CrB,cAAc,WAAW;;wBAqEzB,cAAc,WAAW;;wBAyDzB,WAAW,WAAW;;;;;wBAkBtB,cAAc,WAAW;;;;;;;;;;;;;wBAmCzB,eAAe,WAAW"}
|
package/build/index.js
CHANGED
|
@@ -161,6 +161,300 @@ function packagePurlOf(text) {
|
|
|
161
161
|
name: purl.name
|
|
162
162
|
});
|
|
163
163
|
}
|
|
164
|
+
// src/version.ts
|
|
165
|
+
/** The version scheme of each purl type whose order this module knows. */
|
|
166
|
+
const TYPE_SCHEMES = /* @__PURE__ */ new Map([
|
|
167
|
+
["npm", "semver"],
|
|
168
|
+
["cargo", "semver"],
|
|
169
|
+
["semver", "semver"],
|
|
170
|
+
["golang", "golang"],
|
|
171
|
+
["pypi", "pypi"],
|
|
172
|
+
["deb", "deb"],
|
|
173
|
+
["rpm", "rpm"],
|
|
174
|
+
["generic", "generic"]
|
|
175
|
+
]);
|
|
176
|
+
/** This function gives the version scheme of a purl type, or `undefined` for a type whose order this module does not know. */
|
|
177
|
+
function versionScheme(type) {
|
|
178
|
+
return TYPE_SCHEMES.get(type.toLowerCase());
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* This function compares two versions of one scheme. The result is below 0 when `a` comes before `b`, 0 when they are
|
|
182
|
+
* equal and above 0 when `a` comes after `b`. When a text is not a version of the scheme, the result is `undefined`.
|
|
183
|
+
*/
|
|
184
|
+
function compareVersions(scheme, a, b) {
|
|
185
|
+
switch (scheme) {
|
|
186
|
+
case "semver":
|
|
187
|
+
case "npm":
|
|
188
|
+
case "cargo": return compareSemver(a, b);
|
|
189
|
+
case "golang": return compareGo(a, b);
|
|
190
|
+
case "pypi": return comparePep440(a, b);
|
|
191
|
+
case "deb": return compareDebian(a, b);
|
|
192
|
+
case "rpm": return compareRpm(a, b);
|
|
193
|
+
case "intdot": return compareIntdot(a, b);
|
|
194
|
+
case "generic": return compareGeneric(a, b);
|
|
195
|
+
default: return;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const sign = (n) => n < 0 ? -1 : n > 0 ? 1 : 0;
|
|
199
|
+
const compareText = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
200
|
+
const compareDigits = (a, b) => {
|
|
201
|
+
const x = a.replace(/^0+(?=\d)/, "");
|
|
202
|
+
const y = b.replace(/^0+(?=\d)/, "");
|
|
203
|
+
return x.length !== y.length ? sign(x.length - y.length) : compareText(x, y);
|
|
204
|
+
};
|
|
205
|
+
const SEMVER = /^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-]+)*)?$/;
|
|
206
|
+
function parseSemver(text) {
|
|
207
|
+
const match = SEMVER.exec(text.trim().replace(/^=/, ""));
|
|
208
|
+
if (match === null) return void 0;
|
|
209
|
+
return {
|
|
210
|
+
core: [
|
|
211
|
+
match[1],
|
|
212
|
+
match[2],
|
|
213
|
+
match[3]
|
|
214
|
+
],
|
|
215
|
+
pre: match[4] === void 0 ? [] : match[4].split(".")
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
function compareSemverParts(x, y) {
|
|
219
|
+
for (let i = 0; i < 3; i++) {
|
|
220
|
+
const c = compareDigits(x.core[i], y.core[i]);
|
|
221
|
+
if (c !== 0) return c;
|
|
222
|
+
}
|
|
223
|
+
if (x.pre.length === 0 || y.pre.length === 0) return sign(y.pre.length - x.pre.length);
|
|
224
|
+
for (let i = 0; i < Math.min(x.pre.length, y.pre.length); i++) {
|
|
225
|
+
const p = x.pre[i];
|
|
226
|
+
const q = y.pre[i];
|
|
227
|
+
const pn = /^\d+$/.test(p);
|
|
228
|
+
const qn = /^\d+$/.test(q);
|
|
229
|
+
const c = pn && qn ? compareDigits(p, q) : pn ? -1 : qn ? 1 : compareText(p, q);
|
|
230
|
+
if (c !== 0) return c;
|
|
231
|
+
}
|
|
232
|
+
return sign(x.pre.length - y.pre.length);
|
|
233
|
+
}
|
|
234
|
+
/** This function compares two SemVer 2.0.0 versions. It accepts a leading `v` or `=`, as npm does. */
|
|
235
|
+
function compareSemver(a, b) {
|
|
236
|
+
const x = parseSemver(a);
|
|
237
|
+
const y = parseSemver(b);
|
|
238
|
+
return x === void 0 || y === void 0 ? void 0 : compareSemverParts(x, y);
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* This function compares two versions of Go modules. A Go version starts with `v` and follows SemVer. A pseudo-version is
|
|
242
|
+
* a pre-release, so it has its place in the order of SemVer. The suffix `+incompatible` is build metadata.
|
|
243
|
+
*/
|
|
244
|
+
function compareGo(a, b) {
|
|
245
|
+
if (!a.startsWith("v") || !b.startsWith("v")) return void 0;
|
|
246
|
+
return compareSemver(a, b);
|
|
247
|
+
}
|
|
248
|
+
const PEP440 = /^\s*v?(?:(?:(\d+)!)?(\d+(?:\.\d+)*)(?:[-_.]?(a|b|c|rc|alpha|beta|pre|preview)[-_.]?(\d+)?)?(?:-(\d+)|[-_.]?(post|rev|r)[-_.]?(\d+)?)?(?:[-_.]?(dev)[-_.]?(\d+)?)?)(?:\+([a-z0-9]+(?:[-_.][a-z0-9]+)*))?\s*$/i;
|
|
249
|
+
const PRE_RANK = {
|
|
250
|
+
a: 0,
|
|
251
|
+
alpha: 0,
|
|
252
|
+
b: 1,
|
|
253
|
+
beta: 1,
|
|
254
|
+
c: 2,
|
|
255
|
+
rc: 2,
|
|
256
|
+
pre: 2,
|
|
257
|
+
preview: 2
|
|
258
|
+
};
|
|
259
|
+
function parsePep440(text) {
|
|
260
|
+
const m = PEP440.exec(text);
|
|
261
|
+
if (m === null) return void 0;
|
|
262
|
+
const release = m[2].split(".").map(Number);
|
|
263
|
+
while (release.length > 1 && release[release.length - 1] === 0) release.pop();
|
|
264
|
+
const post = m[5] !== void 0 ? Number(m[5]) : m[6] !== void 0 ? Number(m[7] ?? 0) : void 0;
|
|
265
|
+
return {
|
|
266
|
+
epoch: Number(m[1] ?? 0),
|
|
267
|
+
release,
|
|
268
|
+
...m[3] === void 0 ? {} : { pre: [PRE_RANK[m[3].toLowerCase()], Number(m[4] ?? 0)] },
|
|
269
|
+
...post === void 0 ? {} : { post },
|
|
270
|
+
...m[8] === void 0 ? {} : { dev: Number(m[9] ?? 0) },
|
|
271
|
+
local: m[10] === void 0 ? [] : m[10].toLowerCase().split(/[-_.]/)
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
function compareNumbers(a, b) {
|
|
275
|
+
for (let i = 0; i < Math.max(a.length, b.length); i++) {
|
|
276
|
+
const c = sign((a[i] ?? 0) - (b[i] ?? 0));
|
|
277
|
+
if (c !== 0) return c;
|
|
278
|
+
}
|
|
279
|
+
return 0;
|
|
280
|
+
}
|
|
281
|
+
/** This function compares two PEP 440 versions, with the order of the Python packaging specification. */
|
|
282
|
+
function comparePep440(a, b) {
|
|
283
|
+
const x = parsePep440(a);
|
|
284
|
+
const y = parsePep440(b);
|
|
285
|
+
if (x === void 0 || y === void 0) return void 0;
|
|
286
|
+
const epoch = sign(x.epoch - y.epoch);
|
|
287
|
+
if (epoch !== 0) return epoch;
|
|
288
|
+
const release = compareNumbers(x.release, y.release);
|
|
289
|
+
if (release !== 0) return release;
|
|
290
|
+
const preKey = (v) => v.pre !== void 0 ? [0, ...v.pre] : v.post === void 0 && v.dev !== void 0 ? [-1] : [1];
|
|
291
|
+
const pre = compareNumbers(preKey(x), preKey(y));
|
|
292
|
+
if (pre !== 0) return pre;
|
|
293
|
+
const postKey = (v) => v.post === void 0 ? [-1] : [0, v.post];
|
|
294
|
+
const post = compareNumbers(postKey(x), postKey(y));
|
|
295
|
+
if (post !== 0) return post;
|
|
296
|
+
const devKey = (v) => v.dev === void 0 ? [1] : [0, v.dev];
|
|
297
|
+
const dev = compareNumbers(devKey(x), devKey(y));
|
|
298
|
+
if (dev !== 0) return dev;
|
|
299
|
+
return compareLocal(x.local, y.local);
|
|
300
|
+
}
|
|
301
|
+
function compareLocal(a, b) {
|
|
302
|
+
for (let i = 0; i < Math.min(a.length, b.length); i++) {
|
|
303
|
+
const p = a[i];
|
|
304
|
+
const q = b[i];
|
|
305
|
+
const pn = /^\d+$/.test(p);
|
|
306
|
+
const qn = /^\d+$/.test(q);
|
|
307
|
+
const c = pn && qn ? compareDigits(p, q) : pn ? 1 : qn ? -1 : compareText(p, q);
|
|
308
|
+
if (c !== 0) return c;
|
|
309
|
+
}
|
|
310
|
+
return sign(a.length - b.length);
|
|
311
|
+
}
|
|
312
|
+
function debianRank(text, i) {
|
|
313
|
+
if (i >= text.length) return 0;
|
|
314
|
+
const c = text[i];
|
|
315
|
+
if (/d/.test(c)) return 0;
|
|
316
|
+
if (c === "~") return -1;
|
|
317
|
+
if (/[A-Za-z]/.test(c)) return c.charCodeAt(0);
|
|
318
|
+
return c.charCodeAt(0) + 256;
|
|
319
|
+
}
|
|
320
|
+
function compareDebianPart(a, b) {
|
|
321
|
+
let i = 0;
|
|
322
|
+
let j = 0;
|
|
323
|
+
while (i < a.length || j < b.length) {
|
|
324
|
+
while (i < a.length && !/\d/.test(a[i]) || j < b.length && !/\d/.test(b[j])) {
|
|
325
|
+
const c = debianRank(a, i) - debianRank(b, j);
|
|
326
|
+
if (c !== 0) return sign(c);
|
|
327
|
+
i++;
|
|
328
|
+
j++;
|
|
329
|
+
}
|
|
330
|
+
let p = "";
|
|
331
|
+
let q = "";
|
|
332
|
+
while (i < a.length && /\d/.test(a[i])) p += a[i++];
|
|
333
|
+
while (j < b.length && /\d/.test(b[j])) q += b[j++];
|
|
334
|
+
const c = compareDigits(p || "0", q || "0");
|
|
335
|
+
if (c !== 0) return c;
|
|
336
|
+
}
|
|
337
|
+
return 0;
|
|
338
|
+
}
|
|
339
|
+
const DEBIAN = /^(?:(\d+):)?([0-9][A-Za-z0-9.+~:-]*?)(?:-([A-Za-z0-9.+~]+))?$/;
|
|
340
|
+
/** This function compares two Debian versions (`[epoch:]upstream[-revision]`) with the order of dpkg. */
|
|
341
|
+
function compareDebian(a, b) {
|
|
342
|
+
const x = DEBIAN.exec(a.trim());
|
|
343
|
+
const y = DEBIAN.exec(b.trim());
|
|
344
|
+
if (x === null || y === null) return void 0;
|
|
345
|
+
const epoch = compareDigits(x[1] ?? "0", y[1] ?? "0");
|
|
346
|
+
if (epoch !== 0) return epoch;
|
|
347
|
+
const upstream = compareDebianPart(x[2], y[2]);
|
|
348
|
+
if (upstream !== 0) return upstream;
|
|
349
|
+
return compareDebianPart(x[3] ?? "0", y[3] ?? "0");
|
|
350
|
+
}
|
|
351
|
+
function rpmvercmp(a, b) {
|
|
352
|
+
if (a === b) return 0;
|
|
353
|
+
let i = 0;
|
|
354
|
+
let j = 0;
|
|
355
|
+
for (;;) {
|
|
356
|
+
while (i < a.length && !/[A-Za-z0-9~^]/.test(a[i])) i++;
|
|
357
|
+
while (j < b.length && !/[A-Za-z0-9~^]/.test(b[j])) j++;
|
|
358
|
+
const ca = a[i];
|
|
359
|
+
const cb = b[j];
|
|
360
|
+
if (ca === "~" || cb === "~") {
|
|
361
|
+
if (ca !== "~") return 1;
|
|
362
|
+
if (cb !== "~") return -1;
|
|
363
|
+
i++;
|
|
364
|
+
j++;
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
if (ca === "^" || cb === "^") {
|
|
368
|
+
if (ca === void 0) return -1;
|
|
369
|
+
if (cb === void 0) return 1;
|
|
370
|
+
if (ca !== "^") return 1;
|
|
371
|
+
if (cb !== "^") return -1;
|
|
372
|
+
i++;
|
|
373
|
+
j++;
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
if (ca === void 0 || cb === void 0) break;
|
|
377
|
+
const digits = /\d/.test(ca);
|
|
378
|
+
const same = digits ? /\d/ : /[A-Za-z]/;
|
|
379
|
+
let p = "";
|
|
380
|
+
let q = "";
|
|
381
|
+
while (i < a.length && same.test(a[i])) p += a[i++];
|
|
382
|
+
while (j < b.length && same.test(b[j])) q += b[j++];
|
|
383
|
+
if (q === "") return digits ? 1 : -1;
|
|
384
|
+
const c = digits ? compareDigits(p, q) : compareText(p, q);
|
|
385
|
+
if (c !== 0) return c;
|
|
386
|
+
}
|
|
387
|
+
if (i >= a.length && j >= b.length) return 0;
|
|
388
|
+
return i >= a.length ? -1 : 1;
|
|
389
|
+
}
|
|
390
|
+
const RPM = /^(?:(\d+):)?([^-]+)(?:-([^-]+))?$/;
|
|
391
|
+
/** This function compares two RPM versions (`[epoch:]version[-release]`) with the order of `rpmvercmp`. */
|
|
392
|
+
function compareRpm(a, b) {
|
|
393
|
+
const x = RPM.exec(a.trim());
|
|
394
|
+
const y = RPM.exec(b.trim());
|
|
395
|
+
if (x === null || y === null) return void 0;
|
|
396
|
+
const epoch = compareDigits(x[1] ?? "0", y[1] ?? "0");
|
|
397
|
+
if (epoch !== 0) return epoch;
|
|
398
|
+
const version = rpmvercmp(x[2], y[2]);
|
|
399
|
+
if (version !== 0) return version;
|
|
400
|
+
return rpmvercmp(x[3] ?? "", y[3] ?? "");
|
|
401
|
+
}
|
|
402
|
+
const INTDOT = /^\d+(?:\.\d+)*$/;
|
|
403
|
+
/**
|
|
404
|
+
* This function compares two versions of the `vers` scheme `intdot`: integers with a `.` between them. The integers
|
|
405
|
+
* compare from left to right. A missing integer is 0, so `1.0` equals `1.0.0`.
|
|
406
|
+
*/
|
|
407
|
+
function compareIntdot(a, b) {
|
|
408
|
+
const x = a.trim();
|
|
409
|
+
const y = b.trim();
|
|
410
|
+
if (!INTDOT.test(x) || !INTDOT.test(y)) return void 0;
|
|
411
|
+
const p = x.split(".");
|
|
412
|
+
const q = y.split(".");
|
|
413
|
+
for (let i = 0; i < Math.max(p.length, q.length); i++) {
|
|
414
|
+
const c = compareDigits(p[i] ?? "0", q[i] ?? "0");
|
|
415
|
+
if (c !== 0) return c;
|
|
416
|
+
}
|
|
417
|
+
return 0;
|
|
418
|
+
}
|
|
419
|
+
/** This function gives the segments of a version of a vendor: the runs of digits and the runs of letters. */
|
|
420
|
+
function genericSegments(text) {
|
|
421
|
+
const body = text.trim().replace(/^[vV](?=\d)/, "");
|
|
422
|
+
if (!/^\d/.test(body)) return void 0;
|
|
423
|
+
return body.match(/\d+|[A-Za-z]+/g) ?? void 0;
|
|
424
|
+
}
|
|
425
|
+
const isDigits = (segment) => /^\d/.test(segment);
|
|
426
|
+
/**
|
|
427
|
+
* This function compares two versions of the `vers` scheme `generic`, the versions of vendors, for example `V6.80` or
|
|
428
|
+
* `2.9.1.0_FR`. A version starts with a digit, after one optional `v`. The segments are the runs of digits and of letters.
|
|
429
|
+
* The other characters separate the segments.
|
|
430
|
+
*
|
|
431
|
+
* The segments compare from left to right, and the first difference decides. Two runs of digits compare as integers.
|
|
432
|
+
* Two runs of letters are equal when they are the same with no regard to case. Other pairs have no known order. These
|
|
433
|
+
* pairs give `undefined`: two different runs of letters, or a run of digits and a run of letters.
|
|
434
|
+
*
|
|
435
|
+
* When one version ends first, a rest of zeros is equal to the end. A rest with a number above 0 before its first letters
|
|
436
|
+
* comes after the end. A rest with letters before such a number, for example ` SP1` or `-beta`, has no known order.
|
|
437
|
+
*/
|
|
438
|
+
function compareGeneric(a, b) {
|
|
439
|
+
const p = genericSegments(a);
|
|
440
|
+
const q = genericSegments(b);
|
|
441
|
+
if (p === void 0 || q === void 0) return void 0;
|
|
442
|
+
for (let i = 0; i < Math.min(p.length, q.length); i++) {
|
|
443
|
+
const x = p[i];
|
|
444
|
+
const y = q[i];
|
|
445
|
+
if (isDigits(x) && isDigits(y)) {
|
|
446
|
+
const c = compareDigits(x, y);
|
|
447
|
+
if (c !== 0) return c;
|
|
448
|
+
} else if (isDigits(x) || isDigits(y) || x.toLowerCase() !== y.toLowerCase()) return;
|
|
449
|
+
}
|
|
450
|
+
const rest = p.length > q.length ? p.slice(q.length) : q.slice(p.length);
|
|
451
|
+
const longer = p.length > q.length ? 1 : -1;
|
|
452
|
+
for (const segment of rest) {
|
|
453
|
+
if (!isDigits(segment)) return void 0;
|
|
454
|
+
if (/[1-9]/.test(segment)) return longer;
|
|
455
|
+
}
|
|
456
|
+
return 0;
|
|
457
|
+
}
|
|
164
458
|
// src/osv.ts
|
|
165
459
|
/**
|
|
166
460
|
* The map from the ecosystems of OSV (`https://ossf.github.io/osv-schema/`) to purl types, and the purl of a package
|
|
@@ -229,11 +523,11 @@ function split(type, name) {
|
|
|
229
523
|
*/
|
|
230
524
|
function osvPurl(ecosystem, name) {
|
|
231
525
|
const base = ecosystem.split(":")[0];
|
|
232
|
-
const type = OSV_PURL_TYPES[base];
|
|
526
|
+
const type = Object.hasOwn(OSV_PURL_TYPES, base) ? OSV_PURL_TYPES[base] : void 0;
|
|
233
527
|
if (type === void 0 || name === "") return void 0;
|
|
234
528
|
const names = split(type, name);
|
|
235
529
|
if (names === void 0) return void 0;
|
|
236
|
-
const namespace = DISTRIBUTION[base] ?? names.namespace;
|
|
530
|
+
const namespace = (Object.hasOwn(DISTRIBUTION, base) ? DISTRIBUTION[base] : void 0) ?? names.namespace;
|
|
237
531
|
return formatPurl({
|
|
238
532
|
type,
|
|
239
533
|
...namespace === void 0 ? {} : { namespace },
|
|
@@ -247,6 +541,183 @@ function osvPurl(ecosystem, name) {
|
|
|
247
541
|
function osvPackagePurl(pkg) {
|
|
248
542
|
return (pkg.purl === void 0 ? void 0 : packagePurlOf(pkg.purl)) ?? osvPurl(pkg.ecosystem, pkg.name);
|
|
249
543
|
}
|
|
250
|
-
|
|
544
|
+
/**
|
|
545
|
+
* This function gives the `vers` range of an OSV range. A range of the type `SEMVER` has the scheme `semver`. A range
|
|
546
|
+
* of the type `ECOSYSTEM` has the purl type of its ecosystem as its scheme. A `GIT` range, an ecosystem with no purl type
|
|
547
|
+
* or an event with no version gives `undefined`.
|
|
548
|
+
*
|
|
549
|
+
* The events come in the order of their versions, as the OSV schema tells, when the scheme has a known order. Else they
|
|
550
|
+
* keep the order of the record. The event `introduced 0` is the start of each version, so it gives no constraint.
|
|
551
|
+
*/
|
|
552
|
+
function osvRangeVers(ecosystem, range) {
|
|
553
|
+
const base = ecosystem.split(":")[0];
|
|
554
|
+
const type = Object.hasOwn(OSV_PURL_TYPES, base) ? OSV_PURL_TYPES[base] : void 0;
|
|
555
|
+
const scheme = range.type === "SEMVER" ? "semver" : range.type === "ECOSYSTEM" ? type : void 0;
|
|
556
|
+
if (scheme === void 0) return void 0;
|
|
557
|
+
const constraints = [];
|
|
558
|
+
let all = false;
|
|
559
|
+
for (const event of range.events) {
|
|
560
|
+
const constraint = eventConstraint(event);
|
|
561
|
+
if (constraint === void 0) return void 0;
|
|
562
|
+
if (constraint === "start") all = true;
|
|
563
|
+
else if (constraint !== "none") constraints.push(constraint);
|
|
564
|
+
}
|
|
565
|
+
if (constraints.length === 0) return all ? {
|
|
566
|
+
scheme,
|
|
567
|
+
all: true,
|
|
568
|
+
constraints: []
|
|
569
|
+
} : void 0;
|
|
570
|
+
return {
|
|
571
|
+
scheme,
|
|
572
|
+
all: false,
|
|
573
|
+
constraints: sortEvents(scheme, constraints)
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
function eventConstraint(event) {
|
|
577
|
+
if (event.introduced !== void 0) return event.introduced === "0" ? "start" : {
|
|
578
|
+
comparator: ">=",
|
|
579
|
+
version: event.introduced
|
|
580
|
+
};
|
|
581
|
+
if (event.fixed !== void 0) return {
|
|
582
|
+
comparator: "<",
|
|
583
|
+
version: event.fixed
|
|
584
|
+
};
|
|
585
|
+
if (event.last_affected !== void 0) return {
|
|
586
|
+
comparator: "<=",
|
|
587
|
+
version: event.last_affected
|
|
588
|
+
};
|
|
589
|
+
if (event.limit !== void 0) return event.limit === "*" ? "none" : {
|
|
590
|
+
comparator: "<",
|
|
591
|
+
version: event.limit
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
function sortEvents(scheme, constraints) {
|
|
595
|
+
if (versionScheme(scheme) === void 0) return [...constraints];
|
|
596
|
+
let unknown = false;
|
|
597
|
+
const sorted = [...constraints].sort((a, b) => {
|
|
598
|
+
const c = compareVersions(scheme, a.version, b.version);
|
|
599
|
+
if (c === void 0) unknown = true;
|
|
600
|
+
return c ?? 0;
|
|
601
|
+
});
|
|
602
|
+
return unknown ? [...constraints] : sorted;
|
|
603
|
+
}
|
|
604
|
+
// src/vers.ts
|
|
605
|
+
/**
|
|
606
|
+
* Version ranges in the `vers` format of the purl specification, for example `vers:npm/>=1.0.0|<1.2.3`. The functions
|
|
607
|
+
* parse a range, format it and tell if a version is in it, with the version order of the scheme.
|
|
608
|
+
*/
|
|
609
|
+
const COMPARATORS = [
|
|
610
|
+
"!=",
|
|
611
|
+
"<=",
|
|
612
|
+
">=",
|
|
613
|
+
"<",
|
|
614
|
+
">",
|
|
615
|
+
"="
|
|
616
|
+
];
|
|
617
|
+
/** This function reads one constraint, for example `>=1.0.0`. A constraint with no comparator has the comparator `=`. */
|
|
618
|
+
function parseConstraint(text) {
|
|
619
|
+
const trimmed = text.trim();
|
|
620
|
+
const comparator = COMPARATORS.find((c) => trimmed.startsWith(c)) ?? "=";
|
|
621
|
+
const raw = trimmed.slice(trimmed.startsWith(comparator) ? comparator.length : 0).trim();
|
|
622
|
+
if (raw === "") return void 0;
|
|
623
|
+
let version;
|
|
624
|
+
try {
|
|
625
|
+
version = decodeURIComponent(raw);
|
|
626
|
+
} catch {
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
return {
|
|
630
|
+
comparator,
|
|
631
|
+
version
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
/** This function reads a `vers` text. A text that is not a range gives `undefined`. */
|
|
635
|
+
function parseVers(text) {
|
|
636
|
+
const match = /^vers:([A-Za-z0-9.+-]+)\/(.+)$/.exec(text.replace(/\s+/g, ""));
|
|
637
|
+
if (match === null) return void 0;
|
|
638
|
+
const scheme = match[1].toLowerCase();
|
|
639
|
+
const body = match[2];
|
|
640
|
+
if (body === "*") return {
|
|
641
|
+
scheme,
|
|
642
|
+
all: true,
|
|
643
|
+
constraints: []
|
|
644
|
+
};
|
|
645
|
+
const constraints = [];
|
|
646
|
+
for (const part of body.split("|")) {
|
|
647
|
+
const constraint = parseConstraint(part);
|
|
648
|
+
if (constraint === void 0) return void 0;
|
|
649
|
+
constraints.push(constraint);
|
|
650
|
+
}
|
|
651
|
+
return {
|
|
652
|
+
scheme,
|
|
653
|
+
all: false,
|
|
654
|
+
constraints
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
const encodeVersion = (version) => version.replace(/[^A-Za-z0-9.+~_:-]/g, (c) => encodeURIComponent(c));
|
|
658
|
+
/**
|
|
659
|
+
* This function gives the `vers` text of a range. The constraints come in the order of the versions, when the scheme
|
|
660
|
+
* has a known order. The comparator `=` has no sign, as the specification tells.
|
|
661
|
+
*/
|
|
662
|
+
function formatVers(vers) {
|
|
663
|
+
if (vers.all) return `vers:${vers.scheme}/*`;
|
|
664
|
+
const body = (sortConstraints(vers.scheme, vers.constraints) ?? vers.constraints).map((c) => `${c.comparator === "=" ? "" : c.comparator}${encodeVersion(c.version)}`).join("|");
|
|
665
|
+
return `vers:${vers.scheme}/${body}`;
|
|
666
|
+
}
|
|
667
|
+
function sortConstraints(scheme, constraints) {
|
|
668
|
+
let unknown = false;
|
|
669
|
+
const sorted = [...constraints].sort((a, b) => {
|
|
670
|
+
const c = compareVersions(scheme, a.version, b.version);
|
|
671
|
+
if (c === void 0) unknown = true;
|
|
672
|
+
return c ?? 0;
|
|
673
|
+
});
|
|
674
|
+
return unknown ? void 0 : sorted;
|
|
675
|
+
}
|
|
676
|
+
/**
|
|
677
|
+
* This function tells if a version is in a range, with the algorithm of the `vers` specification. The result is
|
|
678
|
+
* `undefined` when the scheme has no known order, or when a version is not a version of the scheme. Thus a caller
|
|
679
|
+
* does not take an unknown result as a match or as no match.
|
|
680
|
+
*/
|
|
681
|
+
function versContains(vers, version) {
|
|
682
|
+
if (vers.all) return true;
|
|
683
|
+
const compare = (a, b) => compareVersions(vers.scheme, a, b);
|
|
684
|
+
for (const constraint of vers.constraints) {
|
|
685
|
+
const c = compare(version, constraint.version);
|
|
686
|
+
if (c === void 0) return void 0;
|
|
687
|
+
if (c === 0 && [
|
|
688
|
+
"=",
|
|
689
|
+
"<=",
|
|
690
|
+
">="
|
|
691
|
+
].includes(constraint.comparator)) return true;
|
|
692
|
+
if (c === 0 && constraint.comparator === "!=") return false;
|
|
693
|
+
}
|
|
694
|
+
const ranged = sortConstraints(vers.scheme, vers.constraints.filter((c) => c.comparator !== "=" && c.comparator !== "!="));
|
|
695
|
+
if (ranged === void 0) return void 0;
|
|
696
|
+
if (ranged.length === 0) return vers.constraints.some((c) => c.comparator === "!=");
|
|
697
|
+
const below = (c) => c.comparator === "<" || c.comparator === "<=";
|
|
698
|
+
const above = (c) => c.comparator === ">" || c.comparator === ">=";
|
|
699
|
+
const first = ranged[0];
|
|
700
|
+
const last = ranged[ranged.length - 1];
|
|
701
|
+
if (below(first) && compare(version, first.version) < 0) return true;
|
|
702
|
+
if (above(last) && compare(version, last.version) > 0) return true;
|
|
703
|
+
for (let i = 0; i + 1 < ranged.length; i++) {
|
|
704
|
+
const low = ranged[i];
|
|
705
|
+
const high = ranged[i + 1];
|
|
706
|
+
if (above(low) && below(high) && compare(version, low.version) > 0 && compare(version, high.version) < 0) return true;
|
|
707
|
+
}
|
|
708
|
+
return false;
|
|
709
|
+
}
|
|
710
|
+
/**
|
|
711
|
+
* This function gives the end of the part of a range that holds a version, when that end is a constraint `<`. For the
|
|
712
|
+
* range of an advisory, it is the first fixed version above the version. The result is `undefined` for a version out
|
|
713
|
+
* of the range, or for an unknown order. It is also `undefined` for a part with no such end: `<=`, an open end or `=`.
|
|
714
|
+
*/
|
|
715
|
+
function versEnd(vers, version) {
|
|
716
|
+
if (vers.all || versContains(vers, version) !== true) return void 0;
|
|
717
|
+
if (vers.constraints.some((c) => c.comparator === "=" && compareVersions(vers.scheme, version, c.version) === 0)) return void 0;
|
|
718
|
+
const next = sortConstraints(vers.scheme, vers.constraints.filter((c) => c.comparator !== "=" && c.comparator !== "!="))?.find((c) => compareVersions(vers.scheme, version, c.version) < 0);
|
|
719
|
+
return next?.comparator === "<" ? next.version : void 0;
|
|
720
|
+
}
|
|
721
|
+
export { OSV_PURL_TYPES, compareDebian, compareGeneric, compareGo, compareIntdot, comparePep440, compareRpm, compareSemver, compareVersions, formatPurl, formatVers, normalizePurl, osvPackagePurl, osvPurl, osvRangeVers, packagePurlOf, parsePurl, parseVers, versContains, versEnd, versionScheme };
|
|
251
722
|
|
|
252
723
|
//# sourceMappingURL=index.js.map
|