aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/api
diff options
context:
space:
mode:
Diffstat (limited to 'packages/server/src/api')
-rw-r--r--packages/server/src/api/util.ts26
-rw-r--r--packages/server/src/api/v1/index.ts15
-rw-r--r--packages/server/src/api/v1/repo/branches.ts4
-rw-r--r--packages/server/src/api/v1/repo/index.ts42
-rw-r--r--packages/server/src/api/v1/repo/log.ts6
5 files changed, 43 insertions, 50 deletions
diff --git a/packages/server/src/api/util.ts b/packages/server/src/api/util.ts
index e1eaa2c..e7e7657 100644
--- a/packages/server/src/api/util.ts
+++ b/packages/server/src/api/util.ts
@@ -26,30 +26,8 @@ export class VerificationResult {
message: string | null = null;
}
-export function verifyRepoName(base_dir: string, repo_name: string): Promise<VerificationResult> {
- return new Promise<VerificationResult>(resolve => {
- console.log(repo_name);
- const is_valid_repo_name = (/^[a-zA-Z0-9.\-_]+$/u).test(repo_name);
- if(!is_valid_repo_name) {
- resolve(new VerificationResult("INVALID", "repository"));
- return;
- }
-
- readdir(base_dir, (err, dir_content) => {
- if(err) {
- resolve(new VerificationResult("NOT_FOUND", "repository"));
- return;
- }
-
- const dir_content_repos = dir_content.filter(repo => repo.endsWith(".git"));
- if(!dir_content_repos.includes(repo_name + ".git")) {
- resolve(new VerificationResult("NOT_FOUND", "repository"));
- return;
- }
-
- resolve(new VerificationResult("SUCCESS"));
- });
- });
+export function verifyRepoName(repo_name: string): boolean {
+ return /^[a-zA-Z0-9.\-_]+$/u.test(repo_name);
}
export async function verifySHA(repository: Repository, sha: string): Promise<VerificationResult> {
diff --git a/packages/server/src/api/v1/index.ts b/packages/server/src/api/v1/index.ts
index c6b9187..169ba3a 100644
--- a/packages/server/src/api/v1/index.ts
+++ b/packages/server/src/api/v1/index.ts
@@ -4,6 +4,7 @@ import { Route } from "../../fastify_types";
import repo from "./repo";
import { verifyRepoName } from "../util";
import { Info as APIInfo, RepositorySummary as APIRepositorySummary, Repository as APIRepository } from "shared_types";
+import { BaseError } from "../../git/error";
function setHandlers(fastify: FastifyInstance): void {
fastify.setErrorHandler((err, req, reply) => {
@@ -32,7 +33,7 @@ function reposEndpoints(fastify: FastifyInstance, opts: FastifyPluginOptions, do
return <APIRepositorySummary>{
name: repository.name.short,
description: repository.description,
- last_updated: (await repository.latestCommit()).date
+ last_updated: (await repository.masterCommit()).date
};
}))
});
@@ -43,13 +44,17 @@ function reposEndpoints(fastify: FastifyInstance, opts: FastifyPluginOptions, do
method: "GET",
url: "/repos/:repo",
handler: async(req, reply) => {
- const repo_verification = await verifyRepoName(opts.config.settings.base_dir, req.params.repo);
- if(repo_verification.success === false && repo_verification.code) {
- reply.code(repo_verification.code).send({ error: repo_verification.message });
+ if(!verifyRepoName(req.params.repo)) {
+ reply.code(400).send({ error: "Bad request" });
return;
}
- const repository = await Repository.open(opts.config.settings.base_dir, req.params.repo);
+ const repository = await Repository.open(opts.config.settings.base_dir, req.params.repo).catch(err => err);
+
+ if(repository instanceof BaseError) {
+ reply.code(repository.code).send({ error: repository.message });
+ return;
+ }
const data: APIRepository = {
name: repository.name.short,
diff --git a/packages/server/src/api/v1/repo/branches.ts b/packages/server/src/api/v1/repo/branches.ts
index b280a4a..c01e858 100644
--- a/packages/server/src/api/v1/repo/branches.ts
+++ b/packages/server/src/api/v1/repo/branches.ts
@@ -8,7 +8,7 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
method: "GET",
url: "/branches",
handler: async(req, reply) => {
- const branches = await (await req.repository).branches();
+ const branches = await req.repository.branches();
reply.send({
data: branches.map(branch => {
@@ -25,7 +25,7 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
method: "GET",
url: "/branches/:branch",
handler: async(req, reply) => {
- const branch = await Branch.lookup(await req.repository, req.params.branch);
+ const branch = await Branch.lookup(req.repository, req.params.branch);
if(!branch) {
reply.code(404).send({ error: "Branch not found!" });
diff --git a/packages/server/src/api/v1/repo/index.ts b/packages/server/src/api/v1/repo/index.ts
index 8fc9ac1..e33484a 100644
--- a/packages/server/src/api/v1/repo/index.ts
+++ b/packages/server/src/api/v1/repo/index.ts
@@ -9,26 +9,31 @@ import branches from "./branches";
import log from "./log";
import { verifyRepoName } from "../../util";
import { Tree as APITree, Tag as APITag, TreeEntry as APITreeEntry } from "shared_types";
+import { BaseError } from "../../../git/error";
+import { Tree } from "../../../git/tree";
declare module "fastify" {
interface FastifyRequest {
- repository: Promise<Repository>,
+ repository: Repository,
}
}
function addHooks(fastify: FastifyInstance, opts: FastifyPluginOptions): void {
- fastify.addHook("preHandler", (req: CoolFastifyRequest, reply, hookDone) => {
- req.repository = Repository.open(opts.config.settings.base_dir, req.params.repo);
+ fastify.addHook("preHandler", async(req: CoolFastifyRequest, reply) => {
+ const repository = await Repository.open(opts.config.settings.base_dir, req.params.repo, req.query.branch).catch((err: BaseError) => err);
- hookDone();
+ if(repository instanceof BaseError) {
+ reply.code(repository.code).send({ error: repository.message });
+ return;
+ }
+
+ req.repository = repository;
});
fastify.addHook("onRequest", async(req: CoolFastifyRequest, reply) => {
- const repo_verification = await verifyRepoName(opts.config.settings.base_dir, req.params.repo);
- if(repo_verification.success === false && repo_verification.code) {
- reply.code(repo_verification.code).send({ error: repo_verification.message });
+ if(!verifyRepoName(req.params.repo)) {
+ reply.code(400).send({ error: "Bad request" });
}
-
});
}
async function treeEntryMap(entry: TreeEntry) {
@@ -63,23 +68,28 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
method: "GET",
url: "/tree",
handler: async(req, reply) => {
- const tree = await (await req.repository).tree();
+ const tree: Tree | BaseError = await req.repository.tree().catch(err => err);
+
+ if(tree instanceof BaseError) {
+ reply.code(tree.code).send({ error: tree.message });
+ return;
+ }
const tree_path = (Object.keys(req.query).length !== 0 && req.query.path) ? req.query.path : null;
let data: APITree;
if(tree_path) {
- const tree_found = await tree.find(tree_path);
+ const tree_path_entry: Tree | Blob | BaseError = await tree.find(tree_path).catch(err => err);
- if(!tree_found) {
- reply.code(404).send({ error: "Tree path not found!" });
+ if(tree_path_entry instanceof BaseError) {
+ reply.code(tree_path_entry.code).send({ error: tree_path_entry.message });
return;
}
- data = tree_found instanceof Blob
- ? { type: "blob", content: await tree_found.content() }
- : { type: "tree", content: await Promise.all(tree_found.entries().map(treeEntryMap)) };
+ data = tree_path_entry instanceof Blob
+ ? { type: "blob", content: await tree_path_entry.content() }
+ : { type: "tree", content: await Promise.all(tree_path_entry.entries().map(treeEntryMap)) };
}
else {
data = { type: "tree", content: await Promise.all(tree.entries().map(treeEntryMap)) };
@@ -93,7 +103,7 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
method: "GET",
url: "/tags",
handler: async(req, reply) => {
- const tags = await (await req.repository).tags();
+ const tags = await req.repository.tags();
reply.send({
data: await Promise.all(tags.map(tagMap))
});
diff --git a/packages/server/src/api/v1/repo/log.ts b/packages/server/src/api/v1/repo/log.ts
index 8157696..fb876b8 100644
--- a/packages/server/src/api/v1/repo/log.ts
+++ b/packages/server/src/api/v1/repo/log.ts
@@ -37,7 +37,7 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
method: "GET",
url: "/log",
handler: async(req, reply) => {
- const commits = (await (await req.repository).commits());
+ const commits = await req.repository.commits();
reply.send({
data: await Promise.all(commits.map(commitMap))
@@ -49,12 +49,12 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
method: "GET",
url: "/log/:commit",
handler: async(req, reply) => {
- const commit_verification = await verifySHA(await req.repository, req.params.commit);
+ const commit_verification = await verifySHA(req.repository, req.params.commit);
if(commit_verification.success === false && commit_verification.code) {
reply.code(commit_verification.code).send({ error: commit_verification.message });
}
- const commit = await Commit.lookup(await req.repository, req.params.commit);
+ const commit = await Commit.lookup(req.repository, req.params.commit);
const stats = await commit.stats();