Use zoekt-webserver /print route to get file contents

This commit is contained in:
bkellam 2024-08-31 19:36:23 -07:00
parent 0bfbf5a257
commit 9af8696a6d
6 changed files with 28 additions and 40 deletions

View file

@ -14,7 +14,6 @@ export async function GET(request: NextRequest) {
["num", numResults],
["format", "json"],
);
console.log(url);
const res = await fetch(url);
const data = await res.json();

View file

@ -1,13 +1,11 @@
"use server";
import { ErrorCode } from "@/lib/errorCodes";
import { serviceError, missingQueryParam } from "@/lib/serviceError";
import { missingQueryParam } from "@/lib/serviceError";
import { StatusCodes } from "http-status-codes";
import { NextRequest } from "next/server";
import path from "path";
import fs from "fs";
import { GetSourceResponse, pathQueryParamName, repoQueryParamName } from "@/lib/types";
import { GetSourceResponse, pathQueryParamName, repoQueryParamName, ZoektPrintResponse } from "@/lib/types";
import { ZOEKT_WEBSERVER_URL } from "@/lib/environment";
import { createPathWithQueryParams } from "@/lib/utils";
/**
* Returns the content of a source file at the given path.
@ -28,44 +26,23 @@ export async function GET(request: NextRequest) {
return missingQueryParam(repoQueryParamName);
}
// Get the contents of the path
const repoPath = getRepoPath(repo);
if (!repoPath) {
return serviceError({
statusCode: StatusCodes.NOT_FOUND,
errorCode: ErrorCode.REPOSITORY_NOT_FOUND,
message: `Could not find repository '${repo}'.`
});
}
const url = createPathWithQueryParams(
`${ZOEKT_WEBSERVER_URL}/print`,
["f", filepath],
["r", repo],
["format", "json"],
);
const fullPath = path.join(repoPath, filepath);
if (!fs.existsSync(fullPath)) {
return serviceError({
statusCode: StatusCodes.NOT_FOUND,
errorCode: ErrorCode.FILE_NOT_FOUND,
message: `Could not find file '${filepath}' in repository '${repo}'.`
});
}
// @todo : some error handling here would be nice
const content = fs.readFileSync(fullPath, "utf8");
const res = await fetch(url);
const data = await res.json() as ZoektPrintResponse;
return Response.json(
{
content,
content: data.Content,
encoding: data.Encoding,
} satisfies GetSourceResponse,
{
status: StatusCodes.OK
}
);
}
// @todo : we will need to figure out a more sophisticated system for this..
const getRepoPath = (repo: string): string | undefined => {
switch (repo) {
case "monorepo":
return "/Users/brendan/monorepo"
}
return undefined;
}

View file

@ -151,6 +151,7 @@ export const CodePreview = ({
extensions={extensions}
/>
<Scrollbar orientation="vertical" />
<Scrollbar orientation="horizontal" />
</ScrollArea>
</div>
)

View file

@ -105,9 +105,14 @@ export default function Home() {
fetch(url)
.then(response => response.json())
.then((body: GetSourceResponse) => {
if (body.encoding !== "base64") {
throw new Error("Expected base64 encoding");
}
const content = atob(body.content);
setSelectedMatchIndex(0);
setPreviewFile({
content: body.content,
content: content,
filepath: match.FileName,
matches: match.Matches,
});

View file

@ -5,6 +5,7 @@ export const repoQueryParamName = "repo";
export type GetSourceResponse = {
content: string;
encoding: string;
}
export interface ZoektMatch {
@ -39,4 +40,9 @@ export interface ZoektSearchResponse {
result: ZoektResult,
}
export interface ZoektPrintResponse {
Content: string,
Encoding: string,
}
export type KeymapType = "default" | "vim";

2
vendor/zoekt vendored

@ -1 +1 @@
Subproject commit 96970313fe9081d5e176341a85172287402d5f44
Subproject commit b0fd0b52e8c6d01b5f13ba283602f291ac34b95a