Repository navigation
Expand file tree
/
Copy pathapi.ts
More file actions
112 lines (102 loc) · 3.73 KB
/
Copy pathapi.ts
File metadata and controls
112 lines (102 loc) · 3.73 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
// bundlephobia.com (bundle size, similar packages, history), packagephobia.com (install size), npm search.
import {
bundlephobiaError,
type BundleSize,
type InstallSize,
parseHistory,
parseInstall,
parseSearch,
parseSimilar,
parseSize,
type SimilarPackage,
type Suggestion,
} from "./lib.ts";
const BUNDLEPHOBIA = "https://bundlephobia.com/api";
// Bundlephobia asks API clients to say who they are.
const HEADERS = { Accept: "application/json", "X-Bundlephobia-User": "Backslash launcher" };
const message = (e: unknown) => e instanceof Error ? e.message : String(e);
async function getJson(service: string, url: string, signal?: AbortSignal, headers: HeadersInit = HEADERS) {
let res: Response;
try {
res = await fetch(url, { headers, signal });
} catch (e) {
if (signal?.aborted) throw e;
throw new Error(`Can't reach ${service}: ${message(e)}`);
}
if (res.status === 429) throw new Error(`${service} is rate limiting: try again in a minute`);
const text = await res.text();
let json: unknown;
try {
json = JSON.parse(text);
} catch {
if (!res.ok) throw new Error(`${service} answered ${res.status}`);
throw new Error(`${service} sent something unexpected`);
}
return { ok: res.ok, status: res.status, json: json as any };
}
/** Builds (or reads bundlephobia's cache of) the package: can take a while for a new version. */
export async function bundleSize(spec: string, signal?: AbortSignal): Promise<BundleSize> {
const { ok, status, json } = await getJson(
"Bundlephobia",
`${BUNDLEPHOBIA}/size?package=${encodeURIComponent(spec)}&record=true`,
signal,
);
const size = ok ? parseSize(json) : undefined;
if (size) return size;
if (json?.error) throw new Error(bundlephobiaError(json, spec));
throw new Error(`Bundlephobia answered ${status} for ${spec}`);
}
/** What `npm install` downloads; undefined when packagephobia doesn't know. */
export async function installSize(spec: string, signal?: AbortSignal): Promise<InstallSize | undefined> {
const { ok, json } = await getJson(
"Packagephobia",
`https://packagephobia.com/v2/api.json?p=${encodeURIComponent(spec)}`,
signal,
{ Accept: "application/json" },
);
return ok ? parseInstall(json) : undefined;
}
export async function similar(name: string, signal?: AbortSignal) {
const { ok, json } = await getJson(
"Bundlephobia",
`${BUNDLEPHOBIA}/similar-packages?package=${encodeURIComponent(name)}`,
signal,
);
return ok ? parseSimilar(json) : { packages: [] };
}
export async function history(name: string, signal?: AbortSignal) {
const { ok, json } = await getJson(
"Bundlephobia",
`${BUNDLEPHOBIA}/package-history?package=${encodeURIComponent(name)}`,
signal,
);
return ok ? parseHistory(json) : [];
}
export async function search(text: string, signal?: AbortSignal): Promise<Suggestion[]> {
const { ok, status, json } = await getJson(
"npm",
`https://registry.npmjs.org/-/v1/search?text=${encodeURIComponent(text)}&size=12`,
signal,
{ Accept: "application/json" },
);
if (!ok) throw new Error(`npm search answered ${status}`);
return parseSearch(json);
}
/** Sizes of the similar packages that don't come with one, a few at a time, never failing. */
export async function measure(
packages: SimilarPackage[],
signal?: AbortSignal,
max = 6,
): Promise<SimilarPackage[]> {
return await Promise.all(
packages.slice(0, max).map(async (p) => {
if (p.gzip !== undefined) return p;
try {
const b = await bundleSize(p.name, AbortSignal.any([AbortSignal.timeout(15_000), ...(signal ? [signal] : [])]));
return { ...p, gzip: b.gzip, size: b.size, description: p.description ?? b.description };
} catch {
return p;
}
}),
);
}