export function setCookie(res, name, value, options = {}) { const cookieParts = [ `${encodeURIComponent(name)}=${encodeURIComponent(value)}`, ]; if (options.expires) { cookieParts.push(`Expires=${options.expires.toUTCString()}`); } if (options.maxAge !== undefined) { cookieParts.push(`Max-Age=${options.maxAge}`); } if (options.path) { cookieParts.push(`Path=${options.path}`); } if (options.domain) { cookieParts.push(`Domain=${options.domain}`); } if (options.secure) { cookieParts.push("Secure"); } if (options.httpOnly) { cookieParts.push("HttpOnly"); } res.setHeader("Set-Cookie", cookieParts.join("; ")); } export function getCookie(req, name) { const cookieHeader = req.headers.cookie; if (!cookieHeader) return null; const cookies = cookieHeader .split(";") .reduce((acc, cookie) => { const [key, val] = cookie.trim().split("=").map(decodeURIComponent); acc[key] = val; return acc; }, {}); return cookies[name] || null; } export function updateCookie(res, name, value, options = {}) { setCookie(res, name, value, options); } export function deleteCookie(res, name, options = {}) { setCookie(res, name, "", Object.assign(Object.assign({}, options), { expires: new Date(0), maxAge: 0 })); }