-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
63 lines (54 loc) · 2.28 KB
/
index.ts
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
import path from "node:path";
function getPacketLength(packet: string) {
const hex = "0123456789abcdef";
const length = packet.length + 4;
let prefix = hex[(length >> 12) & 0xf];
prefix += hex[(length >> 8) & 0xf];
prefix += hex[(length >> 4 & 0xf)];
prefix += hex[length & 0xf];
return prefix;
}
const server = Bun.serve({
async fetch(request, server) {
const reqURL = new URL(request.url);
const repoPath = path.join(__dirname, "repo");
switch (reqURL.pathname) {
case "/info/refs":
const service = reqURL.searchParams.get("service");
const firstLine = `# service=${service}\n`;
const infoRefs = await Bun.$`${service} --stateless-rpc --advertise-refs ${repoPath}`.text();
return new Response(`${getPacketLength(firstLine)}${firstLine}0000${infoRefs}`, {
headers: {
"Cache-Control": "no-cache, max-age=0, must-revalidate",
"Content-Type": `application/x-${service}-advertisement`,
Expires: "Fri, 01 Jan 1980 00:00:00 GMT",
Pragma: "no-cache"
}
});
case "/git-receive-pack":
const payload = await Bun.readableStreamToArrayBuffer(request.body as ReadableStream);
const commitPayload = await (await Bun.$`git receive-pack --stateless-rpc ${repoPath} < ${payload}`).stdout;
return new Response(commitPayload, {
headers: {
"Cache-Control": "no-cache, max-age=0, must-revalidate",
"Content-Type": "application/x-git-receive-pack-result",
Expires: "Fri, 01 Jan 1980 00:00:00 GMT",
Pragma: "no-cache"
}
});
case "/git-upload-pack":
const payload2 = await Bun.readableStreamToArrayBuffer(request.body as ReadableStream);
const pullPayload = await (await Bun.$`git upload-pack --stateless-rpc ${repoPath} < ${payload2}`).stdout;
return new Response(pullPayload, {
headers: {
"Cache-Control": "no-cache, max-age=0, must-revalidate",
"Content-Type": "application/x-git-upload-pack-result",
Expires: "Fri, 01 Jan 1980 00:00:00 GMT",
Pragma: "no-cache"
}
});
}
return new Response("", { status: 404 });
}
});
console.log(`listen http://${server.hostname}:${server.port}`);