@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/src/version.ts ADDED
@@ -0,0 +1,353 @@
1
+ /**
2
+ * The version order of each ecosystem: SemVer, Go modules, PEP 440, Debian and RPM. The `vers` schemes `intdot` and
3
+ * `generic` have an order too. Each function compares two versions of one scheme. A text that is not a version of the
4
+ * scheme gives `undefined`, not a guess.
5
+ */
6
+
7
+ /** The version schemes that this module compares. */
8
+ export type VersionScheme = "semver" | "golang" | "pypi" | "deb" | "rpm" | "intdot" | "generic";
9
+
10
+ /** The version scheme of each purl type whose order this module knows. */
11
+ const TYPE_SCHEMES: ReadonlyMap<string, VersionScheme> = new Map([
12
+ ["npm", "semver"],
13
+ ["cargo", "semver"],
14
+ ["semver", "semver"],
15
+ ["golang", "golang"],
16
+ ["pypi", "pypi"],
17
+ ["deb", "deb"],
18
+ ["rpm", "rpm"],
19
+ ["generic", "generic"],
20
+ ]);
21
+
22
+ /** This function gives the version scheme of a purl type, or `undefined` for a type whose order this module does not know. */
23
+ export function versionScheme(type: string): VersionScheme | undefined {
24
+ return TYPE_SCHEMES.get(type.toLowerCase());
25
+ }
26
+
27
+ /**
28
+ * This function compares two versions of one scheme. The result is below 0 when `a` comes before `b`, 0 when they are
29
+ * equal and above 0 when `a` comes after `b`. When a text is not a version of the scheme, the result is `undefined`.
30
+ */
31
+ export function compareVersions(scheme: string, a: string, b: string): number | undefined {
32
+ switch (scheme) {
33
+ case "semver":
34
+ case "npm":
35
+ case "cargo":
36
+ return compareSemver(a, b);
37
+ case "golang":
38
+ return compareGo(a, b);
39
+ case "pypi":
40
+ return comparePep440(a, b);
41
+ case "deb":
42
+ return compareDebian(a, b);
43
+ case "rpm":
44
+ return compareRpm(a, b);
45
+ case "intdot":
46
+ return compareIntdot(a, b);
47
+ case "generic":
48
+ return compareGeneric(a, b);
49
+ default:
50
+ return undefined;
51
+ }
52
+ }
53
+
54
+ const sign = (n: number): number => (n < 0 ? -1 : n > 0 ? 1 : 0);
55
+ const compareText = (a: string, b: string): number => (a < b ? -1 : a > b ? 1 : 0);
56
+ const compareDigits = (a: string, b: string): number => {
57
+ const x = a.replace(/^0+(?=\d)/, "");
58
+ const y = b.replace(/^0+(?=\d)/, "");
59
+ return x.length !== y.length ? sign(x.length - y.length) : compareText(x, y);
60
+ };
61
+
62
+ // SemVer 2.0.0. The build metadata does not change the order.
63
+ 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-]+)*)?$/;
64
+
65
+ /** The parts of a SemVer version that decide its order. */
66
+ interface Semver {
67
+ readonly core: readonly [string, string, string];
68
+ readonly pre: readonly string[];
69
+ }
70
+
71
+ function parseSemver(text: string): Semver | undefined {
72
+ const match = SEMVER.exec(text.trim().replace(/^=/, ""));
73
+ if (match === null) return undefined;
74
+ return { core: [match[1]!, match[2]!, match[3]!], pre: match[4] === undefined ? [] : match[4].split(".") };
75
+ }
76
+
77
+ function compareSemverParts(x: Semver, y: Semver): number {
78
+ for (let i = 0; i < 3; i++) {
79
+ const c = compareDigits(x.core[i]!, y.core[i]!);
80
+ if (c !== 0) return c;
81
+ }
82
+ // A version with a pre-release comes before the same version with no pre-release.
83
+ if (x.pre.length === 0 || y.pre.length === 0) return sign(y.pre.length - x.pre.length);
84
+ for (let i = 0; i < Math.min(x.pre.length, y.pre.length); i++) {
85
+ const p = x.pre[i]!;
86
+ const q = y.pre[i]!;
87
+ const pn = /^\d+$/.test(p);
88
+ const qn = /^\d+$/.test(q);
89
+ const c = pn && qn ? compareDigits(p, q) : pn ? -1 : qn ? 1 : compareText(p, q);
90
+ if (c !== 0) return c;
91
+ }
92
+ return sign(x.pre.length - y.pre.length);
93
+ }
94
+
95
+ /** This function compares two SemVer 2.0.0 versions. It accepts a leading `v` or `=`, as npm does. */
96
+ export function compareSemver(a: string, b: string): number | undefined {
97
+ const x = parseSemver(a);
98
+ const y = parseSemver(b);
99
+ return x === undefined || y === undefined ? undefined : compareSemverParts(x, y);
100
+ }
101
+
102
+ /**
103
+ * This function compares two versions of Go modules. A Go version starts with `v` and follows SemVer. A pseudo-version is
104
+ * a pre-release, so it has its place in the order of SemVer. The suffix `+incompatible` is build metadata.
105
+ */
106
+ export function compareGo(a: string, b: string): number | undefined {
107
+ if (!a.startsWith("v") || !b.startsWith("v")) return undefined;
108
+ return compareSemver(a, b);
109
+ }
110
+
111
+ // PEP 440: the pattern of the version specification of the Python packaging guide.
112
+ const PEP440 =
113
+ /^\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;
114
+
115
+ /** The parts of a PEP 440 version that decide its order. */
116
+ interface Pep440 {
117
+ readonly epoch: number;
118
+ readonly release: readonly number[];
119
+ /** The pre-release as a rank (a 0, b 1, rc 2) and a number. */
120
+ readonly pre?: readonly [number, number];
121
+ readonly post?: number;
122
+ readonly dev?: number;
123
+ readonly local: readonly string[];
124
+ }
125
+
126
+ const PRE_RANK: Readonly<Record<string, number>> = { a: 0, alpha: 0, b: 1, beta: 1, c: 2, rc: 2, pre: 2, preview: 2 };
127
+
128
+ function parsePep440(text: string): Pep440 | undefined {
129
+ const m = PEP440.exec(text);
130
+ if (m === null) return undefined;
131
+ const release = m[2]!.split(".").map(Number);
132
+ while (release.length > 1 && release[release.length - 1] === 0) release.pop();
133
+ const post = m[5] !== undefined ? Number(m[5]) : m[6] !== undefined ? Number(m[7] ?? 0) : undefined;
134
+ return {
135
+ epoch: Number(m[1] ?? 0),
136
+ release,
137
+ ...(m[3] === undefined ? {} : { pre: [PRE_RANK[m[3].toLowerCase()]!, Number(m[4] ?? 0)] as const }),
138
+ ...(post === undefined ? {} : { post }),
139
+ ...(m[8] === undefined ? {} : { dev: Number(m[9] ?? 0) }),
140
+ local: m[10] === undefined ? [] : m[10].toLowerCase().split(/[-_.]/),
141
+ };
142
+ }
143
+
144
+ function compareNumbers(a: readonly number[], b: readonly number[]): number {
145
+ for (let i = 0; i < Math.max(a.length, b.length); i++) {
146
+ const c = sign((a[i] ?? 0) - (b[i] ?? 0));
147
+ if (c !== 0) return c;
148
+ }
149
+ return 0;
150
+ }
151
+
152
+ /** This function compares two PEP 440 versions, with the order of the Python packaging specification. */
153
+ export function comparePep440(a: string, b: string): number | undefined {
154
+ const x = parsePep440(a);
155
+ const y = parsePep440(b);
156
+ if (x === undefined || y === undefined) return undefined;
157
+ const epoch = sign(x.epoch - y.epoch);
158
+ if (epoch !== 0) return epoch;
159
+ const release = compareNumbers(x.release, y.release);
160
+ if (release !== 0) return release;
161
+ // A dev release with no pre-release and no post-release comes before each pre-release.
162
+ const preKey = (v: Pep440): readonly number[] => (v.pre !== undefined ? [0, ...v.pre] : v.post === undefined && v.dev !== undefined ? [-1] : [1]);
163
+ const pre = compareNumbers(preKey(x), preKey(y));
164
+ if (pre !== 0) return pre;
165
+ const postKey = (v: Pep440): readonly number[] => (v.post === undefined ? [-1] : [0, v.post]);
166
+ const post = compareNumbers(postKey(x), postKey(y));
167
+ if (post !== 0) return post;
168
+ const devKey = (v: Pep440): readonly number[] => (v.dev === undefined ? [1] : [0, v.dev]);
169
+ const dev = compareNumbers(devKey(x), devKey(y));
170
+ if (dev !== 0) return dev;
171
+ return compareLocal(x.local, y.local);
172
+ }
173
+
174
+ // A local version comes after the same version with no local part. A number segment comes after a text segment.
175
+ function compareLocal(a: readonly string[], b: readonly string[]): number {
176
+ for (let i = 0; i < Math.min(a.length, b.length); i++) {
177
+ const p = a[i]!;
178
+ const q = b[i]!;
179
+ const pn = /^\d+$/.test(p);
180
+ const qn = /^\d+$/.test(q);
181
+ const c = pn && qn ? compareDigits(p, q) : pn ? 1 : qn ? -1 : compareText(p, q);
182
+ if (c !== 0) return c;
183
+ }
184
+ return sign(a.length - b.length);
185
+ }
186
+
187
+ // The order of a character of a Debian version: `~` comes before the end, the end before a letter, a letter before
188
+ // each other character. A digit has the rank of the end, as in dpkg.
189
+ function debianRank(text: string, i: number): number {
190
+ if (i >= text.length) return 0;
191
+ const c = text[i]!;
192
+ if (/d/.test(c)) return 0;
193
+ if (c === "~") return -1;
194
+ if (/[A-Za-z]/.test(c)) return c.charCodeAt(0);
195
+ return c.charCodeAt(0) + 256;
196
+ }
197
+
198
+ // The comparison of dpkg: parts with no digit and parts of digits, one after the other.
199
+ function compareDebianPart(a: string, b: string): number {
200
+ let i = 0;
201
+ let j = 0;
202
+ while (i < a.length || j < b.length) {
203
+ while ((i < a.length && !/\d/.test(a[i]!)) || (j < b.length && !/\d/.test(b[j]!))) {
204
+ const c = debianRank(a, i) - debianRank(b, j);
205
+ if (c !== 0) return sign(c);
206
+ i++;
207
+ j++;
208
+ }
209
+ let p = "";
210
+ let q = "";
211
+ while (i < a.length && /\d/.test(a[i]!)) p += a[i++];
212
+ while (j < b.length && /\d/.test(b[j]!)) q += b[j++];
213
+ const c = compareDigits(p || "0", q || "0");
214
+ if (c !== 0) return c;
215
+ }
216
+ return 0;
217
+ }
218
+
219
+ const DEBIAN = /^(?:(\d+):)?([0-9][A-Za-z0-9.+~:-]*?)(?:-([A-Za-z0-9.+~]+))?$/;
220
+
221
+ /** This function compares two Debian versions (`[epoch:]upstream[-revision]`) with the order of dpkg. */
222
+ export function compareDebian(a: string, b: string): number | undefined {
223
+ const x = DEBIAN.exec(a.trim());
224
+ const y = DEBIAN.exec(b.trim());
225
+ if (x === null || y === null) return undefined;
226
+ const epoch = compareDigits(x[1] ?? "0", y[1] ?? "0");
227
+ if (epoch !== 0) return epoch;
228
+ const upstream = compareDebianPart(x[2]!, y[2]!);
229
+ if (upstream !== 0) return upstream;
230
+ return compareDebianPart(x[3] ?? "0", y[3] ?? "0");
231
+ }
232
+
233
+ // The comparison of rpmvercmp: segments of letters or digits. `~` comes before each other thing, also the end. `^` comes
234
+ // after the end, but before each other segment.
235
+ function rpmvercmp(a: string, b: string): number {
236
+ if (a === b) return 0;
237
+ let i = 0;
238
+ let j = 0;
239
+ for (;;) {
240
+ while (i < a.length && !/[A-Za-z0-9~^]/.test(a[i]!)) i++;
241
+ while (j < b.length && !/[A-Za-z0-9~^]/.test(b[j]!)) j++;
242
+ const ca = a[i];
243
+ const cb = b[j];
244
+ if (ca === "~" || cb === "~") {
245
+ if (ca !== "~") return 1;
246
+ if (cb !== "~") return -1;
247
+ i++;
248
+ j++;
249
+ continue;
250
+ }
251
+ if (ca === "^" || cb === "^") {
252
+ if (ca === undefined) return -1;
253
+ if (cb === undefined) return 1;
254
+ if (ca !== "^") return 1;
255
+ if (cb !== "^") return -1;
256
+ i++;
257
+ j++;
258
+ continue;
259
+ }
260
+ if (ca === undefined || cb === undefined) break;
261
+ const digits = /\d/.test(ca);
262
+ const same = digits ? /\d/ : /[A-Za-z]/;
263
+ let p = "";
264
+ let q = "";
265
+ while (i < a.length && same.test(a[i]!)) p += a[i++];
266
+ while (j < b.length && same.test(b[j]!)) q += b[j++];
267
+ // A segment of digits comes after a segment of letters.
268
+ if (q === "") return digits ? 1 : -1;
269
+ const c = digits ? compareDigits(p, q) : compareText(p, q);
270
+ if (c !== 0) return c;
271
+ }
272
+ if (i >= a.length && j >= b.length) return 0;
273
+ return i >= a.length ? -1 : 1;
274
+ }
275
+
276
+ const RPM = /^(?:(\d+):)?([^-]+)(?:-([^-]+))?$/;
277
+
278
+ /** This function compares two RPM versions (`[epoch:]version[-release]`) with the order of `rpmvercmp`. */
279
+ export function compareRpm(a: string, b: string): number | undefined {
280
+ const x = RPM.exec(a.trim());
281
+ const y = RPM.exec(b.trim());
282
+ if (x === null || y === null) return undefined;
283
+ const epoch = compareDigits(x[1] ?? "0", y[1] ?? "0");
284
+ if (epoch !== 0) return epoch;
285
+ const version = rpmvercmp(x[2]!, y[2]!);
286
+ if (version !== 0) return version;
287
+ // A version with no release comes before each release of that version. Thus a check of a fix does not take it as fixed.
288
+ return rpmvercmp(x[3] ?? "", y[3] ?? "");
289
+ }
290
+
291
+ const INTDOT = /^\d+(?:\.\d+)*$/;
292
+
293
+ /**
294
+ * This function compares two versions of the `vers` scheme `intdot`: integers with a `.` between them. The integers
295
+ * compare from left to right. A missing integer is 0, so `1.0` equals `1.0.0`.
296
+ */
297
+ export function compareIntdot(a: string, b: string): number | undefined {
298
+ const x = a.trim();
299
+ const y = b.trim();
300
+ if (!INTDOT.test(x) || !INTDOT.test(y)) return undefined;
301
+ const p = x.split(".");
302
+ const q = y.split(".");
303
+ for (let i = 0; i < Math.max(p.length, q.length); i++) {
304
+ const c = compareDigits(p[i] ?? "0", q[i] ?? "0");
305
+ if (c !== 0) return c;
306
+ }
307
+ return 0;
308
+ }
309
+
310
+ /** This function gives the segments of a version of a vendor: the runs of digits and the runs of letters. */
311
+ function genericSegments(text: string): readonly string[] | undefined {
312
+ // One `v` before a digit is a prefix, not a segment: `V6.80` is the version `6.80`.
313
+ const body = text.trim().replace(/^[vV](?=\d)/, "");
314
+ if (!/^\d/.test(body)) return undefined;
315
+ return body.match(/\d+|[A-Za-z]+/g) ?? undefined;
316
+ }
317
+
318
+ const isDigits = (segment: string): boolean => /^\d/.test(segment);
319
+
320
+ /**
321
+ * This function compares two versions of the `vers` scheme `generic`, the versions of vendors, for example `V6.80` or
322
+ * `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.
323
+ * The other characters separate the segments.
324
+ *
325
+ * The segments compare from left to right, and the first difference decides. Two runs of digits compare as integers.
326
+ * Two runs of letters are equal when they are the same with no regard to case. Other pairs have no known order. These
327
+ * pairs give `undefined`: two different runs of letters, or a run of digits and a run of letters.
328
+ *
329
+ * 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
330
+ * comes after the end. A rest with letters before such a number, for example ` SP1` or `-beta`, has no known order.
331
+ */
332
+ export function compareGeneric(a: string, b: string): number | undefined {
333
+ const p = genericSegments(a);
334
+ const q = genericSegments(b);
335
+ if (p === undefined || q === undefined) return undefined;
336
+ for (let i = 0; i < Math.min(p.length, q.length); i++) {
337
+ const x = p[i]!;
338
+ const y = q[i]!;
339
+ if (isDigits(x) && isDigits(y)) {
340
+ const c = compareDigits(x, y);
341
+ if (c !== 0) return c;
342
+ } else if (isDigits(x) || isDigits(y) || x.toLowerCase() !== y.toLowerCase()) {
343
+ return undefined;
344
+ }
345
+ }
346
+ const rest = p.length > q.length ? p.slice(q.length) : q.slice(p.length);
347
+ const longer = p.length > q.length ? 1 : -1;
348
+ for (const segment of rest) {
349
+ if (!isDigits(segment)) return undefined;
350
+ if (/[1-9]/.test(segment)) return longer;
351
+ }
352
+ return 0;
353
+ }