@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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mark Russell
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,61 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mark1russell7/package-id
|
|
2
2
|
|
|
3
|
-
|
|
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
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { compareVersions, formatPurl, osvPurl, packagePurlOf, parsePurl, parseVers, versContains } from "@mark1russell7/package-id";
|
|
7
|
+
|
|
8
|
+
parsePurl("pkg:npm/%40angular/animation@12.3.1");
|
|
9
|
+
// { type: "npm", namespace: "@angular", name: "animation", version: "12.3.1" }
|
|
10
|
+
|
|
11
|
+
formatPurl({ type: "pypi", name: "Django_package", version: "1.11.1" });
|
|
12
|
+
// "pkg:pypi/django-package@1.11.1"
|
|
13
|
+
|
|
14
|
+
packagePurlOf("pkg:npm/a@1.0.0?arch=x#sub");
|
|
15
|
+
// "pkg:npm/a"
|
|
16
|
+
|
|
17
|
+
osvPurl("Maven", "org.example:core");
|
|
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
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## The functions
|
|
28
|
+
|
|
29
|
+
| Function | What it gives |
|
|
30
|
+
| --- | --- |
|
|
31
|
+
| `parsePurl(text)` | The parts of a purl text, decoded and normalized, or `undefined` for a text that is not a purl. |
|
|
32
|
+
| `formatPurl(purl)` | The canonical text of a purl, or `undefined` when a part breaks a rule. |
|
|
33
|
+
| `normalizePurl(purl)` | The canonical parts: the case rules of the type, sorted qualifiers and no empty part. |
|
|
34
|
+
| `packagePurlOf(text)` | The canonical purl with no version, no qualifiers and no subpath. |
|
|
35
|
+
| `osvPurl(ecosystem, name)` | The purl of a package of an OSV ecosystem, with no version. |
|
|
36
|
+
| `osvPackagePurl(pkg)` | The purl of an OSV record package. A valid purl of the record wins. |
|
|
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
|
+
| `versionScheme(type)` | The version scheme of a purl type, or `undefined` when the order of the type is not known. |
|
|
41
|
+
| `parseVers(text)` | The scheme and the constraints of a `vers` range, or `undefined` for a text that is not a range. |
|
|
42
|
+
| `formatVers(vers)` | The `vers` text of a range, with the constraints in the order of their versions. |
|
|
43
|
+
| `versContains(vers, version)` | True when the version is in the range. The result is `undefined` when the order is not known. |
|
|
44
|
+
| `versEnd(vers, version)` | The end `<` of the part of the range that holds the version: for an advisory, the fixed version. |
|
|
45
|
+
|
|
46
|
+
## The canonical form
|
|
47
|
+
|
|
48
|
+
- The type is in lower case.
|
|
49
|
+
- For the types `alpm`, `apk`, `bitbucket`, `composer`, `deb`, `github`, `golang` and `npm`, the namespace and the name are in lower case.
|
|
50
|
+
- For the types `hex`, `pub` and `pypi`, the name is in lower case. A `pypi` name has `-` in place of `_`.
|
|
51
|
+
- The qualifier keys are in lower case and in sorted order. A qualifier with an empty value is not in the purl.
|
|
52
|
+
- The subpath has no empty, `.` or `..` segment.
|
|
53
|
+
- Each part is percent-encoded, but a colon stays as it is. A qualifier value keeps its `/`.
|
|
54
|
+
|
|
55
|
+
## An unknown order
|
|
56
|
+
|
|
57
|
+
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. Thus a caller that matches advisories to installed versions does not take an unknown result as a match or as no match.
|
|
58
|
+
|
|
59
|
+
## License
|
|
60
|
+
|
|
61
|
+
MIT
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
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
|
+
* no fetch.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Package URLs (purl): parse, format and normalize, as the purl specification tells
|
|
7
|
+
* (`https://github.com/package-url/purl-spec`). The form is `pkg:type/namespace/name@version?qualifiers#subpath`.
|
|
8
|
+
*
|
|
9
|
+
* The functions are plain functions with no fetch and no state.
|
|
10
|
+
*/
|
|
11
|
+
/** A package URL as its parts. Each text part is decoded: it has no percent-encoding. */
|
|
12
|
+
interface Purl {
|
|
13
|
+
/** The type of the package, in lower case, for example `npm` or `pypi`. */
|
|
14
|
+
readonly type: string;
|
|
15
|
+
/** The namespace, with `/` between its segments, or `undefined` when the purl has no namespace. */
|
|
16
|
+
readonly namespace?: string;
|
|
17
|
+
/** The name of the package. */
|
|
18
|
+
readonly name: string;
|
|
19
|
+
/** The version, or `undefined` when the purl has no version. */
|
|
20
|
+
readonly version?: string;
|
|
21
|
+
/** The qualifiers, with lower-case keys. A qualifier with an empty value is not in the record. */
|
|
22
|
+
readonly qualifiers?: Readonly<Record<string, string>>;
|
|
23
|
+
/** The subpath, with `/` between its segments, or `undefined` when the purl has no subpath. */
|
|
24
|
+
readonly subpath?: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* This function gives the canonical parts of a purl. The type and the qualifier keys are in lower case. The parts
|
|
28
|
+
* follow the case rules of the type, and no part is empty.
|
|
29
|
+
* It gives `undefined` when a part breaks a rule of the specification.
|
|
30
|
+
*/
|
|
31
|
+
export declare function normalizePurl(purl: Purl): Purl | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* This function reads a purl text into its parts, as the parse steps of the specification tell, and normalizes them.
|
|
34
|
+
* It gives `undefined` when the text is not a valid purl.
|
|
35
|
+
*/
|
|
36
|
+
export declare function parsePurl(text: string): Purl | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* This function writes the canonical text of a purl: the parts normalized, each part percent-encoded and the
|
|
39
|
+
* qualifiers in the order of their keys. It gives `undefined` when a part breaks a rule of the specification.
|
|
40
|
+
*/
|
|
41
|
+
export declare function formatPurl(purl: Purl): string | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* This function gives the canonical purl of the package with no version, no qualifiers and no subpath. A person who
|
|
44
|
+
* names a package with this text names each version of it. It gives `undefined` when the text is not a valid purl.
|
|
45
|
+
*/
|
|
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;
|
|
81
|
+
/**
|
|
82
|
+
* The purl type of each OSV ecosystem that has one. An ecosystem that is not in the table has no purl.
|
|
83
|
+
* For a Linux distribution, the table key is the part of the ecosystem before the first `:`.
|
|
84
|
+
*/
|
|
85
|
+
export declare const OSV_PURL_TYPES: Readonly<Record<string, string>>;
|
|
86
|
+
/**
|
|
87
|
+
* This function gives the canonical purl of an OSV package, with no version. For an ecosystem with no purl type, or
|
|
88
|
+
* a bad name, it gives `undefined`.
|
|
89
|
+
*/
|
|
90
|
+
export declare function osvPurl(ecosystem: string, name: string): string | undefined;
|
|
91
|
+
/**
|
|
92
|
+
* This function gives the purl of a package of an OSV record, with no version, qualifiers or subpath. A valid purl
|
|
93
|
+
* of the record wins. Else the function builds the purl from the ecosystem and the name.
|
|
94
|
+
*/
|
|
95
|
+
export declare function osvPackagePurl(pkg: {
|
|
96
|
+
readonly ecosystem: string;
|
|
97
|
+
readonly name: string;
|
|
98
|
+
readonly purl?: string;
|
|
99
|
+
}): string | undefined;
|
|
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. Each function compares two
|
|
123
|
+
* versions of one scheme. A text that is not a version of the scheme gives `undefined`, not a guess.
|
|
124
|
+
*/
|
|
125
|
+
/** The version schemes that this module compares. */
|
|
126
|
+
type VersionScheme = "semver" | "golang" | "pypi" | "deb" | "rpm";
|
|
127
|
+
/** This function gives the version scheme of a purl type, or `undefined` for a type whose order this module does not know. */
|
|
128
|
+
export declare function versionScheme(type: string): VersionScheme | undefined;
|
|
129
|
+
/**
|
|
130
|
+
* This function compares two versions of one scheme. The result is below 0 when `a` comes before `b`, 0 when they are
|
|
131
|
+
* equal and above 0 when `a` comes after `b`. When a text is not a version of the scheme, the result is `undefined`.
|
|
132
|
+
*/
|
|
133
|
+
export declare function compareVersions(scheme: string, a: string, b: string): number | undefined;
|
|
134
|
+
/** This function compares two SemVer 2.0.0 versions. It accepts a leading `v` or `=`, as npm does. */
|
|
135
|
+
export declare function compareSemver(a: string, b: string): number | undefined;
|
|
136
|
+
/**
|
|
137
|
+
* This function compares two versions of Go modules. A Go version starts with `v` and follows SemVer. A pseudo-version is
|
|
138
|
+
* a pre-release, so it has its place in the order of SemVer. The suffix `+incompatible` is build metadata.
|
|
139
|
+
*/
|
|
140
|
+
export declare function compareGo(a: string, b: string): number | undefined;
|
|
141
|
+
/** This function compares two PEP 440 versions, with the order of the Python packaging specification. */
|
|
142
|
+
export declare function comparePep440(a: string, b: string): number | undefined;
|
|
143
|
+
/** This function compares two Debian versions (`[epoch:]upstream[-revision]`) with the order of dpkg. */
|
|
144
|
+
export declare function compareDebian(a: string, b: string): number | undefined;
|
|
145
|
+
/** This function compares two RPM versions (`[epoch:]version[-release]`) with the order of `rpmvercmp`. */
|
|
146
|
+
export declare function compareRpm(a: string, b: string): number | undefined;
|
|
147
|
+
export type { OsvEvent, OsvRange, Purl, Vers, VersComparator, VersConstraint, VersionScheme };
|
|
148
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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;;;;;;KCjGtD;;wBAcI,cAAc,eAAe;;;;;wBAQ7B,gBAAgB,gBAAgB,WAAW;;wBA6D3C,cAAc,WAAW;;;;;wBAUzB,UAAU,WAAW;;wBA+CrB,cAAc,WAAW;;wBAqEzB,cAAc,WAAW;;wBAyDzB,WAAW,WAAW"}
|