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
oci-registry.ts30.3 KB · 855 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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
/**
 * OCI Distribution Spec v1.0 — Container Registry
 *
 * Implements the standard Docker / OCI image push-pull protocol so teams
 * can push and pull images directly against Gluecron without GitHub Packages
 * or an external registry.
 *
 * URL surface (all under /v2/):
 *   GET  /v2/                              — version check (requires auth)
 *   HEAD /v2/:name/blobs/:digest           — check blob existence
 *   GET  /v2/:name/blobs/:digest           — download blob
 *   POST /v2/:name/blobs/uploads/          — start chunked upload
 *   PATCH /v2/:name/blobs/uploads/:uuid    — stream blob chunk
 *   PUT  /v2/:name/blobs/uploads/:uuid     — complete upload
 *   DELETE /v2/:name/blobs/:digest         — delete blob
 *   GET  /v2/:name/manifests/:ref          — get manifest (tag or digest)
 *   PUT  /v2/:name/manifests/:ref          — push manifest (create tag)
 *   DELETE /v2/:name/manifests/:ref        — delete manifest/tag
 *   GET  /v2/:name/tags/list               — list tags for a repo
 *   GET  /v2/_catalog                      — list all repositories
 *
 * Auth: Docker clients send `Authorization: Basic base64(user:token)`.
 * We validate against the api_tokens table (SHA-256 of the raw token,
 * same as every other Gluecron API surface). 401 responses carry
 * `WWW-Authenticate: Basic realm="Gluecron Container Registry"` and
 * the OCI error JSON body format.
 *
 * Storage: blobs on disk at ${OCI_STORE_PATH}/blobs/sha256/<hex>,
 * manifests at ${OCI_STORE_PATH}/manifests/<name>/<ref>.
 * In-progress uploads accumulate in ${OCI_STORE_PATH}/uploads/<uuid>.
 */

import { Hono } from "hono";
import { createHash, randomUUID } from "crypto";
import { eq, and, desc } from "drizzle-orm";
import { mkdir } from "node:fs/promises";
import { join } from "node:path";
import { db } from "../db";
import { apiTokens, users, ociRepositories, ociTags } from "../db/schema";
import { config } from "../lib/config";
import type { AuthEnv } from "../middleware/auth";

// ---------------------------------------------------------------------------
// OCI error helpers
// ---------------------------------------------------------------------------

type OciErrorCode =
  | "UNAUTHORIZED"
  | "DENIED"
  | "UNSUPPORTED"
  | "BLOB_UNKNOWN"
  | "BLOB_UPLOAD_INVALID"
  | "BLOB_UPLOAD_UNKNOWN"
  | "DIGEST_INVALID"
  | "MANIFEST_BLOB_UNKNOWN"
  | "MANIFEST_INVALID"
  | "MANIFEST_UNKNOWN"
  | "NAME_INVALID"
  | "NAME_UNKNOWN"
  | "SIZE_INVALID"
  | "TAG_INVALID";

function ociError(
  code: OciErrorCode,
  message: string,
  detail?: unknown
): { errors: Array<{ code: string; message: string; detail?: unknown }> } {
  const err: { code: string; message: string; detail?: unknown } = {
    code,
    message,
  };
  if (detail !== undefined) err.detail = detail;
  return { errors: [err] };
}

// ---------------------------------------------------------------------------
// Auth
// ---------------------------------------------------------------------------

function sha256hex(value: string): string {
  return createHash("sha256").update(value).digest("hex");
}

type AuthResult =
  | { ok: true; user: { id: string; username: string } }
  | { ok: false };

/**
 * Docker clients send `Authorization: Basic base64("user:token")`.
 * We ignore the username part and validate the token (password) against
 * the api_tokens table just like Bearer token auth elsewhere.
 */
async function authenticateBasic(authHeader: string | undefined): Promise<AuthResult> {
  if (!authHeader) return { ok: false };

  let encoded: string;
  if (authHeader.startsWith("Basic ")) {
    encoded = authHeader.slice(6).trim();
  } else if (authHeader.startsWith("Bearer ")) {
    // Docker Desktop sometimes sends Bearer after the initial 401 WWW-Auth exchange
    const raw = authHeader.slice(7).trim();
    if (!raw) return { ok: false };
    const tokenHash = sha256hex(raw);
    try {
      const [tokenRow] = await db
        .select({ userId: apiTokens.userId, expiresAt: apiTokens.expiresAt, id: apiTokens.id })
        .from(apiTokens)
        .where(eq(apiTokens.tokenHash, tokenHash))
        .limit(1);
      if (!tokenRow) return { ok: false };
      if (tokenRow.expiresAt && new Date(tokenRow.expiresAt) < new Date()) return { ok: false };
      const [user] = await db
        .select({ id: users.id, username: users.username })
        .from(users)
        .where(eq(users.id, tokenRow.userId))
        .limit(1);
      if (!user) return { ok: false };
      db.update(apiTokens).set({ lastUsedAt: new Date() }).where(eq(apiTokens.id, tokenRow.id)).catch(() => {});
      return { ok: true, user };
    } catch {
      return { ok: false };
    }
  } else {
    return { ok: false };
  }

  let decoded: string;
  try {
    decoded = Buffer.from(encoded, "base64").toString("utf-8");
  } catch {
    return { ok: false };
  }

  // "user:token" — Docker spec allows colons in password, split on first colon only
  const colonIdx = decoded.indexOf(":");
  if (colonIdx < 0) return { ok: false };
  const rawToken = decoded.slice(colonIdx + 1);
  if (!rawToken) return { ok: false };

  const tokenHash = sha256hex(rawToken);
  try {
    const [tokenRow] = await db
      .select({ userId: apiTokens.userId, expiresAt: apiTokens.expiresAt, id: apiTokens.id })
      .from(apiTokens)
      .where(eq(apiTokens.tokenHash, tokenHash))
      .limit(1);
    if (!tokenRow) return { ok: false };
    if (tokenRow.expiresAt && new Date(tokenRow.expiresAt) < new Date()) return { ok: false };
    const [user] = await db
      .select({ id: users.id, username: users.username })
      .from(users)
      .where(eq(users.id, tokenRow.userId))
      .limit(1);
    if (!user) return { ok: false };
    // Best-effort touch
    db.update(apiTokens).set({ lastUsedAt: new Date() }).where(eq(apiTokens.id, tokenRow.id)).catch(() => {});
    return { ok: true, user };
  } catch {
    return { ok: false };
  }
}

const UNAUTHORIZED_HEADERS = {
  "WWW-Authenticate": 'Basic realm="Gluecron Container Registry"',
  "Content-Type": "application/json",
};

// ---------------------------------------------------------------------------
// Storage helpers
// ---------------------------------------------------------------------------

/** Resolve (and lazily create) storage directories. */
function storePath(...segments: string[]): string {
  return join(config.ociStorePath, ...segments);
}

async function ensureDir(path: string): Promise<void> {
  await mkdir(path, { recursive: true });
}

/** Absolute path to a finished blob file. */
function blobPath(digest: string): string {
  // digest = "sha256:<hex>" or just "<hex>"
  const hex = digest.startsWith("sha256:") ? digest.slice(7) : digest;
  return storePath("blobs", "sha256", hex);
}

/** Absolute path to a manifest file for a given image name + ref. */
function manifestPath(name: string, ref: string): string {
  return storePath("manifests", name, ref);
}

/** Absolute path to an in-progress upload chunk file. */
function uploadPath(uuid: string): string {
  return storePath("uploads", uuid);
}

/** Compute sha256 digest of a file on disk. */
async function digestOfFile(path: string): Promise<string> {
  const file = Bun.file(path);
  const buf = await file.arrayBuffer();
  const bytes = new Uint8Array(buf);
  const h = new Bun.CryptoHasher("sha256");
  h.update(bytes);
  return `sha256:${h.digest("hex")}`;
}

/** Validate that a "sha256:<hex64>" digest string is well-formed. */
function isValidDigest(digest: string): boolean {
  return /^sha256:[0-9a-f]{64}$/.test(digest);
}

// ---------------------------------------------------------------------------
// DB helpers — oci_repositories + oci_tags
// ---------------------------------------------------------------------------

/** Find or create an oci_repositories row for owner/image. */
async function findOrCreateOciRepo(
  ownerId: string,
  name: string,
  visibility: "public" | "private" = "private"
): Promise<string> {
  const [existing] = await db
    .select({ id: ociRepositories.id })
    .from(ociRepositories)
    .where(and(eq(ociRepositories.ownerId, ownerId), eq(ociRepositories.name, name)))
    .limit(1);
  if (existing) return existing.id;

  const [inserted] = await db
    .insert(ociRepositories)
    .values({ ownerId, name, visibility })
    .returning({ id: ociRepositories.id });
  return inserted.id;
}

/** Upsert a tag → digest mapping. */
async function upsertTag(repositoryId: string, tag: string, manifestDigest: string): Promise<void> {
  const [existing] = await db
    .select({ id: ociTags.id })
    .from(ociTags)
    .where(and(eq(ociTags.repositoryId, repositoryId), eq(ociTags.tag, tag)))
    .limit(1);

  if (existing) {
    await db
      .update(ociTags)
      .set({ manifestDigest, updatedAt: new Date() })
      .where(eq(ociTags.id, existing.id));
  } else {
    await db.insert(ociTags).values({ repositoryId, tag, manifestDigest });
  }
}

// ---------------------------------------------------------------------------
// Route setup
// ---------------------------------------------------------------------------

const registry = new Hono<AuthEnv>();

// The OCI spec uses `:name` which can contain slashes (e.g. "owner/image").
// Hono's wildcard param ({name:*}) doesn't play well with path segments, so
// we parse the image name from c.req.path directly in each handler.

// ---------------------------------------------------------------------------
// GET /v2/ — Version check
// ---------------------------------------------------------------------------
registry.get("/v2/", async (c) => {
  const auth = await authenticateBasic(c.req.header("authorization"));
  if (!auth.ok) {
    return c.json(ociError("UNAUTHORIZED", "authentication required"), 401, UNAUTHORIZED_HEADERS);
  }
  return c.json({}, 200, { "Docker-Distribution-API-Version": "registry/2.0" });
});

// ---------------------------------------------------------------------------
// GET /v2/_catalog — list all repositories the caller can see
// ---------------------------------------------------------------------------
registry.get("/v2/_catalog", async (c) => {
  const auth = await authenticateBasic(c.req.header("authorization"));
  if (!auth.ok) {
    return c.json(ociError("UNAUTHORIZED", "authentication required"), 401, UNAUTHORIZED_HEADERS);
  }

  try {
    const rows = await db
      .select({ name: ociRepositories.name })
      .from(ociRepositories)
      .where(eq(ociRepositories.ownerId, auth.user.id))
      .orderBy(ociRepositories.name);

    return c.json({ repositories: rows.map((r) => r.name) });
  } catch (err) {
    console.error("[oci] catalog:", err);
    return c.json(ociError("UNSUPPORTED", "service error"), 500);
  }
});

// ---------------------------------------------------------------------------
// Blob endpoints — /v2/:name/blobs/...
// We parse :name as a wildcard from the URL manually.
// ---------------------------------------------------------------------------

/** Extract image name + remainder from path like /v2/<name>/blobs/... */
function parseV2Path(
  path: string,
  segment: string
): { name: string; rest: string } | null {
  // /v2/<name>/blobs/...  or  /v2/<name>/manifests/... etc.
  const prefix = "/v2/";
  if (!path.startsWith(prefix)) return null;
  const after = path.slice(prefix.length);
  const segIdx = after.indexOf(`/${segment}/`);
  if (segIdx < 0) {
    // check for exact match at end (e.g., /v2/<name>/tags/list)
    const segEnd = after.indexOf(`/${segment}`);
    if (segEnd < 0) return null;
    const name = after.slice(0, segEnd);
    const rest = after.slice(segEnd + segment.length + 1);
    return { name, rest };
  }
  const name = after.slice(0, segIdx);
  const rest = after.slice(segIdx + segment.length + 2);
  return { name, rest };
}

// HEAD /v2/:name/blobs/:digest
registry.on(["HEAD", "GET"], "/v2/*", async (c) => {
  const path = c.req.path;

  // ── HEAD/GET /v2/:name/blobs/:digest ──────────────────────────────────────
  if (/\/blobs\/sha256:[0-9a-f]{64}$/.test(path)) {
    const auth = await authenticateBasic(c.req.header("authorization"));
    if (!auth.ok) {
      return c.json(ociError("UNAUTHORIZED", "authentication required"), 401, UNAUTHORIZED_HEADERS);
    }

    const digestMatch = path.match(/\/blobs\/(sha256:[0-9a-f]{64})$/);
    if (!digestMatch) {
      return c.json(ociError("DIGEST_INVALID", "invalid digest"), 400);
    }
    const digest = digestMatch[1];
    const bp = blobPath(digest);
    const file = Bun.file(bp);
    const exists = await file.exists();
    if (!exists) {
      return c.json(ociError("BLOB_UNKNOWN", "blob unknown to registry"), 404, {
        "Docker-Content-Digest": digest,
      });
    }

    const headers: Record<string, string> = {
      "Docker-Content-Digest": digest,
      "Content-Length": String(file.size),
      "Content-Type": "application/octet-stream",
    };

    if (c.req.method === "HEAD") {
      return new Response(null, { status: 200, headers });
    }

    // GET — stream the blob
    return new Response(file.stream(), { status: 200, headers });
  }

  // ── GET /v2/:name/tags/list ───────────────────────────────────────────────
  if (path.endsWith("/tags/list")) {
    const auth = await authenticateBasic(c.req.header("authorization"));
    if (!auth.ok) {
      return c.json(ociError("UNAUTHORIZED", "authentication required"), 401, UNAUTHORIZED_HEADERS);
    }

    const parsed = parseV2Path(path, "tags");
    if (!parsed) return c.json(ociError("NAME_INVALID", "invalid name"), 400);
    const name = parsed.name;

    try {
      const [repo] = await db
        .select({ id: ociRepositories.id })
        .from(ociRepositories)
        .where(and(eq(ociRepositories.ownerId, auth.user.id), eq(ociRepositories.name, name)))
        .limit(1);

      if (!repo) {
        return c.json({ name, tags: [] });
      }

      const tagRows = await db
        .select({ tag: ociTags.tag })
        .from(ociTags)
        .where(eq(ociTags.repositoryId, repo.id))
        .orderBy(ociTags.tag);

      return c.json({ name, tags: tagRows.map((t) => t.tag) });
    } catch (err) {
      console.error("[oci] tags/list:", err);
      return c.json(ociError("UNSUPPORTED", "service error"), 500);
    }
  }

  // ── GET /v2/:name/manifests/:ref ──────────────────────────────────────────
  if (path.includes("/manifests/")) {
    const auth = await authenticateBasic(c.req.header("authorization"));
    if (!auth.ok) {
      return c.json(ociError("UNAUTHORIZED", "authentication required"), 401, UNAUTHORIZED_HEADERS);
    }

    const parsed = parseV2Path(path, "manifests");
    if (!parsed) return c.json(ociError("NAME_INVALID", "invalid name"), 400);
    const name = parsed.name;
    const ref = parsed.rest;

    // ref may be a tag name or a digest (sha256:...)
    try {
      let resolvedPath: string | null = null;
      let resolvedDigest: string | null = null;

      if (isValidDigest(ref)) {
        // Direct digest reference — look up the blob path
        const bp = blobPath(ref);
        const file = Bun.file(bp);
        if (!(await file.exists())) {
          return c.json(ociError("MANIFEST_UNKNOWN", "manifest unknown"), 404);
        }
        resolvedPath = manifestPath(name, ref);
        // Fall back: the manifest might be stored by digest directly
        const mf = Bun.file(resolvedPath);
        if (!(await mf.exists())) {
          resolvedPath = bp; // manifests stored as blobs too
        }
        resolvedDigest = ref;
      } else {
        // Tag name — look up in DB
        const [repo] = await db
          .select({ id: ociRepositories.id })
          .from(ociRepositories)
          .where(and(eq(ociRepositories.ownerId, auth.user.id), eq(ociRepositories.name, name)))
          .limit(1);

        if (!repo) {
          return c.json(ociError("NAME_UNKNOWN", "repository name not known to registry"), 404);
        }

        const [tagRow] = await db
          .select({ manifestDigest: ociTags.manifestDigest })
          .from(ociTags)
          .where(and(eq(ociTags.repositoryId, repo.id), eq(ociTags.tag, ref)))
          .limit(1);

        if (!tagRow) {
          return c.json(ociError("MANIFEST_UNKNOWN", "manifest unknown"), 404);
        }

        resolvedDigest = tagRow.manifestDigest;
        resolvedPath = manifestPath(name, ref);
        const mf = Bun.file(resolvedPath);
        if (!(await mf.exists())) {
          // Fall back to digest path
          resolvedPath = blobPath(resolvedDigest);
        }
      }

      if (!resolvedPath || !resolvedDigest) {
        return c.json(ociError("MANIFEST_UNKNOWN", "manifest unknown"), 404);
      }

      const file = Bun.file(resolvedPath);
      if (!(await file.exists())) {
        return c.json(ociError("MANIFEST_UNKNOWN", "manifest unknown"), 404);
      }

      const content = await file.text();
      let contentType = "application/vnd.oci.image.manifest.v1+json";
      try {
        const parsed2 = JSON.parse(content);
        if (parsed2.mediaType) contentType = parsed2.mediaType;
        else if (parsed2.schemaVersion === 2 && parsed2.config) {
          contentType = "application/vnd.docker.distribution.manifest.v2+json";
        }
      } catch { /* leave default */ }

      if (c.req.method === "HEAD") {
        return new Response(null, {
          status: 200,
          headers: {
            "Docker-Content-Digest": resolvedDigest,
            "Content-Length": String(Buffer.byteLength(content)),
            "Content-Type": contentType,
          },
        });
      }

      return new Response(content, {
        status: 200,
        headers: {
          "Docker-Content-Digest": resolvedDigest,
          "Content-Type": contentType,
        },
      });
    } catch (err) {
      console.error("[oci] get manifest:", err);
      return c.json(ociError("UNSUPPORTED", "service error"), 500);
    }
  }

  return c.json(ociError("UNSUPPORTED", "unsupported endpoint"), 404);
});

// ---------------------------------------------------------------------------
// POST /v2/:name/blobs/uploads/ — start a new upload session
// ---------------------------------------------------------------------------
registry.post("/v2/*", async (c) => {
  const path = c.req.path;

  if (!path.includes("/blobs/uploads")) {
    return c.json(ociError("UNSUPPORTED", "unsupported endpoint"), 404);
  }

  const auth = await authenticateBasic(c.req.header("authorization"));
  if (!auth.ok) {
    return c.json(ociError("UNAUTHORIZED", "authentication required"), 401, UNAUTHORIZED_HEADERS);
  }

  // Parse image name
  const parsed = parseV2Path(path, "blobs");
  if (!parsed) return c.json(ociError("NAME_INVALID", "invalid image name"), 400);
  const name = parsed.name;

  // Check for single-request monolithic upload: POST with ?digest=sha256:...
  const digestParam = c.req.query("digest");
  if (digestParam) {
    if (!isValidDigest(digestParam)) {
      return c.json(ociError("DIGEST_INVALID", "invalid digest format"), 400);
    }
    // Single-step upload (used for small layers)
    try {
      const body = await c.req.arrayBuffer();
      const bytes = new Uint8Array(body);
      const h = new Bun.CryptoHasher("sha256");
      h.update(bytes);
      const computed = `sha256:${h.digest("hex")}`;
      if (computed !== digestParam) {
        return c.json(ociError("DIGEST_INVALID", "digest mismatch"), 400);
      }
      await ensureDir(storePath("blobs", "sha256"));
      const bp = blobPath(digestParam);
      const existing = Bun.file(bp);
      if (!(await existing.exists())) {
        await Bun.write(bp, bytes);
      }
      // Ensure OCI repo record exists
      await findOrCreateOciRepo(auth.user.id, name);
      const baseUrl = new URL(c.req.url).origin;
      return new Response(null, {
        status: 201,
        headers: {
          Location: `${baseUrl}/v2/${name}/blobs/${digestParam}`,
          "Docker-Content-Digest": digestParam,
          "Content-Length": "0",
        },
      });
    } catch (err) {
      console.error("[oci] monolithic upload:", err);
      return c.json(ociError("UNSUPPORTED", "upload failed"), 500);
    }
  }

  // Chunked upload — allocate a UUID
  const uuid = randomUUID();
  try {
    await ensureDir(storePath("uploads"));
    // Create an empty placeholder so PATCH has something to append to
    await Bun.write(uploadPath(uuid), new Uint8Array(0));
    await ensureDir(storePath("blobs", "sha256"));
    await findOrCreateOciRepo(auth.user.id, name);
    const baseUrl = new URL(c.req.url).origin;
    return new Response(null, {
      status: 202,
      headers: {
        Location: `${baseUrl}/v2/${name}/blobs/uploads/${uuid}`,
        "Docker-Upload-UUID": uuid,
        Range: "0-0",
        "Content-Length": "0",
      },
    });
  } catch (err) {
    console.error("[oci] start upload:", err);
    return c.json(ociError("UNSUPPORTED", "failed to start upload"), 500);
  }
});

// ---------------------------------------------------------------------------
// PATCH /v2/:name/blobs/uploads/:uuid — stream chunk data
// ---------------------------------------------------------------------------
registry.patch("/v2/*", async (c) => {
  const path = c.req.path;
  const auth = await authenticateBasic(c.req.header("authorization"));
  if (!auth.ok) {
    return c.json(ociError("UNAUTHORIZED", "authentication required"), 401, UNAUTHORIZED_HEADERS);
  }

  // Extract uuid from path: /v2/<name>/blobs/uploads/<uuid>
  const uuidMatch = path.match(/\/blobs\/uploads\/([^/]+)$/);
  if (!uuidMatch) {
    return c.json(ociError("BLOB_UPLOAD_UNKNOWN", "upload not found"), 404);
  }
  const uuid = uuidMatch[1];

  const parsed = parseV2Path(path, "blobs");
  if (!parsed) return c.json(ociError("NAME_INVALID", "invalid name"), 400);
  const name = parsed.name;

  try {
    const uploadFile = Bun.file(uploadPath(uuid));
    if (!(await uploadFile.exists())) {
      return c.json(ociError("BLOB_UPLOAD_UNKNOWN", "upload session not found"), 404);
    }

    const chunk = await c.req.arrayBuffer();
    const chunkBytes = new Uint8Array(chunk);

    // Append the chunk to the upload accumulator using Bun's writer
    const existingBytes = new Uint8Array(await uploadFile.arrayBuffer());
    const combined = new Uint8Array(existingBytes.length + chunkBytes.length);
    combined.set(existingBytes, 0);
    combined.set(chunkBytes, existingBytes.length);
    await Bun.write(uploadPath(uuid), combined);

    const newSize = combined.length;
    const baseUrl = new URL(c.req.url).origin;
    return new Response(null, {
      status: 202,
      headers: {
        Location: `${baseUrl}/v2/${name}/blobs/uploads/${uuid}`,
        "Docker-Upload-UUID": uuid,
        Range: `0-${Math.max(0, newSize - 1)}`,
        "Content-Length": "0",
      },
    });
  } catch (err) {
    console.error("[oci] patch upload:", err);
    return c.json(ociError("BLOB_UPLOAD_INVALID", "chunk write failed"), 500);
  }
});

// ---------------------------------------------------------------------------
// PUT /v2/:name/blobs/uploads/:uuid?digest=sha256:... — complete upload
// PUT /v2/:name/manifests/:ref — push manifest
// ---------------------------------------------------------------------------
registry.put("/v2/*", async (c) => {
  const path = c.req.path;
  const auth = await authenticateBasic(c.req.header("authorization"));
  if (!auth.ok) {
    return c.json(ociError("UNAUTHORIZED", "authentication required"), 401, UNAUTHORIZED_HEADERS);
  }

  // ── PUT manifest ──────────────────────────────────────────────────────────
  if (path.includes("/manifests/")) {
    const parsed = parseV2Path(path, "manifests");
    if (!parsed) return c.json(ociError("NAME_INVALID", "invalid name"), 400);
    const name = parsed.name;
    const ref = parsed.rest;

    try {
      const body = await c.req.text();
      const bytes = Buffer.from(body);

      // Compute the digest of the manifest
      const h = new Bun.CryptoHasher("sha256");
      h.update(bytes);
      const digest = `sha256:${h.digest("hex")}`;

      // Store manifest under both the ref (tag/digest) and the canonical digest path
      await ensureDir(storePath("manifests", name));
      await ensureDir(storePath("blobs", "sha256"));

      const mPath = manifestPath(name, ref);
      await Bun.write(mPath, bytes);

      // Also write under the digest ref so GET by digest works
      if (ref !== digest) {
        const mDigestPath = manifestPath(name, digest);
        await Bun.write(mDigestPath, bytes);
      }

      // Store the raw bytes in the blob store too (some clients fetch by digest)
      const bp = blobPath(digest);
      const existing = Bun.file(bp);
      if (!(await existing.exists())) {
        await Bun.write(bp, bytes);
      }

      // If ref is a tag (not a digest), record in DB
      const ociRepoId = await findOrCreateOciRepo(auth.user.id, name);
      if (!isValidDigest(ref)) {
        await upsertTag(ociRepoId, ref, digest);
      }

      const contentType =
        c.req.header("content-type") ||
        "application/vnd.oci.image.manifest.v1+json";

      return new Response(null, {
        status: 201,
        headers: {
          "Docker-Content-Digest": digest,
          Location: `/v2/${name}/manifests/${ref}`,
          "Content-Type": contentType,
          "Content-Length": "0",
        },
      });
    } catch (err) {
      console.error("[oci] put manifest:", err);
      return c.json(ociError("MANIFEST_INVALID", "failed to store manifest"), 500);
    }
  }

  // ── PUT /v2/:name/blobs/uploads/:uuid — complete chunked upload ───────────
  const uuidMatch = path.match(/\/blobs\/uploads\/([^/?]+)/);
  if (!uuidMatch) {
    return c.json(ociError("UNSUPPORTED", "unsupported endpoint"), 404);
  }
  const uuid = uuidMatch[1];
  const digestParam = c.req.query("digest");

  if (!digestParam || !isValidDigest(digestParam)) {
    return c.json(ociError("DIGEST_INVALID", "missing or invalid digest query param"), 400);
  }

  try {
    const uploadFile = Bun.file(uploadPath(uuid));
    if (!(await uploadFile.exists())) {
      return c.json(ociError("BLOB_UPLOAD_UNKNOWN", "upload session not found"), 404);
    }

    // There may be a final chunk in the PUT body
    const finalChunk = await c.req.arrayBuffer();
    let allBytes: Uint8Array;
    const existingBytes = new Uint8Array(await uploadFile.arrayBuffer());
    if (finalChunk.byteLength > 0) {
      const chunkBytes = new Uint8Array(finalChunk);
      allBytes = new Uint8Array(existingBytes.length + chunkBytes.length);
      allBytes.set(existingBytes, 0);
      allBytes.set(chunkBytes, existingBytes.length);
    } else {
      allBytes = existingBytes;
    }

    // Verify digest
    const h = new Bun.CryptoHasher("sha256");
    h.update(allBytes);
    const computed = `sha256:${h.digest("hex")}`;
    if (computed !== digestParam) {
      return c.json(ociError("DIGEST_INVALID", "digest mismatch"), 400);
    }

    // Move from uploads to blobs
    await ensureDir(storePath("blobs", "sha256"));
    const bp = blobPath(digestParam);
    const existingBlob = Bun.file(bp);
    if (!(await existingBlob.exists())) {
      await Bun.write(bp, allBytes);
    }

    // Clean up upload temp file
    const uploadFilePath = uploadPath(uuid);
    try {
      await Bun.file(uploadFilePath);
      // Overwrite with empty so it's not left hanging; true deletion needs fs.unlink
      const { unlink } = await import("node:fs/promises");
      await unlink(uploadFilePath).catch(() => {});
    } catch { /* ignore */ }

    const parsed = parseV2Path(path, "blobs");
    const name = parsed?.name ?? "unknown";

    return new Response(null, {
      status: 201,
      headers: {
        "Docker-Content-Digest": digestParam,
        Location: `/v2/${name}/blobs/${digestParam}`,
        "Content-Length": "0",
      },
    });
  } catch (err) {
    console.error("[oci] complete upload:", err);
    return c.json(ociError("BLOB_UPLOAD_INVALID", "failed to finalize upload"), 500);
  }
});

// ---------------------------------------------------------------------------
// DELETE /v2/:name/blobs/:digest
// DELETE /v2/:name/manifests/:ref
// ---------------------------------------------------------------------------
registry.delete("/v2/*", async (c) => {
  const path = c.req.path;
  const auth = await authenticateBasic(c.req.header("authorization"));
  if (!auth.ok) {
    return c.json(ociError("UNAUTHORIZED", "authentication required"), 401, UNAUTHORIZED_HEADERS);
  }

  const { unlink } = await import("node:fs/promises");

  // ── DELETE /v2/:name/manifests/:ref ───────────────────────────────────────
  if (path.includes("/manifests/")) {
    const parsed = parseV2Path(path, "manifests");
    if (!parsed) return c.json(ociError("NAME_INVALID", "invalid name"), 400);
    const name = parsed.name;
    const ref = parsed.rest;

    try {
      const mPath = manifestPath(name, ref);
      await unlink(mPath).catch(() => {});

      // Remove tag from DB if ref is a tag
      if (!isValidDigest(ref)) {
        const [repo] = await db
          .select({ id: ociRepositories.id })
          .from(ociRepositories)
          .where(and(eq(ociRepositories.ownerId, auth.user.id), eq(ociRepositories.name, name)))
          .limit(1);
        if (repo) {
          await db
            .delete(ociTags)
            .where(and(eq(ociTags.repositoryId, repo.id), eq(ociTags.tag, ref)));
        }
      }

      return new Response(null, { status: 202 });
    } catch (err) {
      console.error("[oci] delete manifest:", err);
      return c.json(ociError("MANIFEST_UNKNOWN", "manifest not found"), 404);
    }
  }

  // ── DELETE /v2/:name/blobs/:digest ────────────────────────────────────────
  if (path.includes("/blobs/")) {
    const digestMatch = path.match(/\/blobs\/(sha256:[0-9a-f]{64})$/);
    if (!digestMatch) {
      return c.json(ociError("DIGEST_INVALID", "invalid digest"), 400);
    }
    const digest = digestMatch[1];
    const bp = blobPath(digest);
    const file = Bun.file(bp);
    if (!(await file.exists())) {
      return c.json(ociError("BLOB_UNKNOWN", "blob not found"), 404);
    }
    await unlink(bp).catch(() => {});
    return new Response(null, { status: 202 });
  }

  return c.json(ociError("UNSUPPORTED", "unsupported endpoint"), 404);
});

export default registry;