gitoria
All repositories: gitoria
5.7 KB
// lib/api-helpers.hl — the plumbing of the function routes (lib/api.hl): JSON / Markdown / plain answers, redirects,// the guard of the internal ssh routes, the repo of a request's host, where a login may return to. Statics only.import { Response } from 'hl:http1'import { NL, scheme, hostPort, slugOfHost } from './util.hl'import { slugOfRequest } from './orgs.hl'import { slugError, fullNameError } from './repos-helpers.hl'import { repoBySlug } from './repos.hl'import { sshSecret, sshEnabled } from './sshkeys.hl'static jsonHeaders = { 'Content-Type' = 'application/json; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' }static reply = (status, value) => {return new Response(JSON.stringify(value), { status = status headers = jsonHeaders })}static fail = (status, message) => { return reply(status, { error = message }) }// THE APPS API'S ERROR SHAPE (docs/apps-api.md §6): { error: { code, message, details } }static apiError = (status, code, message, details) => {return reply(status, { error = { code = code message = message details = details } })}// an answer { status, body } of a topic function (lib/reviews.hl) as the Responsestatic answerOf = (r) => { return reply(r.status, r.body) }// THE JSON OBJECT OF AN APPS API REQUEST (docs/apps-api.md §6): { json } or { error } (a Response: 413 over 1 MB, 400 invalid_json).// DOCS/GAP: JSON.parse of broken JSON is a located error that ends the request (500), there is no safe parse; only a body that is not even shaped like an object gets the 400 here.static maxRequest = 1048576static jsonOfRequest = (req) => {if (req.bytes != null && req.bytes.length > maxRequest) { return { error = apiError(413, 'too_large', 'the request body is over 1 MB', null) } }t = req.body == null ? '' : req.body.trim()if (!(t.startsWith('{') && t.endsWith('}'))) { return { error = apiError(400, 'invalid_json', 'the body must be a JSON object', null) } }return { json = JSON.parse(t) }}// THE ROUTE PATTERNS OF AN APPS API ROUTE (gitoria#41/#42; docs/apps-api.md §0, the API base): `path` (e.g. '/apps/token', '/pulls/:n') exists on the// repo's own address (`/api/…`) and on an organization's address in both spellings: `/api/<repo>/…` and `/<repo>/api/…`. project.hl lists a route once:// for (p of apiPatterns('/apps/token')) { { pattern = p function = appToken } }static apiPatterns = (path) => {return ['/api' + path, '/api/:orepo' + path, '/:orepo/api' + path]}// THE MARKDOWN READ VIEW: `Accept: text/markdown` (for LLM readers)static wantsMarkdown = (req) => {a = req.headers['accept']return a != null && hlTypeName(a) == 'String' && a.includes('text/markdown')}static markdownReply = (text) => {return new Response(text, { status = 200 headers = { 'Content-Type' = 'text/markdown; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' } })}// a plain-text answer of the internal ssh routesstatic textReply = (status, text) => {return new Response(text, { status = status headers = { 'Content-Type' = 'text/plain; charset=utf-8' 'Cache-Control' = 'no-store' 'X-Content-Type-Options' = 'nosniff' } })}static goTo = (where) => {return new Response('redirect', { status = 302 headers = { 'Location' = where 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' } })}// THE INTERNAL SSH ROUTES' GUARD (docker/sshd asks them): null when the caller may ask, else the refusing answer. Without// GITORIA_SSH_SECRET they do not exist (404); every call needs `X-Gitoria-Secret` = that secret, and a request that came// through the public proxy (it carries X-Forwarded-For / X-Real-IP) is refused.static sshGuard = (req) => {if (!sshEnabled) { return textReply(404, 'not found' + NL) }if (req.method != 'GET') { return textReply(405, 'GET only' + NL) }if (req.headers['x-forwarded-for'] != null || req.headers['x-real-ip'] != null) { return textReply(403, 'internal only' + NL) }given = req.headers['x-gitoria-secret']if (given == null || hlTypeName(given) != 'String' || given != sshSecret) { return textReply(403, 'wrong secret' + NL) }return null}// the repo of a request (its host, and inside an organization the first path segment `:orepo`), or nullstatic repoOfRequest = (route, req) => {h = req.headers['host']orepo = route.params != null && route.params.orepo != null ? route.params.orepo : ''slug = slugOfRequest(h == null ? '' : h.split(':')[0], orepo)return slug == '' || fullNameError(slug) != null ? null : repoBySlug(slug)}// BACK TO THE PAGE after a login: /login.js puts `?next=` into the button's return URL at the click. Only a same-origin PATH// goes (one `/`, URL-safe characters, ≤ 500) — or a full URL of THIS system: <scheme>://<label>.<host>// with a valid, non-reserved repo label and such a path. Anything else → `/`.static nextChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~/?&=%+,;@!$()*:'static nextPath = (want) => {if (want == null || hlTypeName(want) != 'String' || want == '' || want.length > 500) { return '/' }if (want.slice(0, 1) != '/' || want.slice(0, 2) == '//' || want.slice(0, 7) == '/login/') { return '/' }let i = 0while (i < want.length) {if (!nextChars.includes(want[i])) { return '/' }i = i + 1}return want}static safeNext = (want) => {if (want == null || hlTypeName(want) != 'String' || want.length > 500) { return '/' }prefix = scheme + '://'if (!want.startsWith(prefix)) { return nextPath(want) }rest = want.slice(prefix.length)slash = rest.indexOf('/')hp = slash < 0 ? rest : rest.slice(0, slash)path = slash < 0 ? '/' : rest.slice(slash)dot = hp.indexOf('.')if (dot < 1 || hp.slice(dot + 1) != hostPort || slugError(hp.slice(0, dot)) != null) { return '/' }p = nextPath(path)if (p == '/' && path != '/') { return '/' }return prefix + hp + p}
Branches
- f1-broken-json
- f2-api-bugs
- gp-gate-ports
- heap-revert
- hl25-records
- ident-chooser-selector
- mainmain branch
- p51-public-pr-reads
- records-1112
- s25-no-session-on-login-error
- v59
- w52-person-approval
- w52b-approval-followup
- w52c-header-phone
- w53-pulls-speed-pr
- w54-repo-rename-pr
- w55-rename-org
- w56-owner-members
- w62-pr-close
- w63-force-push
- w64-org-slash
Latest commits
- 2175e48clogmre
- 40eb3ca1gitoria#38: review package, logmre
- 06a5a50egitoria#38: deploy b8252ca, logmre
- b8252ca0gitoria#46: W46b report, logmre
- 50093aafgitoria#46: statuses keyed by app + context (merge w46b-keyed)mre
- dd0267edgitoria#46: fix a label in the keyed gatemre
- b1dcb580gitoria#46: statuses keyed by app + context, required = app + context, old rows still readmre
- 2287363egitoria#47: W47 report, logmre
- 9f502db1gitoria#46: W46b briefmre
- 9678a8f3gitoria#46: contract v1.4, statuses keyed by app + contextmre
- bdcbbaa9gitoria#47: rate limits, status read route, statuses + required on the pull (merge w47-limits)mre
- 772c401fgitoria#45: W45 report, logmre
- b6b793afgitoria#45: PR page with reviews, threads, conversation, Review again (merge w45-pr-page)mre
- fb9e7554gitoria#45: browser gate: a /pulls title links to the PR pagemre
- f7198131gitoria#45: tests/pr-page.mjs gatemre
- 483141d0gitoria#45: Settings shows the Apps heading only to the ownermre
- e2eff8f6gitoria#45: the PR page /pulls/N with reviews, threads, verdicts, conversation, comment, reply, resolve and Review againmre
- 7a602839gitoria#45: raiseEvent can send to one app only; people's comments, replies and resolving in reviews.hlmre
- 5de7f578gitoria#47: rate limits per app (429, Retry-After), status read route, statuses and required on the pull, one JSON check for every write routemre
- 19edc1f5gitoria#44: contract v1.3.1, what outdated meansmre