// post.mjs — the editor's same-origin form POST, which answers JSON. // A request that has not answered in POST_TIMEOUT is aborted, so one hung // autosave never blocks every later one with the status stuck at // "Saving…": the saver takes the abort as offline and retries (review // round 4). An aborted request may still have landed; every draft write // is version-checked, so the retry meets a 409 carrying the text it sent, // which the saver recognises as its own (saver.mjs, stale()). A // keepalive POST (the closing tab's) has no page left to time it out. export const POST_TIMEOUT = 10000 export function poster (fetchImpl = (url, init) => fetch(url, init), timeout = POST_TIMEOUT) { return async (url, fields, { keepalive = false } = {}) => { const body = new URLSearchParams() for (const [name, value] of Object.entries(fields)) { for (const one of [].concat(value)) body.append(name, one) } const controller = keepalive ? null : new AbortController() const timer = controller ? setTimeout(() => controller.abort(), timeout) : null try { const response = await fetchImpl(url, { method: 'POST', body, credentials: 'same-origin', keepalive, headers: { Accept: 'application/json' }, signal: controller?.signal }) let json = null try { json = await response.json() } catch {} return { status: response.status, json } } finally { if (timer !== null) clearTimeout(timer) } } } export const post = poster()