Repository navigation
Expand file tree
/
Copy pathapp.ts
More file actions
173 lines (155 loc) · 7 KB
/
Copy pathapp.ts
File metadata and controls
173 lines (155 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// The forge server: healthz, git smart HTTP, REST v3, GraphQL v4, MCP.
// Binds nothing; main.ts listens on the ephemeral port.
import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http';
import { Store } from './store.ts';
import { GitService, writePack, FLUSH, encodePktLine } from './git.ts';
import { RestService } from './rest.ts';
import { GraphQLService } from './graphql.ts';
import { McpService } from './mcp.ts';
const JSON_TYPE = 'application/json; charset=utf-8';
function sendJson(res: ServerResponse, status: number, body: unknown, headers: Record<string, string> = {}): void {
const text = body === undefined ? '' : `${JSON.stringify(body)}\n`;
res.writeHead(status, { 'content-type': JSON_TYPE, 'content-length': Buffer.byteLength(text), ...headers });
res.end(text);
}
function pathOf(req: IncomingMessage): string {
try {
return new URL(req.url ?? '/', 'http://entry').pathname;
} catch {
return '/';
}
}
function readBody(req: IncomingMessage): Promise<Buffer> {
return new Promise((resolve, reject) => {
const chunks: Buffer[] = [];
req.on('data', (chunk: Buffer) => chunks.push(chunk));
req.on('end', () => resolve(Buffer.concat(chunks)));
req.on('error', reject);
});
}
export function createApp(): Server {
const store = new Store();
const git = new GitService(store);
const rest = new RestService(store);
const graphql = new GraphQLService(store);
const mcp = new McpService(store);
// Pre-seed the scenario viewer user so GraphQL/MCP resolve a login.
store.createUser(1000, 'adaiversen', 'Ada Iversen', 'adaiversen@example.invalid');
return createServer(async (req: IncomingMessage, res: ServerResponse) => {
const path = pathOf(req);
const method = req.method ?? 'GET';
// Boot probe: 200 {"ok":true}, twice within budget.
if (method === 'GET' && path === '/healthz') {
sendJson(res, 200, { ok: true });
return;
}
// Git smart HTTP: /{owner}/{repo}.git/info/refs?service=...
const mGit = path.match(/^\/([^/]+)\/([^/]+?)\.git\/(info\/refs|git-upload-pack|git-receive-pack)(?:\?.*|$)/);
if (mGit) {
const owner = mGit[1];
const repo = mGit[2];
const tail = mGit[3];
console.error(`[git] ${method} ${path}`);
if (tail === 'info/refs') {
const service = new URL(req.url, 'http://entry').searchParams.get('service') ?? 'git-upload-pack';
if (method !== 'GET') { sendJson(res, 405, { message: 'Method Not Allowed' }); return; }
if (!store.repoExists(owner, repo)) {
res.writeHead(404, { 'content-type': 'text/plain', 'content-length': '0' });
res.end();
return;
}
const gitProto = req.headers['git-protocol'];
const isV2 = typeof gitProto === 'string' && gitProto.includes('version=2');
if (service === 'git-upload-pack' && isV2) {
const adv = git.advertiseV2(owner, repo);
const lines: Buffer[] = [encodePktLine('version 2\n')];
for (const [cap, val] of adv.capabilities) lines.push(encodePktLine(`${cap}${val ? '=' + val : ''}\n`));
lines.push(FLUSH);
const body = Buffer.concat(lines);
console.error(`[git-v2] body=${body.toString('latin1')}`);
res.writeHead(200, { 'content-type': 'application/x-git-upload-pack-advertisement', 'content-length': body.length });
res.end(body);
return;
}
const body = git.advertiseV0(owner, repo, service as 'git-upload-pack' | 'git-receive-pack');
const ct = service === 'git-upload-pack' ? 'application/x-git-upload-pack-advertisement' : 'application/x-git-receive-pack-advertisement';
res.writeHead(200, { 'content-type': ct, 'content-length': body.length });
res.end(body);
return;
}
if (tail === 'git-upload-pack' || tail === 'git-receive-pack') {
if (method !== 'POST') { sendJson(res, 405, { message: 'Method Not Allowed' }); return; }
if (!store.repoExists(owner, repo)) {
res.writeHead(404, { 'content-type': 'text/plain', 'content-length': '0' });
res.end();
return;
}
const body = await readBody(req);
if (tail === 'git-upload-pack') {
// v2 fetch if the first packet is 'command=fetch\n', else v0
const text = body.toString('utf8');
if (text.includes('command=ls-refs')) {
const result = git.handleLsRefs(owner, repo, body);
res.writeHead(200, { 'content-type': 'application/x-git-upload-pack-result', 'content-length': result.body.length });
res.end(result.body);
return;
}
if (text.startsWith('command=fetch\n') || text.includes('command=fetch')) {
const result = git.handleV2Fetch(owner, repo, body);
res.writeHead(200, { 'content-type': 'application/x-git-upload-pack-result', 'content-length': result.body.length });
res.end(result.body);
return;
}
const result = git.handleV0Fetch(owner, repo, body);
res.writeHead(200, { 'content-type': 'application/x-git-upload-pack-result', 'content-length': result.body.length });
res.end(result.body);
return;
}
// receive-pack
const result = git.handleReceivePack(owner, repo, body);
res.writeHead(200, { 'content-type': 'application/x-git-receive-pack-result', 'content-length': result.body.length });
res.end(result.body);
return;
}
}
// GraphQL
if (path === '/api/graphql') {
const body = await readBody(req);
const auth = req.headers.authorization;
const token = auth && auth.startsWith('Bearer ') ? auth.slice(7) : null;
graphql.handle({ method, url: req.url ?? '/', body: body.toString('utf8') }, res, token);
return;
}
// MCP
if (path === '/mcp') {
const body = await readBody(req);
mcp.handle({ method, url: req.url ?? '/', headers: req.headers, body: body.toString('utf8') }, res);
return;
}
// REST v3 - needs body for POST/PUT/PATCH/DELETE-with-body
if (path.startsWith('/api/v3')) {
let bodyStr = '';
if (method !== 'GET' && method !== 'HEAD') {
const b = await readBody(req);
bodyStr = b.toString('utf8');
}
// Attach body to req for RestService handlers that need it.
(req as any)._body = bodyStr;
// A non-empty body that isn't valid JSON is a 400 (malformed-body check).
if (bodyStr.trim() !== '') {
try {
JSON.parse(bodyStr);
} catch {
sendJson(res, 400, { message: 'Malformed JSON' });
return;
}
}
const origin = req.headers.host ? `http://${req.headers.host}` : 'http://entry';
const handled = rest.handle({ method, url: req.url ?? '/', _body: bodyStr, headers: req.headers, origin }, res);
if (handled === false) sendJson(res, 404, { message: 'Not Found' });
return;
}
// Unknown route -> JSON 404
sendJson(res, 404, { message: 'Not Found' });
});
}