diff options
author | HampusM <hampus@hampusmat.com> | 2021-07-23 14:02:57 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-07-23 14:02:57 +0200 |
commit | 22e44b4ef5f74f7058c01ade82ce14aaf9214f18 (patch) | |
tree | 2f24834b6d45af6f0c94357eafef5523b797d0cd /packages/server/src/git | |
parent | 998d566194cf901a2c927851f9b87c8b577d21a9 (diff) |
Cleaned and improved the backend
Put the initial Fastify stuff into a dedicated file, organized the types & made several improvements in the git http backend
Diffstat (limited to 'packages/server/src/git')
-rw-r--r-- | packages/server/src/git/http.ts | 40 |
1 files changed, 22 insertions, 18 deletions
diff --git a/packages/server/src/git/http.ts b/packages/server/src/git/http.ts index 2d707cb..66d296c 100644 --- a/packages/server/src/git/http.ts +++ b/packages/server/src/git/http.ts @@ -1,18 +1,16 @@ import { FastifyReply, FastifyRequest } from "fastify"; -import { IncomingMessage } from "http"; import { Repository } from "./repository"; -import { Route } from "../fastify_types"; +import { Route } from "../types/fastify"; import { join } from "path"; import { spawn } from "child_process"; import { verifyGitRequest } from "../api/util"; export type RequestInfo = { - repo: string, url_path: string, parsed_url: URL, url_path_parts: string[], is_discovery: boolean, - service: string | null, + service: string, content_type: string } @@ -20,21 +18,21 @@ export interface Request extends FastifyRequest { params: Route["Params"], } -function getRequestInfo(req: Request): RequestInfo { - const repo = req.params.repo + ".git"; - const url_path = req.url.replace(req.params.repo, repo); +function getRequestInfo(req: Request, repository_name: string): RequestInfo { + const url_path = req.url.replace(req.params.repo, repository_name); const parsed_url = new URL(`${req.protocol}://${req.hostname}${url_path}`); const url_path_parts = parsed_url.pathname.split("/"); const is_discovery = (/\/info\/refs$/u).test(parsed_url.pathname); - const service = is_discovery ? parsed_url.searchParams.get("service") : url_path_parts[url_path_parts.length - 1]; + let service = is_discovery ? parsed_url.searchParams.get("service") : url_path_parts[url_path_parts.length - 1]; const content_type = `application/x-${service}-${is_discovery ? "advertisement" : "result"}`; + service = service || ""; + return { - repo, url_path, parsed_url, is_discovery, @@ -45,7 +43,7 @@ function getRequestInfo(req: Request): RequestInfo { } export function connect(repository: Repository, req: Request, reply: FastifyReply): void { - const request_info = getRequestInfo(req); + const request_info = getRequestInfo(req, repository.name.short); const valid_request = verifyGitRequest(request_info); if(valid_request.success === false && valid_request.code) { @@ -56,12 +54,12 @@ export function connect(repository: Repository, req: Request, reply: FastifyRepl reply.raw.writeHead(200, { "Content-Type": request_info.content_type }); - const spawn_args = [ "--stateless-rpc", join(repository.base_dir, request_info.repo) ]; + const spawn_args = [ "--stateless-rpc", join(repository.base_dir, repository.name.full) ]; if(request_info.is_discovery) { spawn_args.push("--advertise-refs"); } - const git_pack = spawn(<string>request_info.service, spawn_args); + const git_pack = spawn(request_info.service, spawn_args); if(request_info.is_discovery) { const s = "# service=" + request_info.service + "\n"; @@ -69,16 +67,22 @@ export function connect(repository: Repository, req: Request, reply: FastifyRepl reply.raw.write(Buffer.from((Array(4 - n.length + 1).join("0") + n + s) + "0000")); } else { - const request_body: IncomingMessage = req.raw; - - request_body.on("data", data => git_pack.stdin.write(data)); - request_body.on("close", () => git_pack.stdin.end()); + const request_body = req.raw; + + request_body.on("data", data => { + git_pack.stdin.write(data); + }); + request_body.on("end", () => { + git_pack.stdin.end(); + }); } git_pack.on("error", err => console.log(err)); git_pack.stderr.on("data", (stderr: Buffer) => console.log(stderr.toString())); - git_pack.stdout.on("data", data => reply.raw.write(data)); + git_pack.stdout.on("data", data => { + reply.raw.write(data); + }); - git_pack.on("close", () => reply.raw.end()); + git_pack.stdout.on("end", () => reply.raw.end()); }
\ No newline at end of file |