-
Notifications
You must be signed in to change notification settings - Fork 10
/
crates_io.ts
72 lines (63 loc) · 1.93 KB
/
crates_io.ts
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
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { $ } from "./deps.ts";
export interface CratesIoMetadata {
crate: {
id: string;
name: string;
max_stable_version: string;
};
versions: {
crate: string;
num: string;
}[];
}
export async function getCratesIoMetadata(crateName: string) {
// rate limit
await new Promise((resolve) => setTimeout(resolve, 100));
return await $.request(`https://crates.io/api/v1/crates/${crateName}`)
.noThrow(404)
.json<CratesIoMetadata | undefined>();
}
export interface CratesIoOwner {
id: number;
avatar: string;
kind: "user" | "team";
name: string;
login: string;
url: string;
}
export async function getCratesIoOwners(crateName: string) {
// rate limit
await new Promise((resolve) => setTimeout(resolve, 100));
const result = await $.request(
`https://crates.io/api/v1/crates/${crateName}/owners`,
)
.noThrow(404)
.json<{ users: CratesIoOwner[] } | undefined>();
return result?.users;
}
export class CratesIoCache {
#hasOwnerCache = new Map<string, boolean>();
#metaDataCache = new Map<string, CratesIoMetadata | "not-found">();
async getMetadata(crateName: string) {
let metadata = this.#metaDataCache.get(crateName);
if (metadata == null) {
metadata = await getCratesIoMetadata(crateName);
this.#metaDataCache.set(crateName, metadata ?? "not-found");
}
return metadata === "not-found" ? undefined : metadata;
}
async hasDenoLandOwner(crateName: string) {
if (crateName.startsWith("deno_")) {
return true;
}
let hasDenoLandOwner = this.#hasOwnerCache.get(crateName);
if (hasDenoLandOwner == null) {
const owners = await getCratesIoOwners(crateName);
hasDenoLandOwner = owners
?.some((s) => s.login === "github:denoland:engineering") ?? false;
this.#hasOwnerCache.set(crateName, hasDenoLandOwner);
}
return hasDenoLandOwner;
}
}