UNPKG

azion

Version:

Azion Packages for Edge Computing.

76 lines (75 loc) 2.67 kB
const f = (a, r) => { const t = a.trim().split(";").reduce( (e, n) => { const [i, u] = n.split("="); let o = i; if (o = i.trim(), o) { let s = decodeURIComponent(u).trim(); s.startsWith('"') && s.endsWith('"') && (s = s.slice(1, -1)), e[o] = s; } return e; }, {} ); return r ? t[r.trim()] : t; }, c = (a, r, t) => { if (!(a instanceof Request)) return; const e = a.headers.get("Cookie"); if (!e) return typeof r == "string" ? void 0 : {}; const n = t ? `__${t.charAt(0).toUpperCase() + t.slice(1)}-${r}` : r; return f(e, n); }, h = (a, r, t, e) => { if (!(a instanceof Response)) throw new Error("response is not an instance of Response"); const n = m(r, e); (e?.prefix === "host" || e?.prefix === "secure") && (e = { ...e, secure: !0 }); let i = `${n}=${t}`; if (e?.domain && (i += `; Domain=${e.domain}`), e?.expires) { if (e?.expires < /* @__PURE__ */ new Date()) throw new Error("expires option must be a future date"); if (e.expires.getTime() - Date.now() > 3456e7) throw new Error("expires option must be less than or equal to (400 days) 34560000"); i += `; Expires=${e.expires.toUTCString()}`; } if (e?.httpOnly && (i += "; HttpOnly"), e?.maxAge) { if (e?.maxAge < 0) throw new Error("maxAge must be a positive number"); if (e?.maxAge > 3456e4) throw new Error("maxAge must be less than or equal to (400 days) 34560000"); i += `; Max-Age=${e.maxAge}`; } if (e?.path && (i += `; Path=${e.path}`), e?.sameSite) { if (e.sameSite === "None" && !e.secure) throw new Error("secure option must be true when using SameSite=None"); if (e.sameSite === "None" && e.secure && e.partitioned) throw new Error("Partitioned cookies cannot be used with SameSite=None and Secure"); i += `; SameSite=${e.sameSite}`; } return e?.secure && (i += "; Secure"), e?.partitioned && (i += "; Partitioned"), a.headers.append("Set-Cookie", i), a; }, m = (a, r) => { let t = a; if (r?.prefix === "secure") { if (r?.secure === !1) throw new Error("secure option must be true when using secure prefix"); t = `__Secure-${t}`; } if (r?.prefix === "host") { if (r?.secure === !1) throw new Error("secure option must be true when using host prefix"); if (r?.domain) throw new Error("domain option must not be set when using host prefix"); if (r?.path !== "/") throw new Error("path option must be set to / when using host prefix"); t = `__Host-${t}`; } return t; }, w = { getCookie: c, setCookie: h }; export { w as default, c as getCookie, h as setCookie };