File size: 2,121 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { NextResponse } from "next/server";
import { z } from "zod";
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
import { retrieveBlock } from "@omniroute/open-sse/services/compression/engines/ccr/index";
import { queryBlock } from "@omniroute/open-sse/services/compression/engines/ccr/ccrQuery";
import { sanitizeErrorMessage } from "@omniroute/open-sse/utils/error";

export const dynamic = "force-dynamic";

const RetrieveRequestSchema = z.object({
  hash: z.string().min(6).max(64),
  mode: z.enum(["full", "head", "tail", "lines", "grep", "stats"]).optional(),
  n: z.number().int().positive().max(10000).optional(),
  start: z.number().int().positive().optional(),
  end: z.number().int().positive().optional(),
  pattern: z.string().max(512).optional(),
  unique: z.boolean().optional(),
});

export async function POST(req: Request) {
  const authError = await requireManagementAuth(req);
  if (authError) return authError;
  let body: unknown;
  try {
    body = await req.json();
  } catch {
    return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 });
  }
  const parsed = RetrieveRequestSchema.safeParse(body);
  if (!parsed.success) {
    return NextResponse.json(
      { error: "Invalid request", details: parsed.error.issues },
      { status: 400 }
    );
  }
  try {
    const block = retrieveBlock(parsed.data.hash); // string | null; no principalId → management scope
    if (block == null) return NextResponse.json({ found: false });
    if (!parsed.data.mode || parsed.data.mode === "full") {
      return NextResponse.json({ found: true, block });
    }
    const result = queryBlock(block, parsed.data);
    return NextResponse.json(
      "content" in result
        ? { found: true, block: result.content }
        : { found: true, error: result.error }
    );
  } catch (err: unknown) {
    const msg = err instanceof Error ? err.message : String(err);
    console.error("[/api/compression/retrieve]", msg);
    return NextResponse.json(
      { error: "Retrieve failed", details: sanitizeErrorMessage(msg) },
      { status: 500 }
    );
  }
}