import { ssoUser } from "./auth.js"; import { json } from "./platform.js"; import { readLimitedText } from "./request.js"; export async function syncRequest(request, env, limit = 1e6) { if (request.method !== "POST") return { response: json({ error: "method not allowed" }, 405) }; if (request.headers.get("origin") !== new URL(request.url).origin) return { response: json({ error: "invalid origin" }, 403) }; if (!request.headers.get("Content-Type")?.startsWith("application/json")) return { response: json({ error: "expected json" }, 415) }; const user = await ssoUser(request, env); if (!user) return { response: json({ error: "sign in to sync" }, 401) }; let body; try { body = JSON.parse(await readLimitedText(request, limit)); } catch (error) { return { response: json( { error: error.status === 413 ? "request too large" : "invalid json" }, error.status === 413 ? 413 : 400 ) }; } if (body?.owner !== user.id) return { response: json({ error: "account changed; reload to continue" }, 409) }; return { user, body }; }