export function get_ws_host() { const protocol = document.location.protocol === "https:" ? "wss:" : "ws:"; return `${protocol}//${document.location.host}`; } export type JSONResult = { ok: true; status: 0; data: T; } | { ok: false; status: Exclude; error: string; }; function gen_response(d: JSONResult, status = 200) { if (d.status !== 0) { status = (d.status >= 400 && d.status < 600) ? d.status : 400; } return new Response(JSON.stringify(d), { status, headers: { "Content-Type": "application/json" }, }); } export function return_error( status: Exclude, error: string, ) { return gen_response({ ok: false, status, error }); } export function return_data(data: T, status = 200) { return gen_response({ ok: true, status: 0, data }, status); }