git-tm 0.4.1 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +176 -89
- package/package.json +40 -45
- package/src/cli.js +147 -235
- package/src/git.js +690 -213
- package/src/public/app.css +2628 -0
- package/src/public/index.html +78 -88
- package/src/public/js/compare.js +426 -0
- package/src/public/js/detail.js +269 -0
- package/src/public/js/diff.js +328 -0
- package/src/public/js/graph.js +110 -0
- package/src/public/js/history.js +386 -0
- package/src/public/js/insights.js +150 -0
- package/src/public/js/main.js +409 -0
- package/src/public/js/theme.js +5 -0
- package/src/public/js/ui.js +226 -0
- package/src/public/logo.svg +1 -0
- package/src/server.js +207 -103
- package/src/public/app.js +0 -960
- package/src/public/fonts/InterVariable.woff2 +0 -0
- package/src/public/images/logo.png +0 -0
- package/src/public/images/logo.svg +0 -9
- package/src/public/styles.css +0 -1259
package/src/server.js
CHANGED
|
@@ -1,103 +1,207 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} from
|
|
11
|
-
|
|
12
|
-
const
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
1
|
+
// Local HTTP server: JSON API over the repository + the static UI.
|
|
2
|
+
// Uses only node:http. Bound to loopback by default and checks the Host
|
|
3
|
+
// header so other websites cannot read the repository via DNS rebinding.
|
|
4
|
+
|
|
5
|
+
import http from "node:http";
|
|
6
|
+
import { createHash } from "node:crypto";
|
|
7
|
+
import { readFile } from "node:fs/promises";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
import { GitError } from "./git.js";
|
|
11
|
+
|
|
12
|
+
const PUBLIC_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "public");
|
|
13
|
+
|
|
14
|
+
const MIME = {
|
|
15
|
+
".html": "text/html; charset=utf-8",
|
|
16
|
+
".js": "text/javascript; charset=utf-8",
|
|
17
|
+
".css": "text/css; charset=utf-8",
|
|
18
|
+
".svg": "image/svg+xml",
|
|
19
|
+
".png": "image/png",
|
|
20
|
+
".json": "application/json; charset=utf-8",
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const CSP = [
|
|
24
|
+
"default-src 'self'",
|
|
25
|
+
"script-src 'self'",
|
|
26
|
+
"style-src 'self' 'unsafe-inline'",
|
|
27
|
+
"img-src 'self' data: https://avatars.githubusercontent.com https://www.gravatar.com",
|
|
28
|
+
"connect-src 'self'",
|
|
29
|
+
"base-uri 'none'",
|
|
30
|
+
"form-action 'none'",
|
|
31
|
+
"frame-ancestors 'none'",
|
|
32
|
+
].join("; ");
|
|
33
|
+
|
|
34
|
+
const LOOPBACK = new Set(["localhost", "127.0.0.1", "[::1]", "::1"]);
|
|
35
|
+
|
|
36
|
+
function md5(s) {
|
|
37
|
+
return createHash("md5").update(s).digest("hex");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Avatar URL for an email: GitHub noreply addresses map exactly, otherwise Gravatar. */
|
|
41
|
+
function avatarUrl(email) {
|
|
42
|
+
if (!email) return null;
|
|
43
|
+
const e = email.trim().toLowerCase();
|
|
44
|
+
const gh = e.match(/^(?:(\d+)\+)?([^@]+)@users\.noreply\.github\.com$/);
|
|
45
|
+
if (gh && gh[1]) return `https://avatars.githubusercontent.com/u/${gh[1]}?s=64&v=4`;
|
|
46
|
+
return `https://www.gravatar.com/avatar/${md5(e)}?s=64&d=blank`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function attachAvatars(enabled, ...lists) {
|
|
50
|
+
if (!enabled) return {};
|
|
51
|
+
const map = {};
|
|
52
|
+
for (const list of lists) {
|
|
53
|
+
for (const item of list || []) {
|
|
54
|
+
for (const email of [item.email, item.committerEmail]) {
|
|
55
|
+
if (email && !(email in map)) map[email] = avatarUrl(email);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return map;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function createServer(repo, { avatars = true, host = "127.0.0.1" } = {}) {
|
|
63
|
+
const staticCache = new Map();
|
|
64
|
+
const checkHost = LOOPBACK.has(host);
|
|
65
|
+
|
|
66
|
+
const bool = (v) => v === "1" || v === "true";
|
|
67
|
+
|
|
68
|
+
const routes = {
|
|
69
|
+
"GET /api/repo": async () => ({ ...(await repo.info()), avatars }),
|
|
70
|
+
"GET /api/state": async () => ({ fingerprint: await repo.fingerprint() }),
|
|
71
|
+
"GET /api/history": async (q) => {
|
|
72
|
+
const res = await repo.log({
|
|
73
|
+
ref: q.get("rev") || undefined,
|
|
74
|
+
skip: Number(q.get("skip")) || 0,
|
|
75
|
+
limit: Number(q.get("limit")) || 300,
|
|
76
|
+
message: q.get("message") || undefined,
|
|
77
|
+
author: q.get("author") || undefined,
|
|
78
|
+
code: q.get("code") || undefined,
|
|
79
|
+
path: q.get("path") || undefined,
|
|
80
|
+
follow: bool(q.get("follow")),
|
|
81
|
+
});
|
|
82
|
+
return { ...res, avatars: attachAvatars(avatars, res.commits) };
|
|
83
|
+
},
|
|
84
|
+
"GET /api/commit": async (q) => {
|
|
85
|
+
const commit = await repo.commit(required(q, "rev"));
|
|
86
|
+
return { commit, avatars: attachAvatars(avatars, [commit]) };
|
|
87
|
+
},
|
|
88
|
+
"GET /api/contains": async (q) => repo.containedIn(required(q, "rev")),
|
|
89
|
+
"GET /api/files": async (q) =>
|
|
90
|
+
repo.files(q.get("from") || "", required(q, "to"), { ignoreWhitespace: bool(q.get("ws")) }),
|
|
91
|
+
"GET /api/patch": async (q) =>
|
|
92
|
+
repo.patch(q.get("from") || "", required(q, "to"), required(q, "path"), q.get("oldPath") || undefined, {
|
|
93
|
+
ignoreWhitespace: bool(q.get("ws")),
|
|
94
|
+
context: q.has("context") ? Number(q.get("context")) : 3,
|
|
95
|
+
}),
|
|
96
|
+
"GET /api/range": async (q) => {
|
|
97
|
+
const res = await repo.range(required(q, "from"), required(q, "to"));
|
|
98
|
+
return { ...res, avatars: attachAvatars(avatars, res.added, res.removed, res.path) };
|
|
99
|
+
},
|
|
100
|
+
"GET /api/tree": async (q) => repo.tree(required(q, "rev"), q.get("path") || ""),
|
|
101
|
+
"GET /api/blob": async (q) => repo.blob(required(q, "rev"), required(q, "path")),
|
|
102
|
+
"GET /api/blame": async (q) => {
|
|
103
|
+
const res = await repo.blame(required(q, "rev"), required(q, "path"));
|
|
104
|
+
return { ...res, avatars: attachAvatars(avatars, Object.values(res.commits)) };
|
|
105
|
+
},
|
|
106
|
+
"GET /api/insights": async (q) => {
|
|
107
|
+
const res = await repo.insights(q.get("rev") || undefined);
|
|
108
|
+
return { ...res, avatars: attachAvatars(avatars, res.contributors) };
|
|
109
|
+
},
|
|
110
|
+
"POST /api/fetch": async () => {
|
|
111
|
+
await repo.fetch();
|
|
112
|
+
return { ok: true };
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
async function serveStatic(res, pathname) {
|
|
117
|
+
const rel = pathname === "/" ? "index.html" : decodeURIComponent(pathname).replace(/^\/+/, "");
|
|
118
|
+
const file = path.resolve(PUBLIC_DIR, rel);
|
|
119
|
+
if (!file.startsWith(PUBLIC_DIR + path.sep)) return send(res, 403, "Forbidden");
|
|
120
|
+
let body = staticCache.get(file);
|
|
121
|
+
if (!body) {
|
|
122
|
+
body = await readFile(file).catch(() => null);
|
|
123
|
+
if (!body) return send(res, 404, "Not found");
|
|
124
|
+
staticCache.set(file, body);
|
|
125
|
+
}
|
|
126
|
+
res.writeHead(200, {
|
|
127
|
+
"Content-Type": MIME[path.extname(file)] || "application/octet-stream",
|
|
128
|
+
"Cache-Control": "no-cache",
|
|
129
|
+
...securityHeaders(),
|
|
130
|
+
});
|
|
131
|
+
res.end(body);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return http.createServer(async (req, res) => {
|
|
135
|
+
try {
|
|
136
|
+
if (checkHost) {
|
|
137
|
+
const h = (req.headers.host || "").replace(/:\d+$/, "");
|
|
138
|
+
if (!LOOPBACK.has(h)) return send(res, 421, "Misdirected request");
|
|
139
|
+
}
|
|
140
|
+
const url = new URL(req.url, "http://localhost");
|
|
141
|
+
if (!url.pathname.startsWith("/api/")) {
|
|
142
|
+
if (req.method !== "GET" && req.method !== "HEAD") return send(res, 405, "Method not allowed");
|
|
143
|
+
return await serveStatic(res, url.pathname);
|
|
144
|
+
}
|
|
145
|
+
const route = routes[`${req.method} ${url.pathname}`];
|
|
146
|
+
if (!route) return json(res, 404, { error: "Unknown endpoint" });
|
|
147
|
+
// Simple CSRF guard: browsers will not send this custom header cross-origin without a preflight.
|
|
148
|
+
if (req.method !== "GET" && req.headers["x-git-tm"] !== "1") return json(res, 403, { error: "Forbidden" });
|
|
149
|
+
const data = await route(url.searchParams);
|
|
150
|
+
json(res, 200, data);
|
|
151
|
+
} catch (err) {
|
|
152
|
+
const status = err instanceof GitError ? err.status : 500;
|
|
153
|
+
if (status >= 500) console.error(`${req.method} ${req.url}: ${err instanceof GitError ? err.message : err.stack}`);
|
|
154
|
+
if (res.headersSent) res.destroy();
|
|
155
|
+
else json(res, status, { error: err.message || "Internal error" });
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function required(q, name) {
|
|
161
|
+
const v = q.get(name);
|
|
162
|
+
if (!v) throw new GitError(`Missing parameter: ${name}`, 400);
|
|
163
|
+
return v;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function securityHeaders() {
|
|
167
|
+
return {
|
|
168
|
+
"Content-Security-Policy": CSP,
|
|
169
|
+
"X-Content-Type-Options": "nosniff",
|
|
170
|
+
"Referrer-Policy": "no-referrer",
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function send(res, status, text) {
|
|
175
|
+
res.writeHead(status, { "Content-Type": "text/plain; charset=utf-8", ...securityHeaders() });
|
|
176
|
+
res.end(text);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function json(res, status, data) {
|
|
180
|
+
res.writeHead(status, {
|
|
181
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
182
|
+
"Cache-Control": "no-store",
|
|
183
|
+
...securityHeaders(),
|
|
184
|
+
});
|
|
185
|
+
res.end(JSON.stringify(data));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Listen on the first free port starting at `port`. */
|
|
189
|
+
export function listen(server, port, host, attempts = 20) {
|
|
190
|
+
return new Promise((resolve, reject) => {
|
|
191
|
+
const tryPort = (p, left) => {
|
|
192
|
+
const onError = (err) => {
|
|
193
|
+
server.off("listening", onListening);
|
|
194
|
+
if (err.code === "EADDRINUSE" && left > 0) tryPort(p + 1, left - 1);
|
|
195
|
+
else reject(err);
|
|
196
|
+
};
|
|
197
|
+
const onListening = () => {
|
|
198
|
+
server.off("error", onError);
|
|
199
|
+
resolve(server.address().port);
|
|
200
|
+
};
|
|
201
|
+
server.once("error", onError);
|
|
202
|
+
server.once("listening", onListening);
|
|
203
|
+
server.listen(p, host);
|
|
204
|
+
};
|
|
205
|
+
tryPort(port, attempts);
|
|
206
|
+
});
|
|
207
|
+
}
|