Skip to content
File

Blob: src/worker/git/operations/args.ts

typescript34 lines
1import { decodePktLines } from "@/worker/git/core";
2 
3/**
4 * Parses Git fetch protocol v2 arguments from request body.
5 * Extracts wants, haves, and done flag from pkt-line formatted data.
6 *
7 * @param body - Raw request body in pkt-line format
8 * @returns Object containing wants array, haves array, and done flag
9 */
10export function parseFetchArgs(body: Uint8Array) {
11 const items = decodePktLines(body);
12 const wantSet = new Set<string>();
13 const haves: string[] = [];
14 let done = false;
15 
16 for (const item of items) {
17 if (item.type === "line" && item.text) {
18 const text = item.text.trimEnd();
19 if (text.startsWith("want ")) {
20 const oid = text.slice(5);
21 if (oid.length >= 40) wantSet.add(oid.substring(0, 40));
22 } else if (text.startsWith("have ")) {
23 const oid = text.slice(5);
24 if (oid.length >= 40) haves.push(oid.substring(0, 40));
25 } else if (text === "done") {
26 done = true;
27 }
28 }
29 }
30 
31 const wants = Array.from(wantSet);
32 return { wants, haves, done };
33}