install-guard 1.0.1 → 1.1.2

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.
@@ -0,0 +1,99 @@
1
+ import { getCached, setCache } from "./cache.js";
2
+
3
+ function encodePkg(pkg) {
4
+ return encodeURIComponent(pkg).replace("%40", "@");
5
+ }
6
+
7
+ async function fetchJSON(url) {
8
+ const res = await fetch(url);
9
+ if (!res.ok) throw new Error(`HTTP ${res.status} for ${url}`);
10
+ return res.json();
11
+ }
12
+
13
+ /**
14
+ * Fetches full registry metadata for a package.
15
+ * Returns the raw document from registry.npmjs.org/<pkg>
16
+ */
17
+ export async function getRegistryData(pkg) {
18
+ const key = `registry:${pkg}`;
19
+ const cached = getCached(key);
20
+ if (cached) return cached;
21
+
22
+ const data = await fetchJSON(`https://registry.npmjs.org/${encodePkg(pkg)}`);
23
+ setCache(key, data);
24
+ return data;
25
+ }
26
+
27
+ /**
28
+ * Fetches weekly download count.
29
+ */
30
+ export async function getDownloads(pkg) {
31
+ const key = `downloads:${pkg}`;
32
+ const cached = getCached(key);
33
+ if (cached) return cached;
34
+
35
+ try {
36
+ const data = await fetchJSON(
37
+ `https://api.npmjs.org/downloads/point/last-week/${encodePkg(pkg)}`
38
+ );
39
+ setCache(key, data);
40
+ return data;
41
+ } catch {
42
+ return { downloads: 0 };
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Resolves version, fetches metadata + downloads, returns a normalized context
48
+ * that every check module can consume.
49
+ */
50
+ export async function buildContext(pkg, requestedVersion) {
51
+ const registry = await getRegistryData(pkg);
52
+ const latest = registry["dist-tags"]?.latest;
53
+ if (!latest) throw new Error(`No published version found for "${pkg}"`);
54
+
55
+ const version = requestedVersion || latest;
56
+ const versionData = registry.versions?.[version];
57
+ if (!versionData) throw new Error(`Version "${version}" not found for "${pkg}"`);
58
+
59
+ const timeData = registry.time || {};
60
+ const allVersions = Object.keys(registry.versions || {});
61
+ const versionIndex = allVersions.indexOf(version);
62
+ const previousVersion = versionIndex > 0 ? allVersions[versionIndex - 1] : null;
63
+ const previousVersionData = previousVersion
64
+ ? registry.versions[previousVersion]
65
+ : null;
66
+
67
+ const downloads = await getDownloads(pkg);
68
+
69
+ return {
70
+ name: registry.name,
71
+ version,
72
+ previousVersion,
73
+ description: registry.description || "",
74
+ downloads: downloads.downloads || 0,
75
+ maintainers: registry.maintainers || [],
76
+ license: versionData.license || registry.license || "Unknown",
77
+ publishedAt: timeData[version],
78
+ previousPublishedAt: previousVersion ? timeData[previousVersion] : null,
79
+ firstPublished: timeData.created,
80
+ repository: registry.repository?.url || versionData.repository?.url || null,
81
+ deprecated: versionData.deprecated || false,
82
+ totalVersions: allVersions.length,
83
+ allVersions,
84
+
85
+ // Script data
86
+ scripts: versionData.scripts || {},
87
+
88
+ // Dependency data
89
+ dependencies: versionData.dependencies || {},
90
+ previousDependencies: previousVersionData?.dependencies || {},
91
+
92
+ // Maintainer history — registry only exposes current maintainers
93
+ currentMaintainers: registry.maintainers || [],
94
+
95
+ // Raw registry for advanced checks
96
+ _registry: registry,
97
+ _versionData: versionData,
98
+ };
99
+ }
Binary file