Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
packages-api.ts17.0 KB · 581 lines
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/**
 * npm-compatible package registry HTTP endpoints (Block C2).
 *
 * Two surfaces on one sub-app:
 *   1) /api/packages/... — JSON helpers the UI uses
 *   2) /npm/...          — the actual npm client protocol
 *
 * The npm client URL-encodes scoped names like `@acme/foo` as
 * `@acme%2Ffoo`, and also may send them un-encoded. Both work because we
 * parse the full tail of the path ourselves instead of relying on Hono's
 * parameter extraction (which struggles with `@` and `/` in names).
 */

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 } from "../db/schema";
import { softAuth, requireAuth } from "../middleware/auth";
import type { AuthEnv } from "../middleware/auth";
import { audit } from "../lib/notify";
import {
  parsePackageName,
  computeShasum,
  computeIntegrity,
  buildPackument,
  resolveRepoFromPackageJson,
  tarballFilename,
} from "../lib/packages";

const api = new Hono<AuthEnv>();
api.use("*", softAuth);

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

type NpmPublishBody = {
  name?: string;
  "dist-tags"?: Record<string, string>;
  versions?: Record<string, Record<string, unknown>>;
  _attachments?: Record<
    string,
    { content_type?: string; data: string; length?: number }
  >;
};

/**
 * Extract the package name and optional trailing segment from a path like
 *   /npm/@scope/foo
 *   /npm/@scope%2Ffoo
 *   /npm/foo
 *   /npm/@scope/foo/-/foo-1.0.0.tgz
 *   /npm/foo/-/foo-1.0.0.tgz
 *   /npm/foo/-rev/42
 */
function parseNpmPath(path: string): {
  nameRaw: string;
  tail: string | null;
  revTail: string | null;
} {
  const rest = path.replace(/^\/+/, "").replace(/^npm\/+/, "");

  // Split on "/-/" (tarball endpoint) first.
  const dashIdx = rest.indexOf("/-/");
  if (dashIdx !== -1) {
    return {
      nameRaw: rest.slice(0, dashIdx),
      tail: rest.slice(dashIdx + 3),
      revTail: null,
    };
  }
  // "-rev" style (npm unpublish).
  const revIdx = rest.indexOf("/-rev/");
  if (revIdx !== -1) {
    return {
      nameRaw: rest.slice(0, revIdx),
      tail: null,
      revTail: rest.slice(revIdx + 6),
    };
  }
  return { nameRaw: rest, tail: null, revTail: null };
}

function baseUrlFrom(req: Request): string {
  try {
    const u = new URL(req.url);
    return `${u.protocol}//${u.host}`;
  } catch {
    return "";
  }
}

async function loadRepo(owner: string, repo: string) {
  const [row] = await db
    .select({
      id: repositories.id,
      name: repositories.name,
      ownerId: repositories.ownerId,
      visibility: repositories.visibility,
    })
    .from(repositories)
    .innerJoin(users, eq(repositories.ownerId, users.id))
    .where(and(eq(users.username, owner), eq(repositories.name, repo)))
    .limit(1);
  return row || null;
}

async function loadPackage(
  repoId: string,
  scope: string | null,
  name: string
): Promise<Package | null> {
  const conds = [
    eq(packages.repositoryId, repoId),
    eq(packages.ecosystem, "npm"),
    eq(packages.name, name),
  ];
  const rows = await db
    .select()
    .from(packages)
    .where(and(...conds))
    .limit(20);
  // Manual scope equality because drizzle's eq + null is awkward.
  const match = rows.find((p) => (p.scope ?? null) === (scope ?? null));
  return match || null;
}

async function loadPackageByName(
  scope: string | null,
  name: string
): Promise<Package | null> {
  const rows = await db
    .select()
    .from(packages)
    .where(and(eq(packages.ecosystem, "npm"), eq(packages.name, name)))
    .limit(50);
  const match = rows.find((p) => (p.scope ?? null) === (scope ?? null));
  return match || null;
}

// ---------------------------------------------------------------------------
// UI-facing JSON helpers
// ---------------------------------------------------------------------------

api.get("/api/packages/:owner/:repo", async (c) => {
  const { owner, repo } = c.req.param();
  try {
    const repoRow = await loadRepo(owner, repo);
    if (!repoRow) return c.json({ error: "repo not found" }, 404);
    const rows = await db
      .select()
      .from(packages)
      .where(
        and(
          eq(packages.repositoryId, repoRow.id),
          eq(packages.ecosystem, "npm")
        )
      )
      .orderBy(desc(packages.updatedAt));
    return c.json({ packages: rows });
  } catch (err) {
    console.error("[packages] list:", err);
    return c.json({ error: "service unavailable" }, 503);
  }
});

api.get("/api/packages/:owner/:repo/:pkgName{.+}", async (c) => {
  const { owner, repo, pkgName } = c.req.param();
  const parsed = parsePackageName(pkgName);
  if (!parsed) return c.json({ error: "invalid package name" }, 400);
  try {
    const repoRow = await loadRepo(owner, repo);
    if (!repoRow) return c.json({ error: "repo not found" }, 404);
    const pkg = await loadPackage(repoRow.id, parsed.scope, parsed.name);
    if (!pkg) return c.json({ error: "package not found" }, 404);
    const versions = await db
      .select()
      .from(packageVersions)
      .where(eq(packageVersions.packageId, pkg.id))
      .orderBy(desc(packageVersions.publishedAt));
    const tags = await db
      .select()
      .from(packageTags)
      .where(eq(packageTags.packageId, pkg.id));
    return c.json({ package: pkg, versions, tags });
  } catch (err) {
    console.error("[packages] detail:", err);
    return c.json({ error: "service unavailable" }, 503);
  }
});

// Yank endpoint (owner-only; marks a version as yanked but leaves it
// downloadable so existing installs don't break — matches npm semantics).
api.post(
  "/api/packages/:owner/:repo/:pkgName/:version/yank",
  requireAuth,
  async (c) => {
    const user = c.get("user")!;
    const { owner, repo, pkgName, version } = c.req.param();
    const parsed = parsePackageName(pkgName);
    if (!parsed) return c.json({ error: "invalid package name" }, 400);
    try {
      const repoRow = await loadRepo(owner, repo);
      if (!repoRow) return c.json({ error: "repo not found" }, 404);
      if (repoRow.ownerId !== user.id) {
        return c.json({ error: "forbidden" }, 403);
      }
      const pkg = await loadPackage(repoRow.id, parsed.scope, parsed.name);
      if (!pkg) return c.json({ error: "package not found" }, 404);

      await db
        .update(packageVersions)
        .set({ yanked: true, yankedReason: "yanked by owner" })
        .where(
          and(
            eq(packageVersions.packageId, pkg.id),
            eq(packageVersions.version, version)
          )
        );

      await audit({
        userId: user.id,
        repositoryId: repoRow.id,
        action: "package.yank",
        targetType: "package_version",
        targetId: pkg.id,
        metadata: { version, name: parsed.full },
      });

      return c.json({ ok: true });
    } catch (err) {
      console.error("[packages] yank:", err);
      return c.json({ error: "service unavailable" }, 503);
    }
  }
);

// ---------------------------------------------------------------------------
// npm protocol: packument + tarball
// ---------------------------------------------------------------------------

api.get("/npm/*", async (c) => {
  const { nameRaw, tail } = parseNpmPath(c.req.path);
  if (!nameRaw) return c.json({ error: "not found" }, 404);

  const parsed = parsePackageName(nameRaw);
  if (!parsed) return c.json({ error: "invalid package name" }, 400);

  try {
    const pkg = await loadPackageByName(parsed.scope, parsed.name);
    if (!pkg) return c.json({ error: "not found" }, 404);

    // Tarball request?
    if (tail) {
      const filename = decodeURIComponent(tail);
      const versions = await db
        .select()
        .from(packageVersions)
        .where(eq(packageVersions.packageId, pkg.id));

      // Match by filename → version. Filename is `<name>-<version>.tgz`.
      const match = versions.find(
        (v) => tarballFilename(parsed, v.version) === filename
      );
      if (!match || !match.tarball) {
        return c.json({ error: "tarball not found" }, 404);
      }
      const bytes = Buffer.from(match.tarball, "base64");
      return new Response(bytes, {
        status: 200,
        headers: {
          "Content-Type": "application/octet-stream",
          "Content-Length": String(bytes.length),
          "Cache-Control": "public, max-age=31536000, immutable",
        },
      });
    }

    // Packument request.
    const versions = await db
      .select()
      .from(packageVersions)
      .where(eq(packageVersions.packageId, pkg.id))
      .orderBy(desc(packageVersions.publishedAt));
    const tags = await db
      .select()
      .from(packageTags)
      .where(eq(packageTags.packageId, pkg.id));

    const doc = buildPackument(pkg, versions, tags, baseUrlFrom(c.req.raw));
    return c.json(doc);
  } catch (err) {
    console.error("[packages] npm get:", err);
    return c.json({ error: "service unavailable" }, 503);
  }
});

// ---------------------------------------------------------------------------
// npm protocol: publish (`npm publish` → PUT /<name>)
// ---------------------------------------------------------------------------

api.put("/npm/*", requireAuth, async (c) => {
  const user = c.get("user")!;
  const { nameRaw } = parseNpmPath(c.req.path);
  const parsed = parsePackageName(nameRaw);
  if (!parsed) {
    return c.json({ error: "invalid package name" }, 400);
  }

  let body: NpmPublishBody;
  try {
    body = await c.req.json<NpmPublishBody>();
  } catch {
    return c.json({ error: "invalid JSON body" }, 400);
  }

  const versionsObj = body.versions || {};
  const versionKeys = Object.keys(versionsObj);
  if (versionKeys.length === 0) {
    return c.json({ error: "no version in payload" }, 400);
  }
  // npm always sends exactly one version per publish.
  const version = versionKeys[0];
  const versionMeta = versionsObj[version] || {};

  const attachments = body._attachments || {};
  const attachKeys = Object.keys(attachments);
  if (attachKeys.length === 0) {
    return c.json({ error: "no tarball attachment" }, 400);
  }
  const attachment = attachments[attachKeys[0]];
  if (!attachment || !attachment.data) {
    return c.json({ error: "empty tarball attachment" }, 400);
  }

  const tarballBytes = Buffer.from(attachment.data, "base64");
  if (tarballBytes.length === 0) {
    return c.json({ error: "tarball decoded to zero bytes" }, 400);
  }

  // Resolve owner+repo from the metadata's repository.url.
  const repoRef = resolveRepoFromPackageJson(versionMeta);
  if (!repoRef) {
    return c.json(
      {
        error:
          "repository.url must point to a gluecron repo you own (e.g. http://host/:owner/:repo.git)",
      },
      400
    );
  }

  try {
    const repoRow = await loadRepo(repoRef.owner, repoRef.repo);
    if (!repoRow) {
      return c.json(
        { error: `repo ${repoRef.owner}/${repoRef.repo} not found` },
        404
      );
    }
    if (repoRow.ownerId !== user.id) {
      return c.json(
        { error: "you do not own the repository named in repository.url" },
        403
      );
    }

    // Upsert the package row.
    let pkg = await loadPackage(repoRow.id, parsed.scope, parsed.name);
    if (!pkg) {
      const description =
        typeof versionMeta.description === "string"
          ? (versionMeta.description as string)
          : null;
      const homepage =
        typeof versionMeta.homepage === "string"
          ? (versionMeta.homepage as string)
          : null;
      const license =
        typeof versionMeta.license === "string"
          ? (versionMeta.license as string)
          : null;
      const readme =
        typeof (body as Record<string, unknown>).readme === "string"
          ? ((body as Record<string, unknown>).readme as string)
          : typeof versionMeta.readme === "string"
            ? (versionMeta.readme as string)
            : null;

      const [inserted] = await db
        .insert(packages)
        .values({
          repositoryId: repoRow.id,
          ecosystem: "npm",
          scope: parsed.scope,
          name: parsed.name,
          description,
          readme,
          homepage,
          license,
          visibility: repoRow.visibility === "private" ? "private" : "public",
        })
        .returning();
      pkg = inserted;
    }
    if (!pkg) {
      return c.json({ error: "failed to create package" }, 503);
    }

    // Reject duplicate version.
    const [existing] = await db
      .select()
      .from(packageVersions)
      .where(
        and(
          eq(packageVersions.packageId, pkg.id),
          eq(packageVersions.version, version)
        )
      )
      .limit(1);
    if (existing) {
      return c.json(
        {
          error: `You cannot publish over the previously published version ${version}.`,
        },
        409
      );
    }

    const shasum = computeShasum(tarballBytes);
    const integrity = computeIntegrity(tarballBytes);

    const [insertedVersion] = await db
      .insert(packageVersions)
      .values({
        packageId: pkg.id,
        version,
        shasum,
        integrity,
        sizeBytes: tarballBytes.length,
        metadata: JSON.stringify(versionMeta),
        tarball: tarballBytes.toString("base64"),
        publishedBy: user.id,
      })
      .returning();

    // Upsert "latest" dist-tag (and any other tags from the payload).
    const distTags = body["dist-tags"] || { latest: version };
    for (const [tag, tagVersion] of Object.entries(distTags)) {
      if (tagVersion !== version) continue; // Only set tags pointing at this publish.
      const [existingTag] = await db
        .select()
        .from(packageTags)
        .where(
          and(
            eq(packageTags.packageId, pkg.id),
            eq(packageTags.tag, tag)
          )
        )
        .limit(1);
      if (existingTag) {
        await db
          .update(packageTags)
          .set({ versionId: insertedVersion.id, updatedAt: new Date() })
          .where(eq(packageTags.id, existingTag.id));
      } else {
        await db.insert(packageTags).values({
          packageId: pkg.id,
          tag,
          versionId: insertedVersion.id,
        });
      }
    }

    // Update package bookkeeping fields on every publish (license/description
    // may evolve version-to-version; we keep the most recent).
    await db
      .update(packages)
      .set({
        updatedAt: new Date(),
        description:
          typeof versionMeta.description === "string"
            ? (versionMeta.description as string)
            : pkg.description,
        homepage:
          typeof versionMeta.homepage === "string"
            ? (versionMeta.homepage as string)
            : pkg.homepage,
        license:
          typeof versionMeta.license === "string"
            ? (versionMeta.license as string)
            : pkg.license,
      })
      .where(eq(packages.id, pkg.id));

    await audit({
      userId: user.id,
      repositoryId: repoRow.id,
      action: "package.publish",
      targetType: "package_version",
      targetId: insertedVersion.id,
      metadata: {
        name: parsed.full,
        version,
        size: tarballBytes.length,
      },
    });

    return c.json({ ok: true, id: parsed.full, version }, 201);
  } catch (err) {
    console.error("[packages] publish:", err);
    return c.json({ error: "service unavailable" }, 503);
  }
});

// npm unpublish: DELETE /npm/<name>/-rev/<rev> — we treat this as a yank.
api.delete("/npm/*", requireAuth, async (c) => {
  const user = c.get("user")!;
  const { nameRaw, revTail } = parseNpmPath(c.req.path);
  const parsed = parsePackageName(nameRaw);
  if (!parsed) return c.json({ error: "invalid package name" }, 400);
  if (!revTail) {
    return c.json(
      { error: "unpublish without rev is not supported" },
      400
    );
  }

  try {
    const pkg = await loadPackageByName(parsed.scope, parsed.name);
    if (!pkg) return c.json({ error: "not found" }, 404);
    const [repoRow] = await db
      .select()
      .from(repositories)
      .where(eq(repositories.id, pkg.repositoryId))
      .limit(1);
    if (!repoRow || repoRow.ownerId !== user.id) {
      return c.json({ error: "forbidden" }, 403);
    }

    // Yank the latest version.
    const [latest] = await db
      .select()
      .from(packageVersions)
      .where(eq(packageVersions.packageId, pkg.id))
      .orderBy(desc(packageVersions.publishedAt))
      .limit(1);
    if (latest) {
      await db
        .update(packageVersions)
        .set({ yanked: true, yankedReason: "unpublished" })
        .where(eq(packageVersions.id, latest.id));
    }

    await audit({
      userId: user.id,
      repositoryId: repoRow.id,
      action: "package.unpublish",
      targetType: "package",
      targetId: pkg.id,
      metadata: { name: parsed.full },
    });

    return c.json({ ok: true });
  } catch (err) {
    console.error("[packages] unpublish:", err);
    return c.json({ error: "service unavailable" }, 503);
  }
});

export default api;

// Re-export helpers internally for the UI route.
export type { Package, PackageVersion };