Brings the branch up to the current base (executor settings, project skills, Chinese run messages, cloud IDE removal, wish attachments) and resolves the ten conflicted files, keeping both sides: - wish-refiner: the tenant's executor settings pick the models first, then the Copilot model setting; the tenant credential still goes before the operator's login - worker providers: executor settings apply, and a run with its own credential still gets an empty home - main.ts: Copilot screenshot uploads and wish attachments each keep their own body limit - contracts, http, web types: default budget and skill auto-activation, Copilot and attachment entity kinds, ./text and ./attachments exports - globals.css: Copilot styles and attachment styles, one after the other - tests: the canceled-release reason is Chinese; the refiner's default models have no [1m] suffix, as this branch names them Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
561 lines
24 KiB
TypeScript
561 lines
24 KiB
TypeScript
import { plainHttpAllowed } from '@wishforge/contracts';
|
||
import { createHmac, randomUUID, timingSafeEqual } from 'node:crypto';
|
||
import { spawn } from 'node:child_process';
|
||
import { mkdir, open, realpath, rename, stat, unlink, writeFile } from 'node:fs/promises';
|
||
import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';
|
||
import path from 'node:path';
|
||
import { checkedCommand, RuntimeError, safeIdentifier, requireBranchName } from '@wishforge/runtime';
|
||
import { codeReader } from './code.js';
|
||
|
||
export interface GitServiceConfig {
|
||
root: string;
|
||
publicBaseUrl: string;
|
||
adminToken: string;
|
||
tokenSecret: string;
|
||
allowLocalHttp?: boolean;
|
||
maxPackBytes?: number;
|
||
}
|
||
export interface GitClaims {
|
||
aud: 'wishforge-git';
|
||
tenantId: string;
|
||
projectId: string;
|
||
permission: 'read' | 'write';
|
||
iat: number;
|
||
exp: number;
|
||
runId?: string;
|
||
/** Pushes may only update refs under this prefix, for example `refs/heads/ide/*`. */
|
||
refPrefix?: string;
|
||
}
|
||
const REF_PREFIX = /^refs\/heads\/[A-Za-z0-9][A-Za-z0-9._-]{0,63}\/$/;
|
||
/** Wiki pages are Markdown files under `wiki/`; nothing else in the repository is readable here. */
|
||
const WIKI_FILE = /^wiki(?:\/[A-Za-z0-9._-]+)+\.md$/;
|
||
function wikiFile(value: unknown) {
|
||
const file = String(value ?? '');
|
||
if (file.length > 256 || !WIKI_FILE.test(file) || file.split('/').includes('..'))
|
||
throw new RuntimeError('INVALID_PATH', 'Use a Markdown page under wiki/, such as wiki/features/wishes.md');
|
||
return file;
|
||
}
|
||
/** `git ls-tree` reports a symbolic link or a directory; neither is a Wiki page we may serve. */
|
||
function regularBlob(entry: string) {
|
||
const [meta = ''] = entry.split('\t');
|
||
const [mode = '', type = ''] = meta.split(' ');
|
||
return mode !== '120000' && type === 'blob';
|
||
}
|
||
const gitEnv = () => ({
|
||
PATH: process.env.PATH,
|
||
LANG: 'C.UTF-8',
|
||
GIT_CONFIG_GLOBAL: '/dev/null',
|
||
GIT_CONFIG_NOSYSTEM: '1',
|
||
GIT_AUTHOR_NAME: 'WishForge',
|
||
GIT_AUTHOR_EMAIL: 'platform@wishforge.invalid',
|
||
GIT_COMMITTER_NAME: 'WishForge',
|
||
GIT_COMMITTER_EMAIL: 'platform@wishforge.invalid',
|
||
});
|
||
|
||
/**
|
||
* Every push runs this hook. A credential scoped to a ref prefix cannot update anything else,
|
||
* so such pushes reach the target branch only through a verified wish.
|
||
*/
|
||
export const PRE_RECEIVE_HOOK = [
|
||
'#!/bin/sh',
|
||
'[ -z "$WISHFORGE_REF_PREFIX" ] && exit 0',
|
||
'while read -r old new ref; do',
|
||
' case "$ref" in',
|
||
' "$WISHFORGE_REF_PREFIX"*) ;;',
|
||
' *)',
|
||
' branch="${WISHFORGE_REF_PREFIX#refs/heads/}"',
|
||
' echo "WishForge:这个凭据只能推送 $branch 开头的分支,已拒绝 $ref。" >&2',
|
||
' echo "WishForge:请推送到 $branch 开头的分支,再提交一个从该分支开始的愿望,由它验证并合入。" >&2',
|
||
' exit 1',
|
||
' ;;',
|
||
' esac',
|
||
'done',
|
||
'',
|
||
].join('\n');
|
||
/** Writes the platform hook; returns the directory pushes use as core.hooksPath. */
|
||
export async function installHooks(root: string) {
|
||
const directory = path.join(root, '.hooks');
|
||
await mkdir(directory, { recursive: true, mode: 0o700 });
|
||
const hook = path.join(directory, 'pre-receive');
|
||
await writeFile(`${hook}.tmp`, PRE_RECEIVE_HOOK, { mode: 0o700 });
|
||
await rename(`${hook}.tmp`, hook);
|
||
return directory;
|
||
}
|
||
/** The branches under a prefix, most recently committed first. */
|
||
export async function branchesIn(repository: string, prefix: string) {
|
||
if (!REF_PREFIX.test(prefix)) throw new RuntimeError('INVALID_REF', 'Use a branch prefix like refs/heads/ide/');
|
||
const output = await checkedCommand({
|
||
executable: 'git',
|
||
args: [
|
||
'-c',
|
||
'core.hooksPath=/dev/null',
|
||
'for-each-ref',
|
||
'--sort=-committerdate',
|
||
'--count=200',
|
||
'--format=%(refname)%09%(objectname)%09%(committerdate:iso-strict)',
|
||
prefix,
|
||
],
|
||
cwd: repository,
|
||
env: gitEnv(),
|
||
});
|
||
return output
|
||
.split('\n')
|
||
.filter(Boolean)
|
||
.map(line => {
|
||
const [ref, commit, updatedAt] = line.split('\t');
|
||
return { name: ref!.slice('refs/heads/'.length), commit: commit!, updatedAt: updatedAt! };
|
||
});
|
||
}
|
||
function sameSecret(a: string, b: string) {
|
||
const x = Buffer.from(a),
|
||
y = Buffer.from(b);
|
||
return x.length === y.length && timingSafeEqual(x, y);
|
||
}
|
||
export function issueGitToken(claims: GitClaims, secret: string): string {
|
||
const payload = Buffer.from(JSON.stringify(claims)).toString('base64url');
|
||
return `${payload}.${createHmac('sha256', secret).update(payload).digest('base64url')}`;
|
||
}
|
||
export function verifyGitToken(
|
||
token: string,
|
||
secret: string,
|
||
scope: { tenantId: string; projectId: string; write: boolean },
|
||
now = Math.floor(Date.now() / 1000),
|
||
): GitClaims {
|
||
const [payload, signature, ...extra] = token.split('.');
|
||
if (
|
||
!payload ||
|
||
!signature ||
|
||
extra.length ||
|
||
!sameSecret(signature, createHmac('sha256', secret).update(payload).digest('base64url'))
|
||
)
|
||
throw new RuntimeError('UNAUTHORIZED', 'Invalid Git token');
|
||
let claims: GitClaims;
|
||
try {
|
||
claims = JSON.parse(Buffer.from(payload, 'base64url').toString()) as GitClaims;
|
||
} catch {
|
||
throw new RuntimeError('UNAUTHORIZED', 'Invalid Git token payload');
|
||
}
|
||
if (
|
||
claims.aud !== 'wishforge-git' ||
|
||
claims.tenantId !== scope.tenantId ||
|
||
claims.projectId !== scope.projectId ||
|
||
(scope.write && claims.permission !== 'write') ||
|
||
!['read', 'write'].includes(claims.permission) ||
|
||
!Number.isInteger(claims.exp) ||
|
||
!Number.isInteger(claims.iat) ||
|
||
(claims.refPrefix !== undefined && (typeof claims.refPrefix !== 'string' || !REF_PREFIX.test(claims.refPrefix))) ||
|
||
claims.exp <= now ||
|
||
claims.iat > now + 30 ||
|
||
claims.exp - claims.iat > 900
|
||
)
|
||
throw new RuntimeError('UNAUTHORIZED', 'Git token expired or does not authorize this repository/operation');
|
||
return claims;
|
||
}
|
||
async function jsonBody(request: IncomingMessage): Promise<Record<string, unknown>> {
|
||
const chunks: Buffer[] = [];
|
||
let bytes = 0;
|
||
for await (const chunk of request) {
|
||
bytes += Buffer.byteLength(chunk);
|
||
if (bytes > 64 * 1024) throw new RuntimeError('PAYLOAD_TOO_LARGE', 'Admin request exceeds limit');
|
||
chunks.push(Buffer.from(chunk));
|
||
}
|
||
return JSON.parse(Buffer.concat(chunks).toString() || '{}') as Record<string, unknown>;
|
||
}
|
||
function json(response: ServerResponse, status: number, body: unknown) {
|
||
response.writeHead(status, { 'content-type': 'application/json', 'cache-control': 'no-store' });
|
||
response.end(JSON.stringify(body));
|
||
}
|
||
|
||
export async function createGitService(config: GitServiceConfig) {
|
||
if (config.adminToken.length < 24 || config.tokenSecret.length < 32)
|
||
throw new RuntimeError('UNCONFIGURED', 'Strong Git admin and token-signing secrets are required');
|
||
const publicBase = new URL(config.publicBaseUrl);
|
||
if (publicBase.username || publicBase.password || publicBase.search || publicBase.hash || publicBase.pathname !== '/')
|
||
throw new RuntimeError('INVALID_ENDPOINT', 'Public Git URL must be an origin without credentials');
|
||
if (publicBase.protocol !== 'https:' && !(config.allowLocalHttp && plainHttpAllowed(process.env, publicBase)))
|
||
throw new RuntimeError(
|
||
'INSECURE_ENDPOINT',
|
||
'Public Git URL must be HTTPS (explicit loopback HTTP is development-only)',
|
||
);
|
||
await mkdir(config.root, { recursive: true, mode: 0o700 });
|
||
const root = await realpath(config.root);
|
||
const hooks = await installHooks(root);
|
||
const git = (cwd: string, args: string[], input?: string, extraEnv?: Record<string, string | undefined>) =>
|
||
checkedCommand({
|
||
executable: 'git',
|
||
args: ['-c', 'core.hooksPath=/dev/null', ...args],
|
||
cwd,
|
||
input,
|
||
env: { ...gitEnv(), ...extraEnv },
|
||
});
|
||
const location = async (tenantId: string, projectId: string) => {
|
||
safeIdentifier(tenantId);
|
||
safeIdentifier(projectId);
|
||
const parent = path.join(root, tenantId);
|
||
await mkdir(parent, { recursive: true, mode: 0o700 });
|
||
if (!(await realpath(parent)).startsWith(`${root}${path.sep}`))
|
||
throw new RuntimeError('REPOSITORY_SCOPE', 'Repository parent escaped the storage root');
|
||
const repository = path.join(parent, `${projectId}.git`);
|
||
const resolved = await realpath(repository).catch(error => {
|
||
if (error.code === 'ENOENT') return repository;
|
||
throw error;
|
||
});
|
||
if (resolved !== repository) throw new RuntimeError('REPOSITORY_SCOPE', 'Repository links are not permitted');
|
||
return repository;
|
||
};
|
||
const server = createServer((request, response) => {
|
||
void (async () => {
|
||
const url = new URL(request.url ?? '/', publicBase);
|
||
if (url.pathname === '/health' && request.method === 'GET') {
|
||
json(response, 200, { status: 'ok', service: 'git-storage' });
|
||
return;
|
||
}
|
||
if (
|
||
[
|
||
'/repos',
|
||
'/tokens',
|
||
'/refs',
|
||
'/wiki',
|
||
'/wiki/write',
|
||
'/code/tree',
|
||
'/code/blob',
|
||
'/code/search',
|
||
'/code/log',
|
||
'/code/show',
|
||
].includes(url.pathname) &&
|
||
request.method === 'POST'
|
||
) {
|
||
const auth = request.headers.authorization ?? '';
|
||
if (!auth.startsWith('Bearer ') || !sameSecret(auth.slice(7), config.adminToken)) {
|
||
json(response, 401, { error: 'UNAUTHORIZED' });
|
||
return;
|
||
}
|
||
const body = await jsonBody(request);
|
||
const tenantId = String(body.tenantId ?? ''),
|
||
projectId = String(body.projectId ?? '');
|
||
const repository = await location(tenantId, projectId);
|
||
if (url.pathname === '/repos') {
|
||
if (body.defaultBranch !== undefined && typeof body.defaultBranch !== 'string')
|
||
throw new RuntimeError('INVALID_REF', 'defaultBranch must be a safe Git branch name');
|
||
const requestedBranch = requireBranchName((body.defaultBranch as string | undefined) ?? 'main');
|
||
const lockPath = `${repository}.provision.lock`;
|
||
const lock = await open(lockPath, 'wx', 0o600).catch(() => {
|
||
throw new RuntimeError('REPOSITORY_BUSY', 'Repository provisioning is already in progress');
|
||
});
|
||
try {
|
||
if (
|
||
!(await stat(repository).then(
|
||
() => true,
|
||
error => {
|
||
if (error.code === 'ENOENT') return false;
|
||
throw error;
|
||
},
|
||
))
|
||
) {
|
||
await git(path.dirname(repository), [
|
||
'init',
|
||
'--bare',
|
||
`--initial-branch=${requestedBranch}`,
|
||
repository,
|
||
]);
|
||
const tree = await git(repository, ['hash-object', '-t', 'tree', '--stdin'], '');
|
||
const commit = await git(repository, ['commit-tree', tree, '-m', 'Initialize WishForge project']);
|
||
await git(repository, ['update-ref', `refs/heads/${requestedBranch}`, commit]);
|
||
await git(repository, ['config', 'http.receivepack', 'true']);
|
||
await git(repository, ['config', 'receive.denyNonFastForwards', 'true']);
|
||
await git(repository, ['config', 'receive.denyDeletes', 'true']);
|
||
}
|
||
const headRef = await git(repository, ['symbolic-ref', '--quiet', 'HEAD']);
|
||
if (!headRef.startsWith('refs/heads/'))
|
||
throw new RuntimeError(
|
||
'REPOSITORY_DEFAULT_BRANCH_UNAVAILABLE',
|
||
'Repository HEAD is not a branch; existing state was not changed',
|
||
);
|
||
const defaultBranch = requireBranchName(headRef.slice('refs/heads/'.length));
|
||
const initialCommit = await git(repository, ['rev-parse', '--verify', `${headRef}^{commit}`]);
|
||
json(response, 200, {
|
||
repositoryUrl: new URL(`/git/${tenantId}/${projectId}.git`, publicBase).href,
|
||
initialCommit,
|
||
defaultBranch,
|
||
});
|
||
} finally {
|
||
await lock.close();
|
||
await unlink(lockPath);
|
||
}
|
||
} else if (url.pathname === '/wiki') {
|
||
await stat(repository);
|
||
const branch = requireBranchName(String(body.branch ?? 'main'));
|
||
const version = (await git(repository, ['rev-parse', '--verify', `${branch}^{commit}`])).trim();
|
||
if (body.path !== undefined) {
|
||
const file = wikiFile(body.path);
|
||
const entry = (await git(repository, ['ls-tree', branch, '--', file])).trim();
|
||
if (!entry) throw new RuntimeError('NOT_FOUND', 'Wiki page not found');
|
||
if (!regularBlob(entry)) throw new RuntimeError('INVALID_PATH', 'Wiki pages must be regular files');
|
||
const content = await git(repository, ['show', `${branch}:${file}`]);
|
||
if (Buffer.byteLength(content) > 512 * 1024)
|
||
throw new RuntimeError('PAYLOAD_TOO_LARGE', 'File is too large');
|
||
json(response, 200, { version, file: { path: file, content } });
|
||
return;
|
||
}
|
||
const listed = await git(repository, ['ls-tree', '-r', branch, '--', 'wiki']);
|
||
const paths = listed
|
||
.split('\n')
|
||
.filter(line => line.endsWith('.md') && regularBlob(line) && WIKI_FILE.test(line.split('\t').at(-1)!))
|
||
.slice(0, 100)
|
||
.map(line => line.split('\t').at(-1)!);
|
||
const files = [];
|
||
for (const file of paths) {
|
||
const content = await git(repository, ['show', `${branch}:${file}`]);
|
||
if (Buffer.byteLength(content) <= 128 * 1024) files.push({ path: file, content });
|
||
}
|
||
json(response, 200, { version, files });
|
||
} else if (url.pathname === '/wiki/write') {
|
||
// The Wiki is written by the platform, never by a customer: a page the Copilot generated
|
||
// is committed on the project's own branch, with the platform as its author.
|
||
await stat(repository);
|
||
const branch = requireBranchName(String(body.branch ?? 'main'));
|
||
const raw = Array.isArray(body.files) ? body.files : [];
|
||
if (!raw.length || raw.length > 10)
|
||
throw new RuntimeError('INVALID_WIKI_BATCH', 'Write 1 to 10 pages at once');
|
||
const pages = raw.map(item => {
|
||
const entry = item as { path?: unknown; content?: unknown };
|
||
const file = wikiFile(entry.path);
|
||
if (!/^wiki\/code\/[A-Za-z0-9._-]+\.md$/.test(file))
|
||
throw new RuntimeError('INVALID_WIKI_PATH', 'Generated pages live under wiki/code/');
|
||
const content = typeof entry.content === 'string' ? entry.content : '';
|
||
if (!content || Buffer.byteLength(content) > 256 * 1024)
|
||
throw new RuntimeError('PAYLOAD_TOO_LARGE', 'A generated page must be 1 byte to 256 KB');
|
||
return { path: file, content };
|
||
});
|
||
const parent = (await git(repository, ['rev-parse', '--verify', `${branch}^{commit}`])).trim();
|
||
const index = `${repository}.index-${randomUUID()}`;
|
||
const env = { ...gitEnv(), GIT_INDEX_FILE: index };
|
||
try {
|
||
await git(repository, ['read-tree', branch], undefined, env);
|
||
for (const page of pages) {
|
||
const blob = (await git(repository, ['hash-object', '-w', '--stdin'], page.content, env)).trim();
|
||
await git(
|
||
repository,
|
||
['update-index', '--add', '--cacheinfo', `100644,${blob},${page.path}`],
|
||
undefined,
|
||
env,
|
||
);
|
||
}
|
||
const tree = (await git(repository, ['write-tree'], undefined, env)).trim();
|
||
const message = String(body.message ?? 'Update the generated project pages').slice(0, 200);
|
||
const commit = (
|
||
await git(repository, ['commit-tree', tree, '-p', parent, '-m', message], undefined, env)
|
||
).trim();
|
||
// Only advance the branch if nobody else moved it: a concurrent push must not be lost.
|
||
await git(repository, ['update-ref', `refs/heads/${branch}`, commit, parent]);
|
||
json(response, 200, { commit, branch, files: pages.map(page => page.path) });
|
||
} finally {
|
||
await unlink(index).catch(() => undefined);
|
||
}
|
||
} else if (url.pathname.startsWith('/code/')) {
|
||
// Read-only code views for the Copilot. The repository is never checked out: every call
|
||
// reads the object store, and a credential or operator file is refused before it is read.
|
||
await stat(repository);
|
||
const reader = codeReader({
|
||
run: (args, timeoutMs = 10_000) =>
|
||
checkedCommand({
|
||
executable: 'git',
|
||
args: ['-c', 'core.hooksPath=/dev/null', ...args],
|
||
cwd: repository,
|
||
env: gitEnv(),
|
||
timeoutMs,
|
||
}),
|
||
});
|
||
const ref = typeof body.ref === 'string' && body.ref ? body.ref : 'main';
|
||
const text = (value: unknown) => (typeof value === 'string' ? value : undefined);
|
||
const number = (value: unknown) => (typeof value === 'number' && Number.isInteger(value) ? value : undefined);
|
||
if (url.pathname === '/code/tree')
|
||
json(response, 200, await reader.tree({ ref, path: text(body.path), depth: number(body.depth) }));
|
||
else if (url.pathname === '/code/blob')
|
||
json(
|
||
response,
|
||
200,
|
||
await reader.blob({
|
||
ref,
|
||
path: String(body.path ?? ''),
|
||
from: number(body.from),
|
||
to: number(body.to),
|
||
}),
|
||
);
|
||
else if (url.pathname === '/code/search')
|
||
json(
|
||
response,
|
||
200,
|
||
await reader.search({ ref, pattern: String(body.pattern ?? ''), glob: text(body.glob) }),
|
||
);
|
||
else if (url.pathname === '/code/log')
|
||
json(response, 200, await reader.log({ ref, since: text(body.since), limit: number(body.limit) }));
|
||
else json(response, 200, await reader.show({ sha: String(body.sha ?? ''), path: text(body.path) }));
|
||
} else if (url.pathname === '/refs') {
|
||
await stat(repository);
|
||
json(response, 200, { branches: await branchesIn(repository, String(body.prefix ?? '')) });
|
||
} else {
|
||
await stat(repository);
|
||
if (body.refPrefix !== undefined && (typeof body.refPrefix !== 'string' || !REF_PREFIX.test(body.refPrefix)))
|
||
throw new RuntimeError('INVALID_REF', 'refPrefix must look like refs/heads/<name>/');
|
||
if (body.permission !== 'read' && body.permission !== 'write')
|
||
throw new RuntimeError('INVALID_PERMISSION', 'Git permission must be read or write');
|
||
const ttl = Number(body.ttlSeconds ?? 300);
|
||
if (!Number.isInteger(ttl) || ttl < 30 || ttl > 900)
|
||
throw new RuntimeError('INVALID_TTL', 'Token TTL must be 30..900 seconds');
|
||
const issued = Math.floor(Date.now() / 1000);
|
||
const claims: GitClaims = {
|
||
aud: 'wishforge-git',
|
||
tenantId,
|
||
projectId,
|
||
permission: body.permission,
|
||
iat: issued,
|
||
exp: issued + ttl,
|
||
...(typeof body.runId === 'string' ? { runId: safeIdentifier(body.runId) } : {}),
|
||
...(typeof body.refPrefix === 'string' ? { refPrefix: body.refPrefix } : {}),
|
||
};
|
||
json(response, 200, {
|
||
token: issueGitToken(claims, config.tokenSecret),
|
||
expiresAt: new Date(claims.exp * 1000).toISOString(),
|
||
});
|
||
}
|
||
return;
|
||
}
|
||
const match =
|
||
/^\/git\/([A-Za-z0-9][A-Za-z0-9_-]{0,100})\/([A-Za-z0-9][A-Za-z0-9_-]{0,100})\.git\/(info\/refs|git-upload-pack|git-receive-pack)$/.exec(
|
||
url.pathname,
|
||
);
|
||
if (!match) {
|
||
json(response, 404, { error: 'NOT_FOUND' });
|
||
return;
|
||
}
|
||
const tenantId = match[1]!,
|
||
projectId = match[2]!,
|
||
operation = match[3]!;
|
||
const service = operation === 'info/refs' ? url.searchParams.get('service') : operation;
|
||
if (
|
||
!['git-upload-pack', 'git-receive-pack'].includes(service ?? '') ||
|
||
(operation === 'info/refs' ? request.method !== 'GET' : request.method !== 'POST')
|
||
) {
|
||
json(response, 400, { error: 'INVALID_GIT_OPERATION' });
|
||
return;
|
||
}
|
||
const authorization = request.headers.authorization ?? '';
|
||
if (!authorization.startsWith('Bearer ')) {
|
||
response.setHeader('www-authenticate', 'Bearer realm="wishforge-git"');
|
||
json(response, 401, { error: 'UNAUTHORIZED' });
|
||
return;
|
||
}
|
||
const claims = verifyGitToken(authorization.slice(7), config.tokenSecret, {
|
||
tenantId,
|
||
projectId,
|
||
write: service === 'git-receive-pack',
|
||
});
|
||
const repository = await location(tenantId, projectId);
|
||
await stat(repository);
|
||
const child = spawn('git', ['http-backend'], {
|
||
env: {
|
||
...gitEnv(),
|
||
// Pushes run the platform hook (and nothing a repository could carry itself).
|
||
GIT_CONFIG_COUNT: '1',
|
||
GIT_CONFIG_KEY_0: 'core.hooksPath',
|
||
GIT_CONFIG_VALUE_0: hooks,
|
||
...(claims.refPrefix ? { WISHFORGE_REF_PREFIX: claims.refPrefix } : {}),
|
||
GIT_PROJECT_ROOT: root,
|
||
GIT_HTTP_EXPORT_ALL: '1',
|
||
PATH_INFO: `/${tenantId}/${projectId}.git/${operation}`,
|
||
REQUEST_METHOD: request.method!,
|
||
QUERY_STRING: url.searchParams.toString(),
|
||
CONTENT_TYPE: String(request.headers['content-type'] ?? ''),
|
||
CONTENT_LENGTH: String(request.headers['content-length'] ?? ''),
|
||
// Git gzips larger upload-pack negotiations; http-backend inflates them only when told.
|
||
HTTP_CONTENT_ENCODING: String(request.headers['content-encoding'] ?? ''),
|
||
REMOTE_USER: claims.runId ?? 'wishforge',
|
||
REMOTE_ADDR: request.socket.remoteAddress ?? '',
|
||
HTTP_GIT_PROTOCOL: String(request.headers['git-protocol'] ?? ''),
|
||
},
|
||
stdio: ['pipe', 'pipe', 'pipe'],
|
||
shell: false,
|
||
});
|
||
let headers = Buffer.alloc(0),
|
||
headersSent = false,
|
||
bytes = 0;
|
||
const timer = setTimeout(() => child.kill('SIGKILL'), 300_000);
|
||
request.on('data', chunk => {
|
||
bytes += chunk.length;
|
||
if (bytes > (config.maxPackBytes ?? 512 * 1024 * 1024)) {
|
||
child.kill('SIGKILL');
|
||
request.destroy();
|
||
}
|
||
});
|
||
request.on('aborted', () => child.kill('SIGKILL'));
|
||
response.on('close', () => {
|
||
if (!response.writableEnded) child.kill('SIGKILL');
|
||
});
|
||
child.stdout.on('data', (chunk: Buffer) => {
|
||
if (headersSent) {
|
||
response.write(chunk);
|
||
return;
|
||
}
|
||
headers = Buffer.concat([headers, chunk]);
|
||
const end = headers.indexOf('\r\n\r\n');
|
||
if (end === -1) {
|
||
if (headers.length > 64 * 1024) child.kill('SIGKILL');
|
||
return;
|
||
}
|
||
let status = 200;
|
||
const parsed: Record<string, string> = {};
|
||
for (const line of headers.subarray(0, end).toString().split('\r\n')) {
|
||
const separator = line.indexOf(':');
|
||
if (separator < 1) continue;
|
||
const name = line.slice(0, separator).toLowerCase(),
|
||
value = line.slice(separator + 1).trim();
|
||
if (name === 'status') status = parseInt(value, 10);
|
||
else if (['content-type', 'cache-control', 'expires', 'pragma', 'content-encoding'].includes(name))
|
||
parsed[name] = value;
|
||
}
|
||
response.writeHead(status, parsed);
|
||
headersSent = true;
|
||
response.write(headers.subarray(end + 4));
|
||
headers = Buffer.alloc(0);
|
||
});
|
||
child.stderr.on('data', () => undefined);
|
||
child.stdin.on('error', () => undefined);
|
||
child.on('error', () => {
|
||
clearTimeout(timer);
|
||
if (!headersSent) json(response, 503, { error: 'GIT_BACKEND_UNAVAILABLE' });
|
||
else response.destroy();
|
||
});
|
||
child.on('close', code => {
|
||
clearTimeout(timer);
|
||
if (!headersSent) json(response, code === 0 ? 502 : 503, { error: 'GIT_BACKEND_FAILED' });
|
||
else response.end();
|
||
});
|
||
request.pipe(child.stdin);
|
||
})().catch(error => {
|
||
if (response.headersSent) {
|
||
response.destroy();
|
||
return;
|
||
}
|
||
const code = error instanceof RuntimeError ? error.code : 'GIT_SERVICE_ERROR';
|
||
// A refused read is worth a line: the caller only sees "cannot read", the operator needs why.
|
||
if (request.url?.startsWith('/code/'))
|
||
console.warn(
|
||
`code ${request.url} refused: ${code} ${error instanceof Error ? error.message : ''}`.slice(0, 240),
|
||
);
|
||
const status =
|
||
code === 'UNAUTHORIZED'
|
||
? 401
|
||
: code === 'NOT_FOUND'
|
||
? 404
|
||
: code === 'REPOSITORY_BUSY'
|
||
? 409
|
||
: code === 'PAYLOAD_TOO_LARGE'
|
||
? 413
|
||
: 400;
|
||
json(response, status, { error: code });
|
||
});
|
||
});
|
||
return server;
|
||
}
|