Something went wrong. Try again.
forked from nandi.uk/gitproxy
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155import { createServer } from 'node:http'import { readFile } from 'node:fs/promises'import { extname, join } from 'node:path'import { fileURLToPath } from 'node:url'
import { loadRepoView } from './view.js'
const __dirname = fileURLToPath(new URL('.', import.meta.url))const publicDir = join(__dirname, 'public')const assetCache = new Map()
const CONTENT_TYPES = { '.html': 'text/html; charset=utf-8', '.js': 'application/javascript; charset=utf-8', '.css': 'text/css; charset=utf-8', '.svg': 'image/svg+xml', '.png': 'image/png', '.ico': 'image/x-icon',}
function send(res, status, body, headers = {}) { res.writeHead(status, { 'content-type': 'text/plain; charset=utf-8', ...headers, }) res.end(body)}
function sendJson(res, status, payload) { res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store', }) res.end(`${JSON.stringify(payload, null, 2)}\n`)}
async function readAsset(name) { const filePath = join(publicDir, name) const existing = assetCache.get(filePath) if (existing) return existing const task = readFile(filePath) assetCache.set(filePath, task) try { return await task } catch (error) { assetCache.delete(filePath) throw error }}
function parseRepoRoute(pathname) { if (!pathname.startsWith('/r/')) return null const rest = pathname.slice(3) const parts = rest.split('/').filter(Boolean) if (parts.length < 2) return null return { handle: decodeURIComponent(parts[0]), repo: parts.slice(1).map((part) => decodeURIComponent(part)).join('/'), }}
function parseRequestTarget(url) { const route = parseRepoRoute(url.pathname) if (route) return route const handle = url.searchParams.get('handle')?.trim() const repo = url.searchParams.get('repo')?.trim() if (handle && repo) return { handle, repo } return null}
async function serveStatic(req, res, pathname) { const assetName = pathname === '/' ? 'index.html' : pathname.slice(1) if (!assetName || assetName.includes('..')) return false
try { const file = await readAsset(assetName) res.writeHead(200, { 'content-type': CONTENT_TYPES[extname(assetName)] ?? 'application/octet-stream', 'cache-control': assetName === 'index.html' ? 'no-store' : 'public, max-age=3600', }) res.end(file) return true } catch { return false }}
function errorResponse(res, error) { const message = error instanceof Error ? error.message : String(error) sendJson(res, 500, { error: message })}
const server = createServer(async (req, res) => { try { const url = new URL(req.url ?? '/', 'http://127.0.0.1')
if (req.method === 'GET' && url.pathname === '/healthz') { send(res, 200, 'ok\n') return }
if (req.method === 'GET' && url.pathname === '/api/repo') { const target = parseRequestTarget(url) if (!target) { sendJson(res, 400, { error: 'handle and repo query parameters are required' }) return }
const commit = url.searchParams.get('commit')?.trim() || null const path = url.searchParams.get('path')?.trim() || null const data = await loadRepoView({ handle: target.handle, repoId: target.repo, commit, path, }) sendJson(res, 200, data) return }
if (req.method === 'GET' && (url.pathname === '/' || url.pathname.startsWith('/r/'))) { const handled = await serveStatic(req, res, '/index.html') if (!handled) { send(res, 500, 'missing appview public assets\n') } return }
if (req.method === 'GET' && (url.pathname === '/app.js' || url.pathname === '/styles.css')) { const handled = await serveStatic(req, res, url.pathname) if (!handled) { send(res, 404, 'not found\n') } return }
if (req.method === 'GET' && url.pathname.startsWith('/assets/')) { const handled = await serveStatic(req, res, url.pathname) if (!handled) { send(res, 404, 'not found\n') } return }
send(res, 404, 'not found\n') } catch (error) { errorResponse(res, error) }})
const port = Number(process.env.PORT ?? 8787)server.listen(port, () => { process.stdout.write(`appview listening on http://127.0.0.1:${port}\n`)})