@mark1russell7/package-id 0.0.0-stage → 0.0.2
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/LICENSE +21 -0
- package/README.md +60 -2
- package/build/index.d.ts +148 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +664 -0
- package/build/index.js.map +1 -0
- package/package.json +58 -3
- package/src/index.ts +18 -0
- package/src/osv.ts +140 -0
- package/src/purl.ts +187 -0
- package/src/vers.ts +135 -0
- package/src/version.ts +283 -0
package/build/index.js
ADDED
|
@@ -0,0 +1,664 @@
|
|
|
1
|
+
// src/purl.ts
|
|
2
|
+
/** The pattern of a type: a letter first, then letters, digits, `.`, `+` or `-`. */
|
|
3
|
+
const TYPE = /^[a-z][a-z0-9.+-]*$/;
|
|
4
|
+
/** The pattern of a qualifier key: a letter first, then letters, digits, `.`, `-` or `_`. */
|
|
5
|
+
const KEY = /^[a-z][a-z0-9._-]*$/;
|
|
6
|
+
/**
|
|
7
|
+
* The types whose namespace and name are not case sensitive. The canonical form of these parts is lower case.
|
|
8
|
+
* The list comes from the type definitions of the purl specification.
|
|
9
|
+
*/
|
|
10
|
+
const LOWER_NAMESPACE_AND_NAME = /* @__PURE__ */ new Set([
|
|
11
|
+
"alpm",
|
|
12
|
+
"apk",
|
|
13
|
+
"bitbucket",
|
|
14
|
+
"composer",
|
|
15
|
+
"deb",
|
|
16
|
+
"github",
|
|
17
|
+
"golang",
|
|
18
|
+
"npm"
|
|
19
|
+
]);
|
|
20
|
+
/** The types whose name only is not case sensitive. The canonical form of the name is lower case. */
|
|
21
|
+
const LOWER_NAME = /* @__PURE__ */ new Set([
|
|
22
|
+
"hex",
|
|
23
|
+
"pub",
|
|
24
|
+
"pypi"
|
|
25
|
+
]);
|
|
26
|
+
/** This function percent-encodes one part of a purl. A letter, a digit, `.-_~` and `:` stay as they are. */
|
|
27
|
+
function encode(text) {
|
|
28
|
+
return encodeURIComponent(text).replace(/[!'()*]/g, (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`).replace(/%3A/g, ":");
|
|
29
|
+
}
|
|
30
|
+
/** This function decodes one part of a purl, or gives `undefined` for a bad percent-encoding. */
|
|
31
|
+
function decode(text) {
|
|
32
|
+
try {
|
|
33
|
+
return decodeURIComponent(text);
|
|
34
|
+
} catch {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** This function splits a path into its decoded segments. It discards each empty segment. */
|
|
39
|
+
function segments(text) {
|
|
40
|
+
const parts = [];
|
|
41
|
+
for (const raw of text.split("/")) {
|
|
42
|
+
if (raw === "") continue;
|
|
43
|
+
const part = decode(raw);
|
|
44
|
+
if (part === void 0) return void 0;
|
|
45
|
+
parts.push(part);
|
|
46
|
+
}
|
|
47
|
+
return parts;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* This function gives the canonical parts of a purl. The type and the qualifier keys are in lower case. The parts
|
|
51
|
+
* follow the case rules of the type, and no part is empty.
|
|
52
|
+
* It gives `undefined` when a part breaks a rule of the specification.
|
|
53
|
+
*/
|
|
54
|
+
function normalizePurl(purl) {
|
|
55
|
+
const type = purl.type.toLowerCase();
|
|
56
|
+
if (!TYPE.test(type)) return void 0;
|
|
57
|
+
const lowerAll = LOWER_NAMESPACE_AND_NAME.has(type);
|
|
58
|
+
let name = lowerAll || LOWER_NAME.has(type) ? purl.name.toLowerCase() : purl.name;
|
|
59
|
+
if (type === "pypi") name = name.replace(/_/g, "-");
|
|
60
|
+
if (name === "") return void 0;
|
|
61
|
+
const space = purl.namespace?.split("/").filter((segment) => segment !== "").join("/");
|
|
62
|
+
const namespace = space === void 0 || space === "" ? void 0 : lowerAll ? space.toLowerCase() : space;
|
|
63
|
+
if (type === "maven" && namespace === void 0) return void 0;
|
|
64
|
+
const qualifiers = {};
|
|
65
|
+
for (const [key, value] of Object.entries(purl.qualifiers ?? {})) {
|
|
66
|
+
const lower = key.toLowerCase();
|
|
67
|
+
if (!KEY.test(lower)) return void 0;
|
|
68
|
+
if (value !== "") qualifiers[lower] = value;
|
|
69
|
+
}
|
|
70
|
+
const subpath = purl.subpath?.split("/").filter((segment) => segment !== "" && segment !== "." && segment !== "..").join("/");
|
|
71
|
+
return {
|
|
72
|
+
type,
|
|
73
|
+
...namespace === void 0 ? {} : { namespace },
|
|
74
|
+
name,
|
|
75
|
+
...purl.version === void 0 || purl.version === "" ? {} : { version: purl.version },
|
|
76
|
+
...Object.keys(qualifiers).length === 0 ? {} : { qualifiers },
|
|
77
|
+
...subpath === void 0 || subpath === "" ? {} : { subpath }
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* This function reads a purl text into its parts, as the parse steps of the specification tell, and normalizes them.
|
|
82
|
+
* It gives `undefined` when the text is not a valid purl.
|
|
83
|
+
*/
|
|
84
|
+
function parsePurl(text) {
|
|
85
|
+
let rest = text.trim();
|
|
86
|
+
let subpath;
|
|
87
|
+
const hash = rest.indexOf("#");
|
|
88
|
+
if (hash >= 0) {
|
|
89
|
+
const parts = segments(rest.slice(hash + 1));
|
|
90
|
+
if (parts === void 0) return void 0;
|
|
91
|
+
subpath = parts.filter((segment) => segment !== "." && segment !== "..").join("/");
|
|
92
|
+
rest = rest.slice(0, hash);
|
|
93
|
+
}
|
|
94
|
+
let qualifiers;
|
|
95
|
+
const question = rest.indexOf("?");
|
|
96
|
+
if (question >= 0) {
|
|
97
|
+
qualifiers = {};
|
|
98
|
+
for (const pair of rest.slice(question + 1).split("&")) {
|
|
99
|
+
if (pair === "") continue;
|
|
100
|
+
const equal = pair.indexOf("=");
|
|
101
|
+
if (equal <= 0) return void 0;
|
|
102
|
+
const key = pair.slice(0, equal).toLowerCase();
|
|
103
|
+
const value = decode(pair.slice(equal + 1));
|
|
104
|
+
if (value === void 0 || key in qualifiers) return void 0;
|
|
105
|
+
qualifiers[key] = value;
|
|
106
|
+
}
|
|
107
|
+
rest = rest.slice(0, question);
|
|
108
|
+
}
|
|
109
|
+
const colon = rest.indexOf(":");
|
|
110
|
+
if (colon < 0 || rest.slice(0, colon).toLowerCase() !== "pkg") return void 0;
|
|
111
|
+
rest = rest.slice(colon + 1).replace(/^\/+/, "");
|
|
112
|
+
const slash = rest.indexOf("/");
|
|
113
|
+
if (slash <= 0) return void 0;
|
|
114
|
+
const type = rest.slice(0, slash);
|
|
115
|
+
rest = rest.slice(slash + 1);
|
|
116
|
+
let version;
|
|
117
|
+
const at = rest.lastIndexOf("@");
|
|
118
|
+
if (at > 0) {
|
|
119
|
+
version = decode(rest.slice(at + 1));
|
|
120
|
+
if (version === void 0) return void 0;
|
|
121
|
+
rest = rest.slice(0, at);
|
|
122
|
+
}
|
|
123
|
+
const path = segments(rest);
|
|
124
|
+
if (path === void 0 || path.length === 0) return void 0;
|
|
125
|
+
const name = path[path.length - 1];
|
|
126
|
+
const namespace = path.length > 1 ? path.slice(0, -1).join("/") : void 0;
|
|
127
|
+
return normalizePurl({
|
|
128
|
+
type,
|
|
129
|
+
...namespace === void 0 ? {} : { namespace },
|
|
130
|
+
name,
|
|
131
|
+
...version === void 0 ? {} : { version },
|
|
132
|
+
...qualifiers === void 0 ? {} : { qualifiers },
|
|
133
|
+
...subpath === void 0 ? {} : { subpath }
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* This function writes the canonical text of a purl: the parts normalized, each part percent-encoded and the
|
|
138
|
+
* qualifiers in the order of their keys. It gives `undefined` when a part breaks a rule of the specification.
|
|
139
|
+
*/
|
|
140
|
+
function formatPurl(purl) {
|
|
141
|
+
const canonical = normalizePurl(purl);
|
|
142
|
+
if (canonical === void 0) return void 0;
|
|
143
|
+
let text = `pkg:${canonical.type}/`;
|
|
144
|
+
if (canonical.namespace !== void 0) text += `${canonical.namespace.split("/").map(encode).join("/")}/`;
|
|
145
|
+
text += encode(canonical.name);
|
|
146
|
+
if (canonical.version !== void 0) text += `@${encode(canonical.version)}`;
|
|
147
|
+
const keys = Object.keys(canonical.qualifiers ?? {}).sort();
|
|
148
|
+
if (keys.length > 0) text += `?${keys.map((key) => `${key}=${encode(canonical.qualifiers[key]).replace(/%2F/g, "/")}`).join("&")}`;
|
|
149
|
+
if (canonical.subpath !== void 0) text += `#${canonical.subpath.split("/").map(encode).join("/")}`;
|
|
150
|
+
return text;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* This function gives the canonical purl of the package with no version, no qualifiers and no subpath. A person who
|
|
154
|
+
* names a package with this text names each version of it. It gives `undefined` when the text is not a valid purl.
|
|
155
|
+
*/
|
|
156
|
+
function packagePurlOf(text) {
|
|
157
|
+
const purl = parsePurl(text);
|
|
158
|
+
return purl === void 0 ? void 0 : formatPurl({
|
|
159
|
+
type: purl.type,
|
|
160
|
+
...purl.namespace === void 0 ? {} : { namespace: purl.namespace },
|
|
161
|
+
name: purl.name
|
|
162
|
+
});
|
|
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
|
+
]);
|
|
175
|
+
/** This function gives the version scheme of a purl type, or `undefined` for a type whose order this module does not know. */
|
|
176
|
+
function versionScheme(type) {
|
|
177
|
+
return TYPE_SCHEMES.get(type.toLowerCase());
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* This function compares two versions of one scheme. The result is below 0 when `a` comes before `b`, 0 when they are
|
|
181
|
+
* equal and above 0 when `a` comes after `b`. When a text is not a version of the scheme, the result is `undefined`.
|
|
182
|
+
*/
|
|
183
|
+
function compareVersions(scheme, a, b) {
|
|
184
|
+
switch (scheme) {
|
|
185
|
+
case "semver":
|
|
186
|
+
case "npm":
|
|
187
|
+
case "cargo": return compareSemver(a, b);
|
|
188
|
+
case "golang": return compareGo(a, b);
|
|
189
|
+
case "pypi": return comparePep440(a, b);
|
|
190
|
+
case "deb": return compareDebian(a, b);
|
|
191
|
+
case "rpm": return compareRpm(a, b);
|
|
192
|
+
default: return;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const sign = (n) => n < 0 ? -1 : n > 0 ? 1 : 0;
|
|
196
|
+
const compareText = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
197
|
+
const compareDigits = (a, b) => {
|
|
198
|
+
const x = a.replace(/^0+(?=\d)/, "");
|
|
199
|
+
const y = b.replace(/^0+(?=\d)/, "");
|
|
200
|
+
return x.length !== y.length ? sign(x.length - y.length) : compareText(x, y);
|
|
201
|
+
};
|
|
202
|
+
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-]+)*)?$/;
|
|
203
|
+
function parseSemver(text) {
|
|
204
|
+
const match = SEMVER.exec(text.trim().replace(/^=/, ""));
|
|
205
|
+
if (match === null) return void 0;
|
|
206
|
+
return {
|
|
207
|
+
core: [
|
|
208
|
+
match[1],
|
|
209
|
+
match[2],
|
|
210
|
+
match[3]
|
|
211
|
+
],
|
|
212
|
+
pre: match[4] === void 0 ? [] : match[4].split(".")
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function compareSemverParts(x, y) {
|
|
216
|
+
for (let i = 0; i < 3; i++) {
|
|
217
|
+
const c = compareDigits(x.core[i], y.core[i]);
|
|
218
|
+
if (c !== 0) return c;
|
|
219
|
+
}
|
|
220
|
+
if (x.pre.length === 0 || y.pre.length === 0) return sign(y.pre.length - x.pre.length);
|
|
221
|
+
for (let i = 0; i < Math.min(x.pre.length, y.pre.length); i++) {
|
|
222
|
+
const p = x.pre[i];
|
|
223
|
+
const q = y.pre[i];
|
|
224
|
+
const pn = /^\d+$/.test(p);
|
|
225
|
+
const qn = /^\d+$/.test(q);
|
|
226
|
+
const c = pn && qn ? compareDigits(p, q) : pn ? -1 : qn ? 1 : compareText(p, q);
|
|
227
|
+
if (c !== 0) return c;
|
|
228
|
+
}
|
|
229
|
+
return sign(x.pre.length - y.pre.length);
|
|
230
|
+
}
|
|
231
|
+
/** This function compares two SemVer 2.0.0 versions. It accepts a leading `v` or `=`, as npm does. */
|
|
232
|
+
function compareSemver(a, b) {
|
|
233
|
+
const x = parseSemver(a);
|
|
234
|
+
const y = parseSemver(b);
|
|
235
|
+
return x === void 0 || y === void 0 ? void 0 : compareSemverParts(x, y);
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* This function compares two versions of Go modules. A Go version starts with `v` and follows SemVer. A pseudo-version is
|
|
239
|
+
* a pre-release, so it has its place in the order of SemVer. The suffix `+incompatible` is build metadata.
|
|
240
|
+
*/
|
|
241
|
+
function compareGo(a, b) {
|
|
242
|
+
if (!a.startsWith("v") || !b.startsWith("v")) return void 0;
|
|
243
|
+
return compareSemver(a, b);
|
|
244
|
+
}
|
|
245
|
+
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;
|
|
246
|
+
const PRE_RANK = {
|
|
247
|
+
a: 0,
|
|
248
|
+
alpha: 0,
|
|
249
|
+
b: 1,
|
|
250
|
+
beta: 1,
|
|
251
|
+
c: 2,
|
|
252
|
+
rc: 2,
|
|
253
|
+
pre: 2,
|
|
254
|
+
preview: 2
|
|
255
|
+
};
|
|
256
|
+
function parsePep440(text) {
|
|
257
|
+
const m = PEP440.exec(text);
|
|
258
|
+
if (m === null) return void 0;
|
|
259
|
+
const release = m[2].split(".").map(Number);
|
|
260
|
+
while (release.length > 1 && release[release.length - 1] === 0) release.pop();
|
|
261
|
+
const post = m[5] !== void 0 ? Number(m[5]) : m[6] !== void 0 ? Number(m[7] ?? 0) : void 0;
|
|
262
|
+
return {
|
|
263
|
+
epoch: Number(m[1] ?? 0),
|
|
264
|
+
release,
|
|
265
|
+
...m[3] === void 0 ? {} : { pre: [PRE_RANK[m[3].toLowerCase()], Number(m[4] ?? 0)] },
|
|
266
|
+
...post === void 0 ? {} : { post },
|
|
267
|
+
...m[8] === void 0 ? {} : { dev: Number(m[9] ?? 0) },
|
|
268
|
+
local: m[10] === void 0 ? [] : m[10].toLowerCase().split(/[-_.]/)
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
function compareNumbers(a, b) {
|
|
272
|
+
for (let i = 0; i < Math.max(a.length, b.length); i++) {
|
|
273
|
+
const c = sign((a[i] ?? 0) - (b[i] ?? 0));
|
|
274
|
+
if (c !== 0) return c;
|
|
275
|
+
}
|
|
276
|
+
return 0;
|
|
277
|
+
}
|
|
278
|
+
/** This function compares two PEP 440 versions, with the order of the Python packaging specification. */
|
|
279
|
+
function comparePep440(a, b) {
|
|
280
|
+
const x = parsePep440(a);
|
|
281
|
+
const y = parsePep440(b);
|
|
282
|
+
if (x === void 0 || y === void 0) return void 0;
|
|
283
|
+
const epoch = sign(x.epoch - y.epoch);
|
|
284
|
+
if (epoch !== 0) return epoch;
|
|
285
|
+
const release = compareNumbers(x.release, y.release);
|
|
286
|
+
if (release !== 0) return release;
|
|
287
|
+
const preKey = (v) => v.pre !== void 0 ? [0, ...v.pre] : v.post === void 0 && v.dev !== void 0 ? [-1] : [1];
|
|
288
|
+
const pre = compareNumbers(preKey(x), preKey(y));
|
|
289
|
+
if (pre !== 0) return pre;
|
|
290
|
+
const postKey = (v) => v.post === void 0 ? [-1] : [0, v.post];
|
|
291
|
+
const post = compareNumbers(postKey(x), postKey(y));
|
|
292
|
+
if (post !== 0) return post;
|
|
293
|
+
const devKey = (v) => v.dev === void 0 ? [1] : [0, v.dev];
|
|
294
|
+
const dev = compareNumbers(devKey(x), devKey(y));
|
|
295
|
+
if (dev !== 0) return dev;
|
|
296
|
+
return compareLocal(x.local, y.local);
|
|
297
|
+
}
|
|
298
|
+
function compareLocal(a, b) {
|
|
299
|
+
for (let i = 0; i < Math.min(a.length, b.length); i++) {
|
|
300
|
+
const p = a[i];
|
|
301
|
+
const q = b[i];
|
|
302
|
+
const pn = /^\d+$/.test(p);
|
|
303
|
+
const qn = /^\d+$/.test(q);
|
|
304
|
+
const c = pn && qn ? compareDigits(p, q) : pn ? 1 : qn ? -1 : compareText(p, q);
|
|
305
|
+
if (c !== 0) return c;
|
|
306
|
+
}
|
|
307
|
+
return sign(a.length - b.length);
|
|
308
|
+
}
|
|
309
|
+
function debianRank(text, i) {
|
|
310
|
+
if (i >= text.length) return 0;
|
|
311
|
+
const c = text[i];
|
|
312
|
+
if (/d/.test(c)) return 0;
|
|
313
|
+
if (c === "~") return -1;
|
|
314
|
+
if (/[A-Za-z]/.test(c)) return c.charCodeAt(0);
|
|
315
|
+
return c.charCodeAt(0) + 256;
|
|
316
|
+
}
|
|
317
|
+
function compareDebianPart(a, b) {
|
|
318
|
+
let i = 0;
|
|
319
|
+
let j = 0;
|
|
320
|
+
while (i < a.length || j < b.length) {
|
|
321
|
+
while (i < a.length && !/\d/.test(a[i]) || j < b.length && !/\d/.test(b[j])) {
|
|
322
|
+
const c = debianRank(a, i) - debianRank(b, j);
|
|
323
|
+
if (c !== 0) return sign(c);
|
|
324
|
+
i++;
|
|
325
|
+
j++;
|
|
326
|
+
}
|
|
327
|
+
let p = "";
|
|
328
|
+
let q = "";
|
|
329
|
+
while (i < a.length && /\d/.test(a[i])) p += a[i++];
|
|
330
|
+
while (j < b.length && /\d/.test(b[j])) q += b[j++];
|
|
331
|
+
const c = compareDigits(p || "0", q || "0");
|
|
332
|
+
if (c !== 0) return c;
|
|
333
|
+
}
|
|
334
|
+
return 0;
|
|
335
|
+
}
|
|
336
|
+
const DEBIAN = /^(?:(\d+):)?([0-9][A-Za-z0-9.+~:-]*?)(?:-([A-Za-z0-9.+~]+))?$/;
|
|
337
|
+
/** This function compares two Debian versions (`[epoch:]upstream[-revision]`) with the order of dpkg. */
|
|
338
|
+
function compareDebian(a, b) {
|
|
339
|
+
const x = DEBIAN.exec(a.trim());
|
|
340
|
+
const y = DEBIAN.exec(b.trim());
|
|
341
|
+
if (x === null || y === null) return void 0;
|
|
342
|
+
const epoch = compareDigits(x[1] ?? "0", y[1] ?? "0");
|
|
343
|
+
if (epoch !== 0) return epoch;
|
|
344
|
+
const upstream = compareDebianPart(x[2], y[2]);
|
|
345
|
+
if (upstream !== 0) return upstream;
|
|
346
|
+
return compareDebianPart(x[3] ?? "0", y[3] ?? "0");
|
|
347
|
+
}
|
|
348
|
+
function rpmvercmp(a, b) {
|
|
349
|
+
if (a === b) return 0;
|
|
350
|
+
let i = 0;
|
|
351
|
+
let j = 0;
|
|
352
|
+
for (;;) {
|
|
353
|
+
while (i < a.length && !/[A-Za-z0-9~^]/.test(a[i])) i++;
|
|
354
|
+
while (j < b.length && !/[A-Za-z0-9~^]/.test(b[j])) j++;
|
|
355
|
+
const ca = a[i];
|
|
356
|
+
const cb = b[j];
|
|
357
|
+
if (ca === "~" || cb === "~") {
|
|
358
|
+
if (ca !== "~") return 1;
|
|
359
|
+
if (cb !== "~") return -1;
|
|
360
|
+
i++;
|
|
361
|
+
j++;
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
if (ca === "^" || cb === "^") {
|
|
365
|
+
if (ca === void 0) return -1;
|
|
366
|
+
if (cb === void 0) return 1;
|
|
367
|
+
if (ca !== "^") return 1;
|
|
368
|
+
if (cb !== "^") return -1;
|
|
369
|
+
i++;
|
|
370
|
+
j++;
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
if (ca === void 0 || cb === void 0) break;
|
|
374
|
+
const digits = /\d/.test(ca);
|
|
375
|
+
const same = digits ? /\d/ : /[A-Za-z]/;
|
|
376
|
+
let p = "";
|
|
377
|
+
let q = "";
|
|
378
|
+
while (i < a.length && same.test(a[i])) p += a[i++];
|
|
379
|
+
while (j < b.length && same.test(b[j])) q += b[j++];
|
|
380
|
+
if (q === "") return digits ? 1 : -1;
|
|
381
|
+
const c = digits ? compareDigits(p, q) : compareText(p, q);
|
|
382
|
+
if (c !== 0) return c;
|
|
383
|
+
}
|
|
384
|
+
if (i >= a.length && j >= b.length) return 0;
|
|
385
|
+
return i >= a.length ? -1 : 1;
|
|
386
|
+
}
|
|
387
|
+
const RPM = /^(?:(\d+):)?([^-]+)(?:-([^-]+))?$/;
|
|
388
|
+
/** This function compares two RPM versions (`[epoch:]version[-release]`) with the order of `rpmvercmp`. */
|
|
389
|
+
function compareRpm(a, b) {
|
|
390
|
+
const x = RPM.exec(a.trim());
|
|
391
|
+
const y = RPM.exec(b.trim());
|
|
392
|
+
if (x === null || y === null) return void 0;
|
|
393
|
+
const epoch = compareDigits(x[1] ?? "0", y[1] ?? "0");
|
|
394
|
+
if (epoch !== 0) return epoch;
|
|
395
|
+
const version = rpmvercmp(x[2], y[2]);
|
|
396
|
+
if (version !== 0) return version;
|
|
397
|
+
return rpmvercmp(x[3] ?? "", y[3] ?? "");
|
|
398
|
+
}
|
|
399
|
+
// src/osv.ts
|
|
400
|
+
/**
|
|
401
|
+
* The map from the ecosystems of OSV (`https://ossf.github.io/osv-schema/`) to purl types, and the purl of a package
|
|
402
|
+
* of an OSV record.
|
|
403
|
+
*/
|
|
404
|
+
/**
|
|
405
|
+
* The purl type of each OSV ecosystem that has one. An ecosystem that is not in the table has no purl.
|
|
406
|
+
* For a Linux distribution, the table key is the part of the ecosystem before the first `:`.
|
|
407
|
+
*/
|
|
408
|
+
const OSV_PURL_TYPES = {
|
|
409
|
+
npm: "npm",
|
|
410
|
+
PyPI: "pypi",
|
|
411
|
+
"crates.io": "cargo",
|
|
412
|
+
RubyGems: "gem",
|
|
413
|
+
NuGet: "nuget",
|
|
414
|
+
Go: "golang",
|
|
415
|
+
Maven: "maven",
|
|
416
|
+
Packagist: "composer",
|
|
417
|
+
Pub: "pub",
|
|
418
|
+
Hex: "hex",
|
|
419
|
+
Hackage: "hackage",
|
|
420
|
+
CRAN: "cran",
|
|
421
|
+
"GitHub Actions": "github",
|
|
422
|
+
Debian: "deb",
|
|
423
|
+
Ubuntu: "deb",
|
|
424
|
+
Alpine: "apk"
|
|
425
|
+
};
|
|
426
|
+
/** The purl namespace of each OSV ecosystem of a Linux distribution: the name of the distribution. */
|
|
427
|
+
const DISTRIBUTION = {
|
|
428
|
+
Debian: "debian",
|
|
429
|
+
Ubuntu: "ubuntu",
|
|
430
|
+
Alpine: "alpine"
|
|
431
|
+
};
|
|
432
|
+
/** This function gives the namespace and the name of an OSV package name, as the purl type of its ecosystem splits them. */
|
|
433
|
+
function split(type, name) {
|
|
434
|
+
switch (type) {
|
|
435
|
+
case "npm": {
|
|
436
|
+
const slash = name.indexOf("/");
|
|
437
|
+
return name.startsWith("@") && slash > 1 ? {
|
|
438
|
+
namespace: name.slice(0, slash),
|
|
439
|
+
name: name.slice(slash + 1)
|
|
440
|
+
} : { name };
|
|
441
|
+
}
|
|
442
|
+
case "golang":
|
|
443
|
+
case "composer":
|
|
444
|
+
case "github": {
|
|
445
|
+
const slash = name.lastIndexOf("/");
|
|
446
|
+
return slash > 0 ? {
|
|
447
|
+
namespace: name.slice(0, slash),
|
|
448
|
+
name: name.slice(slash + 1)
|
|
449
|
+
} : { name };
|
|
450
|
+
}
|
|
451
|
+
case "maven": {
|
|
452
|
+
const colon = name.indexOf(":");
|
|
453
|
+
return colon > 0 ? {
|
|
454
|
+
namespace: name.slice(0, colon),
|
|
455
|
+
name: name.slice(colon + 1)
|
|
456
|
+
} : void 0;
|
|
457
|
+
}
|
|
458
|
+
default: return { name };
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* This function gives the canonical purl of an OSV package, with no version. For an ecosystem with no purl type, or
|
|
463
|
+
* a bad name, it gives `undefined`.
|
|
464
|
+
*/
|
|
465
|
+
function osvPurl(ecosystem, name) {
|
|
466
|
+
const base = ecosystem.split(":")[0];
|
|
467
|
+
const type = Object.hasOwn(OSV_PURL_TYPES, base) ? OSV_PURL_TYPES[base] : void 0;
|
|
468
|
+
if (type === void 0 || name === "") return void 0;
|
|
469
|
+
const names = split(type, name);
|
|
470
|
+
if (names === void 0) return void 0;
|
|
471
|
+
const namespace = (Object.hasOwn(DISTRIBUTION, base) ? DISTRIBUTION[base] : void 0) ?? names.namespace;
|
|
472
|
+
return formatPurl({
|
|
473
|
+
type,
|
|
474
|
+
...namespace === void 0 ? {} : { namespace },
|
|
475
|
+
name: names.name
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* This function gives the purl of a package of an OSV record, with no version, qualifiers or subpath. A valid purl
|
|
480
|
+
* of the record wins. Else the function builds the purl from the ecosystem and the name.
|
|
481
|
+
*/
|
|
482
|
+
function osvPackagePurl(pkg) {
|
|
483
|
+
return (pkg.purl === void 0 ? void 0 : packagePurlOf(pkg.purl)) ?? osvPurl(pkg.ecosystem, pkg.name);
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* This function gives the `vers` range of an OSV range. A range of the type `SEMVER` has the scheme `semver`. A range
|
|
487
|
+
* of the type `ECOSYSTEM` has the purl type of its ecosystem as its scheme. A `GIT` range, an ecosystem with no purl type
|
|
488
|
+
* or an event with no version gives `undefined`.
|
|
489
|
+
*
|
|
490
|
+
* The events come in the order of their versions, as the OSV schema tells, when the scheme has a known order. Else they
|
|
491
|
+
* keep the order of the record. The event `introduced 0` is the start of each version, so it gives no constraint.
|
|
492
|
+
*/
|
|
493
|
+
function osvRangeVers(ecosystem, range) {
|
|
494
|
+
const base = ecosystem.split(":")[0];
|
|
495
|
+
const type = Object.hasOwn(OSV_PURL_TYPES, base) ? OSV_PURL_TYPES[base] : void 0;
|
|
496
|
+
const scheme = range.type === "SEMVER" ? "semver" : range.type === "ECOSYSTEM" ? type : void 0;
|
|
497
|
+
if (scheme === void 0) return void 0;
|
|
498
|
+
const constraints = [];
|
|
499
|
+
let all = false;
|
|
500
|
+
for (const event of range.events) {
|
|
501
|
+
const constraint = eventConstraint(event);
|
|
502
|
+
if (constraint === void 0) return void 0;
|
|
503
|
+
if (constraint === "start") all = true;
|
|
504
|
+
else if (constraint !== "none") constraints.push(constraint);
|
|
505
|
+
}
|
|
506
|
+
if (constraints.length === 0) return all ? {
|
|
507
|
+
scheme,
|
|
508
|
+
all: true,
|
|
509
|
+
constraints: []
|
|
510
|
+
} : void 0;
|
|
511
|
+
return {
|
|
512
|
+
scheme,
|
|
513
|
+
all: false,
|
|
514
|
+
constraints: sortEvents(scheme, constraints)
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
function eventConstraint(event) {
|
|
518
|
+
if (event.introduced !== void 0) return event.introduced === "0" ? "start" : {
|
|
519
|
+
comparator: ">=",
|
|
520
|
+
version: event.introduced
|
|
521
|
+
};
|
|
522
|
+
if (event.fixed !== void 0) return {
|
|
523
|
+
comparator: "<",
|
|
524
|
+
version: event.fixed
|
|
525
|
+
};
|
|
526
|
+
if (event.last_affected !== void 0) return {
|
|
527
|
+
comparator: "<=",
|
|
528
|
+
version: event.last_affected
|
|
529
|
+
};
|
|
530
|
+
if (event.limit !== void 0) return event.limit === "*" ? "none" : {
|
|
531
|
+
comparator: "<",
|
|
532
|
+
version: event.limit
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
function sortEvents(scheme, constraints) {
|
|
536
|
+
if (versionScheme(scheme) === void 0) return [...constraints];
|
|
537
|
+
let unknown = false;
|
|
538
|
+
const sorted = [...constraints].sort((a, b) => {
|
|
539
|
+
const c = compareVersions(scheme, a.version, b.version);
|
|
540
|
+
if (c === void 0) unknown = true;
|
|
541
|
+
return c ?? 0;
|
|
542
|
+
});
|
|
543
|
+
return unknown ? [...constraints] : sorted;
|
|
544
|
+
}
|
|
545
|
+
// src/vers.ts
|
|
546
|
+
/**
|
|
547
|
+
* Version ranges in the `vers` format of the purl specification, for example `vers:npm/>=1.0.0|<1.2.3`. The functions
|
|
548
|
+
* parse a range, format it and tell if a version is in it, with the version order of the scheme.
|
|
549
|
+
*/
|
|
550
|
+
const COMPARATORS = [
|
|
551
|
+
"!=",
|
|
552
|
+
"<=",
|
|
553
|
+
">=",
|
|
554
|
+
"<",
|
|
555
|
+
">",
|
|
556
|
+
"="
|
|
557
|
+
];
|
|
558
|
+
/** This function reads one constraint, for example `>=1.0.0`. A constraint with no comparator has the comparator `=`. */
|
|
559
|
+
function parseConstraint(text) {
|
|
560
|
+
const trimmed = text.trim();
|
|
561
|
+
const comparator = COMPARATORS.find((c) => trimmed.startsWith(c)) ?? "=";
|
|
562
|
+
const raw = trimmed.slice(trimmed.startsWith(comparator) ? comparator.length : 0).trim();
|
|
563
|
+
if (raw === "") return void 0;
|
|
564
|
+
let version;
|
|
565
|
+
try {
|
|
566
|
+
version = decodeURIComponent(raw);
|
|
567
|
+
} catch {
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
return {
|
|
571
|
+
comparator,
|
|
572
|
+
version
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
/** This function reads a `vers` text. A text that is not a range gives `undefined`. */
|
|
576
|
+
function parseVers(text) {
|
|
577
|
+
const match = /^vers:([A-Za-z0-9.+-]+)\/(.+)$/.exec(text.replace(/\s+/g, ""));
|
|
578
|
+
if (match === null) return void 0;
|
|
579
|
+
const scheme = match[1].toLowerCase();
|
|
580
|
+
const body = match[2];
|
|
581
|
+
if (body === "*") return {
|
|
582
|
+
scheme,
|
|
583
|
+
all: true,
|
|
584
|
+
constraints: []
|
|
585
|
+
};
|
|
586
|
+
const constraints = [];
|
|
587
|
+
for (const part of body.split("|")) {
|
|
588
|
+
const constraint = parseConstraint(part);
|
|
589
|
+
if (constraint === void 0) return void 0;
|
|
590
|
+
constraints.push(constraint);
|
|
591
|
+
}
|
|
592
|
+
return {
|
|
593
|
+
scheme,
|
|
594
|
+
all: false,
|
|
595
|
+
constraints
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
const encodeVersion = (version) => version.replace(/[^A-Za-z0-9.+~_:-]/g, (c) => encodeURIComponent(c));
|
|
599
|
+
/**
|
|
600
|
+
* This function gives the `vers` text of a range. The constraints come in the order of the versions, when the scheme
|
|
601
|
+
* has a known order. The comparator `=` has no sign, as the specification tells.
|
|
602
|
+
*/
|
|
603
|
+
function formatVers(vers) {
|
|
604
|
+
if (vers.all) return `vers:${vers.scheme}/*`;
|
|
605
|
+
const body = (sortConstraints(vers.scheme, vers.constraints) ?? vers.constraints).map((c) => `${c.comparator === "=" ? "" : c.comparator}${encodeVersion(c.version)}`).join("|");
|
|
606
|
+
return `vers:${vers.scheme}/${body}`;
|
|
607
|
+
}
|
|
608
|
+
function sortConstraints(scheme, constraints) {
|
|
609
|
+
let unknown = false;
|
|
610
|
+
const sorted = [...constraints].sort((a, b) => {
|
|
611
|
+
const c = compareVersions(scheme, a.version, b.version);
|
|
612
|
+
if (c === void 0) unknown = true;
|
|
613
|
+
return c ?? 0;
|
|
614
|
+
});
|
|
615
|
+
return unknown ? void 0 : sorted;
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* This function tells if a version is in a range, with the algorithm of the `vers` specification. The result is
|
|
619
|
+
* `undefined` when the scheme has no known order, or when a version is not a version of the scheme. Thus a caller
|
|
620
|
+
* does not take an unknown result as a match or as no match.
|
|
621
|
+
*/
|
|
622
|
+
function versContains(vers, version) {
|
|
623
|
+
if (vers.all) return true;
|
|
624
|
+
const compare = (a, b) => compareVersions(vers.scheme, a, b);
|
|
625
|
+
for (const constraint of vers.constraints) {
|
|
626
|
+
const c = compare(version, constraint.version);
|
|
627
|
+
if (c === void 0) return void 0;
|
|
628
|
+
if (c === 0 && [
|
|
629
|
+
"=",
|
|
630
|
+
"<=",
|
|
631
|
+
">="
|
|
632
|
+
].includes(constraint.comparator)) return true;
|
|
633
|
+
if (c === 0 && constraint.comparator === "!=") return false;
|
|
634
|
+
}
|
|
635
|
+
const ranged = sortConstraints(vers.scheme, vers.constraints.filter((c) => c.comparator !== "=" && c.comparator !== "!="));
|
|
636
|
+
if (ranged === void 0) return void 0;
|
|
637
|
+
if (ranged.length === 0) return vers.constraints.some((c) => c.comparator === "!=");
|
|
638
|
+
const below = (c) => c.comparator === "<" || c.comparator === "<=";
|
|
639
|
+
const above = (c) => c.comparator === ">" || c.comparator === ">=";
|
|
640
|
+
const first = ranged[0];
|
|
641
|
+
const last = ranged[ranged.length - 1];
|
|
642
|
+
if (below(first) && compare(version, first.version) < 0) return true;
|
|
643
|
+
if (above(last) && compare(version, last.version) > 0) return true;
|
|
644
|
+
for (let i = 0; i + 1 < ranged.length; i++) {
|
|
645
|
+
const low = ranged[i];
|
|
646
|
+
const high = ranged[i + 1];
|
|
647
|
+
if (above(low) && below(high) && compare(version, low.version) > 0 && compare(version, high.version) < 0) return true;
|
|
648
|
+
}
|
|
649
|
+
return false;
|
|
650
|
+
}
|
|
651
|
+
/**
|
|
652
|
+
* This function gives the end of the part of a range that holds a version, when that end is a constraint `<`. For the
|
|
653
|
+
* range of an advisory, it is the first fixed version above the version. The result is `undefined` for a version out
|
|
654
|
+
* of the range, or for an unknown order. It is also `undefined` for a part with no such end: `<=`, an open end or `=`.
|
|
655
|
+
*/
|
|
656
|
+
function versEnd(vers, version) {
|
|
657
|
+
if (vers.all || versContains(vers, version) !== true) return void 0;
|
|
658
|
+
if (vers.constraints.some((c) => c.comparator === "=" && compareVersions(vers.scheme, version, c.version) === 0)) return void 0;
|
|
659
|
+
const next = sortConstraints(vers.scheme, vers.constraints.filter((c) => c.comparator !== "=" && c.comparator !== "!="))?.find((c) => compareVersions(vers.scheme, version, c.version) < 0);
|
|
660
|
+
return next?.comparator === "<" ? next.version : void 0;
|
|
661
|
+
}
|
|
662
|
+
export { OSV_PURL_TYPES, compareDebian, compareGo, comparePep440, compareRpm, compareSemver, compareVersions, formatPurl, formatVers, normalizePurl, osvPackagePurl, osvPurl, osvRangeVers, packagePurlOf, parsePurl, parseVers, versContains, versEnd, versionScheme };
|
|
663
|
+
|
|
664
|
+
//# sourceMappingURL=index.js.map
|