website

#astro#js#html#css

git clone https://git.pyrossh.dev/website

木 Personal website of pyrossh. Built with astrojs, shiki, vite.


367d057 — pyrossh 2026-07-07T10:59:58+05:30
refactr

Generated files not shown: pnpm-lock.yaml

astro.config.mjs CHANGED
@@ -16,13 +16,34 @@ export default defineConfig({
16
16
  enabled: false,
17
17
  },
18
18
  vite: {
19
+ plugins: [
19
- plugins: [Icons({ compiler: "astro" })],
20
+ Icons({ compiler: "astro" }),
21
+ {
22
+ name: "externalize-spamscanner",
23
+ enforce: "pre",
24
+ resolveId(source) {
25
+ if (
26
+ [
27
+ "spamscanner",
28
+ "stopword",
29
+ "striptags",
30
+ "url-regex-safe",
31
+ "email-regex-safe",
32
+ "mailparser",
33
+ "superagent",
34
+ "re2",
35
+ ].includes(source)
36
+ ) {
37
+ return { id: source, external: true };
38
+ }
39
+ },
40
+ },
41
+ ],
42
+ optimizeDeps: {
43
+ exclude: ["re2", "@mapbox/node-pre-gyp", "superagent", "spamscanner"],
44
+ },
20
45
  ssr: {
21
46
  external: ["svgo"],
22
- // isomorphic-git pulls in a couple of legacy CJS-only packages; force
23
- // Vite to bundle them so their `module.exports` interop is resolved
24
- // at build time instead of relying on runtime globals.
25
- // noExternal: ["isomorphic-git", "pify", "crc-32", "async-lock"],
26
47
  },
27
48
  },
28
49
  });
ec.config.mjs CHANGED
@@ -3,6 +3,7 @@ import { defineEcConfig } from "astro-expressive-code";
3
3
  export default defineEcConfig({
4
4
  // themes: [],
5
5
  shiki: {
6
+ engine: "javascript",
6
7
  langAlias: {
7
8
  h: "c",
8
9
  ino: "c",
package.json CHANGED
@@ -19,10 +19,16 @@
19
19
  "astro-color-scheme": "^1.1.6",
20
20
  "astro-expressive-code": "^0.42.0",
21
21
  "diff": "^9.0.0",
22
- "isomorphic-git": "^1.38.6",
22
+ "gray-matter": "^4.0.3",
23
23
  "marked": "^18.0.5",
24
24
  "pretty-bytes": "^7.1.0",
25
+ "rehype-expressive-code": "^0.44.0",
26
+ "rehype-stringify": "^10.0.1",
27
+ "remark-parse": "^11.0.0",
28
+ "remark-rehype": "^11.1.2",
29
+ "spamscanner": "^6.1.5",
25
- "timeago.js": "^4.0.2"
30
+ "timeago.js": "^4.0.2",
31
+ "unified": "^11.0.5"
26
32
  },
27
33
  "devDependencies": {
28
34
  "@astrojs/check": "^0.9.9",
playwright.config.ts CHANGED
@@ -18,8 +18,9 @@ export default defineConfig({
18
18
  },
19
19
  ],
20
20
  webServer: {
21
- command: "bun run dev",
21
+ command: "pnpm run build && pnpm wrangler dev --port 4321",
22
22
  url: "http://localhost:4321",
23
23
  reuseExistingServer: !process.env.CI,
24
+ timeout: 120000,
24
25
  },
25
26
  });
src/actions/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { env } from "cloudflare:workers";
1
2
  import { defineAction } from "astro:actions";
2
3
  import { z } from "astro/zod";
3
4
  import { addGitBugIssueComment, createGitBugIssue, setGitBugIssueState } from "@/utils/gitBug";
@@ -16,7 +17,7 @@ export const server = {
16
17
  labels: z.string().trim().optional(),
17
18
  }),
18
19
  handler: async (input) => {
19
- return await createGitBugIssue(input.repoId, {
20
+ return await createGitBugIssue(env.REPOS, input.repoId, {
20
21
  title: input.title,
21
22
  body: input.body,
22
23
  author: input.author,
@@ -36,7 +37,7 @@ export const server = {
36
37
  author: z.string().trim().optional(),
37
38
  }),
38
39
  handler: async (input) => {
39
- return await addGitBugIssueComment(input.repoId, input.issueId, {
40
+ return await addGitBugIssueComment(env.REPOS, input.repoId, input.issueId, {
40
41
  body: input.body,
41
42
  author: input.author,
42
43
  });
@@ -51,7 +52,13 @@ export const server = {
51
52
  author: z.string().trim().optional(),
52
53
  }),
53
54
  handler: async (input) => {
54
- return await setGitBugIssueState(input.repoId, input.issueId, input.state, input.author);
55
+ return await setGitBugIssueState(
56
+ env.REPOS,
57
+ input.repoId,
58
+ input.issueId,
59
+ input.state,
60
+ input.author,
61
+ );
55
62
  },
56
63
  }),
57
64
  };
src/components/RecursiveList.astro CHANGED
@@ -10,7 +10,6 @@ interface FileNode {
10
10
  isDirectory: boolean;
11
11
  size: number;
12
12
  ext: string;
13
- absolutePath: string;
14
13
  children?: FileNode[];
15
14
  }
16
15
 
src/layouts/FileLayout.astro CHANGED
@@ -2,7 +2,8 @@
2
2
  import DynamicIcon from "@/components/DynamicIcon.astro";
3
3
  import RepoLayout from "@/layouts/RepoLayout.astro";
4
4
  import { resolveFileIcon, type FileNode } from "@/utils/files";
5
- import type { RepoEntry } from "@/utils/repos";
5
+ import type { RuntimeRepo } from "@/utils/content";
6
+ type RepoEntry = RuntimeRepo;
6
7
 
7
8
  type Props = {
8
9
  repo: RepoEntry;
src/layouts/RepoLayout.astro CHANGED
@@ -1,7 +1,8 @@
1
1
  ---
2
2
  import MdiInformationOutline from "~icons/mdi/information-outline";
3
3
  import Layout from "@/layouts/BaseLayout.astro";
4
- import type { RepoEntry } from "@/utils/repos";
4
+ import type { RuntimeRepo } from "@/utils/content";
5
+ type RepoEntry = RuntimeRepo;
5
6
 
6
7
  type Props = {
7
8
  repo: RepoEntry;
src/pages/repos/[...repoId]/commits/[...hash]/index.astro CHANGED
@@ -1,4 +1,5 @@
1
1
  ---
2
+ import { env } from "cloudflare:workers";
2
3
  import RepoLayout from "@/layouts/RepoLayout.astro";
3
4
  import Commit from "@/components/Commit.astro";
4
5
  import { generateHTMLDiff, getCommits } from "@/utils/commit";
@@ -10,13 +11,15 @@ if (!repo) {
10
11
  return Astro.redirect("/404");
11
12
  }
12
13
 
13
- const commits = await getCommits(repo.data.absolutePath);
14
+ const commits = await getCommits(env.REPOS, repo.id);
14
15
  const commit = commits.find((item) => item.hash === Astro.params.hash);
15
16
  if (!commit) {
16
17
  return Astro.redirect(`/repos/${repo.id}/commits`);
17
18
  }
18
19
 
19
- const diffHtml = await generateHTMLDiff(repo.data.absolutePath, commit.hash);
20
+ const diffHtml = await generateHTMLDiff(env.REPOS, repo.id, commit.hash);
21
+
22
+ Astro.response.headers.set("Cache-Control", "public, max-age=300, s-maxage=3600");
20
23
  ---
21
24
 
22
25
  <RepoLayout repo={repo}>
src/pages/repos/[...repoId]/commits/index.astro CHANGED
@@ -1,4 +1,5 @@
1
1
  ---
2
+ import { env } from "cloudflare:workers";
2
3
  import RepoLayout from "@/layouts/RepoLayout.astro";
3
4
  import Commit from "@/components/Commit.astro";
4
5
  import { getCommits } from "@/utils/commit";
@@ -9,7 +10,9 @@ if (!repo) {
9
10
  return Astro.redirect("/404");
10
11
  }
11
12
 
12
- const commits = await getCommits(repo.data.absolutePath);
13
+ const commits = await getCommits(env.REPOS, repo.id);
14
+
15
+ Astro.response.headers.set("Cache-Control", "public, max-age=300, s-maxage=3600");
13
16
  ---
14
17
 
15
18
  <RepoLayout repo={repo}>
src/pages/repos/[...repoId]/files/[...file]/blame/index.astro CHANGED
@@ -1,4 +1,5 @@
1
1
  ---
2
+ import { env } from "cloudflare:workers";
2
3
  import FileLayout from "@/layouts/FileLayout.astro";
3
4
  import { getFileHistory, getFiles } from "@/utils/commit";
4
5
  import { getRepo } from "@/utils/content";
@@ -8,13 +9,15 @@ if (!repo) {
8
9
  return Astro.redirect("/404");
9
10
  }
10
11
 
11
- const files = await getFiles(repo.data.absolutePath);
12
+ const files = await getFiles(env.REPOS, repo.id);
12
13
  const file = files.find((item) => item.name === Astro.params.file);
13
14
  if (!file) {
14
15
  return Astro.redirect(`/repos/${repo.id}/files`);
15
16
  }
16
17
 
17
- const history = await getFileHistory(repo.data.absolutePath, file.absolutePath);
18
+ const history = await getFileHistory(env.REPOS, repo.id, file.absolutePath);
19
+
20
+ Astro.response.headers.set("Cache-Control", "public, max-age=300, s-maxage=3600");
18
21
  ---
19
22
 
20
23
  <FileLayout repo={repo} file={file}> WIP: Work in Progress... </FileLayout>
src/pages/repos/[...repoId]/files/[...file]/history/index.astro CHANGED
@@ -1,4 +1,5 @@
1
1
  ---
2
+ import { env } from "cloudflare:workers";
2
3
  import FileLayout from "@/layouts/FileLayout.astro";
3
4
  import Commit from "@/components/Commit.astro";
4
5
  import { getFileHistory, getFiles } from "@/utils/commit";
@@ -9,13 +10,15 @@ if (!repo) {
9
10
  return Astro.redirect("/404");
10
11
  }
11
12
 
12
- const files = await getFiles(repo.data.absolutePath);
13
+ const files = await getFiles(env.REPOS, repo.id);
13
14
  const file = files.find((item) => item.name === Astro.params.file);
14
15
  if (!file) {
15
16
  return Astro.redirect(`/repos/${repo.id}/files`);
16
17
  }
17
18
 
18
- const history = await getFileHistory(repo.data.absolutePath, file.absolutePath);
19
+ const history = await getFileHistory(env.REPOS, repo.id, file.absolutePath);
20
+
21
+ Astro.response.headers.set("Cache-Control", "public, max-age=300, s-maxage=3600");
19
22
  ---
20
23
 
21
24
  <FileLayout repo={repo} file={file}>
src/pages/repos/[...repoId]/files/[...file]/index.astro CHANGED
@@ -1,8 +1,8 @@
1
1
  ---
2
- import fs from "fs/promises";
2
+ import { env } from "cloudflare:workers";
3
3
  import { Code } from "astro-expressive-code/components";
4
4
  import FileLayout from "@/layouts/FileLayout.astro";
5
- import { getFiles } from "@/utils/commit";
5
+ import { getFiles, getFileContentData } from "@/utils/commit";
6
6
  import { getRepo } from "@/utils/content";
7
7
 
8
8
  const repo = getRepo(Astro.params.repoId);
@@ -10,13 +10,13 @@ if (!repo) {
10
10
  return Astro.redirect("/404");
11
11
  }
12
12
 
13
- const files = await getFiles(repo.data.absolutePath);
13
+ const files = await getFiles(env.REPOS, repo.id);
14
14
  const file = files.find((item) => item.name === Astro.params.file);
15
15
  if (!file) {
16
16
  return Astro.redirect(`/repos/${repo.id}/files`);
17
17
  }
18
18
 
19
- const contentBuffer = await fs.readFile(file.absolutePath);
19
+ const contentBuffer = await getFileContentData(env.REPOS, repo.id, file.absolutePath);
20
20
  const isBinary = ["apk", "dex", "ap_", "jar", "fnt"].includes(file.ext);
21
21
  const isLarge = file.size > 1024 * 512; // 512KB
22
22
  const isImage = [
@@ -31,6 +31,16 @@ const isImage = [
31
31
  "curve",
32
32
  "atlas",
33
33
  ].includes(file.ext);
34
+
35
+ Astro.response.headers.set("Cache-Control", "public, max-age=300, s-maxage=3600");
36
+
37
+ const toBase64 = (data: Uint8Array) => {
38
+ let binary = "";
39
+ for (let i = 0; i < data.length; i++) {
40
+ binary += String.fromCharCode(data[i]);
41
+ }
42
+ return btoa(binary);
43
+ };
34
44
  ---
35
45
 
36
46
  <FileLayout repo={repo} file={file}>
@@ -38,18 +48,20 @@ const isImage = [
38
48
  {
39
49
  isBinary || isLarge ? (
40
50
  <p>The file is too large to be displayed ({file.size} bytes).</p>
41
- ) : isImage ? (
51
+ ) : isImage && contentBuffer ? (
42
52
  <img
43
- src={`data:image/${file.ext};base64,${contentBuffer.toString("base64")}`}
53
+ src={`data:image/${file.ext};base64,${toBase64(contentBuffer)}`}
44
54
  alt={file.path}
45
55
  />
46
- ) : (
56
+ ) : contentBuffer ? (
47
57
  <Code
48
58
  code={
49
- contentBuffer.toString("utf-8").trim() || "No content to display."
59
+ new TextDecoder("utf-8").decode(contentBuffer).trim() || "No content to display."
50
60
  }
51
61
  lang={file.ext || "txt"}
52
62
  />
63
+ ) : (
64
+ <p>Unable to load file content.</p>
53
65
  )
54
66
  }
55
67
  </div>
src/pages/repos/[...repoId]/files/index.astro CHANGED
@@ -1,4 +1,5 @@
1
1
  ---
2
+ import { env } from "cloudflare:workers";
2
3
  import RepoLayout from "@/layouts/RepoLayout.astro";
3
4
  import RecursiveList from "@/components/RecursiveList.astro";
4
5
  import { getFiles } from "@/utils/commit";
@@ -10,8 +11,10 @@ if (!repo) {
10
11
  return Astro.redirect("/404");
11
12
  }
12
13
 
13
- const files = await getFiles(repo.data.absolutePath);
14
+ const files = await getFiles(env.REPOS, repo.id);
14
15
  const sortedFiles = sortChildren(buildFileTree(files));
16
+
17
+ Astro.response.headers.set("Cache-Control", "public, max-age=300, s-maxage=3600");
15
18
  ---
16
19
 
17
20
  <RepoLayout repo={repo}>
src/pages/repos/[...repoId]/index.astro CHANGED
@@ -1,13 +1,19 @@
1
1
  ---
2
+ import { env } from "cloudflare:workers";
2
3
  import RepoLayout from "@/layouts/RepoLayout.astro";
4
+ import { getRepo } from "@/utils/content";
3
- import { getRepo, getRepoReadmeHtml } from "@/utils/content";
5
+ import { getRepoReadme } from "@/utils/repoContent";
6
+ import { renderMarkdown } from "@/utils/markdown";
4
7
 
5
8
  const repo = getRepo(Astro.params.repoId);
6
9
  if (!repo) {
7
10
  return Astro.redirect("/404");
8
11
  }
9
12
 
10
- const readmeHtml = await getRepoReadmeHtml(repo);
13
+ const readmeRaw = await getRepoReadme(env.REPOS, repo.id);
14
+ const readmeHtml = readmeRaw ? await renderMarkdown(readmeRaw) : "";
15
+
16
+ Astro.response.headers.set("Cache-Control", "public, max-age=300, s-maxage=3600");
11
17
  ---
12
18
 
13
19
  <RepoLayout repo={repo}>
src/pages/repos/[...repoId]/issues/[...issueId]/index.astro CHANGED
@@ -1,20 +1,18 @@
1
1
  ---
2
+ import { env } from "cloudflare:workers";
2
3
  import { actions } from "astro:actions";
3
4
  import RepoLayout from "@/layouts/RepoLayout.astro";
4
5
  import Issue from "@/components/Issue.astro";
5
6
  import TimeAgo from "@/components/TimeAgo.astro";
6
7
  import { getRepo } from "@/utils/content";
7
- import {
8
- getGitBugIssue,
9
- getGitBugIssues,
10
- } from "@/utils/gitBug";
8
+ import { getGitBugIssue } from "@/utils/gitBug";
11
9
 
12
10
  const repo = getRepo(Astro.params.repoId);
13
11
  if (!repo) {
14
12
  return Astro.redirect("/404");
15
13
  }
16
14
 
17
- const issue = await getGitBugIssue(repo.id, Astro.params.issueId ?? "");
15
+ const issue = await getGitBugIssue(env.REPOS, repo.id, Astro.params.issueId ?? "");
18
16
  if (!issue) {
19
17
  return Astro.redirect(`/repos/${repo.id}/issues`);
20
18
  }
src/pages/repos/[...repoId]/issues/index.astro CHANGED
@@ -1,4 +1,5 @@
1
1
  ---
2
+ import { env } from "cloudflare:workers";
2
3
  import { actions } from "astro:actions";
3
4
  import RepoLayout from "@/layouts/RepoLayout.astro";
4
5
  import Issue from "@/components/Issue.astro";
@@ -10,7 +11,7 @@ if (!repo) {
10
11
  return Astro.redirect("/404");
11
12
  }
12
13
 
13
- const { issues, isAvailable } = await getGitBugIssues(repo.id);
14
+ const { issues, isAvailable } = await getGitBugIssues(env.REPOS, repo.id);
14
15
  const openIssues = issues.filter((issue) => issue.state === "open");
15
16
  const closedIssues = issues.filter((issue) => issue.state === "closed");
16
17
  ---
src/utils/commit.ts CHANGED
@@ -1,98 +1,12 @@
1
+ import {
2
+ type Commit,
3
+ generateHTMLDiff,
4
+ getCommits,
1
- import { html, parse } from "diff2html";
5
+ getFileContentData,
6
+ getFileHistory,
7
+ getFiles,
2
- import fs from "fs/promises";
8
+ } from "./gitReader";
3
- import path from "path";
4
- import { simpleGit } from "simple-git";
5
9
  import { type FileNode } from "./files";
6
10
 
7
- export interface Commit {
8
- hash: string;
9
- message: string;
10
- body: string;
11
- author_name: string;
12
- author_email: string;
13
- date: string;
14
- branches: string[];
15
- tags: string[];
16
- }
17
-
18
- export const collectCommits = (item: {
11
+ export type { Commit, FileNode };
19
- hash: any;
20
- message: any;
21
- body: any;
22
- author_name: any;
23
- author_email: any;
24
- date: any;
25
- refs: string;
26
- }) => ({
27
- hash: item.hash,
28
- message: item.message,
29
- body: `${item.message}\n\n${item.body}`,
30
- author_name: item.author_name,
31
- author_email: item.author_email,
32
- date: item.date,
33
- branches: item.refs
34
- .split(",")
35
- .filter((ref: string) => !ref.includes("origin"))
36
- .map((ref) => ref.trim()),
37
- tags: item.refs
38
- .split(",")
39
- .filter((ref: string) => ref.includes("tag: "))
40
- .map((ref) => ref.replace("tag: ", "")),
41
- });
42
-
43
- export const getCommits = async (repoPath: string): Promise<Commit[]> => {
44
- const git = simpleGit(repoPath);
45
- const commits = await git.log(["--branches", "--tags"]);
46
- return commits.all.map(collectCommits);
47
- };
48
-
49
- export const getFiles = async (repoPath: string): Promise<FileNode[]> => {
50
- const git = simpleGit(repoPath);
51
- const paths = await git.raw(["ls-files"]);
52
- const files = [];
53
- for (const p of paths.split("\n").filter((p) => p.length > 0)) {
54
- try {
55
- const stat = await fs.stat(`${repoPath}/${p}`);
56
- const ext = path.extname(p).replace(".", "");
57
- const name = p.endsWith("index.html") ? p.replace("index.html", "index_html") : p;
58
- files.push({
59
- name,
60
- ext,
61
- size: stat.size,
62
- isDirectory: stat.isDirectory(),
63
- absolutePath: `${repoPath}/${p}`,
64
- });
65
- } catch (e) {
66
- // Ignore files not found
67
- console.log(e);
68
- }
69
- }
70
- return files;
71
- };
72
-
73
- export const getFileHistory = async (repoPath: string, filePath: string): Promise<Commit[]> => {
74
- const git = simpleGit(repoPath);
75
- const stat = await fs.stat(`${filePath}`);
76
- const isLarge = stat.size > 1024 * 512;
77
- const history = isLarge ? { all: [] } : await git.log([filePath.replace(`${repoPath}/`, "")]);
78
- return history.all.map(collectCommits);
79
- };
80
-
81
- export const generateHTMLDiff = async (repoPath: string, hash: string): Promise<string> => {
12
+ export { generateHTMLDiff, getCommits, getFileContentData, getFileHistory, getFiles };
82
- const git = simpleGit(repoPath);
83
- let diff = "";
84
- try {
85
- diff = await git.diff([`${hash}^..${hash}`]);
86
- } catch (e) {
87
- diff = await git.diff([hash]);
88
- }
89
- if (diff.length > 1024 * 512) {
90
- return "<p>Diff too large to display.</p>";
91
- }
92
- const diffJson = parse(diff);
93
- const diffHtml = html(diffJson, {
94
- drawFileList: true,
95
- matching: "lines",
96
- });
97
- return diffHtml;
98
- };
src/utils/content.ts CHANGED
@@ -1,13 +1,8 @@
1
1
  import fs from "fs/promises";
2
2
  import path from "path";
3
3
  import matter from "gray-matter";
4
- import rehypeExpressiveCode from "rehype-expressive-code";
5
- import rehypeStringify from "rehype-stringify";
6
- import remarkParse from "remark-parse";
7
- import remarkRehype from "remark-rehype";
8
- import { unified } from "unified";
9
4
  import { REPOS } from "@/consts";
10
- import ecConfig from "../../ec.config.mjs";
5
+ import { renderMarkdown } from "./markdown";
11
6
 
12
7
  export interface RuntimeRepo {
13
8
  id: string;
@@ -15,7 +10,6 @@ export interface RuntimeRepo {
15
10
  title: string;
16
11
  description: string;
17
12
  tags: string[];
18
- absolutePath: string;
19
13
  };
20
14
  }
21
15
 
@@ -34,17 +28,6 @@ export interface RuntimePost {
34
28
 
35
29
  const contentDir = path.resolve("src/content");
36
30
 
37
- const renderMarkdown = async (markdown: string) => {
38
- const file = await unified()
39
- .use(remarkParse)
40
- .use(remarkRehype)
41
- .use(rehypeExpressiveCode, ecConfig)
42
- .use(rehypeStringify)
43
- .process(markdown);
44
-
45
- return String(file);
46
- };
47
-
48
31
  const toDate = (value: unknown) => {
49
32
  if (value instanceof Date) {
50
33
  return value;
@@ -69,23 +52,11 @@ const readMarkdownFiles = async (dir: string): Promise<string[]> => {
69
52
  export const getRepos = (): RuntimeRepo[] =>
70
53
  REPOS.map((repo) => ({
71
54
  id: repo.title,
72
- data: {
73
- ...repo,
55
+ data: { ...repo },
74
- absolutePath: `/Users/pyrossh/Code/pyrossh/${repo.title}`,
75
- },
76
56
  }));
77
57
 
78
58
  export const getRepo = (repoId?: string) => getRepos().find((repo) => repo.id === repoId);
79
59
 
80
- export const getRepoReadmeHtml = async (repo: RuntimeRepo) => {
81
- try {
82
- const readme = await fs.readFile(path.join(repo.data.absolutePath, "README.md"), "utf-8");
83
- return await renderMarkdown(readme);
84
- } catch {
85
- return "";
86
- }
87
- };
88
-
89
60
  export const getPosts = async (): Promise<RuntimePost[]> => {
90
61
  const files = await readMarkdownFiles(contentDir);
91
62
  const posts = await Promise.all(
src/utils/files.ts CHANGED
@@ -1,4 +1,3 @@
1
- import fs from "fs/promises";
2
1
  import { extensions as folderExtensions } from "vscode-icons/src/iconsManifest/supportedFolders.ts";
3
2
  import { extensions as fileExtensions } from "vscode-icons/src/iconsManifest/supportedExtensions.ts";
4
3
 
@@ -68,15 +67,6 @@ export const buildFileTree = (files: any[]): FileNode[] => {
68
67
  return root;
69
68
  };
70
69
 
71
- export const checkFileExists = async (filePath: string) => {
72
- try {
73
- await fs.stat(filePath);
74
- return true; // File exists
75
- } catch (error) {
76
- return false; // File does not exist or cannot be accessed
77
- }
78
- };
79
-
80
70
  export const resolveFolderIcon = (foldername: string) => {
81
71
  for (const item of folderExtensions.supported) {
82
72
  if (item.extensions?.includes(foldername)) {
src/utils/gitBug.ts CHANGED
@@ -1,10 +1,3 @@
1
- import {
2
- GetObjectCommand,
3
- ListObjectsV2Command,
4
- PutObjectCommand,
5
- S3Client,
6
- } from "@aws-sdk/client-s3";
7
-
8
1
  export type GitBugIssueState = "open" | "closed";
9
2
 
10
3
  export interface GitBugIssue {
@@ -52,7 +45,6 @@ const issuesPrefix = (repoId: string) => `${safeRepoId(repoId)}/issues/`;
52
45
  const issueKey = (repoId: string, issueId: string) =>
53
46
  `${issuesPrefix(repoId)}${safeIssueId(issueId)}.json`;
54
47
  let spamScanner: SpamScanner | undefined;
55
- let s3Client: S3Client | undefined;
56
48
 
57
49
  const normalizeLabels = (labels?: string[]) =>
58
50
  labels?.map((label) => label.trim()).filter(Boolean) ?? [];
@@ -61,14 +53,19 @@ const safeHeaderValue = (value: string) => value.replaceAll(/[\r\n]+/g, " ").tri
61
53
 
62
54
  const getSpamScanner = async () => {
63
55
  if (!spamScanner) {
56
+ try {
64
- const { default: SpamScanner } = await import("spamscanner");
57
+ const { default: SpamScanner } = await import("spamscanner");
65
- spamScanner = new SpamScanner({
58
+ spamScanner = new SpamScanner({
66
- enableAuthentication: false,
59
+ enableAuthentication: false,
67
- enableNsfwDetection: true,
60
+ enableNsfwDetection: true,
68
- enableReputation: true,
61
+ enableReputation: true,
69
- enableToxicityDetection: true,
62
+ enableToxicityDetection: true,
70
- timeout: 10_000,
63
+ timeout: 10_000,
71
- });
64
+ });
65
+ } catch {
66
+ console.warn("Spam scanner not available (unsupported in Workers environment)");
67
+ return undefined;
68
+ }
72
69
  }
73
70
 
74
71
  return spamScanner;
@@ -103,6 +100,8 @@ const assertNotSpam = async (input: {
103
100
  labels?: string[];
104
101
  }) => {
105
102
  const scanner = await getSpamScanner();
103
+ if (!scanner) return;
104
+
106
105
  const result = await scanner.scan(makeSpamScanEmail(input));
107
106
 
108
107
  if (result.isSpam) {
@@ -110,54 +109,11 @@ const assertNotSpam = async (input: {
110
109
  }
111
110
  };
112
111
 
113
- const getR2Config = () => {
114
- const bucket =
115
- process.env.REPOS_R2_BUCKET ?? process.env.R2_BUCKET ?? process.env.AWS_BUCKET_NAME;
116
- const endpoint =
117
- process.env.R2_ENDPOINT ??
118
- process.env.AWS_ENDPOINT_URL_S3 ??
119
- (process.env.R2_ACCOUNT_ID
120
- ? `https://${process.env.R2_ACCOUNT_ID}.r2.cloudflarestorage.com`
121
- : undefined);
122
- const accessKeyId = process.env.R2_ACCESS_KEY_ID ?? process.env.AWS_ACCESS_KEY_ID;
123
- const secretAccessKey = process.env.R2_SECRET_ACCESS_KEY ?? process.env.AWS_SECRET_ACCESS_KEY;
124
-
125
- if (!bucket || !endpoint || !accessKeyId || !secretAccessKey) {
126
- throw new Error(
127
- "Missing R2 configuration. Set REPOS_R2_BUCKET, R2_ENDPOINT or R2_ACCOUNT_ID, R2_ACCESS_KEY_ID, and R2_SECRET_ACCESS_KEY.",
128
- );
129
- }
130
-
131
- return { bucket, endpoint, accessKeyId, secretAccessKey };
132
- };
133
-
134
- const getS3Client = () => {
135
- if (!s3Client) {
136
- const { endpoint, accessKeyId, secretAccessKey } = getR2Config();
137
- s3Client = new S3Client({
138
- region: "auto",
139
- endpoint,
140
- credentials: {
141
- accessKeyId,
142
- secretAccessKey,
143
- },
144
- });
145
- }
146
-
147
- return s3Client;
148
- };
149
-
150
- const getObjectJson = async <T>(key: string) => {
112
+ const getObjectJson = async <T>(bucket: R2Bucket, key: string) => {
151
- const { bucket } = getR2Config();
152
-
153
113
  try {
154
- const object = await getS3Client().send(
114
+ const object = await bucket.get(key);
155
- new GetObjectCommand({
115
+ if (object === null) return undefined;
156
- Bucket: bucket,
157
- Key: key,
158
- }),
159
- );
160
- const body = await object.Body?.transformToString();
116
+ const body = await object.text();
161
117
  return body ? (JSON.parse(body) as T) : undefined;
162
118
  } catch (error) {
163
119
  if (error && typeof error === "object" && "name" in error && error.name === "NoSuchKey") {
@@ -168,44 +124,37 @@ const getObjectJson = async <T>(key: string) => {
168
124
  }
169
125
  };
170
126
 
171
- const putIssue = async (repoId: string, issue: GitBugIssue) => {
127
+ const putIssue = async (bucket: R2Bucket, repoId: string, issue: GitBugIssue) => {
172
- const { bucket } = getR2Config();
173
- await getS3Client().send(
174
- new PutObjectCommand({
175
- Bucket: bucket,
176
- Key: issueKey(repoId, issue.id),
177
- Body: JSON.stringify(issue, null, 2),
128
+ await bucket.put(issueKey(repoId, issue.id), JSON.stringify(issue, null, 2), {
178
- ContentType: "application/json; charset=utf-8",
129
+ httpMetadata: { contentType: "application/json; charset=utf-8" },
179
- }),
130
+ });
180
- );
181
131
  return issue;
182
132
  };
183
133
 
134
+ export const getGitBugIssues = async (
135
+ bucket: R2Bucket,
136
+ repoId: string,
184
- export const getGitBugIssues = async (repoId: string): Promise<GitBugIssuesResult> => {
137
+ ): Promise<GitBugIssuesResult> => {
185
138
  try {
186
- const { bucket } = getR2Config();
187
139
  const issues: GitBugIssue[] = [];
188
- let continuationToken: string | undefined;
140
+ let cursor: string | undefined;
189
141
 
190
142
  do {
191
- const page = await getS3Client().send(
143
+ const page = await bucket.list({
192
- new ListObjectsV2Command({
193
- Bucket: bucket,
194
- Prefix: issuesPrefix(repoId),
144
+ prefix: issuesPrefix(repoId),
195
- ContinuationToken: continuationToken,
145
+ cursor,
196
- }),
146
+ });
197
- );
198
147
  const pageIssues = await Promise.all(
199
- (page.Contents ?? [])
148
+ page.objects
200
- .filter((object) => object.Key?.endsWith(".json"))
149
+ .filter((object) => object.key.endsWith(".json"))
201
150
  .map(async (object) => {
202
- return await getObjectJson<GitBugIssue>(object.Key ?? "");
151
+ return await getObjectJson<GitBugIssue>(bucket, object.key);
203
152
  }),
204
153
  );
205
154
 
206
155
  issues.push(...pageIssues.filter((issue): issue is GitBugIssue => Boolean(issue)));
207
- continuationToken = page.IsTruncated ? page.NextContinuationToken : undefined;
156
+ cursor = page.truncated ? page.cursor : undefined;
208
- } while (continuationToken);
157
+ } while (cursor);
209
158
 
210
159
  return {
211
160
  issues: issues.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt)),
@@ -220,13 +169,15 @@ export const getGitBugIssues = async (repoId: string): Promise<GitBugIssuesResul
220
169
  };
221
170
 
222
171
  export const getGitBugIssue = async (
172
+ bucket: R2Bucket,
223
173
  repoId: string,
224
174
  issueId: string,
225
175
  ): Promise<GitBugIssue | undefined> => {
226
- return await getObjectJson<GitBugIssue>(issueKey(repoId, issueId));
176
+ return await getObjectJson<GitBugIssue>(bucket, issueKey(repoId, issueId));
227
177
  };
228
178
 
229
179
  export const createGitBugIssue = async (
180
+ bucket: R2Bucket,
230
181
  repoId: string,
231
182
  input: {
232
183
  title: string;
@@ -256,10 +207,11 @@ export const createGitBugIssue = async (
256
207
  comments: [],
257
208
  };
258
209
 
259
- return await putIssue(repoId, issue);
210
+ return await putIssue(bucket, repoId, issue);
260
211
  };
261
212
 
262
213
  export const addGitBugIssueComment = async (
214
+ bucket: R2Bucket,
263
215
  repoId: string,
264
216
  issueId: string,
265
217
  input: {
@@ -267,7 +219,7 @@ export const addGitBugIssueComment = async (
267
219
  author?: string;
268
220
  },
269
221
  ) => {
270
- const issue = await getGitBugIssue(repoId, issueId);
222
+ const issue = await getGitBugIssue(bucket, repoId, issueId);
271
223
  if (!issue) {
272
224
  throw new Error("Issue not found.");
273
225
  }
@@ -280,7 +232,7 @@ export const addGitBugIssueComment = async (
280
232
  });
281
233
 
282
234
  const now = new Date().toISOString();
283
- return await putIssue(repoId, {
235
+ return await putIssue(bucket, repoId, {
284
236
  ...issue,
285
237
  updatedAt: now,
286
238
  comments: [
@@ -296,17 +248,18 @@ export const addGitBugIssueComment = async (
296
248
  };
297
249
 
298
250
  export const setGitBugIssueState = async (
251
+ bucket: R2Bucket,
299
252
  repoId: string,
300
253
  issueId: string,
301
254
  state: GitBugIssueState,
302
255
  author?: string,
303
256
  ) => {
304
- const issue = await getGitBugIssue(repoId, issueId);
257
+ const issue = await getGitBugIssue(bucket, repoId, issueId);
305
258
  if (!issue) {
306
259
  throw new Error("Issue not found.");
307
260
  }
308
261
 
309
- return await putIssue(repoId, {
262
+ return await putIssue(bucket, repoId, {
310
263
  ...issue,
311
264
  author: author?.trim() || issue.author,
312
265
  state,
src/utils/gitReader.ts ADDED
@@ -0,0 +1,741 @@
1
+ import { type FileNode } from "./files";
2
+
3
+ export interface Commit {
4
+ hash: string;
5
+ message: string;
6
+ body: string;
7
+ author_name: string;
8
+ author_email: string;
9
+ date: string;
10
+ branches: string[];
11
+ tags: string[];
12
+ }
13
+
14
+ interface TreeEntry {
15
+ mode: string;
16
+ name: string;
17
+ oid: string;
18
+ }
19
+
20
+ interface ParsedIdx {
21
+ fanout: Uint32Array;
22
+ names: Uint8Array;
23
+ offsets: Uint32Array;
24
+ resolvedOffsets: Float64Array;
25
+ packKey: string;
26
+ packSize?: number;
27
+ nextOffsetByIndex: Float64Array;
28
+ offsetToNextOffset: Map<number, number>;
29
+ }
30
+
31
+ const hexToBytes = (hex: string): Uint8Array => {
32
+ const bytes = new Uint8Array(20);
33
+ for (let i = 0; i < 20; i++) {
34
+ bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
35
+ }
36
+ return bytes;
37
+ };
38
+
39
+ const bytesToHex = (bytes: Uint8Array): string => {
40
+ return Array.from(bytes)
41
+ .map((b) => b.toString(16).padStart(2, "0"))
42
+ .join("");
43
+ };
44
+
45
+ const compareBytes = (a: Uint8Array, b: Uint8Array): number => {
46
+ for (let i = 0; i < a.length && i < b.length; i++) {
47
+ if (a[i] !== b[i]) return a[i] - b[i];
48
+ }
49
+ return a.length - b.length;
50
+ };
51
+
52
+ const inflate = async (data: Uint8Array): Promise<Uint8Array> => {
53
+ const ds = new DecompressionStream("deflate");
54
+ const writer = ds.writable.getWriter();
55
+ writer.write(data);
56
+ writer.close();
57
+ const reader = ds.readable.getReader();
58
+ const chunks: Uint8Array[] = [];
59
+ while (true) {
60
+ const { done, value } = await reader.read();
61
+ if (done) break;
62
+ if (value) chunks.push(value);
63
+ }
64
+ const total = chunks.reduce((a, c) => a + c.length, 0);
65
+ const result = new Uint8Array(total);
66
+ let offset = 0;
67
+ for (const chunk of chunks) {
68
+ result.set(chunk, offset);
69
+ offset += chunk.length;
70
+ }
71
+ return result;
72
+ };
73
+
74
+ const readText = async (bucket: R2Bucket, key: string): Promise<string | null> => {
75
+ const obj = await bucket.get(key);
76
+ if (obj === null) return null;
77
+ return await obj.text();
78
+ };
79
+
80
+ const readBytes = async (bucket: R2Bucket, key: string): Promise<Uint8Array | null> => {
81
+ const obj = await bucket.get(key);
82
+ if (obj === null) return null;
83
+ return await obj.bytes();
84
+ };
85
+
86
+ const readRange = async (
87
+ bucket: R2Bucket,
88
+ key: string,
89
+ offset: number,
90
+ length: number,
91
+ ): Promise<Uint8Array | null> => {
92
+ const obj = await bucket.get(key, { range: { offset, length } });
93
+ if (obj === null) return null;
94
+ return await obj.bytes();
95
+ };
96
+
97
+ const resolvePackedRef = async (
98
+ bucket: R2Bucket,
99
+ repoId: string,
100
+ refPath: string,
101
+ ): Promise<string | undefined> => {
102
+ const packed = await readText(bucket, `${repoId}/.git/packed-refs`);
103
+ if (packed === null) return undefined;
104
+ for (const line of packed.split("\n")) {
105
+ if (line.startsWith("#") || line.startsWith("^")) continue;
106
+ const space = line.indexOf(" ");
107
+ if (space === -1) continue;
108
+ if (line.slice(space + 1).trim() === refPath) {
109
+ return line.slice(0, space).trim() || undefined;
110
+ }
111
+ }
112
+ return undefined;
113
+ };
114
+
115
+ const resolveRef = async (bucket: R2Bucket, repoId: string): Promise<string | undefined> => {
116
+ const head = await readText(bucket, `${repoId}/.git/HEAD`);
117
+ if (head === null) return undefined;
118
+ const trimmed = head.trim();
119
+ if (trimmed.startsWith("ref: ")) {
120
+ const refPath = trimmed.slice(5);
121
+ const oid = await readText(bucket, `${repoId}/.git/${refPath}`);
122
+ if (oid) return oid.trim() || undefined;
123
+ return await resolvePackedRef(bucket, repoId, refPath);
124
+ }
125
+ return trimmed || undefined;
126
+ };
127
+
128
+ const parseIdx = (data: Uint8Array, packKey: string): ParsedIdx => {
129
+ const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
130
+ const fanout = new Uint32Array(256);
131
+ for (let i = 0; i < 256; i++) {
132
+ fanout[i] = view.getUint32(8 + i * 4);
133
+ }
134
+ const count = fanout[255];
135
+ const namesOffset = 8 + 256 * 4;
136
+ const names = data.slice(namesOffset, namesOffset + count * 20);
137
+ const crcOffset = namesOffset + count * 20;
138
+ const offsetOffset = crcOffset + count * 4;
139
+ const offsets = new Uint32Array(count);
140
+ const resolvedOffsets = new Float64Array(count);
141
+ let needsLarge = false;
142
+ for (let i = 0; i < count; i++) {
143
+ offsets[i] = view.getUint32(offsetOffset + i * 4);
144
+ if (offsets[i] & 0x80000000) needsLarge = true;
145
+ }
146
+ if (needsLarge) {
147
+ const largeOffsetOffset = offsetOffset + count * 4;
148
+ let largeIdx = 0;
149
+ for (let i = 0; i < count; i++) {
150
+ if (offsets[i] & 0x80000000) {
151
+ const v = view.getBigUint64(largeOffsetOffset + largeIdx * 8);
152
+ resolvedOffsets[i] = Number(v);
153
+ largeIdx++;
154
+ } else {
155
+ resolvedOffsets[i] = offsets[i];
156
+ }
157
+ }
158
+ } else {
159
+ for (let i = 0; i < count; i++) {
160
+ resolvedOffsets[i] = offsets[i];
161
+ }
162
+ }
163
+
164
+ const sortedIndices = new Uint32Array(count);
165
+ for (let i = 0; i < count; i++) sortedIndices[i] = i;
166
+ sortedIndices.sort((a, b) => resolvedOffsets[a] - resolvedOffsets[b]);
167
+
168
+ const nextOffsetByIndex = new Float64Array(count);
169
+ const offsetToNextOffset = new Map<number, number>();
170
+ for (let si = 0; si < count; si++) {
171
+ const idx = sortedIndices[si];
172
+ const offset = resolvedOffsets[idx];
173
+ const nextSi = si + 1;
174
+ if (nextSi < count) {
175
+ const nextOffset = resolvedOffsets[sortedIndices[nextSi]];
176
+ nextOffsetByIndex[idx] = nextOffset;
177
+ offsetToNextOffset.set(offset, nextOffset);
178
+ } else {
179
+ nextOffsetByIndex[idx] = Infinity;
180
+ offsetToNextOffset.set(offset, Infinity);
181
+ }
182
+ }
183
+
184
+ return {
185
+ fanout,
186
+ names,
187
+ offsets,
188
+ resolvedOffsets,
189
+ packKey,
190
+ nextOffsetByIndex,
191
+ offsetToNextOffset,
192
+ };
193
+ };
194
+
195
+ const findOidInIdx = (idx: ParsedIdx, oidHex: string): number | undefined => {
196
+ const oidBytes = hexToBytes(oidHex);
197
+ const firstByte = oidBytes[0];
198
+ const lo = firstByte > 0 ? idx.fanout[firstByte - 1] : 0;
199
+ const hi = idx.fanout[firstByte];
200
+ let left = lo;
201
+ let right = hi;
202
+ while (left < right) {
203
+ const mid = Math.floor((left + right) / 2);
204
+ const midOid = idx.names.slice(mid * 20, mid * 20 + 20);
205
+ const cmp = compareBytes(midOid, oidBytes);
206
+ if (cmp < 0) left = mid + 1;
207
+ else if (cmp > 0) right = mid;
208
+ else return mid;
209
+ }
210
+ return undefined;
211
+ };
212
+
213
+ const getOffsetInIdx = (idx: ParsedIdx, index: number): number => {
214
+ return idx.resolvedOffsets[index];
215
+ };
216
+
217
+ const parseVarint = (data: Uint8Array, offset: number): { value: number; size: number } => {
218
+ let value = 0;
219
+ let shift = 0;
220
+ let i = offset;
221
+ while (i < data.length) {
222
+ const byte = data[i];
223
+ value |= (byte & 0x7f) << shift;
224
+ shift += 7;
225
+ i++;
226
+ if (!(byte & 0x80)) break;
227
+ }
228
+ return { value, size: i - offset };
229
+ };
230
+
231
+ const GIT_OBJECT_TYPES = ["", "commit", "tree", "blob", "tag"];
232
+
233
+ let idxCache: Map<string, ParsedIdx> | undefined;
234
+ let packIdxKeysCache: Map<string, string[]> | undefined;
235
+
236
+ const getPackIdxKeys = async (bucket: R2Bucket, repoId: string): Promise<string[]> => {
237
+ if (!packIdxKeysCache) packIdxKeysCache = new Map();
238
+ let keys = packIdxKeysCache.get(repoId);
239
+ if (keys) return keys;
240
+ const packPrefix = `${repoId}/.git/objects/pack/`;
241
+ const idxResult = await bucket.list({ prefix: packPrefix, delimiter: "/" });
242
+ keys = idxResult.objects.map((o) => o.key).filter((k) => k.endsWith(".idx"));
243
+ packIdxKeysCache.set(repoId, keys);
244
+ return keys;
245
+ };
246
+
247
+ const getParsedIdx = async (bucket: R2Bucket, idxKey: string): Promise<ParsedIdx | undefined> => {
248
+ if (!idxCache) idxCache = new Map();
249
+ let idx = idxCache.get(idxKey);
250
+ if (idx) return idx;
251
+ const data = await readBytes(bucket, idxKey);
252
+ if (data === null) return undefined;
253
+ const packKey = idxKey.replace(/\.idx$/, ".pack");
254
+ idx = parseIdx(data, packKey);
255
+ idxCache.set(idxKey, idx);
256
+ return idx;
257
+ };
258
+
259
+ const readRawObject = async (
260
+ bucket: R2Bucket,
261
+ repoId: string,
262
+ oid: string,
263
+ ): Promise<{ type: string; payload: Uint8Array } | undefined> => {
264
+ const idxKeys = await getPackIdxKeys(bucket, repoId);
265
+
266
+ for (const idxKey of idxKeys) {
267
+ const idx = await getParsedIdx(bucket, idxKey);
268
+ if (!idx) continue;
269
+ const objectIndex = findOidInIdx(idx, oid);
270
+ if (objectIndex === undefined) continue;
271
+
272
+ const offset = getOffsetInIdx(idx, objectIndex);
273
+ return await readPackedObject(
274
+ bucket,
275
+ repoId,
276
+ idx.packKey,
277
+ offset,
278
+ new Set(),
279
+ idx.nextOffsetByIndex[objectIndex],
280
+ idx.offsetToNextOffset,
281
+ );
282
+ }
283
+ return undefined;
284
+ };
285
+
286
+ const readPackedObject = async (
287
+ bucket: R2Bucket,
288
+ repoId: string,
289
+ packKey: string,
290
+ offset: number,
291
+ visited: Set<string>,
292
+ nextOffset?: number,
293
+ offsetToNextOffset?: Map<number, number>,
294
+ ): Promise<{ type: string; payload: Uint8Array } | undefined> => {
295
+ const maxRead =
296
+ nextOffset && nextOffset !== Infinity
297
+ ? Math.min(nextOffset - offset, 5 * 1024 * 1024)
298
+ : 5 * 1024 * 1024;
299
+ const headerBuf = await readRange(bucket, packKey, offset, Math.min(128, maxRead));
300
+ if (headerBuf === null) return undefined;
301
+
302
+ const { value: typeSize, size: hdrSize } = parseVarint(headerBuf, 0);
303
+ const type = (typeSize >> 4) & 0x07;
304
+
305
+ if (type === 6) {
306
+ let i = hdrSize;
307
+ let byte = headerBuf[i++];
308
+ let negOffset = byte & 0x7f;
309
+ while (byte & 0x80) {
310
+ negOffset += 1;
311
+ byte = headerBuf[i++];
312
+ negOffset = (negOffset << 7) + (byte & 0x7f);
313
+ }
314
+ const ofsSize = i - hdrSize;
315
+ const baseOffset = offset - negOffset;
316
+ const objStart = offset + hdrSize + ofsSize;
317
+ const dataLen = nextOffset
318
+ ? Math.min(nextOffset - objStart, maxRead - hdrSize - ofsSize)
319
+ : maxRead;
320
+ const compressed = await readRange(bucket, packKey, objStart, dataLen);
321
+ if (compressed === null) return undefined;
322
+ const inflated = await inflate(compressed);
323
+ const baseNextOffset = offsetToNextOffset?.get(baseOffset);
324
+ const base = await readPackedObject(
325
+ bucket,
326
+ repoId,
327
+ packKey,
328
+ baseOffset,
329
+ visited,
330
+ baseNextOffset,
331
+ offsetToNextOffset,
332
+ );
333
+ if (!base) return undefined;
334
+ return { type: base.type, payload: applyDelta(inflated, base.payload) };
335
+ }
336
+
337
+ if (type === 7) {
338
+ const baseOid = bytesToHex(headerBuf.slice(hdrSize, hdrSize + 20));
339
+ if (visited.has(baseOid)) return undefined;
340
+ visited.add(baseOid);
341
+ const objStart = offset + hdrSize + 20;
342
+ const dataLen = nextOffset ? Math.min(nextOffset - objStart, maxRead - hdrSize - 20) : maxRead;
343
+ const compressed = await readRange(bucket, packKey, objStart, dataLen);
344
+ if (compressed === null) return undefined;
345
+ const inflated = await inflate(compressed);
346
+ const base = await readRawObject(bucket, repoId, baseOid);
347
+ if (!base) return undefined;
348
+ return { type: base.type, payload: applyDelta(inflated, base.payload) };
349
+ }
350
+
351
+ const objStart = offset + hdrSize;
352
+ const dataLen = nextOffset ? nextOffset - objStart : maxRead;
353
+ const compressed = await readRange(bucket, packKey, objStart, dataLen);
354
+ if (compressed === null) return undefined;
355
+ const payload = await inflate(compressed);
356
+ const typeName = GIT_OBJECT_TYPES[type] || "unknown";
357
+ return { type: typeName, payload };
358
+ };
359
+
360
+ const applyDelta = (delta: Uint8Array, base: Uint8Array): Uint8Array => {
361
+ let i = 0;
362
+ const { value: baseSize, size: bs } = parseVarint(delta, i);
363
+ i += bs;
364
+ const { value: resultSize, size: rs } = parseVarint(delta, i);
365
+ i += rs;
366
+
367
+ const result = new Uint8Array(resultSize);
368
+ let outPos = 0;
369
+
370
+ while (i < delta.length) {
371
+ const cmd = delta[i];
372
+ i++;
373
+ if (cmd & 0x80) {
374
+ let copyOffset = 0;
375
+ let copySize = 0;
376
+ if (cmd & 0x01) copyOffset = delta[i++];
377
+ if (cmd & 0x02) copyOffset |= delta[i++] << 8;
378
+ if (cmd & 0x04) copyOffset |= delta[i++] << 16;
379
+ if (cmd & 0x08) copyOffset |= delta[i++] << 24;
380
+ if (cmd & 0x10) copySize = delta[i++];
381
+ if (cmd & 0x20) copySize |= delta[i++] << 8;
382
+ if (cmd & 0x40) copySize |= delta[i++] << 16;
383
+ if (copySize === 0) copySize = 0x10000;
384
+ for (let j = 0; j < copySize; j++) {
385
+ result[outPos + j] = base[copyOffset + j];
386
+ }
387
+ outPos += copySize;
388
+ } else {
389
+ for (let j = 0; j < cmd; j++) {
390
+ result[outPos + j] = delta[i + j];
391
+ }
392
+ i += cmd;
393
+ outPos += cmd;
394
+ }
395
+ }
396
+
397
+ return result;
398
+ };
399
+
400
+ const readGitObject = async (
401
+ bucket: R2Bucket,
402
+ repoId: string,
403
+ oid: string,
404
+ ): Promise<{ type: string; payload: Uint8Array } | undefined> => {
405
+ return await readRawObject(bucket, repoId, oid);
406
+ };
407
+
408
+ const parseCommit = (
409
+ payload: Uint8Array,
410
+ ): {
411
+ tree: string;
412
+ parents: string[];
413
+ message: string;
414
+ author?: { name: string; email: string; when: string };
415
+ } => {
416
+ const text = new TextDecoder().decode(payload);
417
+ const lines = text.split("\n");
418
+ let i = 0;
419
+ let tree = "";
420
+ const parents: string[] = [];
421
+ let author: { name: string; email: string; when: string } | undefined;
422
+
423
+ while (i < lines.length && lines[i] !== "") {
424
+ const line = lines[i];
425
+ if (line.startsWith("tree ")) tree = line.slice(5);
426
+ else if (line.startsWith("parent ")) parents.push(line.slice(7));
427
+ else if (line.startsWith("author ")) {
428
+ const match = line.slice(7).match(/^(.*?) <(.*?)> (\d+)/);
429
+ if (match) author = { name: match[1], email: match[2], when: match[3] };
430
+ }
431
+ i++;
432
+ }
433
+
434
+ const message = lines
435
+ .slice(i + 1)
436
+ .join("\n")
437
+ .trim();
438
+ return { tree, parents, message, author };
439
+ };
440
+
441
+ const parseTree = (payload: Uint8Array): TreeEntry[] => {
442
+ const entries: TreeEntry[] = [];
443
+ let offset = 0;
444
+ while (offset < payload.length) {
445
+ const nullByte = payload.indexOf(0, offset);
446
+ if (nullByte === -1) break;
447
+ const header = new TextDecoder().decode(payload.slice(offset, nullByte));
448
+ const space = header.indexOf(" ");
449
+ if (space === -1) break;
450
+ const mode = header.slice(0, space);
451
+ const name = header.slice(space + 1);
452
+ offset = nullByte + 1;
453
+ const oidBytes = payload.slice(offset, offset + 20);
454
+ if (oidBytes.length < 20) break;
455
+ const oid = bytesToHex(oidBytes);
456
+ entries.push({ mode, name, oid });
457
+ offset += 20;
458
+ }
459
+ return entries;
460
+ };
461
+
462
+ const buildCommit = (oid: string, parsed: ReturnType<typeof parseCommit>): Commit => ({
463
+ hash: oid,
464
+ message: parsed.message.split("\n")[0],
465
+ body: parsed.message,
466
+ author_name: parsed.author?.name ?? "",
467
+ author_email: parsed.author?.email ?? "",
468
+ date: parsed.author?.when ? new Date(parseInt(parsed.author.when) * 1000).toISOString() : "",
469
+ branches: [],
470
+ tags: [],
471
+ });
472
+
473
+ export const getCommits = async (bucket: R2Bucket, repoId: string): Promise<Commit[]> => {
474
+ const cached = await bucket.get(`${repoId}/.git/commits.json`);
475
+ if (cached) {
476
+ return (await cached.json()) as Commit[];
477
+ }
478
+
479
+ const headOid = await resolveRef(bucket, repoId);
480
+ if (!headOid) return [];
481
+
482
+ const commits: Commit[] = [];
483
+ let oid: string | undefined = headOid;
484
+
485
+ while (oid) {
486
+ const obj = await readGitObject(bucket, repoId, oid);
487
+ if (!obj || obj.type !== "commit") break;
488
+ const parsed = parseCommit(obj.payload);
489
+ commits.push(buildCommit(oid, parsed));
490
+ oid = parsed.parents[0];
491
+ }
492
+
493
+ return commits;
494
+ };
495
+
496
+ export const getFiles = async (bucket: R2Bucket, repoId: string): Promise<FileNode[]> => {
497
+ const cached = await bucket.get(`${repoId}/.git/files.json`);
498
+ if (cached) {
499
+ return (await cached.json()) as FileNode[];
500
+ }
501
+
502
+ const prefix = `${repoId}/`;
503
+ const files: FileNode[] = [];
504
+ let cursor: string | undefined;
505
+
506
+ do {
507
+ const result = await bucket.list({ prefix, cursor });
508
+ for (const obj of result.objects) {
509
+ const key = obj.key;
510
+ if (key.startsWith(`${prefix}.git`)) continue;
511
+ if (key.endsWith("/")) continue;
512
+
513
+ const relativePath = key.slice(prefix.length);
514
+ if (!relativePath) continue;
515
+
516
+ const extIndex = relativePath.lastIndexOf(".");
517
+ const ext = extIndex >= 0 ? relativePath.slice(extIndex + 1) : "";
518
+
519
+ files.push({
520
+ name: relativePath,
521
+ path: relativePath,
522
+ ext,
523
+ size: obj.size,
524
+ isDirectory: false,
525
+ absolutePath: relativePath,
526
+ });
527
+ }
528
+ cursor = result.truncated ? result.cursor : undefined;
529
+ } while (cursor);
530
+
531
+ return files;
532
+ };
533
+
534
+ const fileOidInTree = async (
535
+ bucket: R2Bucket,
536
+ repoId: string,
537
+ treeOid: string,
538
+ filePath: string,
539
+ ): Promise<string | undefined> => {
540
+ const parts = filePath.split("/");
541
+ let currentTree = treeOid;
542
+ for (const part of parts) {
543
+ const obj = await readGitObject(bucket, repoId, currentTree);
544
+ if (!obj || obj.type !== "tree") return undefined;
545
+ const entries = parseTree(obj.payload);
546
+ const entry = entries.find((e) => e.name === part);
547
+ if (!entry) return undefined;
548
+ if (part === parts[parts.length - 1]) return entry.oid;
549
+ if (entry.mode !== "40000") return undefined;
550
+ currentTree = entry.oid;
551
+ }
552
+ return undefined;
553
+ };
554
+
555
+ export const getFileHistory = async (
556
+ bucket: R2Bucket,
557
+ repoId: string,
558
+ filePath: string,
559
+ ): Promise<Commit[]> => {
560
+ const headOid = await resolveRef(bucket, repoId);
561
+ if (!headOid) return [];
562
+
563
+ const commits: Commit[] = [];
564
+ let oid: string | undefined = headOid;
565
+
566
+ while (oid) {
567
+ const commitObj = await readGitObject(bucket, repoId, oid);
568
+ if (!commitObj || commitObj.type !== "commit") break;
569
+ const parsed = parseCommit(commitObj.payload);
570
+
571
+ const fileOid = await fileOidInTree(bucket, repoId, parsed.tree, filePath);
572
+ let parentFileOid: string | undefined;
573
+ if (parsed.parents[0]) {
574
+ const parentObj = await readGitObject(bucket, repoId, parsed.parents[0]);
575
+ if (parentObj && parentObj.type === "commit") {
576
+ const parentParsed = parseCommit(parentObj.payload);
577
+ parentFileOid = await fileOidInTree(bucket, repoId, parentParsed.tree, filePath);
578
+ }
579
+ }
580
+
581
+ if (fileOid !== parentFileOid) {
582
+ commits.push(buildCommit(oid, parsed));
583
+ }
584
+
585
+ oid = parsed.parents[0];
586
+ }
587
+
588
+ return commits;
589
+ };
590
+
591
+ export const getFileContentData = async (
592
+ bucket: R2Bucket,
593
+ repoId: string,
594
+ filePath: string,
595
+ ): Promise<Uint8Array | undefined> => {
596
+ const obj = await bucket.get(`${repoId}/${filePath}`);
597
+ if (obj === null) return undefined;
598
+ return await obj.bytes();
599
+ };
600
+
601
+ interface Change {
602
+ filepath: string;
603
+ changeType: "added" | "deleted" | "modified";
604
+ newOid?: string;
605
+ oldOid?: string;
606
+ }
607
+
608
+ const getTreeEntries = async (
609
+ bucket: R2Bucket,
610
+ repoId: string,
611
+ treeOid: string,
612
+ ): Promise<Map<string, { mode: string; oid: string }>> => {
613
+ const map = new Map<string, { mode: string; oid: string }>();
614
+ const obj = await readGitObject(bucket, repoId, treeOid);
615
+ if (!obj || obj.type !== "tree") return map;
616
+ const entries = parseTree(obj.payload);
617
+ for (const entry of entries) {
618
+ map.set(entry.name, { mode: entry.mode, oid: entry.oid });
619
+ }
620
+ return map;
621
+ };
622
+
623
+ const diffTrees = async (
624
+ bucket: R2Bucket,
625
+ repoId: string,
626
+ oldTreeOid: string | undefined,
627
+ newTreeOid: string,
628
+ prefix = "",
629
+ ): Promise<Change[]> => {
630
+ const changes: Change[] = [];
631
+ const oldEntries = oldTreeOid ? await getTreeEntries(bucket, repoId, oldTreeOid) : new Map();
632
+ const newEntries = await getTreeEntries(bucket, repoId, newTreeOid);
633
+
634
+ const allNames = new Set([...oldEntries.keys(), ...newEntries.keys()]);
635
+
636
+ for (const name of allNames) {
637
+ const path = prefix ? `${prefix}/${name}` : name;
638
+ const oldEntry = oldEntries.get(name);
639
+ const newEntry = newEntries.get(name);
640
+
641
+ if (!newEntry) {
642
+ if (oldEntry && oldEntry.mode !== "40000") {
643
+ changes.push({ filepath: path, changeType: "deleted", oldOid: oldEntry.oid });
644
+ }
645
+ } else if (!oldEntry) {
646
+ if (newEntry.mode !== "40000") {
647
+ changes.push({ filepath: path, changeType: "added", newOid: newEntry.oid });
648
+ } else {
649
+ changes.push(...(await diffTrees(bucket, repoId, undefined, newEntry.oid, path)));
650
+ }
651
+ } else if (oldEntry.mode === "40000" && newEntry.mode === "40000") {
652
+ changes.push(...(await diffTrees(bucket, repoId, oldEntry.oid, newEntry.oid, path)));
653
+ } else if (oldEntry.oid !== newEntry.oid) {
654
+ if (newEntry.mode !== "40000") {
655
+ changes.push({
656
+ filepath: path,
657
+ changeType: "modified",
658
+ newOid: newEntry.oid,
659
+ oldOid: oldEntry.oid,
660
+ });
661
+ }
662
+ }
663
+ }
664
+
665
+ return changes;
666
+ };
667
+
668
+ const readBlobText = async (bucket: R2Bucket, repoId: string, oid: string): Promise<string> => {
669
+ const obj = await readGitObject(bucket, repoId, oid);
670
+ if (!obj || obj.type !== "blob") return "";
671
+ return new TextDecoder().decode(obj.payload);
672
+ };
673
+
674
+ export const generateHTMLDiff = async (
675
+ bucket: R2Bucket,
676
+ repoId: string,
677
+ hash: string,
678
+ ): Promise<string> => {
679
+ const { html, parse } = await import("diff2html");
680
+ const { createTwoFilesPatch } = await import("diff");
681
+
682
+ const commitObj = await readGitObject(bucket, repoId, hash);
683
+ if (!commitObj || commitObj.type !== "commit") return "<p>Commit not found.</p>";
684
+ const parsed = parseCommit(commitObj.payload);
685
+ const parentOid = parsed.parents[0];
686
+
687
+ let parentTreeOid: string | undefined;
688
+ if (parentOid) {
689
+ const parentObj = await readGitObject(bucket, repoId, parentOid);
690
+ if (parentObj && parentObj.type === "commit") {
691
+ parentTreeOid = parseCommit(parentObj.payload).tree;
692
+ }
693
+ }
694
+
695
+ const changes = parentTreeOid
696
+ ? await diffTrees(bucket, repoId, parentTreeOid, parsed.tree)
697
+ : await diffTrees(bucket, repoId, undefined, parsed.tree);
698
+
699
+ if (changes.length === 0) {
700
+ return "<p>No changes in this commit.</p>";
701
+ }
702
+
703
+ const chunks: string[] = [];
704
+ for (const change of changes) {
705
+ if (change.changeType === "added" && change.newOid) {
706
+ const content = await readBlobText(bucket, repoId, change.newOid);
707
+ chunks.push(createTwoFilesPatch(`/dev/null`, `b/${change.filepath}`, "", content));
708
+ } else if (change.changeType === "deleted" && change.oldOid) {
709
+ const content = await readBlobText(bucket, repoId, change.oldOid);
710
+ chunks.push(createTwoFilesPatch(`a/${change.filepath}`, `/dev/null`, content, ""));
711
+ } else if (change.changeType === "modified" && change.newOid && change.oldOid) {
712
+ const [oldContent, newContent] = await Promise.all([
713
+ readBlobText(bucket, repoId, change.oldOid),
714
+ readBlobText(bucket, repoId, change.newOid),
715
+ ]);
716
+ if (oldContent.length > 1024 * 512 || newContent.length > 1024 * 512) {
717
+ chunks.push(`diff --git a/${change.filepath} b/${change.filepath}\n`);
718
+ chunks.push(`--- a/${change.filepath}\n+++ b/${change.filepath}\n`);
719
+ chunks.push(`@@ -1 +1 @@\n-File too large to diff\n+File too large to diff\n`);
720
+ } else {
721
+ chunks.push(
722
+ createTwoFilesPatch(
723
+ `a/${change.filepath}`,
724
+ `b/${change.filepath}`,
725
+ oldContent,
726
+ newContent,
727
+ ),
728
+ );
729
+ }
730
+ }
731
+ }
732
+
733
+ const diffText = chunks.join("\n");
734
+ if (diffText.length > 1024 * 512) {
735
+ return "<p>Diff too large to display.</p>";
736
+ }
737
+
738
+ const diffJson = parse(diffText);
739
+ const diffHtml = html(diffJson, { drawFileList: true, matching: "lines" });
740
+ return diffHtml;
741
+ };
src/utils/markdown.ts ADDED
@@ -0,0 +1,17 @@
1
+ import rehypeExpressiveCode from "rehype-expressive-code";
2
+ import rehypeStringify from "rehype-stringify";
3
+ import remarkParse from "remark-parse";
4
+ import remarkRehype from "remark-rehype";
5
+ import { unified } from "unified";
6
+ import ecConfig from "../../ec.config.mjs";
7
+
8
+ export const renderMarkdown = async (markdown: string) => {
9
+ const file = await unified()
10
+ .use(remarkParse)
11
+ .use(remarkRehype)
12
+ .use(rehypeExpressiveCode, ecConfig)
13
+ .use(rehypeStringify)
14
+ .process(markdown);
15
+
16
+ return String(file);
17
+ };
src/utils/repoContent.ts ADDED
@@ -0,0 +1,8 @@
1
+ export const getRepoReadme = async (
2
+ bucket: R2Bucket,
3
+ repoId: string,
4
+ ): Promise<string | undefined> => {
5
+ const obj = await bucket.get(`${repoId}/README.md`);
6
+ if (obj === null) return undefined;
7
+ return await obj.text();
8
+ };
tests/repos.spec.ts ADDED
@@ -0,0 +1,59 @@
1
+ import { expect, test } from "@playwright/test";
2
+
3
+ const TEST_REPO = "rust-embed";
4
+
5
+ test.describe("repo pages", () => {
6
+ test("repo readme page loads", async ({ page }) => {
7
+ const response = await page.goto(`/repos/${TEST_REPO}`);
8
+ expect(response?.status()).toBe(200);
9
+ });
10
+
11
+ test("repo commits page loads and shows commits", async ({ page }) => {
12
+ const response = await page.goto(`/repos/${TEST_REPO}/commits`);
13
+ expect(response?.status()).toBe(200);
14
+ });
15
+
16
+ test("repo files page loads and shows file tree", async ({ page }) => {
17
+ const response = await page.goto(`/repos/${TEST_REPO}/files`);
18
+ expect(response?.status()).toBe(200);
19
+ });
20
+
21
+ test("commit detail page loads from commits page navigation", async ({ page }) => {
22
+ const response = await page.goto(`/repos/${TEST_REPO}/commits`);
23
+ expect(response?.status()).toBe(200);
24
+
25
+ const firstCommit = page.locator(".commit a").first();
26
+ if ((await firstCommit.count()) > 0) {
27
+ const href = await firstCommit.getAttribute("href");
28
+ expect(href).toBeTruthy();
29
+ const commitResponse = await page.goto(href!);
30
+ expect(commitResponse?.status()).toBe(200);
31
+ }
32
+ });
33
+
34
+ test("file page loads from files page navigation", async ({ page }) => {
35
+ const response = await page.goto(`/repos/${TEST_REPO}/files`);
36
+ expect(response?.status()).toBe(200);
37
+
38
+ const firstFile = page.locator(".file a").first();
39
+ if ((await firstFile.count()) > 0) {
40
+ const href = await firstFile.getAttribute("href");
41
+ expect(href).toBeTruthy();
42
+ const fileResponse = await page.goto(href!);
43
+ expect(fileResponse?.status()).toBe(200);
44
+ }
45
+ });
46
+
47
+ test("file history page loads", async ({ page }) => {
48
+ const response = await page.goto(`/repos/${TEST_REPO}/files`);
49
+ expect(response?.status()).toBe(200);
50
+
51
+ const firstFile = page.locator(".file a").first();
52
+ if ((await firstFile.count()) > 0) {
53
+ const href = await firstFile.getAttribute("href");
54
+ expect(href).toBeTruthy();
55
+ const historyResponse = await page.goto(`${href}/history`);
56
+ expect(historyResponse?.status()).toBe(200);
57
+ }
58
+ });
59
+ });
worker-configuration.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // Generated by Wrangler by running `wrangler types` (hash: 097be681a01785b514391866da6b5f7e)
3
3
  // Runtime types generated with [email protected] 2026-06-25 nodejs_compat
4
4
  interface __BaseEnv_Env {
5
- REPOS_BUCKET: R2Bucket;
5
+ REPOS: R2Bucket;
6
6
  ASSETS: Fetcher;
7
7
  DATABASE_URL: string;
8
8
  DATABASE_PASSWORD: string;
wrangler.toml CHANGED
@@ -1,5 +1,6 @@
1
1
  name = "pyrossh-website"
2
- main = "dist/server/entry.mjs"
2
+ main = "@astrojs/cloudflare/entrypoints/server"
3
+ account_id = "dfdb8ca4e419990852eb214c5b50bd5e"
3
4
  compatibility_date = "2026-05-15"
4
5
  compatibility_flags = ["nodejs_compat"]
5
6
  workers_dev = false
@@ -31,7 +32,8 @@ REPOS_R2_BUCKET = "pyrossh-repos-prd"
31
32
  [[r2_buckets]]
32
33
  binding = "REPOS"
33
34
  bucket_name = "pyrossh-repos-prd"
34
- preview_bucket_name = "pyrossh-repos-dev"
35
+ preview_bucket_name = "pyrossh-repos-prd"
36
+ remote = true
35
37
 
36
38
  [env.dev]
37
39
  name = "pyrossh-website-dev"
@@ -42,5 +44,5 @@ REPOS_R2_BUCKET = "pyrossh-repos-dev"
42
44
 
43
45
  [[env.dev.r2_buckets]]
44
46
  binding = "REPOS"
45
- bucket_name = "pyrossh-repos-dev"
47
+ bucket_name = "pyrossh-repos-prd"
46
- preview_bucket_name = "pyrossh-repos-dev"
48
+ preview_bucket_name = "pyrossh-repos-prd"