azion
Version:
Azion Packages for Edge Computing.
2 lines (1 loc) • 2.25 kB
JavaScript
;Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const h=(a,r)=>{const t=a.trim().split(";").reduce((e,s)=>{const[i,c]=s.split("=");let o=i;if(o=i.trim(),o){let n=decodeURIComponent(c).trim();n.startsWith('"')&&n.endsWith('"')&&(n=n.slice(1,-1)),e[o]=n}return e},{});return r?t[r.trim()]:t},u=(a,r,t)=>{if(!(a instanceof Request))return;const e=a.headers.get("Cookie");if(!e)return typeof r=="string"?void 0:{};const s=t?`__${t.charAt(0).toUpperCase()+t.slice(1)}-${r}`:r;return h(e,s)},f=(a,r,t,e)=>{if(!(a instanceof Response))throw new Error("response is not an instance of Response");const s=m(r,e);(e?.prefix==="host"||e?.prefix==="secure")&&(e={...e,secure:!0});let i=`${s}=${t}`;if(e?.domain&&(i+=`; Domain=${e.domain}`),e?.expires){if(e?.expires<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:u,setCookie:f};exports.default=w;exports.getCookie=u;exports.setCookie=f;