@preventive/upstream 1.12.0 → 1.13.1

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.
Files changed (52) hide show
  1. package/advisories.d.ts +125 -89
  2. package/cargo.d.ts +59 -35
  3. package/cocoapods.d.ts +44 -57
  4. package/electron.d.ts +35 -25
  5. package/git.d.ts +25 -16
  6. package/github/write.d.ts +60 -29
  7. package/github.d.ts +207 -193
  8. package/npm.d.ts +151 -87
  9. package/package.d.ts +30 -16
  10. package/package.json +1 -1
  11. package/rubygems.d.ts +22 -9
  12. package/semver.d.ts +40 -19
  13. package/soldeer.d.ts +21 -12
  14. package/src/advisories/actions.js +13 -38
  15. package/src/advisories/common.js +29 -1
  16. package/src/advisories/github.js +5 -19
  17. package/src/advisories/index.js +3 -10
  18. package/src/advisories/npm.js +4 -19
  19. package/src/advisories/osv.js +19 -41
  20. package/src/advisories/packagist.js +1 -4
  21. package/src/advisories/patched.js +6 -18
  22. package/src/advisories/repos.js +8 -13
  23. package/src/advisories/rubysec.js +2 -7
  24. package/src/args.js +5 -10
  25. package/src/attributes.js +3 -4
  26. package/src/cache.js +9 -22
  27. package/src/cargo/crate.js +4 -7
  28. package/src/cargo/repos.js +1 -7
  29. package/src/cocoapods.js +3 -19
  30. package/src/download.js +4 -12
  31. package/src/electron.js +6 -8
  32. package/src/github/commit.js +1 -13
  33. package/src/github/read.js +19 -68
  34. package/src/github/write.js +0 -1
  35. package/src/http.js +2 -7
  36. package/src/lockfiles.js +3 -11
  37. package/src/npm/cacache.js +3 -8
  38. package/src/npm/registry.js +12 -4
  39. package/src/npm/repos.js +3 -15
  40. package/src/npm/tarball.js +5 -20
  41. package/src/npm/versions.js +7 -21
  42. package/src/package.js +1 -7
  43. package/src/pool.js +0 -1
  44. package/src/pypi/version.js +1 -5
  45. package/src/remote.js +4 -6
  46. package/src/rubygems/gem.js +2 -2
  47. package/src/rubygems/version.js +2 -10
  48. package/src/semver.js +3 -9
  49. package/src/soldeer.js +0 -4
  50. package/src/tree.js +14 -44
  51. package/src/weak-edges.js +0 -2
  52. package/weak-edges.d.ts +36 -24
package/npm.d.ts CHANGED
@@ -1,110 +1,174 @@
1
- // Names and versions are checked before any request. A scoped package is
2
- // read with PREVENTIVE_MEASURES_NPM_TOKEN, STASIS_NPM_TOKEN or NPM_TOKEN,
3
- // the first set.
4
-
5
- // The platform's cache directory for a program called `name` (6 to 32 of
6
- // a-z, A-Z, 0-9, `_`, `-`); undefined without an absolute home. Nothing is made.
7
- export function cacheDirFor(name: string): string | undefined
8
-
9
- // cacheDirFor('PreventiveMeasures'), where setCacheDir() caches by default.
10
- export const defaultCacheDir: string | undefined
11
-
12
- // Where cached answers are kept: `dir`, else defaultCacheDir (which throws
13
- // where there is none); `false` turns the cache off. What's cached is
14
- // trusted, so point it only at storage nothing less trusted can write.
15
- export function setCacheDir(dir?: string | false): void
16
-
17
- // A caller's store in place of setCacheDir's: `read` answers what `write`
18
- // last stored under `type` and `key`, or null. Values are JSON, a tarball's
19
- // a Uint8Array; it is trusted as setCacheDir's directory is.
1
+ /**
2
+ * npm's registry: versions' documents and tarballs, held to their integrity, and packages' GitHub repos
3
+ *
4
+ * ```js
5
+ * import { getMeta, getTarball, resolvePackageRepos, setCacheDir } from '@preventive/upstream/npm.js'
6
+ * ```
7
+ *
8
+ * `setCacheDir()` sets where every module of this package caches: nothing is written to a cache until it is called.
9
+ * Names and versions are checked before any request. A scoped package is read with its scope's own token where it
10
+ * is set, `ACME_CORP_NPM_TOKEN`, else `NPM_TOKEN_ACME_CORP`, for `@acme-corp/x` (`.` and `-` read as `_`;
11
+ * `PREVENTIVE_MEASURES_NPM_TOKEN` and `STASIS_NPM_TOKEN` are for every scope, not `@preventive-measures`'s or
12
+ * `@stasis`'s own), else with `PREVENTIVE_MEASURES_NPM_TOKEN`, `STASIS_NPM_TOKEN` or `NPM_TOKEN`, the first set.
13
+ *
14
+ * @module @preventive/upstream/npm.js
15
+ */
16
+
17
+ /**
18
+ * A caller's store in place of `setCacheDir()`'s directory, and as trusted
19
+ *
20
+ * `read` answers what `write` last stored under `type` and `key`, or null. Values are JSON values, a download's a
21
+ * `Uint8Array`.
22
+ */
20
23
  export interface CacheStore {
21
- read(type: string, key: string): Promise<unknown>
22
- write(type: string, key: string, value: unknown): Promise<void>
24
+ read(type: string, key: string): Promise<unknown>;
25
+ write(type: string, key: string, value: unknown): Promise<void>;
23
26
  }
24
27
 
25
- // setCacheDir's cache where `cache` is left out, a caller's store in its
26
- // place, or `false` for none.
28
+ /** Where a call caches */
27
29
  export interface CacheOptions {
28
- cache?: CacheStore | false
30
+ /** Default: `setCacheDir()`'s cache. A store takes its place; `false` still reads it, but writes nothing. */
31
+ cache?: CacheStore | false;
29
32
  }
30
33
 
31
- // A failed request, by its HTTP status: 404 for a package or version the
32
- // registry hasn't.
33
- export class HttpError extends Error {
34
- name: 'HttpError'
35
- status: number
36
- headers?: Headers
34
+ /** A version's tarball and its integrity, as a lockfile records them */
35
+ export interface Dist {
36
+ /** Exactly the registry's own URL for the version's tarball */
37
+ tarball: string;
38
+ /** One `sha512-<base64>` */
39
+ integrity: string;
37
40
  }
38
41
 
39
- // A package's GitHub repo as its `latest` document names it, and where in it
40
- // the package sits (`''` the root). Throws where it names none.
41
- export interface GitHubLink {
42
- github: string
43
- directory?: string
44
- url: string
42
+ /** What `getMeta()` may answer from, and where it caches */
43
+ export interface MetaOptions extends CacheOptions {
44
+ /** One the caller already has, as a lockfile gives it */
45
+ dist?: Dist;
45
46
  }
46
- export function getGitHub(name: string): Promise<GitHubLink>
47
47
 
48
- export interface PackageRepo {
49
- github: string
50
- directory?: string
48
+ /** `repository` given as an object */
49
+ export interface PackageRepository {
50
+ type: string | undefined;
51
+ url: string | undefined;
52
+ directory: string | undefined;
51
53
  }
52
- // A name that resolves to no repo is left out. `cachedOnly` never fetches.
53
- export function resolvePackageRepos(packageNames: Iterable<string>, options?: { cachedOnly?: boolean }): Promise<Map<string, PackageRepo>>
54
-
55
- // resolvePackageRepos' disk cache, a month an entry: a read answers null for
56
- // nothing usable, a write false where it couldn't write.
57
- export function readPackageRepoCache(name: string): Promise<PackageRepo | null>
58
- export function writePackageRepoCache(name: string, github: string, directory?: string): Promise<boolean>
59
54
 
60
- // Exactly a version's own tarball URL and one `sha512-<base64>`.
61
- export interface Dist {
62
- tarball: string
63
- integrity: string
55
+ /** `bugs` given as an object */
56
+ export interface PackageBugs {
57
+ url: string | undefined;
58
+ email: string | undefined;
64
59
  }
65
60
 
66
- // All of it the publisher's word, held to no repository or tarball. A field
67
- // missing or not of its shape is absent.
61
+ /**
62
+ * A version's registry document, as far as it is read here
63
+ *
64
+ * All of it the publisher's word, held to no repository or tarball. A field missing or not of its shape is absent.
65
+ */
68
66
  export interface PackageMeta {
69
- name: string
70
- version: string
71
- dist: Dist
72
- gitHead?: string
73
- repository?: string | PackageRepository
74
- homepage?: string
75
- bugs?: string | PackageBugs
76
- unpackedSize?: number
67
+ name: string;
68
+ version: string;
69
+ dist: Dist;
70
+ gitHead?: string;
71
+ repository?: string | PackageRepository;
72
+ homepage?: string;
73
+ bugs?: string | PackageBugs;
74
+ unpackedSize?: number;
77
75
  }
78
76
 
79
- export interface PackageRepository {
80
- type: string | undefined
81
- url: string | undefined
82
- directory: string | undefined
77
+ /** Where a package's GitHub repo is */
78
+ export interface GitHubLink {
79
+ /** `owner/name` */
80
+ github: string;
81
+ /** Where in the repo the package sits, `''` at its root; absent where unknown */
82
+ directory?: string;
83
+ /** `https://github.com/<github>` */
84
+ url: string;
83
85
  }
84
86
 
85
- export interface PackageBugs {
86
- url: string | undefined
87
- email: string | undefined
87
+ /** Where a package's GitHub repo is, as `resolvePackageRepos()` finds it */
88
+ export interface PackageRepo {
89
+ /** `owner/name` */
90
+ github: string;
91
+ /** Where in the repo the package sits, `''` at its root; absent where unknown */
92
+ directory?: string;
88
93
  }
89
94
 
90
- // `dist`: one the caller already has, as a lockfile gives it.
91
- export interface MetaOptions extends CacheOptions {
92
- dist?: Dist
95
+ /** A failed request: `status` is the HTTP status, 404 for a package or version the registry hasn't */
96
+ export class HttpError extends Error {
97
+ name: 'HttpError';
98
+ status: number;
99
+ headers?: Headers;
93
100
  }
94
101
 
95
- // A version's registry document, refused unless it is for that name and
96
- // version. With `dist`, npm's own cache or ours may answer with no request;
97
- // without one, the registry is always asked. Kept for good.
98
- export function getMeta(name: string, version: string, options?: MetaOptions): Promise<PackageMeta>
99
-
100
- // When each version was published, as Date#toISOString writes it; versions
101
- // without a valid time are left out. Reads the whole package's document.
102
- export function getPublishTimes(name: string): Promise<Map<string, string>>
103
-
104
- // Throws unless that version's integrity is the one given.
105
- export function verifyDist(name: string, version: string, dist: Dist, options?: CacheOptions): Promise<void>
106
-
107
- // A published version's gzipped tarball, checked against its integrity. npm's
108
- // and other tools' caches are read first, never written; setCacheDir's (or
109
- // `cache`) is written.
110
- export function getTarball(name: string, version: string, dist?: Dist, options?: CacheOptions): Promise<Uint8Array>
102
+ /**
103
+ * The platform's cache directory for a program called `name`, or undefined without an absolute home
104
+ *
105
+ * Nothing is made.
106
+ *
107
+ * @param name - 6 to 32 of a-z, A-Z, 0-9, `_` and `-`
108
+ */
109
+ export function cacheDirFor(name: string): string | undefined;
110
+
111
+ /** `cacheDirFor('PreventiveMeasures')`, where `setCacheDir()` caches by default */
112
+ export const defaultCacheDir: string | undefined;
113
+
114
+ /**
115
+ * Set where this package caches, `false` for nowhere
116
+ *
117
+ * What is cached is trusted, so point it only at storage nothing less trusted can write.
118
+ *
119
+ * @param dir - Default: `defaultCacheDir`, which throws where there is none
120
+ */
121
+ export function setCacheDir(dir?: string | false): void;
122
+
123
+ /**
124
+ * A version's registry document, refused unless it is that version's and, where `dist` is given, has that `dist`
125
+ *
126
+ * With `dist`, npm's own cache or ours may answer with no request; without, the registry is always asked. Cached
127
+ * for good.
128
+ */
129
+ export function getMeta(name: string, version: string, options?: MetaOptions): Promise<PackageMeta>;
130
+
131
+ /**
132
+ * When each version was published, as `Date#toISOString` writes it; versions without a valid time are left out
133
+ *
134
+ * Reads the whole package's document, megabytes for some, on every call.
135
+ */
136
+ export function getPublishTimes(name: string): Promise<Map<string, string>>;
137
+
138
+ /** Throw unless `dist` is that version's on the registry; a cached document may confirm it with no request */
139
+ export function verifyDist(name: string, version: string, dist: Dist, options?: CacheOptions): Promise<void>;
140
+
141
+ /**
142
+ * A published version's gzipped tarball, held to its integrity
143
+ *
144
+ * npm's own caches, and this package's and stasis's default ones, are read first and never written: one there that
145
+ * does not match is passed over. One in `setCacheDir()`'s cache (or `cache`) that does not match throws. Without
146
+ * `dist`, the registry is asked for it on every call, tarball cached or not.
147
+ */
148
+ export function getTarball(name: string, version: string, dist?: Dist, options?: CacheOptions): Promise<Uint8Array>;
149
+
150
+ /**
151
+ * The GitHub repo a package's `latest` version names, and where in it the package sits
152
+ *
153
+ * Read as `getRepo()` (package.js) reads it, on every call.
154
+ *
155
+ * @throws Where it names none
156
+ */
157
+ export function getGitHub(name: string): Promise<GitHubLink>;
158
+
159
+ /**
160
+ * Each package's GitHub repo, as `getGitHub()` finds it, cached a month in `setCacheDir()`'s cache
161
+ *
162
+ * A name that resolves to no repo, or whose lookup fails, is left out. `cachedOnly` answers from the cache alone
163
+ * and never fetches.
164
+ */
165
+ export function resolvePackageRepos(
166
+ packageNames: Iterable<string>,
167
+ options?: { cachedOnly?: boolean }
168
+ ): Promise<Map<string, PackageRepo>>;
169
+
170
+ /** Read `resolvePackageRepos()`'s cache: null where it holds nothing usable, as for an entry over a month old */
171
+ export function readPackageRepoCache(name: string): Promise<PackageRepo | null>;
172
+
173
+ /** Write `resolvePackageRepos()`'s cache: false where it could not, as without `setCacheDir()` */
174
+ export function writePackageRepoCache(name: string, github: string, directory?: string): Promise<boolean>;
package/package.d.ts CHANGED
@@ -1,19 +1,33 @@
1
+ /**
2
+ * A package's GitHub repo, read off its package.json or a registry document of a version
3
+ *
4
+ * ```js
5
+ * import { getRepo } from '@preventive/upstream/package.js'
6
+ * ```
7
+ *
8
+ * @module @preventive/upstream/package.js
9
+ */
10
+
11
+ /** Where a package's GitHub repo is */
1
12
  export interface PackageRepoLink {
2
- // `owner/name`, by GitHub's own rules.
3
- github?: string
4
- // Where in that repo the package sits, `/`-separated, as the tree path
5
- // itself (a homepage URL's decoded; a `repository.directory`'s `\` read
6
- // as `/`, its empty and `.` parts dropped); `''` at its root, where
7
- // `repository.directory` declares it (`./`, `/`, `/.`, `.`, `''`). Absent
8
- // where unknown: neither field names a directory in it (one with a `..`
9
- // part names none), and whenever `github` is absent.
10
- directory?: string
11
- // `https://github.com/<github>`, whenever there is a `github`.
12
- url?: string
13
+ /** `owner/name`, by GitHub's own rules */
14
+ github?: string;
15
+ /**
16
+ * Where in the repo the package sits, as a tree path: `/`-separated, decoded, `''` at its root
17
+ *
18
+ * From `repository.directory`, else a homepage's `tree/<ref>/` path; absent where neither names one in the repo
19
+ * (a `..` names none).
20
+ */
21
+ directory?: string;
22
+ /** `https://github.com/<github>` */
23
+ url?: string;
13
24
  }
14
25
 
15
- // Read off `repository`, `bugs` and `homepage`, in that order, from a
16
- // package.json or the registry's document for a version of one. Both
17
- // fields absent where nothing in it names a GitHub repo; a throw only
18
- // for something that is not an object at all.
19
- export function getRepo(pkg: object): PackageRepoLink
26
+ /**
27
+ * The GitHub repo `pkg` names in `repository`, `bugs` or `homepage`, the first that does
28
+ *
29
+ * Empty where none does.
30
+ *
31
+ * @throws Unless `pkg` is an object, and not an array
32
+ */
33
+ export function getRepo(pkg: object): PackageRepoLink;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preventive/upstream",
3
- "version": "1.12.0",
3
+ "version": "1.13.1",
4
4
  "description": "Minimal npm, crates.io, RubyGems, Soldeer, CocoaPods trunk, GitHub, Electron release and git checkout clients",
5
5
  "type": "module",
6
6
  "exports": {
package/rubygems.d.ts CHANGED
@@ -1,12 +1,25 @@
1
- // rubygems.org's gems. Names, versions, platforms (`ruby` for none) and
2
- // checksums are checked before any request, and pass as
3
- // @preventive/lockfile's BundlerSpec has them.
1
+ /**
2
+ * rubygems.org: .gem files held to their checksums
3
+ *
4
+ * ```js
5
+ * import { getGem, verifyChecksum } from '@preventive/upstream/rubygems.js'
6
+ * ```
7
+ *
8
+ * Names, versions, platforms (`ruby` for none) and checksums are checked before any request, and pass as
9
+ * `@preventive/lockfile`'s `BundlerSpec` has them. A checksum is a sha256 in lowercase hex, bare or after `sha256=`.
10
+ *
11
+ * @module @preventive/upstream/rubygems.js
12
+ */
4
13
 
5
- export { HttpError } from './npm.js'
14
+ export { HttpError } from './npm.js';
6
15
 
7
- // Throws unless the compact index has `checksum` for that version and platform.
8
- export function verifyChecksum(name: string, version: string, platform: string, checksum: string): Promise<void>
16
+ /** Throw unless the compact index has `checksum` for that version and platform; nothing is cached */
17
+ export function verifyChecksum(name: string, version: string, platform: string, checksum: string): Promise<void>;
9
18
 
10
- // A published .gem, checked against `checksum`, else the compact index's.
11
- // RubyGems' own caches are read first; setCacheDir's (npm.js) is written.
12
- export function getGem(name: string, version: string, platform: string, checksum?: string): Promise<Uint8Array>
19
+ /**
20
+ * A published .gem, held to `checksum`, else to the compact index's
21
+ *
22
+ * RubyGems' own caches (each gem directory's `cache/`) are read first and never written; `setCacheDir()`'s (npm.js)
23
+ * is written.
24
+ */
25
+ export function getGem(name: string, version: string, platform: string, checksum?: string): Promise<Uint8Array>;
package/semver.d.ts CHANGED
@@ -1,19 +1,40 @@
1
- // npm's own semver, borrowed from the npm beside node and passed through
2
- // as it is; where there is no npm, the `semver` package, an optional peer
3
- // dependency. Without either, every call throws, except valid() on a plain
4
- // MAJOR.MINOR.PATCH with no options.
5
- export function satisfies(version: string, range: string, options?: { includePrerelease?: boolean; loose?: boolean }): boolean
6
- export function compareVersions(a: string, b: string, options?: { loose?: boolean }): -1 | 0 | 1
7
- // The version as semver spells it, or null: `v1.2.3` answers `1.2.3`.
8
- export function valid(version: unknown, options?: { loose?: boolean }): string | null
9
- // A string that valid() answers unchanged.
10
- export function isExactVersion(version: unknown): version is string
11
- // The range as semver normalizes it, or null where it is not one.
12
- export function validRange(range: unknown, options?: { loose?: boolean; includePrerelease?: boolean }): string | null
13
- // Whether some version is in both ranges; throws for one that is not a range.
14
- export function intersects(a: string, b: string, options?: { loose?: boolean; includePrerelease?: boolean }): boolean
15
- // The version as semver cleans it, spaces and a leading `=` or `v` dropped,
16
- // or null: ` =v1.2.3 ` answers `1.2.3`.
17
- export function clean(version: string, options?: { loose?: boolean } | boolean): string | null
18
- // The version's major, as semver reads it; throws for one that is not a version.
19
- export function major(version: string, options?: { loose?: boolean } | boolean): number
1
+ /**
2
+ * npm's own semver, borrowed from the npm beside node and passed through as it is
3
+ *
4
+ * ```js
5
+ * import { compareVersions, satisfies, valid } from '@preventive/upstream/semver.js'
6
+ * ```
7
+ *
8
+ * Where there is no npm, the `semver` package, an optional peer dependency. Without either, every call throws, but
9
+ * `valid()` and `isExactVersion()` on a plain `MAJOR.MINOR.PATCH` with no options.
10
+ *
11
+ * @module @preventive/upstream/semver.js
12
+ */
13
+
14
+ /** Whether `version` is in `range` */
15
+ export function satisfies(
16
+ version: string,
17
+ range: string,
18
+ options?: { includePrerelease?: boolean; loose?: boolean }
19
+ ): boolean;
20
+
21
+ /** -1, 0 or 1 as `a` is below, equal to or above `b`; throws for one that is not a version */
22
+ export function compareVersions(a: string, b: string, options?: { loose?: boolean }): -1 | 0 | 1;
23
+
24
+ /** The version as semver spells it, or null: `v1.2.3` answers `1.2.3` */
25
+ export function valid(version: unknown, options?: { loose?: boolean }): string | null;
26
+
27
+ /** Whether `version` is a string `valid()` answers unchanged */
28
+ export function isExactVersion(version: unknown): version is string;
29
+
30
+ /** The range as semver normalizes it, or null where it is not one */
31
+ export function validRange(range: unknown, options?: { loose?: boolean; includePrerelease?: boolean }): string | null;
32
+
33
+ /** Whether some version is in both ranges; throws for one that is not a range */
34
+ export function intersects(a: string, b: string, options?: { loose?: boolean; includePrerelease?: boolean }): boolean;
35
+
36
+ /** The version as semver cleans it, spaces and a leading `=` or `v` dropped, or null: ` =v1.2.3 ` answers `1.2.3` */
37
+ export function clean(version: string, options?: { loose?: boolean } | boolean): string | null;
38
+
39
+ /** The version's major; throws for one that is not a version */
40
+ export function major(version: string, options?: { loose?: boolean } | boolean): number;
package/soldeer.d.ts CHANGED
@@ -1,14 +1,23 @@
1
- // The name is asserted to be one Soldeer takes, the version a string of
2
- // letters, digits, `.`, `_`, `+` and `-`, and the checksum 64 lowercase hex
3
- // digits, before any request or cache path. The cache is setCacheDir's
4
- // (npm.js).
1
+ /**
2
+ * Soldeer's registry: a version's zip, held to its checksum
3
+ *
4
+ * ```js
5
+ * import { getZip } from '@preventive/upstream/soldeer.js'
6
+ * ```
7
+ *
8
+ * @module @preventive/upstream/soldeer.js
9
+ */
5
10
 
6
- export { HttpError } from './npm.js'
11
+ export { HttpError } from './npm.js';
7
12
 
8
- // A version's zip from Soldeer's registry, whole, in memory. The registry
9
- // says where the zip is but not what it hashes to, so `checksum` is
10
- // required: the zip's sha256, as soldeer.lock records it. The bytes are
11
- // checked against it, whether they were downloaded (before they are
12
- // cached) or read from setCacheDir's cache, where one that does not match
13
- // throws. A cached zip that matches is answered with no request at all.
14
- export function getZip(name: string, version: string, checksum: string): Promise<Uint8Array>
13
+ /**
14
+ * A version's zip from Soldeer's registry, held to `checksum`
15
+ *
16
+ * Cached in `setCacheDir()`'s cache (npm.js), where a zip that does not match throws and one that matches is
17
+ * answered with no request at all.
18
+ *
19
+ * @param name - A name Soldeer takes
20
+ * @param version - Letters, digits, `.`, `_`, `+` and `-`
21
+ * @param checksum - The zip's sha256 in lowercase hex, as soldeer.lock records it
22
+ */
23
+ export function getZip(name: string, version: string, checksum: string): Promise<Uint8Array>;
@@ -1,16 +1,15 @@
1
1
  import { assertRepo, assertion, isRefName, isSha } from '../args.js'
2
- import { readRecord, writeRecord } from '../cache.js'
3
2
  import { GITHUB_WEB, HttpError, buildUrl, send } from '../http.js'
4
- import { pool } from '../pool.js'
5
3
  import { valid, validRange } from '../semver.js'
6
- import { inRange, overlap } from './common.js'
4
+ import { inRange, overlap, publicOnly } from './common.js'
7
5
  import { bySemver, coversPlaceholder, repositoryAdvisories } from './github.js'
8
- import { RECORDS_AT_ONCE, packageRecords, toAdvisory } from './osv.js'
6
+ import { RECORDS_AT_ONCE, isEntryOf, packageRecords, toAdvisory } from './osv.js'
9
7
 
10
8
  const ACTIONS = 'GitHub Actions'
11
9
 
10
+ const isPartial = (tag) => /^\d+(?:\.\d+)?$/u.test(tag)
12
11
  // OSV's `1.2` is 1.2.0, not semver's 1.2.x.
13
- const full = (version) => (/^\d+(?:\.\d+)?$/u.test(version) ? `${version}.0.0`.split('.').slice(0, 3).join('.') : version)
12
+ const full = (version) => (isPartial(version) ? `${version}.0.0`.split('.').slice(0, 3).join('.') : version)
14
13
 
15
14
  // OSV can't match these versions itself: its records' ECOSYSTEM ranges, as
16
15
  // semver's, and whether one is still open, no release having fixed it.
@@ -38,8 +37,6 @@ function readAffected(entry) {
38
37
  return { listed, range, open, all: validRange(range, { loose: true }) === null }
39
38
  }
40
39
 
41
- const isPartial = (tag) => /^\d+(?:\.\d+)?$/u.test(tag)
42
-
43
40
  // A commit may be of any release. A branch, or stasis's 0.0.0, is at its
44
41
  // newest, so only what no release has fixed covers it. A moving tag, `v4`
45
42
  // or `v4.1`, may be anywhere in its line.
@@ -56,7 +53,7 @@ async function actionRows(asked, details) {
56
53
  const rows = []
57
54
  for (const { record, names } of await packageRecords(ACTIONS, [...asked.keys()])) {
58
55
  for (const name of names) {
59
- const entries = (record.affected ?? []).filter((entry) => entry?.package?.ecosystem === ACTIONS && entry.package.name === name).map(readAffected)
56
+ const entries = (record.affected ?? []).filter(isEntryOf(ACTIONS, name)).map(readAffected)
60
57
  const versions = asked.get(name).filter((version) => entries.some((entry) => covers(version, entry)))
61
58
  if (versions.length > 0) rows.push(toAdvisory(ACTIONS, name, versions, record, details))
62
59
  }
@@ -64,17 +61,12 @@ async function actionRows(asked, details) {
64
61
  return rows
65
62
  }
66
63
 
67
- // What github.com answers for a repository it serves to no one without
68
- // credentials: a private or internal one, or one gone (401), and a moved
69
- // one's redirect, which is not followed. 404 too.
64
+ // What github.com answers without credentials for a repository it serves to
65
+ // no one: a private, internal or gone one (401, 404), or a moved one's redirect.
70
66
  const NOT_SERVED = new Set([301, 302, 307, 308, 401, 404])
71
67
 
72
- // Whether github.com serves `repo` to anyone: its ref advertisement, asked
73
- // with no credentials, as `git ls-remote` asks it, over git's protocol v2,
74
- // which answers with its capabilities alone, a few hundred bytes, not every
75
- // ref. Any other answer (a 403 or 429 of a rate limit, a 5xx) or a failure
76
- // throws, before OSV is asked anything. A HEAD is refused (403): a GET, its
77
- // body left unread.
68
+ // Asked as `git ls-remote` asks over protocol v2, whose ref advertisement is its capabilities alone: by a GET
69
+ // left unread, as a HEAD is refused (403).
78
70
  async function servesAnyone(repo) {
79
71
  const [owner, name] = repo.split('/')
80
72
  const url = buildUrl(GITHUB_WEB, [owner, `${name}.git`, 'info', 'refs'], { service: 'git-upload-pack' })
@@ -85,27 +77,10 @@ async function servesAnyone(repo) {
85
77
  throw new HttpError(res.status, `GET ${url} ${res.status}`, res.headers)
86
78
  }
87
79
 
88
- // Those of `asked` github.com serves to anyone (servesAnyone), which alone
89
- // OSV is asked of: a name not known public is sent to it never. Only public
90
- // ones are kept, a month, by the name in lowercase, as GitHub takes it in
91
- // any case; one not public is asked again each time, so one made public is
92
- // asked of OSV as soon as it is.
93
- async function publicOnly(asked, cache) {
94
- const names = [...asked.keys()]
95
- const kept = await pool(names, RECORDS_AT_ONCE, (name) => readRecord('github/public', name.toLowerCase(), cache))
96
- const serving = new Set(names.filter((name, i) => kept[i]?.public === true))
97
- const missing = names.filter((name) => !serving.has(name))
98
- const found = await pool(missing, RECORDS_AT_ONCE, servesAnyone)
99
- const added = missing.filter((name, i) => found[i])
100
- await pool(added, RECORDS_AT_ONCE, (name) => writeRecord('github/public', name.toLowerCase(), { public: true }, cache))
101
- for (const name of added) serving.add(name)
102
- return new Map([...asked].filter(([name]) => serving.has(name)))
103
- }
80
+ // Kept by the name in lowercase, as GitHub takes it in any case.
81
+ const PUBLIC = { dir: 'github/public', atOnce: RECORDS_AT_ONCE, serves: servesAnyone, key: (name) => name.toLowerCase() }
104
82
 
105
- // GitHub repositories as dependencies: OSV's records of them as Actions, of
106
- // those github.com serves to anyone (publicOnly) but those `internal`, which
107
- // are not asked of it either; and every range their own advisories list,
108
- // whichever package it names.
83
+ // A repository's own advisories count every range they list, whichever package it names.
109
84
  export const GITHUB = {
110
85
  repositoryOnly: true,
111
86
  assertName: assertRepo,
@@ -115,7 +90,7 @@ export const GITHUB = {
115
90
  const external = new Map(asked)
116
91
  for (const name of internal) external.delete(name)
117
92
  return (await Promise.all([
118
- publicOnly(external, cache).then((open) => actionRows(open, details)),
93
+ publicOnly(external, cache, PUBLIC).then((open) => actionRows(open, details)),
119
94
  repositoryAdvisories(github, asked, { repoOf: (repo) => repo, versionsIn: bySemver(coversPlaceholder), details, cache }),
120
95
  ])).flat()
121
96
  },
@@ -1,11 +1,16 @@
1
1
  import assert from 'node:assert/strict'
2
2
 
3
3
  import { isGhsa, isRepo, isStrings, matches, show } from '../args.js'
4
+ import { readRecord, writeRecord } from '../cache.js'
5
+ import { pool } from '../pool.js'
4
6
  import { intersects, satisfies, valid, validRange } from '../semver.js'
5
7
 
6
8
  export const order = (a, b) => (a > b) - (a < b)
9
+ // An id as OSV and ruby-advisory-db write one.
10
+ export const isId = matches(/^(?=.{1,128}$)[A-Z][\dA-Z]*(?:-[\dA-Za-z]+)+$/u)
11
+ export const isCve = matches(/^CVE-\d{4}-\d{4,}$/u)
12
+ export const isUrl = (text) => /^https?:$/u.test(URL.parse(text)?.protocol)
7
13
 
8
- // The repository's page for a GHSA where `repo` is given, else the database's.
9
14
  export function advisoryUrl(ghsa, repo) {
10
15
  assert.ok(isGhsa(ghsa) && (repo === undefined || isRepo(repo)), `advisories: no GitHub advisory page for ${show(ghsa)} on ${show(repo)}`)
11
16
  return repo === undefined ? `https://github.com/advisories/${ghsa}` : `https://github.com/${repo}/security/advisories/${ghsa}`
@@ -29,6 +34,16 @@ export const isOpenAbove = (range) => !range.includes('|') && inRange(HIGHEST, r
29
34
  // advisory is worse than a spare one.
30
35
  export const covered = (asked, range, covers = mayBeInRange) => (validRange(range, SEMVER) === null ? [...asked] : asked.filter((version) => covers(version, range)))
31
36
 
37
+ // The same by an ecosystem's own readRange, undefined for a range it can't
38
+ // read, each version tested as `of` reads it.
39
+ export const coveredBy = (readRange, of = (version) => version) => (asked, range) => {
40
+ const test = readRange(range)
41
+ return test === undefined ? [...asked] : asked.filter((version) => test(of(version)))
42
+ }
43
+
44
+ // GitHub's `>= 1.0.0, < 1.2.6` is npm's without the commas.
45
+ export const npmRange = (range) => range.replaceAll(',', ' ')
46
+
32
47
  export const isText = (value) => typeof value === 'string' && value.isWellFormed()
33
48
 
34
49
  export function detailsOf(text, id) {
@@ -56,3 +71,16 @@ export function metrics({ severity, score, vector, cwe }) {
56
71
  cwe: isStrings(cwe) ? cwe.filter(isCwe) : [],
57
72
  }
58
73
  }
74
+
75
+ // Only public ones are kept: one not public is asked again each time, so one made public is asked of OSV at once.
76
+ export async function publicOnly(asked, cache, { dir, atOnce, serves, key = (name) => name }) {
77
+ const names = [...asked.keys()]
78
+ const kept = await pool(names, atOnce, (name) => readRecord(dir, key(name), cache))
79
+ const serving = new Set(names.filter((name, i) => kept[i]?.public === true))
80
+ const missing = names.filter((name) => !serving.has(name))
81
+ const found = await pool(missing, atOnce, serves)
82
+ const added = missing.filter((name, i) => found[i])
83
+ await pool(added, atOnce, (name) => writeRecord(dir, key(name), { public: true }, cache))
84
+ for (const name of added) serving.add(name)
85
+ return new Map([...asked].filter(([name]) => serving.has(name)))
86
+ }