| 83 | * @param headers `x-codegraph-ui` and, for a body-carrying method, `content-type` |
| 84 | */ |
| 85 | export function isWriteRequest( |
| 86 | pathname: string, |
| 87 | headers: { marker: string | undefined; contentType: string | undefined } |
| 88 | ): { ok: true } | { ok: false; reason: string } { |
| 89 | // Writes live under /api/ and nowhere else. The static side of this server |
| 90 | // serves a built bundle; there is nothing there to POST to. |
| 91 | if (pathname !== '/api' && !pathname.startsWith('/api/')) { |
| 92 | return { ok: false, reason: 'Only the /api/ endpoints accept writes.' }; |
| 93 | } |
| 94 | if (headers.marker === undefined || headers.marker.trim() === '') { |
| 95 | return { ok: false, reason: `A write must carry the ${WRITE_HEADER} header.` }; |
| 96 | } |
| 97 | if (headers.contentType !== undefined) { |
| 98 | const type = headers.contentType.split(';')[0]?.trim().toLowerCase(); |
| 99 | if (type !== '' && type !== WRITE_CONTENT_TYPE) { |
| 100 | return { ok: false, reason: `A write body must be ${WRITE_CONTENT_TYPE}.` }; |
| 101 | } |
| 102 | } |
| 103 | return { ok: true }; |
| 104 | } |
| 105 | |
| 106 | interface HostParts { |
| 107 | hostname: string; |