simple-deno-import-gitea/app.ts

197 lines
6.5 KiB
TypeScript
Raw Permalink Normal View History

2023-06-12 10:36:33 +09:00
import { Application, Router, isHttpError } from "https://deno.land/x/oak@v12.1.0/mod.ts";
2023-03-24 01:40:08 +09:00
import {
searchRepositoryWithTopic,
getRepositoryTags,
2023-06-12 10:30:55 +09:00
getRepositoryContent,
GiteaOption
2023-03-24 01:40:08 +09:00
} from "./gitea.ts";
import { ContentsResponse } from "./gitea_api.d.ts";
import { Command } from "https://deno.land/x/cliffy@v0.25.7/mod.ts";
2023-06-12 10:30:55 +09:00
import { load } from "https://deno.land/std@0.191.0/dotenv/mod.ts";
const env = await load();
function getGiteaOptions(): GiteaOption | undefined {
2023-06-12 10:30:55 +09:00
const token = env.TOKEN;
if (token === undefined) {
2023-06-12 10:30:55 +09:00
return undefined
}
return { token: token };
2023-06-12 10:30:55 +09:00
}
2023-03-24 01:40:08 +09:00
const app = new Application();
const router = new Router();
2023-06-12 10:30:55 +09:00
const RelativeTopic = env.tag ?? "denolib";
2023-03-24 01:40:08 +09:00
export interface CompletionList {
/** The list (or partial list) of completion items. */
items: string[];
/** If the list is a partial list, and further queries to the endpoint will
* change the items, set `isIncomplete` to `true`. */
isIncomplete?: boolean;
/** If one of the items in the list should be preselected (the default
* suggestion), then set the value of `preselect` to the value of the item. */
preselect?: string;
}
router.get("/.well-known/deno-import-intellisense.json", (ctx) => {
console.log("get /.well-known/deno-import-intellisense.json");
ctx.response.type = "application/json";
ctx.response.body = {
"version": 2,
"registries": [
{
"schema": "/:package([a-z0-9_]*@[a-z0-9_]*)/:version?/:path*",
"variables": [
{
"key": "package",
// "documentation": "/docs/packages/${package}",
2023-03-24 01:40:08 +09:00
"url": "/packages/${package}"
},
{
"key": "version",
"url": "/packages/${package}/versions"
},
{
"key": "path",
// "documentation": "/docs/packages/${package}/${{version}}/paths/${path}",
2023-03-24 01:40:08 +09:00
"url": "/packages/${package}/${{version}}/paths/${path}"
}
]
}
]
};
});
router.get("/packages/:package", async (ctx) => {
const packageName = ctx.params.package;
console.log(`searchRepositoryWithTopic: ${packageName}`);
2023-06-12 10:30:55 +09:00
const options = getGiteaOptions();
const repositories = await searchRepositoryWithTopic(RelativeTopic, options);
2023-03-24 01:40:08 +09:00
const repo_name = repositories.data?.map((repo) => repo.full_name)
.filter(x => x !== undefined)
.map(x => x?.replace("/", "@")) ?? [];
2023-03-24 01:40:08 +09:00
const completionList: CompletionList = {
items: repo_name as string[],
isIncomplete: true, // TODO: check if there are more than max results
preselect: repo_name[0]
};
ctx.response.type = "application/json";
ctx.response.body = completionList;
});
router.get("/packages/:package/versions", async (ctx) => {
const packageName = ctx.params.package;
const [owner, repo] = packageName.split("@");
console.log(`getTags: owner: ${owner}, repo: ${repo}`);
2023-06-12 10:30:55 +09:00
const options = getGiteaOptions();
const tags = await getRepositoryTags(owner, repo, options);
2023-03-24 01:40:08 +09:00
const candidate = ["main", ...tags.map((tag) => tag.name) as string[]]
const completionList: CompletionList = {
items: candidate,
isIncomplete: false,
preselect: candidate[0]
};
ctx.response.type = "application/json";
ctx.response.body = completionList;
});
router.get("/packages/:package/:version/paths/:path*", async (ctx) => {
const packageName = ctx.params.package;
const version = ctx.params.version;
const path = ctx.params.path;
const [owner, repo] = packageName.split("@");
console.log(`getFilesEntry: owner: ${owner}, repo: ${repo}, path: ${path}, version: ${version}`);
2023-06-12 10:30:55 +09:00
const options = getGiteaOptions();
const entries = await getRepositoryContent(owner, repo, path ?? "", version, options) as ContentsResponse[];
2023-03-24 01:40:08 +09:00
const completionList: CompletionList = {
items: entries.map((entry) => entry.name) as string[],
isIncomplete: false,
preselect: entries[0].name
};
ctx.response.type = "application/json";
ctx.response.body = completionList;
});
router.get("/:package([a-z0-9_]*@[a-z0-9_]*)/:version?/:path*", async (ctx) => {
const packageName = ctx.params.package;
const [owner, repo] = packageName.split("@");
const version = ctx.params.version;
const path = ctx.params.path;
console.log(`getFiles: owner: ${owner}, repo: ${repo}, path: ${path}, version: ${version}`);
2023-06-12 10:30:55 +09:00
const options = getGiteaOptions();
const entries = await getRepositoryContent(owner, repo, path ?? "", version, options);
2023-03-24 01:40:08 +09:00
if (entries instanceof Array) {
ctx.response.type = "application/json";
ctx.response.body = entries;
}
else {
if ("errors" in entries) {
2023-03-24 01:40:08 +09:00
ctx.throw(404);
}
// TODO: check if the file is text file or not (e.g. image)
ctx.response.type = "text/plain";
ctx.response.body = atob(entries.content ?? "");
}
});
2023-06-12 10:30:55 +09:00
app.use(async (ctx, next) => {
try {
2023-06-12 10:30:55 +09:00
await next();
}
catch (err) {
if (isHttpError(err)) {
console.log(err);
ctx.response.status = err.status;
const { message, status, stack } = err;
ctx.response.body = { message, status, stack };
}
else {
throw err;
}
}
});
2023-03-24 01:40:08 +09:00
app.use(router.routes());
app.use(router.allowedMethods());
2023-06-12 10:36:33 +09:00
app.use(async (ctx, next) => {
try {
2023-06-12 10:36:33 +09:00
await next();
}
catch (err) {
console.log(err);
if (isHttpError(err)) {
ctx.response.status = err.status;
const { message, status, stack } = err;
ctx.response.body = { message, status, stack };
}
else {
throw err;
2023-06-12 10:36:33 +09:00
}
}
});
2023-03-24 01:40:08 +09:00
//app.use(async (ctx, next) => {
// ctx.throw(404);
// //ctx.response.body = "Not Found";
// //await next();
//});
app.addEventListener("listen", ({ hostname, port, secure }) => {
console.log(`🚀 Listening on: ${secure ? "https://" : "http://"}${hostname ?? "localhost"}:${port}`);
});
if (import.meta.main) {
const cmd = new Command()
.version("0.1.0")
.description("Simple Deno import intellisense proxy server for Gitea")
.option("-p, --port <port:number>", "Port number to listen on", { default: 9999 })
.action(async ({ port }) => {
await app.listen({ port: port });
});
await cmd.parse(Deno.args);
}