UNPKG

1.23 kBJavaScriptView Raw
1// src/cookie.ts
2var g = new Set([
3 "domain",
4 "path",
5 "max-age",
6 "expires",
7 "samesite",
8 "secure",
9 "httponly"
10]);
11function u(a) {
12 let r = {}, e, t, n = 0, m = a.split(/;\s*/g), s, i;
13 for (; n < m.length; n++)
14 if (t = m[n], e = t.indexOf("="), ~e) {
15 if (s = t.substring(0, e++).trim(), i = t.substring(e).trim(), i[0] === '"' && (i = i.substring(1, i.length - 1)), ~i.indexOf("%"))
16 try {
17 i = decodeURIComponent(i);
18 } catch (f) {
19 }
20 g.has(t = s.toLowerCase()) ? t === "expires" ? r.expires = new Date(i) : t === "max-age" ? r.maxage = +i : r[t] = i : r[s] = i;
21 } else
22 (s = t.trim().toLowerCase()) && (s === "httponly" || s === "secure") && (r[s] = !0);
23 return r;
24}
25function l(a, r, e = {}) {
26 let t = a + "=" + encodeURIComponent(r);
27 return e.expires && (t += "; Expires=" + new Date(e.expires).toUTCString()), e.maxage != null && e.maxage >= 0 && (t += "; Max-Age=" + (e.maxage | 0)), e.domain && (t += "; Domain=" + e.domain), e.path && (t += "; Path=" + e.path), e.samesite && (t += "; SameSite=" + e.samesite), (e.secure || e.samesite === "None") && (t += "; Secure"), e.httponly && (t += "; HttpOnly"), t;
28}
29export {
30 u as parse,
31 l as stringify
32};