Fix: Use robust JSON helper for header handling

This commit is contained in:
2025-10-02 11:54:35 -07:00
parent daa6ec7681
commit 3082970ced

View File

@@ -1,8 +1,12 @@
const json = (d, o) => new Response(JSON.stringify(d), { ...o, headers: { 'Content-Type': 'application/json', ...(o?.headers || {}) } }); const json = (d, o = {}) => {
const h = new Headers(o.headers);
h.set('Content-Type', 'application/json');
return new Response(JSON.stringify(d), { ...o, headers: h });
};
const cookie = c => (c.match(/auth_user=([^;]+)/)?.[1] || null); const cookie = c => (c.match(/auth_user=([^;]+)/)?.[1] || null);
const hash = c => (c.match(/auth_hash=([^;]+)/)?.[1] || null); const hash = c => (c.match(/auth_hash=([^;]+)/)?.[1] || null);
const tsEq=(a,b)=>{if(!a||!b)return!1;let d=a.length^b.length;for(let i=0;i<a.length;i++)d|=a.charCodeAt(i)^b.charCodeAt(i);return d===0}; const tsEq=(a,b)=>{if(!a||!b)return!1;let d=a.length^b.length;for(let i=0;i<a.length;i++)d|=a.charCodeAt(i)^b.charCodeAt(i);return d===0};
const clear = ()=>{const o=`Domain=.speech.capital; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT`;return {'Set-Cookie':`auth_user=; ${o}`,'Set-Cookie':`auth_hash=; ${o}`}}; const clear = ()=>{const o=`Domain=.speech.capital; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT`;const h=new Headers();h.append('Set-Cookie',`auth_user=; ${o}`);h.append('Set-Cookie',`auth_hash=; ${o}`);return h};
export async function onRequest({ request, env }) { export async function onRequest({ request, env }) {
const c = request.headers.get('Cookie') || ''; const c = request.headers.get('Cookie') || '';