-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcargo.ts
More file actions
289 lines (259 loc) · 7.31 KB
/
cargo.ts
File metadata and controls
289 lines (259 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import type { Client } from '../core/client.ts'
import type {
Dependency,
Maintainer,
Package,
Registry,
RegistryFactory,
URLBuilder,
Version,
} from '../core/types.ts'
import { register } from '../core/registry.ts'
import { HTTPError, NotFoundError } from '../core/errors.ts'
import { normalizeLicense } from '../core/license.ts'
import { normalizeRepositoryURL } from '../core/repository.ts'
/** Crates.io API response for a single crate. */
interface CratesPackageResponse {
crate: {
id: number
name: string
updated_at: string
created_at: string
downloads: number
recent_downloads: number
max_version: string
max_stable_version: string
newest_version: string
description: string
homepage: string
documentation: string
repository: string
keywords: string[]
categories: Array<{
id: string
category: string
created_at: string
}>
badges: unknown[]
links: Record<string, string>
}
versions: CratesVersion[]
}
/** Crates.io version data. */
interface CratesVersion {
id: number
num: string
dl_path: string
readme_path: string
created_at: string
updated_at: string
downloads: number
features: Record<string, string[]>
yanked: boolean
license: string
links: Record<string, string>
crate_size: number
published_by: {
id: number
login: string
name: string
avatar: string
url: string
}
checksum: string
}
/** Crates.io dependencies response. */
interface CratesDependenciesResponse {
dependencies: CratesDependency[]
meta: {
prelude: boolean
}
}
/** Crates.io dependency data. */
interface CratesDependency {
id: number
version_id: number
crate_id: string
req: string
optional: boolean
default_features: boolean
features: string[]
target: string | null
kind: string
downloads: number
}
/** Crates.io maintainers response. */
interface CratesMaintainersResponse {
users: CratesMaintainer[]
}
/** Crates.io maintainer data. */
interface CratesMaintainer {
id: number
login: string
name: string
avatar: string
url: string
}
/** Crates.io registry client. */
class CargoRegistry implements Registry {
constructor(
baseURL: string,
client: Client,
) {
this.baseURL = baseURL
this.client = client
}
readonly baseURL: string
readonly client: Client
ecosystem(): string {
return 'cargo'
}
async fetchPackage(name: string, signal?: AbortSignal): Promise<Package> {
const url = `${this.baseURL}/api/v1/crates/${name}`
try {
const data = await this.client.getJSON<CratesPackageResponse>(url, signal)
const crateData = data.crate
const latestVersion = crateData.max_stable_version || crateData.max_version
return {
name: crateData.name,
description: crateData.description || '',
homepage: crateData.homepage || '',
documentation: crateData.documentation || '',
repository: normalizeRepositoryURL(crateData.repository || ''),
licenses: normalizeLicense(data.versions[0]?.license || ''),
keywords: crateData.keywords,
namespace: '',
latestVersion,
metadata: {
downloads: crateData.downloads,
recentDownloads: crateData.recent_downloads,
categories: crateData.categories,
newestVersion: crateData.newest_version,
defaultVersion: crateData.max_version,
updatedAt: crateData.updated_at,
createdAt: crateData.created_at,
},
}
}
catch (error) {
if (error instanceof HTTPError && error.isNotFound()) {
throw new NotFoundError('cargo', name)
}
throw error
}
}
async fetchVersions(name: string, signal?: AbortSignal): Promise<Version[]> {
const url = `${this.baseURL}/api/v1/crates/${name}`
try {
const data = await this.client.getJSON<CratesPackageResponse>(url, signal)
const versions: Version[] = []
for (const versionData of data.versions) {
const publishedAt = new Date(versionData.created_at)
const status = versionData.yanked ? 'yanked' : ''
versions.push({
number: versionData.num,
publishedAt,
licenses: normalizeLicense(versionData.license),
integrity: `sha256-${versionData.checksum}`,
status,
metadata: {
crateSize: versionData.crate_size,
features: versionData.features,
downloads: versionData.downloads,
},
})
}
return versions
}
catch (error) {
if (error instanceof HTTPError && error.isNotFound()) {
throw new NotFoundError('cargo', name)
}
throw error
}
}
async fetchDependencies(
name: string,
version: string,
signal?: AbortSignal,
): Promise<Dependency[]> {
const url = `${this.baseURL}/api/v1/crates/${name}/${version}/dependencies`
try {
const data = await this.client.getJSON<CratesDependenciesResponse>(url, signal)
const dependencies: Dependency[] = []
for (const dep of data.dependencies) {
let scope: 'runtime' | 'development' | 'test' | 'build' | 'optional'
if (dep.kind === 'dev') {
scope = 'development'
}
else if (dep.kind === 'build') {
scope = 'build'
}
else {
scope = 'runtime'
}
dependencies.push({
name: dep.crate_id,
requirements: dep.req,
scope,
optional: dep.optional,
})
}
return dependencies
}
catch (error) {
if (error instanceof HTTPError && error.isNotFound()) {
throw new NotFoundError('cargo', name, version)
}
throw error
}
}
async fetchMaintainers(name: string, signal?: AbortSignal): Promise<Maintainer[]> {
const url = `${this.baseURL}/api/v1/crates/${name}/owner_user`
try {
const data = await this.client.getJSON<CratesMaintainersResponse>(url, signal)
const maintainers: Maintainer[] = []
for (const user of data.users) {
maintainers.push({
uuid: user.id.toString(),
login: user.login,
name: user.name,
email: '',
url: user.url,
role: '',
})
}
return maintainers
}
catch (error) {
if (error instanceof HTTPError && error.isNotFound()) {
throw new NotFoundError('cargo', name)
}
throw error
}
}
urls(): URLBuilder {
return {
registry: (name: string, version?: string) => {
const base = `https://crates.io/crates/${name}`
return version ? `${base}/${version}` : base
},
download: (name: string, version: string) => {
return `https://crates.io/api/v1/crates/${name}/${version}/download`
},
documentation: (name: string, version?: string) => {
return version ? `https://docs.rs/${name}/${version}` : `https://docs.rs/${name}`
},
purl: (name: string, version?: string) => {
const versionSuffix = version ? `@${version}` : ''
return `pkg:cargo/${name}${versionSuffix}`
},
}
}
}
/** Factory function for creating Cargo registry instances. */
const factory: RegistryFactory = (baseURL: string, client: Client): Registry => {
return new CargoRegistry(baseURL, client)
}
// Self-register on import
register('cargo', 'https://crates.io', factory)