@preventive/upstream 1.0.0-alpha.3 → 1.0.0-alpha.4
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-APACHE +202 -0
- package/advisories.d.ts +27 -16
- package/cargo.d.ts +18 -1
- package/cargo.js +2 -1
- package/github.d.ts +21 -11
- package/package.json +6 -3
- package/src/advisories/common.js +8 -4
- package/src/advisories/github.js +24 -8
- package/src/advisories/index.js +3 -3
- package/src/advisories/osv.js +25 -11
- package/src/advisories/repos.js +13 -23
- package/src/args.js +12 -2
- package/src/cache.js +1 -1
- package/src/{cargo.js → cargo/crate.js} +22 -11
- package/src/cargo/repos.js +62 -0
- package/src/github/client.js +2 -1
- package/src/github/read.js +55 -13
- package/src/http.js +16 -11
- package/src/soldeer.js +3 -9
- /package/{LICENSE → LICENSE-MIT} +0 -0
package/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/advisories.d.ts
CHANGED
|
@@ -6,28 +6,32 @@ import type { Client, RepoName } from './github.js'
|
|
|
6
6
|
export { HttpError } from './npm.js'
|
|
7
7
|
|
|
8
8
|
// As purl and stasis name them.
|
|
9
|
-
export type Ecosystem = 'npm' | 'cargo' | 'composer' | 'github'
|
|
9
|
+
export type Ecosystem = 'npm' | 'cargo' | 'composer' | 'soldeer' | 'github'
|
|
10
10
|
|
|
11
11
|
export interface Package {
|
|
12
12
|
ecosystem: Ecosystem
|
|
13
|
-
// An npm package name, a crate name, a Composer `vendor/name`,
|
|
14
|
-
// `github`, the repository `owner/name` itself.
|
|
13
|
+
// An npm package name, a crate name, a Composer `vendor/name`, a Soldeer
|
|
14
|
+
// project name, or for `github`, the repository `owner/name` itself.
|
|
15
15
|
name: string
|
|
16
16
|
// Its GitHub repository, where the caller knows it (package.json,
|
|
17
17
|
// Cargo.toml, installed.json), for `repoAdvisories` to ask without
|
|
18
|
-
// looking it up
|
|
18
|
+
// looking it up, and for a `soldeer` package, to ask instead of the one
|
|
19
|
+
// Soldeer names; not for `github` packages, which are their own.
|
|
19
20
|
github?: RepoName
|
|
20
21
|
// npm and `github`: semver; `github` also takes a branch name or 0.0.0,
|
|
21
22
|
// which every range covers. cargo: semver, build metadata allowed.
|
|
22
23
|
// composer: a release (`v1.2.3`, `1.2.3.4`, `2.0.0-RC1`), dev versions
|
|
23
|
-
// refused; one semver cannot read is covered by every range.
|
|
24
|
+
// refused; one semver cannot read is covered by every range. soldeer: as
|
|
25
|
+
// soldeer.lock has it; one semver cannot read (a bare number, a commit)
|
|
26
|
+
// is covered by every range.
|
|
24
27
|
versions: string[]
|
|
25
28
|
}
|
|
26
29
|
|
|
27
30
|
export interface AdvisoryOptions {
|
|
28
|
-
// The client every GitHub request goes through. Required for `
|
|
29
|
-
// packages, whose repository's published advisories are
|
|
30
|
-
// source, and for `repoAdvisories`; by itself it asks nothing
|
|
31
|
+
// The client every GitHub request goes through. Required for `soldeer`
|
|
32
|
+
// and `github` packages, whose repository's published advisories are
|
|
33
|
+
// their only source, and for `repoAdvisories`; by itself it asks nothing
|
|
34
|
+
// else.
|
|
31
35
|
github?: Client
|
|
32
36
|
// Also asks each npm, cargo and composer package's GitHub repository
|
|
33
37
|
// for its published advisories, which it has before GitHub reviews them
|
|
@@ -43,13 +47,18 @@ export interface Advisory {
|
|
|
43
47
|
ecosystem: Ecosystem
|
|
44
48
|
name: string
|
|
45
49
|
// `registry`: npm's, as `npm audit` has it, one row per vulnerable
|
|
46
|
-
// range. `osv`:
|
|
50
|
+
// range. `osv`: OSV's records for cargo and composer, RustSec's, GitHub's
|
|
51
|
+
// and malicious packages' (MAL-…) among them; of the records naming each
|
|
52
|
+
// other as aliases, directly or through others, a version is reported
|
|
53
|
+
// under one only: RustSec's, else the GHSA, else the first by id. Each
|
|
54
|
+
// has the others' ids among its aliases.
|
|
47
55
|
// `repository`: published on the package's repository, one row per
|
|
48
56
|
// range, holding only the versions the others' answer does not report
|
|
49
|
-
// under that GHSA.
|
|
57
|
+
// under that GHSA. The only source for `soldeer` and `github`, where
|
|
58
|
+
// every range counts, whichever package it names.
|
|
50
59
|
source: 'registry' | 'osv' | 'repository'
|
|
51
|
-
// A GHSA, RUSTSEC-…, DRUPAL-CORE
|
|
52
|
-
// without a GHSA.
|
|
60
|
+
// A GHSA, RUSTSEC-…, MAL-…, DRUPAL-CORE-… or another OSV id, or
|
|
61
|
+
// npm:<id> for a registry row without a GHSA.
|
|
53
62
|
id: string
|
|
54
63
|
// The id itself, or an OSV record's one GHSA alias.
|
|
55
64
|
ghsa?: string
|
|
@@ -75,8 +84,10 @@ export interface Advisory {
|
|
|
75
84
|
// Every package, and every one of its versions, is checked against its
|
|
76
85
|
// ecosystem's rules before the first request; a name given twice is
|
|
77
86
|
// merged, and must not name two repositories. npm is asked 250 names a
|
|
78
|
-
// request, OSV 1000 versions
|
|
79
|
-
//
|
|
80
|
-
//
|
|
81
|
-
//
|
|
87
|
+
// request, OSV 1000 versions, Soldeer one project; a project Soldeer does
|
|
88
|
+
// not have or that names no GitHub repository, and a repository gone,
|
|
89
|
+
// renamed or blocked, add nothing. Any other failure, a malformed answer,
|
|
90
|
+
// or one about something not asked, throws: nothing is left out quietly.
|
|
91
|
+
// Sorted by ecosystem and name. Versions are matched by npm's semver, from
|
|
92
|
+
// the npm beside node.
|
|
82
93
|
export function advisories(packages: Iterable<Package>, options?: AdvisoryOptions): Promise<Advisory[]>
|
package/cargo.d.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
// Hand-written against cargo.js; a change to either belongs with the other.
|
|
2
2
|
//
|
|
3
|
-
//
|
|
3
|
+
// A name is asserted to be a crate name, a version an exact semver
|
|
4
4
|
// version, and a checksum 64 lowercase hex digits, before any request or
|
|
5
5
|
// cache path. The cache is setCacheDir's (npm.js).
|
|
6
6
|
|
|
7
7
|
export { HttpError } from './npm.js'
|
|
8
8
|
|
|
9
|
+
// Reads crates.io's index for that version and throws unless its sha256
|
|
10
|
+
// is the one given, as for a checksum read off Cargo.lock. The index is
|
|
11
|
+
// read on every call; nothing is cached.
|
|
12
|
+
export function verifyChecksum(name: string, version: string, checksum: string): Promise<void>
|
|
13
|
+
|
|
9
14
|
// A published version's .crate, whole, in memory. `checksum` is its
|
|
10
15
|
// sha256 as Cargo.lock records it; without one, crates.io's index is read
|
|
11
16
|
// for it on every call, cached crate or not. Either way the bytes are
|
|
@@ -17,3 +22,15 @@ export { HttpError } from './npm.js'
|
|
|
17
22
|
// `~/.cargo`. A file there that does not match is passed over. One in
|
|
18
23
|
// setCacheDir's cache that does not match throws.
|
|
19
24
|
export function getCrate(name: string, version: string, checksum?: string): Promise<Uint8Array>
|
|
25
|
+
|
|
26
|
+
// What resolveCrateRepos maps each resolved name to: `owner/name`, the
|
|
27
|
+
// GitHub repo the crate's `repository` on crates.io names.
|
|
28
|
+
export interface CrateRepo {
|
|
29
|
+
github: string
|
|
30
|
+
}
|
|
31
|
+
// Fail-soft, as npm.js's resolvePackageRepos: a crate crates.io does not
|
|
32
|
+
// have, one naming no GitHub repo, and those of a request that fails are
|
|
33
|
+
// absent from the map. crates.io is asked a hundred names a request, a
|
|
34
|
+
// second apart, as it asks; a repo found is cached for a month, and
|
|
35
|
+
// `cachedOnly` answers from that cache alone and never fetches.
|
|
36
|
+
export function resolveCrateRepos(crateNames: Iterable<string>, options?: { cachedOnly?: boolean }): Promise<Map<string, CrateRepo>>
|
package/cargo.js
CHANGED
package/github.d.ts
CHANGED
|
@@ -65,16 +65,20 @@ export interface Client {
|
|
|
65
65
|
getRepoFile(options: { repo: RepoName; path: string; ref?: string }): Promise<string>
|
|
66
66
|
// The head of `branch`, or of the default branch without one.
|
|
67
67
|
getRepoHead(options: { repo: RepoName; branch?: string }): Promise<{ branch: string; oid: string }>
|
|
68
|
+
// The commit `tag` names: GitHub's ref for exactly that tag, an annotated
|
|
69
|
+
// one's tag object followed to the commit, through up to 8 tags of tags.
|
|
70
|
+
// Refused for a tag on anything but a commit.
|
|
71
|
+
getRepoTag(options: { repo: RepoName; tag: string }): Promise<{ tag: string; oid: string }>
|
|
68
72
|
// The repo's gzipped tarball at the full commit `sha`, whole, in memory:
|
|
69
73
|
// getRepoTreeTarball's for the tree GitHub names for that commit, asked
|
|
70
74
|
// on every call, cached or not. So its top directory is named for the
|
|
71
75
|
// tree, and files marked `export-subst` are as committed, not rewritten.
|
|
72
76
|
getRepoTarball(options: { repo: RepoName; sha: string }): Promise<Uint8Array>
|
|
73
|
-
// The id of the tree at `
|
|
74
|
-
// of its root without one: from the tree GitHub
|
|
75
|
-
// down GitHub's listings a directory at a time,
|
|
76
|
-
// id. Refused where `
|
|
77
|
-
getRepoTreeId(options: { repo: RepoName; sha: string;
|
|
77
|
+
// The id of the tree at `directory`, `/`-separated as npm's is, in the
|
|
78
|
+
// full commit `sha`, or of its root without one: from the tree GitHub
|
|
79
|
+
// names for the commit, down GitHub's listings a directory at a time,
|
|
80
|
+
// each hashed back to its id. Refused where `directory` is none.
|
|
81
|
+
getRepoTreeId(options: { repo: RepoName; sha: string; directory?: string }): Promise<string>
|
|
78
82
|
// The gzipped tarball of a tree, the repo's own or any subdirectory's, by
|
|
79
83
|
// its id, whole, in memory: GitHub's, its files under one top directory.
|
|
80
84
|
// Its files are hashed back into git's tree and the id must be `tree`,
|
|
@@ -90,13 +94,19 @@ export interface Client {
|
|
|
90
94
|
// has; that repo still gets a tree already cached, pointers and all.
|
|
91
95
|
getRepoTreeTarball(options: { repo: RepoName; tree: string }): Promise<Uint8Array>
|
|
92
96
|
// The repository's published security advisories, as GitHub's
|
|
93
|
-
// repository advisory objects
|
|
94
|
-
//
|
|
97
|
+
// repository advisory objects: every page of 100, each found by the
|
|
98
|
+
// cursor in the one before's Link header. More than 100 pages is an
|
|
99
|
+
// error.
|
|
95
100
|
listRepoAdvisories(options: { repo: RepoName }): Promise<any[]>
|
|
96
|
-
// The entries of
|
|
97
|
-
//
|
|
98
|
-
//
|
|
99
|
-
listRepoDir(options: { repo: RepoName; sha: string;
|
|
101
|
+
// The entries of `directory` in the full commit `sha`, or of its root,
|
|
102
|
+
// found as getRepoTreeId finds it: GitHub's listing, hashed back to the
|
|
103
|
+
// tree's id, so none is left out or changed.
|
|
104
|
+
listRepoDir(options: { repo: RepoName; sha: string; directory?: string }): Promise<RepoDirEntry[]>
|
|
105
|
+
// Every page of the repo's tags, as GitHub lists them, each with the
|
|
106
|
+
// commit GitHub names for it, an annotated tag's included, which
|
|
107
|
+
// getRepoTag checks is one; more than `maxPages` (100 by default) pages
|
|
108
|
+
// of 100 is an error, as is a tag with no commit.
|
|
109
|
+
listRepoTags(options: { repo: RepoName; maxPages?: number }): Promise<{ tag: string; oid: string }[]>
|
|
100
110
|
// Every page of `GET /user/repos`, as GitHub's repository objects; more
|
|
101
111
|
// than `maxPages` (100 by default) pages of 100 is an error.
|
|
102
112
|
listUserRepos(options?: { maxPages?: number }): Promise<any[]>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@preventive/upstream",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.4",
|
|
4
4
|
"description": "Minimal npm, crates.io and Soldeer registry clients, a GitHub client, and a git checkout reader: the calls we use, nothing more",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -42,6 +42,8 @@
|
|
|
42
42
|
}
|
|
43
43
|
},
|
|
44
44
|
"files": [
|
|
45
|
+
"LICENSE-APACHE",
|
|
46
|
+
"LICENSE-MIT",
|
|
45
47
|
"advisories.d.ts",
|
|
46
48
|
"advisories.js",
|
|
47
49
|
"cargo.d.ts",
|
|
@@ -68,7 +70,8 @@
|
|
|
68
70
|
"src/advisories/repos.js",
|
|
69
71
|
"src/args.js",
|
|
70
72
|
"src/cache.js",
|
|
71
|
-
"src/cargo.js",
|
|
73
|
+
"src/cargo/crate.js",
|
|
74
|
+
"src/cargo/repos.js",
|
|
72
75
|
"src/download.js",
|
|
73
76
|
"src/git.js",
|
|
74
77
|
"src/github/client.js",
|
|
@@ -90,7 +93,7 @@
|
|
|
90
93
|
"scripts": {
|
|
91
94
|
"test": "node --test ./tests/*.test.js"
|
|
92
95
|
},
|
|
93
|
-
"license": "MIT",
|
|
96
|
+
"license": "MIT OR Apache-2.0",
|
|
94
97
|
"author": {
|
|
95
98
|
"name": "Nikita Skovoroda",
|
|
96
99
|
"email": "chalkerx@gmail.com",
|
package/src/advisories/common.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isStrings, matches } from '../args.js'
|
|
2
|
-
import { satisfies, validRange } from '../semver.js'
|
|
2
|
+
import { satisfies, valid, validRange } from '../semver.js'
|
|
3
3
|
|
|
4
4
|
export const order = (a, b) => (a > b) - (a < b)
|
|
5
5
|
|
|
@@ -8,9 +8,13 @@ const SEMVER = { includePrerelease: true, loose: true }
|
|
|
8
8
|
|
|
9
9
|
export const inRange = (version, range) => satisfies(version, range, SEMVER)
|
|
10
10
|
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
export const
|
|
11
|
+
// A version semver cannot read may be in any range. Whatever it reads is
|
|
12
|
+
// a version, `v1.2.3` and `1.2.3+build` included.
|
|
13
|
+
export const mayBeInRange = (version, range) => valid(version) === null || inRange(version, range)
|
|
14
|
+
|
|
15
|
+
// The asked versions a range `covers`. One semver cannot read, range or
|
|
16
|
+
// version, covers them all: a missed advisory is worse than a spare one.
|
|
17
|
+
export const covered = (asked, range, covers = mayBeInRange) => (validRange(range, SEMVER) === null ? [...asked] : asked.filter((version) => covers(version, range)))
|
|
14
18
|
|
|
15
19
|
// Remote text, as long as it is well-formed; anything else is refused as
|
|
16
20
|
// malformed by whoever reads it.
|
package/src/advisories/github.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import assert from 'node:assert/strict'
|
|
2
2
|
|
|
3
|
-
import { assertRepo, assertion, isGhsa, isRefName, isStrings } from '../args.js'
|
|
3
|
+
import { assertRepo, assertSoldeerName, assertSoldeerVersion, assertion, isGhsa, isRefName, isStrings } from '../args.js'
|
|
4
4
|
import { isGone } from '../github/client.js'
|
|
5
5
|
import { recover } from '../http.js'
|
|
6
6
|
import { pool } from '../pool.js'
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { covered, isText, mayBeInRange, metrics } from './common.js'
|
|
8
|
+
import { soldeerRepos } from './repos.js'
|
|
9
9
|
|
|
10
10
|
const REPOS_AT_ONCE = 4
|
|
11
11
|
|
|
@@ -59,6 +59,12 @@ async function repositoryAdvisories(github, asked, { repoOf, takes = () => true,
|
|
|
59
59
|
return [...rows.values()]
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
// Each asked name's repository: as given, `known`, or else looked up.
|
|
63
|
+
async function reposOf(asked, known, lookUp) {
|
|
64
|
+
const found = await lookUp([...asked.keys()].filter((name) => !known.has(name)))
|
|
65
|
+
return (name) => known.get(name) ?? found.get(name)
|
|
66
|
+
}
|
|
67
|
+
|
|
62
68
|
// A maintainer's advisory is on the repository before GitHub reviews it
|
|
63
69
|
// into the databases the registries answer from. With `repoAdvisories`,
|
|
64
70
|
// each package's repository, `known` or else looked up, adds what it
|
|
@@ -67,22 +73,32 @@ async function repositoryAdvisories(github, asked, { repoOf, takes = () => true,
|
|
|
67
73
|
export async function withRepositories(rows, asked, { github, repoAdvisories, known }, { ecosystem, lookUp, covers }) {
|
|
68
74
|
if (!repoAdvisories) return rows
|
|
69
75
|
const reported = new Set(rows.flatMap((row) => [row.id, row.ghsa, ...row.aliases].filter(isGhsa).flatMap((id) => row.versions.map((version) => `${row.name} ${id} ${version}`))))
|
|
70
|
-
const
|
|
71
|
-
const repoOf = (name) => known.get(name) ?? found.get(name)
|
|
76
|
+
const repoOf = await reposOf(asked, known, lookUp)
|
|
72
77
|
const takes = (name, pkg) => pkg?.ecosystem === ecosystem && pkg.name === name
|
|
73
78
|
const added = await repositoryAdvisories(github, asked, { repoOf, takes, covers })
|
|
74
79
|
return [...rows, ...added.map((row) => ({ ...row, versions: row.versions.filter((version) => !reported.has(`${row.name} ${row.id} ${version}`)) }))]
|
|
75
80
|
}
|
|
76
81
|
|
|
77
82
|
// stasis versions a repository with no version of its own by its branch,
|
|
78
|
-
// or 0.0.0: every range covers those.
|
|
79
|
-
|
|
80
|
-
const coversPlaceholder = (version, range) => version === '0.0.0' || valid(version) === null || inRange(version, range)
|
|
83
|
+
// or 0.0.0: every range covers those.
|
|
84
|
+
const coversPlaceholder = (version, range) => version === '0.0.0' || mayBeInRange(version, range)
|
|
81
85
|
|
|
82
86
|
// Dependencies that are GitHub repositories themselves: every range their
|
|
83
87
|
// own published advisories list counts, whichever package it names.
|
|
84
88
|
export const GITHUB = {
|
|
89
|
+
repositoryOnly: true,
|
|
85
90
|
assertName: assertRepo,
|
|
86
91
|
assertVersion: assertion('a version or a branch name', isRefName),
|
|
87
92
|
advisories: (asked, { github }) => repositoryAdvisories(github, asked, { repoOf: (repo) => repo, covers: coversPlaceholder }),
|
|
88
93
|
}
|
|
94
|
+
|
|
95
|
+
// Soldeer packages, which no advisory database has: the repository each
|
|
96
|
+
// is published from, as given or else as Soldeer's project names it, is
|
|
97
|
+
// their only source, and every range its advisories list counts, whichever
|
|
98
|
+
// package it names (often the npm package the same release went out as).
|
|
99
|
+
export const SOLDEER = {
|
|
100
|
+
repositoryOnly: true,
|
|
101
|
+
assertName: assertSoldeerName,
|
|
102
|
+
assertVersion: assertSoldeerVersion,
|
|
103
|
+
advisories: async (asked, { github, known }) => await repositoryAdvisories(github, asked, { repoOf: await reposOf(asked, known, soldeerRepos) }),
|
|
104
|
+
}
|
package/src/advisories/index.js
CHANGED
|
@@ -2,11 +2,11 @@ import assert from 'node:assert/strict'
|
|
|
2
2
|
|
|
3
3
|
import { assertArgs, assertBoolean, assertRepo, assertion, optional, sameName } from '../args.js'
|
|
4
4
|
import { order } from './common.js'
|
|
5
|
-
import { GITHUB, assertClient } from './github.js'
|
|
5
|
+
import { GITHUB, SOLDEER, assertClient } from './github.js'
|
|
6
6
|
import { NPM } from './npm.js'
|
|
7
7
|
import { CARGO, COMPOSER } from './osv.js'
|
|
8
8
|
|
|
9
|
-
const ECOSYSTEMS = { npm: NPM, cargo: CARGO, composer: COMPOSER, github: GITHUB }
|
|
9
|
+
const ECOSYSTEMS = { npm: NPM, cargo: CARGO, composer: COMPOSER, soldeer: SOLDEER, github: GITHUB }
|
|
10
10
|
const byNumbers = new Intl.Collator('en', { numeric: true }).compare
|
|
11
11
|
const assertEcosystem = assertion(`one of ${Object.keys(ECOSYSTEMS).join(', ')}`, (value) => typeof value === 'string' && Object.hasOwn(ECOSYSTEMS, value))
|
|
12
12
|
const assertVersions = assertion('a non-empty array', (value) => Array.isArray(value) && value.length > 0)
|
|
@@ -37,7 +37,7 @@ export async function advisories(packages, options = {}) {
|
|
|
37
37
|
assertArgs('advisories', options, { github: optional(assertClient), repoAdvisories: optional(assertBoolean) })
|
|
38
38
|
assert.ok(!options.repoAdvisories || options.github, 'advisories: repoAdvisories needs a github client')
|
|
39
39
|
const byEcosystem = collect(packages)
|
|
40
|
-
assert.ok(!
|
|
40
|
+
for (const ecosystem of byEcosystem.keys()) assert.ok(!ECOSYSTEMS[ecosystem].repositoryOnly || options.github, `advisories: ${ecosystem} packages need a github client`)
|
|
41
41
|
const found = await Promise.all([...byEcosystem].map(async ([ecosystem, named]) => {
|
|
42
42
|
const { compare = byNumbers, advisories: find } = ECOSYSTEMS[ecosystem]
|
|
43
43
|
const names = [...named.keys()].toSorted()
|
package/src/advisories/osv.js
CHANGED
|
@@ -13,6 +13,12 @@ const QUERIES_PER_REQUEST = 1000
|
|
|
13
13
|
const RECORDS_AT_ONCE = 8
|
|
14
14
|
const isOsvId = matches(/^(?=.{1,128}$)[A-Z][\dA-Z]*(?:-[\dA-Za-z]+)+$/u)
|
|
15
15
|
const INFORMATIONAL = new Set(['unmaintained', 'unsound', 'notice'])
|
|
16
|
+
// Of the records that publish one advisory, the first answered for a
|
|
17
|
+
// version stands for the rest on it: RustSec's, what `cargo audit` reads
|
|
18
|
+
// and GitHub mirrors, then the GHSA, then any other (MAL-…), then by id.
|
|
19
|
+
const rank = (id) => (id.startsWith('RUSTSEC-') ? 0 : (isGhsa(id) ? 1 : 2))
|
|
20
|
+
const first = (a, b) => rank(a.id) - rank(b.id) || order(a.id, b.id)
|
|
21
|
+
const named = (a, b) => a.aliases.includes(b.id) || b.aliases.includes(a.id)
|
|
16
22
|
|
|
17
23
|
export const CARGO = {
|
|
18
24
|
osv: 'crates.io',
|
|
@@ -20,7 +26,6 @@ export const CARGO = {
|
|
|
20
26
|
lookUp: crateRepos,
|
|
21
27
|
assertName: assertCrateName,
|
|
22
28
|
assertVersion: assertCrateVersion,
|
|
23
|
-
keep: (id) => id.startsWith('RUSTSEC-'), // What `cargo audit` reads; the GHSA records mirror it.
|
|
24
29
|
advisories: (asked, options) => osvAdvisories(CARGO, asked, options),
|
|
25
30
|
}
|
|
26
31
|
// TODO: packagist.org's API, what `composer audit` reads, also has the
|
|
@@ -70,11 +75,13 @@ function toAdvisory(ecosystem, name, versions, record) {
|
|
|
70
75
|
}
|
|
71
76
|
|
|
72
77
|
// OSV's batch query matches versions on its side, but answers ids only:
|
|
73
|
-
// each record is fetched once after.
|
|
74
|
-
// publishes comes back under
|
|
75
|
-
//
|
|
78
|
+
// each record is fetched once after. An advisory more than one database
|
|
79
|
+
// publishes comes back under each one's id, the records naming each other
|
|
80
|
+
// as aliases, either way round, directly or through others: one group, in
|
|
81
|
+
// which each version is reported under the first answered for it, and
|
|
82
|
+
// each record has the others' ids as aliases.
|
|
76
83
|
async function osvAdvisories(ecosystem, asked, options) {
|
|
77
|
-
const { osv, github, lookUp, covers
|
|
84
|
+
const { osv, github, lookUp, covers } = ecosystem
|
|
78
85
|
const list = [...asked].flatMap(([name, versions]) => versions.map((version) => ({ name, version })))
|
|
79
86
|
const hits = new Map() // id → name → versions, in `list` order
|
|
80
87
|
for (const chunk of chunks(list, QUERIES_PER_REQUEST)) {
|
|
@@ -85,21 +92,28 @@ async function osvAdvisories(ecosystem, asked, options) {
|
|
|
85
92
|
const { name, version } = chunk[j]
|
|
86
93
|
const vulns = result?.vulns ?? []
|
|
87
94
|
assert.ok(result && result.next_page_token === undefined && Array.isArray(vulns) && vulns.every((vuln) => isOsvId(vuln?.id)), `advisories: malformed OSV result for ${name}@${version}`)
|
|
88
|
-
for (const { id } of vulns
|
|
95
|
+
for (const { id } of vulns) {
|
|
89
96
|
const byName = hits.get(id) ?? hits.set(id, new Map()).get(id)
|
|
90
97
|
byName.set(name, (byName.get(name) ?? new Set()).add(version))
|
|
91
98
|
}
|
|
92
99
|
}
|
|
93
100
|
}
|
|
94
101
|
const records = (await pool([...hits.keys()], RECORDS_AT_ONCE, getVuln)).filter((record) => !record.withdrawn)
|
|
95
|
-
const
|
|
102
|
+
const groups = new Map()
|
|
103
|
+
for (const record of records) {
|
|
104
|
+
if (groups.has(record)) continue
|
|
105
|
+
const group = [record]
|
|
106
|
+
for (const member of group) group.push(...records.filter((other) => !group.includes(other) && named(member, other)))
|
|
107
|
+
for (const member of group) groups.set(member, group)
|
|
108
|
+
}
|
|
96
109
|
const rows = []
|
|
97
110
|
for (const record of records) {
|
|
111
|
+
const group = groups.get(record)
|
|
112
|
+
const above = group.filter((other) => first(other, record) < 0)
|
|
113
|
+
const aliases = [...new Set([...record.aliases, ...group.map((other) => other.id)])].filter((id) => id !== record.id)
|
|
98
114
|
for (const [name, versions] of hits.get(record.id)) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const rest = [...versions].filter((version) => !shadowed.includes(version))
|
|
102
|
-
if (rest.length > 0) rows.push(toAdvisory(osv, name, rest, record))
|
|
115
|
+
const rest = [...versions].filter((version) => !above.some((other) => hits.get(other.id).get(name)?.has(version)))
|
|
116
|
+
if (rest.length > 0) rows.push(toAdvisory(osv, name, rest, { ...record, aliases }))
|
|
103
117
|
}
|
|
104
118
|
}
|
|
105
119
|
rows.sort((a, b) => order(a.name, b.name) || order(a.id, b.id))
|
package/src/advisories/repos.js
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
import assert from 'node:assert/strict'
|
|
2
|
-
import { setTimeout as sleep } from 'node:timers/promises'
|
|
3
2
|
|
|
4
3
|
import { isRepo, show } from '../args.js'
|
|
5
4
|
import { readRecord, writeRecord } from '../cache.js'
|
|
6
|
-
import {
|
|
5
|
+
import { lookUpCrateRepos } from '../cargo/repos.js'
|
|
6
|
+
import { PACKAGIST_REPO, SOLDEER_API, buildUrl, isNotFound, recover, request } from '../http.js'
|
|
7
7
|
import { lookUpPackageRepo } from '../npm/repos.js'
|
|
8
|
-
import {
|
|
8
|
+
import { pool } from '../pool.js'
|
|
9
9
|
import { githubRepoOfUrl } from '../remote.js'
|
|
10
10
|
|
|
11
|
-
const CRATES_PER_REQUEST = 100
|
|
12
|
-
const CRATES_PACE_MS = 1000 // crates.io asks for a request a second at most, and a user agent.
|
|
13
|
-
const USER_AGENT = '@preventive/upstream (https://github.com/PreventiveMeasures/libraries)'
|
|
14
11
|
const PACKAGES_AT_ONCE = 8
|
|
15
12
|
|
|
16
13
|
// Name → its GitHub repo, or null where the registry has no such package
|
|
@@ -30,22 +27,6 @@ async function cachedRepos(dir, names, fetchMissing) {
|
|
|
30
27
|
return repos
|
|
31
28
|
}
|
|
32
29
|
|
|
33
|
-
// A crate crates.io leaves out of its answer is one it does not have.
|
|
34
|
-
async function fetchCrates(names) {
|
|
35
|
-
const found = new Map(names.map((name) => [name, null]))
|
|
36
|
-
for (const [i, chunk] of chunks(names, CRATES_PER_REQUEST).entries()) {
|
|
37
|
-
if (i > 0) await sleep(CRATES_PACE_MS)
|
|
38
|
-
const url = buildUrl(CRATES_API, ['api', 'v1', 'crates'], { 'ids[]': chunk, per_page: CRATES_PER_REQUEST })
|
|
39
|
-
const answer = await request(url, { as: 'json', headers: { 'User-Agent': USER_AGENT } })
|
|
40
|
-
assert.ok(Array.isArray(answer?.crates), 'advisories: expected a list of crates from crates.io')
|
|
41
|
-
for (const crate of answer.crates) {
|
|
42
|
-
assert.ok(chunk.includes(crate?.id), `advisories: crates.io answered for ${show(crate?.id)}, which was not asked`)
|
|
43
|
-
found.set(crate.id, githubRepoOfUrl(crate.repository) ?? null)
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return found
|
|
47
|
-
}
|
|
48
|
-
|
|
49
30
|
async function fetchEach(names, fetch) {
|
|
50
31
|
const found = await pool(names, PACKAGES_AT_ONCE, fetch)
|
|
51
32
|
return new Map(names.map((name, i) => [name, found[i]]))
|
|
@@ -61,6 +42,15 @@ async function fetchComposerRepo(name) {
|
|
|
61
42
|
return githubRepoOfUrl(versions[0]?.source?.url) ?? null
|
|
62
43
|
}
|
|
63
44
|
|
|
64
|
-
|
|
45
|
+
// Soldeer answers a project it does not have with an empty list.
|
|
46
|
+
async function fetchSoldeerRepo(name) {
|
|
47
|
+
const answer = await request(buildUrl(SOLDEER_API, ['api', 'v1', 'project'], { project_name: name }), { as: 'json' })
|
|
48
|
+
assert.ok(Array.isArray(answer?.data), `advisories: expected a list of projects from Soldeer for ${name}`)
|
|
49
|
+
for (const project of answer.data) assert.ok(project?.name === name, `advisories: Soldeer answered for ${show(project?.name)}, not ${name}`)
|
|
50
|
+
return githubRepoOfUrl(answer.data[0]?.github_url) ?? null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const crateRepos = (names) => lookUpCrateRepos('advisories', names)
|
|
65
54
|
export const composerRepos = (names) => cachedRepos('composer/repos', names, (missing) => fetchEach(missing, fetchComposerRepo))
|
|
66
55
|
export const npmRepos = (names) => fetchEach(names, async (name) => (await lookUpPackageRepo(name))?.github ?? null)
|
|
56
|
+
export const soldeerRepos = (names) => cachedRepos('soldeer/repos', names, (missing) => fetchEach(missing, fetchSoldeerRepo))
|
package/src/args.js
CHANGED
|
@@ -15,7 +15,9 @@ const isLogin = matches(/^(?=.{1,39}$)[a-z\d](?:-?[a-z\d])*$/iu)
|
|
|
15
15
|
const isRepoName = matches(/^(?!\.\.?$)[\w.-]{1,100}$/u)
|
|
16
16
|
// `.git` is never in a repo's tree, and no commit should write into it.
|
|
17
17
|
export const isRepoPath = (value) => isString(value, 4096) && value.split('/').every((part) => !['', '.', '..', '.git'].includes(part.toLowerCase()))
|
|
18
|
-
|
|
18
|
+
// git check-ref-format, a rule per alternative. A tag's name may be `@` or
|
|
19
|
+
// start with `-`; a branch's may not.
|
|
20
|
+
const BAD_REF = /^$|[ ~^:?*[\\]|\.\.|@\{|^\/|\/$|\/\/|(?:^|\/)\.|\.lock(?:\/|$)|\.$/u
|
|
19
21
|
const isToken = matches(/^[!-~]+$/u)
|
|
20
22
|
// npm's rules for existing names: capitals allowed (JSONStream), the
|
|
21
23
|
// legacy `~'!()*` not.
|
|
@@ -38,7 +40,8 @@ export function isRepo(value) {
|
|
|
38
40
|
|
|
39
41
|
export const isStrings = (value) => Array.isArray(value) && value.every((item) => typeof item === 'string')
|
|
40
42
|
export const sameName = (a, b) => typeof a === 'string' && a.toLowerCase() === b.toLowerCase()
|
|
41
|
-
export const
|
|
43
|
+
export const isTagName = (value) => isString(value, 255) && !BAD_REF.test(value)
|
|
44
|
+
export const isRefName = (value) => isTagName(value) && !/^(?:@$|-)/u.test(value)
|
|
42
45
|
export const isSha = matches(/^(?:[\da-f]{40}|[\da-f]{64})$/u)
|
|
43
46
|
export const isGhsa = matches(/^GHSA(?:-[\da-hj-km-np-tv-z]{4}){3}$/u)
|
|
44
47
|
export const assertion = (must, predicate) => (method, what, value) => assert.ok(predicate(value), `${method}: ${what} must be ${must}, got ${show(value)}`)
|
|
@@ -62,6 +65,7 @@ export const assertLogin = assertion('a GitHub login', isLogin)
|
|
|
62
65
|
export const assertRepoName = assertion('a repository name', isRepoName)
|
|
63
66
|
export const assertRepo = assertion('"owner/name"', isRepo)
|
|
64
67
|
export const assertRef = assertion('a branch or tag name', isRefName)
|
|
68
|
+
export const assertTagName = assertion('a tag name', isTagName)
|
|
65
69
|
export const assertSha = assertion('a full commit sha', isSha)
|
|
66
70
|
export const isSha1 = matches(/^[\da-f]{40}$/u)
|
|
67
71
|
export const assertTreeId = assertion('a full tree id', isSha1)
|
|
@@ -74,5 +78,11 @@ export const assertPackageVersion = assertion('an exact semver version', isExact
|
|
|
74
78
|
export const assertGhsa = assertion('a GHSA id', isGhsa)
|
|
75
79
|
export const assertCrateName = assertion('a crate name', matches(/^[A-Za-z][\w-]{0,63}$/u))
|
|
76
80
|
export const assertCrateVersion = assertion('a semver version', matches(/^(?=.{5,256}$)(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:0|[1-9]\d*|\d*[A-Za-z-][\dA-Za-z-]*)(?:\.(?:0|[1-9]\d*|\d*[A-Za-z-][\dA-Za-z-]*))*)?(?:\+[\dA-Za-z-]+(?:\.[\dA-Za-z-]+)*)?$/u))
|
|
81
|
+
// Soldeer's own rule for a name: 3 to 100 of lowercase letters, digits and
|
|
82
|
+
// `-`, starting with one of them or `@`, ending with a letter or digit.
|
|
83
|
+
export const assertSoldeerName = assertion('a Soldeer package name', matches(/^(?=.{3,100}$)[@\da-z][\da-z-]*[\da-z]$/u))
|
|
84
|
+
// Soldeer takes any version but an empty one. Its registry has semver
|
|
85
|
+
// (`5.7.0-rc.0`, `1.0.2-solc-0.8-simulate`), bare numbers and commit hashes.
|
|
86
|
+
export const assertSoldeerVersion = assertion('letters, digits, `.`, `_`, `+` and `-`', matches(/^(?=.{1,128}$)[\dA-Za-z][\w.+-]*$/u))
|
|
77
87
|
// As Cargo.lock and soldeer.lock write a checksum.
|
|
78
88
|
export const assertSha256 = assertion('a sha256 in lowercase hex', matches(/^[\da-f]{64}$/u))
|
package/src/cache.js
CHANGED
|
@@ -6,7 +6,7 @@ import { dirname, join, resolve } from 'node:path'
|
|
|
6
6
|
import { assertDirectoryPath } from './args.js'
|
|
7
7
|
import { MAX_BYTES, decode } from './http.js'
|
|
8
8
|
|
|
9
|
-
const DIRS = new Set(['npm/repos', 'npm/tarballs', 'cargo/repos', 'cargo/crates', 'composer/repos', 'soldeer/zips', 'github/trees'])
|
|
9
|
+
const DIRS = new Set(['npm/repos', 'npm/tarballs', 'cargo/repos', 'cargo/crates', 'composer/repos', 'soldeer/repos', 'soldeer/zips', 'github/trees'])
|
|
10
10
|
const RECORD_TTL_MS = 30 * 24 * 60 * 60 * 1000 // A link only moves on a transfer or rename, and GitHub redirects those.
|
|
11
11
|
|
|
12
12
|
let root
|
|
@@ -3,16 +3,16 @@ import { readdir } from 'node:fs/promises'
|
|
|
3
3
|
import { homedir } from 'node:os'
|
|
4
4
|
import { join, resolve } from 'node:path'
|
|
5
5
|
|
|
6
|
-
import { assertCrateName, assertCrateVersion, assertSha256, printable, show } from '
|
|
7
|
-
import { verifiedDownload } from '
|
|
8
|
-
import { CRATES_INDEX, CRATES_STATIC, buildUrl, encodeSegment, request } from '
|
|
6
|
+
import { assertCrateName, assertCrateVersion, assertSha256, printable, show } from '../args.js'
|
|
7
|
+
import { verifiedDownload } from '../download.js'
|
|
8
|
+
import { CRATES_INDEX, CRATES_STATIC, buildUrl, encodeSegment, request } from '../http.js'
|
|
9
9
|
|
|
10
10
|
const DIR = 'cargo/crates' // No expiry: crates.io never takes a version twice.
|
|
11
11
|
|
|
12
12
|
// The sparse index, what cargo reads: a file per crate, by its lowercased
|
|
13
13
|
// name under 1/, 2/, 3/<first letter>/ or <1st-2nd>/<3rd-4th>/, with a
|
|
14
14
|
// JSON line per version, `cksum` the .crate's sha256.
|
|
15
|
-
async function getChecksum(name, version) {
|
|
15
|
+
async function getChecksum(method, name, version) {
|
|
16
16
|
const lower = name.toLowerCase()
|
|
17
17
|
const prefix = lower.length < 3 ? [String(lower.length)] : lower.length === 3 ? ['3', lower[0]] : [lower.slice(0, 2), lower.slice(2, 4)]
|
|
18
18
|
const text = await request(buildUrl(CRATES_INDEX, [...prefix, lower]), { as: 'text' })
|
|
@@ -20,13 +20,13 @@ async function getChecksum(name, version) {
|
|
|
20
20
|
try {
|
|
21
21
|
return JSON.parse(line)
|
|
22
22
|
} catch {
|
|
23
|
-
assert.fail(
|
|
23
|
+
assert.fail(`${method}: the index for ${name} has a line that is not JSON: ${printable(line.slice(0, 200))}`)
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
const entry = text.split('\n').filter(Boolean).map(parse).find((candidate) => candidate?.vers === version)
|
|
27
|
-
assert.ok(entry,
|
|
28
|
-
assert.ok(entry.name === name,
|
|
29
|
-
assertSha256(
|
|
27
|
+
assert.ok(entry, `${method}: the index has no ${name}@${version}`)
|
|
28
|
+
assert.ok(entry.name === name, `${method}: the index answered for ${show(entry.name)}, not ${name}`)
|
|
29
|
+
assertSha256(method, 'the index cksum', entry.cksum)
|
|
30
30
|
return entry.cksum
|
|
31
31
|
}
|
|
32
32
|
|
|
@@ -37,11 +37,22 @@ async function localPaths(name, version) {
|
|
|
37
37
|
return (await readdir(root).catch(() => [])).map((registry) => join(root, registry, `${name}-${version}.crate`))
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
function assertCrate(method, name, version) {
|
|
41
|
+
assertCrateName(method, 'name', name)
|
|
42
|
+
assertCrateVersion(method, 'version', version)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export async function verifyChecksum(name, version, checksum) {
|
|
46
|
+
assertCrate('verifyChecksum', name, version)
|
|
47
|
+
assertSha256('verifyChecksum', 'checksum', checksum)
|
|
48
|
+
const cksum = await getChecksum('verifyChecksum', name, version)
|
|
49
|
+
assert.ok(cksum === checksum, `verifyChecksum: ${name}@${version} is ${cksum} on crates.io, not ${checksum}`)
|
|
50
|
+
}
|
|
51
|
+
|
|
40
52
|
export async function getCrate(name, version, checksum) {
|
|
41
|
-
|
|
42
|
-
assertCrateVersion('getCrate', 'version', version)
|
|
53
|
+
assertCrate('getCrate', name, version)
|
|
43
54
|
if (checksum !== undefined) assertSha256('getCrate', 'checksum', checksum)
|
|
44
|
-
const expected = checksum ?? await getChecksum(name, version)
|
|
55
|
+
const expected = checksum ?? await getChecksum('getCrate', name, version)
|
|
45
56
|
const locate = () => buildUrl(CRATES_STATIC, ['crates', name, `${name}-${encodeSegment(version)}.crate`])
|
|
46
57
|
return await verifiedDownload({ method: 'getCrate', dir: DIR, what: `${name}@${version}`, ext: 'crate', algorithm: 'sha256', expected, local: await localPaths(name, version), locate })
|
|
47
58
|
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import { setTimeout as sleep } from 'node:timers/promises'
|
|
3
|
+
|
|
4
|
+
import { assertArgs, assertBoolean, assertCrateName, isRepo, optional, show } from '../args.js'
|
|
5
|
+
import { readRecord, writeRecord } from '../cache.js'
|
|
6
|
+
import { CRATES_API, buildUrl, request } from '../http.js'
|
|
7
|
+
import { chunks } from '../pool.js'
|
|
8
|
+
import { githubRepoOfUrl } from '../remote.js'
|
|
9
|
+
|
|
10
|
+
const DIR = 'cargo/repos'
|
|
11
|
+
const PER_REQUEST = 100
|
|
12
|
+
const PACE_MS = 1000 // crates.io asks for a request a second at most, and a user agent.
|
|
13
|
+
const USER_AGENT = '@preventive/upstream (https://github.com/PreventiveMeasures/libraries)'
|
|
14
|
+
|
|
15
|
+
// One request for up to a hundred crates: each name → the GitHub repo its
|
|
16
|
+
// `repository` names, or null where it names none. A crate crates.io
|
|
17
|
+
// leaves out of its answer is one it does not have, null too.
|
|
18
|
+
async function askCrates(method, names) {
|
|
19
|
+
const url = buildUrl(CRATES_API, ['api', 'v1', 'crates'], { 'ids[]': names, per_page: PER_REQUEST })
|
|
20
|
+
const answer = await request(url, { as: 'json', headers: { 'User-Agent': USER_AGENT } })
|
|
21
|
+
assert.ok(Array.isArray(answer?.crates), `${method}: expected a list of crates from crates.io`)
|
|
22
|
+
const found = new Map(names.map((name) => [name, null]))
|
|
23
|
+
for (const crate of answer.crates) {
|
|
24
|
+
assert.ok(names.includes(crate?.id), `${method}: crates.io answered for ${show(crate?.id)}, which was not asked`)
|
|
25
|
+
found.set(crate.id, githubRepoOfUrl(crate.repository) ?? null)
|
|
26
|
+
}
|
|
27
|
+
return found
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Name → its GitHub repo, or null where crates.io has no such crate or it
|
|
31
|
+
// names no GitHub repo, through the cache: only a repo found is cached,
|
|
32
|
+
// for a month. A failed request throws, or with `soft` leaves its names
|
|
33
|
+
// out; `cachedOnly` asks nothing.
|
|
34
|
+
export async function lookUpCrateRepos(method, names, { soft = false, cachedOnly = false } = {}) {
|
|
35
|
+
const repos = new Map()
|
|
36
|
+
for (const name of names) {
|
|
37
|
+
const entry = await readRecord(DIR, name)
|
|
38
|
+
if (isRepo(entry?.github)) repos.set(name, entry.github)
|
|
39
|
+
}
|
|
40
|
+
if (cachedOnly) return repos
|
|
41
|
+
for (const [i, chunk] of chunks(names.filter((name) => !repos.has(name)), PER_REQUEST).entries()) {
|
|
42
|
+
if (i > 0) await sleep(PACE_MS)
|
|
43
|
+
const answer = await askCrates(method, chunk).catch((error) => {
|
|
44
|
+
if (soft) return new Map()
|
|
45
|
+
throw error
|
|
46
|
+
})
|
|
47
|
+
for (const [name, github] of answer) {
|
|
48
|
+
repos.set(name, github)
|
|
49
|
+
if (github) await writeRecord(DIR, name, { github })
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return repos
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function resolveCrateRepos(crateNames, options = {}) {
|
|
56
|
+
assert.ok(typeof crateNames?.[Symbol.iterator] === 'function' && typeof crateNames !== 'string', 'resolveCrateRepos: crateNames must be an iterable of names')
|
|
57
|
+
assertArgs('resolveCrateRepos', options, { cachedOnly: optional(assertBoolean) })
|
|
58
|
+
const names = [...new Set(crateNames)]
|
|
59
|
+
for (const name of names) assertCrateName('resolveCrateRepos', 'name', name)
|
|
60
|
+
const repos = await lookUpCrateRepos('resolveCrateRepos', names, { soft: true, cachedOnly: options.cachedOnly })
|
|
61
|
+
return new Map(names.flatMap((name) => (repos.get(name) ? [[name, { github: repos.get(name) }]] : [])))
|
|
62
|
+
}
|
package/src/github/client.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import assert from 'node:assert/strict'
|
|
2
2
|
|
|
3
3
|
import { assertArgs, assertToken, assertTokenOrNull, assertUserAgent, optional } from '../args.js'
|
|
4
|
-
import { GITHUB_API, HttpError, buildUrl, request } from '../http.js'
|
|
4
|
+
import { GITHUB_API, HttpError, buildUrl, request, requestWithHeaders } from '../http.js'
|
|
5
5
|
|
|
6
6
|
export const api = (segments, query) => buildUrl(GITHUB_API, segments, query)
|
|
7
7
|
export const repoApi = (repo, segments = [], query) => api(['repos', ...repo.split('/'), ...segments], query)
|
|
8
8
|
export const call = (headers, url, options) => request(url, { as: 'json', headers, ...options })
|
|
9
|
+
export const callWithHeaders = (headers, url, options) => requestWithHeaders(url, { as: 'json', headers, ...options })
|
|
9
10
|
// A repository renamed (a redirect, refused), deleted or blocked, not a
|
|
10
11
|
// failure that could pass.
|
|
11
12
|
export const isGone = (err) => err instanceof HttpError && [301, 404, 410, 451].includes(err.status)
|
package/src/github/read.js
CHANGED
|
@@ -2,11 +2,11 @@ import assert from 'node:assert/strict'
|
|
|
2
2
|
import { Buffer } from 'node:buffer'
|
|
3
3
|
import { createHash } from 'node:crypto'
|
|
4
4
|
|
|
5
|
-
import { assertArgs, assertGhsa, assertLogin, assertNumber, assertPath, assertRef, assertRepo, assertSha, assertTreeId, isSha, isSha1, optional, sameName, show } from '../args.js'
|
|
5
|
+
import { assertArgs, assertGhsa, assertLogin, assertNumber, assertPath, assertRef, assertRepo, assertSha, assertTagName, assertTreeId, isSha, isSha1, isTagName, optional, sameName, show } from '../args.js'
|
|
6
6
|
import { verifiedDownload } from '../download.js'
|
|
7
7
|
import { decode, encodeSegment } from '../http.js'
|
|
8
8
|
import { gitTreeOfListing } from '../tree.js'
|
|
9
|
-
import { api, bindMethods, call, clientHeaders, isGone, repoApi } from './client.js'
|
|
9
|
+
import { api, bindMethods, call, callWithHeaders, clientHeaders, isGone, repoApi } from './client.js'
|
|
10
10
|
|
|
11
11
|
const PER_PAGE = 100
|
|
12
12
|
const MAX_PAGES = 100
|
|
@@ -22,6 +22,24 @@ async function* pages(method, headers, pageUrl, maxPages = MAX_PAGES) {
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
// A list GitHub pages by a cursor instead: the `after` of each page's
|
|
26
|
+
// Link header `next` link, the one thing read off the link, so the next
|
|
27
|
+
// page is asked of the list `pageUrl` names, wherever the link points. A
|
|
28
|
+
// next link proves a page more.
|
|
29
|
+
async function* cursorPages(method, headers, pageUrl, maxPages = MAX_PAGES) {
|
|
30
|
+
const paging = { per_page: PER_PAGE }
|
|
31
|
+
for (let page = 1; ; page++) {
|
|
32
|
+
const answer = await callWithHeaders(headers, pageUrl(paging))
|
|
33
|
+
assert.ok(Array.isArray(answer.body), `${method}: expected an array for page ${page}`)
|
|
34
|
+
yield answer.body
|
|
35
|
+
const next = /<([^>]*)>\s*;\s*rel="next"/u.exec(answer.headers.get('link') ?? '')?.[1]
|
|
36
|
+
if (next === undefined) return
|
|
37
|
+
paging.after = URL.parse(next)?.searchParams.get('after')
|
|
38
|
+
assert.ok(paging.after, `${method}: page ${page} links the next with no cursor`)
|
|
39
|
+
assert.ok(page < maxPages, `${method}: more than ${maxPages} pages`)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
25
43
|
const paginate = async (...args) => (await Array.fromAsync(pages(...args))).flat()
|
|
26
44
|
const getCurrentUser = (headers) => call(headers, api(['user']))
|
|
27
45
|
|
|
@@ -48,6 +66,32 @@ export async function getRepoHead(headers, options) {
|
|
|
48
66
|
return { branch: ref, oid: data.object.sha }
|
|
49
67
|
}
|
|
50
68
|
|
|
69
|
+
async function getRepoTag(headers, options) {
|
|
70
|
+
assertArgs('getRepoTag', options, { repo: assertRepo, tag: assertTagName })
|
|
71
|
+
const { repo, tag } = options
|
|
72
|
+
const ref = await call(headers, repoApi(repo, ['git', 'ref', 'tags', encodeSegment(tag)]))
|
|
73
|
+
assert.ok(ref?.ref === `refs/tags/${tag}`, `getRepoTag: GitHub answered for ${show(ref?.ref)}, not ${show(`refs/tags/${tag}`)}`)
|
|
74
|
+
let { object } = ref
|
|
75
|
+
for (let hops = 0; object?.type === 'tag' && isSha(object.sha) && hops < 8; hops++) {
|
|
76
|
+
const annotated = await call(headers, repoApi(repo, ['git', 'tags', object.sha]))
|
|
77
|
+
assert.ok(annotated?.sha === object.sha, `getRepoTag: GitHub answered for another tag object than ${object.sha}`)
|
|
78
|
+
object = annotated.object
|
|
79
|
+
}
|
|
80
|
+
assert.ok(object?.type === 'commit' && isSha(object.sha), `getRepoTag: ${repo} has no commit for tag ${show(tag)}`)
|
|
81
|
+
return { tag, oid: object.sha }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// GitHub lists each tag with the commit it names, an annotated one's too.
|
|
85
|
+
async function listRepoTags(headers, options) {
|
|
86
|
+
assertArgs('listRepoTags', options, { repo: assertRepo, maxPages: optional(assertNumber) })
|
|
87
|
+
const { repo, maxPages } = options
|
|
88
|
+
const tags = await paginate('listRepoTags', headers, (paging) => repoApi(repo, ['tags'], paging), maxPages)
|
|
89
|
+
return tags.map((entry) => {
|
|
90
|
+
assert.ok(isTagName(entry?.name) && isSha(entry.commit?.sha), `listRepoTags: GitHub listed ${show(entry?.name)} in ${repo}, which is no tag name with a commit`)
|
|
91
|
+
return { tag: entry.name, oid: entry.commit.sha }
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
51
95
|
// GitHub's object for the path first: a directory, symlink or submodule is
|
|
52
96
|
// refused rather than read as a file's text. Up to 1 MB the object holds
|
|
53
97
|
// the content; past it, `encoding: none`, and the blob it names is read,
|
|
@@ -113,12 +157,13 @@ async function listTree(method, headers, repo, tree) {
|
|
|
113
157
|
}
|
|
114
158
|
|
|
115
159
|
async function treeAt(method, headers, options) {
|
|
116
|
-
assertArgs(method, options, { repo: assertRepo, sha: assertSha, path: optional(assertPath) })
|
|
117
|
-
|
|
160
|
+
assertArgs(method, options, { repo: assertRepo, sha: assertSha, directory: optional(assertPath), path: optional(assertPath) })
|
|
161
|
+
assert.ok(options.directory === undefined || options.path === undefined, `${method}: directory and path are one option, pass directory alone`)
|
|
162
|
+
const { repo, sha, directory = options.path } = options
|
|
118
163
|
let tree = await commitTree(method, headers, repo, sha)
|
|
119
|
-
for (const name of
|
|
164
|
+
for (const name of directory?.split('/') ?? []) {
|
|
120
165
|
const entry = (await listTree(method, headers, repo, tree)).find((candidate) => candidate.path === name)
|
|
121
|
-
assert.ok(entry?.type === 'tree', `${method}: ${repo}@${sha} has no directory at ${show(
|
|
166
|
+
assert.ok(entry?.type === 'tree', `${method}: ${repo}@${sha} has no directory at ${show(directory)}`)
|
|
122
167
|
tree = entry.sha
|
|
123
168
|
}
|
|
124
169
|
return tree
|
|
@@ -168,15 +213,12 @@ async function getAdvisory(headers, options) {
|
|
|
168
213
|
return advisory
|
|
169
214
|
}
|
|
170
215
|
|
|
171
|
-
// One page: GitHub pages this list by a cursor in the Link header, which
|
|
172
|
-
// `call` does not read, so a full page is refused rather than cut short.
|
|
173
216
|
async function listRepoAdvisories(headers, options) {
|
|
174
217
|
assertArgs('listRepoAdvisories', options, { repo: assertRepo })
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
return list
|
|
218
|
+
const { repo } = options
|
|
219
|
+
const pageUrl = (paging) => repoApi(repo, ['security-advisories'], { state: 'published', ...paging })
|
|
220
|
+
return (await Array.fromAsync(cursorPages('listRepoAdvisories', headers, pageUrl))).flat()
|
|
179
221
|
}
|
|
180
222
|
|
|
181
|
-
export const readMethods = { getCurrentUser, listUserRepos, getRepo, getRepoHead, getRepoFile, getRepoTarball, getRepoTreeTarball, getRepoTreeId, listRepoDir, getPullRequest, getCollaboratorPermission, getAdvisory, listRepoAdvisories }
|
|
223
|
+
export const readMethods = { getCurrentUser, listUserRepos, getRepo, getRepoHead, getRepoTag, listRepoTags, getRepoFile, getRepoTarball, getRepoTreeTarball, getRepoTreeId, listRepoDir, getPullRequest, getCollaboratorPermission, getAdvisory, listRepoAdvisories }
|
|
182
224
|
export const createClient = (options) => bindMethods(clientHeaders('createClient', options, true), readMethods)
|
package/src/http.js
CHANGED
|
@@ -101,22 +101,27 @@ export async function readBody(res, limit, { truncate = false } = {}) {
|
|
|
101
101
|
return new Uint8Array(await new Blob(chunks).arrayBuffer(), 0, Math.min(size, limit))
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
+
function parse(bytes, url, as) {
|
|
105
|
+
if (as === 'bytes') return bytes
|
|
106
|
+
const text = decode(bytes, url)
|
|
107
|
+
if (as === 'text') return text
|
|
108
|
+
try {
|
|
109
|
+
return JSON.parse(text)
|
|
110
|
+
} catch (cause) {
|
|
111
|
+
throw new Error(`Malformed JSON from ${url}: ${printable(text.slice(0, 200))}`, { cause })
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
104
115
|
// Read as the caller says, not by content type, which proxies drop or
|
|
105
|
-
// rewrite.
|
|
106
|
-
export async function
|
|
116
|
+
// rewrite; answered with the response's headers.
|
|
117
|
+
export async function requestWithHeaders(url, options) {
|
|
107
118
|
const res = await send(url, options)
|
|
108
119
|
if (!res.ok) {
|
|
109
120
|
// A stream decode holds back a character cut at the limit rather than refusing it.
|
|
110
121
|
const text = await readBody(res, 4096, { truncate: true }).then((bytes) => new TextDecoder('utf-8', { fatal: true }).decode(bytes, { stream: true })).catch(() => '')
|
|
111
122
|
throw new HttpError(res.status, `${options.method ?? 'GET'} ${url} ${res.status}: ${printable(text)}`)
|
|
112
123
|
}
|
|
113
|
-
|
|
114
|
-
if (options.as === 'bytes') return bytes
|
|
115
|
-
const text = decode(bytes, url)
|
|
116
|
-
if (options.as === 'text') return text
|
|
117
|
-
try {
|
|
118
|
-
return JSON.parse(text)
|
|
119
|
-
} catch (cause) {
|
|
120
|
-
throw new Error(`Malformed JSON from ${url}: ${printable(text.slice(0, 200))}`, { cause })
|
|
121
|
-
}
|
|
124
|
+
return { body: parse(await readBody(res, LIMITS[options.as].bytes), url, options.as), headers: res.headers }
|
|
122
125
|
}
|
|
126
|
+
|
|
127
|
+
export const request = async (url, options) => (await requestWithHeaders(url, options)).body
|
package/src/soldeer.js
CHANGED
|
@@ -1,16 +1,10 @@
|
|
|
1
1
|
import assert from 'node:assert/strict'
|
|
2
2
|
|
|
3
|
-
import { assertSha256,
|
|
3
|
+
import { assertSha256, assertSoldeerName, assertSoldeerVersion, show } from './args.js'
|
|
4
4
|
import { verifiedDownload } from './download.js'
|
|
5
5
|
import { SOLDEER_API, SOLDEER_REVISIONS, buildUrl, request } from './http.js'
|
|
6
6
|
|
|
7
7
|
const DIR = 'soldeer/zips' // No expiry: the registry refuses a version it already has.
|
|
8
|
-
// Soldeer's own rule for a name: 3 to 100 of lowercase letters, digits and
|
|
9
|
-
// `-`, starting with one of them or `@`, ending with a letter or digit.
|
|
10
|
-
const assertName = assertion('a Soldeer package name', matches(/^(?=.{3,100}$)[@\da-z][\da-z-]*[\da-z]$/u))
|
|
11
|
-
// Soldeer takes any version but an empty one. Its registry has semver
|
|
12
|
-
// (`5.7.0-rc.0`, `1.0.2-solc-0.8-simulate`), bare numbers and commit hashes.
|
|
13
|
-
const assertVersion = assertion('letters, digits, `.`, `_`, `+` and `-`', matches(/^(?=.{1,128}$)[\dA-Za-z][\w.+-]*$/u))
|
|
14
8
|
|
|
15
9
|
// Where the registry keeps the version's zip; it says nothing of what the
|
|
16
10
|
// zip hashes to.
|
|
@@ -26,8 +20,8 @@ async function zipUrl(name, version) {
|
|
|
26
20
|
// The registry has no hash to check a zip against, so the caller brings
|
|
27
21
|
// one, as soldeer.lock records it.
|
|
28
22
|
export async function getZip(name, version, checksum) {
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
assertSoldeerName('getZip', 'name', name)
|
|
24
|
+
assertSoldeerVersion('getZip', 'version', version)
|
|
31
25
|
assertSha256('getZip', 'checksum', checksum)
|
|
32
26
|
return await verifiedDownload({ method: 'getZip', dir: DIR, what: `${name}@${version}`, ext: 'zip', algorithm: 'sha256', expected: checksum, locate: () => zipUrl(name, version) })
|
|
33
27
|
}
|
/package/{LICENSE → LICENSE-MIT}
RENAMED
|
File without changes
|