CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | /**
* Packages UI — lists packages for a repo, per-package detail (Block C2).
*
* GET /:owner/:repo/packages — list of published packages
* GET /:owner/:repo/packages/:pkg{.+} — detail + version list + install help
*
* Packages doesn't yet have a tab in RepoNav — we render with active="code"
* so the page still lays out correctly.
*/
import { Hono } from "hono";
import { and, desc, eq } from "drizzle-orm";
import { db } from "../db";
import {
packages,
packageVersions,
packageTags,
repositories,
users,
} from "../db/schema";
import type { Package, PackageVersion, PackageTag } from "../db/schema";
import { Layout } from "../views/layout";
import { RepoHeader, RepoNav } from "../views/components";
import { softAuth } from "../middleware/auth";
import type { AuthEnv } from "../middleware/auth";
import { getUnreadCount } from "../lib/unread";
import { parsePackageName } from "../lib/packages";
const ui = new Hono<AuthEnv>();
ui.use("*", softAuth);
async function loadRepo(owner: string, repo: string) {
const [row] = await db
.select({
id: repositories.id,
name: repositories.name,
ownerId: repositories.ownerId,
defaultBranch: repositories.defaultBranch,
starCount: repositories.starCount,
forkCount: repositories.forkCount,
})
.from(repositories)
.innerJoin(users, eq(repositories.ownerId, users.id))
.where(and(eq(users.username, owner), eq(repositories.name, repo)))
.limit(1);
return row || null;
}
function relTime(d: Date | string | null): string {
if (!d) return "";
const t = typeof d === "string" ? new Date(d) : d;
const diff = Date.now() - t.getTime();
const mins = Math.floor(diff / 60000);
if (mins < 1) return "just now";
if (mins < 60) return `${mins}m ago`;
const hrs = Math.floor(mins / 60);
if (hrs < 24) return `${hrs}h ago`;
const days = Math.floor(hrs / 24);
if (days < 30) return `${days}d ago`;
return t.toLocaleDateString();
}
function fullPkgName(pkg: { scope: string | null; name: string }): string {
return pkg.scope ? `${pkg.scope}/${pkg.name}` : pkg.name;
}
function humanSize(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
}
// ---------------------------------------------------------------------------
// List page
// ---------------------------------------------------------------------------
ui.get("/:owner/:repo/packages", async (c) => {
const user = c.get("user");
const { owner, repo } = c.req.param();
const repoRow = await loadRepo(owner, repo);
if (!repoRow) return c.notFound();
let rows: (Package & { latestVersion: string | null })[] = [];
try {
const pkgs = await db
.select()
.from(packages)
.where(
and(
eq(packages.repositoryId, repoRow.id),
eq(packages.ecosystem, "npm")
)
)
.orderBy(desc(packages.updatedAt));
// Fetch the "latest" tag for each package.
const latest = await Promise.all(
pkgs.map(async (p) => {
try {
const [tag] = await db
.select({
version: packageVersions.version,
})
.from(packageTags)
.innerJoin(
packageVersions,
eq(packageTags.versionId, packageVersions.id)
)
.where(
and(
eq(packageTags.packageId, p.id),
eq(packageTags.tag, "latest")
)
)
.limit(1);
return tag?.version || null;
} catch {
return null;
}
})
);
rows = pkgs.map((p, i) => ({ ...p, latestVersion: latest[i] }));
} catch (err) {
console.error("[packages] ui list:", err);
return c.text("Service unavailable", 503);
}
const unread = user ? await getUnreadCount(user.id) : 0;
const host = new URL(c.req.url).host;
const registryUrl = `${new URL(c.req.url).protocol}//${host}/npm/`;
return c.html(
<Layout
title={`Packages — ${owner}/${repo}`}
user={user}
notificationCount={unread}
>
<RepoHeader
owner={owner}
repo={repo}
starCount={repoRow.starCount}
forkCount={repoRow.forkCount}
currentUser={user?.username || null}
/>
<RepoNav owner={owner} repo={repo} active="code" />
<div style="max-width: 900px">
<h2 style="margin: 0 0 16px 0">Packages</h2>
{rows.length === 0 ? (
<div class="empty-state">
<p style="margin-bottom: 12px">
No npm packages published from this repository yet.
</p>
<div
style="text-align: left; background: var(--bg-secondary); border: 1px solid var(--border); border-radius: 6px; padding: 12px 16px; font-size: 13px; max-width: 640px; margin: 0 auto"
>
<strong>To publish:</strong>
<ol style="padding-left: 20px; margin: 8px 0 0 0; line-height: 1.6">
<li>
Create a personal access token at{" "}
<a href="/settings/tokens">/settings/tokens</a>.
</li>
<li>
Add to your <code>.npmrc</code>:
<pre style="background: var(--bg-tertiary); color: var(--text); padding: 8px 12px; border-radius: 4px; font-size: 12px; margin: 6px 0">
registry={registryUrl}
{"\n"}
//{host}/npm/:_authToken=YOUR_PAT
</pre>
</li>
<li>
In <code>package.json</code>, point{" "}
<code>repository.url</code> at this repo
(<code>
{`http://${host}/${owner}/${repo}.git`}
</code>
).
</li>
<li>
Run <code>npm publish</code>.
</li>
</ol>
</div>
</div>
) : (
<div class="panel" style="overflow: hidden">
{rows.map((p) => {
const fullName = fullPkgName(p);
return (
<a
href={`/${owner}/${repo}/packages/${encodeURIComponent(fullName)}`}
style="display: block; padding: 12px 16px; border-bottom: 1px solid var(--border); text-decoration: none; color: inherit"
>
<div style="display: flex; justify-content: space-between; align-items: baseline; gap: 12px">
<div style="flex: 1; min-width: 0">
<div style="font-weight: 600">{fullName}</div>
{p.description && (
<div style="font-size: 13px; color: var(--text-muted); margin-top: 2px">
{p.description}
</div>
)}
</div>
<div style="font-size: 12px; color: var(--text-muted); white-space: nowrap">
{p.latestVersion ? (
<span>
<code>{p.latestVersion}</code>
</span>
) : (
<span>no versions</span>
)}
{" · "}
<span>{relTime(p.updatedAt)}</span>
</div>
</div>
</a>
);
})}
</div>
)}
</div>
</Layout>
);
});
// ---------------------------------------------------------------------------
// Detail page
// ---------------------------------------------------------------------------
ui.get("/:owner/:repo/packages/:pkgName{.+}", async (c) => {
const user = c.get("user");
const { owner, repo, pkgName } = c.req.param();
const parsed = parsePackageName(pkgName);
if (!parsed) {
return c.text("Invalid package name", 400);
}
const repoRow = await loadRepo(owner, repo);
if (!repoRow) return c.notFound();
let pkg: Package | null = null;
let versions: PackageVersion[] = [];
let tags: PackageTag[] = [];
try {
const candidates = await db
.select()
.from(packages)
.where(
and(
eq(packages.repositoryId, repoRow.id),
eq(packages.ecosystem, "npm"),
eq(packages.name, parsed.name)
)
)
.limit(10);
pkg =
candidates.find((p) => (p.scope ?? null) === (parsed.scope ?? null)) ||
null;
if (pkg) {
versions = await db
.select()
.from(packageVersions)
.where(eq(packageVersions.packageId, pkg.id))
.orderBy(desc(packageVersions.publishedAt));
tags = await db
.select()
.from(packageTags)
.where(eq(packageTags.packageId, pkg.id));
}
} catch (err) {
console.error("[packages] ui detail:", err);
return c.text("Service unavailable", 503);
}
if (!pkg) return c.notFound();
const unread = user ? await getUnreadCount(user.id) : 0;
const fullName = fullPkgName(pkg);
const latestTag = tags.find((t) => t.tag === "latest");
const latestVersion =
latestTag && versions.find((v) => v.id === latestTag.versionId);
const isOwner = !!user && user.id === repoRow.ownerId;
const host = new URL(c.req.url).host;
return c.html(
<Layout
title={`${fullName} — ${owner}/${repo}`}
user={user}
notificationCount={unread}
>
<RepoHeader
owner={owner}
repo={repo}
starCount={repoRow.starCount}
forkCount={repoRow.forkCount}
currentUser={user?.username || null}
/>
<RepoNav owner={owner} repo={repo} active="code" />
<div style="max-width: 900px">
<div style="font-size: 12px; color: var(--text-muted); margin-bottom: 6px">
<a href={`/${owner}/${repo}/packages`}>Packages</a>
{" / "}
<span>{fullName}</span>
</div>
<h2 style="margin: 0 0 4px 0">{fullName}</h2>
{pkg.description && (
<p style="color: var(--text-muted); margin: 0 0 16px 0">
{pkg.description}
</p>
)}
<div style="display: grid; grid-template-columns: 1fr 280px; gap: 24px">
<div>
<h3 style="font-size: 14px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--text-muted); margin: 0 0 8px 0">
Install
</h3>
<pre style="background: var(--bg-tertiary); color: var(--text); padding: 10px 14px; border-radius: 6px; font-size: 13px; overflow-x: auto">
npm install {fullName}
{latestVersion ? `@${latestVersion.version}` : ""}
</pre>
{pkg.readme && (
<>
<h3 style="font-size: 14px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--text-muted); margin: 20px 0 8px 0">
Readme
</h3>
<pre
style="background: var(--bg-secondary); border: 1px solid var(--border); border-radius: 6px; padding: 12px 14px; white-space: pre-wrap; font-size: 13px; line-height: 1.5"
>
{pkg.readme}
</pre>
</>
)}
<h3 style="font-size: 14px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--text-muted); margin: 20px 0 8px 0">
Versions
</h3>
{versions.length === 0 ? (
<div class="empty-state">
<p>No versions yet.</p>
</div>
) : (
<div class="panel" style="overflow: hidden">
{versions.map((v) => (
<div
style="padding: 10px 14px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; gap: 12px"
>
<div style="flex: 1; min-width: 0">
<div style="font-weight: 500">
<code>{v.version}</code>
{v.yanked && (
<span
style="margin-left: 8px; font-size: 11px; color: var(--red); text-transform: uppercase"
>
yanked
</span>
)}
</div>
<div style="font-size: 12px; color: var(--text-muted); margin-top: 2px">
{humanSize(v.sizeBytes)} · published{" "}
{relTime(v.publishedAt)}
{v.shasum && (
<>
{" · sha1 "}
<code style="font-size: 11px">
{v.shasum.slice(0, 12)}
</code>
</>
)}
</div>
</div>
<a
class="btn btn-sm"
href={`/npm/${encodeURIComponent(fullName)}/-/${pkg.name}-${v.version}.tgz`}
>
Download
</a>
{isOwner && !v.yanked && (
<form
method="post"
action={`/api/packages/${owner}/${repo}/${encodeURIComponent(fullName)}/${v.version}/yank`}
onsubmit="return confirm('Yank this version? It will still download, but will be flagged as yanked.')"
style="margin: 0"
>
<button
type="submit"
class="btn btn-sm btn-danger"
>
Yank
</button>
</form>
)}
</div>
))}
</div>
)}
</div>
<aside>
<div class="panel" style="padding: 12px 14px">
<div style="font-size: 12px; color: var(--text-muted); margin-bottom: 4px">
Registry
</div>
<code style="font-size: 12px">http://{host}/npm/</code>
{pkg.homepage && (
<>
<div style="font-size: 12px; color: var(--text-muted); margin: 10px 0 4px 0">
Homepage
</div>
<a href={pkg.homepage} style="font-size: 13px; word-break: break-all">
{pkg.homepage}
</a>
</>
)}
{pkg.license && (
<>
<div style="font-size: 12px; color: var(--text-muted); margin: 10px 0 4px 0">
License
</div>
<div style="font-size: 13px">{pkg.license}</div>
</>
)}
<div style="font-size: 12px; color: var(--text-muted); margin: 10px 0 4px 0">
Repository
</div>
<a
href={`/${owner}/${repo}`}
style="font-size: 13px; word-break: break-all"
>
{owner}/{repo}
</a>
</div>
</aside>
</div>
</div>
</Layout>
);
});
export default ui;
|