/** * The largest body each route may send. * * The entry point sets one adapter-wide ceiling, high enough for a 100 MiB * import. That ceiling applied to every route as well, so a client could make * the process buffer a hundred megabytes for a log-in. The adapter also applies * its limit while the body is read, which is already too late: the bytes are in * memory by then, and the adapter's own error reaches the handler as a rejected * read rather than a refusal. * * So the policy lives here and it is deny-by-default: a route carries a larger * allowance only when it is named, and a route added later inherits the small * one. `server.js` applies it before the adapter or the app sees a request. * * Plain JavaScript, because `server.js` loads it before the built app. */ /** Anything not named below: a form post, a credential call, a settings save. */ export const DEFAULT_BODY_BYTES = 2 * 1024 * 1024; /** * A route that carries a document: a scene or note save, a note image, a * note-type template, a guest save. The largest of those is the save ceiling in * `src/lib/body-limits.ts`, multiplied by the four bytes a UTF-16 unit can take. */ export const PROSE_BODY_BYTES = 12 * 1024 * 1024; /** An import: the 100 MiB file limit plus room for its multipart headers. */ export const IMPORT_BODY_BYTES = 101 * 1024 * 1024; /** * First match wins, and a rule names one path together with everything below it. * No rule here is a prefix of another, so the order carries no meaning. */ const RULES = [ { path: '/api/import', bytes: IMPORT_BODY_BYTES }, { path: '/api/scenes', bytes: PROSE_BODY_BYTES }, { path: '/api/notes', bytes: PROSE_BODY_BYTES }, { path: '/api/note-types', bytes: PROSE_BODY_BYTES }, { path: '/s', bytes: PROSE_BODY_BYTES } ]; /** * The body allowance for a request target, in bytes. * * The target may carry a query string, which is not part of the path. A target * that only almost names a rule, such as `/api/import-draft`, takes the default: * a mistake here gives a route less room than it needs, never more. */ export function bodyLimitFor(target) { if (typeof target !== 'string') return DEFAULT_BODY_BYTES; const end = target.indexOf('?'); const pathname = end === -1 ? target : target.slice(0, end); for (const rule of RULES) { if (pathname === rule.path || pathname.startsWith(`${rule.path}/`)) return rule.bytes; } return DEFAULT_BODY_BYTES; }