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
|
import type { Package, PackageVersion, PackageTag } from "../db/schema";
export type ParsedPackageName = {
scope: string | null;
name: string;
full: string;
};
export function parsePackageName(raw: string): ParsedPackageName | null {
if (!raw || typeof raw !== "string") return null;
const decoded = (() => {
try {
return decodeURIComponent(raw);
} catch {
return raw;
}
})();
const trimmed = decoded.trim();
if (!trimmed) return null;
const safeSegment = /^[a-z0-9][a-z0-9._-]*$/i;
if (trimmed.startsWith("@")) {
const slash = trimmed.indexOf("/");
if (slash < 2) return null;
const scope = trimmed.slice(0, slash);
const name = trimmed.slice(slash + 1);
const scopeBody = scope.slice(1);
if (!safeSegment.test(scopeBody)) return null;
if (!safeSegment.test(name)) return null;
return { scope, name, full: `${scope}/${name}` };
}
if (!safeSegment.test(trimmed)) return null;
return { scope: null, name: trimmed, full: trimmed };
}
export function computeShasum(bytes: Uint8Array): string {
const h = new Bun.CryptoHasher("sha1");
h.update(bytes);
return h.digest("hex");
}
export function computeIntegrity(bytes: Uint8Array): string {
const h = new Bun.CryptoHasher("sha512");
h.update(bytes);
const digest = h.digest();
const b64 = Buffer.from(digest).toString("base64");
return `sha512-${b64}`;
}
export function resolveRepoFromPackageJson(
meta: unknown
): { owner: string; repo: string } | null {
if (!meta || typeof meta !== "object") return null;
const m = meta as Record<string, unknown>;
const repoField = m["repository"];
let url: string | null = null;
if (typeof repoField === "string") {
url = repoField;
} else if (repoField && typeof repoField === "object") {
const u = (repoField as Record<string, unknown>)["url"];
if (typeof u === "string") url = u;
}
if (!url) return null;
return parseRepoUrl(url);
}
export function parseRepoUrl(
urlRaw: string
): { owner: string; repo: string } | null {
if (!urlRaw || typeof urlRaw !== "string") return null;
let url = urlRaw.trim();
if (!url) return null;
if (url.startsWith("git+")) url = url.slice(4);
if (!url.includes("://") && url.includes(":") && url.includes("/")) {
const colon = url.lastIndexOf(":");
const tail = url.slice(colon + 1);
if (tail.includes("/")) {
return splitOwnerRepo(tail);
}
}
try {
const parsed = new URL(url);
const path = parsed.pathname.replace(/^\/+/, "");
return splitOwnerRepo(path);
} catch {
return splitOwnerRepo(url);
}
}
function splitOwnerRepo(
path: string
): { owner: string; repo: string } | null {
const clean = path.replace(/\.git$/, "").replace(/^\/+|\/+$/g, "");
const parts = clean.split("/").filter(Boolean);
if (parts.length < 2) return null;
const owner = parts[parts.length - 2];
const repo = parts[parts.length - 1];
if (!owner || !repo) return null;
return { owner, repo };
}
export function buildPackument(
pkg: Package,
versions: PackageVersion[],
tags: PackageTag[],
baseUrl: string
): Record<string, unknown> {
const fullName = pkg.scope ? `${pkg.scope}/${pkg.name}` : pkg.name;
const base = baseUrl.replace(/\/+$/, "");
const versionsOut: Record<string, Record<string, unknown>> = {};
const timeOut: Record<string, string> = {};
for (const v of versions) {
let meta: Record<string, unknown> = {};
try {
meta = v.metadata ? JSON.parse(v.metadata) : {};
} catch {
meta = {};
}
const tarballUrl = `${base}/npm/${encodeURI(fullName)}/-/${pkg.name}-${v.version}.tgz`;
versionsOut[v.version] = {
...meta,
name: fullName,
version: v.version,
dist: {
tarball: tarballUrl,
shasum: v.shasum,
integrity: v.integrity ?? undefined,
},
...(v.yanked ? { _yanked: true, _yankedReason: v.yankedReason } : {}),
};
const pub = v.publishedAt instanceof Date
? v.publishedAt.toISOString()
: new Date(v.publishedAt as unknown as string).toISOString();
timeOut[v.version] = pub;
}
const distTags: Record<string, string> = {};
const versionById = new Map(versions.map((v) => [v.id, v]));
for (const t of tags) {
const v = versionById.get(t.versionId);
if (v) distTags[t.tag] = v.version;
}
if (!distTags["latest"] && versions.length > 0) {
const sorted = [...versions].sort((a, b) => {
const ad = new Date(a.publishedAt as unknown as string).getTime();
const bd = new Date(b.publishedAt as unknown as string).getTime();
return bd - ad;
});
distTags["latest"] = sorted[0].version;
}
return {
_id: fullName,
name: fullName,
description: pkg.description ?? undefined,
"dist-tags": distTags,
versions: versionsOut,
time: timeOut,
homepage: pkg.homepage ?? undefined,
license: pkg.license ?? undefined,
readme: pkg.readme ?? undefined,
};
}
export function tarballFilename(
parsed: ParsedPackageName,
version: string
): string {
return `${parsed.name}-${version}.tgz`;
}
|